diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index e55b5ce71cded..c324557711e60 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1735,6 +1735,20 @@ impl<'a> Parser<'a> { err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks"); err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information"); err + } else if self.check_keyword(kw::Struct) { + let kw_token = self.token.clone(); + let item = self.parse_item(ForceCollect::No)?; + let mut err = self.struct_span_err( + kw_token.span, + &format!("structs are not allowed in {adt_ty} definitions"), + ); + err.span_suggestion( + item.unwrap().span, + &format!("consider creating a new `struct` definition instead of nesting"), + "", + Applicability::MaybeIncorrect, + ); + err } else { self.expected_ident_found() }; diff --git a/src/test/ui/parser/issues/issue-101540.rs b/src/test/ui/parser/issues/issue-101540.rs new file mode 100644 index 0000000000000..328ec6f906b1f --- /dev/null +++ b/src/test/ui/parser/issues/issue-101540.rs @@ -0,0 +1,7 @@ +struct S1 { + struct S2 { + //~^ ERROR structs are not allowed in struct definitions + } +} + +fn main() {} diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr new file mode 100644 index 0000000000000..2e169b42ede7c --- /dev/null +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -0,0 +1,11 @@ +error: structs are not allowed in struct definitions + --> $DIR/issue-101540.rs:2:5 + | +LL | struct S2 { + | _____-^^^^^ +LL | | +LL | | } + | |_____- help: consider creating a new `struct` definition instead of nesting + +error: aborting due to previous error +