Skip to content

Collect multiple missing fields on struct pattern into a single diagnostic #47457

Closed
@estebank

Description

@estebank

When confusing a Fn-like struct with a regular struct in an if let pattern, the output leaves a lot to be desired:

struct S(usize, usize, usize, usize);

fn main() {
    if let S { a, b, c, d } = S(1, 2, 3, 4) {
        println!("hi");
    }
}
Current output
error[E0026]: struct `S` does not have a field named `a`
 --> src/main.rs:4:16
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |                ^ struct `S` does not have field `a`
 
error[E0026]: struct `S` does not have a field named `b`
 --> src/main.rs:4:19
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |                   ^ struct `S` does not have field `b`

error[E0026]: struct `S` does not have a field named `c`
 --> src/main.rs:4:22
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |                      ^ struct `S` does not have field `c`

error[E0026]: struct `S` does not have a field named `d`
 --> src/main.rs:4:25
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |                         ^ struct `S` does not have field `d`

error[E0027]: pattern does not mention field `0`
 --> src/main.rs:4:12
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^^^ missing field `0`
  |
  = note: trying to match a tuple variant with a struct variant pattern

error[E0027]: pattern does not mention field `1`
 --> src/main.rs:4:12
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^^^ missing field `1`
  |
  = note: trying to match a tuple variant with a struct variant pattern

error[E0027]: pattern does not mention field `2`
 --> src/main.rs:4:12
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^^^ missing field `2`
  |
  = note: trying to match a tuple variant with a struct variant pattern

error[E0027]: pattern does not mention field `3`
 --> src/main.rs:4:12
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^^^ missing field `3`
  |
  = note: trying to match a tuple variant with a struct variant pattern

error: aborting due to 8 previous errors

We should probably collect all these errors into a single diagnostic error:

error[E0026]: struct `S` does not have a field named `a`, `b`, `c` or `d`
 --> src/main.rs:4:16
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |                ^  ^  ^  ^ struct `S` does not have these fields
  = note: trying to match a tuple variant with a struct variant pattern

error[E0027]: pattern does not mention fields `0`, `1`, `2`, or `3`
 --> src/main.rs:4:12
  |
4 |     if let S { a, b, c, d } = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^^^ missing fields `0`, `1`, `2`, and `3`
  |
  = note: trying to match a tuple variant with a struct variant pattern
  = help: you probably meant to use tuple struct syntax:
  |
4 |     if let S (a, b, c, d) = S(1, 2, 3, 4) {
  |            ^^^^^^^^^^^^^^

Even better if there was only one diagnostic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions