Skip to content

Destructuring a Drop-typed value by pattern (let { .. } / match) never runs its Drop — a silent leak #1272

Closed

Description

@ehartford

Found while implementing modeled-C stage 4a (facade resources rendered with Drop), while looking for a spelling that lets a move fn destroyer consume self without the type's Drop running. Pattern destructuring turns out to be such a spelling, and that is the bug: it disarms Drop for any Drop type, silently, with no ceremony.

Repro

var count: i32 = 0
fn c_destroy(p: i32): count = count + 1
type R { repr: i32 }
impl Drop for R:
    move fn drop(): c_destroy(self.repr)
fn main:
    let r = R { repr: 7 }
    let { repr } = r
    print(f"let-destructure {repr} drops={count}")
    let s = R { repr: 8 }
    match s:
        R { repr: v } => print(f"match-destructure {v} drops={count}")
    print(f"end drops={count}")

with run (installed compiler, main 478b264):

let-destructure 7 drops=0
match-destructure 8 drops=0
end drops=0

Expected: count == 2 by the end of main — r and s are R values with a Drop, and nothing consumed them into another owner. The same program with let t = r in place of the destructure drops twice.

Why it matters

  • Mission: "Rust calls leaking safe, With calls it a defect. Leaking must take deliberate, visible effort." Here a leak costs a struct pattern.
  • Rust refuses this outright (E0509: cannot move out of a type that implements Drop); Vale runs the destructor on destructure of a type with one. Either is defensible; silently skipping the destructor is not.
  • Spec §Patterns "let destructuring" says nothing about Drop types; a ruling is needed (refuse, or run Drop after binding the Copy fields / moving the non-Copy ones).

Route

Drop-scheduling bug, so it is its own batch with :move-audit/:drop-audit. --dump-drop-plan on the repro should show no scheduled drop for r/s after the pattern; the destructure path in MirLower treats the pattern as consuming the whole value without emitting the owner's Drop.

Stage 4a does not rely on this: the rendered resource uses an explicit live arming bit (ruling §13.2), tested by behav_c_facade_resource_destroys_once.w.

Activity

  1. ehartford commented on Sep 22, 2026

    @ehartford
    ContributorAuthor

    Diagnosis (exact lines): the pattern subject is moved into a scratch place whose scheduled drop is cancelled by the #605/#606 payload machinery with no check for a user Drop — src/MirLower.w:5896-5908 (lower_let_else, irrefutable let pattern) and the lower_match "arms own the subject's payloads" block (~:9073-9078); lower_if_let (:6274) shares the shape (then-path leaks, fail path drops). --dump-mir: _3 = move _1; _4 = copy _3.repr; _1 = const zst; --dump-drop-plan: no drop site for _3.

    Matrix (installed compiler): let { .. } = r / let { repr } = r / match r: R { repr: v } => / if let R {..} = r (then-path) skip Drop; let _ = r, let t = r, match r: _ => run it; inside the type's own move fn/drop, destructuring bypasses Drop (the disarm spelling stage 4a wanted).

    Spec: §2.2 "whole values decompose whole", §2.4 drop runs at scope end, §2.5.1 leaking is never inaction — rules out today's behavior, silent on the replacement. References: Rust E0509 refuses any move out of a Drop type; Vale's owning destructure bypasses a user drop everywhere (PatternCompiler.scala:502); Swift "cannot partially consume when it has a deinitializer" except discard self in a consuming method; Mojo __disable_del self only in the type's own methods.

    Ruling needed — what a struct/enum pattern does to a Drop value outside that type's own drop. Proposed (Swift/Mojo shape, predicted ~75%): a compile error with fix-its (let t = r / r.field) everywhere except inside the type's own move fn methods, where the destructure is the visible disarm; every spelling (let, match, if let) shares the one rule. Fix shape once ruled: Sema check at pattern-subject resolution keyed on type_has_drop_impl + enclosing move fn of the subject type; MirLower unchanged.

  2. added a commit that references this issue on Sep 22, 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