]> git.lizzy.rs Git - rust.git/commitdiff
Store macro parent module in ExpnData.
authorCamille GILLOT <gillot.camille@gmail.com>
Mon, 28 Jun 2021 17:29:55 +0000 (19:29 +0200)
committerCamille GILLOT <gillot.camille@gmail.com>
Tue, 6 Jul 2021 06:07:06 +0000 (08:07 +0200)
compiler/rustc_expand/src/base.rs
compiler/rustc_hir/src/definitions.rs
compiler/rustc_middle/src/ty/mod.rs
compiler/rustc_mir/src/transform/inline.rs
compiler/rustc_resolve/src/build_reduced_graph.rs
compiler/rustc_resolve/src/macros.rs
compiler/rustc_span/src/hygiene.rs

index 96fd6cb68e8b93419cc6d9a40b9d02327fc9cba3..67d71ce48ffa1e90dcd9c6ad71ef306f1a0462b0 100644 (file)
@@ -809,6 +809,7 @@ pub fn expn_data(
         call_site: Span,
         descr: Symbol,
         macro_def_id: Option<DefId>,
+        parent_module: Option<DefId>,
     ) -> ExpnData {
         use SyntaxExtensionKind::*;
         let proc_macro = match self.kind {
@@ -828,6 +829,7 @@ pub fn expn_data(
             self.local_inner_macros,
             self.edition,
             macro_def_id,
+            parent_module,
         )
     }
 }
index 753b8c85670ba5dd1e01f9f7ba6c5a57752149e9..325bec309af55205537aa45c7b24ab7846befecc 100644 (file)
@@ -5,9 +5,7 @@
 //! expressions) that are mostly just leftovers.
 
 pub use crate::def_id::DefPathHash;
-use crate::def_id::{
-    CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE,
-};
+use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
 use crate::hir;
 
 use rustc_data_structures::fx::FxHashMap;
@@ -108,9 +106,6 @@ pub struct Definitions {
     /// The reverse mapping of `def_id_to_hir_id`.
     pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
 
-    /// If `ExpnId` is an ID of some macro expansion,
-    /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
-    parent_modules_of_macro_defs: FxHashMap<ExpnId, DefId>,
     /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
     expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
 }
@@ -353,7 +348,6 @@ pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definit
             def_id_to_hir_id: Default::default(),
             hir_id_to_def_id: Default::default(),
             expansions_that_defined: Default::default(),
-            parent_modules_of_macro_defs: Default::default(),
         }
     }
 
@@ -420,14 +414,6 @@ pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
         self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
     }
 
-    pub fn parent_module_of_macro_def(&self, expn_id: ExpnId) -> DefId {
-        self.parent_modules_of_macro_defs[&expn_id]
-    }
-
-    pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) {
-        self.parent_modules_of_macro_defs.insert(expn_id, module);
-    }
-
     pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
         self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
     }
index f9dad42a4b7c5056cea6e78b2608561867ce8901..156e860e1a3fbc0ab1648f8384ed5cd0ef02a78b 100644 (file)
@@ -1902,13 +1902,11 @@ pub fn adjust_ident_and_get_scope(
         scope: DefId,
         block: hir::HirId,
     ) -> (Ident, DefId) {
-        let scope =
-            match ident.span.normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope)) {
-                Some(actual_expansion) => {
-                    self.hir().definitions().parent_module_of_macro_def(actual_expansion)
-                }
-                None => self.parent_module(block).to_def_id(),
-            };
+        let scope = ident
+            .span
+            .normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope))
+            .and_then(|actual_expansion| actual_expansion.expn_data().parent_module)
+            .unwrap_or_else(|| self.parent_module(block).to_def_id());
         (ident, scope)
     }
 
index f1c95a84ade85a04800a9af36440ba28429fbe6d..c8791ec227cdfdd2eeda3cacb3671fa467f8332b 100644 (file)
@@ -836,7 +836,7 @@ fn visit_source_scope(&mut self, scope: &mut SourceScope) {
 
     fn visit_span(&mut self, span: &mut Span) {
         let mut expn_data =
-            ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None);
+            ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None);
         expn_data.def_site = self.body_span;
         // Make sure that all spans track the fact that they were inlined.
         *span = self.callsite_span.fresh_expansion(expn_data);
index e10314a11fc1bbacd6e83a378f10aecc774fb686..f91bf0cbab727f45ad59489197d1871638f3f2bc 100644 (file)
@@ -159,6 +159,10 @@ pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
             Some(def_id) => def_id,
             None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
         };
+        self.macro_def_scope_from_def_id(def_id)
+    }
+
+    crate fn macro_def_scope_from_def_id(&mut self, def_id: DefId) -> Module<'a> {
         if let Some(id) = def_id.as_local() {
             self.local_macro_def_scopes[&id]
         } else {
index 38d052f988c63fefa192da601a8ef0507bf09d2c..1727586071d5f58601b09cad297faeca8593cc50 100644 (file)
@@ -20,7 +20,7 @@
 use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
 use rustc_feature::is_builtin_attr_name;
 use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
-use rustc_hir::def_id::{self, CrateNum};
+use rustc_hir::def_id::{CrateNum, LocalDefId};
 use rustc_hir::PrimTy;
 use rustc_middle::middle::stability;
 use rustc_middle::ty;
@@ -217,26 +217,20 @@ fn expansion_for_ast_pass(
         features: &[Symbol],
         parent_module_id: Option<NodeId>,
     ) -> ExpnId {
+        let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
         let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
             ExpnKind::AstPass(pass),
             call_site,
             self.session.edition(),
             features.into(),
             None,
+            parent_module.map(LocalDefId::to_def_id),
         )));
 
-        let parent_scope = if let Some(module_id) = parent_module_id {
-            let parent_def_id = self.local_def_id(module_id);
-            self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id.to_def_id());
-            self.module_map[&parent_def_id]
-        } else {
-            self.definitions.add_parent_module_of_macro_def(
-                expn_id,
-                def_id::DefId::local(def_id::CRATE_DEF_INDEX),
-            );
-            self.empty_module
-        };
+        let parent_scope = parent_module
+            .map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
         self.ast_transform_scopes.insert(expn_id, parent_scope);
+
         expn_id
     }
 
@@ -298,12 +292,12 @@ fn resolve_macro_invocation(
             span,
             fast_print_path(path),
             res.opt_def_id(),
+            res.opt_def_id().map(|macro_def_id| {
+                self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
+            }),
         ));
 
         if let Res::Def(_, _) = res {
-            let normal_module_def_id = self.macro_def_scope(invoc_id).nearest_parent_mod;
-            self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
-
             // Gate macro attributes in `#[derive]` output.
             if !self.session.features_untracked().macro_attributes_in_derive_output
                 && kind == MacroKind::Attr
index 23efaf6f4f33d861c5d59c5a4b86c2ac72bc3e1b..913aeeca78bebca2a2c9fdd9d96f55a7b1c558c8 100644 (file)
@@ -181,6 +181,7 @@ impl HygieneData {
             DUMMY_SP,
             edition,
             Some(DefId::local(CRATE_DEF_INDEX)),
+            None,
         );
         root_data.orig_id = Some(0);
 
@@ -687,7 +688,7 @@ pub fn mark_with_reason(
     ) -> Span {
         self.fresh_expansion(ExpnData {
             allow_internal_unstable,
-            ..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None)
+            ..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
         })
     }
 }
@@ -734,6 +735,8 @@ pub struct ExpnData {
     /// The `DefId` of the macro being invoked,
     /// if this `ExpnData` corresponds to a macro invocation
     pub macro_def_id: Option<DefId>,
+    /// The normal module (`mod`) in which the expanded macro was defined.
+    pub parent_module: Option<DefId>,
     /// The crate that originally created this `ExpnData`. During
     /// metadata serialization, we only encode `ExpnData`s that were
     /// created locally - when our serialized metadata is decoded,
@@ -777,6 +780,7 @@ pub fn new(
         local_inner_macros: bool,
         edition: Edition,
         macro_def_id: Option<DefId>,
+        parent_module: Option<DefId>,
     ) -> ExpnData {
         ExpnData {
             kind,
@@ -788,6 +792,7 @@ pub fn new(
             local_inner_macros,
             edition,
             macro_def_id,
+            parent_module,
             krate: LOCAL_CRATE,
             orig_id: None,
             disambiguator: 0,
@@ -800,6 +805,7 @@ pub fn default(
         call_site: Span,
         edition: Edition,
         macro_def_id: Option<DefId>,
+        parent_module: Option<DefId>,
     ) -> ExpnData {
         ExpnData {
             kind,
@@ -811,6 +817,7 @@ pub fn default(
             local_inner_macros: false,
             edition,
             macro_def_id,
+            parent_module,
             krate: LOCAL_CRATE,
             orig_id: None,
             disambiguator: 0,
@@ -823,10 +830,11 @@ pub fn allow_unstable(
         edition: Edition,
         allow_internal_unstable: Lrc<[Symbol]>,
         macro_def_id: Option<DefId>,
+        parent_module: Option<DefId>,
     ) -> ExpnData {
         ExpnData {
             allow_internal_unstable: Some(allow_internal_unstable),
-            ..ExpnData::default(kind, call_site, edition, macro_def_id)
+            ..ExpnData::default(kind, call_site, edition, macro_def_id, parent_module)
         }
     }