Skip to content

Some struct destructors not run when Dest = Ignore. #6892

Closed

Description

@luqmana

While the destructor for newtypes always seems to run the same is not true for regular or unit-like structs.

struct Foo;
struct Bar { x: int }
struct Baz(int);

impl Drop for Foo {
    fn drop(&mut self) {
        println!("finalize Foo");
    }
}
impl Drop for Bar {
    fn drop(&mut self) {
        println!("finalize Bar");
    }
}
impl Drop for Baz {
    fn drop(&mut self) {
        println!("finalize Baz");
    }
}

fn main() {
    { let _x = Foo; }
    { let _x = Bar { x: 21 }; }
    { let _x = Baz(21); }

    println!("------------");

    { let _ = Foo; }
    { let _ = Bar { x: 21 }; }
    { let _ = Baz(21); }
}
-> % rust run test.rs
finalize Foo
finalize Bar
finalize Baz
------------
finalize Baz

I'm not sure which is the right behaviour.

Activity

  1. bblum commented on Jun 11, 2013

    @bblum
    Contributor

    possibly related to #3181 though obviously this is more severe.

  2. emberian commented on Aug 5, 2013

    @emberian
    Contributor

    Reproduces with:

    struct Foo;
    struct Bar { x: int }
    struct Baz(int);
    
    impl Drop for Foo {
        fn drop(&self) {
            println("finalize Foo");
        }
    }
    impl Drop for Bar {
        fn drop(&self) {
            println("finalize Bar");
        }
    }
    impl Drop for Baz {
        fn drop(&self) {
            println("finalize Baz");
        }
    }
    
    fn main() {
        { let _x = Foo; }
        { let _x = Bar { x: 21 }; }
        { let _x = Baz(21); }
    
        println("------------");
    
        { let _ = Foo; }
        { let _ = Bar { x: 21 }; }
        { let _ = Baz(21); }
    }
  3. chris-morgan commented on Oct 17, 2013

    @chris-morgan
    Member

    This still exhibits the same behaviour, though fn drop(&self) now needs to be fn drop(&mut self).

  4. nikomatsakis commented on Dec 19, 2013

    @nikomatsakis
    Contributor

    I think we should just remove "dest=ignore"

  5. pnkfelix commented on Dec 19, 2013

    @pnkfelix
    Contributor

    Accepted for "first major rel" milestone, assigning P-high.

  6. added a commit that references this issue on Apr 16, 2014
    74bd233
  7. added a commit that references this issue on Mar 25, 2021
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

    A-destructorsA-trait-systemA-type-systemP-medium

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions