Skip to content

Commit 0adf293

Browse files
authored
Rollup merge of #102143 - Rageking8:fix-101540, r=TaKO8Ki
Recover from struct nested in struct Fixes #101540 r? `@TaKO8Ki` Not sure If I have done it right.
2 parents 3288d3a + c66c2e8 commit 0adf293

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

compiler/rustc_parse/src/parser/item.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ impl<'a> Parser<'a> {
17151715
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
17161716
let (ident, is_raw) = self.ident_or_err()?;
17171717
if !is_raw && ident.is_reserved() {
1718+
let snapshot = self.create_snapshot_for_diagnostic();
17181719
let err = if self.check_fn_front_matter(false) {
17191720
let inherited_vis = Visibility {
17201721
span: rustc_span::DUMMY_SP,
@@ -1735,6 +1736,22 @@ impl<'a> Parser<'a> {
17351736
err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
17361737
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
17371738
err
1739+
} else if self.eat_keyword(kw::Struct) {
1740+
match self.parse_item_struct() {
1741+
Ok((ident, _)) => {
1742+
let mut err = self.struct_span_err(
1743+
lo.with_hi(ident.span.hi()),
1744+
&format!("structs are not allowed in {adt_ty} definitions"),
1745+
);
1746+
err.help("consider creating a new `struct` definition instead of nesting");
1747+
err
1748+
}
1749+
Err(err) => {
1750+
err.cancel();
1751+
self.restore_snapshot(snapshot);
1752+
self.expected_ident_found()
1753+
}
1754+
}
17381755
} else {
17391756
self.expected_ident_found()
17401757
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct S1 {
2+
struct S2 {
3+
//~^ ERROR structs are not allowed in struct definitions
4+
}
5+
}
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: structs are not allowed in struct definitions
2+
--> $DIR/issue-101540.rs:2:5
3+
|
4+
LL | struct S2 {
5+
| ^^^^^^^^^
6+
|
7+
= help: consider creating a new `struct` definition instead of nesting
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)