Skip to content

Commit

Permalink
Suggest quoting unquoted idents in attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
sjwang05 committed Dec 27, 2023
1 parent f8fe517 commit 72d44e3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
parse_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`
.suggestion = surround the identifier with quotation marks to parse it as a string
parse_invalid_unicode_escape = invalid unicode character escape
.label = invalid escape
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,17 @@ pub(crate) struct InvalidMetaItem {
#[primary_span]
pub span: Span,
pub token: Token,
#[subdiagnostic]
pub sugg: Option<InvalidMetaItemSuggQuoteIdent>,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
pub(crate) struct InvalidMetaItemSuggQuoteIdent {
#[suggestion_part(code = "\"")]
pub before: Span,
#[suggestion_part(code = "\"")]
pub after: Span,
}

#[derive(Subdiagnostic)]
Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
use crate::errors::{InvalidMetaItem, InvalidMetaItemSuggQuoteIdent, SuffixedLiteralInAttribute};
use crate::fluent_generated as fluent;

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

Err(self
.dcx()
.create_err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }))
let token = self.token.clone();
let sugg = if 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();
}
let after = self.prev_token.span.shrink_to_hi();
Some(InvalidMetaItemSuggQuoteIdent { before, after })
} else {
None
};

Err(self.dcx().create_err(InvalidMetaItem { span: token.span, token, sugg }))
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/ui/parser/attribute/attr-unquoted-ident.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile-flags: -Zdeduplicate-diagnostics=yes
// run-rustfix

fn main() {
#[cfg(key="foo")]
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
//~| HELP surround the identifier with quotation marks to parse it as a string
println!();
#[cfg(key="bar")]
println!();
#[cfg(key="foo bar baz")]
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
//~| HELP surround the identifier with quotation marks to parse it as a string
println!();
}
15 changes: 15 additions & 0 deletions tests/ui/parser/attribute/attr-unquoted-ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile-flags: -Zdeduplicate-diagnostics=yes
// run-rustfix

fn main() {
#[cfg(key=foo)]
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
//~| HELP surround the identifier with quotation marks to parse it as a string
println!();
#[cfg(key="bar")]
println!();
#[cfg(key=foo bar baz)]
//~^ ERROR expected unsuffixed literal or identifier, found `foo`
//~| HELP surround the identifier with quotation marks to parse it as a string
println!();
}
24 changes: 24 additions & 0 deletions tests/ui/parser/attribute/attr-unquoted-ident.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error: expected unsuffixed literal or identifier, found `foo`
--> $DIR/attr-unquoted-ident.rs:5:15
|
LL | #[cfg(key=foo)]
| ^^^
|
help: surround the identifier with quotation marks to parse it as a string
|
LL | #[cfg(key="foo")]
| + +

error: expected unsuffixed literal or identifier, found `foo`
--> $DIR/attr-unquoted-ident.rs:11:15
|
LL | #[cfg(key=foo bar baz)]
| ^^^
|
help: surround the identifier with quotation marks to parse it as a string
|
LL | #[cfg(key="foo bar baz")]
| + +

error: aborting due to 2 previous errors

0 comments on commit 72d44e3

Please sign in to comment.