Skip to content

fix(hy-check,hy-emit): assign key shape and emit key range on single-var map iteration (fixes #3422) - #3675

Merged
limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
CarlitoDon:fix/issue-3422-single-var-map-iteration
Nov 5, 2026
Merged

limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
CarlitoDon:fix/issue-3422-single-var-map-iteration

Conversation

@CarlitoDon

Copy link
Copy Markdown
Contributor

Summary

Fixes #3422.

Per SPEC-LANGUAGE.md LX13, single-variable map iteration (for k in m) binds the map key (for k := range m in Go), whereas two-variable iteration (for k, v in m) binds key and value. Two soundness and lowering gaps were identified:

  1. hy-check: In shape.rs:iterate_var_shapes and freeze.rs, single-variable map iteration assigned the value shape (**v).clone() instead of the key shape (**k).clone(). For maps with pointer/reference keys (e.g. map[*box]int), this prevented alias := k from being tracked under alias sharing and allowed concurrent mutation after moving alias, causing runtime data races.
  2. hy-emit: In lib.rs:binding_kind_of_ty and stmt.rs:is_map_expr, map type aliases and struct fields of map type were not unwrapped/resolved to BindingKind::Map, causing single-variable iteration to lower to for _, k := range ... (value binding) instead of for k := range ... (key binding).

Changes

  • In compiler/crates/hy-check/src/shape.rs and src/freeze.rs: assign k (the key shape) on single-variable map iteration when n_vars == 1 / vars.len() == 1.
  • In compiler/crates/hy-emit/src/lib.rs (binding_kind_of_ty): recursively unwrap user type aliases (n5_aliases) to recognize map types.
  • In compiler/crates/hy-emit/src/stmt.rs (is_map_expr): resolve struct fields (n5_structs) to detect map fields and emit for k := range h.m.
  • Added tests:
    • compiler/crates/hy-check/tests/issue_3422_single_var_map_iteration.rs
    • compiler/crates/hy-emit/tests/issue_3422_single_var_map_range_emit.rs

Verification

  • cargo test --test issue_3422_single_var_map_iteration: passes (verdict Reject on data race repro).
  • cargo test --test issue_3422_single_var_map_range_emit: passes (emits for k := range).
  • Full make check: OK (all 33 differential loop tests, guarantees, runner, and cargo checks pass).

Closes #3422
/claim #3422

@CarlitoDon
CarlitoDon force-pushed the fix/issue-3422-single-var-map-iteration branch from b6ac5a0 to b5b2dac Compare November 5, 2026 01:57
@limitlessprojects
limitlessprojects merged commit f0bce7f into limitlessprojects:main Nov 5, 2026
3 of 4 checks passed
limitlessprojects pushed a commit that referenced this pull request Nov 5, 2026
… iteration (fixes #3422)

Verification evidence (battery on committed trial merge f0bce7f, tree 5b1724d):
- merge gate: clean auto-merge of PR head b5b2dac with main
- cargo test --workspace: 437 suites ok, 0 failed
- corpus harness: verdict agreement 135/135, tripwire not fired
- differential loop FULL: accept-side 45/45 build+run, reject-side 90/90

Contributions by CarlitoDon:
- freeze.rs: replay loop var shape for Map now binds the KEY when iterating with a single variable (vars.len()==1), value only for two-variable form — per SPEC-LANGUAGE.md LX13
- shape.rs: shape_of_expr single-var map iteration vec![v] -> vec![k], same LX13 rule
- new tests issue_3422_single_var_map_iteration.rs (checker: for k in m binds key, k/v two-var binds both) and issue_3422_single_var_map_range_emit.rs (emitted Go range form)

CREDIT: @CarlitoDon
@limitlessprojects

Copy link
Copy Markdown
Owner

Landed: merged to main as df73308c (merge of trial f0bce7fb) ✅

Thank you @CarlitoDon — #3422 is closed. Battery on the committed trial merge (S107 rule):

Gate Result
Merge gate clean auto-merge with main
cargo test --workspace 437 suites ok, 0 failed
Corpus harness verdict agreement 135/135, tripwire not fired
Differential loop FULL accept-side 45/45 build+run, reject-side 90/90

What landed

  • freeze.rs: single-variable map iteration now replays the key shape (vars.len()==1 → *k), value shape only for the two-variable form — per SPEC-LANGUAGE.md LX13.
  • shape.rs: shape_of_expr single-var map arm vec![v] → vec![k].
  • Two new suites pinning the checker behavior and the emitted for k := range m lowering.

Both call sites were small and exactly where the LX13 rule lives; the two-variable path is untouched, matching the spec. The emitter side already produced the correct Go range form on current main, so no emit change was needed in the merge — your checker tests pin the contract.

Bounty: recorded per BOUNTY.md / discussion #295 against issue #3422.

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

Labels

bot:stale-base Shepherd: behind main beyond threshold

Projects

None yet

2 participants