Skip to content

improve error for missing await #84166

Closed
Closed
@nikomatsakis

Description

@nikomatsakis
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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 13, 2021
nikomatsakis

nikomatsakis commented on Apr 13, 2021

@nikomatsakis
ContributorAuthor

This comes from @jlkiri's comment here

nikomatsakis

nikomatsakis commented on Apr 13, 2021

@nikomatsakis
ContributorAuthor

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

estebank commented on Apr 13, 2021

@estebank
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`
...
changed the title [-]improve error for missing [/-] [+]improve error for missing await[/+] on Apr 13, 2021
SNCPlay42

SNCPlay42 commented on Apr 13, 2021

@SNCPlay42
Contributor

Related, if not a duplicate, to #80658

added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
on Apr 13, 2021
SNCPlay42

SNCPlay42 commented on Apr 13, 2021

@SNCPlay42
Contributor

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

nikomatsakis commented on Apr 13, 2021

@nikomatsakis
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nikomatsakis@estebank@SNCPlay42

        Issue actions

          improve error for missing await · Issue #84166 · rust-lang/rust