Skip to content

Commit b224261

Browse files
committed
[experiment] Eagerly expand cfg and cfg_attr in all attribute inputs
1 parent ca40d7d commit b224261

File tree

7 files changed

+27
-691
lines changed

7 files changed

+27
-691
lines changed

compiler/rustc_builtin_macros/src/cfg_eval.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::util::check_builtin_macro_attribute;
22

33
use rustc_ast as ast;
44
use rustc_expand::base::{Annotatable, ExtCtxt};
5-
use rustc_expand::config::cfg_eval;
65
use rustc_span::symbol::sym;
76
use rustc_span::Span;
87

@@ -13,5 +12,5 @@ crate fn expand(
1312
annotatable: Annotatable,
1413
) -> Vec<Annotatable> {
1514
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
16-
cfg_eval(ecx, annotatable)
15+
vec![annotatable]
1716
}

compiler/rustc_builtin_macros/src/derive.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_ast::{self as ast, attr, token, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
22
use rustc_errors::{struct_span_err, Applicability};
33
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
4-
use rustc_expand::config::cfg_eval;
54
use rustc_feature::AttributeTemplate;
65
use rustc_parse::validate_attr;
76
use rustc_session::Session;
@@ -58,7 +57,7 @@ impl MultiItemModifier for Expander {
5857
});
5958

6059
match result {
61-
Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)),
60+
Ok(()) => ExpandResult::Ready(vec![item]),
6261
Err(Indeterminate) => ExpandResult::Retry(item),
6362
}
6463
}

compiler/rustc_expand/src/config.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,15 @@ fn is_cfg(sess: &Session, attr: &Attribute) -> bool {
547547
sess.check_name(attr, sym::cfg)
548548
}
549549

550-
pub fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> {
550+
pub(crate) fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
551551
let mut visitor = CfgEval {
552552
cfg: &mut StripUnconfigured {
553553
sess: ecx.sess,
554554
features: ecx.ecfg.features,
555555
config_tokens: true,
556556
},
557557
};
558-
let annotatable = visitor.configure_annotatable(annotatable);
559-
vec![annotatable]
558+
visitor.configure_annotatable(annotatable)
560559
}
561560

562561
struct CfgEval<'a, 'b> {

compiler/rustc_expand/src/expand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::base::*;
2-
use crate::config::StripUnconfigured;
2+
use crate::config::{cfg_eval, StripUnconfigured};
33
use crate::configure;
44
use crate::hygiene::SyntaxContext;
55
use crate::mbe::macro_rules::annotate_err_with_kind;
@@ -731,6 +731,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
731731
(item_inner.ident.name.is_empty() || !matches!(mod_kind, ast::ModKind::Loaded(_, Inline::Yes, _)));
732732
}
733733
}
734+
let item = cfg_eval(self.cx, item);
734735
let tokens = if fake_tokens {
735736
rustc_parse::fake_token_stream(
736737
&self.cx.sess.parse_sess,
@@ -753,6 +754,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
753754
SyntaxExtensionKind::LegacyAttr(expander) => {
754755
match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
755756
Ok(meta) => {
757+
let item = cfg_eval(self.cx, item);
756758
let items = match expander.expand(self.cx, span, &meta, item) {
757759
ExpandResult::Ready(items) => items,
758760
ExpandResult::Retry(item) => {

src/test/ui/proc-macro/attribute-after-derive.stdout

+2-46
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field : u8, }
1+
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { }
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Punct {
44
ch: '#',
@@ -35,51 +35,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
3535
},
3636
Group {
3737
delimiter: Brace,
38-
stream: TokenStream [
39-
Punct {
40-
ch: '#',
41-
spacing: Alone,
42-
span: $DIR/attribute-after-derive.rs:19:5: 19:6 (#0),
43-
},
44-
Group {
45-
delimiter: Bracket,
46-
stream: TokenStream [
47-
Ident {
48-
ident: "cfg",
49-
span: $DIR/attribute-after-derive.rs:19:7: 19:10 (#0),
50-
},
51-
Group {
52-
delimiter: Parenthesis,
53-
stream: TokenStream [
54-
Ident {
55-
ident: "FALSE",
56-
span: $DIR/attribute-after-derive.rs:19:11: 19:16 (#0),
57-
},
58-
],
59-
span: $DIR/attribute-after-derive.rs:19:10: 19:17 (#0),
60-
},
61-
],
62-
span: $DIR/attribute-after-derive.rs:19:6: 19:18 (#0),
63-
},
64-
Ident {
65-
ident: "field",
66-
span: $DIR/attribute-after-derive.rs:20:5: 20:10 (#0),
67-
},
68-
Punct {
69-
ch: ':',
70-
spacing: Alone,
71-
span: $DIR/attribute-after-derive.rs:20:10: 20:11 (#0),
72-
},
73-
Ident {
74-
ident: "u8",
75-
span: $DIR/attribute-after-derive.rs:20:12: 20:14 (#0),
76-
},
77-
Punct {
78-
ch: ',',
79-
spacing: Alone,
80-
span: $DIR/attribute-after-derive.rs:20:14: 20:15 (#0),
81-
},
82-
],
38+
stream: TokenStream [],
8339
span: $DIR/attribute-after-derive.rs:18:24: 21:2 (#0),
8440
},
8541
]

src/test/ui/proc-macro/auxiliary/attr-cfg.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use proc_macro::TokenStream;
1111
pub fn attr_cfg(args: TokenStream, input: TokenStream) -> TokenStream {
1212
let input_str = input.to_string();
1313

14-
assert_eq!(input_str, "fn outer() -> u8
15-
{
16-
#[cfg(foo)] fn inner() -> u8 { 1 } #[cfg(bar)] fn inner() -> u8 { 2 }
17-
inner()
18-
}");
14+
assert!(
15+
input_str == "fn outer() -> u8 { #[cfg(foo)] fn inner() -> u8 { 1 } inner() }"
16+
|| input_str == "fn outer() -> u8 { #[cfg(bar)] fn inner() -> u8 { 2 } inner() }"
17+
);
1918

2019
input
2120
}

0 commit comments

Comments
 (0)