@@ -75,7 +75,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7575 // moving them back to their correct locations.
7676 ' exported_macros: for def in krate. exported_macros {
7777 // The `def` of a macro in `exported_macros` should correspond to either:
78- // - a `#[macro-export ] macro_rules!` macro,
78+ // - a `#[macro_export ] macro_rules!` macro,
7979 // - a built-in `derive` (or attribute) macro such as the ones in `::core`,
8080 // - a `pub macro`.
8181 // Only the last two need to be fixed, thus:
@@ -84,17 +84,18 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
8484 continue ' exported_macros;
8585 }
8686 let tcx = self . cx . tcx ;
87- /* Because of #77828 we cannot do the simpler:
88- let macro_parent_module = tcx.def_path(tcx.parent_module(def.hir_id).to_def_id());
89- // and instead have to do: */
90- let macro_parent_module = tcx . def_path ( {
87+ // Note: this is not the same as `.parent_module()`. Indeed, the latter looks
88+ // for the closest module _ancestor_, which is not necessarily a direct parent
89+ // (since a direct parent isn't necessarily a module, c.f. #77828).
90+ let macro_parent_def_id = {
9191 use rustc_middle:: ty:: DefIdTree ;
9292 tcx. parent ( tcx. hir ( ) . local_def_id ( def. hir_id ) . to_def_id ( ) ) . unwrap ( )
93- } ) ;
93+ } ;
94+ let macro_parent_path = tcx. def_path ( macro_parent_def_id) ;
9495 // HACK: rustdoc has no way to lookup `doctree::Module`s by their HirId. Instead,
9596 // lookup the module by its name, by looking at each path segment one at a time.
9697 let mut cur_mod = & mut top_level_module;
97- for path_segment in macro_parent_module . data {
98+ for path_segment in macro_parent_path . data {
9899 // Path segments may refer to a module (in which case they belong to the type
99100 // namespace), which is _necessary_ for the macro to be accessible outside it
100101 // (no "associated macros" as of yet). Else we bail with an outer `continue`.
@@ -108,6 +109,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
108109 None => continue ' exported_macros,
109110 }
110111 }
112+ let cur_mod_def_id = tcx. hir ( ) . local_def_id ( cur_mod. id ) . to_def_id ( ) ;
113+ assert_eq ! ( cur_mod_def_id, macro_parent_def_id) ;
111114 cur_mod. macros . push ( ( def, None ) ) ;
112115 }
113116 self . cx . renderinfo . get_mut ( ) . exact_paths = self . exact_paths ;
0 commit comments