Skip to content

incorrectly ignoring a field with { _ } yields suggestion that assumes _ is a valid field name  #83263

Closed
@Lotterleben

Description

@Lotterleben

hi,
I think I ran into a bit of an error reporting hickup with my erroneous code.

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cb47de4bf36f023caa0c21fc77ec4271

enum TopException {
    HardFault { stack_overflow: bool },
    Other,
}

fn main() {
    let top_exception = Some(TopException::Other);

    if let Some(TopException::HardFault {_}) = top_exception {
     //                                  ^^ oh no
        todo!();
    }
}

The current output is:

error: expected identifier, found reserved identifier `_`
  --> src/main.rs:10:42
   |
10 |     if let Some(TopException::HardFault {_}) = top_exception {
   |                                          ^ expected identifier, found reserved identifier

error[E0026]: variant `TopException::HardFault` does not have a field named `_`
  --> src/main.rs:10:42
   |
10 |     if let Some(TopException::HardFault {_}) = top_exception {
   |                                          ^ variant `TopException::HardFault` does not have this field

error[E0027]: pattern does not mention field `stack_overflow`
  --> src/main.rs:10:17
   |
10 |     if let Some(TopException::HardFault {_}) = top_exception {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `stack_overflow`
   |
help: include the missing field in the pattern
   |
10 |     if let Some(TopException::HardFault {_, stack_overflow}) = top_exception {
   |                                           ^^^^^^^^^^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
   |
10 |     if let Some(TopException::HardFault {_, ..}) = top_exception {
   |                                           ^^^^

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0026, E0027.
For more information about an error, try `rustc --explain E0026`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Applying this suggestion yields


error: expected identifier, found reserved identifier `_`
  --> src/main.rs:10:42
   |
10 |     if let Some(TopException::HardFault {_, stack_overflow}) = top_exception {
   |                                          ^ expected identifier, found reserved identifier

error[E0026]: variant `TopException::HardFault` does not have a field named `_`
  --> src/main.rs:10:42
   |
10 |     if let Some(TopException::HardFault {_, stack_overflow}) = top_exception {
   |                                          ^ variant `TopException::HardFault` does not have this field

Instead, I would've expected the suggestion to be along the lines of:

if let Some(TopException::HardFault {stack_overflow: _}) = top_exception {

or at least

    if let Some(TopException::HardFault {stack_overflow}) = top_exception {

@jonas-schievink pointed out to me that this is likely caused by rustc recognizing that _ is not a valid field pattern, trying to recover from it by treating it as something like _: _, which also fails because there's no field named _ – but of course this is not a valid field name at all, which should be recognized as such?

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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