{ expr1 } || expr2; logical or / closure ambiguity #150552
Copy link
Copy link
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
fn main() {
{ false } || { true };
}Current output
error[E0308]: mismatched types
--> src/main.rs:2:7
|
2 | { false } || { true };
| ^^^^^ expected `()`, found `bool`
For more information about this error, try `rustc --explain E0308`.Desired output
help: parentheses are required to parse this as an expression
|
2 | ({ false } || { true });
help: use `if` instead
|
2 | if !{ false } { { true }; }Rationale and extra context
Probably something left out by #60188
Other cases
Rust Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
BartSimpson001 commentedon Dec 31, 2025
Hi, I’d like to work on improving the diagnostic for this case.
Is anyone already working on it, or can I take this up?
Bryntet commentedon Jan 1, 2026
@BartSimpson001 you can write
@ rustbot claimin a comment here (without the space between the @ ) to say that you want to work on this issueBartSimpson001 commentedon Jan 1, 2026
@rustbot claim
BartSimpson001 commentedon Jan 1, 2026
I investigated improving this diagnostic from check_expr_binop, but in the current diagnostics API the E0308 emitted by check_expr_coercible_to_type does not expose a Diag, and DiagCtxtHandle cannot emit standalone notes or help.
This seems to require either a diagnostics hook in coercion or refactoring error emission. Happy to help if guidance is available.
A4-Tacks commentedon Jan 3, 2026
Perhaps it would be more appropriate to point out the cause of the error
For example:
In my opinion, changing to
if !{ false } { { true }; }is a decrease in readabilityALSO, changing to
({ false }) || { true };can also make it workSunshine40 commentedon Jan 3, 2026
While the
ifversion is a little bit more verbose, using a logical OR expression purely for control flow (and discarding the result) feels like an anti-pattern to me. (However, once operator||becomes overloadable, switching from logical OR to if NOT might change the behavior)By the way, someone who intended a logical OR expression might still not get it by reading
It is an expression block statementif the message doesn't emphasize the fact that{ false }and the part after it belong to different statements. Honestly I don't know what's the most effective way to give the hint 'coz English isn't my first language.