Skip to content

Reading Box[T].ptr traps at run time (SIGTRAP) and fails codegen inside move fn drop (actual=i32 expected=ptr) #1280

Closed

Description

@ehartford

Found while rendering D54's pinned in-place resources over Box[T] (PR #1275). The rendering uses Box.as_ptr()/as_mut_ptr(), so 4b is not blocked; filed, not fixed.

Repro (installed with, main 478b264 + #1274)

use std.box.Box
type Cell { a: i32 }
fn main:
    let b = Box.new(Cell { a: 7 })
    let p = b.ptr
    print(f"{unsafe { (*p).a }}")

with check says ok; with run traps (rc=133, SIGTRAP) with no output. Replacing b.ptr with b.as_ptr() prints 7.

The same read inside a move fn drop() of a struct holding the Box fails at codegen instead:

use std.box.Box
type Cell { a: i32 }
type W { repr: Box[Cell] }
impl Drop for W:
    move fn drop():
        let p = self.repr.ptr
        print(f"{unsafe { (*p).a }}")
fn main:
    let w = W { repr: Box.new(Cell { a: 7 }) }
    drop(w)

with build (a variant handing self.repr.ptr straight to an extern taking *mut Cell) reports error: wrong argument type actual=i32 expected=ptr fn=W.drop / code generation failed; the let p = form builds and traps at run time like the first program.

Expected vs actual

Box[T] is declared pub type Box[T] { ptr: *mut T } (lib/std/box.w); b.ptr is an ordinary field read of a *mut T and should yield the cell's address (spec §1 struct field access; §2.2 — a raw pointer is Copy, so nothing moves). Actual: the read lowers to something typed i32 (codegen's own message) and the program traps. Box's own methods avoid the field (self as *mut T), which is why nothing in std hits it; mir_emit_box_drop_place and the ptr as Box[T] cast suggest Box has a special lowering the field path does not share.

Workaround in use

Box.as_ptr() and the new Box.as_mut_ptr() (PR #1275). Remove once the field read is correct.

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