Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8ac16c6

Browse files
committed
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.
1 parent d919dbe commit 8ac16c6

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,18 @@ impl<'a> Parser<'a> {
362362
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
363363
/// ```
364364
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
365-
let nt_meta = match &self.token.kind {
366-
token::Interpolated(nt) => match &nt.0 {
367-
token::NtMeta(e) => Some(e.clone()),
368-
_ => None,
369-
},
370-
_ => None,
371-
};
372-
373-
if let Some(item) = nt_meta {
374-
match item.meta(item.path.span) {
365+
// We can't use `maybe_whole` here because it would bump in the `None`
366+
// case, which we don't want.
367+
if let token::Interpolated(nt) = &self.token.kind
368+
&& let token::NtMeta(attr_item) = &nt.0
369+
{
370+
match attr_item.meta(attr_item.path.span) {
375371
Some(meta) => {
376372
self.bump();
377373
return Ok(meta);
378374
}
379375
None => self.unexpected()?,
380-
};
376+
}
381377
}
382378

383379
let lo = self.token.span;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
macro_rules! mac {
2+
($attr_item: meta) => {
3+
#[cfg($attr_item)]
4+
//~^ ERROR expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
5+
//~| ERROR expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
6+
struct S;
7+
}
8+
}
9+
10+
mac!(an(arbitrary token stream));
11+
12+
fn main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
2+
--> $DIR/attr-bad-meta-4.rs:3:15
3+
|
4+
LL | #[cfg($attr_item)]
5+
| ^^^^^^^^^^
6+
...
7+
LL | mac!(an(arbitrary token stream));
8+
| -------------------------------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
13+
--> $DIR/attr-bad-meta-4.rs:3:15
14+
|
15+
LL | #[cfg($attr_item)]
16+
| ^^^^^^^^^^
17+
...
18+
LL | mac!(an(arbitrary token stream));
19+
| -------------------------------- in this macro invocation
20+
|
21+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
22+
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)