Skip to content

fix(cgen): preserve span bounds checks across body rebinding (#729) - #1414

Closed
godofecht wants to merge 7 commits into
mainfrom
fix/729-span-proof-invalidated-by-rebinding
Closed

godofecht wants to merge 7 commits into
mainfrom
fix/729-span-proof-invalidated-by-rebinding

Conversation

@godofecht

@godofecht godofecht commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of #729. Does not close it.

Change

For for i in 0 to xs.len, the C backend drops the bounds check on xs[i]. flowc_cgen_bounds_span_base accepted the loop from its header alone, so a body that reassigned xs (or rebound i) still lost the check, and xs[i] could read past the new span. The length-hoisting path already guarded against this with flowc_cgen_name_rebound_in_node; the bounds path did not.

  • flowc_cgen_bounds_span_base now returns AST_NONE when the body assigns, shadows or takes the address of the span or the induction variable, so the checks stay.
  • flowc_cgen_name_rebound_in_node returns early for any id outside 0 .. arena.len, so a negative sentinel field is never indexed.
  • docs/language/spans.md says when range loops keep their checks.

Evidence

Rebased onto main 9f1d6d0. Bootstrap regenerated in its own commit.

  • tests/cgen/span_bounds_rebinding.flow requires the span index out of bounds fault path. With main's compiler it fails (missing: flow_fault_handler("span index out of bounds")); with this branch it passes. span_bounds_elision and span_bounds_retained still pass, so the ordinary loop keeps its elision.
  • Edited compiler selftest: flowc: PASS.
  • ./compiler/scripts/bootstrap_from_c.sh --regen: fixed point at gen2. --verify: PASS.
  • ./flow tool compiler/scripts/self_host_full.flow: ALL PASS. roundtrip.flow: ALL PASS.
  • ./flow tool tests/cgen/run.flow: 128 passed, 0 failed.
  • ./flow test-lang: 274/0. ./flow test-runtime: 34/0. ./flow tool tests/cli/run.flow: 152 passed, 2 failed (jit_hello, test_mlir: golden drift on main).

Merge note: #1383 (also #729) touches other functions in cgen.flow and merges cleanly apart from the generated bootstrap files. Whichever lands second needs its own bootstrap regeneration.

godofecht added a commit that referenced this pull request Oct 7, 2026
…s shadowed (#729)

Adds array_bounds_kept (for i in 0 to 4 over array<i32, 3>) and
array_bounds_rebound (the body shadows i). The rebased commit also
applies the #1414 rebinding rule to the new literal-bound path: the
proof is dropped when the body assigns, shadows or addresses the
induction name or the indexed array.
godofecht added a commit that referenced this pull request Oct 7, 2026
…s shadowed (#729)

Adds array_bounds_kept (for i in 0 to 4 over array<i32, 3>) and
array_bounds_rebound (the body shadows i). The rebased commit also
applies the #1414 rebinding rule to the new literal-bound path: the
proof is dropped when the body assigns, shadows or addresses the
induction name or the indexed array.
godofecht added a commit that referenced this pull request Oct 7, 2026
…s shadowed (#729)

Adds array_bounds_kept (for i in 0 to 4 over array<i32, 3>) and
array_bounds_rebound (the body shadows i). The rebased commit also
applies the #1414 rebinding rule to the new literal-bound path: the
proof is dropped when the body assigns, shadows or addresses the
induction name or the indexed array.
@godofecht
godofecht force-pushed the fix/729-span-proof-invalidated-by-rebinding branch from 2a86145 to 7b543f4 Compare October 7, 2026 11:35
@godofecht
godofecht marked this pull request as ready for review October 7, 2026 11:35
godofecht added a commit that referenced this pull request Oct 7, 2026
A literal-bound ascending loop `for i in 0 to N` with N <= M proves its indexed reads of `array<T, M>` in range, so FLOWC_CHECKS elides those checks. The proof is dropped when the loop body assigns, shadows or takes the address of the induction name or the indexed array (the #1414 rule). Goldens: array_bounds_elision, array_bounds_kept, array_bounds_rebound. Part of #729.
@godofecht

Copy link
Copy Markdown
Contributor Author

Note from the merge train: #1383 (literal-bound array bounds elision, merged as f43efb7) already applies the rebinding rule to its new bounds_hi_val path: the proof is dropped when the loop body assigns, shadows or addresses the induction name or the indexed array (golden array_bounds_rebound). When this draft lands, keep that guard; your span-path change is still needed for 0 to xs.len.

@godofecht

Copy link
Copy Markdown
Contributor Author

Landed in #1429 as 3891827 (lane C train).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant