Destructuring Droppable structs

As discussed in the Internals thread for this RFC, the issue with "just don't call Drop upon partial move" is that whether an assignment is a partial move or not depends on whether the field type is Copy. And that can change in any minor version, and can also depend on lifetimes, which aren't allowed to influence monomorphization:

#[derive(Clone)]
struct Foo<'a>(&'a ());

impl Copy for Foo<'static> {}

struct Bar<'a>(Foo<'a>);

impl<'a> Drop for Bar<'a> {
    fn drop(&mut self) {
        println!("goodbye world");
    }
}

fn do_thing<'a>(bar: Bar<'a>) {
    drop(bar.0); // This is a move only when `'a` is not `'static`
}

(interestingly, it seems the compiler has a bug here: Implementing `Copy` can be a breaking change · Issue #126179 · rust-lang/rust · GitHub)

3 Likes