From fffe9fbb5134da7ce77660dfc4562b5e395853e8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 23 Jun 2018 19:27:28 +0300 Subject: [PATCH] hygiene: More descriptive names for things involved in late hygienic name resolution --- src/librustc/hir/map/definitions.rs | 31 +++++++++++++++++------------ src/librustc/ty/mod.rs | 11 +++++----- src/librustc_resolve/macros.rs | 3 ++- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index b1cb9d7fbd4..b2365e22cc6 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -157,8 +157,13 @@ pub struct Definitions { node_to_def_index: NodeMap, def_index_to_node: [Vec; 2], pub(super) node_to_hir_id: IndexVec, - macro_def_scopes: FxHashMap, - expansions: FxHashMap, + /// If `Mark` 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, + /// Item with a given `DefIndex` was defined during opaque macro expansion with ID `Mark`. + /// It can actually be defined during transparent macro expansions inside that opaque expansion, + /// but transparent expansions are ignored here. + opaque_expansions_that_defined: FxHashMap, next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>, def_index_to_span: FxHashMap, } @@ -175,8 +180,8 @@ impl Clone for Definitions { self.def_index_to_node[1].clone(), ], node_to_hir_id: self.node_to_hir_id.clone(), - macro_def_scopes: self.macro_def_scopes.clone(), - expansions: self.expansions.clone(), + parent_modules_of_macro_defs: self.parent_modules_of_macro_defs.clone(), + opaque_expansions_that_defined: self.opaque_expansions_that_defined.clone(), next_disambiguator: self.next_disambiguator.clone(), def_index_to_span: self.def_index_to_span.clone(), } @@ -397,8 +402,8 @@ impl Definitions { node_to_def_index: NodeMap(), def_index_to_node: [vec![], vec![]], node_to_hir_id: IndexVec::new(), - macro_def_scopes: FxHashMap(), - expansions: FxHashMap(), + parent_modules_of_macro_defs: FxHashMap(), + opaque_expansions_that_defined: FxHashMap(), next_disambiguator: FxHashMap(), def_index_to_span: FxHashMap(), } @@ -580,7 +585,7 @@ impl Definitions { let expansion = expansion.modern(); if expansion != Mark::root() { - self.expansions.insert(index, expansion); + self.opaque_expansions_that_defined.insert(index, expansion); } // The span is added if it isn't DUMMY_SP @@ -600,16 +605,16 @@ impl Definitions { self.node_to_hir_id = mapping; } - pub fn expansion(&self, index: DefIndex) -> Mark { - self.expansions.get(&index).cloned().unwrap_or(Mark::root()) + pub fn opaque_expansion_that_defined(&self, index: DefIndex) -> Mark { + self.opaque_expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root()) } - pub fn macro_def_scope(&self, mark: Mark) -> DefId { - self.macro_def_scopes[&mark] + pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId { + self.parent_modules_of_macro_defs[&mark] } - pub fn add_macro_def_scope(&mut self, mark: Mark, scope: DefId) { - self.macro_def_scopes.insert(mark, scope); + pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) { + self.parent_modules_of_macro_defs.insert(mark, module); } } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 4f5f0c9d740..ce709831455 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2732,13 +2732,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) { - let expansion = match scope.krate { - LOCAL_CRATE => self.hir.definitions().expansion(scope.index), + ident = ident.modern(); + let target_expansion = match scope.krate { + LOCAL_CRATE => self.hir.definitions().opaque_expansion_that_defined(scope.index), _ => Mark::root(), }; - ident = ident.modern(); - let scope = match ident.span.adjust(expansion) { - Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def), + let scope = match ident.span.adjust(target_expansion) { + Some(actual_expansion) => + self.hir.definitions().parent_module_of_macro_def(actual_expansion), None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId None => self.hir.get_module_parent(block), }; diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 1c49e8f4753..ebdaa456170 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -327,7 +327,8 @@ impl<'a> base::Resolver for Resolver<'a> { self.macro_defs.insert(invoc.expansion_data.mark, def_id); let normal_module_def_id = self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id; - self.definitions.add_macro_def_scope(invoc.expansion_data.mark, normal_module_def_id); + self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark, + normal_module_def_id); self.unused_macros.remove(&def_id); let ext = self.get_macro(def);