Skip to content

+= in a let chain gives bad errors #147664

@theemathas

Description

@theemathas

Code

fn main() {
    let mut y: i32 = 0;
    if let x = 1 && y += 2 {};
}

Current output

error: expected expression, found `let` statement
 --> src/main.rs:3:8
  |
3 |     if let x = 1 && y += 2 {};
  |        ^^^^^^^^^
  |
  = note: only supported directly in conditions of `if` and `while` expressions

error[E0308]: mismatched types
 --> src/main.rs:3:21
  |
3 |     if let x = 1 && y += 2 {};
  |                     ^ expected `bool`, found `i32`

error[E0368]: binary assignment operation `+=` cannot be applied to type `bool`
 --> src/main.rs:3:8
  |
3 |     if let x = 1 && y += 2 {};
  |        --------------^^^^^
  |        |
  |        cannot use `+=` on type `bool`

error[E0067]: invalid left-hand side of assignment
 --> src/main.rs:3:23
  |
3 |     if let x = 1 && y += 2 {};
  |        -------------- ^^
  |        |
  |        cannot assign to this expression

Some errors have detailed explanations: E0067, E0308, E0368.
For more information about an error, try `rustc --explain E0067`.
error: could not compile `playground` (bin "playground") due to 4 previous errors

Desired output

Rationale and extra context

I find the error very confusing, since it doesn't point to the actual problem, which is that += isn't allowed in let chains.

Inspecting the AST with -Zunpretty=ast-tree, it seems that rustc is trying to parse the thing as:

if (((let x = 1) && y) += 2) {};

which doesn't make a ton of sense to me.

Other cases

Rust Version

Reproducible on the playground with version: 1.92.0-nightly (2025-10-13 4b94758d2ba7d0ef71cc)

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