E0308 arising from unary/binary `-` grammar ambiguity
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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