rust-lang / rust-lang/rust

"cannot assign in match guard" error message only triggers if the pattern doesn't bind anything

Open
#137,690 3 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 meow(mut x: Option<u32>) {
    match x {
        Some(y) if { x = None; false } => loop {},
        _ => loop {},
    }
}
Current output
error[E0506]: cannot assign to `x` because it is borrowed
 --> meow.rs:3:22
  |
3 |         Some(y) if { x = None; false } => loop {},
  |              -       ^^^^^^^^        - borrow later used here
  |              |       |
  |              |       `x` is assigned to here but it was already borrowed
  |              `x` is borrowed here
Desired output
error[E0510]: cannot assign `x` in match guard
 --> meow.rs:3:22
  |
2 |     match x {
  |           - value is immutable in match guard
3 |         Some(y) if { x = None; false } => loop {},
  |                      ^^^^^^^^ cannot assign
Rationale and extra context

No response

Other cases
// with Some(_) instead of Some(y) as the pattern, E0510 is emitted already
fn meow(mut x: Option<u32>) {
    match x {
        Some(_) if { x = None; false } => loop {},
        _ => loop {},
    }
}
Rust Version
rustc 1.87.0-nightly (85abb2763 2025-02-25)
binary: rustc
commit-hash: 85abb276361c424d64743c0965242dd0e7b866d1
commit-date: 2025-02-25
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0
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

Start by reproducing the reported example with rustc 1.87.0-nightly and compare it with the Some() variant, which already emits E0510. Trace the compiler path that selects the borrow-checking diagnostic for match guards; done means the Some(y) case emits E0510 with the expected location and message without regressing the existing Some() case.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.