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

Uninformative error message when using wrong ellipsis in pattern matching #47968

Closed
DRMacIver opened this issue Feb 2, 2018 · 2 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DRMacIver
Copy link

Because I naturally write ellipsises as three dots rather than two, I found myself writing the following:

#[derive(Debug, Clone)]
enum Stuff {
  Blank,
  Holder{
    thing: u64,
  },
}


impl Stuff {
  fn is_blank(&self) -> bool {
    match self {
      &Stuff::Blank => true,
      &Stuff::Holder{...} => false,
    }
  }
}

fn main(){
  println!("{}", Stuff::Blank.is_blank());
}

This is wrong because it should be Stuff::Holder{..}: wrong number of dots in the match.

But the error message I got is this:

error: expected identifier, found `...`
  --> ellipsis.rs:14:22
   |
14 |       &Stuff::Holder{...} => false,
   |                      ^^^

error[E0027]: pattern does not mention field `thing`
  --> ellipsis.rs:14:8
   |
14 |       &Stuff::Holder{...} => false,
   |        ^^^^^^^^^^^^^^^^^^ missing field `thing`

error: aborting due to 2 previous errors

This isn't an intrinsically unreasonable error message or anything, but it totally mislead me: I did not parse "expected identifier, found ..." as meaning "You have the wrong number of dots and three dots is not valid syntax here", I just figured that I was misunderstanding something and that e.g. you had to mention at least one field or something and the ellipsis syntax was only for then ignoring the other fields.

This was I think compounded by the fact that it provided a second error message "pattern does not mention field thing" because it meant that I didn't read it as "You have a syntax error" but "You have a syntactically valid pattern match that happens not to be semantically valid due to some requirement you don't understand".

Meta

rustc --version --verbose:

rustc 1.21.0 (3b72af97e 2017-10-09)
binary: rustc
commit-hash: 3b72af97e42989b2fe104d8edbaee123cdf7c58f
commit-date: 2017-10-09
host: x86_64-unknown-linux-gnu
release: 1.21.0
LLVM version: 4.0
@gsollazzo gsollazzo added C-enhancement Category: An issue proposing an enhancement or a PR with one. 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 Feb 2, 2018
@zackmdavis
Copy link
Member

Hi, David! This was improved in #46763 and will be in the stable release 1.24. The output on nightly is:

error: expected field pattern, found `...`
  --> david.rs:14:28
   |
14 |             &Stuff::Holder{...} => false,
   |                            ^^^ help: to omit remaining fields, use one fewer `.`: `..`

error: aborting due to previous error

@DRMacIver
Copy link
Author

Hi, David! This was improved in #46763 and will be in the stable release 1.24. The output on nightly is:

Ah! So it is. Apologies, I should have tried nightly (I actually thought I was trying this on a much more recent version of rust than it appears that I did based on that output, but I have no one to blame for tht mistake but myself).

Anyway, that new error message is more than good enough for me, thank you! Sorry for the redundant bug report.

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. 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

3 participants