Skip to content

Destructor for @Trait is not run in certain circumstances #10802

Closed
@Blei

Description

@Blei

I'm not sure if this is a duplicate of an existing issue, there are many destructor related issues open already. But anyways, here it is:

#[feature(managed_boxes)];

struct DroppableStruct { a: int }
impl Drop for DroppableStruct {
    fn drop(&mut self) {
        println!("Dropping");
    }
}

trait MyTrait { }
impl MyTrait for DroppableStruct {}


struct Whatever<'a> { w: &'a MyTrait }
impl <'a> Whatever<'a> {
    fn new<'b>(w: &'b MyTrait) -> Whatever<'b> {
        Whatever { w: w }
    }
}

fn main() {
    let mut f = DroppableStruct;
    let _a = Whatever::new(@f as @MyTrait);
}

This doesn't run the destructor of DroppableStruct. This is pretty much as minimal as I could get the example, many changes make it work. E.g. changing the main function body to:

    let f = DroppableStruct { a: 1 };
    let b = @f as @MyTrait;
    let _a = Whatever::new(b);

makes the destructor work. Also, not saving the Whatever struct in a variable makes it work. Also, not saving the @MyTrait in Whatever makes it work. Good luck.

Update: removed mut, was not relevant.
Update2: added a field to the struct to show it's not an issue about zero-sized structs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions