Repository navigation
fix(hy-check): track nested address-of borrows in composites and enforce Rule O12 - #312
Conversation
…12 container restrictions - Fix soundness hole where taking the address of a local (&x) inside a slice, map, or struct literal dropped borrow aliases, bypassing A1b move freeze (HYB-E3003), T22 const immutability (HYB-E3006), and O5.1 non-owned move restrictions (HYB-E3004). - Add Expr::AddrOf and Expr::Unary(Addr) handling in collect_ref_roots. - Introduce collect_addr_of_roots to recursively extract address-of identifiers from composite literals, slice expressions, and call arguments. - Propagate share edges and alias bindings for composite address-of expressions in loans.rs. - Enforce Rule O12 on nested pointer and borrow-handle container types (e.g. []*T, map[K]*T) in struct fields (HYB-E3103). - Add comprehensive integration test suite pointer_borrow_composites.rs with 10 tests.
…with positive controls
Multi-Root Borrow and Deep Nesting Hardening UpdateTo ensure complete soundness when tracking address-of borrow roots across complex expressions:
|
… results and restrict composite borrow roots
|
Wave-2 triage: queued — next tier. Nested address-of borrows in composites + Rule O12 enforcement; new rule needs a spec cross-check before landing. |
|
Pushed an empty commit ( |
|
Triage (agent session-15/2-a, validated against main @ c53978b): VALIDATED — READY TO LAND (with the maintainer's pending O12 spec cross-check). Hard repros (both accepted with 0 diagnostics on main, both rejected with HYB-E3003 on this PR's binary): (1) Battery: Per the earlier wave-2 note: Rule O12 enforcement is new policy, so the spec cross-check (GUARANTEES/SPEC borrow section wording) is the one open item before merge. Thank you — the negative_regression_* tests encode exactly the hole I could reproduce on main. |
# Conflicts: # compiler/crates/hy-check/src/loans.rs
…sable slice Merge pr-319 (Taurahh, fixes #318): freeze the array behind an addressable slice `binding_may_share` is false for a primitive array ([N]int copies its elements), but a slice of that array views its storage: s := a[:], s := b.data[:], row := g[i][:], append(a[0:1], x). The define-alias decision now also links the share edge when expr_slices_addressable_array(value, root) holds, so a later `move s` freezes the array `a` too (E3003). Integration: main's O9 handle-continue + moved_non_owned_origin + (#312's landed) addr_roots loop kept; the PR's disjunct grafted into the share_edge condition. Corpus fixture renumbered 46 -> 53 (main took 46-49 for #185's rejects); parse_all 53; VERDICTS row renumbered. Evidence (main @ 8f0a152 vs merged tree): `var a [4]int; s := a[:]; task worker(move s, done); a[1] = 2` - 0-diagnostic bypass on main -> REJECT HYB-E3003 (write through a live alias of a moved value); copy control `b := a` accepts both. Suites: cargo 665/0, corpus 53/53. CI green on 09c621d. Fixes #318.
Summary
This PR fixes a soundness hole where explicit address-of borrows embedded in composite literals were not fully propagated through the borrow checker.
Before the fix, moving a composite containing references to local storage could fail to freeze the borrowed local, permitting subsequent access that can race with another task.
The patch also extends borrow-shape validation for nested pointer/borrow containers in struct fields.
Root Cause
Two related gaps were involved:
collect_ref_rootsdid not account for explicit address-of expressions.Local/assignment borrow tracking only recognized simple top-level address-of expressions, so explicit borrows embedded in composite literals such as slices, maps, and structs were not recorded correctly.
Nested container shapes containing pointers or borrow handles were also not recursively recognized by field-borrow validation.
Current Implementation
At current HEAD
6fce9bb4:collect_addr_of_rootsfollows only syntax that actually preserves an explicit borrow into the resulting value:Try;Move;It deliberately does NOT recursively descend through ordinary:
This restriction prevents false positives such as:
where a scalar result must not become an alias of
n.record_local_borrow_aliasesandrecord_assign_borrow_aliasesare shape-gated before creating borrow aliases.field_borrow_kindrecursively recognizes borrow-bearing shapes through slices, arrays, and maps for O12 / field validation.Soundness Coverage
The regression suite covers:
The final adversarial validation additionally verified:
is rejected with
HYB-E3003;is rejected with
HYB-E3003;Current dedicated suite:
25 passed, 0 failedNo production-code changes were required after the final adversarial gate.
Verification
Current HEAD has passed:
pointer_borrow_compositesregression suite;cargo test -p hy-check;git diff --check.The final adversarial gate found no remaining false negative in the tested container-extraction or projection paths and no regression requiring an additional production patch.