rust-lang / rust-lang/rust

E0308 arising from unary/binary `-` grammar ambiguity

Open
#117,232 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn f(x: bool)->i32 {
    if x { 5 } else { 10 } - 3
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:12
  |
2 |     if x { 5 } else { 10 } - 3
  |     -------^--------------
  |     |      |
  |     |      expected `()`, found integer
  |     expected this to be `()`
  |
help: you might have meant to return this value
  |
2 |     if x { return 5; } else { 10 } - 3
  |            ++++++  +

error[E0308]: mismatched types
 --> src/main.rs:2:23
  |
2 |     if x { 5 } else { 10 } - 3
  |     ------------------^^--
  |     |                 |
  |     |                 expected `()`, found integer
  |     expected this to be `()`
  |
help: you might have meant to return this value
  |
2 |     if x { 5 } else { return 10; } - 3
  |                       ++++++   +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
Desired output
help: parentheses are required to parse this as an expression
  |
2 |     (if x { 5 } else { 10 }) - 3
  |     +                      +

help: a semicolon is required to discard this value
  |
2 |     if x { 5 } else { 10 }; - 3
  |                           +
Rationale and extra context

This arises from a grammar ambiguity regarding block expressions and unary vs. binary -.

A brief search of open issues found #116128 which arises from a similar ambiguity between boolean or and an empty closure parameter list.

This issue was discovered during an URLO conversation

Other cases

If another operator, like '+', is used, a help message about adding parens is already emitted:

fn f(x: bool)->i32 {
    if x { 5 } else { 10 } + 3
}

Partial output:

error: leading `+` is not supported
 --> src/main.rs:2:28
  |
2 |     if x { 5 } else { 10 } + 3
  |                            ^ unexpected `+`
  |
help: parentheses are required to parse this as an expression
  |
2 |     (if x { 5 } else { 10 }) + 3
  |     +                      +

error[E0308]: mismatched types
 --> src/main.rs:2:12
  |
2 |     if x { 5 } else { 10 } + 3
  |     -------^--------------
  |     |      |
  |     |      expected `()`, found integer
  |     expected this to be `()`
  |
help: you might have meant to return this value
  |
2 |     if x { return 5; } else { 10 } + 3
  |            ++++++  +

error[E0308]: mismatched types
 --> src/main.rs:2:23
  |
2 |     if x { 5 } else { 10 } + 3
  |     ------------------^^--
  |     |                 |
  |     |                 expected `()`, found integer
  |     expected this to be `()`
  |
help: you might have meant to return this value
  |
2 |     if x { 5 } else { return 10; } + 3
  |                       ++++++   +
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in src/main.rs and compare its diagnostics with the '+' variant, which already emits the parentheses suggestion. Trace the rustc diagnostic path for E0308 and make the '-' case produce the requested parentheses and semicolon-help output, then verify both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.