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

When encountering a doc comment without an identifier after, we'd unconditionally state "this doc comment doesn't document anything", swallowing the *actual* error which is that the thing *after* the doc comment wasn't expected. Added a check that the found token is something that "conceptually" closes the previous item before emitting that error, otherwise just complain about the missing identifier.

In both of the following cases, the syntax error follows a doc comment:
```
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: expected identifier, found `<`
  --> $DIR/doc-before-syntax-error.rs:2:1
   |
LL | <>
   | ^ expected identifier
```

Fix #71982.
This commit is contained in:
Esteban Küber 2025-02-19 17:26:03 +00:00
parent d88ffcdb8b
commit a090e76dab
5 changed files with 38 additions and 7 deletions

View file

@ -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,
@ -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