Skip to content

Commit af758e9

Browse files
committed
Suggest add missing , before doc comments in parsing item list
Signed-off-by: xizheyin <[email protected]>
1 parent 3309d0f commit af758e9

File tree

3 files changed

+55
-37
lines changed

3 files changed

+55
-37
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -685,24 +685,38 @@ impl<'a> Parser<'a> {
685685
);
686686
}
687687

688+
// This is used to avoid adding suggestions for doc comments in item list.
689+
let is_expect_item_list_comma =
690+
edible.len() == 1 && edible.iter().any(|e| e.token_type == TokenType::Comma);
688691
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-
);
692+
if !is_expect_item_list_comma {
693+
// We have something like `expr //!val` where the user likely meant `expr // !val`
694+
let pos = self.token.span.lo() + BytePos(2);
695+
let span = self.token.span.with_lo(pos).with_hi(pos);
696+
err.span_suggestion_verbose(
697+
span,
698+
format!(
699+
"add a space before {} to write a regular comment",
700+
match (kind, style) {
701+
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
702+
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
703+
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
704+
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
705+
},
706+
),
707+
" ".to_string(),
708+
Applicability::MachineApplicable,
709+
);
710+
} else {
711+
// For fine-grained suggestions, we should suggest to add a comma before the doc comment
712+
// like `expr, //!val`
713+
err.span_suggestion_verbose(
714+
self.prev_token.span.shrink_to_hi(),
715+
"missing `,` before the doc comment",
716+
", ".to_string(),
717+
Applicability::MachineApplicable,
718+
);
719+
}
706720
}
707721

708722
let sp = if self.token == token::Eof {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,27 @@ impl<'a> Parser<'a> {
918918
Ok(t) => {
919919
// Parsed successfully, therefore most probably the code only
920920
// misses a separator.
921-
expect_err
922-
.with_span_suggestion_short(
921+
922+
// FIXME(xizheyin): This is a hack to avoid adding suggestions for doc comments in item list.
923+
let contains_missing_comma_suggestion =
924+
expect_err.suggestions.clone().unwrap_tag().iter().any(
925+
|s| {
926+
if let Some(msg) = s.msg.as_str() {
927+
msg.contains("missing `,`")
928+
} else {
929+
false
930+
}
931+
},
932+
);
933+
if !contains_missing_comma_suggestion {
934+
expect_err.span_suggestion_short(
923935
sp,
924936
format!("missing `{token_str}`"),
925937
token_str,
926938
Applicability::MaybeIncorrect,
927-
)
928-
.emit();
929-
939+
);
940+
}
941+
expect_err.emit();
930942
v.push(t);
931943
continue;
932944
}

tests/ui/enum/enum-lack-comma-suggestion-issue-142311.stderr

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ LL | Bar
66
LL | /// Like where people drink
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
88
|
9-
help: add a space before the last `/` to write a regular comment
9+
help: missing `,` before the doc comment
1010
|
11-
LL | // / Like where people drink
12-
| +
13-
help: missing `,`
14-
|
15-
LL | Bar,
11+
LL | Bar,
1612
| +
1713

1814
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
@@ -21,13 +17,9 @@ error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
2117
LL | Baa///xxxxxx
2218
| ^^^^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
2319
|
24-
help: add a space before the last `/` to write a regular comment
25-
|
26-
LL | Baa// /xxxxxx
27-
| +
28-
help: missing `,`
20+
help: missing `,` before the doc comment
2921
|
30-
LL | Baa,///xxxxxx
22+
LL | Baa, ///xxxxxx
3123
| +
3224

3325
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
@@ -38,10 +30,10 @@ LL | Baz///xxxxxx
3830
|
3931
= help: doc comments must come before what they document, if a comment was intended use `//`
4032
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
41-
help: add a space before the last `/` to write a regular comment
33+
help: missing `,` before the doc comment
4234
|
43-
LL | Baz// /xxxxxx
44-
| +
35+
LL | Baz, ///xxxxxx
36+
| +
4537

4638
error: aborting due to 3 previous errors
4739

0 commit comments

Comments
 (0)