1
Fork 0

Rewrite parse_meta_item.

It can't use `maybe_whole`, but it can match `maybe_whole` more closely.

Also add a test for a case that wasn't previously covered.
This commit is contained in:
Nicholas Nethercote 2024-03-20 06:47:59 +11:00
parent d919dbe370
commit 8ac16c6193
3 changed files with 44 additions and 11 deletions

View file

@ -362,22 +362,18 @@ impl<'a> Parser<'a> {
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ; /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
/// ``` /// ```
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> { pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
let nt_meta = match &self.token.kind { // We can't use `maybe_whole` here because it would bump in the `None`
token::Interpolated(nt) => match &nt.0 { // case, which we don't want.
token::NtMeta(e) => Some(e.clone()), if let token::Interpolated(nt) = &self.token.kind
_ => None, && let token::NtMeta(attr_item) = &nt.0
}, {
_ => None, match attr_item.meta(attr_item.path.span) {
};
if let Some(item) = nt_meta {
match item.meta(item.path.span) {
Some(meta) => { Some(meta) => {
self.bump(); self.bump();
return Ok(meta); return Ok(meta);
} }
None => self.unexpected()?, None => self.unexpected()?,
}; }
} }
let lo = self.token.span; let lo = self.token.span;

View file

@ -0,0 +1,12 @@
macro_rules! mac {
($attr_item: meta) => {
#[cfg($attr_item)]
//~^ ERROR expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
//~| ERROR expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
struct S;
}
}
mac!(an(arbitrary token stream));
fn main() {}

View file

@ -0,0 +1,25 @@
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
--> $DIR/attr-bad-meta-4.rs:3:15
|
LL | #[cfg($attr_item)]
| ^^^^^^^^^^
...
LL | mac!(an(arbitrary token stream));
| -------------------------------- in this macro invocation
|
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
--> $DIR/attr-bad-meta-4.rs:3:15
|
LL | #[cfg($attr_item)]
| ^^^^^^^^^^
...
LL | mac!(an(arbitrary token stream));
| -------------------------------- in this macro invocation
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors