Skip to content

Commit 5d27728

Browse files
committed
rustc_builtin_macros: Share some more logic between derive and cfg_eval
1 parent 10ed08f commit 5d27728

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

compiler/rustc_builtin_macros/src/cfg_eval.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,41 @@ crate fn expand(
1414
ecx: &mut ExtCtxt<'_>,
1515
_span: Span,
1616
meta_item: &ast::MetaItem,
17-
item: Annotatable,
17+
annotatable: Annotatable,
1818
) -> Vec<Annotatable> {
1919
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
20+
cfg_eval(ecx, annotatable)
21+
}
2022

23+
crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> {
2124
let mut visitor = CfgEval {
2225
cfg: StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, modified: false },
2326
};
24-
let mut item = visitor.fully_configure(item);
27+
let mut annotatable = visitor.configure_annotatable(annotatable);
2528
if visitor.cfg.modified {
2629
// Erase the tokens if cfg-stripping modified the item
2730
// This will cause us to synthesize fake tokens
2831
// when `nt_to_tokenstream` is called on this item.
29-
if let Some(tokens) = item.tokens_mut() {
32+
if let Some(tokens) = annotatable.tokens_mut() {
3033
*tokens = None;
3134
}
3235
}
33-
vec![item]
36+
vec![annotatable]
3437
}
3538

36-
crate struct CfgEval<'a> {
37-
pub cfg: StripUnconfigured<'a>,
39+
struct CfgEval<'a> {
40+
cfg: StripUnconfigured<'a>,
3841
}
3942

4043
impl CfgEval<'_> {
4144
fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
4245
self.cfg.configure(node)
4346
}
4447

45-
crate fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
48+
fn configure_annotatable(&mut self, annotatable: Annotatable) -> Annotatable {
4649
// Since the item itself has already been configured by the InvocationCollector,
4750
// we know that fold result vector will contain exactly one element
48-
match item {
51+
match annotatable {
4952
Annotatable::Item(item) => Annotatable::Item(self.flat_map_item(item).pop().unwrap()),
5053
Annotatable::TraitItem(item) => {
5154
Annotatable::TraitItem(self.flat_map_trait_item(item).pop().unwrap())

compiler/rustc_builtin_macros/src/derive.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use crate::cfg_eval::CfgEval;
1+
use crate::cfg_eval::cfg_eval;
22

3-
use rustc_ast::{self as ast, token, AstLike, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
3+
use rustc_ast::{self as ast, token, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
44
use rustc_errors::{struct_span_err, Applicability};
55
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
6-
use rustc_expand::config::StripUnconfigured;
76
use rustc_feature::AttributeTemplate;
87
use rustc_parse::validate_attr;
98
use rustc_session::Session;
@@ -53,21 +52,7 @@ impl MultiItemModifier for Expander {
5352

5453
// FIXME: Try to cache intermediate results to avoid collecting same paths multiple times.
5554
match ecx.resolver.resolve_derives(ecx.current_expansion.id, derives, ecx.force_mode) {
56-
Ok(()) => {
57-
let mut visitor = CfgEval {
58-
cfg: StripUnconfigured { sess, features: ecx.ecfg.features, modified: false },
59-
};
60-
let mut item = visitor.fully_configure(item);
61-
if visitor.cfg.modified {
62-
// Erase the tokens if cfg-stripping modified the item
63-
// This will cause us to synthesize fake tokens
64-
// when `nt_to_tokenstream` is called on this item.
65-
if let Some(tokens) = item.tokens_mut() {
66-
*tokens = None;
67-
}
68-
}
69-
ExpandResult::Ready(vec![item])
70-
}
55+
Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)),
7156
Err(Indeterminate) => ExpandResult::Retry(item),
7257
}
7358
}

0 commit comments

Comments
 (0)