Repository navigation
MIR: a generic receiver reached through a reference is borrowed at the reached place (#1443) - #1461
Merged
Conversation
…e place the autoderef walk reached (#1443) `fn sum(l: &L) -> i64: match l: L.Cons(v, next) => v + sum(next.as_ref())` returned the head only (sum=1 for 1,2,3) and the `Expr.Neg` variant crashed (139). Route: lowering bug on a compilable input — analyze, then lldb on stage2. Evidence. `analyze r1443a.w 'matrix:name~as_ref'`: the MIR call-argument for `Box.as_ref__receiver__146_135` is `copy _8.*`, type 146 (Box[L]), while the specialization's param[0] is type 453 (&Box[L]); codegen passes it as a direct value — a Box and `&Box` are both `ptr`, so `as_ref` read the list node as the box. lldb on `with-stage2 check` with a breakpoint on MirBuilder.lower_generic_receiver_arg conditioned on the receiver node (w1 == 7623), stepping: 10195 (autoderef walk returns `next.*`), 10198, 10199 (`actual = expr_type(next)` = &Box[L]), 10200 returns raw_op — no autoref. Root cause. MirLower.w lower_generic_receiver_arg judged the `&Self` contract against the receiver expression's type. The walk had already stepped through the reference to `next.*`; the expression is `&Box[L]`, so `can_auto_ref_arg(&Box[L], &Box[L])` said no and the Box value was passed. The contract is now judged against the reached place's type, and the place is borrowed. Tool gap. The typed MIR validator accepted the value-for-`&T` argument. It now rejects a monomorphized generic call whose argument is the value `T` where the callee's parameter is `&T` (direct calls keep the place-value convention: codegen takes a struct's address). Built with only the validator and the old lowering: g03 (`b: &Box[i64]`, `b.as_ref()`), a generic `Wrap[T].get` through `&Wrap`, and `Rc.as_ref` through `&Rc` all fail "invalid MIR before codegen: call argument 0 is a value where the callee parameter is a reference to it (a missing borrow)"; the compiler's own MIR validates clean. mir_generic_call_missing_borrow_test pins the rule on hand-built MIR. Matrix (base = stage1 of 1c76062+#1406+#1444): list sum via `&L` (1 -> 6), Expr eval with Neg/Add (139 -> 7), `&Box[i64]` param (139 -> 41), `&&Box` (139 -> 10), tree of `Box[T]` with str leaves (139 -> "(a (b c))"), `for bx in Vec[Box[i64]]` (crash -> 7); unchanged and correct: `&Wrap[T]` generic methods, `&Rc`, a Box field of `&Node`, generic methods through `Box[Wrap[T]]` deref. All leak count=0 except an owned-subject case that leaks on base too: dropping a Box of a recursive enum does not drop the Box inside it (#1456, filed). A stage2 built by the fixed stage1 validates src/main.w (`check --validate-all`: ok). Fixtures: behav_ref_box_generic_receiver (exact stdout), da_ref_box_recursive_walk (leak count=0), mir_generic_call_missing_borrow_test.
ehartford
force-pushed
the
fix-1443-ref-box-receiver
branch
from
September 23, 2026 09:32
7f457c5 to
7206302
Compare
ehartford
force-pushed
the
fix-1443-ref-box-receiver
branch
from
September 23, 2026 09:57
7206302 to
463925b
Compare
This was referenced Sep 23, 2026
Contributor
Author
|
Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1443. Stack #1463, above #1460.
Bug.
match l: L.Cons(v, next) => v + sum(next.as_ref())overl: &Lreturned the head only (sum=1).Expr.Negrecursion crashed (139).Evidence.
analyze 'matrix:name~as_ref': the MIR call-argument iscopy _8.*of type Box[L], butas_ref's param is&Box[L]. Codegen passes it as a direct value; Box and&Boxare bothptr. lldb (breakpoint onlower_generic_receiver_argfor the receiver node, stepping) shows it returning the autoderef'd place at MirLower.w:10200 without autoref.Root cause.
lower_generic_receiver_argjudged the&Selfcontract against the receiver expression's type (&Box[L], already a reference) instead of the place the autoderef walk reached (next.*, aBox[L]).Fix. Judge against the reached place and borrow it. Tool gap: the typed MIR validator now rejects a monomorphized generic call that passes the value
Tfor a&Tparameter. With the old lowering it fires on&Box,&Wrap[T]and&Rcreceivers, and the compiler's own MIR validates clean. Pinned bymir_generic_call_missing_borrow_test.Matrix. Fixed, all crashing or wrong on base:
&L: 6&Box[i64]param: 41&&Box: 10Box[T]with str leaves:(a (b c))for bx in Vec[Box[i64]]: 7Unchanged and correct:
&Wrap[T],&Rc, a Box field of&Node, generic methods throughBox[Wrap[T]]. A stage2 from the fixed stage1 validates src/main.w and passes 135 box/generic/receiver/trait tests.Fixtures.
behav_ref_box_generic_receiver,da_ref_box_recursive_walk(leak count=0),mir_generic_call_missing_borrow_test.Filed. #1456 (a dropped Box of a recursive enum leaks the inner Box; reproduces on base).
Draft until the batch battery is green.