Skip to content

Handle Foo { a: usize, b: usize } used as Foo(1, 2) gracefully #61326

@estebank

Description

@estebank

Given the code

enum Enum {
Foo { a: usize, b: usize },
Bar(usize, usize),
}
fn main() {
let x = Enum::Foo(a: 3, b: 4);
//~^ ERROR expected type, found `3`
match x {
Enum::Foo(a, b) => {}
//~^ ERROR expected tuple struct/variant, found struct variant `Enum::Foo`
Enum::Bar(a, b) => {}
}
}

we emit

error: expected type, found `3`
--> $DIR/recover-from-bad-variant.rs:7:26
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/recover-from-bad-variant.rs:7:23
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^
= help: this might be indicative of a syntax error elsewhere
error[E0532]: expected tuple struct/variant, found struct variant `Enum::Foo`
--> $DIR/recover-from-bad-variant.rs:10:9
|
LL | Enum::Foo(a, b) => {}
| ^^^^^^^^^ did you mean `Enum::Foo { /* fields */ }`?
error: aborting due to 2 previous errors

We should emit something closer to

error: expected type, found `3`
  --> $DIR/recover-from-bad-variant.rs:7:26
   |
LL |     let x = Enum::Foo(a: 3, b: 4);
   |                          ^ expecting a type here because of type ascription

error[E0532]: expected tuple struct/variant, found struct variant `Enum::Foo`
  --> $DIR/recover-from-bad-variant.rs:10:9
   |
LL |         Enum::Foo(a, b) => {}
   |         ^^^^^^^^^ did you mean `Enum::Foo { a, b }`?

error: aborting due to 2 previous errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-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