Skip to content

Commit cc5d90c

Browse files
Rollup merge of rust-lang#137534 - xizheyin:issue-137342, r=GuillaumeGomez
[rustdoc] hide item that is not marked as doc(inline) and whose src is doc(hidden) Part of rust-lang#137342 ![image](https://github.com/user-attachments/assets/68b4649a-a64a-43b2-8a73-6ac92b486e9e)
2 parents 98dfe93 + 133705c commit cc5d90c

9 files changed

+60
-26
lines changed

src/librustdoc/passes/strip_hidden.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88
use tracing::debug;
99

10-
use crate::clean;
1110
use crate::clean::utils::inherits_doc_hidden;
12-
use crate::clean::{Item, ItemIdSet};
11+
use crate::clean::{self, Item, ItemIdSet, reexport_chain};
1312
use crate::core::DocContext;
1413
use crate::fold::{DocFolder, strip_item};
1514
use crate::passes::{ImplStripper, Pass};
@@ -89,6 +88,25 @@ impl Stripper<'_, '_> {
8988
impl DocFolder for Stripper<'_, '_> {
9089
fn fold_item(&mut self, i: Item) -> Option<Item> {
9190
let has_doc_hidden = i.is_doc_hidden();
91+
92+
if let clean::ImportItem(clean::Import { source, .. }) = &i.kind
93+
&& let Some(source_did) = source.did
94+
&& let Some(import_def_id) = i.def_id().and_then(|def_id| def_id.as_local())
95+
{
96+
let reexports = reexport_chain(self.tcx, import_def_id, source_did);
97+
98+
// Check if any reexport in the chain has a hidden source
99+
let has_hidden_source = reexports
100+
.iter()
101+
.filter_map(|reexport| reexport.id())
102+
.any(|reexport_did| self.tcx.is_doc_hidden(reexport_did))
103+
|| self.tcx.is_doc_hidden(source_did);
104+
105+
if has_hidden_source {
106+
return None;
107+
}
108+
}
109+
92110
let is_impl_or_exported_macro = match i.kind {
93111
clean::ImplItem(..) => true,
94112
// If the macro has the `#[macro_export]` attribute, it means it's accessible at the

tests/rustdoc/doc-hidden-reexports-109449.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ pub mod single_reexport {
2626
//@ has 'foo/single_reexport/index.html'
2727

2828
// First we check that we have 4 type aliases.
29-
//@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 4
29+
//@ count - '//*[@id="main-content"]/*[@class="item-table reexports"]//code' 0
3030

3131
// Then we check that we have the correct link for each re-export.
3232

3333
//@ !has - '//*[@href="struct.Foo.html"]' 'Foo'
34-
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
34+
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
3535
pub use crate::private_module::Public as Foo;
3636
//@ !has - '//*[@href="type.Foo2.html"]' 'Foo2'
37-
//@ has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
37+
//@ !has - '//*[@id="reexport.Foo2"]/code' 'pub use crate::private_module::Bar as Foo2;'
3838
pub use crate::private_module::Bar as Foo2;
3939
//@ !has - '//*[@href="type.Yo.html"]' 'Yo'
40-
//@ has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
40+
//@ !has - '//*[@id="reexport.Yo"]/code' 'pub use crate::Bar3 as Yo;'
4141
pub use crate::Bar3 as Yo;
4242
//@ !has - '//*[@href="struct.Yo2.html"]' 'Yo2'
43-
//@ has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
43+
//@ !has - '//*[@id="reexport.Yo2"]/code' 'pub use crate::FooFoo as Yo2;'
4444
pub use crate::FooFoo as Yo2;
4545

4646
// Checking that each file is also created as expected.
@@ -70,19 +70,19 @@ pub mod single_reexport_no_inline {
7070
//@ has - '//*[@id="main-content"]/*[@class="section-header"]' 'Re-exports'
7171

7272
// Now we check that we don't have links to the items, just `pub use`.
73-
//@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
73+
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Public as XFoo;'
7474
//@ !has - '//*[@id="main-content"]//a' 'XFoo'
7575
#[doc(no_inline)]
7676
pub use crate::private_module::Public as XFoo;
77-
//@ has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
77+
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::private_module::Bar as Foo2;'
7878
//@ !has - '//*[@id="main-content"]//a' 'Foo2'
7979
#[doc(no_inline)]
8080
pub use crate::private_module::Bar as Foo2;
81-
//@ has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
81+
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::Bar3 as Yo;'
8282
//@ !has - '//*[@id="main-content"]//a' 'Yo'
8383
#[doc(no_inline)]
8484
pub use crate::Bar3 as Yo;
85-
//@ has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
85+
//@ !has - '//*[@id="main-content"]//*' 'pub use crate::FooFoo as Yo2;'
8686
//@ !has - '//*[@id="main-content"]//a' 'Yo2'
8787
#[doc(no_inline)]
8888
pub use crate::FooFoo as Yo2;

tests/rustdoc/doc-hidden-source.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Test for <https://github.com/rust-lang/rust/issues/137342>.
2+
3+
#![crate_name = "foo"]
4+
5+
//@ has 'foo/index.html'
6+
//@ !has - '//*[@id="main-content"]//*[@class="struct"]' 'Bar'
7+
#[doc(hidden)]
8+
pub struct Bar;
9+
10+
//@ !has - '//*' 'pub use crate::Bar as A;'
11+
pub use crate::Bar as A;
12+
//@ !has - '//*' 'pub use crate::A as B;'
13+
pub use crate::A as B;
14+
//@ has - '//dt/a[@class="struct"]' 'C'
15+
#[doc(inline)]
16+
pub use crate::Bar as C;

tests/rustdoc/inline_cross/inline_hidden.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate rustdoc_hidden;
66

77
//@ has inline_hidden/index.html
88
// Ensures this item is not inlined.
9-
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
9+
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
1010
#[doc(no_inline)]
1111
pub use rustdoc_hidden::Foo;
1212

@@ -16,7 +16,7 @@ pub use rustdoc_hidden::Foo;
1616
pub use rustdoc_hidden::Foo as Inlined;
1717

1818
// Even with this import, we should not see `Foo`.
19-
//@ count - '//dt' 4
19+
//@ count - '//dt' 3
2020
//@ has - '//dt/a[@class="struct"]' 'Bar'
2121
//@ has - '//dt/a[@class="fn"]' 'foo'
2222
pub use rustdoc_hidden::*;

tests/rustdoc/reexport-attr-merge.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use Foo1 as Foo2;
1818

1919
// First we ensure that only the reexport `Bar2` and the inlined struct `Bar`
2020
// are inlined.
21-
//@ count - '//a[@class="struct"]' 2
21+
//@ count - '//a[@class="struct"]' 1
2222
// Then we check that `cfg` is displayed for base item, but not for intermediate re-exports.
2323
//@ has - '//*[@class="stab portability"]' 'foo'
2424
//@ !has - '//*[@class="stab portability"]' 'bar'
@@ -29,5 +29,5 @@ pub use Foo2 as Bar;
2929

3030
// This one should appear but `Bar2` won't be linked because there is no
3131
// `#[doc(inline)]`.
32-
//@ has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
32+
//@ !has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
3333
pub use Foo2 as Bar2;

tests/rustdoc/reexport-doc-hidden-inside-private.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ mod private_module {
99
}
1010

1111
//@ has 'foo/index.html'
12-
//@ has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
12+
//@ !has - '//*[@id="reexport.Foo"]/code' 'pub use crate::private_module::Public as Foo;'
1313
pub use crate::private_module::Public as Foo;
1414
// Glob re-exports with no visible items should not be displayed.
15-
//@ count - '//*[@class="item-table reexports"]/dt' 1
15+
//@ count - '//*[@class="item-table reexports"]/dt' 0
1616
pub use crate::private_module::*;

tests/rustdoc/reexport-doc-hidden.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pub type Type = u32;
99

1010
//@ has 'foo/index.html'
11-
//@ has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
11+
//@ !has - '//*[@id="reexport.Type2"]/code' 'pub use crate::Type as Type2;'
1212
pub use crate::Type as Type2;
1313

1414
//@ count - '//*[@id="reexport.Type3"]' 0
@@ -21,5 +21,5 @@ macro_rules! foo {
2121
() => {};
2222
}
2323

24-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
24+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
2525
pub use crate::foo as Macro;

tests/rustdoc/reexport-hidden-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
//@ has 'foo/index.html'
77
//@ has - '//*[@id="main-content"]//a[@href="macro.Macro2.html"]' 'Macro2'
8-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
8+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
99

1010
//@ has 'foo/macro.Macro2.html'
1111
//@ has - '//*[@class="docblock"]' 'Displayed'

tests/rustdoc/reexport-of-doc-hidden.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ macro_rules! foo {
1212
}
1313

1414
//@ has 'foo/index.html'
15-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
15+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
1616
pub use crate::foo as Macro;
17-
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
17+
//@ !has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
1818
pub use crate::foo as Macro2;
19-
//@ has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
19+
//@ !has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
2020
pub use crate::Bar as Boo;
21-
//@ has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
21+
//@ !has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
2222
pub use crate::Bar as Boo2;
2323

2424
pub fn fofo() {}
@@ -30,9 +30,9 @@ pub use crate::fofo as f2;
3030

3131
pub mod sub {
3232
//@ has 'foo/sub/index.html'
33-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
33+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
3434
pub use crate::foo as Macro;
35-
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
35+
//@ !has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
3636
pub use crate::foo as Macro2;
3737

3838
//@ has - '//*[@id="reexport.f1"]/code' 'pub use crate::fofo as f1;'

0 commit comments

Comments
 (0)