Skip to content

Ugly error reporting on binary operation between two condition flows #70115

@GuillaumeGomez

Description

@GuillaumeGomez

The following code:

fn to_log_flags(fatal: bool, recursive: bool) -> u32 {
    if fatal { 1 } else { 0 } |
    if recursive { 2 } else { 0 }
}

gives the following error:

   Compiling playground v0.0.1 (/playground)
error: expected identifier, found keyword `if`
 --> src/main.rs:3:5
  |
3 |     if recursive { 2 } else { 0 }
  |     ^^ expected identifier, found keyword

error: expected identifier, found `2`
 --> src/main.rs:3:20
  |
3 |     if recursive { 2 } else { 0 }
  |                    ^ expected identifier

error: expected one of `,`, `:`, or `@`, found `recursive`
 --> src/main.rs:3:8
  |
3 |     if recursive { 2 } else { 0 }
  |       -^^^^^^^^^ expected one of `,`, `:`, or `@`
  |       |
  |       help: missing `,`

error: expected identifier, found keyword `else`
 --> src/main.rs:3:24
  |
3 |     if recursive { 2 } else { 0 }
  |                        ^^^^ expected identifier, found keyword
  |
help: you can escape reserved keywords to use them as identifiers
  |
3 |     if recursive { 2 } r#else { 0 }
  |                        ^^^^^^

error: expected identifier, found `0`
 --> src/main.rs:3:31
  |
3 |     if recursive { 2 } else { 0 }
  |                               ^ expected identifier

error: expected one of `,` or `:`, found keyword `else`
 --> src/main.rs:3:24
  |
3 |     if recursive { 2 } else { 0 }
  |                       -^^^^ expected one of `,` or `:`
  |                       |
  |                       help: missing `,`

error: expected one of `:` or `|`, found `}`
 --> src/main.rs:4:1
  |
3 |     if recursive { 2 } else { 0 }
  |                                  - expected one of `:` or `|`
4 | }
  | ^ unexpected token

error[E0308]: mismatched types
 --> src/main.rs:2:16
  |
2 |     if fatal { 1 } else { 0 } |
  |     -----------^-------------- help: consider using a semicolon here
  |     |          |
  |     |          expected `()`, found integer
  |     expected this to be `()`

error[E0308]: mismatched types
 --> src/main.rs:2:27
  |
2 |     if fatal { 1 } else { 0 } |
  |     ----------------------^--- help: consider using a semicolon here
  |     |                     |
  |     |                     expected `()`, found integer
  |     expected this to be `()`

error: aborting due to 9 previous errors

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

This is clearly sub-optimal. The code can be fixed by wrapping the first if condition in ().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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