Skip to content

Commit

Permalink
Always emit help when failing to parse enum variant
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 17, 2023
1 parent 9ae5b7c commit 22fc51f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,8 @@ impl<'a> Parser<'a> {
self.recover_diff_marker();
let variant_attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
let help = "enum variants can be `Variant`, `Variant = <integer>`, \
`Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
self.collect_tokens_trailing_token(
variant_attrs,
ForceCollect::No,
Expand Down Expand Up @@ -1486,6 +1488,7 @@ impl<'a> Parser<'a> {
this.eat_to_tokens(&[&token::CloseDelim(Delimiter::Brace)]);
this.bump(); // }
err.span_label(span, "while parsing this enum");
err.help(help);
err.emit();
(thin_vec![], true)
}
Expand All @@ -1502,6 +1505,7 @@ impl<'a> Parser<'a> {
this.eat_to_tokens(&[&token::CloseDelim(Delimiter::Parenthesis)]);
this.bump(); // )
err.span_label(span, "while parsing this enum");
err.help(help);
err.emit();
thin_vec![]
}
Expand All @@ -1527,8 +1531,9 @@ impl<'a> Parser<'a> {

Ok((Some(vr), TrailingToken::MaybeComma))
},
).map_err(|mut err| {
err.help("enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`");
)
.map_err(|mut err| {
err.help(help);
err
})
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-68890.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ LL | enum e{A((?'a a+?+l))}
| - ^ expected one of `)`, `+`, or `,`
| |
| while parsing this enum
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: expected item, found `)`
--> $DIR/issue-68890.rs:1:21
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/parser/recover/recover-enum2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ LL | enum Test4 {
| ----- while parsing this enum
LL | Nope(i32 {})
| ^ expected one of 7 possible tokens
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error[E0308]: mismatched types
--> $DIR/recover-enum2.rs:11:9
Expand Down

0 comments on commit 22fc51f

Please sign in to comment.