Skip to content

fix(hy-check): self-shadowing define (m := m) keeps the shadowed binding's non-owned origin (HYB-E3004) - #140

Merged
limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
salvocanci:fix/self-shadow-define-non-owned-origin
Oct 15, 2026
Merged

limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
salvocanci:fix/self-shadow-define-non-owned-origin

Conversation

@salvocanci

@salvocanci salvocanci commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

New soundness finding plus fix (main track, related to the #5 bounty cycle). Nothing in the tracker reports it: I searched every issue and PR for the self-shadow pattern and found none. It is close to #14 (inner-block shadowing), but #14 was a different hole and it is closed. Here, a self-shadowing define (m := m, h := h, m := same(m)) in an inner block drops the shadowed binding's non_owned_origin (PR #61's O5.1 guard). The result: move of a parameter, method receiver, or struct parameter gets through with 0 diagnostics, while the caller keeps writing the same storage.

m := m is the ordinary Go re-declare idiom, and the emitted Go passes go vet.

Repro (main @ f99d6d0, hyb built with the pinned go1.26.9 / rust 1.99.0)

package main

fn worker(m map[string]int, done chan bool) {
    for i in 0..1000 {
        m["k"] = i
    }
    done <- true
}

fn spawn(m map[string]int, done chan bool) {
    if true {
        m := m
        task worker(move m, done)
    }
}

fn main() {
    m := map[string]int{"k": 0}
    done := make(chan bool, 1)
    spawn(m, done)
    for i in 0..1000 {
        m["k"] = i
    }
    <-done
}
$ hyb check param.hy
param.hy: OK (0 diagnostics)
$ hyb build param.hy -o out/param --emit-go && go vet out/param.go   # clean
$ go run -race out/param.go
WARNING: DATA RACE  (x2, exit status 66)

Twins that already reject on main: task worker(move m, done) on the parameter directly, and m2 := m; task worker(move m2, done). Both give HYB-E3004. Only the spelling of the copy changed.

Expected: error[HYB-E3004]: cannot move m(map[string]int) — move requires an exclusively owned local binding, not parameterm ... (tier0_rule: A1b). That is what this PR emits.

The same hole shows up with a method receiver (fn (s state) spawn(...) { if true { s := s; task worker(move s, done) } }) and a struct parameter (h := h). Every one of these is accepted on main with 0 diagnostics, and go run -race reports WARNING: DATA RACE on each.

Root cause

process_local (loans.rs) calls define_binding first and only then records links with record_may_share / record_alias. Both look up the RHS ident by name, after the new inner m already shadows the outer one:

  • record_alias sees m -> m and inherits the inner binding's (empty) origin
  • share_edge(m, m) is a no-op

So the outer binding's origin (parameter / receiver / range variable) never reaches the new binding, and check_non_owned_move finds nothing to cite.

Fix

  • freeze.rs: move check_non_owned_move's origin computation into non_owned_origin_of(st, name) -> Option<(String, bool)>, covering both the own/alias-root origin and the [soundness] 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 may-share peer origin. The diagnostic itself does not change.
  • loans.rs / process_local: before the define, if the initializer hands back the storage of the binding it shadows, read that binding's origin with non_owned_origin_of. "Hands back" means any form the alias / may-share recorders already link: a copy, paren, re-slice, reference-bearing projection, composite literal, or a non-move call argument or receiver whose result may share. The new binding inherits that origin if it has none, and only when its shape may share. Scalar shadows (n := n) and copy-outs (m := map[string]int{"k": m["k"]}) are untouched.
  • SPEC-LANGUAGE.md O5.4: one sentence on the exclusively-owned requirement, including the self-shadow case.

Tests

New compiler/crates/hy-check/tests/self_shadow_origin.rs (4 tests):

  • rejects HYB-E3004 / A1b and names the shadowed binding: m := m, (m), var m map[string]int = m, s := s[:], m := same(m), struct h := h, projection o := o.h, literal h := holder{m: h.m}, move-send ch <- move m, receiver s := s
  • still accepts (negative controls): n := n, a scalar copy-out into a fresh map, a fresh value under the same name, a shadow with no boundary move, and a self-shadow of an owned local moved into a task

On unpatched main, 3 of the 4 tests fail: every reject case is accepted. The control test passes on both.

Local runs, with pinned go1.26.9 / rust 1.99.0:

cd compiler && cargo test --workspace --quiet      # all ok, 326 tests (322 on main + 4 new), 0 failed
python3 tools/corpus_harness.py --bin $HYB         # verdict agreement 31/31, tripwire not fired
python3 tools/differential_loop.py --bin $HYB      # accept-side 10/10, reject-side 21/21, FULL AGREEMENT
python3 tools/managed_gate.py $HYB gates/managed-baseline.json tests/checker-corpus/02-shared-map-managed.hy tests/checker-corpus/18-pool-shared-struct-managed.hy   # PASS
bash tools/run_examples.sh                         # examples: ALL PASS (01-10)
python3 -m unittest discover -s tools -p 'test_*.py'   # differential / managed_gate / empty_corpus OK
bash tools/check-versions.sh                       # CONSISTENT

I also ran hyb check --json on all 55 tracked .hy files in the repo with the main binary and the patched binary. Diagnostics are identical on every file: no verdict changes outside the new tests.

Scope note: this PR covers the flat pass only. The closure-boundary pass (closures.rs) has its own Stmt::Local path, which is #87 (repro 3) / #137 and is addressed by #88. I left it alone so the two changes don't collide.

Proposed tier: High (an accepted data race through a common Go idiom; the reach is narrower than a blanket bypass because it needs a non-owned binding plus an inner-block re-declare). Maintainer's call.

Fixes #141

…on-owned origin (HYB-E3004)

`m := m` in an inner block reads the OUTER binding, but process_local
defined the new binding first and only then recorded alias / may-share
links, which resolved `m` against the new inner binding itself
(record_alias saw `m -> m`, share_edge dropped the self-edge). The outer
parameter / receiver / range variable's non_owned_origin was lost, so

    fn spawn(m map[string]int, done chan bool) {
        if true {
            m := m
            task worker(move m, done)
        }
    }

was accepted with 0 diagnostics while the caller kept writing the same
map (go vet clean, `go run -race`: WARNING: DATA RACE). `move m` of the
parameter itself, or a differently named copy `m2 := m`, already
rejected HYB-E3004.

process_local now reads, before the define, the origin the shadowed
binding would cite (check_non_owned_move's origin computation, factored
into non_owned_origin_of) whenever the initializer hands back that
binding's storage in a form the alias / may-share recorders link (copy,
paren, re-slice, reference-bearing projection, composite literal, call
argument / receiver), and gives it to the new binding.

Tests: hy-check/tests/self_shadow_origin.rs (param, receiver, struct,
slice, call, projection, literal, move-send; scalar / fresh / owned
negative controls). SPEC-LANGUAGE O5.4 note.
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] Self-shadowing define (m := m) in an inner block drops the parameter/receiver non_owned_origin: move m accepted, data race

2 participants