Skip to content

MIR: a generic receiver reached through a reference is borrowed at the reached place (#1443) - #1461

Merged
ehartford merged 1 commit into
fix-1444-disc-repr-matchfrom
fix-1443-ref-box-receiver
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1444-disc-repr-matchfrom
fix-1443-ref-box-receiver

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1443. Stack #1463, above #1460.

Bug. match l: L.Cons(v, next) => v + sum(next.as_ref()) over l: &L returned the head only (sum=1). Expr.Neg recursion crashed (139).

Evidence. analyze 'matrix:name~as_ref': the MIR call-argument is copy _8.* of type Box[L], but as_ref's param is &Box[L]. Codegen passes it as a direct value; Box and &Box are both ptr. lldb (breakpoint on lower_generic_receiver_arg for the receiver node, stepping) shows it returning the autoderef'd place at MirLower.w:10200 without autoref.

Root cause. lower_generic_receiver_arg judged the &Self contract against the receiver expression's type (&Box[L], already a reference) instead of the place the autoderef walk reached (next.*, a Box[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 T for a &T parameter. With the old lowering it fires on &Box, &Wrap[T] and &Rc receivers, and the compiler's own MIR validates clean. Pinned by mir_generic_call_missing_borrow_test.

Matrix. Fixed, all crashing or wrong on base:

  • list sum via &L: 6
  • Expr eval: 7
  • &Box[i64] param: 41
  • &&Box: 10
  • tree of Box[T] with str leaves: (a (b c))
  • for bx in Vec[Box[i64]]: 7

Unchanged and correct: &Wrap[T], &Rc, a Box field of &Node, generic methods through Box[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.

@ehartford
ehartford added this pull request to stack #1463 September 23, 2026 08:41
@ehartford ehartford changed the title fix 1443 ref box receiver MIR: a generic receiver reached through a reference is borrowed at the reached place (#1443) Sep 23, 2026
…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

Copy link
Copy Markdown
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: gh stack merge 1462 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 09:58
@ehartford
ehartford merged commit fb52dd7 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1443-ref-box-receiver branch September 24, 2026 16:55
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.

Silent wrong value: recursing through a Box payload bound from a &Enum match subject (sum over a Box list returns the head only)

1 participant