Skip to content

Confusing error messages when a pattern match contains .foo() expressions #107523

Open
@Wilfred

Description

Code

#[derive(Debug, PartialEq)]
enum Pet {
    Dog(String),
}

fn main() {
    let d = Pet::Dog("fido".to_string());

    // Intended.
    assert_eq!(d, Pet::Dog("fido".to_string()));

    // What I wrote by accident.
    assert!(matches!(d, Pet::Dog("fido".to_string())));
}

Current output

error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
  --> src/main.rs:14:40
   |
14 |     assert!(matches!(d, Pet::Dog("fido".to_string())));
   |                                        ^
   |                                        |
   |                                        expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
   |                                        help: missing `,`

error[E0531]: cannot find tuple struct or tuple variant `to_string` in this scope
  --> src/main.rs:14:41
   |
14 |     assert!(matches!(d, Pet::Dog("fido".to_string())));
   |                                         ^^^^^^^^^ not found in this scope

error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
  --> src/main.rs:14:34
   |
3  |     Dog(String),
   |         ------ tuple variant has 1 field
...
14 |     assert!(matches!(d, Pet::Dog("fido".to_string())));
   |                                  ^^^^^^ ^^^^^^^^^^^ expected 1 field, found 2

Desired output

No response

Rationale and extra context

The third message is really confusing: I think rustc has assumed I intended "fido", to_string()? It's surprising to see an error talk about a field with two patterns.

Other cases

No response

Anything else?

No response

Metadata

Assignees

No one assigned

    Labels

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