Skip to content

Commit 856c691

Browse files
Allow passing expr metavariable to cfg
1 parent 03749d6 commit 856c691

15 files changed

Lines changed: 134 additions & 60 deletions

File tree

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl MetaItem {
521521
_span,
522522
_spacing,
523523
Delimiter::Invisible(InvisibleOrigin::MetaVar(
524-
MetaVarKind::Meta { .. } | MetaVarKind::Path,
524+
MetaVarKind::Meta | MetaVarKind::Path,
525525
)),
526526
_stream,
527527
)) => {

compiler/rustc_ast/src/token.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ pub enum MetaVarKind {
8888
Ident,
8989
Lifetime,
9090
Literal,
91-
Meta {
92-
/// Will `AttrItem::meta` succeed on this, if reparsed?
93-
has_meta_form: bool,
94-
},
91+
Meta,
9592
Path,
9693
Vis,
9794
TT,
@@ -111,7 +108,7 @@ impl fmt::Display for MetaVarKind {
111108
MetaVarKind::Ident => sym::ident,
112109
MetaVarKind::Lifetime => sym::lifetime,
113110
MetaVarKind::Literal => sym::literal,
114-
MetaVarKind::Meta { .. } => sym::meta,
111+
MetaVarKind::Meta => sym::meta,
115112
MetaVarKind::Path => sym::path,
116113
MetaVarKind::Vis => sym::vis,
117114
MetaVarKind::TT => sym::tt,
@@ -717,7 +714,7 @@ impl Token {
717714
OpenInvisible(InvisibleOrigin::MetaVar(
718715
MetaVarKind::Expr { .. } |
719716
MetaVarKind::Literal |
720-
MetaVarKind::Meta { .. } |
717+
MetaVarKind::Meta |
721718
MetaVarKind::Pat(_) |
722719
MetaVarKind::Path |
723720
MetaVarKind::Ty { .. }

compiler/rustc_attr_parsing/src/parser.rs

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

450450
fn parse_attr_item(&mut self) -> PResult<'sess, MetaItemParser> {
451-
if let Some(MetaVarKind::Meta { has_meta_form }) = self.parser.token.is_metavar_seq() {
452-
return if has_meta_form {
453-
let attr_item = self
454-
.parser
455-
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
456-
MetaItemListParserContext { parser: this, should_emit: self.should_emit }
457-
.parse_attr_item()
458-
})
459-
.unwrap();
460-
Ok(attr_item)
461-
} else {
462-
self.parser.unexpected_any()
463-
};
451+
if let Some(mv_kind @ (MetaVarKind::Meta | MetaVarKind::Expr { .. })) =
452+
self.parser.token.is_metavar_seq()
453+
{
454+
return self
455+
.parser
456+
.eat_metavar_seq(mv_kind, |this| {
457+
MetaItemListParserContext { parser: this, should_emit: self.should_emit }
458+
.parse_attr_item()
459+
})
460+
.ok_or_else(|| {
461+
self.parser.unexpected_any::<core::convert::Infallible>().unwrap_err()
462+
});
464463
}
465464

466465
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
@@ -543,12 +543,7 @@ fn transcribe_pnr<'tx>(
543543
mk_delimited(ty.span, MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
544544
}
545545
ParseNtResult::Meta(attr_item) => {
546-
let has_meta_form = attr_item.meta_kind().is_some();
547-
mk_delimited(
548-
attr_item.span(),
549-
MetaVarKind::Meta { has_meta_form },
550-
TokenStream::from_ast(attr_item),
551-
)
546+
mk_delimited(attr_item.span(), MetaVarKind::Meta, TokenStream::from_ast(attr_item))
552547
}
553548
ParseNtResult::Path(path) => {
554549
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
@@ -310,7 +310,7 @@ impl<'a> Parser<'a> {
310310
/// The delimiters or `=` are still put into the resulting token stream.
311311
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
312312
if let Some(item) = self.eat_metavar_seq_with_matcher(
313-
|mv_kind| matches!(mv_kind, MetaVarKind::Meta { .. }),
313+
|mv_kind| matches!(mv_kind, MetaVarKind::Meta),
314314
|this| this.parse_attr_item(force_collect),
315315
) {
316316
return Ok(item);
@@ -420,17 +420,13 @@ impl<'a> Parser<'a> {
420420
&mut self,
421421
unsafe_allowed: AllowLeadingUnsafe,
422422
) -> PResult<'a, ast::MetaItem> {
423-
if let Some(MetaVarKind::Meta { has_meta_form }) = self.token.is_metavar_seq() {
424-
return if has_meta_form {
425-
let attr_item = self
426-
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
427-
this.parse_attr_item(ForceCollect::No)
428-
})
429-
.unwrap();
430-
Ok(attr_item.meta(attr_item.path.span).unwrap())
431-
} else {
432-
self.unexpected_any()
433-
};
423+
if let Some(mv_kind @ (MetaVarKind::Meta | MetaVarKind::Expr { .. })) =
424+
self.token.is_metavar_seq()
425+
{
426+
return self
427+
.eat_metavar_seq(mv_kind, |this| this.parse_attr_item(ForceCollect::No))
428+
.map(|attr_item| attr_item.meta(attr_item.path.span).unwrap())
429+
.ok_or_else(|| self.unexpected_any::<core::convert::Infallible>().unwrap_err());
434430
}
435431

436432
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
@@ -26,7 +26,7 @@ impl<'a> Parser<'a> {
2626
| MetaVarKind::Expr { .. }
2727
| MetaVarKind::Ty { .. }
2828
| MetaVarKind::Literal // `true`, `false`
29-
| MetaVarKind::Meta { .. }
29+
| MetaVarKind::Meta
3030
| MetaVarKind::Path => true,
3131

3232
MetaVarKind::Item
@@ -84,7 +84,7 @@ impl<'a> Parser<'a> {
8484
MetaVarKind::Item
8585
| MetaVarKind::Pat(_)
8686
| MetaVarKind::Ty { .. }
87-
| MetaVarKind::Meta { .. }
87+
| MetaVarKind::Meta
8888
| MetaVarKind::Path
8989
| MetaVarKind::Vis => false,
9090
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)