about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-02-27 20:54:26 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-03-04 01:35:48 +0800
commit133705c402941c0825fe658a932289847478cf9f (patch)
tree3dadcfce60003adfd40d473836e81efd5d845104
parent96cfc75584359ae7ad11cc45968059f29e7b44b7 (diff)
downloadrust-133705c402941c0825fe658a932289847478cf9f.tar.gz
rust-133705c402941c0825fe658a932289847478cf9f.zip
[rustdoc] hide item that is not marked as doc(inline) and whose src is doc(hidden)
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--src/librustdoc/passes/strip_hidden.rs22
-rw-r--r--tests/rustdoc/doc-hidden-reexports-109449.rs18
-rw-r--r--tests/rustdoc/doc-hidden-source.rs16
-rw-r--r--tests/rustdoc/inline_cross/inline_hidden.rs4
-rw-r--r--tests/rustdoc/reexport-attr-merge.rs4
-rw-r--r--tests/rustdoc/reexport-doc-hidden-inside-private.rs4
-rw-r--r--tests/rustdoc/reexport-doc-hidden.rs4
-rw-r--r--tests/rustdoc/reexport-hidden-macro.rs2
-rw-r--r--tests/rustdoc/reexport-of-doc-hidden.rs12
9 files changed, 60 insertions, 26 deletions
diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs
index a71bb62e56c..bcdca862862 100644
--- a/src/librustdoc/passes/strip_hidden.rs
+++ b/src/librustdoc/passes/strip_hidden.rs
@@ -7,9 +7,8 @@ use rustc_middle::ty::TyCtxt;
 use rustc_span::symbol::sym;
 use tracing::debug;
 
-use crate::clean;
 use crate::clean::utils::inherits_doc_hidden;
-use crate::clean::{Item, ItemIdSet};
+use crate::clean::{self, Item, ItemIdSet, reexport_chain};
 use crate::core::DocContext;
 use crate::fold::{DocFolder, strip_item};
 use crate::passes::{ImplStripper, Pass};
@@ -89,6 +88,25 @@ impl Stripper<'_, '_> {
 impl DocFolder for Stripper<'_, '_> {
     fn fold_item(&mut self, i: Item) -> Option<Item> {
         let has_doc_hidden = i.is_doc_hidden();
+
+        if let clean::ImportItem(clean::Import { source, .. }) = &i.kind
+            && let Some(source_did) = source.did
+            && let Some(import_def_id) = i.def_id().and_then(|def_id| def_id.as_local())
+        {
+            let reexports = reexport_chain(self.tcx, import_def_id, source_did);
+
+            // Check if any reexport in the chain has a hidden source
+            let has_hidden_source = reexports
+                .iter()
+                .filter_map(|reexport| reexport.id())
+                .any(|reexport_did| self.tcx.is_doc_hidden(reexport_did))
+                || self.tcx.is_doc_hidden(source_did);
+
+            if has_hidden_source {
+                return None;
+            }
+        }
+
         let is_impl_or_exported_macro = match i.kind {
             clean::ImplItem(..) => true,
             // If the macro has the `#[macro_export]` attribute, it means it's accessible at the
diff --git a/tests/rustdoc/doc-hidden-reexports-109449.rs b/tests/rustdoc/doc-hidden-reexports-109449.rs
index 8f195544120..78b9214300a 100644
--- a/tests/rustdoc/doc-hidden-reexports-109449.rs
+++ b/tests/rustdoc/doc-hidden-reexports-109449.rs
@@ -26,21 +26,21 @@ pub mod single_reexport {
     //@ has 'foo/single_reexport/index.html'
 
     // First we check that we have 4 type aliases.
-    //@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 4
+    //@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 0
 
     // Then we check that we have the correct link for each re-export.
 
     //@ !has - '//*[@href="struct.Foo.html"]' 'Foo'
-    //@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
+    //@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
     pub use crate::private_module::Public as Foo;
     //@ !has - '//*[@href="type.Foo2.html"]' 'Foo2'
-    //@ has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
+    //@ !has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
     pub use crate::private_module::Bar as Foo2;
     //@ !has - '//*[@href="type.Yo.html"]' 'Yo'
-    //@ has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
+    //@ !has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
     pub use crate::Bar3 as Yo;
     //@ !has - '//*[@href="struct.Yo2.html"]' 'Yo2'
-    //@ has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
+    //@ !has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
     pub use crate::FooFoo as Yo2;
 
     // Checking that each file is also created as expected.
@@ -70,19 +70,19 @@ pub mod single_reexport_no_inline {
     //@ has - '//*[@id="main-content"]/*[@class="section-header"]' 'Re-exports'
 
     // Now we check that we don't have links to the items, just `pub use`.
-    //@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
+    //@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
     //@ !has - '//*[@id="main-content"]//a' 'XFoo'
     #[doc(no_inline)]
     pub use crate::private_module::Public as XFoo;
-    //@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
+    //@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
     //@ !has - '//*[@id="main-content"]//a' 'Foo2'
     #[doc(no_inline)]
     pub use crate::private_module::Bar as Foo2;
-    //@ has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
+    //@ !has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
     //@ !has - '//*[@id="main-content"]//a' 'Yo'
     #[doc(no_inline)]
     pub use crate::Bar3 as Yo;
-    //@ has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
+    //@ !has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
     //@ !has - '//*[@id="main-content"]//a' 'Yo2'
     #[doc(no_inline)]
     pub use crate::FooFoo as Yo2;
diff --git a/tests/rustdoc/doc-hidden-source.rs b/tests/rustdoc/doc-hidden-source.rs
new file mode 100644
index 00000000000..b6bc622dd58
--- /dev/null
+++ b/tests/rustdoc/doc-hidden-source.rs
@@ -0,0 +1,16 @@
+// Test for <https://github.com/rust-lang/rust/issues/137342>.
+
+#![crate_name = "foo"]
+
+//@ has 'foo/index.html'
+//@ !has - '//*[@id="main-content"]//*[@class="struct"]' 'Bar'
+#[doc(hidden)]
+pub struct Bar;
+
+//@ !has - '//*' 'pub use crate::Bar as A;'
+pub use crate::Bar as A;
+//@ !has - '//*' 'pub use crate::A as B;'
+pub use crate::A as B;
+//@ has - '//dt/a[@class="struct"]' 'C'
+#[doc(inline)]
+pub use crate::Bar as C;
diff --git a/tests/rustdoc/inline_cross/inline_hidden.rs b/tests/rustdoc/inline_cross/inline_hidden.rs
index 49ca2db6a22..f6727de2cc6 100644
--- a/tests/rustdoc/inline_cross/inline_hidden.rs
+++ b/tests/rustdoc/inline_cross/inline_hidden.rs
@@ -6,7 +6,7 @@ extern crate rustdoc_hidden;
 
 //@ has inline_hidden/index.html
 // Ensures this item is not inlined.
-//@ has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
+//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
 #[doc(no_inline)]
 pub use rustdoc_hidden::Foo;
 
@@ -16,7 +16,7 @@ pub use rustdoc_hidden::Foo;
 pub use rustdoc_hidden::Foo as Inlined;
 
 // Even with this import, we should not see `Foo`.
-//@ count - '//dt' 4
+//@ count - '//dt' 3
 //@ has - '//dt/a[@class="struct"]' 'Bar'
 //@ has - '//dt/a[@class="fn"]' 'foo'
 pub use rustdoc_hidden::*;
diff --git a/tests/rustdoc/reexport-attr-merge.rs b/tests/rustdoc/reexport-attr-merge.rs
index e4a406c3845..aef302eb0b2 100644
--- a/tests/rustdoc/reexport-attr-merge.rs
+++ b/tests/rustdoc/reexport-attr-merge.rs
@@ -18,7 +18,7 @@ pub use Foo1 as Foo2;
 
 // First we ensure that only the reexport `Bar2` and the inlined struct `Bar`
 // are inlined.
-//@ count - '//a[@class="struct"]' 2
+//@ count - '//a[@class="struct"]' 1
 // Then we check that `cfg` is displayed for base item, but not for intermediate re-exports.
 //@ has - '//*[@class="stab portability"]' 'foo'
 //@ !has - '//*[@class="stab portability"]' 'bar'
@@ -29,5 +29,5 @@ pub use Foo2 as Bar;
 
 // This one should appear but `Bar2` won't be linked because there is no
 // `#[doc(inline)]`.
-//@ has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
+//@ !has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
 pub use Foo2 as Bar2;
diff --git a/tests/rustdoc/reexport-doc-hidden-inside-private.rs b/tests/rustdoc/reexport-doc-hidden-inside-private.rs
index 8e194ef74fb..bae2aa78ec7 100644
--- a/tests/rustdoc/reexport-doc-hidden-inside-private.rs
+++ b/tests/rustdoc/reexport-doc-hidden-inside-private.rs
@@ -9,8 +9,8 @@ mod private_module {
 }
 
 //@ has 'foo/index.html'
-//@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
+//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
 pub use crate::private_module::Public as Foo;
 // Glob re-exports with no visible items should not be displayed.
-//@ count - '//*[@class="item-table reexports"]/dt' 1
+//@ count - '//*[@class="item-table reexports"]/dt' 0
 pub use crate::private_module::*;
diff --git a/tests/rustdoc/reexport-doc-hidden.rs b/tests/rustdoc/reexport-doc-hidden.rs
index b912362f298..1468e9ad957 100644
--- a/tests/rustdoc/reexport-doc-hidden.rs
+++ b/tests/rustdoc/reexport-doc-hidden.rs
@@ -8,7 +8,7 @@
 pub type Type = u32;
 
 //@ has 'foo/index.html'
-//@ has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
+//@ !has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
 pub use crate::Type as Type2;
 
 //@ count - '//*[@id="reexport.Type3"]' 0
@@ -21,5 +21,5 @@ macro_rules! foo {
     () => {};
 }
 
-//@ 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 9b83bca3906..7345149c645 100644
--- a/tests/rustdoc/reexport-hidden-macro.rs
+++ b/tests/rustdoc/reexport-hidden-macro.rs
@@ -5,7 +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 - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
 
 //@ has 'foo/macro.Macro2.html'
 //@ has - '//*[@class="docblock"]' 'Displayed'
diff --git a/tests/rustdoc/reexport-of-doc-hidden.rs b/tests/rustdoc/reexport-of-doc-hidden.rs
index 21511bc2aea..e901d0ff8a2 100644
--- a/tests/rustdoc/reexport-of-doc-hidden.rs
+++ b/tests/rustdoc/reexport-of-doc-hidden.rs
@@ -12,13 +12,13 @@ macro_rules! foo {
 }
 
 //@ has 'foo/index.html'
-//@ 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;
-//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
+//@ !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;'
+//@ !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;'
+//@ !has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
 pub use crate::Bar as Boo2;
 
 pub fn fofo() {}
@@ -30,9 +30,9 @@ 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;'
+    //@ !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;'
+    //@ !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;'