Skip to content

{ expr1 } || expr2; logical or / closure ambiguity #150552

Open

Description

@Sunshine40

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.3

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 31, 2025
  2. BartSimpson001 commented on Dec 31, 2025

    @BartSimpson001

    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?

  3. Bryntet commented on Jan 1, 2026

    @Bryntet
    Contributor

    @BartSimpson001 you can write @ rustbot claim in a comment here (without the space between the @ ) to say that you want to work on this issue

  4. BartSimpson001 commented on Jan 1, 2026

    @BartSimpson001

    @rustbot claim

  5. BartSimpson001 commented on Jan 1, 2026

    @BartSimpson001

    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.

  6. A4-Tacks commented on Jan 3, 2026

    @A4-Tacks
    Member

    Perhaps it would be more appropriate to point out the cause of the error

    For example:

    error[E0308]: mismatched types
     --> src/main.rs:2:7
      |
    2 |     { false } || { true };
      |       ^^^^^ expected `()`, found `bool`
      |             - It is an expression block statement, that requires a `()` return value
    
    For more information about this error, try `rustc --explain E0308`.
    

    In my opinion, changing to if !{ false } { { true }; } is a decrease in readability

    ALSO, changing to ({ false }) || { true }; can also make it work

  7. Sunshine40 commented on Jan 3, 2026

    @Sunshine40
    ContributorAuthor

    In my opinion, changing to if !{ false } { { true }; } is a decrease in readability

    While the if version 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 statement if 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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