Skip to content

Box[dyn Trait] never runs the boxed value's Drop and leaks its allocation #1847

Closed

Description

@ehartford

A Box[dyn Trait] never runs the boxed value's Drop and leaks the box's allocation (found fixing #1827; verified on b4957bba, the parent of the fix-globals stack).

use std.box.Box

trait Named:
    fn name(self: &Self) -> i32

type Tok:
    n: i32

impl Named for Tok:
    fn name(self: &Self) -> i32: self.n

impl Drop for Tok:
    move fn drop(): print(f"drop {self.n}")

fn main:
    {
        let b: Box[dyn Named] = Box.new(Tok { n: 1 })
        print(f"{b.name()}")
    }
    print("end")

with run --debug-alloc (b4957bba):

1
end
debug-alloc: LEAK addr=4334190608 size=16 origin=with_alloc
debug-alloc: leak count=1

Expected (§2.4: the value is dropped when its owner goes out of scope; With calls a leak a defect): 1, drop 1, end, leak count=0.

Sema's #1827 rule already treats the drop of a dyn Trait value as running any implementor's drop (push_drop_glue_bodies), so a view of a global live across that drop is refused once the drop runs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions