Skip to content

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

Closed
@DRMacIver

Description

@DRMacIver

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

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