Skip to content

If-else value followed by operator parsing error provided only for some operators #91999

Open
@ChaiTRex

Description

@ChaiTRex

When an if-else block produces a value and this is followed by an operator and this all happens inside a block, this is apparently a parsing error.

When the operator is +, the parsing error is pointed out. When the operator is &, the parsing error is not pointed out.

Please provide the parsing error for all operators.

Given the following code: https://play.rust-lang.org/?edition=2021&gist=55a7b19b44fc6010a33338774bef3b11

fn main() {
    { if true { 0 } else { 0 } & 0 };
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:17
  |
2 |     { if true { 0 } else { 0 } & 0 };
  |       ----------^-------------
  |       |         |
  |       |         expected `()`, found integer
  |       expected this to be `()`

error[E0308]: mismatched types
 --> src/main.rs:2:28
  |
2 |     { if true { 0 } else { 0 } & 0 };
  |       ---------------------^--
  |       |                    |
  |       |                    expected `()`, found integer
  |       expected this to be `()`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look like the output when + is used instead of &:

   Compiling playground v0.0.1 (/playground)
error: leading `+` is not supported
 --> src/main.rs:2:32
  |
2 |     { if true { 0 } else { 0 } + 0 };
  |                                ^ unexpected `+`
  |
help: parentheses are required to parse this as an expression
  |
2 |     { (if true { 0 } else { 0 }) + 0 };
  |       +                        +

error[E0308]: mismatched types
 --> src/main.rs:2:17
  |
2 |     { if true { 0 } else { 0 } + 0 };
  |       ----------^-------------
  |       |         |
  |       |         expected `()`, found integer
  |       expected this to be `()`

error[E0308]: mismatched types
 --> src/main.rs:2:28
  |
2 |     { if true { 0 } else { 0 } + 0 };
  |       ---------------------^--
  |       |                    |
  |       |                    expected `()`, found integer
  |       expected this to be `()`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to 3 previous errors

Note the added help line: parentheses are required to parse this as an expression

This explanation should be included for all operators.

Nightly has the same problem.

Stable: rustc 1.57.0 (f1edd04 2021-11-29)
Nightly: rustc 1.59.0-nightly (c5ecc15 2021-12-15) (from rustc 1.59.0-nightly (404c847 2021-12-14))

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-frontendArea: Compiler frontend (errors, parsing and HIR)A-parserArea: The lexing & parsing of Rust source code to an ASTD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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