Skip to content

Bad suggestion for tuple struct used in pattern with struct syntax and missing fields #80174

Closed
@estebank

Description

@estebank

For the following code:

struct S(i32, f32);
enum E {
    S(i32, f32),
}
fn main() {
    let x = E::S(1, 2.2);
    match x {
        E::S {} => {}
    }
    let y = S(1, 2.2);
    match y {
        S {} => {}
    }
}

we currently emit

error[E0027]: pattern does not mention fields `0`, `1`
 --> src/main.rs:8:9
  |
8 |         E::S {} => {}
  |         ^^^^^^^ missing fields `0`, `1`
  |
help: include the missing fields in the pattern
  |
8 |         E::S { 0, 1 } => {}
  |              ^^^^^^^^
help: if you don't care about these missing fields, you can explicitely ignore them
  |
8 |         E::S { .. } => {}
  |              ^^^^^^

error[E0027]: pattern does not mention fields `0`, `1`
  --> src/main.rs:12:9
   |
12 |         S {} => {}
   |         ^^^^ missing fields `0`, `1`
   |
help: include the missing fields in the pattern
   |
12 |         S { 0, 1 } => {}
   |           ^^^^^^^^
help: if you don't care about these missing fields, you can explicitely ignore them
   |
12 |         S { .. } => {}
   |           ^^^^^^

but struct patterns cannot refer to tuple field names with numbers, as the parser expects identifiers.

I think that 1) the parser should be changed to accept the positions, but ultimately reject them, suggesting to use the tuple syntax and 2) that the suggestion should be for the tuple syntax to begin with.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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