Skip to content

Box.as_ref() broken: primitive box run-fails, struct box segfaults (auto-deref path works) #627

Closed

Description

@ehartford

Summary

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).

Spec

§3.7 auto-deref / §8 Box surface — as_ref should return &T.

Repro

fn main:
    let b = Box.new(42)
    print_i32(b.as_ref())   // run failed; struct variant segfaults (139)

Found 2026-07-03 while building #600 controls.

Activity

  1. added a commit that references this issue on Jul 4, 2026
  2. ehartford commented on Jul 5, 2026

    @ehartford
    ContributorAuthor

    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/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.

  3. ehartford commented on Jul 5, 2026

    @ehartford
    ContributorAuthor

    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.

  4. added 4 commits that reference this issue on Jul 5, 2026
    aa0a0d1
    5b1d74c
    f68403f
    fcb3d38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions