diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 2c1e5807aa7f9..5670729253dac 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -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()?; diff --git a/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.fixed b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.fixed new file mode 100644 index 0000000000000..4b4a416b1ac82 --- /dev/null +++ b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.fixed @@ -0,0 +1,13 @@ +// run-rustfix + +pub enum Range { + //~^ ERROR `enum` and `struct` are mutually exclusive + Valid { + begin: u32, + len: u32, + }, + Out, +} + +fn main() { +} diff --git a/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs new file mode 100644 index 0000000000000..9cc886641293b --- /dev/null +++ b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.rs @@ -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() { +} diff --git a/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.stderr b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.stderr new file mode 100644 index 0000000000000..edc640bf5ec22 --- /dev/null +++ b/src/test/ui/parser/issue-99625-enum-struct-mutually-exclusive.stderr @@ -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 +