Skip to content

Commit

Permalink
Rollup merge of #99786 - obeis:issue-99625, r=compiler-errors
Browse files Browse the repository at this point in the history
Recover from C++ style `enum struct`

Closes #99625
  • Loading branch information
matthiaskrgr authored Aug 3, 2022
2 parents 0de7f75 + 0ad06f1 commit 9c18fdc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-rustfix

pub enum Range {
//~^ ERROR `enum` and `struct` are mutually exclusive
Valid {
begin: u32,
len: u32,
},
Out,
}

fn main() {
}
13 changes: 13 additions & 0 deletions src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs
Original file line number Diff line number Diff line change
@@ -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() {
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9c18fdc

Please sign in to comment.