Skip to content

Place reassignment never drops the old contents — plain 'v = w' on a local leaks (shipped) #689

Closed

Description

@ehartford

Found during D12/#677 bring-up via the debug allocator; reproduces on the shipped compiler (v0.15.1-ga80c38c0c) with no D12 changes involved.

Repro (leaks 2 allocations today):

fn main:
    var v: Vec[i32] = Vec.new()
    v.push(1)
    var w: Vec[i32] = Vec.new()
    w.push(2)
    v = w
    print_i32(v.get(0))

with run --debug-alloc repro.w → LEAK ... origin=Vec, leak count=2. The old v contents are never dropped when the binding is reassigned.

Root cause (MIR level): assignment lowering emits the store but never a drop of the destination's old value; --dump-drop-plan shows only scope-exit drop records — there is no drop site at the reassignment statement. Scope-exit correctly drops the FINAL value; every intermediate value assigned over is leaked. The drop-flag machinery (Init/Moved states, conditional skip) already exists — what's missing is drop elaboration at assignment: place = value on a maybe-Init droppable place must drop the old contents first (flag-conditional), exactly like Rust's drop elaboration.

Blast radius: every reassignment of a heap-bearing local/place in every program. Also blocks two D12 surfaces deferred from #677: bare-self replacement on heap-bearing owners (mut fn reset(): self = Buf{...} — sema-gated off until this lands, see d12_mut_self_binding_allowed), and by extension part of #678 (str self-reassignment interacts with view rules but str contents are unowned slices).

Acceptance test parked (from the #677 cycle): a Buf { data: Vec[i32] } self-replacement matrix across straight-line/branch/loop/early-return that must run allocator-clean; plus the plain-local repro above. The #677 session transcript has both.

This is foundation work on drop scheduling — /drop-audit-class gating required (note: the drop-audit skill in .claude/skills/drop-audit/ was never checked into the repo and did not survive the machine switch — it should be committed to the repo or copied from the old machine).

Activity

  1. ehartford commented on Jul 19, 2026

    @ehartford
    ContributorAuthor

    INVESTIGATION COMPLETE — closing as a duplicate of #608's provisional by-design ruling, with the full mechanism now mapped. This is NOT a reassignment-specific bug, and my original framing ("shipped leak") was wrong in scope.

    What's actually true, verified at every layer:

    • Reassignment drop-before-overwrite exists and works for types under the drop gate: a struct with a user Drop impl reassigned over prints old-drop/new-value/final-drop exactly once each (1, 2, 2), allocator-silent, on the shipped compiler. The lowering guard is in lower_assign (MirLower.w ~4938) and its expr-position twin (~11592), with the §16.11/D8 raw-pointer exception.
    • POD-element containers (Vec[i32] etc.) never free their buffers ANYWHERE — not at reassignment, not at plain scope exit (var v = Vec.new(); v.push(1) leaks on the shipped binary). That is the A5/Soundness: Drop is not propagated through Option/Vec/array/tuple/enum contents → leak #606 narrow drop gate (Sema.type_needs_drop: Vec[T] needs drop iff T does), ruled by-design (provisional) in POD Vec[i32] buffer never freed at scope exit (debug-allocator finding) #608 (2026-07-02): the compiler pervasively copies POD Vec headers as cheap value handles, so freeing on drop would double-free shared buffers; the "wide flip" (all Vecs own their buffers) is a deliberate coordinated migration, explicitly deferred, to be refiled as its own project when scheduled. test/debug_alloc/da_pod_vec.w pins the leak-count=1 expectation.
    • I prototyped broadening the assign-site gate to the value-drop predicate: the Drop statements appear in MIR exactly as intended, and the allocator verdict is UNCHANGED — codegen's drop glue deliberately no-ops POD-element containers. The prototype was therefore inert-but-noise and is reverted; nothing lands from this cycle.

    What this means for the queue:

    Acceptance matrix from this cycle (reassignment cells across shapes/control-flow) is preserved in the session transcript for whenever the flip project is scheduled.

  2. added a commit that references this issue on Jul 19, 2026
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