dtolnay/cargo-expand

Lifetime issue when using "impl FnMut" as argument type (Edit: wrong post, sorry)

JanBeh opened this issue · 1 comments

Edit: Sorry, this ticket wasn't supposed to go to cargo-expand but to async-trait. Please delete.

Case 1:

async fn foo(&self, mut callback: impl FnMut(&str) + Send)

expands to:

    fn foo<'life0, 'life1, 'async_trait>(
        &'life0 self,
        callback: impl FnMut(&'life1 str) + Send,
    ) -> ::core::pin::Pin<
        Box<dyn ::core::future::Future<Output = ()> + ::core::marker::Send + 'async_trait>,
    >
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: ::core::marker::Sync + 'async_trait,

which is not functioning as expected.

Case 2:

async fn foo<C: FnMut(&str) + Send>(&self, mut callback: C)

works however.

The Rust reference states for anonymous type parameters:

That is, impl Trait in argument position is syntactic sugar for a generic type parameter like <T: Trait>, except that the type is anonymous and doesn't appear in the GenericParams list.

It can thus be very surprising that Case 2 is alright, but Case 1 fails.

See also this discussion in the Rust users Forum for an idea what should/could be done about it.

Alternatively, this surprising behavior could be mentioned in the documentation.

Sorry, this should have been going to async-trait. Please delete.