Rollup merge of #111118 - chenyukang:yukang-sugg-struct, r=compiler-errors
Suggest struct when we get colon in fileds in enum A follow-up fix for https://github.com/rust-lang/rust/pull/109128 From: https://github.com/rust-lang/rust/pull/109128#discussion_r1179304932 r? `@estebank`
This commit is contained in:
commit
beb49671c2
4 changed files with 50 additions and 8 deletions
|
@ -1262,6 +1262,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let prev_span = self.prev_token.span;
|
||||||
let id = self.parse_ident()?;
|
let id = self.parse_ident()?;
|
||||||
let mut generics = self.parse_generics()?;
|
let mut generics = self.parse_generics()?;
|
||||||
generics.where_clause = self.parse_where_clause()?;
|
generics.where_clause = self.parse_where_clause()?;
|
||||||
|
@ -1273,10 +1274,28 @@ impl<'a> Parser<'a> {
|
||||||
(thin_vec![], false)
|
(thin_vec![], false)
|
||||||
} else {
|
} else {
|
||||||
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err(
|
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err(
|
||||||
|mut e| {
|
|mut err| {
|
||||||
e.span_label(id.span, "while parsing this enum");
|
err.span_label(id.span, "while parsing this enum");
|
||||||
|
if self.token == token::Colon {
|
||||||
|
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();
|
self.recover_stmt();
|
||||||
e
|
err
|
||||||
},
|
},
|
||||||
)?
|
)?
|
||||||
};
|
};
|
||||||
|
|
13
tests/ui/structs-enums/issue-103869.fixed
Normal file
13
tests/ui/structs-enums/issue-103869.fixed
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -1,8 +1,13 @@
|
||||||
enum VecOrMap{
|
// run-rustfix
|
||||||
|
|
||||||
|
enum VecOrMap {
|
||||||
|
//~^ HELP: perhaps you meant to use `struct` here
|
||||||
vec: Vec<usize>,
|
vec: Vec<usize>,
|
||||||
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
|
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
|
||||||
//~| HELP: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
|
//~| 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);
|
||||||
|
}
|
|
@ -1,12 +1,17 @@
|
||||||
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
|
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
|
| -------- while parsing this enum
|
||||||
|
LL |
|
||||||
LL | vec: Vec<usize>,
|
LL | vec: Vec<usize>,
|
||||||
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
|
| ^ expected one of `(`, `,`, `=`, `{`, or `}`
|
||||||
|
|
|
|
||||||
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
|
= 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
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Reference in a new issue