You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Box.new(42) + b.as_ref() fails at run; Box.new(Owned{n:7}) + b.as_ref().n exits 139 (also on the current seed — pre-existing). The idiomatic auto-deref path (b.n, guard.id as behav_box_drop uses) works. Likely the same generic-arg resolution quirk noted in the #600 investigation ('as_ref() yields str' when annotated).
Box[T] { ptr: *mut T } is a single-field struct, so codegen represents it transparently as its inner pointer (Box ≡ T*; mir_sema_type_is_box, CodegenDispatch.w:4294). Box.as_ref/as_ptr/deref all use the self-reinterpret idiom *(self as *const *const T) as &T, which is correct only when self is &Box = T** (dereference once → the ptr field → the heap pointer).
Auto-deref path (b.n, *b) passes the receiver correctly as &Box (T**) → *(T**) = heap ptr → works.
Explicit generic &self call (b.as_ref(), b.as_ptr(), b.deref()) passes the receiver one indirection too shallow — as the box value T* (the heap ptr itself), not &Box. Then *(T* as **T) dereferences the heap pointer, reads the payload's first word (42, or Owned.n), and casts that to a reference → SIGSEGV (exit 139).
Isolation:
A generic &self method with ordinary field access (fn Pair.first[T](self: &Self) -> &T: &self.a) called explicitly works — so generic &self receiver passing is fine for non-transparent structs.
A user MyBox[T]{ptr} (non-transparent) with the identical self-reinterpret body called explicitly works.
Only the transparent stdlib Box fails — the transparency optimization collapses &Box to the box pointer for the explicit-call receiver operand.
The method body is correct; the defect is that codegen materializes the &self receiver operand for a transparent single-pointer type as the place value (T*) instead of the place address (T**).
Why the obvious stdlib fixes don't work
self.ptr as &T (field access) breaks the transparent box even on the working auto-deref path (returns garbage) — field projection on a transparent single-pointer struct doesn't yield the pointer as expected. Reverted.
The self-reinterpret body is correct for a correctly-passed &self; the fix belongs in the receiver-operand lowering, not box.w.
Scope
Codegen foundation work: transparent-box (mir_sema_type_is_box) + explicit generic &self receiver materialization must pass the place address, uniformly with the non-transparent case. Deferring per AGENTS.md (located at the representation level; fix needs codegen-foundation work). The primary Box surface (auto-deref b.n / *b) is unaffected and works.
Fixed in aa0a0d1. Explicit Box/Rc/Arc .as_ref()/.as_ptr()/.deref() passed the transparent-box receiver one indirection too shallow to the generic &self method (T* instead of &Box=T**), so the layout-hack body dereferenced the heap pointer as an address (lldb: fault at 0x2a for Box.new(42)). Root cause was mir_try_place_ptr_for_ref loading the box slot's pointer value; that load is required by the cast path (std.ffi.box_ctx), so the fix (mir_operand_place_addr, slot address without the load) is scoped to the two generic-method receiver sites for transparent box/rc/arc &self receivers. Auto-deref (b.n/*b) was always fine. Fixpoint + full suite green; 5 fixtures pin the matrix.
Activity
Fix #600: ephemeral values can no longer escape into heap-owning ctors
ehartford commentedon Jul 5, 2026
Root cause (located at the representation level)
Box[T] { ptr: *mut T }is a single-field struct, so codegen represents it transparently as its inner pointer (Box ≡ T*;mir_sema_type_is_box, CodegenDispatch.w:4294).Box.as_ref/as_ptr/derefall use the self-reinterpret idiom*(self as *const *const T) as &T, which is correct only whenselfis&Box=T**(dereference once → theptrfield → the heap pointer).b.n,*b) passes the receiver correctly as&Box(T**) →*(T**)= heap ptr → works.&selfcall (b.as_ref(),b.as_ptr(),b.deref()) passes the receiver one indirection too shallow — as the box valueT*(the heap ptr itself), not&Box. Then*(T* as **T)dereferences the heap pointer, reads the payload's first word (42, orOwned.n), and casts that to a reference → SIGSEGV (exit 139).Isolation:
&selfmethod with ordinary field access (fn Pair.first[T](self: &Self) -> &T: &self.a) called explicitly works — so generic&selfreceiver passing is fine for non-transparent structs.MyBox[T]{ptr}(non-transparent) with the identical self-reinterpret body called explicitly works.Boxfails — the transparency optimization collapses&Boxto the box pointer for the explicit-call receiver operand.The method body is correct; the defect is that codegen materializes the
&selfreceiver operand for a transparent single-pointer type as the place value (T*) instead of the place address (T**).Why the obvious stdlib fixes don't work
self.ptr as &T(field access) breaks the transparent box even on the working auto-deref path (returns garbage) — field projection on a transparent single-pointer struct doesn't yield the pointer as expected. Reverted.&self; the fix belongs in the receiver-operand lowering, not box.w.Scope
Codegen foundation work: transparent-box (
mir_sema_type_is_box) + explicit generic&selfreceiver materialization must pass the place address, uniformly with the non-transparent case. Deferring per AGENTS.md (located at the representation level; fix needs codegen-foundation work). The primary Box surface (auto-derefb.n/*b) is unaffected and works.ehartford commentedon Jul 5, 2026
Fixed in aa0a0d1. Explicit Box/Rc/Arc
.as_ref()/.as_ptr()/.deref()passed the transparent-box receiver one indirection too shallow to the generic&selfmethod (T*instead of&Box=T**), so the layout-hack body dereferenced the heap pointer as an address (lldb: fault at 0x2a for Box.new(42)). Root cause wasmir_try_place_ptr_for_refloading the box slot's pointer value; that load is required by the cast path (std.ffi.box_ctx), so the fix (mir_operand_place_addr, slot address without the load) is scoped to the two generic-method receiver sites for transparent box/rc/arc&selfreceivers. Auto-deref (b.n/*b) was always fine. Fixpoint + full suite green; 5 fixtures pin the matrix.Fix #627: Box/Rc/Arc .as_ref()/.as_ptr()/.deref() segfault on explici…
Make friendly aliases (Int/UInt/String/StrView/CStr) user-shadowable (#…
Analysis: method-resolution facts (Tool Gap #2); MirLower: scope.trac…
MirLower: generic-method receivers honor the instantiated signature (…