Skip to content

Commit ef15235

Browse files
committed
parser: suggest quotes only for ident attribute values
When encountering code like: ``` #[cfg(key = ["a", "b"])] fn main() {} ``` the parser incorrectly suggests: ``` error: expected unsuffixed literal, found `[` --> ../pg/src/main.rs:1:13 | 1 | #[cfg(key = ["foo", "bar"])] | ^ | help: surround the identifier with quotation marks to parse it as a string | 1 | #[cfg(key =" "["foo", "bar"])] | + + error: aborting due to 1 previous error ``` This commit modifies the parser to check if an attribute value is an ident before suggesting.
1 parent c672773 commit ef15235

File tree

1 file changed

+4
-1
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+4
-1
lines changed

compiler/rustc_parse/src/parser/attr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ impl<'a> Parser<'a> {
412412
// Check for unquoted idents in meta items, e.g.: #[cfg(key = foo)]
413413
// `from_expansion()` ensures we don't suggest for cases such as
414414
// `#[cfg(feature = $expr)]` in macros
415-
if self.prev_token == token::Eq && !self.token.span.from_expansion() {
415+
if self.token.is_ident()
416+
&& self.prev_token == token::Eq
417+
&& !self.token.span.from_expansion()
418+
{
416419
let before = self.token.span.shrink_to_lo();
417420
while matches!(self.token.kind, token::Ident(..)) {
418421
self.bump();

0 commit comments

Comments
 (0)