Skip to content

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

Open

Description

@Wilfred

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 31, 2023
  2. mejrs commented on Jan 31, 2023

    @mejrs
    Member

    It seems that

    matches!(d, Pet::Dog("fido".to_string()));

    expands to

    match d { Pet::Dog("fido", to_string()) => true, _ => false, };
  3. added
    A-parserArea: The lexing & parsing of Rust source code to an AST
    on Feb 1, 2023
  4. fmease commented on Feb 29, 2024

    @fmease
    Member

    For the manually expanded match form we now provide a better error message since #118625:

    error: expected a pattern, found a method call
      --> src/main.rs:13:24
       |
    13 |     match d { Pet::Dog("fido".to_string()) => true, _ => false };
       |                        ^^^^^^^^^^^^^^^^^^ method calls are not allowed in patterns
    

    For the matches! form, nothing has changed because we generally no longer try to recover anything inside of a macro context to prevent issues of the form #103534 (we haven't updated the entirety of the parser yet though that's why the issue is still open). Ideally, we would actually do that but due to the way macro matching is implemented, recovery is oftentimes observable by macros and may lead to breakage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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