Skip to content

Tweak "expected ident" parse error to avoid talking about doc comments #137281

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

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
16 changes: 9 additions & 7 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,6 @@ impl<'a> Parser<'a> {
&mut self,
recover: bool,
) -> PResult<'a, (Ident, IdentIsRaw)> {
if let TokenKind::DocComment(..) = self.prev_token.kind {
return Err(self.dcx().create_err(DocCommentDoesNotDocumentAnything {
span: self.prev_token.span,
missing_comma: None,
}));
}

let valid_follow = &[
TokenKind::Eq,
TokenKind::Colon,
Expand All @@ -319,6 +312,15 @@ impl<'a> Parser<'a> {
TokenKind::CloseDelim(Delimiter::Brace),
TokenKind::CloseDelim(Delimiter::Parenthesis),
];
if let TokenKind::DocComment(..) = self.prev_token.kind
&& valid_follow.contains(&self.token.kind)
{
let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {
span: self.prev_token.span,
missing_comma: None,
});
return Err(err);
}

let mut recovered_ident = None;
// we take this here so that the correct original token is retained in
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/parser/doc-before-bad-variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum TestEnum {
Works,
/// Some documentation
Self, //~ ERROR expected identifier, found keyword `Self`
//~^ HELP enum variants can be
}
13 changes: 13 additions & 0 deletions tests/ui/parser/doc-before-bad-variant.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: expected identifier, found keyword `Self`
--> $DIR/doc-before-bad-variant.rs:4:5
|
LL | enum TestEnum {
| -------- while parsing this enum
...
LL | Self,
| ^^^^ expected identifier, found keyword
|
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error: aborting due to 1 previous error

2 changes: 2 additions & 0 deletions tests/ui/parser/doc-before-syntax-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Some documentation
<> //~ ERROR expected identifier
8 changes: 8 additions & 0 deletions tests/ui/parser/doc-before-syntax-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected identifier, found `<`
--> $DIR/doc-before-syntax-error.rs:2:1
|
LL | <>
| ^ expected identifier

error: aborting due to 1 previous error

Loading