Skip to content

Non-existant ternary operator is not gracefully handled  #112578

Closed
@jens1o

Description

@jens1o

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

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