Skip to content

Commit 41601ef

Browse files
committed
Auto merge of #80415 - cjgillot:issue-77828, r=petrochenkov
Compute parent module when collecting hir::MacroDef. Fixes #77828. r? `@jyn514`
2 parents da305a2 + 4fb1236 commit 41601ef

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Entry<'hir> {
3131
impl<'hir> Entry<'hir> {
3232
fn parent_node(self) -> Option<HirId> {
3333
match self.node {
34-
Node::Crate(_) | Node::MacroDef(_) => None,
34+
Node::Crate(_) => None,
3535
_ => Some(self.parent),
3636
}
3737
}

src/test/rustdoc/macros.rs

+14
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ macro_rules! my_macro {
88
($a:tt) => ();
99
($e:expr) => {};
1010
}
11+
12+
// Check that exported macro defined in a module are shown at crate root.
13+
// @has macros/macro.my_sub_macro.html //pre 'macro_rules! my_sub_macro {'
14+
// @has - //pre '() => { ... };'
15+
// @has - //pre '($a:tt) => { ... };'
16+
// @has - //pre '($e:expr) => { ... };'
17+
mod sub {
18+
#[macro_export]
19+
macro_rules! my_sub_macro {
20+
() => {};
21+
($a:tt) => {};
22+
($e:expr) => {};
23+
}
24+
}

0 commit comments

Comments
 (0)