Move where doc comment meant as comment check

The new place makes more sense and covers more cases beyond individual
statements.

```
error: expected one of `.`, `;`, `?`, `else`, or an operator, found doc comment `//!foo
  --> $DIR/doc-comment-in-stmt.rs:25:22
   |
LL |     let y = x.max(1) //!foo
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator
   |
help: add a space before `!` to write a regular comment
   |
LL |     let y = x.max(1) // !foo
   |                        +
```

Fix #65329.
This commit is contained in:
Esteban Küber 2023-10-20 02:54:45 +00:00
parent cc705b8012
commit 20de5c762d
5 changed files with 85 additions and 37 deletions

View file

@ -632,23 +632,6 @@ impl<'a> Parser<'a> {
// Recover from parser, skip type error to avoid extra errors.
Ok(true) => true,
Err(mut e) => {
if let TokenKind::DocComment(..) = self.token.kind
&& let Ok(snippet) = self.span_to_snippet(self.token.span)
{
let sp = self.token.span;
let marker = &snippet[..3];
let (comment_marker, doc_comment_marker) = marker.split_at(2);
e.span_suggestion(
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
format!(
"add a space before `{doc_comment_marker}` to use a regular comment",
),
format!("{comment_marker} {doc_comment_marker}"),
Applicability::MaybeIncorrect,
);
}
if self.recover_colon_as_semi() {
// recover_colon_as_semi has already emitted a nicer error.
e.delay_as_bug();