Skip to content

fix: resolve Moving a may-share peer of a parameter escapes the O5.1 non-owned check (h2 := h; task worker(move h2) — OK accepted, fatal error: concurrent map writes) (#75) - #410

Merged
limitlessprojects merged 3 commits into
limitlessprojects:mainfrom
gelismarvel:fix/issue-75-real-patch
Oct 17, 2026

Conversation

@gelismarvel

Copy link
Copy Markdown
Contributor

Summary

Fixes #75

Moving a may_share peer of a parameter (or receiver / capture / range variable / const composite) could still escape the O5.1 / O5.4 non-owned move check (HYB-E3004) in two pathways in compiler/crates/hy-check/src/loans.rs:

  1. Shadowing or multi-defining the parameter/root after creating a may_share peer (h2 := h; if true { h := fresh; ... } or h2 := h; h, tag := fresh, 1):

    • Unlike rebind_assign_targets, which transfers name's non_owned_origin to st.share_group(name) before calling st.share_clear(name), define_binding immediately called st.aliases.remove(name); st.share_clear(name); without transferring name's non-owned origin to its existing may_share peers or direct alias children.
    • Once h was shadowed in an inner block or redefined via a multi-name :=, st.share_clear("h") severed "h" from st.may_share["h2"] while replacing "h" in st.scopes with an owned binding (non_owned_origin: None), allowing task worker(move h2, done) to pass with 0 diagnostics (OK) and race with the caller's h.m.
  2. Multi-value unpack defines and assignments from a parameter (h2, ok := alias_pair(h), h2, ok = alias_pair(h), m, ok := buckets["k"]):

    • The l.values.len() == 1 && l.names.len() > 1 branch of process_local and the values.len() == 1 && targets.len() > 1 branch of rebind_assign_targets updated target shapes/types without recording may_share or alias edges from the RHS expression for reference-bearing targets, allowing a may_share peer unpacked from a multi-return call or comma-ok map lookup on a parameter to be moved across a task / <- boundary without triggering HYB-E3004.

Root Cause & Changes (compiler/crates/hy-check/src/loans.rs)

  • define_binding: Before clearing st.aliases and st.may_share for name, check self.non_owned_origin_of(st, name) and propagate peer_origin ("a may-share peer of ...") to every peer in st.share_group(name) and orig to direct alias children (st.aliases entries pointing to name) whose non_owned_origin is None.
  • record_multi_value_share_and_alias (process_local & rebind_assign_targets): Record may_share and alias edges per target binding in multi-value defines (h2, ok := alias_pair(h), m, ok := buckets["k"]) and multi-value assignments (h2, ok = alias_pair(h)) using each target's resolved TypeShape, while preserving self-shadowed non-owned origins and transferring prior non-owned origins before clearing st.may_share.
  • Unit Tests: Added 5 regression tests in compiler/crates/hy-check/src/loans.rs:
    • param_may_share_peer_survived_inner_shadow_rejects_with_e3004
    • param_may_share_peer_survived_parallel_redefine_rejects_with_e3004
    • multi_return_define_from_param_rejects_may_share_move_with_e3004
    • multi_return_assign_from_param_rejects_may_share_move_with_e3004
    • comma_ok_map_lookup_on_param_rejects_move_with_e3004

Verification

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

…non-owned check (h2 := h; task worker(move h2) — OK accepted, fatal error: concurrent map writes) (limitlessprojects#75)
@G0st13

G0st13 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Built your branch locally (bbc7ef5) and ran the checker corpus against it: 52 files, 49 registered verdicts matched, 0 mismatches, 3 without a parsed registration row. No regression from my side — the fix does what it says.

I also tried to break the precision boundary, since record_multi_value_share_and_alias edges a target to every reference root of the call expression regardless of whether the returned value actually aliases it:

fn fresh(h holder) (holder, bool) {
    var x holder
    x.m = map[string]int{}   // fresh map — does not alias h.m
    return x, true
}

// h2, ok := fresh(h)
// task worker(move h2)
// h.m["main"]++

This rejects with HYB-E3003 on your branch. That is not a regression — I checked the single-value twin (h2 := fresh(h)) on current main and it rejects the same way, so you are consistent with the existing call-result approximation (no interprocedural summary ⇒ conservative edge). Flagging it only so the tradeoff is a recorded decision rather than an accident.

Two small things worth a minute:

  1. The ACCEPT side of that boundary isn't pinned. A control where the multi-value call's arguments carry no mutable GC storage (e.g. n, ok := pair(42)) would lock in that the new edge fires on reference-bearing roots only, and stop a future tightening from silently over-rejecting.

  2. The non-owned-origin / alias-child transfer is now duplicated. The same peer_origin + alias_children sweep appears in the define path and in the multi-value assign path (roughly 30 lines each). It would sit naturally beside record_multi_value_share_and_alias as one helper, and that also stops the two paths from drifting apart.

Neither blocks the fix — the soundness direction is right and the corpus is clean.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

Thanks for verifying against the corpus and for the suggestions @G0st13! Addressed both in e9edf1a:

  1. Extracted transfer_non_owned_origin_to_peers_and_aliases: unified the peer_origin + alias_children transfer helper beside record_multi_value_share_and_alias and reused it across define_binding and both process_assign paths.
  2. Added multi_return_value_only_args_accept_move_control: pins the ACCEPT boundary when multi-return call arguments carry only value types (pair(42) and make_holder(seed) where seed is an int parameter).

@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: the base repro from #75 (h2 := h; task worker(move h2, done) with a struct param) and the inner-shadow variant are already rejected on current main with HYB-E3004 — the landed may-share/origin machinery covers those two forms. But the two pathways this PR adds are still live bypasses on main, and I reproduced all three claimed ones:

  1. Multi-name rebind after the peer is taken — h2 := h; h, tag := fresh_holder, 1; task worker(move h2, done): main 0 diagnostics → PR rejects HYB-E3004 (peer of parameter h).
  2. Multi-value unpack from a parameter — h2, ok := alias_pair(h) then move h2: main 0 diagnostics → PR rejects HYB-E3004.
  3. Comma-ok map lookup on a parameter — v, ok := buckets["k"] (map-of-map param) then move v: main 0 diagnostics → PR rejects HYB-E3004 (not parameter buckets).

Positive controls stay green: unpacking a fresh composite (fresh_pair() returning new storage) and moving it accepts on both, and the pure-local struct peer (75-control) still accepts on the PR — no false-reject regression. The map-twin negative control still rejects E3004 identically.

Battery on the merged worktree: cargo test --workspace 600 passed / 0 failed (main 595 + 5 new loans.rs regression tests), tools/corpus_harness.py 52/52.

One integration note: this PR and #395 both rework define_binding's sever path in loans.rs (#395 adds preserve_peers_before_sever, this PR adds transfer_non_owned_origin_to_peers_and_aliases across define_binding/process_assign) — they will not merge cleanly over each other; decide the owner for the shared hunk before landing both. Thanks @exantiv — the multi-value/unpack families are exactly the ones the origin-transfer-only fix missed.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

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

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

Labels

None yet

Projects

None yet

3 participants