dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

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

Closed ComputerDruid closed 1 year ago

ComputerDruid commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

ComputerDruid commented 2 years ago

https://github.com/dtolnay/async-trait/pull/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.

jplatte commented 1 year ago

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 commented 1 year ago

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