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

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

Closed
estebank opened this issue May 29, 2019 · 1 comment · Fixed by #75354 or #88729
Closed

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

estebank opened this issue May 29, 2019 · 1 comment · Fixed by #75354 or #88729
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented May 29, 2019

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
@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 A-parser Area: The parsing of Rust source code to an AST. labels May 29, 2019
@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 30, 2019
@estebank estebank added E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. P-low Low priority labels Jun 11, 2019
@estebank estebank removed the A-parser Area: The parsing of Rust source code to an AST. label Oct 18, 2019
@bors bors closed this as completed in ef1d58e Aug 12, 2020
@estebank
Copy link
Contributor Author

estebank commented Sep 5, 2021

Reopening to track the let x = Enum::Foo(a: 3, b: 4); case.

@estebank estebank reopened this Sep 5, 2021
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. and removed E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. labels Sep 5, 2021
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 16, 2021
…s, r=oli-obk

Recover from `Foo(a: 1, b: 2)`

Detect likely `struct` literal using parentheses as delimiters and emit
targeted suggestion instead of type ascription parse error.

Fix rust-lang#61326.
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 16, 2021
…s, r=oli-obk

Recover from `Foo(a: 1, b: 2)`

Detect likely `struct` literal using parentheses as delimiters and emit
targeted suggestion instead of type ascription parse error.

Fix rust-lang#61326.
@bors bors closed this as completed in b82ec36 Sep 16, 2021
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 A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants