Skip to content

Unbound compile_blk recursion on flat statement lists #402

Description

@Sahilgill24

Project version

0.7.2

Project

compiler

What happened?

compile_blk recursively processes every statement in a block, making stack usage proportional to statement count rather than syntactic nesting depth

At src/compile/mod.rs:227, compile_blk constructs the block's pair chain recursively. The recursive calls at lines 244 and 249 introduce approximately one stack frame per statement so a block containing N sequential statements produces O(N) call-stack depth, even though the source itself has no nesting.

This is specific to the code-generation phase. Parsing and type-checking process the same flat statement list iteratively and do not exhibit the same stack growth. As a result, an otherwise ordinary function containing many sequential statements can cause the compiler to exhaust its 8 MB stack and abort with a stack overflow.

It is not fixed by #401 as well and

Minimal reproduction steps

The code stack overflow's at around 3000~4000 let statements and compiles fine for 2000.
try using the python script below to generate a file for it and run it using simc

# python script
NUM_LETS = 5000
OUTPUT_FILE = "bug.simf"

with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
    f.write("fn main() {\n")

    for i in range(NUM_LETS):
        f.write(f"    let x{i}: u32 = {i};\n")

    f.write("    assert!(jet::eq_32(x0, 0));\n")
    f.write("}\n")

print(f"Generated {NUM_LETS} sequential let bindings in {OUTPUT_FILE}")

The above script generates the bug.simf file with 5000 let statements which produces the following error on compilation.

error

thread 'main' (7347361) has overflowed its stack
fatal runtime error: stack overflow, aborting
[1]    38741 abort      ./target/debug/simc ./bug.simf

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions