about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2021-03-07 01:11:21 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2021-03-07 01:12:18 +0300
commit5d27728141beed69bf64212de1eb504b6b2ac0f0 (patch)
treee0eb6810a558aff35fb128ad9653c33336e74996
parent10ed08f5b644e1bd521eaf7d64474a837b17a7ae (diff)
downloadrust-5d27728141beed69bf64212de1eb504b6b2ac0f0.tar.gz
rust-5d27728141beed69bf64212de1eb504b6b2ac0f0.zip
rustc_builtin_macros: Share some more logic between `derive` and `cfg_eval`
-rw-r--r--compiler/rustc_builtin_macros/src/cfg_eval.rs19
-rw-r--r--compiler/rustc_builtin_macros/src/derive.rs21
2 files changed, 14 insertions, 26 deletions
diff --git a/compiler/rustc_builtin_macros/src/cfg_eval.rs b/compiler/rustc_builtin_macros/src/cfg_eval.rs
index efc5f5af877..eea4d785dee 100644
--- a/compiler/rustc_builtin_macros/src/cfg_eval.rs
+++ b/compiler/rustc_builtin_macros/src/cfg_eval.rs
@@ -14,27 +14,30 @@ crate fn expand(
     ecx: &mut ExtCtxt<'_>,
     _span: Span,
     meta_item: &ast::MetaItem,
-    item: Annotatable,
+    annotatable: Annotatable,
 ) -> Vec<Annotatable> {
     check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
+    cfg_eval(ecx, annotatable)
+}
 
+crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> {
     let mut visitor = CfgEval {
         cfg: StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, modified: false },
     };
-    let mut item = visitor.fully_configure(item);
+    let mut annotatable = visitor.configure_annotatable(annotatable);
     if visitor.cfg.modified {
         // Erase the tokens if cfg-stripping modified the item
         // This will cause us to synthesize fake tokens
         // when `nt_to_tokenstream` is called on this item.
-        if let Some(tokens) = item.tokens_mut() {
+        if let Some(tokens) = annotatable.tokens_mut() {
             *tokens = None;
         }
     }
-    vec![item]
+    vec![annotatable]
 }
 
-crate struct CfgEval<'a> {
-    pub cfg: StripUnconfigured<'a>,
+struct CfgEval<'a> {
+    cfg: StripUnconfigured<'a>,
 }
 
 impl CfgEval<'_> {
@@ -42,10 +45,10 @@ impl CfgEval<'_> {
         self.cfg.configure(node)
     }
 
-    crate fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
+    fn configure_annotatable(&mut self, annotatable: Annotatable) -> Annotatable {
         // Since the item itself has already been configured by the InvocationCollector,
         // we know that fold result vector will contain exactly one element
-        match item {
+        match annotatable {
             Annotatable::Item(item) => Annotatable::Item(self.flat_map_item(item).pop().unwrap()),
             Annotatable::TraitItem(item) => {
                 Annotatable::TraitItem(self.flat_map_trait_item(item).pop().unwrap())
diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs
index 48b1dddfdd1..0da2c1c1021 100644
--- a/compiler/rustc_builtin_macros/src/derive.rs
+++ b/compiler/rustc_builtin_macros/src/derive.rs
@@ -1,9 +1,8 @@
-use crate::cfg_eval::CfgEval;
+use crate::cfg_eval::cfg_eval;
 
-use rustc_ast::{self as ast, token, AstLike, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
+use rustc_ast::{self as ast, token, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
 use rustc_errors::{struct_span_err, Applicability};
 use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
-use rustc_expand::config::StripUnconfigured;
 use rustc_feature::AttributeTemplate;
 use rustc_parse::validate_attr;
 use rustc_session::Session;
@@ -53,21 +52,7 @@ impl MultiItemModifier for Expander {
 
         // FIXME: Try to cache intermediate results to avoid collecting same paths multiple times.
         match ecx.resolver.resolve_derives(ecx.current_expansion.id, derives, ecx.force_mode) {
-            Ok(()) => {
-                let mut visitor = CfgEval {
-                    cfg: StripUnconfigured { sess, features: ecx.ecfg.features, modified: false },
-                };
-                let mut item = visitor.fully_configure(item);
-                if visitor.cfg.modified {
-                    // Erase the tokens if cfg-stripping modified the item
-                    // This will cause us to synthesize fake tokens
-                    // when `nt_to_tokenstream` is called on this item.
-                    if let Some(tokens) = item.tokens_mut() {
-                        *tokens = None;
-                    }
-                }
-                ExpandResult::Ready(vec![item])
-            }
+            Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)),
             Err(Indeterminate) => ExpandResult::Retry(item),
         }
     }