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

rustc nightly (7480389 2022-08-25) gives erroneous fix advice on mismatching if-branch types #101065

Closed
fritschy opened this issue Aug 26, 2022 · 1 comment · Fixed by #102210
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fritschy
Copy link

fritschy commented Aug 26, 2022

rustc version: rustc 1.65.0-nightly (7480389 2022-08-25)

I have a very small reproducer for a weird fix advice from rustc when the two branches of an if had a type mismatch:

fn main() {
    let x = if true {
        Ok(Ok(()))
    } else {
        Ok(())
    };
}

This will produce the following output:

error[E0308]: `if` and `else` have incompatible types
 --> weird-fix-advice-else-ok.rs:5:9
  |
2 |       let x = if true {
  |  _____________-
3 | |         Ok(Ok(()))
  | |         ---------- expected because of this
4 | |     } else {
5 | |         Ok(())
  | |         ^^^^^^ expected enum `Result`, found `()`
6 | |     };
  | |_____- `if` and `else` have incompatible types
  |
  = note: expected enum `Result<Result<(), _>, _>`
             found enum `Result<(), _>`
help: try wrapping the expression in a variant of `Result`
  |
4 ~     } else Ok({
5 |         Ok(())
6 ~     });
  |
4 ~     } else Err({
5 |         Ok(())
6 ~     });
  |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Applying this fix obviously doesn't help:

error: expected `{`, found `Ok`
 --> weird-fix-advice-else-ok.rs:4:12
  |
4 |     } else Ok({
  |            ^^ expected `{`
  |
help: try placing this code inside a block
  |
4 ~     } else { Ok({
5 |         Ok(())
6 ~     }) };
  |

error: aborting due to previous error
@Rageking8
Copy link
Contributor

@rustbot label +A-diagnostics +T-compiler +D-invalid-suggestion

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 27, 2022
@notriddle notriddle self-assigned this Sep 24, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Sep 24, 2022
…r=cjgillot

diagnostics: avoid syntactically invalid suggestion in if conditionals

Fixes rust-lang#101065
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 24, 2022
…r=cjgillot

diagnostics: avoid syntactically invalid suggestion in if conditionals

Fixes rust-lang#101065
@bors bors closed this as completed in ac06d9c Sep 24, 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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

4 participants