Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from struct nested in struct #101847

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Comment on lines +1738 to +1740
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use Parser::parse_item_struct instead?

Copy link
Member

@TaKO8Ki TaKO8Ki Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation would eventually look something like the following:

// create a snapshot by using `Parser::ccreate_snapshot_for_diagnostic` at line 1707
// ..
} 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()
        }
    }
}

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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you just use &str?

Suggested change
&format!("consider creating a new `struct` definition instead of nesting"),
"consider creating a new `struct` definition instead of nesting",

"",
Applicability::MaybeIncorrect,
);
Comment on lines +1745 to +1750
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this suggestion should be like the following:

Suggested change
err.span_suggestion(
item.unwrap().span,
&format!("consider creating a new `struct` definition instead of nesting"),
"",
Applicability::MaybeIncorrect,
);
err.span_help("consider creating a new `struct` definition instead of nesting");

err
} else {
self.expected_ident_found()
};
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/parser/issues/issue-101540.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct S1 {
struct S2 {
//~^ ERROR structs are not allowed in struct definitions
}
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/parser/issues/issue-101540.stderr
Original file line number Diff line number Diff line change
@@ -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