Skip to content

Commit 1cd49c1

Browse files
Allow passing expr metavariable to cfg
1 parent ae12bc2 commit 1cd49c1

File tree

15 files changed

+131
-60
lines changed

15 files changed

+131
-60
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl MetaItem {
424424
_span,
425425
_spacing,
426426
Delimiter::Invisible(InvisibleOrigin::MetaVar(
427-
MetaVarKind::Meta { .. } | MetaVarKind::Path,
427+
MetaVarKind::Meta | MetaVarKind::Path,
428428
)),
429429
_stream,
430430
)) => {

compiler/rustc_ast/src/token.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ pub enum MetaVarKind {
6464
Ident,
6565
Lifetime,
6666
Literal,
67-
Meta {
68-
/// Will `AttrItem::meta` succeed on this, if reparsed?
69-
has_meta_form: bool,
70-
},
67+
Meta,
7168
Path,
7269
Vis,
7370
TT,
@@ -87,7 +84,7 @@ impl fmt::Display for MetaVarKind {
8784
MetaVarKind::Ident => sym::ident,
8885
MetaVarKind::Lifetime => sym::lifetime,
8986
MetaVarKind::Literal => sym::literal,
90-
MetaVarKind::Meta { .. } => sym::meta,
87+
MetaVarKind::Meta => sym::meta,
9188
MetaVarKind::Path => sym::path,
9289
MetaVarKind::Vis => sym::vis,
9390
MetaVarKind::TT => sym::tt,
@@ -693,7 +690,7 @@ impl Token {
693690
OpenInvisible(InvisibleOrigin::MetaVar(
694691
MetaVarKind::Expr { .. } |
695692
MetaVarKind::Literal |
696-
MetaVarKind::Meta { .. } |
693+
MetaVarKind::Meta |
697694
MetaVarKind::Pat(_) |
698695
MetaVarKind::Path |
699696
MetaVarKind::Ty { .. }

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,18 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
410410
}
411411

412412
fn parse_attr_item(&mut self) -> PResult<'sess, MetaItemParser<'static>> {
413-
if let Some(MetaVarKind::Meta { has_meta_form }) = self.parser.token.is_metavar_seq() {
414-
return if has_meta_form {
415-
let attr_item = self
416-
.parser
417-
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
418-
MetaItemListParserContext { parser: this, should_emit: self.should_emit }
419-
.parse_attr_item()
420-
})
421-
.unwrap();
422-
Ok(attr_item)
423-
} else {
424-
self.parser.unexpected_any()
425-
};
413+
if let Some(mv_kind @ (MetaVarKind::Meta | MetaVarKind::Expr { .. })) =
414+
self.parser.token.is_metavar_seq()
415+
{
416+
return self
417+
.parser
418+
.eat_metavar_seq(mv_kind, |this| {
419+
MetaItemListParserContext { parser: this, should_emit: self.should_emit }
420+
.parse_attr_item()
421+
})
422+
.ok_or_else(|| {
423+
self.parser.unexpected_any::<core::convert::Infallible>().unwrap_err()
424+
});
426425
}
427426

428427
let path = self.parser.parse_path(PathStyle::Mod)?;

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,7 @@ fn transcribe_pnr<'tx>(
481481
mk_delimited(ty.span, MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
482482
}
483483
ParseNtResult::Meta(attr_item) => {
484-
let has_meta_form = attr_item.meta_kind().is_some();
485-
mk_delimited(
486-
attr_item.span(),
487-
MetaVarKind::Meta { has_meta_form },
488-
TokenStream::from_ast(attr_item),
489-
)
484+
mk_delimited(attr_item.span(), MetaVarKind::Meta, TokenStream::from_ast(attr_item))
490485
}
491486
ParseNtResult::Path(path) => {
492487
mk_delimited(path.span, MetaVarKind::Path, TokenStream::from_ast(path))

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a> Parser<'a> {
290290
/// The delimiters or `=` are still put into the resulting token stream.
291291
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
292292
if let Some(item) = self.eat_metavar_seq_with_matcher(
293-
|mv_kind| matches!(mv_kind, MetaVarKind::Meta { .. }),
293+
|mv_kind| matches!(mv_kind, MetaVarKind::Meta),
294294
|this| this.parse_attr_item(force_collect),
295295
) {
296296
return Ok(item);
@@ -421,17 +421,13 @@ impl<'a> Parser<'a> {
421421
&mut self,
422422
unsafe_allowed: AllowLeadingUnsafe,
423423
) -> PResult<'a, ast::MetaItem> {
424-
if let Some(MetaVarKind::Meta { has_meta_form }) = self.token.is_metavar_seq() {
425-
return if has_meta_form {
426-
let attr_item = self
427-
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
428-
this.parse_attr_item(ForceCollect::No)
429-
})
430-
.unwrap();
431-
Ok(attr_item.meta(attr_item.path.span).unwrap())
432-
} else {
433-
self.unexpected_any()
434-
};
424+
if let Some(mv_kind @ (MetaVarKind::Meta | MetaVarKind::Expr { .. })) =
425+
self.token.is_metavar_seq()
426+
{
427+
return self
428+
.eat_metavar_seq(mv_kind, |this| this.parse_attr_item(ForceCollect::No))
429+
.map(|attr_item| attr_item.meta(attr_item.path.span).unwrap())
430+
.ok_or_else(|| self.unexpected_any::<core::convert::Infallible>().unwrap_err());
435431
}
436432

437433
let lo = self.token.span;

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'a> Parser<'a> {
2424
| MetaVarKind::Expr { .. }
2525
| MetaVarKind::Ty { .. }
2626
| MetaVarKind::Literal // `true`, `false`
27-
| MetaVarKind::Meta { .. }
27+
| MetaVarKind::Meta
2828
| MetaVarKind::Path => true,
2929

3030
MetaVarKind::Item
@@ -82,7 +82,7 @@ impl<'a> Parser<'a> {
8282
MetaVarKind::Item
8383
| MetaVarKind::Pat(_)
8484
| MetaVarKind::Ty { .. }
85-
| MetaVarKind::Meta { .. }
85+
| MetaVarKind::Meta
8686
| MetaVarKind::Path
8787
| MetaVarKind::Vis => false,
8888
MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {

tests/ui/attributes/nonterminal-expansion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
macro_rules! pass_nonterminal {
66
($n:expr) => {
77
#[repr(align($n))]
8-
//~^ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
9-
struct S;
8+
fn foo() {}
109
};
1110
}
1211

@@ -15,5 +14,6 @@ macro_rules! n {
1514
}
1615

1716
pass_nonterminal!(n!());
17+
//~^ ERROR expected one of `(`, `::`, or `=`, found `!`
1818

1919
fn main() {}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
2-
--> $DIR/nonterminal-expansion.rs:7:22
1+
error: expected one of `(`, `::`, or `=`, found `!`
2+
--> $DIR/nonterminal-expansion.rs:16:20
33
|
4-
LL | #[repr(align($n))]
5-
| ^^
6-
...
74
LL | pass_nonterminal!(n!());
8-
| ----------------------- in this macro invocation
9-
|
10-
= note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
5+
| ^ expected one of `(`, `::`, or `=`
116

127
error: aborting due to 1 previous error
138

tests/ui/macros/attr-expr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
macro_rules! foo {
2+
($e:expr) => {
3+
#[$e]
4+
//~^ ERROR expected identifier, found metavariable
5+
fn foo() {}
6+
}
7+
}
8+
9+
foo!(inline);
10+
11+
fn main() {}

tests/ui/macros/attr-expr.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected identifier, found metavariable
2+
--> $DIR/attr-expr.rs:3:11
3+
|
4+
LL | #[$e]
5+
| ^^ expected identifier, found metavariable
6+
...
7+
LL | foo!(inline);
8+
| ------------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)