Skip to content

Commit e486f5e

Browse files
authored
Rollup merge of #68669 - euclio:doc-comment-stmt, r=estebank
suggest adding space in accidental doc comments Fixes #67553. r? @estebank
2 parents e33fe2a + db319a8 commit e486f5e

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/librustc_parse/parser/stmt.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use crate::maybe_whole;
77
use crate::DirectoryOwnership;
88

99
use rustc_errors::{Applicability, PResult};
10-
use rustc_span::source_map::{respan, Span};
10+
use rustc_span::source_map::{respan, BytePos, Span};
1111
use rustc_span::symbol::{kw, sym, Symbol};
1212
use syntax::ast;
1313
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle, VisibilityKind};
1414
use syntax::ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt, StmtKind, DUMMY_NODE_ID};
1515
use syntax::ptr::P;
16-
use syntax::token;
16+
use syntax::token::{self, TokenKind};
1717
use syntax::util::classify;
1818

1919
use std::mem;
@@ -431,6 +431,23 @@ impl<'a> Parser<'a> {
431431
if let Err(mut e) =
432432
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
433433
{
434+
if let TokenKind::DocComment(..) = self.token.kind {
435+
if let Ok(snippet) = self.span_to_snippet(self.token.span) {
436+
let sp = self.token.span;
437+
let marker = &snippet[..3];
438+
let (comment_marker, doc_comment_marker) = marker.split_at(2);
439+
440+
e.span_suggestion(
441+
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
442+
&format!(
443+
"add a space before `{}` to use a regular comment",
444+
doc_comment_marker,
445+
),
446+
format!("{} {}", comment_marker, doc_comment_marker),
447+
Applicability::MaybeIncorrect,
448+
);
449+
}
450+
}
434451
e.emit();
435452
self.recover_stmt();
436453
// Don't complain about type errors in body tail after parse error (#57383).
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn foo() -> bool {
2+
false
3+
//!self.allow_ty_infer()
4+
//~^ ERROR found doc comment
5+
}
6+
7+
fn bar() -> bool {
8+
false
9+
/*! bar */ //~ ERROR found doc comment
10+
}
11+
12+
fn baz() -> i32 {
13+
1 /** baz */ //~ ERROR found doc comment
14+
}
15+
16+
fn quux() -> i32 {
17+
2 /*! quux */ //~ ERROR found doc comment
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `//!self.allow_ty_infer()`
2+
--> $DIR/doc-comment-in-stmt.rs:3:5
3+
|
4+
LL | false
5+
| - expected one of `.`, `;`, `?`, `}`, or an operator
6+
LL | //!self.allow_ty_infer()
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
8+
|
9+
help: add a space before `!` to use a regular comment
10+
|
11+
LL | // !self.allow_ty_infer()
12+
| ^^^^
13+
14+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! bar */`
15+
--> $DIR/doc-comment-in-stmt.rs:9:5
16+
|
17+
LL | false
18+
| - expected one of `.`, `;`, `?`, `}`, or an operator
19+
LL | /*! bar */
20+
| ^^^^^^^^^^ unexpected token
21+
|
22+
help: add a space before `!` to use a regular comment
23+
|
24+
LL | /* ! bar */
25+
| ^^^^
26+
27+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/** baz */`
28+
--> $DIR/doc-comment-in-stmt.rs:13:7
29+
|
30+
LL | 1 /** baz */
31+
| ^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
32+
|
33+
help: add a space before `*` to use a regular comment
34+
|
35+
LL | 1 /* * baz */
36+
| ^^^^
37+
38+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! quux */`
39+
--> $DIR/doc-comment-in-stmt.rs:17:7
40+
|
41+
LL | 2 /*! quux */
42+
| ^^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
43+
|
44+
help: add a space before `!` to use a regular comment
45+
|
46+
LL | 2 /* ! quux */
47+
| ^^^^
48+
49+
error: aborting due to 4 previous errors
50+

0 commit comments

Comments
 (0)