Skip to content

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

Closed
@fritschy

Description

@fritschy

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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