Skip to content

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

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

Open
ChaiTRex opened this issue Dec 16, 2021 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-frontend Area: Compiler frontend (errors, parsing and HIR) A-parser Area: The lexing & parsing of Rust source code to an AST D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChaiTRex
Copy link
Contributor

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))

@ChaiTRex ChaiTRex 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 Dec 16, 2021
@BGR360
Copy link
Contributor

BGR360 commented Dec 21, 2021

@rustbot label +D-terse +A-frontend +A-parser

@rustbot rustbot added D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-frontend Area: Compiler frontend (errors, parsing and HIR) A-parser Area: The lexing & parsing of Rust source code to an AST labels Dec 21, 2021
@Havvy
Copy link
Contributor

Havvy commented Mar 4, 2022

Is this fixed by #94344 ?

@ChaiTRex
Copy link
Contributor Author

@Havvy No, the error message remains the same.

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 A-frontend Area: Compiler frontend (errors, parsing and HIR) A-parser Area: The lexing & parsing of Rust source code to an AST D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants