Description
I have reviewed previous issues about type inference in async closures, which are about the type inference not working for the some
variable in async |some| {}
. However, the issue I encountered is different. Please see my code below.
I tried this code:
struct Foo;
impl Foo {
fn method(&self) {
}
}
async fn some_async_fn<F>(_: F)
where
F: AsyncFn(Foo),
{
}
async fn test() {
some_async_fn(|boo| async move { boo.method() }).await;
}
I expected the type inference to work correctly
Because the following code can work:
async fn test() {
// is ok
some_async_fn(async |boo| boo.method()).await;
// or this is ok
some_async_fn(|boo: Foo| async move { boo.method() }).await;
}
Instead, this happened:
error[E0282]: type annotations needed
--> test_1/src/lib.rs:16:20
|
16 | some_async_fn(|boo| async move { boo.method() }).await;
| ^^^ --- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
16 | some_async_fn(|boo: /* Type */| async move { boo.method() }).await;
| ++++++++++++
I need to edit this issue because I think I should highlight its importance. async || {}
is compatible with the previous Fn() -> Fut
, upgrading from || async {}
to async || {}
is a good choice, but what about the other way around? If a function is upgraded from Fn() -> Fut
to AsyncFn, if it cannot infer the types of the input parameters in || async {}
, much of the existing code that calls this function will no longer work as expected. This could become a barrier for developers upgrading to AsyncFn
.
Meta
rustc --version --verbose
:
rustc 1.86.0-nightly (942db6782 2025-02-06)
binary: rustc
commit-hash: 942db6782f4a28c55b0b75b38fd4394d0483390f
commit-date: 2025-02-06
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7