-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
suggest await
for future-related type errors
#61076
Comments
cc @rust-lang/wg-diagnostics @estebank |
@wycats was reporting a much worse error, but I'm quite sure what he did to encounter it: Probably has something to do with streams? |
This will be harder to do for chained expessions |
@nikomatsakis Looks like @wycats's error was the same, it's just that the output type of the future is more complicated ( |
Hi, I'd be really interested in working on this |
@doctorn awesome! Sending |
@rustbot claim |
…jasper Add suggestion for missing `.await` keyword This commit adds a suggestion diagnostic for missing `.await`. In order to do this, the trait `Future` is promoted to a lang item. Compiling code of the form: ```rust #![feature(async_await)] fn take_u32(x: u32) {} async fn make_u32() -> u32 { 22 } async fn foo() { let x = make_u32(); take_u32(x) } fn main() {} ``` Will now result in the suggestion: ``` error[E0308]: mismatched types --> src/main.rs:11:18 | 11 | take_u32(x) | ^ | | | expected u32, found opaque type | help: consider using `.await` here: `x.await` | = note: expected type `u32` found type `impl std::future::Future` ``` This commit does not cover chained expressions and therefore only covers the case originally pointed out in rust-lang#61076. Cases I can think of that still need to be covered: - [ ] Return places for functions - [ ] Field access - [ ] Method invocation I'm planning to submit PRs for each of these separately as and when I have figured them out.
…jasper Add suggestion for missing `.await` keyword This commit adds a suggestion diagnostic for missing `.await`. In order to do this, the trait `Future` is promoted to a lang item. Compiling code of the form: ```rust #![feature(async_await)] fn take_u32(x: u32) {} async fn make_u32() -> u32 { 22 } async fn foo() { let x = make_u32(); take_u32(x) } fn main() {} ``` Will now result in the suggestion: ``` error[E0308]: mismatched types --> src/main.rs:11:18 | 11 | take_u32(x) | ^ | | | expected u32, found opaque type | help: consider using `.await` here: `x.await` | = note: expected type `u32` found type `impl std::future::Future` ``` This commit does not cover chained expressions and therefore only covers the case originally pointed out in rust-lang#61076. Cases I can think of that still need to be covered: - [ ] Return places for functions - [ ] Field access - [ ] Method invocation I'm planning to submit PRs for each of these separately as and when I have figured them out.
…jasper Add suggestion for missing `.await` keyword This commit adds a suggestion diagnostic for missing `.await`. In order to do this, the trait `Future` is promoted to a lang item. Compiling code of the form: ```rust #![feature(async_await)] fn take_u32(x: u32) {} async fn make_u32() -> u32 { 22 } async fn foo() { let x = make_u32(); take_u32(x) } fn main() {} ``` Will now result in the suggestion: ``` error[E0308]: mismatched types --> src/main.rs:11:18 | 11 | take_u32(x) | ^ | | | expected u32, found opaque type | help: consider using `.await` here: `x.await` | = note: expected type `u32` found type `impl std::future::Future` ``` This commit does not cover chained expressions and therefore only covers the case originally pointed out in rust-lang#61076. Cases I can think of that still need to be covered: - [ ] Return places for functions - [ ] Field access - [ ] Method invocation I'm planning to submit PRs for each of these separately as and when I have figured them out.
Can we close this now @doctorn ? - seems like this is now done. |
Unfortunately not... I haven't had a lot of time for the past few weeks, but basically all cases similar to this one that @estebank pointed out still aren't covered.
I've made some progress towards this, but there are some complicated trait predicates involved in solving the |
I fear that that case will remain open for a while, as it'll be hard to get right. |
I was thinking that this issue was fairly similar to #63100. |
It is essentially the same issue. It boils down to altering the trait inference code to give diagnostics when certain necessary trait predicates don’t hold. For example if you consider the question mark case: async fn foo() -> Result<(), ()> {
Ok(())
}
async fn bar() -> Result<(), ()> {
foo()?;
Ok(())
} The error here arises because What we’d want to do is say that any time type Long story short, we need to verify if a trait bound holds for a projected type; however, I don’t think you can do this easily. |
From #62067, these are the cases still missing:
In bodies that aren't |
Here's another example where suggesting async fn a() -> Result<(), ()> {
Ok(())
}
fn main() {
match a() {
Ok(()) => (),
Err(()) => (),
}
} |
Suggest to await future before ? operator Closes rust-lang#71811 cc rust-lang#61076
Suggest to await future before ? operator Closes rust-lang#71811 cc rust-lang#61076
Suggest to await future before ? operator Closes rust-lang#71811 cc rust-lang#61076
@tmandry I'll try to fix them all, more commits pending. |
@estebank could you write a case of |
Indeed. I'm not sure if it is due to an improvement since august last year, or just a mistake on my part. Either way, we are in a good place today :) |
Ping from wg-async-foundations triage. @csmoe are you still working on this? |
@tmandry yes, in slow progress since time is tough these days. |
A common problem when using async-await is to forget to invoke
.await
. This often shows up as a type error, e.g. in a case like this (playground):currently we give a rather generic error:
it'd be nice if we could suggest adding
.await
somewhere.The text was updated successfully, but these errors were encountered: