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

"if may be missing an else clause" sometimes reverses expectations #102397

Closed
nox opened this issue Sep 28, 2022 · 1 comment · Fixed by #102720
Closed

"if may be missing an else clause" sometimes reverses expectations #102397

nox opened this issue Sep 28, 2022 · 1 comment · Fixed by #102720
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@nox
Copy link
Contributor

nox commented Sep 28, 2022

Given the following code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5a939f14b1d92d9d4b3a3edbbb46f5c0

fn missing_else(x: i32) -> i32 {
    if true {
        return 1
    }
}

fn missing_else_nested(x: i32) -> i32 {
    if x < 0 {
        0
    } else {
        if true {
            return 1
        }
    }
}

The current output is:

error[E0317]: `if` may be missing an `else` clause
 --> src/lib.rs:2:5
  |
1 |   fn missing_else(x: i32) -> i32 {
  |                              --- expected `i32` because of this return type
2 | /     if true {
3 | |         return 1
4 | |     }
  | |_____^ expected `i32`, found `()`
  |
  = note: `if` expressions without `else` evaluate to `()`
  = help: consider adding an `else` block that evaluates to the expected type

error[E0317]: `if` may be missing an `else` clause
  --> src/lib.rs:11:9
   |
11 | /         if true {
12 | |             return 1
   | |             -------- found here
13 | |         }
   | |_________^ expected `()`, found `i32`
   |
   = note: `if` expressions without `else` evaluate to `()`
   = help: consider adding an `else` block that evaluates to the expected type

Note how the expected and found types are reversed for the missing_else_nested error and how the note about the function's return type is gone, compared to the missing_else error.

Ideally the output for the missing_else_nested error should be similar to the error for missing_else:

error[E0317]: `if` may be missing an `else` clause
  --> src/lib.rs:11:9
  |
7 |   fn missing_else_nested(x: i32) -> i32 {
  |                                     --- expected `i32` because of this return type
   |
11 | /         if true {
12 | |             return 1
   | |             -------- found here
13 | |         }
   | |_________^ expected `i32`, found `()`
   |
   = note: `if` expressions without `else` evaluate to `()`
   = help: consider adding an `else` block that evaluates to the expected type

This kinda duplicates #78656, but @estebank asked me to file a new issue on Twitter.

@nox nox 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 Sep 28, 2022
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Sep 28, 2022
@lyming2007
Copy link

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 7, 2022
…piler-errors

do not reverse the expected type and found type for ObligationCauseCo…

…de of IfExpressionWithNoElse

this will fix rust-lang#102397
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 7, 2022
…piler-errors

do not reverse the expected type and found type for ObligationCauseCo…

…de of IfExpressionWithNoElse

this will fix rust-lang#102397
@bors bors closed this as completed in 0501d61 Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.

3 participants