Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Wilfred opened this issue Jan 31, 2023 · 2 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Jan 31, 2023

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

@Wilfred Wilfred added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 31, 2023
@mejrs
Copy link
Contributor

mejrs commented Jan 31, 2023

It seems that

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

expands to

match d { Pet::Dog("fido", to_string()) => true, _ => false, };

@estebank estebank added the A-parser Area: The parsing of Rust source code to an AST label Feb 1, 2023
@fmease
Copy link
Member

fmease commented Feb 29, 2024

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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants