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

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

Closed
luqmana opened this issue Jun 1, 2013 · 5 comments · Fixed by #13390
Closed

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

luqmana opened this issue Jun 1, 2013 · 5 comments · Fixed by #13390
Labels
A-destructors Area: Destructors (`Drop`, …) A-traits Area: Trait system A-typesystem Area: The type system P-medium Medium priority
Milestone

Comments

@luqmana
Copy link
Member

luqmana commented Jun 1, 2013

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.

@bblum
Copy link
Contributor

bblum commented Jun 11, 2013

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

@emberian
Copy link
Member

emberian commented Aug 5, 2013

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); }
}

@chris-morgan
Copy link
Member

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

@nikomatsakis
Copy link
Contributor

I think we should just remove "dest=ignore"

@pnkfelix
Copy link
Member

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

alexcrichton added a commit to alexcrichton/rust that referenced this issue Apr 8, 2014
Previously, if statements of the form "Foo;" or "let _ = Foo;" were encountered
where Foo had a destructor, the destructors were not run. This changes
the relevant locations in trans to check for ty::type_needs_drop and invokes
trans_to_lvalue instead of trans_into.

Closes rust-lang#4734
Closes rust-lang#6892
bors added a commit that referenced this issue Apr 16, 2014
Previously, if statements of the form "Foo;" or "let _ = Foo;" were encountered
where Foo had a destructor, the destructors were not run. This changes
the relevant locations in trans to check for ty::type_needs_drop and invokes
trans_to_lvalue instead of trans_into.

Closes #4734
Closes #6892
flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 25, 2021
inconsistent_struct_constructor: try to make message and lint description a bit clearer

changelog: inconsistent_struct_constructor: try to make message and lint description a bit clearer

r? `@ghost`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: Destructors (`Drop`, …) A-traits Area: Trait system A-typesystem Area: The type system P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants