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

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

Closed
estebank opened this issue Jan 15, 2018 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 15, 2018

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.

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 15, 2018
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 23, 2018
…, r=oli-obk

Reduce the diagnostic spam when multiple fields are missing in pattern

Fix rust-lang#47457.
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 23, 2018
…, r=oli-obk

Reduce the diagnostic spam when multiple fields are missing in pattern

Fix rust-lang#47457.
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 23, 2018
…, r=oli-obk

Reduce the diagnostic spam when multiple fields are missing in pattern

Fix rust-lang#47457.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

1 participant