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

Better error message for missing return in async fns #92308

Closed
nico-abram opened this issue Dec 27, 2021 · 3 comments · Fixed by #92381
Closed

Better error message for missing return in async fns #92308

nico-abram opened this issue Dec 27, 2021 · 3 comments · Fixed by #92381
Assignees
Labels
A-async-await Area: Async & Await A-diagnostics Area: Messages for errors, warnings, and lints AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nico-abram
Copy link
Contributor

nico-abram commented Dec 27, 2021

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
@nico-abram nico-abram 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 Dec 27, 2021
@BGR360
Copy link
Contributor

BGR360 commented Dec 28, 2021

@rustbot label +A-async-await +D-papercut

@rustbot rustbot added A-async-await Area: Async & Await D-papercut Diagnostics: An error or lint that needs small tweaks. labels Dec 28, 2021
@ThePuzzlemaker
Copy link
Contributor

I'm gonna try and take a shot at this.
@rustbot claim

ThePuzzlemaker added a commit to ThePuzzlemaker/rust that referenced this issue Dec 29, 2021
This amends off of an existing test introduced in rust-lang#81769, if you think I
should make a separate test I will.
@eholk
Copy link
Contributor

eholk commented Jan 10, 2022

Thanks for putting up a PR so quickly, @ThePuzzlemaker!

@rustbot label +AsyncAwait-Triaged +AsyncAwait-Polish

@rustbot rustbot added AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. labels Jan 10, 2022
@bors bors closed this as completed in 347c744 Jan 14, 2022
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 AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants