Skip to content

Non-existant ternary operator is not gracefully handled #112578

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

Closed
jens1o opened this issue Jun 13, 2023 · 0 comments · Fixed by #114028
Closed

Non-existant ternary operator is not gracefully handled #112578

jens1o opened this issue Jun 13, 2023 · 0 comments · Fixed by #114028
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jens1o
Copy link
Contributor

jens1o commented Jun 13, 2023

Code

fn main() {
    let x = 5 > 2 ? 3 : 1;
}

Current output

Compiling rust-playground v0.1.0 (/Users/Jens.Hausdorf/dev/rust-playground)
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `3`
 --> src/main.rs:2:21
  |
2 |     let x = 5 > 2 ? 3 : 1;
  |                     ^ expected one of `.`, `;`, `?`, `else`, or an operator

error: could not compile `rust-playground` (bin "rust-playground") due to previous error

Desired output

Compiling rust-playground v0.1.0 (/Users/Jens.Hausdorf/dev/rust-playground)
error: should have used inline if-expression instead
 --> src/main.rs:2:21
  |
2 |     let x = 5 > 2 ? 3 : 1;
  |                     ^ expected one of `.`, `;`, `?`, `else`, or an operator
help: consider changing this to use if-expression
  |
2 |     let x = if 5 > 2 { 3 } else { 1 };
  |  
error: could not compile `rust-playground` (bin "rust-playground") due to previous error

Rationale and extra context

New rustaceans might expect that Rust has the ternary operator.

Other cases

No response

Anything else?

No response

@jens1o jens1o 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 Jun 13, 2023
@jyn514 jyn514 added the A-parser Area: The lexing & parsing of Rust source code to an AST label Jun 14, 2023
@bors bors closed this as completed in a04e649 Jul 29, 2023
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-parser Area: The lexing & parsing of Rust source code to an AST 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.

2 participants