Skip to content

return-type mismatch in async function gives "return type inferred" pointing to use of ? #87461

Description

@joshtriplett

Given the following code (playground link):

async fn func() -> Result<u16, u64> {
    let _ = async {
        Err(42u64)
    }.await?;

    Ok(())
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:6:8
  |
6 |     Ok(())
  |        ^^ expected `u16`, found `()`
  |
note: return type inferred to be `u16` here
 --> src/lib.rs:2:13
  |
2 |       let _ = async {
  |  _____________^
3 | |         Err(42u64)
4 | |     }.await?;
  | |____________^

error: aborting due to previous error

An even more minimal example:

async fn func() -> Result<u16, u64> {
    Err(42u64)?;

    Ok(())
}

This similarly produces a note pointing to the Err(42u64)?; line.

The note seems entirely unhelpful here; the function's declared type doesn't match the Ok(()), and the use of ? has nothing to do with it. An equivalent non-async function omits the note, and just reports the type mismatch.

The same output occurs on both stable and today's nightly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

    Issue actions