Skip to content

Commit ed45c11

Browse files
authored
Rollup merge of #137281 - estebank:doc-comment-syntax-error, r=compiler-errors
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.
2 parents be73ea8 + a090e76 commit ed45c11

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,6 @@ impl<'a> Parser<'a> {
301301
&mut self,
302302
recover: bool,
303303
) -> PResult<'a, (Ident, IdentIsRaw)> {
304-
if let TokenKind::DocComment(..) = self.prev_token.kind {
305-
return Err(self.dcx().create_err(DocCommentDoesNotDocumentAnything {
306-
span: self.prev_token.span,
307-
missing_comma: None,
308-
}));
309-
}
310-
311304
let valid_follow = &[
312305
TokenKind::Eq,
313306
TokenKind::Colon,
@@ -319,6 +312,15 @@ impl<'a> Parser<'a> {
319312
TokenKind::CloseDelim(Delimiter::Brace),
320313
TokenKind::CloseDelim(Delimiter::Parenthesis),
321314
];
315+
if let TokenKind::DocComment(..) = self.prev_token.kind
316+
&& valid_follow.contains(&self.token.kind)
317+
{
318+
let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {
319+
span: self.prev_token.span,
320+
missing_comma: None,
321+
});
322+
return Err(err);
323+
}
322324

323325
let mut recovered_ident = None;
324326
// we take this here so that the correct original token is retained in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum TestEnum {
2+
Works,
3+
/// Some documentation
4+
Self, //~ ERROR expected identifier, found keyword `Self`
5+
//~^ HELP enum variants can be
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected identifier, found keyword `Self`
2+
--> $DIR/doc-before-bad-variant.rs:4:5
3+
|
4+
LL | enum TestEnum {
5+
| -------- while parsing this enum
6+
...
7+
LL | Self,
8+
| ^^^^ expected identifier, found keyword
9+
|
10+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
11+
12+
error: aborting due to 1 previous error
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Some documentation
2+
<> //~ ERROR expected identifier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected identifier, found `<`
2+
--> $DIR/doc-before-syntax-error.rs:2:1
3+
|
4+
LL | <>
5+
| ^ expected identifier
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)