Skip to content

fix(hy-check): propagate moved alias and may_share state across block scope exit and loop back-edges (#313, #399, #253) - #401

Closed
gelismarvel wants to merge 3 commits into
limitlessprojects:mainfrom
gelismarvel:fix/issue-313-loop-backedge-alias
Closed

gelismarvel wants to merge 3 commits into
limitlessprojects:mainfrom
gelismarvel:fix/issue-313-loop-backedge-alias

Conversation

@gelismarvel

@gelismarvel gelismarvel commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

/claim #313
Closes #313

Root Cause Analysis (check_loop_backedge & FnState::scope_pop)

In compiler/crates/hy-check/src/freeze.rs (Checker::check_loop_backedge) and compiler/crates/hy-check/src/model.rs (FnState::scope_pop), moved alias and may_share state failed to propagate across loop back-edges in three interacting ways:

  1. iter_st.scopes.truncate(outer_depth) bypassed FnState::scope_pop():
    At the end of a loop body (idx == loop_end), check_loop_backedge truncated iter_st.scopes directly instead of popping inner loop scopes via iter_st.scope_pop(). Because alias_scopes and share_scopes were not popped and scope_pop()'s shadow-exit freeze propagation did not run on iter_st, any outer alias alias := m that was shadowed inside the loop body before move m lost its "alias" -> "m" link in st.aliases and remained moved: None on the back-edge, allowing alias to be mutated or moved again at the top of the next iteration with zero diagnostics.
  2. Unscoped Stmt::Local handling in check_loop_backedge clobbered outer bindings:
    While walking the flattened loop_stmts, check_loop_backedge treated every Stmt::Local(l) (for name in &l.names { iter_st.aliases.remove(name); b.moved = None; }) as if it permanently re-owned the outer binding for the remainder of the loop body—even when Stmt::Local occurred inside a nested block (if i == 0 { alias := 1; ... }) whose scope exited before task worker(move alias, done).
  3. Outer-to-outer alias / may_share rebinds inside inner blocks were dropped on scope_pop():
    When two outer bindings were linked inside a conditional block (if i == 0 { alias = m }), scope_pop() restored outer_aliases / outer_share without preserving outer-to-outer edges (!popped_scope.contains_key(a) && !popped_scope.contains_key(&root)), and mark_move did not expand st.aliases chains anchored at may_share peers.

Minimal Fix

  • compiler/crates/hy-check/src/freeze.rs:
    • Pop inner loop scopes on iter_st via while iter_st.scopes.len() > outer_depth { iter_st.scope_pop(); } and resolve roots against iter_st.
    • Track lexical shadow scopes across loop_body via walk::flatten(loop_body) (ScopeEvent::Push / Pop / SelectRecv) instead of mutating iter_st's outer bindings and alias map when walking inner Stmt::Local definitions.
    • Expand alias chains of may_share peers in both mark_move and check_loop_backedge.
  • compiler/crates/hy-check/src/model.rs:
    • Preserve outer-to-outer alias and may_share edges across scope_pop() and guard inner_root_moves against inner shadow bindings defined in popped_scope.
  • compiler/crates/hy-check/src/lib.rs:
    • Added 4 focused unit tests covering nested shadow before move alias in a loop, shadowed outer alias propagation across iterations, conditional outer alias rebind in a loop, and a positive control where moving a loop-local shadow does not freeze an untouched outer map.

Verification

  • cargo test --manifest-path compiler/Cargo.toml -p hy-check: 121/121 unit tests + 86/86 integration tests pass (0 failed).

Fixes #253
/claim #253

@limitlessprojects

Copy link
Copy Markdown
Owner

Validation (agent session-15/2-a3, worktree = PR head merged with main @ c53978b): VALIDATED — READY TO LAND.

Status note first: the headline repro from #313 (for i in 0..2 { task worker(move m, done) }) is already rejected on current main with HYB-E3003 (m is moved again after ownership already left) — intervening machinery closed the plain double-move form since the b6878f8 report. But the back-edge gap this PR targets is still live in the alias/may_share forms, and I reproduced two of them:

  1. Loop body shadows an outer alias inside a nested block, then the next iteration spawns move alias: main accepts with 0 diagnostics → this PR rejects with HYB-E3003 (alias is moved again after ownership already left). This shape is not fixed by hy-check: keep may_share edges that outlive a block (fixes #399) #400's binary — it is uniquely covered here.
  2. The [soundness] P0: block-scoped assignment drops may_share edges - accepted program races #399/hy-check: keep may_share edges that outlive a block (fixes #399) #400 block-scoped escape (alias = shared inside an if, task worker(move alias), write shared[...] after): main 0 diagnostics → this PR rejects with HYB-E3003. I also verified hy-check: keep may_share edges that outlive a block (fixes #399) #400's own corpus fixture and a doubly-nested variant reject on this PR's binary — i.e., the scope_pop outer-to-outer edge preservation here subsumes hy-check: keep may_share edges that outlive a block (fixes #399) #400's fix.

Positive controls stay green: loop-local fresh map moved per iteration accepts, direct move shared control rejects identically, pure-local struct peer accepts, map-twin param peer still E3004.

Battery on the merged worktree: cargo test --workspace 599 passed / 0 failed (main 595 + 4 new unit tests), tools/corpus_harness.py 52/52.

Merge-order recommendation: land this PR first; #400 (same model.rs scope_pop restoration path) becomes redundant as a fix — salvage its block_scope_share.rs tests and corpus fixture (renumbered, with the REJECT A1b rule tag) onto this tree if you want the extra pins. Thanks @exantiv — careful RCA, and the three interacting causes match what I measured.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

Thank you for validating @limitlessprojects! Confirmed ready to land cleanly on main.

@gelismarvel
gelismarvel force-pushed the fix/issue-313-loop-backedge-alias branch from 2bb6951 to e701ebb Compare October 17, 2026 13:28
@gelismarvel gelismarvel changed the title fix(hy-check): propagate moved alias and may_share state across loop back-edges (#313) fix(hy-check): propagate moved alias and may_share state across block scope exits and loop back-edges (#313, #399) Oct 17, 2026
@gelismarvel
gelismarvel force-pushed the fix/issue-313-loop-backedge-alias branch 2 times, most recently from a4ab434 to ef8280c Compare October 17, 2026 14:40
@gelismarvel gelismarvel changed the title fix(hy-check): propagate moved alias and may_share state across block scope exits and loop back-edges (#313, #399) fix(hy-check): propagate moved alias and may_share state across block scope exit and loop back-edges (#313, #399, #253) Oct 17, 2026
@gelismarvel

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto current main (e2a5f7d, commit ef8280c) after #234 / #296 / #398 / #410 landed, and also closed the two remaining live check_loop_backedge soundness bypasses from #253:

  1. Direct continue before trailing break / return (block_has_direct_continue): for i in 0..2 { m["main"] = i; task worker(move m, done); if i == 0 { continue }; break } returned 0 diagnostics (ACCEPT) on e2a5f7d because loop_body.stmts.last() is break; check_loop_backedge now checks whether the loop body contains any reachable continue targeting this loop (0 diagnostics -> HYB-E3003).
  2. ForKind::Iterate { expr, .. } collection expression checked at i == 0: for k, v in m { ... } now collects expr_uses(expr, &mut uses) at i == 0 alongside ForKind::While and ForKind::Range, rejecting loop-carried moves of the iterated collection with HYB-E3003.

Verified: all 134 hy-check unit tests, all 34 hy-check test binaries, and 55/55 corpus checks pass.

@gelismarvel
gelismarvel force-pushed the fix/issue-313-loop-backedge-alias branch from ef8280c to 869f9aa Compare October 17, 2026 16:17
@gelismarvel
gelismarvel force-pushed the fix/issue-313-loop-backedge-alias branch from 869f9aa to cbe9696 Compare October 17, 2026 20:52
…lessprojects#313)

inner_root_moves is already filtered by survives_as_outer. The extra popped_scope guard skipped block self-shadowing such as 'h, ok := alias_pair(h)', so moving the inner h no longer froze the outer h and main's multi_value_self_redefine_and_shadow_preserves_share_and_alias_edges test failed in CI. All limitlessprojects#313 tests still pass.
Integrate pr-254 (limitlessprojects#253): adopt main's loop back-edge implementation in freeze.rs (block_has_continue_for_loop, the Iterate collection use and the Range hi-only back-edge check with its integration note), and keep only limitlessprojects#313's remaining pieces: shadow seeding for the loop variables and share-group peer/alias clearing. Drop the now-duplicate block_has_direct_continue helpers and the unreachable Iterate arm.
dancerdan15 added a commit to dancerdan15/Go-Hybrid that referenced this pull request Oct 23, 2026
…xit outer aliases

Instantiate generic function and method return shapes and TypeExprs from call-site arguments across single-value and multi-value calls so shallow-copied references, struct field projections, slice element copies, and closure slices cannot escape O5.2 freeze tracking.

Also preserve surviving outer alias and self-shadow links across FnState::scope_pop and clear alias chains across loop back-edge share groups.

Fixes limitlessprojects#73
Fixes limitlessprojects#313
Sibling-take credit: @Visional-Dev (limitlessprojects#408), @Andrrewss (limitlessprojects#401)
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.

[soundness] Loop back-edge check (check_loop_backedge) fails to propagate moved alias state across iterations, allowing double-move data races

2 participants