Rollup merge of #99786 - obeis:issue-99625, r=compiler-errors

Recover from C++ style `enum struct`

Closes #99625
This commit is contained in:
Matthias Krüger 2022-08-03 22:29:29 +02:00 committed by GitHub
commit 9c18fdc71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 0 deletions

View File

@ -1216,6 +1216,25 @@ impl<'a> Parser<'a> {
/// Parses an enum declaration.
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
if self.token.is_keyword(kw::Struct) {
let mut err = self.struct_span_err(
self.prev_token.span.to(self.token.span),
"`enum` and `struct` are mutually exclusive",
);
err.span_suggestion(
self.prev_token.span.to(self.token.span),
"replace `enum struct` with",
"enum",
Applicability::MachineApplicable,
);
if self.look_ahead(1, |t| t.is_ident()) {
self.bump();
err.emit();
} else {
return Err(err);
}
}
let id = self.parse_ident()?;
let mut generics = self.parse_generics()?;
generics.where_clause = self.parse_where_clause()?;

View File

@ -0,0 +1,13 @@
// run-rustfix
pub enum Range {
//~^ ERROR `enum` and `struct` are mutually exclusive
Valid {
begin: u32,
len: u32,
},
Out,
}
fn main() {
}

View File

@ -0,0 +1,13 @@
// run-rustfix
pub enum struct Range {
//~^ ERROR `enum` and `struct` are mutually exclusive
Valid {
begin: u32,
len: u32,
},
Out,
}
fn main() {
}

View File

@ -0,0 +1,8 @@
error: `enum` and `struct` are mutually exclusive
--> $DIR/issue-99625-enum-struct-mutually-exclusive.rs:3:5
|
LL | pub enum struct Range {
| ^^^^^^^^^^^ help: replace `enum struct` with: `enum`
error: aborting due to previous error