Skip to content

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

Closed

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.

Activity

  1. emberian commented on Dec 4, 2013

    @emberian
    Contributor
  2. flaper87 commented on Apr 14, 2014

    @flaper87
    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.

  3. emberian commented on Apr 14, 2014

    @emberian
    Contributor

    @flaper87 is there a testcase which covers this?

  4. flaper87 commented on Apr 14, 2014

    @flaper87
    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);
    }
  5. added a commit that references this issue on Apr 28, 2014
    35f295d
  6. added a commit that references this issue on Apr 28, 2014
    7a19a82
  7. added a commit that references this issue on May 20, 2023
  8. added a commit that references this issue on Aug 21, 2026
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

    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