Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Blei opened this issue Dec 4, 2013 · 4 comments
Closed

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

Blei opened this issue Dec 4, 2013 · 4 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@Blei
Copy link
Contributor

Blei commented Dec 4, 2013

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.

@emberian
Copy link
Member

emberian commented Dec 4, 2013

cc @nikomatsakis

@flaper87
Copy link
Contributor

It's no longer possible to cast to @-traits as of b2d30b7

I tried to reproduced this bug using ~ and & pointers but it worked as expected.

@emberian
Copy link
Member

@flaper87 is there a testcase which covers this?

@flaper87
Copy link
Contributor

@cmr I had seen it before but I can't find it anymore. Re-opening and flagging as needs test:

struct DroppableStruct;

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

trait MyTrait { }
impl MyTrait for ~DroppableStruct {}

struct Whatever { w: ~MyTrait }
impl  Whatever {
    fn new(w: ~MyTrait) -> Whatever {
        Whatever { w: w }
    }
}

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

@flaper87 flaper87 reopened this Apr 14, 2014
bors added a commit that referenced this issue Apr 28, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants