Skip to content

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

@cowlicks

Description

@cowlicks

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

Metadata

Metadata

Assignees

No one assigned

    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