Skip to content

Stage-A: no tail-call rewrite, deep self-recursion overflows the stack #951

Closed

Description

@godofecht

The Python host rewrites a self tail call into a loop. Stage-A emits plain recursion.

function down_to(n: i32) -> i32 {
    if n <= 0 { return 0 } else { return down_to(n - 1) }
}
function main() -> i32 { return down_to(1000000) }

Observed: return down_to((n - 1)); in the C, and the binary segfaults (signal 11) at depth 1,000,000.

Reproduce with the Stage-A compiler built from compiler/bootstrap/flowc_stage_a.c:

cc -O2 -o compiler/build/flowc_bootstrap compiler/bootstrap/flowc_stage_a.c
FLOWC_BUNDLE=1 FLOWC_DIR=. FLOWC_IN=repro.flow FLOWC_OUT=repro.c compiler/build/flowc_bootstrap
cc -O0 -o repro repro.c -lm && ./repro; echo $?

The Python host (./flow test-lang) handles the same program correctly.

Found while porting the Python run tests to tests/lang/. The Python test stays until this is fixed: tests/unit/test_tco_shim.py::test_deep_tail_recursion_runs_without_stack_overflow.

Activity

  1. added 3 commits that reference this issue on Sep 27, 2026
    fd2d2dd
    3485411
    77d7b16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions