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

changes to closure capture in Rust 2021 affect drop order of ignored arguments #199

Closed
ComputerDruid opened this issue May 31, 2022 · 5 comments · Fixed by #203
Closed
Labels
bug Something isn't working

Comments

@ComputerDruid
Copy link

ComputerDruid commented May 31, 2022

Playground demo

This code:

pub struct ArgWithDropGlue;
impl Drop for ArgWithDropGlue {
    fn drop(&mut self) {
        println!("dropped");
    }
}

#[async_trait]
pub(crate) trait Create: Sized {
    async fn create(arg: ArgWithDropGlue) -> Result<Self, ()>;
}

struct Foo;

#[async_trait]
impl Create for Foo {
    async fn create(_: ArgWithDropGlue) -> Result<Self, ()> {
        Ok(Foo)
    }
}

triggers the 2021 edition migration tooling to insert a let _ = &__arg0; line, in order to try to avoid the edition change changing drop ordering. If you don't do that, and change the edition, it really does change drop ordering:

fn main() {
    println!("create future");
    let f = Foo::create(ArgWithDropGlue);
    println!("drop future");
    drop(f);
}

in edition 2018:

create future
drop future
dropped

in edition 2021:

create future
dropped
drop future

but regular async functions don't work that way, even in edition 2021 playground:

async fn create(_: ArgWithDropGlue) -> Result<Foo, ()> {
    Ok(Foo)
}

fn main() {
    println!("create future");
    let f = create(ArgWithDropGlue);
    println!("drop future");
    drop(f);
}

prints:

create future
drop future
dropped

I think we do want the argument to be captured here, to match the behavior of async functions. That would also incidentally stop rustfix from making the odd suggestion. Surprisingly, the suggestion rustfix makes does actually work, which also seems like a bug to me -- I would have expected the __arg0 arguments to have some kind of hygiene which prevented them from being named.

@ComputerDruid
Copy link
Author

I found this while running the 2021 edition migration over the Fuchsia codebase, you can see the full example pre-minimization in https://fuchsia-review.googlesource.com/c/fuchsia/+/684586/1/src/settings/service/src/tests/setting_handler_tests.rs if that's interesting.

@dtolnay
Copy link
Owner

dtolnay commented Jun 2, 2022

I've published a fix in async-trait 0.1.55.

@ComputerDruid
Copy link
Author

ComputerDruid commented Jun 23, 2022

#203 seems to fix it for _ patterns as arguments, but not patterns with underscores nested inside them:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2d998a28f5b49520167ef0868f0beab5

It drops the argument early if edition is set to 2021, and produces the migration lint.

Although that example is quite contrived and doesn't affect me personally.

@dtolnay dtolnay reopened this Jun 24, 2022
@jplatte
Copy link

jplatte commented Nov 5, 2022

The initial fix in 0.1.55 also broke a pattern that used to work:

#[async_trait]
trait Generic<T> {
    fn takes_ref(&self, thing: &T);
}

// no need for T: Send + Sync bound on this impl because &T is not used
#[async_trait]
impl<T> Generic<T> for () {
    fn takes_ref(&self, _: &T) {}
}

@dtolnay
Copy link
Owner

dtolnay commented Jan 22, 2023

I believe the remaining edition-sensitive cases have been resolved in #232 and #234.

@dtolnay dtolnay closed this as completed Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants