Skip to content

Stage-A: fix the bugs blocking the Python-to-Flow test port - #966

Merged
godofecht merged 13 commits into
mainfrom
fix/stage-a-test-blockers
Sep 27, 2026
Merged

godofecht merged 13 commits into
mainfrom
fix/stage-a-test-blockers

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Stage-A fixes that unblock porting Python run tests to tests/lang/. Each fix has a Flow regression test, and the Python tests that were kept only because of the bug are ported and deleted.

Fixes #948. Fixes #946. Fixes #947. Fixes #941. Fixes #945. Fixes #943. Fixes #944. Fixes #949. Fixes #950. Fixes #940. Fixes #942. Fixes #951. Part of #952.

Fixes

Issue Change Flow test
#948, #946 cgen classifies an expression's primitive kind (literal, cast, declared variable, known call return type, arithmetic). println picks %s / %f / %lld / %llu / %u / %d from it. String + formats number and bool operands through new __flowc_str_of_* helpers. test_string_concat_numbers.flow (+ .expected)
#947 Pending defers live on a stack in CgenBuf. Every statement chain is a scope. A return computes its value into __flowc_ret, runs all pending defers innermost first, then returns. break and continue run the loop body's defers. test_defer_return_order.flow
#941 A match arm ident that spells <Enum>_<Variant> for an enum in the program is a value pattern. test_enum_match_arms.flow
#945 [value; N] parses; lowers to { [0 ... N - 1] = value }. test_array_repeat.flow
#943 A function returning array<T, N> returns a typedef'd wrapper __flowc_arr_<T>_<N> { T v[N]; }; calls append .v, also across bundle modules via the signature table. The C identifier I (a <complex.h> macro) is now mangled, which stdlib/audio/scales.flow needed. test_array_return_values.flow, test_stdlib_scales.flow
#944 array<array<T, M>, N> lets and params are T x[N][M]. A sized-array let from a non-literal is copied with memcpy. test_array_return_values.flow
#949 sum(a..b), sum(a..b step s), sum(A | B), sum(A & B) parse and lower to preamble helpers (closed form in 64-bit, intersection by CRT). Each bound is evaluated once. test_range_sum_step.flow, test_range_algebra.flow
#950 Before a bundle emits, it lists every module's top-level function definitions. A private function whose name another module exports, or an earlier module defines privately, is wrapped in #define name __flowc_m<i>_name / #undef. Bundles without collisions emit the same C as before. test_private_fn_collision.flow
#940 @max_iterations(N) on a while loop is kept on the node; cgen counts and aborts with the Python host's diagnostic. test_max_iterations_bound.flow (+ .exitcode, .expected-stderr)
#942 c64, c128 and the <complex.h> functions are checker intrinsics. test_complex_builtins.flow
#952 (part) sizeof<T>() is an intrinsic, ((int64_t)sizeof(T)). The Python host's --strict checker rejects sizeof<T>(), so the test is a Stage-A fixture in roundtrip.sh: compiler/fixtures/stage_a_sizeof.flow. compiler/fixtures/stage_a_sizeof.flow
#951 A function with a self tail call gets a __flowc_tail label. The tail return evaluates all arguments into temporaries, rebinds the parameters and jumps. Not applied with pending defers or inside lambdas. test_deep_tail_recursion.flow

Also: lexer.flow now imports KW_SHADER (leftover from #890), which made compile_module lexer in roundtrip.sh fail. The self-test cgen buffers in main.flow grow from 8 KiB to 32 KiB because the preamble passed 8 KiB.

Not fixed

#952, stdlib mutex half. concurrent.flow's functions are not exported. The Python resolver puts every declaration of an imported module in one flat namespace, while the Stage-A bundle checker seeds only exports. Making Stage-A match means deciding what private means across imports, which conflicts with the #950 behaviour. With the checker off, the concurrent.flow bundle compiles to an object. Linking still needs the runtime sources (#953).

Python tests deleted or trimmed

  • tests/integration/test_defer_ordering.py (deleted)
  • tests/integration/test_private_function_module_collision.py (deleted)
  • tests/integration/test_cast_in_string_concat.py: the two numeric-operand tests
  • tests/integration/test_array_return.py: six value tests. The clang return-stack-address check stays and now covers the nested source too.
  • tests/integration/test_range_sum.py: the two run tests and the ctypes value checks. The closed-form C-shape check and the parser tests stay.
  • tests/integration/test_range_algebra.py: the two run tests. The literal-fold C check stays.
  • tests/unit/test_match_exhaustive_returns.py::test_every_arm_still_dispatches_to_its_own_variant
  • tests/unit/test_complex_types.py::test_complex_compile_and_run
  • tests/unit/test_tco_shim.py::test_deep_tail_recursion_runs_without_stack_overflow

Known gaps

  • The Python host skips defers on break/continue. Stage-A now runs them. test_defer_return_order.flow does not cover that case because it would fail under test-lang.
  • tests/unit/test_match_exhaustive_returns.py was already broken on main: its fixture reads build/m.c, but ./flow run now writes to build/run.*/.
  • The Python host rejects a binding arm on k.tag ("use of undeclared identifier"). test_enum_match_arms.flow uses _.

Verification

  • The Python-host self-test (FLOW_HOST=python ./flow run compiler/src/main.flow) prints flowc: PASS.
  • Bootstrap C is regenerated in its own commit, and the fixed point holds: the regenerated C is byte-identical to what the rebuilt bootstrap emits.
  • ./compiler/scripts/bootstrap_from_c.sh --verify: PASS
  • ./compiler/scripts/roundtrip.sh: PASS, including compile_module lexer and the new stage_a_sizeof case.
  • ./compiler/scripts/self_host_full.sh: PASS
  • Bootstrap suite (tests/lang, -Itests/lang): 107/19 before, 119/19 after. The same 19 files fail as on main, and the 12 new tests all compile.
  • Every new tests/lang file passes ./flow test-lang (Python host) and exits as expected under Stage-A.

… by type

println and print chose %d for every argument that was not a literal, so a
string variable or a concat result printed as a pointer. String + with an
i64, u32, f64 or bool operand passed the raw value to the concat helper.

cgen now classifies an expression's primitive kind from literals, casts,
declared variables, known call return types and arithmetic, and uses it for
both paths. Numbers go through new __flowc_str_of_{i64,u64,f64} helpers.

Fixes #948. Fixes #946.
lexer.flow returns KW_SHADER for the shader keyword but never imported it,
so compile_module lexer in compiler/scripts/roundtrip.sh failed.
cgen emitted a block's defers before its return statement, so the return
read memory the defer had already freed, and a return inside an if body
skipped the enclosing block's defers entirely.

Pending defers now live on a stack in CgenBuf. Every statement chain
(block, if branch, match arm) is one scope. A return evaluates its value
into __flowc_ret, runs every pending defer innermost first, then returns.
break and continue run the loop body's defers. A scope that falls off its
end runs its own defers.

tests/lang/test_defer_return_order.flow replaces
tests/integration/test_defer_ordering.py.

Fixes #947.
…terns

A bare ident arm parses as a catch-all binding, so a match with one arm per
variant (Kind_A => ..., Kind_B => ...) failed with 'catch-all match arm
must be last'. After parsing a program, arms whose ident spells
<Enum>_<Variant> for an enum in the same program become value patterns.

tests/lang/test_enum_match_arms.flow replaces
test_match_exhaustive_returns.py::test_every_arm_still_dispatches_to_its_own_variant.

Fixes #941.
…rays

[value; N] now parses. The array literal node keeps the count in b and
cgen lowers it to a GNU range initializer, { [0 ... N - 1] = value }.

A function returning array<T, N> returns a struct wrapper
__flowc_arr_<T>_<N> { T v[N]; }, typedef'd once before its first use. A
call to it appends .v, including calls into other bundle modules (the
signature table records the wrapper). A return builds the wrapper from a
literal, a named array or rows given by name.

array<array<T, M>, N> lets and params are T x[N][M] instead of an array of
pointers. A sized-array let from anything other than a literal is copied
with memcpy.

The C identifier I (a <complex.h> macro, always included) is now mangled,
which stdlib/audio/scales.flow needed.

tests/lang/test_array_repeat.flow, test_array_return_values.flow and
test_stdlib_scales.flow replace the value tests in
tests/integration/test_array_return.py.

Fixes #945. Fixes #943. Fixes #944.
sum(a..b), sum(a..b step s), and the union or intersection of two stepped
ranges (sum(A | B), sum(A & B)) now parse. Bounds and steps bind tighter
than the | and & that combine ranges. The call is marked with ival 2, 3 or
4 and lowers to preamble helpers, so every bound is evaluated once:
__flowc_range_sum is the closed form in 64-bit arithmetic, and the
intersection is one progression with step lcm(p1, p2) found by CRT.

The self-test cgen buffers grow from 8 KiB to 32 KiB; the preamble passed
8 KiB with these helpers.

tests/lang/test_range_sum_step.flow and test_range_algebra.flow replace
the run tests in tests/integration/test_range_sum.py and
test_range_algebra.py.

Fixes #949.
A bundle is one C TU, so two modules that each define a private function
with the same name failed with 'conflicting types'. Before emitting, the
bundle lists every module's top-level function definitions. A private
definition whose name is exported by another module, or defined privately
by an earlier one, is wrapped in #define name __flowc_m<i>_name and a
matching #undef around its module's C. One definition keeps its name, and
a bundle without collisions emits the same C as before.

tests/lang/test_private_fn_collision.flow replaces
tests/integration/test_private_function_module_collision.py.

Fixes #950.
Statement attributes were skipped, so @max_iterations(N) had no effect.
The parser now keeps an integer bound on the while node (ival), and cgen
counts iterations and aborts past the bound with the same diagnostic as
the Python host: 'flow: while exceeded @max_iterations(N)'.

tests/lang/test_max_iterations_bound.flow checks a loop inside the bound
and one past it (exit 134, diagnostic on stderr).

Fixes #940.
The checker rejected c64, c128 and the <complex.h> functions (creal,
cimag, cabs, carg, conj, cexp, clog, csqrt, cpow) as unbound calls, though
cgen already lowered the constructors and C provides the rest. They are
now intrinsics.

sizeof<T>() was also an unbound call. It is now an intrinsic that lowers
to ((int64_t)sizeof(T)). The Python host's --strict checker rejects
sizeof<T>(), so its test is a Stage-A fixture in roundtrip.sh
(compiler/fixtures/stage_a_sizeof.flow) rather than a tests/lang program.

tests/lang/test_complex_builtins.flow replaces
test_complex_types.py::test_complex_compile_and_run.

Fixes #942. Part of #952 (the stdlib mutex half stays open).
Stage-A emitted plain recursion, so a million-deep tail-recursive function
overflowed the stack. When a function's body contains 'return f(...)'
calling itself (same name, one argument per parameter, not overloaded,
not generic), the body opens with a __flowc_tail label, and each such
return evaluates every argument into a temporary, rebinds the parameters
and jumps back. Returns with pending defers and returns inside lambdas
keep the real call.

tests/lang/test_deep_tail_recursion.flow replaces
test_tco_shim.py::test_deep_tail_recursion_runs_without_stack_overflow.

Fixes #951.
Regenerated from compiler/src with the Python host in bundle mode. Fixed
point verified: the rebuilt bootstrap emits byte-identical C.
@godofecht

Copy link
Copy Markdown
Contributor Author

Merge train: rebased onto main after #980 (the ruff fix). The check.py commit dropped as already upstream. Please avoid pushing to this branch while the train is running.

@godofecht
godofecht force-pushed the fix/stage-a-test-blockers branch from d453cdc to 613ab0d Compare September 27, 2026 20:03
@godofecht
godofecht merged commit 77d7b16 into main Sep 27, 2026
24 checks passed
godofecht added a commit that referenced this pull request Sep 27, 2026
Thirteen tests/lang programs cover run tests that used to transpile, compile and run from Python. The superseded Python tests are deleted. Rebased over #966, which had already deleted test_defer_ordering.py and the numeric cases of test_cast_in_string_concat.py; with both sides' deletions the latter file has no tests left and is removed.
@godofecht
godofecht deleted the fix/stage-a-test-blockers branch October 1, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment