rust-lang / rust-lang/rust

`matches!` macro gives confusing compiler error when arguments are switched with catch-all pattern (`_`)

Open
#139,071 0 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 main() {
    let foo: Option<()> = Some(());
    if matches!(Some(_), foo) {
        println!("👍");
    } else {
        panic!();
    }
}
Current output
Compiling playground v0.0.1 (/playground)
error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:3:22
  |
3 |     if matches!(Some(_), foo) {
  |                      ^ `_` not allowed here

error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
The error should say something about how the arguments are in the wrong order. The phrase "error: in expressions, `_` can only be used on the left-hand side of an assignment" is confusing because does not appear to be an assignment, and the `_` is in the left-hand position.
Rationale and extra context

Playground link here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024

Other cases

Rust Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
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

Reproduce the issue using the supplied matches! example in the Rust Playground and compare the current diagnostic with the requested wording. Start by investigating how this macro invocation reaches the compiler error, then add coverage for the switched arguments and catch-all pattern. Done means the diagnostic explains that the arguments are in the wrong order rather than referring to an assignment.

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.