Skip to content

Commit 274455a

Browse files
committed
Auto merge of #116965 - estebank:issue-65329, r=cjgillot
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.
2 parents f31316f + 20de5c7 commit 274455a

File tree

5 files changed

+85
-37
lines changed

5 files changed

+85
-37
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_errors::{
3939
use rustc_session::errors::ExprParenthesesNeeded;
4040
use rustc_span::source_map::Spanned;
4141
use rustc_span::symbol::{kw, sym, Ident};
42-
use rustc_span::{Span, SpanSnippetError, Symbol, DUMMY_SP};
42+
use rustc_span::{BytePos, Span, SpanSnippetError, Symbol, DUMMY_SP};
4343
use std::mem::take;
4444
use std::ops::{Deref, DerefMut};
4545
use thin_vec::{thin_vec, ThinVec};
@@ -648,6 +648,26 @@ impl<'a> Parser<'a> {
648648
);
649649
}
650650

651+
if let token::DocComment(kind, style, _) = self.token.kind {
652+
// We have something like `expr //!val` where the user likely meant `expr // !val`
653+
let pos = self.token.span.lo() + BytePos(2);
654+
let span = self.token.span.with_lo(pos).with_hi(pos);
655+
err.span_suggestion_verbose(
656+
span,
657+
format!(
658+
"add a space before {} to write a regular comment",
659+
match (kind, style) {
660+
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
661+
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
662+
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
663+
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
664+
},
665+
),
666+
" ".to_string(),
667+
Applicability::MachineApplicable,
668+
);
669+
}
670+
651671
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
652672
// there are unclosed angle brackets
653673
if self.unmatched_angle_bracket_count > 0

compiler/rustc_parse/src/parser/stmt.rs

-17
Original file line numberDiff line numberDiff line change
@@ -632,23 +632,6 @@ impl<'a> Parser<'a> {
632632
// Recover from parser, skip type error to avoid extra errors.
633633
Ok(true) => true,
634634
Err(mut e) => {
635-
if let TokenKind::DocComment(..) = self.token.kind
636-
&& let Ok(snippet) = self.span_to_snippet(self.token.span)
637-
{
638-
let sp = self.token.span;
639-
let marker = &snippet[..3];
640-
let (comment_marker, doc_comment_marker) = marker.split_at(2);
641-
642-
e.span_suggestion(
643-
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
644-
format!(
645-
"add a space before `{doc_comment_marker}` to use a regular comment",
646-
),
647-
format!("{comment_marker} {doc_comment_marker}"),
648-
Applicability::MaybeIncorrect,
649-
);
650-
}
651-
652635
if self.recover_colon_as_semi() {
653636
// recover_colon_as_semi has already emitted a nicer error.
654637
e.delay_as_bug();
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run-rustfix
2+
#![allow(unused)]
3+
fn foo() -> bool {
4+
false
5+
// !self.allow_ty_infer()
6+
//~^ ERROR found doc comment
7+
}
8+
9+
fn bar() -> bool {
10+
false
11+
/* ! bar */ //~ ERROR found doc comment
12+
}
13+
14+
fn baz() -> i32 {
15+
1 /* * baz */ //~ ERROR found doc comment
16+
}
17+
18+
fn quux() -> i32 {
19+
2 // / quux
20+
//~^ ERROR found doc comment
21+
}
22+
23+
fn main() {
24+
let x = 0;
25+
let y = x.max(1) // !foo //~ ERROR found doc comment
26+
.min(2);
27+
}

tests/ui/parser/doc-comment-in-stmt.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
#![allow(unused)]
13
fn foo() -> bool {
24
false
35
//!self.allow_ty_infer()
@@ -14,7 +16,12 @@ fn baz() -> i32 {
1416
}
1517

1618
fn quux() -> i32 {
17-
2 /*! quux */ //~ ERROR found doc comment
19+
2 /// quux
20+
//~^ ERROR found doc comment
1821
}
1922

20-
fn main() {}
23+
fn main() {
24+
let x = 0;
25+
let y = x.max(1) //!foo //~ ERROR found doc comment
26+
.min(2);
27+
}
+28-17
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,61 @@
11
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `//!self.allow_ty_infer()`
2-
--> $DIR/doc-comment-in-stmt.rs:3:5
2+
--> $DIR/doc-comment-in-stmt.rs:5:5
33
|
44
LL | false
55
| - expected one of `.`, `;`, `?`, `}`, or an operator
66
LL | //!self.allow_ty_infer()
77
| ^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
88
|
9-
help: add a space before `!` to use a regular comment
9+
help: add a space before `!` to write a regular comment
1010
|
1111
LL | // !self.allow_ty_infer()
12-
| ~~~~
12+
| +
1313

1414
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! bar */`
15-
--> $DIR/doc-comment-in-stmt.rs:9:5
15+
--> $DIR/doc-comment-in-stmt.rs:11:5
1616
|
1717
LL | false
1818
| - expected one of `.`, `;`, `?`, `}`, or an operator
1919
LL | /*! bar */
2020
| ^^^^^^^^^^ unexpected token
2121
|
22-
help: add a space before `!` to use a regular comment
22+
help: add a space before `!` to write a regular comment
2323
|
2424
LL | /* ! bar */
25-
| ~~~~
25+
| +
2626

2727
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/** baz */`
28-
--> $DIR/doc-comment-in-stmt.rs:13:7
28+
--> $DIR/doc-comment-in-stmt.rs:15:7
2929
|
3030
LL | 1 /** baz */
3131
| ^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
3232
|
33-
help: add a space before `*` to use a regular comment
33+
help: add a space before the last `*` to write a regular comment
3434
|
3535
LL | 1 /* * baz */
36-
| ~~~~
36+
| +
3737

38-
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! quux */`
39-
--> $DIR/doc-comment-in-stmt.rs:17:7
38+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/// quux`
39+
--> $DIR/doc-comment-in-stmt.rs:19:7
4040
|
41-
LL | 2 /*! quux */
42-
| ^^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
41+
LL | 2 /// quux
42+
| ^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
4343
|
44-
help: add a space before `!` to use a regular comment
44+
help: add a space before the last `/` to write a regular comment
4545
|
46-
LL | 2 /* ! quux */
47-
| ~~~~
46+
LL | 2 // / quux
47+
| +
4848

49-
error: aborting due to 4 previous errors
49+
error: expected one of `.`, `;`, `?`, `else`, or an operator, found doc comment `//!foo
50+
--> $DIR/doc-comment-in-stmt.rs:25:22
51+
|
52+
LL | let y = x.max(1) //!foo
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator
54+
|
55+
help: add a space before `!` to write a regular comment
56+
|
57+
LL | let y = x.max(1) // !foo
58+
| +
59+
60+
error: aborting due to 5 previous errors
5061

0 commit comments

Comments
 (0)