Skip to content

Commit 255f107

Browse files
Fix ICE on pub macros defined within a non-module type namespace.
1 parent ce3ecd6 commit 255f107

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/librustdoc/visit_ast.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
9393
});
9494
// HACK: rustdoc has no way to lookup `doctree::Module`s by their HirId. Instead,
9595
// lookup the module by its name, by looking at each path segment one at a time.
96+
// Once #80415 is merged, this whole `for` loop research can be replaced by that.
9697
let mut cur_mod = &mut top_level_module;
9798
for path_segment in macro_parent_module.data {
9899
let path_segment_ty_ns = match path_segment.data {
@@ -106,11 +107,18 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
106107
continue 'exported_macros;
107108
}
108109
};
109-
cur_mod = cur_mod
110-
.mods
111-
.iter_mut()
112-
.find(|module| module.name == Some(path_segment_ty_ns))
113-
.unwrap();
110+
// The obtained name in the type namespace may belong to something that is not
111+
// a `mod`ule (_e.g._, it could be an `enum` with a `pub macro` defined within
112+
// the block used for a discriminant.
113+
if let Some(child_mod) =
114+
cur_mod.mods.iter_mut().find(|module| module.name == Some(path_segment_ty_ns))
115+
{
116+
cur_mod = child_mod;
117+
} else {
118+
// If the macro's parent def path is not exclusively made of module
119+
// components, then it is not accessible (c.f. previous `continue`).
120+
continue 'exported_macros;
121+
}
114122
}
115123
cur_mod.macros.push((def, None));
116124
}

src/test/rustdoc/macro_pub_in_module.rs

+8
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ const __: () = {
7272
pub mod __ {
7373
// @!has krate/__/macro.in_both_const_and_mod.html
7474
}
75+
76+
enum Enum {
77+
Crazy = {
78+
// @!has krate/macro.this_is_getting_weird.html;
79+
pub macro this_is_getting_weird() {}
80+
42
81+
},
82+
}

0 commit comments

Comments
 (0)