You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
type inference fails when using async_fn_in_trait #108362
#![feature(async_fn_in_trait)]fnmain(){}traitMyTrait<T>{asyncfnfoo(&mutself) -> T;}impl<T>MyTrait<bool>forT{asyncfnfoo(&mutself) -> bool{true}}impl<T>MyTrait<i32>forT{asyncfnfoo(&mutself) -> i32{let x:bool = self.foo().await;//error[E0698]: type inside `async fn` body must be known in this context42}}
works fine when removing async or using the async-trait crate
Actually, this is also just a limitation of the double indirection of async:
use futures::future::LocalBoxFuture;use core::future::Future;fnmain(){}traitMyTrait<T>{typeFoo<'a>:Future<Output = T> + 'awhereSelf:'a;fnfoo(&mutself) -> Self::Foo<'_>;}impl<T>MyTrait<bool>forT{typeFoo<'a> = LocalBoxFuture<'a,bool>whereT:'a;fnfoo(&mutself) -> Self::Foo<'_>{Box::pin(async{true})}}impl<T>MyTrait<i32>forT{typeFoo<'a> = LocalBoxFuture<'a,i32>whereT:'a;fnfoo(&mutself) -> Self::Foo<'_>{Box::pin(async{let x:bool = self.foo().await;//error[E0698]: type inside `async fn` body must be known in this context42})}}
Actually, this isn't even a bug with AFIT. This is just an async bug:
Details
fnmain(){}fnfoo<T:Default>() -> T{T::default()}asyncfnbar() -> i32{let x:bool = foo().await;//error[E0698]: type inside `async fn` body must be known in this context42}
We discussed this in the WG-async triage meeting. The feeling was that this may be difficult to fix and that it may be due to indirection introduced in the .await desugaring. To investigate further, someone might manually desugar .await to see where the ambiguity is originating.
error[E0284]: type annotations needed
--> src/main.rs:18:22
|
18 | let x: bool = self.foo().await; //error[E0698]: type inside `async fn` body must be known in this context
| ^^^
|
= note: cannot satisfy `impl Future<Output = _> == _`
help: try using a fully qualified path to specify the expected types
|
18 | let x: bool = <T as MyTrait<T>>::foo(self).await; //error[E0698]: type inside `async fn` body must be known in this context
| +++++++++++++++++++++++ ~
error[E0283]: type annotations needed
--> src/main.rs:18:22
|
18 | let x: bool = self.foo().await; //error[E0698]: type inside `async fn` body must be known in this context
| ^^^
|
note: multiple `impl`s satisfying `T: MyTrait<_>` found
--> src/main.rs:10:1
|
10 | impl<T> MyTrait<bool> for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
16 | impl<T> MyTrait<i32> for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try using a fully qualified path to specify the expected types
|
18 | let x: bool = <T as MyTrait<T>>::foo(self).await; //error[E0698]: type inside `async fn` body must be known in this context
| +++++++++++++++++++++++ ~
Some errors have detailed explanations: E0283, E0284.
For more information about an error, try `rustc --explain E0283`.
Activity
compiler-errors commentedon Jun 12, 2023
This issue isn't really related to async fn in trait itself, but more a limitation of opaque type inference (since opaques power async fn in trait):
compiler-errors commentedon Sep 11, 2023
Actually, this is also just a limitation of the double indirection of
async:compiler-errors commentedon Sep 13, 2023
Actually, this isn't even a bug with AFIT. This is just an async bug:Details
edit: see below
Auto merge of rust-lang#115822 - compiler-errors:stabilize-rpitit, r=…
Auto merge of #115822 - compiler-errors:stabilize-rpitit, r=jackh726
traviscross commentedon Dec 18, 2023
We discussed this in the WG-async triage meeting. The feeling was that this may be difficult to fix and that it may be due to indirection introduced in the
.awaitdesugaring. To investigate further, someone might manually desugar.awaitto see where the ambiguity is originating.(Thanks in particular to @compiler-errors.)
compiler-errors commentedon Dec 18, 2023
I'm an idiot. The code I provided was just not well-formed. Let me look into this again I guess.
Dylan-DPC commentedon Jan 9, 2025
current output: