Skip to content

A Drop value bound in a block statement or if arm is destroyed at function exit, not at the end of its scope #1532

Closed

Description

@ehartford

Found while writing a D60 fixture. Reproduced on origin/main 5848ae9 (stage1):

type Tracer { id: str }
impl Drop for Tracer:
    fn drop(move self: Self): print(f"drop {self.id}")
fn main:
    {
        let t = Tracer { id: "a".clone() }
    }
    print("after block")
    if true:
        let u = Tracer { id: "b".clone() }
    print("after if")

prints

after block
after if
drop b
drop a

Expected drop a before after block and drop b before after if: §11.7's syntax-trait table (Drop — destructor — automatic at scope exit) and §21.1 rule 7 ("The compiler inserts implicit drop points at scope exit in reverse declaration order"); the mission: "a resource lives in its scope and is released when the scope ends". A binding declared in a braced block statement or an if arm is destroyed at the function's exit instead. (A helper function's local drops at that function's end as expected.)

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Verified fixed on main 55739d4: a Drop value bound in an if arm is destroyed at the end of the arm (in block, drop 1, after block; --debug-alloc 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