Skip to content

Commit 0b6b505

Browse files
Rollup merge of #157592 - ariagivens:suggest-comma-multiple, r=JonathanBrouwer
Suggest comma multiple Following from #157545 If there are multiple missing comma's, suggest fixing all of them in one step to avoid error cascade. For example: ``` error: attribute items not separated with `,` | LL | name = "name" | ^ help: try adding `,` here ``` followed by ``` error: attribute items not separated with `,` | LL | kind = "static" | ^ help: try adding `,` here ``` after adding the comma. Now this becomes one error: ``` error: attribute items not separated with `,` --> $DIR/attr-missing-comma.rs:10:18 | LL | name = "name" | ^ | help: try adding `,` here | LL | name = "name", | + help: try adding `,` here | LL | kind = "static", | + ```
2 parents 87749ce + c865af6 commit 0b6b505

4 files changed

Lines changed: 71 additions & 10 deletions

File tree

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use thin_vec::ThinVec;
3030

3131
use crate::ShouldEmit;
3232
use crate::session_diagnostics::{
33-
ExpectedComma, InvalidMetaItem, InvalidMetaItemQuoteIdentSugg, InvalidMetaItemRemoveNegSugg,
34-
MetaBadDelim, MetaBadDelimSugg, SuffixedLiteralInAttribute,
33+
AdditionalCommaSuggestion, ExpectedComma, InvalidMetaItem, InvalidMetaItemQuoteIdentSugg,
34+
InvalidMetaItemRemoveNegSugg, MetaBadDelim, MetaBadDelimSugg, SuffixedLiteralInAttribute,
3535
};
3636

3737
#[derive(Clone, Debug)]
@@ -713,15 +713,30 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
713713

714714
let mut snapshot = self.parser.create_snapshot_for_diagnostic();
715715
if matches!(self.should_emit, ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }) {
716-
let span = self.parser.prev_token.span.shrink_to_hi();
717-
self.should_emit = ShouldEmit::Nothing;
718-
match self.parse_meta_item_inner() {
719-
Ok(_) => {
720-
return Err(self.parser.dcx().create_err(ExpectedComma { span }));
721-
}
722-
Err(e) => {
723-
e.cancel();
716+
let mut missing_commas = ThinVec::new();
717+
let mut found_comma = false;
718+
while self.parser.token != token::Eof {
719+
let span = self.parser.prev_token.span.shrink_to_hi();
720+
self.should_emit = ShouldEmit::Nothing;
721+
match self.parse_meta_item_inner() {
722+
Ok(_) => {
723+
if !found_comma {
724+
missing_commas.push(span);
725+
}
726+
}
727+
Err(e) => {
728+
e.cancel();
729+
break;
730+
}
724731
}
732+
found_comma = self.parser.eat(exp!(Comma));
733+
}
734+
735+
let mut missing_commas = missing_commas.into_iter();
736+
if let Some(span) = missing_commas.next() {
737+
let additional =
738+
missing_commas.map(|span| AdditionalCommaSuggestion { span }).collect();
739+
return Err(self.parser.dcx().create_err(ExpectedComma { span, additional }));
725740
}
726741
}
727742
snapshot.unexpected_any()

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,4 +1044,13 @@ pub(crate) struct ExpectedComma {
10441044
style = "short"
10451045
)]
10461046
pub span: Span,
1047+
#[subdiagnostic]
1048+
pub additional: Vec<AdditionalCommaSuggestion>,
1049+
}
1050+
1051+
#[derive(Subdiagnostic)]
1052+
#[suggestion("try adding `,` here", code = ",", applicability = "maybe-incorrect", style = "short")]
1053+
pub(crate) struct AdditionalCommaSuggestion {
1054+
#[primary_span]
1055+
pub span: Span,
10471056
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {}
2+
3+
#[deprecated(
4+
since = "since" //~ ERROR attribute items not separated with `,`
5+
note = "note"
6+
)]
7+
fn f0() {}
8+
9+
#[link(
10+
name = "name" //~ ERROR attribute items not separated with `,`
11+
kind = "static"
12+
modifiers = "modifiers"
13+
)]
14+
fn f1() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: attribute items not separated with `,`
2+
--> $DIR/attr-missing-comma.rs:4:20
3+
|
4+
LL | since = "since"
5+
| ^ help: try adding `,` here
6+
7+
error: attribute items not separated with `,`
8+
--> $DIR/attr-missing-comma.rs:10:18
9+
|
10+
LL | name = "name"
11+
| ^
12+
|
13+
help: try adding `,` here
14+
|
15+
LL | name = "name",
16+
| +
17+
help: try adding `,` here
18+
|
19+
LL | kind = "static",
20+
| +
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)