Skip to content
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

improve error for missing await #84166

Closed
nikomatsakis opened this issue Apr 13, 2021 · 6 comments
Closed

improve error for missing await #84166

nikomatsakis opened this issue Apr 13, 2021 · 6 comments
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Confusing diagnostic error that should be reworked T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

Given the following code:

use futures::executor::block_on;

async fn return_number() -> usize {
    return 1;
}

async fn return_call_async() -> usize {
    // return return_number().await;
    return return_number();
}

fn main() {
    let s = block_on(return_call_async());
    print!("{}", &s);
}

The current (pretty decent) output is:

error[E0308]: mismatched types
 --> src/main.rs:8:12
  |
3 | async fn return_number() -> usize {
  |                             ----- the `Output` of this `async fn`'s found opaque type
...
8 |     return return_number();
  |            ^^^^^^^^^^^^^^^ expected `usize`, found opaque type
  |
  = note:     expected type `usize`
          found opaque type `impl futures::Future`
help: consider `await`ing on the `Future`
  |
8 |     return return_number().await;
  |                           ^^^^^^

error: aborting due to previous error
@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 13, 2021
@nikomatsakis
Copy link
Contributor Author

This comes from @jlkiri's comment here

@nikomatsakis
Copy link
Contributor Author

I think the output should be more aggressive. For example:

error[E0308]: missing await on future
 --> src/main.rs:8:12
  |
3 | async fn return_number() -> usize {
  | ----- calling an async function returns a future
...
8 |     return return_number();
  |            ^^^^^^^^^^^^^^^ expected `usize`, found future
  |
help: consider `await`ing on the `Future`
  |
8 |     return return_number().await;
  |                           ^^^^^^

error: aborting due to previous error

@estebank
Copy link
Contributor

Another alternative would be to explicitly state what the fns desugared type is:

  |
3 | async fn return_number() -> usize {
  | ----- by annotating this function with `async` makes it `fn() -> impl Future<Output = usize>` instead of `fn() -> usize`
...

@nikomatsakis nikomatsakis changed the title improve error for missing improve error for missing await Apr 13, 2021
@nikomatsakis nikomatsakis added the A-async-await Area: Async & Await label Apr 13, 2021
@SNCPlay42
Copy link
Contributor

Related, if not a duplicate, to #80658

@estebank estebank added the D-confusing Confusing diagnostic error that should be reworked label Apr 13, 2021
@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Apr 13, 2021

The note has changed on nightly/beta, from the PR linked to that isssue:

3 | async fn return_number() -> usize {
  |                             ----- checked the `Output` of this `async fn`, found opaque type

@nikomatsakis
Copy link
Contributor Author

I agree this is something of a duplicate. I can move the conversation over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Confusing diagnostic error that should be reworked T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants