Change:
rustdoc is showing the macro at the wrong path in libcore: says but really it is (and the same goes for ). Looks like is not handled correctly? Cc
cc
When a macro is marked as reexported, you can use it directly from the "top", so in this regard, isn't it the expected behavior?
AFAIK that is not true for macros, just for macros.
Note that there is no here. This macro is exported via , like normal items (functions, structs, ...).
Code example: So I think there is definitely a rustdoc bug here. The path it shows does not work.
Oh I see. Indeed, it's a bug.
Not sure if helpful, but the way we got public exports to work in rustdoc in was by creating a separate crate (), and then re-exporting from from the main crate. This enabled us to render  in rustdoc. This issue will also affect the upcoming  PR.
how hard is it to fix this problem, and what does the rustdoc team's prioritization for this look like? We'd like to stabilize , and I am not sure if we want to stabilize this while its docs are rendered incorrectly.
Sorry but no idea. I don't have much time lately so maybe someone else from the will give it a try? Otherwise I'll try to fix it as soon as I have time.
Yeah so the problem here is that we have TypeKind::Macro which forces all macros to live at the root. Given a Def or a Res, how do I tell if a macro is MBE or a newstyle macro? (There's a possibility that our current code actually handles this correctly already and we need to remove the special case for TypeKind::Macro)
Okay so here are the two offending bits of code: The latter is more about linking to these items rather than rendering them. So the problem is that by the time stuff gets to rustdoc, we're looking at the HIR and the two kinds of macros are squashed together on . The newstyle macros are not a variant of at all, they disappear from the HIR tree at that point and only exist as an array on the crate. It would be nice if MBEs stayed on and newstyle macros were actually Items or at least stored separately on the module. We could handle these as we see their containing module, but that seems prone to issues when there are reexports and private modules in the mix. I think having newstyle macros live on rather than is a mistake.
An option to do this purely in rustdoc would be to check the and insert it in the appropriate modules after the fact, however this is kinda icky and i don't think this will be the last tooling problem encountered by newstyle macros living at the crate root in HIR.
will probably know best how to interpret this information; somehow macro name resolution also has to work with this, after all.
I don't think name resolution actually operates on the hir that way. But yeah, worth letting him answer.
I think you can recover the module they came from using the on it.
You can tell whether it's a macro or a by looking at .

Discussion:
} fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) { <ins> // Non-opaque macros cannot make other items more accessible than they already are. </ins> if attr::find_transparency(&self.tcx.sess, &md.attrs, md.ast.macro_rules).0 != Transparency::Opaque { <del> self.update(md.hir_id, Some(AccessLevel::Public)); </del> <ins> // `#[macro_export]`-ed `macro_rules!` are `Public` since they // ignore their containing path to always appear at the crate root. if md.ast.macro_rules { self.update(md.hir_id, Some(AccessLevel::Public)); } </ins> return; }

pub use core::prelude::v1::{ asm, assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args, format_args_nl, global_asm, include, include_bytes, include_str, line, llvm_asm, log_syntax, <del> module_path, option_env, stringify, trace_macros, </del> <ins> module_path, option_env, stringify, trace_macros, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, </ins> }; <del> // FIXME: Attribute and derive macros are not documented because for them rustdoc generates </del> <ins> // FIXME: Attribute and internal derive macros are not documented because for them rustdoc generates </ins> // dead links which fail link checker testing. #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[allow(deprecated)] #[doc(hidden)] pub use core::prelude::v1::{ <del> bench, global_allocator, test, test_case, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, RustcDecodable, RustcEncodable, </del> <ins> bench, global_allocator, test, test_case, RustcDecodable, RustcEncodable, </ins> }; #[unstable(

if !s.is_empty() { Some(s) } else { None } }); let fqn = if let clean::TypeKind::Macro = kind { <del> vec![crate_name, relative.last().expect("relative was empty")] </del> <ins> // Check to see if it is a macro 2.0 or built-in macro if matches!( cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())), LoadedMacro::MacroDef(def, _) if matches!(&def.kind, ast::ItemKind::MacroDef(ast_def) if !ast_def.macro_rules) ) { once(crate_name).chain(relative).collect() } else { vec![crate_name, relative.last().expect("relative was empty")] } </ins> } else { once(crate_name).chain(relative).collect() };

} crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> { <del> let mut module = self.visit_mod_contents( </del> <ins> let mut top_level_module = self.visit_mod_contents( </ins> krate.item.span, &Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public }, hir::CRATE_HIR_ID, &krate.item.module, None, ); <del> // Attach the crate's exported macros to the top-level module: module.macros.extend(krate.exported_macros.iter().map(|def| (def, None))); module.is_crate = true; </del> <ins> top_level_module.is_crate = true; // Attach the crate's exported macros to the top-level module. // In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as // well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by // moving them back to their correct locations. 'exported_macros: for def in krate.exported_macros { // The `def` of a macro in `exported_macros` should correspond to either: //  - a `#[macro-export] macro_rules!` macro, //  - a built-in `derive` (or attribute) macro such as the ones in `::core`, //  - a `pub macro`. // Only the last two need to be fixed, thus: if def.ast.macro_rules { top_level_module.macros.push((def, None)); continue 'exported_macros; } let tcx = self.cx.tcx; /* Because of #77828 we cannot do the simpler: let macro_parent_module = tcx.def_path(tcx.parent_module(def.hir_id).to_def_id()); // and instead have to do: */ let macro_parent_module = tcx.def_path({ use rustc_middle::ty::DefIdTree; tcx.parent(tcx.hir().local_def_id(def.hir_id).to_def_id()).unwrap() }); // HACK: rustdoc has no way to lookup `doctree::Module`s by their HirId. Instead, // lookup the module by its name, by looking at each path segment one at a time. // Once #80415 is merged, this whole `for` loop research can be replaced by that. let mut cur_mod = &mut top_level_module; for path_segment in macro_parent_module.data { let path_segment_ty_ns = match path_segment.data { rustc_hir::definitions::DefPathData::TypeNs(symbol) => symbol, _ => { // If the path segment is not from the type namespace // (_e.g._, it can be from a value namespace in the case of `f::` in: // `fn f() { pub macro m() {} }` // then the item is not accessible, and should thus act as if it didn't // exist (unless "associated macros" (inside an `impl`) were a thing…). continue 'exported_macros; } }; // The obtained name in the type namespace may belong to something that is not // a `mod`ule (_e.g._, it could be an `enum` with a `pub macro` defined within // the block used for a discriminant. if let Some(child_mod) = cur_mod.mods.iter_mut().find(|module| module.name == Some(path_segment_ty_ns)) { cur_mod = child_mod; } else { // If the macro's parent def path is not exclusively made of module // components, then it is not accessible (c.f. previous `continue`). continue 'exported_macros; } } cur_mod.macros.push((def, None)); } </ins> self.cx.renderinfo.get_mut().exact_paths = self.exact_paths; <del> module </del> <ins> top_level_module </ins> } fn visit_mod_contents(

<ins> // edition:2018 #![feature(decl_macro)] #![crate_name = "external_crate"] pub mod some_module { /* == Make sure the logic is not affected by a re-export == */ mod private { pub macro external_macro() {} } pub use private::external_macro; } </ins>

<ins> // aux-build:macro_pub_in_module.rs // edition:2018 // build-aux-docs //! See issue #74355 #![feature(decl_macro, no_core, rustc_attrs)] #![crate_name = "krate"] #![no_core] // @has external_crate/some_module/macro.external_macro.html // @!has external_crate/macro.external_macro.html extern crate external_crate; pub mod inner { // @has krate/inner/macro.raw_const.html // @!has krate/macro.raw_const.html pub macro raw_const() {} // @has krate/inner/macro.test.html // @!has krate/macro.test.html #[rustc_builtin_macro] pub macro test($item:item) {} // @has krate/inner/macro.Clone.html // @!has krate/macro.Clone.html #[rustc_builtin_macro] pub macro Clone($item:item) {} // Make sure the logic is not affected by re-exports. mod unrenamed { // @!has krate/macro.unrenamed.html #[rustc_macro_transparency = "semitransparent"] pub macro unrenamed() {} } // @has krate/inner/macro.unrenamed.html pub use unrenamed::unrenamed; mod private { // @!has krate/macro.m.html pub macro m() {} } // @has krate/inner/macro.renamed.html // @!has krate/macro.renamed.html pub use private::m as renamed; mod private2 { // @!has krate/macro.m2.html pub macro m2() {} } use private2 as renamed_mod; // @has krate/inner/macro.m2.html pub use renamed_mod::m2; // @has krate/inner/macro.external_macro.html // @!has krate/macro.external_macro.html pub use ::external_crate::some_module::external_macro; } // Namespaces: Make sure the logic does not mix up a function name with a module name… fn both_fn_and_mod() { // @!has krate/macro.in_both_fn_and_mod.html pub macro in_both_fn_and_mod() {} } pub mod both_fn_and_mod { // @!has krate/both_fn_and_mod/macro.in_both_fn_and_mod.html } const __: () = { // @!has krate/macro.in_both_const_and_mod.html pub macro in_both_const_and_mod() {} }; pub mod __ { // @!has krate/__/macro.in_both_const_and_mod.html } enum Enum { Crazy = { // @!has krate/macro.this_is_getting_weird.html; pub macro this_is_getting_weird() {} 42 }, } </ins>