Skip to content

Commit fffe9fb

Browse files
committed
hygiene: More descriptive names for things involved in late hygienic name resolution
1 parent 296955a commit fffe9fb

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ pub struct Definitions {
157157
node_to_def_index: NodeMap<DefIndex>,
158158
def_index_to_node: [Vec<ast::NodeId>; 2],
159159
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
160-
macro_def_scopes: FxHashMap<Mark, DefId>,
161-
expansions: FxHashMap<DefIndex, Mark>,
160+
/// If `Mark` is an ID of some macro expansion,
161+
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
162+
parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
163+
/// Item with a given `DefIndex` was defined during opaque macro expansion with ID `Mark`.
164+
/// It can actually be defined during transparent macro expansions inside that opaque expansion,
165+
/// but transparent expansions are ignored here.
166+
opaque_expansions_that_defined: FxHashMap<DefIndex, Mark>,
162167
next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
163168
def_index_to_span: FxHashMap<DefIndex, Span>,
164169
}
@@ -175,8 +180,8 @@ impl Clone for Definitions {
175180
self.def_index_to_node[1].clone(),
176181
],
177182
node_to_hir_id: self.node_to_hir_id.clone(),
178-
macro_def_scopes: self.macro_def_scopes.clone(),
179-
expansions: self.expansions.clone(),
183+
parent_modules_of_macro_defs: self.parent_modules_of_macro_defs.clone(),
184+
opaque_expansions_that_defined: self.opaque_expansions_that_defined.clone(),
180185
next_disambiguator: self.next_disambiguator.clone(),
181186
def_index_to_span: self.def_index_to_span.clone(),
182187
}
@@ -397,8 +402,8 @@ impl Definitions {
397402
node_to_def_index: NodeMap(),
398403
def_index_to_node: [vec![], vec![]],
399404
node_to_hir_id: IndexVec::new(),
400-
macro_def_scopes: FxHashMap(),
401-
expansions: FxHashMap(),
405+
parent_modules_of_macro_defs: FxHashMap(),
406+
opaque_expansions_that_defined: FxHashMap(),
402407
next_disambiguator: FxHashMap(),
403408
def_index_to_span: FxHashMap(),
404409
}
@@ -580,7 +585,7 @@ impl Definitions {
580585

581586
let expansion = expansion.modern();
582587
if expansion != Mark::root() {
583-
self.expansions.insert(index, expansion);
588+
self.opaque_expansions_that_defined.insert(index, expansion);
584589
}
585590

586591
// The span is added if it isn't DUMMY_SP
@@ -600,16 +605,16 @@ impl Definitions {
600605
self.node_to_hir_id = mapping;
601606
}
602607

603-
pub fn expansion(&self, index: DefIndex) -> Mark {
604-
self.expansions.get(&index).cloned().unwrap_or(Mark::root())
608+
pub fn opaque_expansion_that_defined(&self, index: DefIndex) -> Mark {
609+
self.opaque_expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
605610
}
606611

607-
pub fn macro_def_scope(&self, mark: Mark) -> DefId {
608-
self.macro_def_scopes[&mark]
612+
pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
613+
self.parent_modules_of_macro_defs[&mark]
609614
}
610615

611-
pub fn add_macro_def_scope(&mut self, mark: Mark, scope: DefId) {
612-
self.macro_def_scopes.insert(mark, scope);
616+
pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
617+
self.parent_modules_of_macro_defs.insert(mark, module);
613618
}
614619
}
615620

src/librustc/ty/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,13 +2732,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27322732
}
27332733

27342734
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) {
2735-
let expansion = match scope.krate {
2736-
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
2735+
ident = ident.modern();
2736+
let target_expansion = match scope.krate {
2737+
LOCAL_CRATE => self.hir.definitions().opaque_expansion_that_defined(scope.index),
27372738
_ => Mark::root(),
27382739
};
2739-
ident = ident.modern();
2740-
let scope = match ident.span.adjust(expansion) {
2741-
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
2740+
let scope = match ident.span.adjust(target_expansion) {
2741+
Some(actual_expansion) =>
2742+
self.hir.definitions().parent_module_of_macro_def(actual_expansion),
27422743
None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
27432744
None => self.hir.get_module_parent(block),
27442745
};

src/librustc_resolve/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ impl<'a> base::Resolver for Resolver<'a> {
327327
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
328328
let normal_module_def_id =
329329
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
330-
self.definitions.add_macro_def_scope(invoc.expansion_data.mark, normal_module_def_id);
330+
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
331+
normal_module_def_id);
331332

332333
self.unused_macros.remove(&def_id);
333334
let ext = self.get_macro(def);

0 commit comments

Comments
 (0)