From d277904582d530feb4701dc76508fdf473cce8f4 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Fri, 22 May 2020 16:57:25 -0400 Subject: [PATCH] Remove `macro_defs` map We store store the `DefId` directly in `ExpnData`. This will allow us to serialize `ExpnData` in PR #72121 without needing to manage a side table. --- src/librustc_ast_lowering/lib.rs | 2 +- src/librustc_expand/base.rs | 10 +++++++++- src/librustc_expand/expand.rs | 1 + src/librustc_resolve/build_reduced_graph.rs | 4 ++-- src/librustc_resolve/lib.rs | 11 +++-------- src/librustc_resolve/macros.rs | 13 ++++++++---- src/librustc_span/hygiene.rs | 22 ++++++++++++++++++--- 7 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/librustc_ast_lowering/lib.rs b/src/librustc_ast_lowering/lib.rs index 2cf81af04166..c67829f7a08e 100644 --- a/src/librustc_ast_lowering/lib.rs +++ b/src/librustc_ast_lowering/lib.rs @@ -688,7 +688,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ) -> Span { span.fresh_expansion(ExpnData { allow_internal_unstable, - ..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition()) + ..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition(), None) }) } diff --git a/src/librustc_expand/base.rs b/src/librustc_expand/base.rs index fe5bf6f82c6d..0137080938fd 100644 --- a/src/librustc_expand/base.rs +++ b/src/librustc_expand/base.rs @@ -13,6 +13,7 @@ use rustc_data_structures::sync::{self, Lrc}; use rustc_errors::{DiagnosticBuilder, ErrorReported}; use rustc_parse::{self, parser, MACRO_ARGUMENTS}; use rustc_session::parse::ParseSess; +use rustc_span::def_id::DefId; use rustc_span::edition::Edition; use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind}; use rustc_span::source_map::SourceMap; @@ -857,7 +858,13 @@ impl SyntaxExtension { SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr { mark_used }, edition) } - pub fn expn_data(&self, parent: ExpnId, call_site: Span, descr: Symbol) -> ExpnData { + pub fn expn_data( + &self, + parent: ExpnId, + call_site: Span, + descr: Symbol, + macro_def_id: Option, + ) -> ExpnData { ExpnData { kind: ExpnKind::Macro(self.macro_kind(), descr), parent, @@ -867,6 +874,7 @@ impl SyntaxExtension { allow_internal_unsafe: self.allow_internal_unsafe, local_inner_macros: self.local_inner_macros, edition: self.edition, + macro_def_id, } } } diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs index 485c5147d2c2..b505302f6250 100644 --- a/src/librustc_expand/expand.rs +++ b/src/librustc_expand/expand.rs @@ -988,6 +988,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { ExpnKind::Macro(MacroKind::Attr, sym::derive), item.span(), self.cx.parse_sess.edition, + None, ) }), _ => None, diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 988ec3d4374e..c32b823fe73b 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -126,8 +126,8 @@ impl<'a> Resolver<'a> { } crate fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> { - let def_id = match self.macro_defs.get(&expn_id) { - Some(def_id) => *def_id, + let def_id = match expn_id.expn_data().macro_def_id { + Some(def_id) => def_id, None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root), }; if let Some(id) = self.definitions.as_local_node_id(def_id) { diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index bfb7f081fc33..34368a07071f 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -922,7 +922,6 @@ pub struct Resolver<'a> { dummy_ext_bang: Lrc, dummy_ext_derive: Lrc, non_macro_attrs: [Lrc; 2], - macro_defs: FxHashMap, local_macro_def_scopes: FxHashMap>, ast_transform_scopes: FxHashMap>, unused_macros: NodeMap, @@ -1152,9 +1151,6 @@ impl<'a> Resolver<'a> { let mut invocation_parent_scopes = FxHashMap::default(); invocation_parent_scopes.insert(ExpnId::root(), ParentScope::module(graph_root)); - let mut macro_defs = FxHashMap::default(); - macro_defs.insert(ExpnId::root(), root_def_id); - let features = session.features_untracked(); let non_macro_attr = |mark_used| Lrc::new(SyntaxExtension::non_macro_attr(mark_used, session.edition())); @@ -1229,7 +1225,6 @@ impl<'a> Resolver<'a> { invocation_parent_scopes, output_macro_rules_scopes: Default::default(), helper_attrs: Default::default(), - macro_defs, local_macro_def_scopes: FxHashMap::default(), name_already_seen: FxHashMap::default(), potentially_unused_imports: Vec::new(), @@ -1335,8 +1330,8 @@ impl<'a> Resolver<'a> { fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId { loop { - match self.macro_defs.get(&ctxt.outer_expn()) { - Some(&def_id) => return def_id, + match ctxt.outer_expn().expn_data().macro_def_id { + Some(def_id) => return def_id, None => ctxt.remove_mark(), }; } @@ -1820,7 +1815,7 @@ impl<'a> Resolver<'a> { && module.expansion.is_descendant_of(parent.expansion) { // The macro is a proc macro derive - if let Some(&def_id) = self.macro_defs.get(&module.expansion) { + if let Some(def_id) = module.expansion.expn_data().macro_def_id { if let Some(ext) = self.get_macro_by_def_id(def_id) { if !ext.is_builtin && ext.macro_kind() == MacroKind::Derive { if parent.expansion.outer_expn_is_descendant_of(span.ctxt()) { diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 1b6268dc8cbc..7027c8262678 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -186,6 +186,7 @@ impl<'a> base::Resolver for Resolver<'a> { call_site, self.session.edition(), features.into(), + None, ))); let parent_scope = if let Some(module_id) = parent_module_id { @@ -290,13 +291,17 @@ impl<'a> base::Resolver for Resolver<'a> { let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?; let span = invoc.span(); - invoc_id.set_expn_data(ext.expn_data(parent_scope.expansion, span, fast_print_path(path))); - - if let Res::Def(_, def_id) = res { + invoc_id.set_expn_data(ext.expn_data( + parent_scope.expansion, + span, + fast_print_path(path), + res.opt_def_id(), + )); + + if let Res::Def(_, _) = res { if after_derive { self.session.span_err(span, "macro attributes must be placed before `#[derive]`"); } - self.macro_defs.insert(invoc_id, def_id); let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id; self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id); } diff --git a/src/librustc_span/hygiene.rs b/src/librustc_span/hygiene.rs index 23c3dccb130f..c0fb84e741f4 100644 --- a/src/librustc_span/hygiene.rs +++ b/src/librustc_span/hygiene.rs @@ -25,6 +25,7 @@ // because getting it wrong can lead to nested `HygieneData::with` calls that // trigger runtime aborts. (Fortunately these are obvious and easy to fix.) +use crate::def_id::{DefId, CRATE_DEF_INDEX}; use crate::edition::Edition; use crate::symbol::{kw, sym, Symbol}; use crate::GLOBALS; @@ -155,7 +156,12 @@ crate struct HygieneData { impl HygieneData { crate fn new(edition: Edition) -> Self { HygieneData { - expn_data: vec![Some(ExpnData::default(ExpnKind::Root, DUMMY_SP, edition))], + expn_data: vec![Some(ExpnData::default( + ExpnKind::Root, + DUMMY_SP, + edition, + Some(DefId::local(CRATE_DEF_INDEX)), + ))], syntax_context_data: vec![SyntaxContextData { outer_expn: ExpnId::root(), outer_transparency: Transparency::Opaque, @@ -673,11 +679,19 @@ pub struct ExpnData { pub local_inner_macros: bool, /// Edition of the crate in which the macro is defined. pub edition: Edition, + /// The `DefId` of the macro being invoked, + /// if this `ExpnData` corresponds to a macro invocation + pub macro_def_id: Option, } impl ExpnData { /// Constructs expansion data with default properties. - pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnData { + pub fn default( + kind: ExpnKind, + call_site: Span, + edition: Edition, + macro_def_id: Option, + ) -> ExpnData { ExpnData { kind, parent: ExpnId::root(), @@ -687,6 +701,7 @@ impl ExpnData { allow_internal_unsafe: false, local_inner_macros: false, edition, + macro_def_id, } } @@ -695,10 +710,11 @@ impl ExpnData { call_site: Span, edition: Edition, allow_internal_unstable: Lrc<[Symbol]>, + macro_def_id: Option, ) -> ExpnData { ExpnData { allow_internal_unstable: Some(allow_internal_unstable), - ..ExpnData::default(kind, call_site, edition) + ..ExpnData::default(kind, call_site, edition, macro_def_id) } }