Skip to content

A Drop value whose bytes are all zero is indistinguishable from the reset-on-move sentinel: its destructor is skipped (Vec element, enum payload, moved value) #1431

Closed

Description

@ehartford

Summary

A Drop value whose live bytes are all zero is treated as a moved-out (reset) value, so its destructor never runs on the guarded drop paths.

Repro (seed at 8c859a7 / main 04e4934)

type Fd { n: i32 }
impl Drop for Fd:
    move fn drop():
        print(f"close {self.n}")

fn take(f: Fd):
    print(f"took {f.n}")

fn main:
    take(Fd { n: 0 })
    take(Fd { n: 5 })
    var v: Vec[Fd] = Vec.new()
    v.push(Fd { n: 0 })
    v.push(Fd { n: 7 })
    print("end")

Output:

took 0
close 0
took 5
close 5
end
close 7

Fd { n: 0 } in the Vec is never closed. The same happens for an enum payload: let e = E.B(14, Handle { p: null }) in a function never runs Handle.drop, while Handle { p: cell_new() } in the same position does. The MIR schedules drop(_1) for the enum local in both cases, so the drop is lost at the guarded destructor, not in drop elaboration.

Why

Spec §2.5.1: "The reset is unconditional (every move blanks its source) and the null guard is part of the destructor itself." The guard cannot tell a blanked value from a live value whose representation is all zero:

  • a file descriptor 0;
  • a null handle a user type legitimately owns;
  • an index of 0;
  • a by-value C token whose id is 0.

On a single never-moved owner the guard is elided (§2.5.2), so let f = Fd { n: 0 } in a function does close. On a moved value, or inside a Vec or an enum, it does not. The result is a skipped destructor: a leak, or a descriptor never closed. §2.5.1 calls that a defect.

Impact on modeled C (D51)

The facade renderings are not affected today, but only by construction:

  • a rendered resource carries live: true;
  • a failed-state resource (Failed<R>, stage 5) holds a non-NULL pointer.

A by-value C token resource that dropped the live byte would be exposed.

Question

This is the §2.5.1 mechanism, so it needs a ruling rather than a spot fix. The options:

  • a reserved blank bit or tag for types whose zero pattern is a valid live value;
  • live bits generalized by the compiler;
  • a spec sentence restricting what a Drop type's representation may be.

Activity

  1. ehartford commented on Sep 27, 2026

    @ehartford
    ContributorAuthor

    Ruled by Eric (D72, spec v7.5, PR #1784): a Drop type whose all-zero storage can be a live value gets a hidden liveness byte; types with an owning non-null field pay nothing. Implementation on the batch-2 ownership layer.

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