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

Adding a drop impl breaks drop order #42222

Closed
main-- opened this issue May 25, 2017 · 2 comments
Closed

Adding a drop impl breaks drop order #42222

main-- opened this issue May 25, 2017 · 2 comments

Comments

@main--
Copy link
Contributor

main-- commented May 25, 2017

struct Context;

struct Foo<'a>(&'a Context);
struct Bar<'a>(&'a Foo<'a>);
struct Baz<'a>(&'a Context);

impl<'a> Drop for Baz<'a> {
    fn drop(&mut self) {
    }
}

impl<'a> Bar<'a> {
    fn next(&mut self) -> Option<Baz<'a>> {
        None
    }
}

fn util(ctx: &Context) {
    let foo = Foo(ctx);
    let mut bar = Bar(&foo);
    
    match bar.next() { _ => () } // this fails
    // return match bar.next() { _ => () }; // this works
}
error: `foo` does not live long enough
  --> <anon>:24:1
   |
20 |     let mut bar = Bar(&foo);
   |                        --- borrow occurs here
...
24 | }
   | ^ `foo` dropped here while still borrowed
   |
   = note: values in a scope are dropped in the opposite order they are created

error: aborting due to previous error

Playground

Note how the message even states that bar is dropped first (reverse order) while complaining that it outlives foo.

This works fine if you remove Baz's Drop impl or - interestingly - if you just use the return keyword instead of "naturally" returning the value.

@Stebalien
Copy link
Contributor

Stebalien commented May 25, 2017

Probably a duplicate of #17462 (scratch that, not related), #22449, and #21114. May also be related to #22321. I thought there was a more "canonical" issue for this but I can't find it.

@Mark-Simulacrum
Copy link
Member

Yes, closing in favor of #21114.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants