Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute parent module when collecting hir::MacroDef. #80415

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions compiler/rustc_middle/src/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
})
});
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Entry<'hir> {
impl<'hir> Entry<'hir> {
fn parent_node(self) -> Option<HirId> {
match self.node {
Node::Crate(_) | Node::MacroDef(_) => None,
Node::Crate(_) => None,
_ => Some(self.parent),
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/rustdoc/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ macro_rules! my_macro {
($a:tt) => ();
($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) => {};
}
}