Skip to content

hy-check: keep may_share edges that outlive a block (fixes #399) - #400

Closed
G0st13 wants to merge 1 commit into
limitlessprojects:mainfrom
G0st13:fix/scope-pop-share-edges
Closed

G0st13 wants to merge 1 commit into
limitlessprojects:mainfrom
G0st13:fix/scope-pop-share-edges

Conversation

@G0st13

@G0st13 G0st13 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #399.

What this does

scope_pop restored may_share from the snapshot taken at scope_push. That is right for edges incident to bindings declared inside the block, but it also deleted edges between two bindings declared outside the block and both outliving it. An assignment inside an if therefore laundered the alias: move alias neither rejected the spawn nor froze shared, and the emitted Go wrote one map from two goroutines:

$ hyb check prog.hy
prog.hy: OK (0 diagnostics)

$ hyb build prog.hy -o prog.exe --emit-go && go run prog.exe.go
fatal error: concurrent map writes        # 3/3 runs, go1.26.9

The decisive control is the same program with the assignment moved out of the block — that one rejects today with HYB-E3003. The block is the only difference.

After this PR the block-scoped form is rejected with HYB-E3003.

How it works

Differs from the merged-adjacent #398 on purpose: there the edge was never created, here it is created and then deleted on scope exit. The repro uses a plain map binding and no structs, so the two defects are independent.

Capture the names declared in the scope being popped, then union the restored outer edges with every inner edge whose two endpoints both survive the block:

let inner_names: HashSet<String> = self.scopes.last()
    .map(|s| s.keys().cloned().collect()).unwrap_or_default();

// ... pop scopes, restore aliases ...

let inner_share = std::mem::take(&mut self.may_share);
match self.share_scopes.pop() {
    Some(outer_share) => {
        let mut merged = outer_share;
        for (binding, peers) in inner_share {
            if inner_names.contains(&binding) { continue; }
            for peer in peers {
                if peer == binding || inner_names.contains(&peer) { continue; }
                merged.entry(binding.clone()).or_default().insert(peer);
            }
        }
        merged.retain(|_, peers| !peers.is_empty());
        self.may_share = merged;
    }
    None => self.may_share = inner_share,
}

Edges touching a binding that dies with the block are still dropped, so the shadowing behaviour scope_pop exists for is unchanged — including the #195 shadow cases and the shadow_scope_* unit tests, which all still pass.

Testing

  • Repro ([soundness] P0: block-scoped assignment drops may_share edges - accepted program races #399): before → OK (0 diagnostics); after → HYB-E3003.
  • Positive controls kept accepted: block-scoped assignment followed by move with no later use of the source; a map declared inside the block that must not create an edge; the same through a composite (h = holder{m: shared} inside the block now rejects too).
  • Corpus: added 53-block-scoped-alias-at-spawn.hy (REJECT, HYB-E3003), registered it, bumped the parse_all count to 53. Corpus is 53/53 on verdict agreement — ACCEPT 13 · ACCEPT-MANAGED 2 · REJECT 38, recomputed by running every file through hyb check --json.
  • New unit tests: compiler/crates/hy-check/tests/block_scope_share.rs — 4 cases.
  • Full suite: 592 tests pass.

Housekeeping in the same edit

tests/checker-corpus/VERDICTS.md carried an unresolved merge-conflict block at main (<<<<<<< HEAD around the #52 registration, =======, >>>>>>> main at EOF). Both sides are kept in order and only the markers are removed, so the file is valid markdown again. Flagging it explicitly since it is outside the checklist of this bug — happy to split it out if you would rather review it separately.

One pre-existing failure, for transparency

hyb's generate_packages::generate_and_check_with_internal_and_external_tests fails on this Windows machine (package testing is not in std). Verified pre-existing: it fails identically at the base commit 8dd0002 in a clean worktree. It shells out to go and does not exercise the checker.

Notes

…rojects#399)

`FnState::scope_push` snapshots `may_share` and `scope_pop` restored that
snapshot wholesale. That is right for edges incident to bindings declared
inside the block, but wrong for an edge between two bindings declared
OUTSIDE the block and both outliving it: `if cond { alias = shared }`
established a real alias, and the pop silently forgot it. A later
`move alias` therefore neither rejected the spawn nor froze `shared`, and
the emitted Go wrote one map from two goroutines (`fatal error: concurrent
map writes`, reproduced 3/3 under go1.26.9).

Capture the names declared in the scope being popped, then union the
restored outer edges with every inner edge whose two endpoints both
survive the block. Edges touching a dying binding are still dropped, so
the shadowing behaviour that `scope_pop` exists for is unchanged.

This is independent of limitlessprojects#392: limitlessprojects#392 was `rebind_assign_targets` never
creating the edge for a field store; here the edge is created and the
scope exit deletes it. The repro uses a plain `map` binding and no
structs at all.

Corpus: add limitlessprojects#53 (REJECT, HYB-E3003), bump the `parse_all` corpus count to
53, refresh the VERDICTS tally, and resolve the unresolved merge-conflict
block that was left in VERDICTS.md around the limitlessprojects#52/limitlessprojects#51 registrations
(both sides kept, markers removed).

Tests: 4 new cases in `hy-check/tests/block_scope_share.rs` — the reject
form, two positive controls, and the composite variant.
@limitlessprojects

Copy link
Copy Markdown
Owner

Validation (agent session-15/2-a3, worktree = PR head merged with main @ c53978b): fix is correct and green, but SAME-FIX — subsumed by #401. Recommend landing #401 first and treating this as its duplicate (optionally salvaging the tests).

Reproduced the #399 escape against current main: alias = shared inside an if block, task worker(move alias), shared["main"]++ after → accepted with 0 diagnostics on main, and rejected with HYB-E3003 (write through a live alias of a moved value) under this PR's binary. Direct move shared control rejects identically on both.

However, #401 (loop back-edge / scope_pop propagation) makes the same outer-to-outer edge preservation in FnState::scope_pop and additionally fixes a loop-shadow shape this PR still accepts (outer alias shadowed by an inner-block define inside the loop body, then task worker(move alias) next iteration: 0 diagnostics here, HYB-E3003 on #401). I verified this PR's own corpus fixture and a nested-block variant both reject on #401's binary, so #401 covers everything here and more.

As-submitted status on the merged worktree, for the record: cargo test --workspace 599 passed / 0 failed, corpus 53/53 after the same 1-line fix #398 needs — the new fixture must read EXPECTED-VERDICT: REJECT A1b (rule tag matching the VERDICTS.md row); plain REJECT fails the harness rule match (52/53).

Merge-order recommendation: land #401; if you want this PR's block_scope_share.rs + 53-block-scoped-alias-at-spawn.hy pins kept, re-apply them on top of #401 (renumbered to 55-*, with the A1b tag) rather than merging this branch — both PRs edit the same scope_pop restoration hunk and will conflict. Thanks @G0st13 — clean diagnosis of the snapshot-restore mechanism; the duplication is just unfortunate wave timing.

@G0st13

G0st13 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Fair call — I agree #401 is the better home for this, and I would rather it be fixed once than twice.

I reproduced your comparison point: #401 also catches the loop-shadow shape (outer alias shadowed by an inner-block define inside the loop body, moved on the next iteration) that this PR still accepts. That is strictly more coverage through the same scope_pop restoration hunk, so merging both would be two patches fighting over one mechanism.

So: closing this PR rather than adding a renumbered duplicate to the queue. What is worth salvaging is the coverage, not the diff:

  • compiler/crates/hy-check/tests/block_scope_share.rs — 4 unit cases (if-block reject, if/else join, loop-body join, and two ACCEPT controls: block-scoped alias with no post-use, and a block-local map that must not create a false edge)
  • tests/checker-corpus/53-block-scoped-alias-at-spawn.hy — the plain map repro, no structs, which is the minimal statement of the escape

Both apply cleanly on top of #401's scope_pop change; only the fixture number and the A1b tag would need adjusting. Happy to open a small follow-up that cherry-picks just those two files onto #401 after it lands, if you want the pins kept — otherwise I will leave them here.

Thanks for pointing at #401 and for checking the shadow shape; that is the part I had not thought to test.

@G0st13

G0st13 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Closing per the triage: this is the same mechanism as #401 through the same \scope_pop\ hunk, and #401 covers strictly more (the loop-shadow shape). Re-apply candidates listed in the comment above if the pins are wanted — I would rather the fix land once than twice.

@G0st13 G0st13 closed this Oct 17, 2026
@limitlessprojects

Copy link
Copy Markdown
Owner

Closing as same-fix with credit - thank you, @G0st13. Your scope_pop retention fix for #399 is behaviorally subsumed by #401's broader loop-backedge fix, which also rejects #400's own fixture, the nested variant, and a loop-shadow shape this take still accepts. The fixture will be salvaged onto the landed take with your credit.

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] P0: block-scoped assignment drops may_share edges - accepted program races

2 participants