From 3802ba0f6a88355396a4b27b922f6f6b494dbd32 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 26 May 2023 17:31:11 +0200 Subject: [PATCH 1/3] Fix re-export of doc hidden macro not showing up --- src/librustdoc/clean/mod.rs | 3 ++- src/librustdoc/visit_ast.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 59a3e63172406..03adc19e359c1 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2592,7 +2592,8 @@ fn clean_use_statement_inner<'tcx>( } else { if inline_attr.is_none() && let Res::Def(DefKind::Mod, did) = path.res - && !did.is_local() && did.is_crate_root() + && !did.is_local() + && did.is_crate_root() { // if we're `pub use`ing an extern crate root, don't inline it unless we // were specifically asked for it diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 8f8dc6b709053..891574eb46647 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -310,6 +310,16 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } let ret = match tcx.hir().get_by_def_id(res_did) { + // Bang macros are handled a bit on their because of how they are handled by the + // compiler. If they have `#[doc(hidden)]` and the re-export doesn't have + // `#[doc(inline)]`, then we don't inline it. + Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, MacroKind::Bang), .. }) + if !please_inline + && renamed.is_some() + && self.cx.tcx.is_doc_hidden(ori_res_did) => + { + return false; + } Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => { let prev = mem::replace(&mut self.inlining, true); for &i in m.item_ids { From c908d1e4decdb0a6db8280a48baa64f8344acab7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 26 May 2023 17:31:54 +0200 Subject: [PATCH 2/3] Update tests for re-exports of doc hidden macros --- tests/rustdoc/reexport-doc-hidden.rs | 3 +-- tests/rustdoc/reexport-hidden-macro.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/rustdoc/reexport-doc-hidden.rs b/tests/rustdoc/reexport-doc-hidden.rs index 3ea5fde72f711..d9ed954868e50 100644 --- a/tests/rustdoc/reexport-doc-hidden.rs +++ b/tests/rustdoc/reexport-doc-hidden.rs @@ -21,6 +21,5 @@ macro_rules! foo { () => {}; } -// This is a bug: https://github.com/rust-lang/rust/issues/59368 -// @!has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' +// @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' pub use crate::foo as Macro; diff --git a/tests/rustdoc/reexport-hidden-macro.rs b/tests/rustdoc/reexport-hidden-macro.rs index afcfa979616ec..e498acbab0b52 100644 --- a/tests/rustdoc/reexport-hidden-macro.rs +++ b/tests/rustdoc/reexport-hidden-macro.rs @@ -15,7 +15,7 @@ macro_rules! foo { () => {}; } -/// not displayed +// @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' pub use crate::foo as Macro; /// Displayed #[doc(inline)] From 898dfc680f22b0b07eb569384c5e947d2922d0d5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 26 May 2023 23:53:14 +0200 Subject: [PATCH 3/3] Correctly handle multiple re-exports of bang macros at the same level --- src/librustdoc/visit_ast.rs | 12 +++++-- tests/rustdoc/reexport-hidden-macro.rs | 2 +- tests/rustdoc/reexport-of-doc-hidden.rs | 42 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/rustdoc/reexport-of-doc-hidden.rs diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 891574eb46647..eb813af779ef6 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -305,7 +305,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { return false; } - if !self.view_item_stack.insert(res_did) { + let is_bang_macro = matches!( + tcx.hir().get_by_def_id(res_did), + Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, MacroKind::Bang), .. }) + ); + + if !self.view_item_stack.insert(res_did) && !is_bang_macro { return false; } @@ -313,8 +318,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // Bang macros are handled a bit on their because of how they are handled by the // compiler. If they have `#[doc(hidden)]` and the re-export doesn't have // `#[doc(inline)]`, then we don't inline it. - Node::Item(&hir::Item { kind: hir::ItemKind::Macro(_, MacroKind::Bang), .. }) - if !please_inline + Node::Item(_) + if is_bang_macro + && !please_inline && renamed.is_some() && self.cx.tcx.is_doc_hidden(ori_res_did) => { diff --git a/tests/rustdoc/reexport-hidden-macro.rs b/tests/rustdoc/reexport-hidden-macro.rs index e498acbab0b52..47a21e3946225 100644 --- a/tests/rustdoc/reexport-hidden-macro.rs +++ b/tests/rustdoc/reexport-hidden-macro.rs @@ -5,6 +5,7 @@ // @has 'foo/index.html' // @has - '//*[@id="main-content"]//a[@href="macro.Macro2.html"]' 'Macro2' +// @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' // @has 'foo/macro.Macro2.html' // @has - '//*[@class="docblock"]' 'Displayed' @@ -15,7 +16,6 @@ macro_rules! foo { () => {}; } -// @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' pub use crate::foo as Macro; /// Displayed #[doc(inline)] diff --git a/tests/rustdoc/reexport-of-doc-hidden.rs b/tests/rustdoc/reexport-of-doc-hidden.rs new file mode 100644 index 0000000000000..b733716c22a3b --- /dev/null +++ b/tests/rustdoc/reexport-of-doc-hidden.rs @@ -0,0 +1,42 @@ +// This test ensures that all re-exports of doc hidden elements are displayed. + +#![crate_name = "foo"] + +#[doc(hidden)] +pub struct Bar; + +#[macro_export] +#[doc(hidden)] +macro_rules! foo { + () => {}; +} + +// @has 'foo/index.html' +// @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' +pub use crate::foo as Macro; +// @has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;' +pub use crate::foo as Macro2; +// @has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;' +pub use crate::Bar as Boo; +// @has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;' +pub use crate::Bar as Boo2; + +pub fn fofo() {} + +// @has - '//*[@id="reexport.f1"]/code' 'pub use crate::fofo as f1;' +pub use crate::fofo as f1; +// @has - '//*[@id="reexport.f2"]/code' 'pub use crate::fofo as f2;' +pub use crate::fofo as f2; + +pub mod sub { + // @has 'foo/sub/index.html' + // @has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;' + pub use crate::foo as Macro; + // @has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;' + pub use crate::foo as Macro2; + + // @has - '//*[@id="reexport.f1"]/code' 'pub use crate::fofo as f1;' + pub use crate::fofo as f1; + // @has - '//*[@id="reexport.f2"]/code' 'pub use crate::fofo as f2;' + pub use crate::fofo as f2; +}