Auto merge of #80415 - cjgillot:issue-77828, r=petrochenkov

Compute parent module when collecting hir::MacroDef.

Fixes #77828.

r? `@jyn514`
This commit is contained in:
bors 2021-01-06 00:36:14 +00:00
commit 41601ef394
3 changed files with 31 additions and 8 deletions

View File

@ -529,13 +529,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
} }
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'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| { // Exported macros are visited directly from the crate root,
this.insert_with_hash( // so they do not have `parent_node` set.
macro_def.span, // Find the correct enclosing module from their DefKey.
macro_def.hir_id, let def_key = self.definitions.def_key(macro_def.hir_id.owner);
Node::MacroDef(macro_def), let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
hash, 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,
);
})
}); });
} }

View File

@ -31,7 +31,7 @@ pub struct Entry<'hir> {
impl<'hir> Entry<'hir> { impl<'hir> Entry<'hir> {
fn parent_node(self) -> Option<HirId> { fn parent_node(self) -> Option<HirId> {
match self.node { match self.node {
Node::Crate(_) | Node::MacroDef(_) => None, Node::Crate(_) => None,
_ => Some(self.parent), _ => Some(self.parent),
} }
} }

View File

@ -8,3 +8,17 @@ macro_rules! my_macro {
($a:tt) => (); ($a:tt) => ();
($e:expr) => {}; ($e:expr) => {};
} }
// Check that exported macro defined in a module are shown at crate root.
// @has macros/macro.my_sub_macro.html //pre 'macro_rules! my_sub_macro {'
// @has - //pre '() => { ... };'
// @has - //pre '($a:tt) => { ... };'
// @has - //pre '($e:expr) => { ... };'
mod sub {
#[macro_export]
macro_rules! my_sub_macro {
() => {};
($a:tt) => {};
($e:expr) => {};
}
}