Skip to content

Commit 59f1ccd

Browse files
committed
Compute parent module when collecting hir::MacroDef.
1 parent 68ec332 commit 59f1ccd

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

compiler/rustc_middle/src/hir/map/collector.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
529529
}
530530

531531
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
532-
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
533-
this.insert_with_hash(
534-
macro_def.span,
535-
macro_def.hir_id,
536-
Node::MacroDef(macro_def),
537-
hash,
538-
);
532+
// Exported macros are visited directly from the crate root,
533+
// so they do not have `parent_node` set.
534+
// Find the correct enclosing module from their DefKey.
535+
let def_key = self.definitions.def_key(macro_def.hir_id.owner);
536+
let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
537+
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
538+
});
539+
self.with_parent(parent, |this| {
540+
this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
541+
this.insert_with_hash(
542+
macro_def.span,
543+
macro_def.hir_id,
544+
Node::MacroDef(macro_def),
545+
hash,
546+
);
547+
})
539548
});
540549
}
541550

0 commit comments

Comments
 (0)