Skip to content

Commit

Permalink
parser: suggest quotes only for ident attribute values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ohno418 committed Apr 29, 2024
1 parent c672773 commit ef15235
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ impl<'a> Parser<'a> {
// Check for unquoted idents in meta items, e.g.: #[cfg(key = foo)]
// `from_expansion()` ensures we don't suggest for cases such as
// `#[cfg(feature = $expr)]` in macros
if self.prev_token == token::Eq && !self.token.span.from_expansion() {
if self.token.is_ident()
&& self.prev_token == token::Eq
&& !self.token.span.from_expansion()
{
let before = self.token.span.shrink_to_lo();
while matches!(self.token.kind, token::Ident(..)) {
self.bump();
Expand Down

0 comments on commit ef15235

Please sign in to comment.