diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-06-20 18:10:02 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-06-20 18:48:42 +0300 |
| commit | 3f0729f37877d32565a6829327db84a84b27fc3b (patch) | |
| tree | e7c8f178efeacc1c9b6984fc0fb1f167ca27ab5e | |
| parent | 75d1500f028d0e5dcb3978b873bf2d0a0be9c75f (diff) | |
| download | rust-3f0729f37877d32565a6829327db84a84b27fc3b.tar.gz rust-3f0729f37877d32565a6829327db84a84b27fc3b.zip | |
expand: Move some more derive logic to `rustc_builtin_macros`
| -rw-r--r-- | compiler/rustc_builtin_macros/src/cfg_eval.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/derive.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/base.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/expand.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 2 |
5 files changed, 13 insertions, 38 deletions
diff --git a/compiler/rustc_builtin_macros/src/cfg_eval.rs b/compiler/rustc_builtin_macros/src/cfg_eval.rs index 79dc857074d..415822c4573 100644 --- a/compiler/rustc_builtin_macros/src/cfg_eval.rs +++ b/compiler/rustc_builtin_macros/src/cfg_eval.rs @@ -24,19 +24,18 @@ crate fn expand( annotatable: Annotatable, ) -> Vec<Annotatable> { check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval); - cfg_eval(ecx, annotatable) + vec![cfg_eval(ecx, annotatable)] } -crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> { - let mut visitor = CfgEval { +crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable { + CfgEval { cfg: &mut StripUnconfigured { sess: ecx.sess, features: ecx.ecfg.features, config_tokens: true, }, - }; - let annotatable = visitor.configure_annotatable(annotatable); - vec![annotatable] + } + .configure_annotatable(annotatable) } struct CfgEval<'a, 'b> { diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index 1bb050a40ce..60d6bae38b5 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -26,6 +26,8 @@ impl MultiItemModifier for Expander { return ExpandResult::Ready(vec![item]); } + let item = cfg_eval(ecx, item); + let result = ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| { let template = @@ -54,12 +56,12 @@ impl MultiItemModifier for Expander { report_path_args(sess, &meta); meta.path }) - .map(|path| (path, None)) + .map(|path| (path, item.clone(), None)) .collect() }); match result { - Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)), + Ok(()) => ExpandResult::Ready(vec![item]), Err(Indeterminate) => ExpandResult::Retry(item), } } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index aab2741c852..96fd6cb68e8 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -835,7 +835,7 @@ impl SyntaxExtension { /// Error type that denotes indeterminacy. pub struct Indeterminate; -pub type DeriveResolutions = Vec<(ast::Path, Option<Lrc<SyntaxExtension>>)>; +pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>)>; pub trait ResolverExpand { fn next_node_id(&mut self) -> NodeId; diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 39c0447bd09..234a49fbbf1 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -500,42 +500,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> { .resolver .take_derive_resolutions(expn_id) .map(|derives| { - enum AnnotatableRef<'a> { - Item(&'a P<ast::Item>), - Stmt(&'a ast::Stmt), - } - let item = match &fragment { - AstFragment::Items(items) => match &items[..] { - [item] => AnnotatableRef::Item(item), - _ => unreachable!(), - }, - AstFragment::Stmts(stmts) => match &stmts[..] { - [stmt] => AnnotatableRef::Stmt(stmt), - _ => unreachable!(), - }, - _ => unreachable!(), - }; - derive_invocations.reserve(derives.len()); derives .into_iter() - .map(|(path, _exts)| { + .map(|(path, item, _exts)| { // FIXME: Consider using the derive resolutions (`_exts`) // instead of enqueuing the derives to be resolved again later. let expn_id = ExpnId::fresh(None); derive_invocations.push(( Invocation { - kind: InvocationKind::Derive { - path, - item: match item { - AnnotatableRef::Item(item) => { - Annotatable::Item(item.clone()) - } - AnnotatableRef::Stmt(stmt) => { - Annotatable::Stmt(P(stmt.clone())) - } - }, - }, + kind: InvocationKind::Derive { path, item }, fragment_kind, expansion_data: ExpansionData { id: expn_id, diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 3f7db2b6962..38d052f988c 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -380,7 +380,7 @@ impl<'a> ResolverExpand for Resolver<'a> { has_derive_copy: false, }); let parent_scope = self.invocation_parent_scopes[&expn_id]; - for (i, (path, opt_ext)) in entry.resolutions.iter_mut().enumerate() { + for (i, (path, _, opt_ext)) in entry.resolutions.iter_mut().enumerate() { if opt_ext.is_none() { *opt_ext = Some( match self.resolve_macro_path( |
