Skip to content

A &P field of a mut self receiver is moved and blanked when P: Drop — reading self.db into a child nulls the receiver's field (segfault) #1492

Closed

Description

@ehartford

Spec: §2.2 / §2.5.1 (reset-on-move applies to moves of owned values), §3 (a &T is Copy). A &Db field read inside a mut fn of an ephemeral struct must be a copy; the field must keep its value.

Repro (seed 7c3ed983 / installed v0.15.2.1, and stage1 at ba2d5d1 — pre-existing):

type Db { n: i32, live: bool }
impl Drop for Db:
    move fn drop():
        if self.live: print(f"close db {self.n}")
type Stmt = ephemeral { parent: &Db, n: i32 }
impl Stmt:
    fn step(): self.parent.n * 100 + self.n
type Cache = ephemeral { db: &Db, stmts: Vec[Stmt] }
impl Cache:
    mut fn step(id: i32) -> i32:
        self.stmts.push(Stmt { parent: self.db, n: id })
        self.stmts[self.stmts.len() as i32 - 1].step()
fn Cache.over(db: &Db) -> Cache: Cache { db: db, stmts: Vec.new() }
fn main:
    let db = Db { n: 1, live: true }
    var cache = Cache.over(db)
    let b = cache.step(11)
    let c = cache.step(10)
    print(f"{b} {c}")

Expected: 111 110, then close db 1.
Actual: exit 139. lldb: EXC_BAD_ACCESS (address=0x0) in Stmt.step (self.parent.n) on the second cache.step — the second statement's parent is null.

Evidence (with check --dump-mir), Cache.step bb0/bb1:

_3 = aggregate(kind=0, tag=0, fields=[move _1.db, copy _2]);
call const fn sym52(copy _1.stmts, move _3) -> [return: _4, next: bb1];
bb1:
_3 = const zst(ty237);
_1.db = const zst(ty236);          // <- the receiver's `&Db` field is blanked

The same program with type Db { n: i32 } (no Drop) lowers copy _1.db and has no blank, and prints 111 110. So the &Db field of the mut self receiver is treated as a moved owned field and reset-on-move nulls the caller's cache.db.

Hypothesis, not yet the exact line: Sema.get_type_name (SemaCheck.w ~26093) returns the pointee's name for TY_REF/TY_PTR, and type_has_drop_impl (Sema.w ~5911) keys on get_type_name, so type_has_drop_impl(&Db) answers 1 whenever Db: Drop; type_needs_drop(&Db) follows. Some field-operand path in MirLower then picks move/reset for the field. Needs lldb on the branch that built move _1.db to confirm.

Impact: any ephemeral struct holding &P of a Drop type and creating children from that field in a mut fn corrupts itself — e.g. a statement cache over a modeled-C Database (D51 stage 6). Ownership/drop change: its own isolated batch with :drop-audit/:move-audit.

Workaround used: modeled-C stage 6's statement-cache fixture (test/behavior/behav_c_facade_statement_cache.w) reborrows the field explicitly, Statement.st_new(&*self.db, id), which lowers as a copy. A call argument (Stmt.p(self.db, id)) and a typed local (let db: &Db = self.db) crash the same way. Remove the reborrow when this is fixed.

Found while implementing modeled-C stage 6; not fixed there (out of scope).

Activity

  1. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    Stage 6's PR #1519 adds type_owns_user_drop (Sema.w), a ref-blind twin of type_carries_user_drop, for the Rule 7 check: type_has_drop_impl(&W) answering as for W made a non-Drop view holding &Statement report as an implicit drop. The fix for this issue should make type_has_drop_impl/type_needs_drop stop at references and retire that twin.

  2. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Fixed by #1718 (Sema: type_has_drop_impl is false for TY_REF/TY_PTR, so a &P field is copied, not moved). Verified on main 3ccb998: the repro prints the expected 111 110 then close db 1, leak count=0.

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