From a1062b9bdcf691fc09f8683ae36d2b31c06c3d8f Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Thu, 22 Sep 2022 23:41:38 +0800 Subject: [PATCH 1/3] recover from struct nested in struct --- compiler/rustc_parse/src/parser/item.rs | 17 +++++++++++++++++ src/test/ui/parser/issues/issue-101540.rs | 7 +++++++ src/test/ui/parser/issues/issue-101540.stderr | 10 ++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/test/ui/parser/issues/issue-101540.rs create mode 100644 src/test/ui/parser/issues/issue-101540.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index e55b5ce71cded..e385ac44113a6 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1715,6 +1715,7 @@ impl<'a> Parser<'a> { fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> { let (ident, is_raw) = self.ident_or_err()?; if !is_raw && ident.is_reserved() { + let snapshot = self.create_snapshot_for_diagnostic(); let err = if self.check_fn_front_matter(false) { let inherited_vis = Visibility { span: rustc_span::DUMMY_SP, @@ -1735,6 +1736,22 @@ 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.eat_keyword(kw::Struct) { + match self.parse_item_struct() { + Ok((ident, _)) => { + let mut err = self.struct_span_err( + lo.with_hi(ident.span.hi()), + &format!("structs are not allowed in {adt_ty} definitions"), + ); + err.help("consider creating a new `struct` definition instead of nesting"); + err + } + Err(err) => { + err.cancel(); + self.restore_snapshot(snapshot); + self.expected_ident_found() + } + } } 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..42e471cc13504 --- /dev/null +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -0,0 +1,10 @@ +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 From f4170b38c8735daa9e764ea395c6c32cd7b9ffa3 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 23 Sep 2022 00:02:50 +0800 Subject: [PATCH 2/3] Change UI test stderr --- src/test/ui/parser/issues/issue-101540.stderr | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr index 42e471cc13504..d4e861b6061fa 100644 --- a/src/test/ui/parser/issues/issue-101540.stderr +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -2,9 +2,8 @@ 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 + | ^^^^^^^^^ + | + = help: consider creating a new `struct` definition instead of nesting error: aborting due to previous error From c66c2e82c1a405ed3fff99953ab96de03806ca09 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 23 Sep 2022 00:24:30 +0800 Subject: [PATCH 3/3] Fix slight issues with stderr --- src/test/ui/parser/issues/issue-101540.stderr | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr index d4e861b6061fa..53c7c9590e6ca 100644 --- a/src/test/ui/parser/issues/issue-101540.stderr +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -1,9 +1,10 @@ error: structs are not allowed in struct definitions --> $DIR/issue-101540.rs:2:5 | -LL | struct S2 { - | ^^^^^^^^^ +LL | struct S2 { + | ^^^^^^^^^ | = help: consider creating a new `struct` definition instead of nesting error: aborting due to previous error +