Skip to content

Commit 72d44e3

Browse files
committed
Suggest quoting unquoted idents in attrs
1 parent f8fe517 commit 72d44e3

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

compiler/rustc_parse/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
407407
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
408408
409409
parse_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`
410+
.suggestion = surround the identifier with quotation marks to parse it as a string
410411
411412
parse_invalid_unicode_escape = invalid unicode character escape
412413
.label = invalid escape

compiler/rustc_parse/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,17 @@ pub(crate) struct InvalidMetaItem {
971971
#[primary_span]
972972
pub span: Span,
973973
pub token: Token,
974+
#[subdiagnostic]
975+
pub sugg: Option<InvalidMetaItemSuggQuoteIdent>,
976+
}
977+
978+
#[derive(Subdiagnostic)]
979+
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
980+
pub(crate) struct InvalidMetaItemSuggQuoteIdent {
981+
#[suggestion_part(code = "\"")]
982+
pub before: Span,
983+
#[suggestion_part(code = "\"")]
984+
pub after: Span,
974985
}
975986

976987
#[derive(Subdiagnostic)]

compiler/rustc_parse/src/parser/attr.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
1+
use crate::errors::{InvalidMetaItem, InvalidMetaItemSuggQuoteIdent, SuffixedLiteralInAttribute};
22
use crate::fluent_generated as fluent;
33

44
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
@@ -416,9 +416,19 @@ impl<'a> Parser<'a> {
416416
Err(err) => err.cancel(),
417417
}
418418

419-
Err(self
420-
.dcx()
421-
.create_err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }))
419+
let token = self.token.clone();
420+
let sugg = if self.prev_token == token::Eq && !self.token.span.from_expansion() {
421+
let before = self.token.span.shrink_to_lo();
422+
while matches!(self.token.kind, token::Ident(..)) {
423+
self.bump();
424+
}
425+
let after = self.prev_token.span.shrink_to_hi();
426+
Some(InvalidMetaItemSuggQuoteIdent { before, after })
427+
} else {
428+
None
429+
};
430+
431+
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, token, sugg }))
422432
}
423433
}
424434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile-flags: -Zdeduplicate-diagnostics=yes
2+
// run-rustfix
3+
4+
fn main() {
5+
#[cfg(key="foo")]
6+
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
7+
//~| HELP surround the identifier with quotation marks to parse it as a string
8+
println!();
9+
#[cfg(key="bar")]
10+
println!();
11+
#[cfg(key="foo bar baz")]
12+
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
13+
//~| HELP surround the identifier with quotation marks to parse it as a string
14+
println!();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile-flags: -Zdeduplicate-diagnostics=yes
2+
// run-rustfix
3+
4+
fn main() {
5+
#[cfg(key=foo)]
6+
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
7+
//~| HELP surround the identifier with quotation marks to parse it as a string
8+
println!();
9+
#[cfg(key="bar")]
10+
println!();
11+
#[cfg(key=foo bar baz)]
12+
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
13+
//~| HELP surround the identifier with quotation marks to parse it as a string
14+
println!();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: expected unsuffixed literal or identifier, found `foo`
2+
--> $DIR/attr-unquoted-ident.rs:5:15
3+
|
4+
LL | #[cfg(key=foo)]
5+
| ^^^
6+
|
7+
help: surround the identifier with quotation marks to parse it as a string
8+
|
9+
LL | #[cfg(key="foo")]
10+
| + +
11+
12+
error: expected unsuffixed literal or identifier, found `foo`
13+
--> $DIR/attr-unquoted-ident.rs:11:15
14+
|
15+
LL | #[cfg(key=foo bar baz)]
16+
| ^^^
17+
|
18+
help: surround the identifier with quotation marks to parse it as a string
19+
|
20+
LL | #[cfg(key="foo bar baz")]
21+
| + +
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)