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

Suggest struct when we get colon in fileds in enum #111118

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ impl<'a> Parser<'a> {
}
}

let prev_span = self.prev_token.span;
let id = self.parse_ident()?;
let mut generics = self.parse_generics()?;
generics.where_clause = self.parse_where_clause()?;
Expand All @@ -1273,10 +1274,28 @@ impl<'a> Parser<'a> {
(thin_vec![], false)
} else {
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err(
|mut e| {
e.span_label(id.span, "while parsing this enum");
|mut err| {
err.span_label(id.span, "while parsing this enum");
if self.token == token::Colon {
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Ok(_) => {
err.span_suggestion_verbose(
prev_span,
"perhaps you meant to use `struct` here",
"struct".to_string(),
Applicability::MaybeIncorrect,
);
}
Err(e) => {
e.cancel();
}
}
self.restore_snapshot(snapshot);
}
self.recover_stmt();
e
err
},
)?
};
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/structs-enums/issue-103869.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-rustfix

struct VecOrMap {
//~^ HELP: perhaps you meant to use `struct` here
vec: Vec<usize>,
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
//~| HELP: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
}

fn main() {
let o = VecOrMap { vec: vec![1, 2, 3] };
println!("{:?}", o.vec);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
enum VecOrMap{
// run-rustfix

enum VecOrMap {
//~^ HELP: perhaps you meant to use `struct` here
vec: Vec<usize>,
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
//~| HELP: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
map: HashMap<String,usize>
}

fn main() {}
fn main() {
let o = VecOrMap { vec: vec![1, 2, 3] };
println!("{:?}", o.vec);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
--> $DIR/issue-103869.rs:2:8
--> $DIR/issue-103869.rs:5:8
|
LL | enum VecOrMap{
LL | enum VecOrMap {
| -------- while parsing this enum
LL |
LL | vec: Vec<usize>,
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
help: perhaps you meant to use `struct` here
|
LL | struct VecOrMap {
| ~~~~~~

error: aborting due to previous error