Skip to content

Commit d68432a

Browse files
authored
Rollup merge of #142341 - xizheyin:142311, r=fee1-dead
Don't suggest converting `///` to `//` when expecting `,` Fixes #142311
2 parents 4479d42 + c63665c commit d68432a

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -686,23 +686,34 @@ impl<'a> Parser<'a> {
686686
}
687687

688688
if let token::DocComment(kind, style, _) = self.token.kind {
689-
// We have something like `expr //!val` where the user likely meant `expr // !val`
690-
let pos = self.token.span.lo() + BytePos(2);
691-
let span = self.token.span.with_lo(pos).with_hi(pos);
692-
err.span_suggestion_verbose(
693-
span,
694-
format!(
695-
"add a space before {} to write a regular comment",
696-
match (kind, style) {
697-
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
698-
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
699-
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
700-
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
701-
},
702-
),
703-
" ".to_string(),
704-
Applicability::MachineApplicable,
705-
);
689+
// This is to avoid suggesting converting a doc comment to a regular comment
690+
// when missing a comma before the doc comment in lists (#142311):
691+
//
692+
// ```
693+
// enum Foo{
694+
// A /// xxxxxxx
695+
// B,
696+
// }
697+
// ```
698+
if !expected.contains(&TokenType::Comma) {
699+
// We have something like `expr //!val` where the user likely meant `expr // !val`
700+
let pos = self.token.span.lo() + BytePos(2);
701+
let span = self.token.span.with_lo(pos).with_hi(pos);
702+
err.span_suggestion_verbose(
703+
span,
704+
format!(
705+
"add a space before {} to write a regular comment",
706+
match (kind, style) {
707+
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
708+
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
709+
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
710+
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
711+
},
712+
),
713+
" ".to_string(),
714+
Applicability::MaybeIncorrect,
715+
);
716+
}
706717
}
707718

708719
let sp = if self.token == token::Eof {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Check that if the parser suggests converting `///` to a regular comment
2+
//! when it appears after a missing comma in an list (e.g. `enum` variants).
3+
//!
4+
//! Related issue
5+
//! - https://github.com/rust-lang/rust/issues/142311
6+
7+
enum Foo {
8+
/// Like the noise a sheep makes
9+
Bar
10+
/// Like where people drink
11+
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like where people drink`
12+
Baa///xxxxxx
13+
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
14+
Baz///xxxxxx
15+
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
16+
}
17+
18+
fn foo() {
19+
let a = [
20+
1///xxxxxx
21+
//~^ ERROR expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
22+
2
23+
];
24+
}
25+
26+
fn bar() {
27+
let a = [
28+
1,
29+
2///xxxxxx
30+
//~^ ERROR expected one of `,`, `.`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
31+
];
32+
}
33+
34+
fn main() {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like where people drink`
2+
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:10:5
3+
|
4+
LL | Bar
5+
| -
6+
| |
7+
| expected one of `(`, `,`, `=`, `{`, or `}`
8+
| help: missing `,`
9+
LL | /// Like where people drink
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
11+
12+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
13+
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:12:8
14+
|
15+
LL | Baa///xxxxxx
16+
| -^^^^^^^^
17+
| |
18+
| expected one of `(`, `,`, `=`, `{`, or `}`
19+
| help: missing `,`
20+
21+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
22+
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:14:8
23+
|
24+
LL | Baz///xxxxxx
25+
| ^^^^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
26+
|
27+
= help: doc comments must come before what they document, if a comment was intended use `//`
28+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
29+
30+
error: expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
31+
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:20:10
32+
|
33+
LL | 1///xxxxxx
34+
| ^^^^^^^^^ expected one of `,`, `.`, `;`, `?`, `]`, or an operator
35+
36+
error: expected one of `,`, `.`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
37+
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:29:10
38+
|
39+
LL | 2///xxxxxx
40+
| ^^^^^^^^^ expected one of `,`, `.`, `?`, `]`, or an operator
41+
42+
error: aborting due to 5 previous errors
43+

0 commit comments

Comments
 (0)