rustc_builtin_macros: Share some more logic between derive and cfg_eval

This commit is contained in:
Vadim Petrochenkov 2021-03-07 01:11:21 +03:00
parent 10ed08f5b6
commit 5d27728141
2 changed files with 14 additions and 26 deletions

View file

@ -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),
}
}