about summary refs log tree commit diff
path: root/compiler/rustc_middle
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2020-12-27 17:57:17 +0100
committerCamille GILLOT <gillot.camille@gmail.com>2021-01-05 21:10:34 +0100
commit59f1ccd35ca57da4cef5740d61ecfe5ea52939d7 (patch)
tree62fcb17edec6522dd0f0ef840accba560268147e /compiler/rustc_middle
parent68ec33261183ffc21de96d6f825b02373e22e835 (diff)
downloadrust-59f1ccd35ca57da4cef5740d61ecfe5ea52939d7.tar.gz
rust-59f1ccd35ca57da4cef5740d61ecfe5ea52939d7.zip
Compute parent module when collecting hir::MacroDef.
Diffstat (limited to 'compiler/rustc_middle')
-rw-r--r--compiler/rustc_middle/src/hir/map/collector.rs23
1 files changed, 16 insertions, 7 deletions
diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs
index 82cfca4f171..872fcb0f581 100644
--- a/compiler/rustc_middle/src/hir/map/collector.rs
+++ b/compiler/rustc_middle/src/hir/map/collector.rs
@@ -529,13 +529,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
     }
 
     fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
-        self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
-            this.insert_with_hash(
-                macro_def.span,
-                macro_def.hir_id,
-                Node::MacroDef(macro_def),
-                hash,
-            );
+        // Exported macros are visited directly from the crate root,
+        // so they do not have `parent_node` set.
+        // Find the correct enclosing module from their DefKey.
+        let def_key = self.definitions.def_key(macro_def.hir_id.owner);
+        let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
+            self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
+        });
+        self.with_parent(parent, |this| {
+            this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
+                this.insert_with_hash(
+                    macro_def.span,
+                    macro_def.hir_id,
+                    Node::MacroDef(macro_def),
+                    hash,
+                );
+            })
         });
     }