Skip to content

Better error message for missing return in async fns #92308

Closed
@nico-abram

Description

@nico-abram

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d8b89e2ba445cca680d18063b9d62a20

async fn f() -> Result<u8, u16> {
    let x = false;
    if x {
        Ok(0u8)
    }
    Err(2u16)
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:4:9
  |
3 | /     if x {
4 | |         Ok(0u8)
  | |         ^^^^^^^ expected `()`, found enum `Result`
5 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<u8, _>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Ideally the output should look like it does for the non-async version:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:4:9
  |
3 | /     if x {
4 | |         Ok(0u8)
  | |         ^^^^^^^ expected `()`, found enum `Result`
5 | |     }
  | |_____- expected this to be `()`
  |
  = note: expected unit type `()`
                  found enum `Result<u8, _>`
help: you might have meant to return this value
  |
4 |         return Ok(0u8);
  |         ++++++        +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsAsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.D-papercutDiagnostics: An error or lint that needs small tweaks.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

Issue actions