Skip to content

Commit 8717f2f

Browse files
committed
[rustdoc] hide item that is not marked as doc(inline) and whose src is doc(hidden)
Signed-off-by: xizheyin <[email protected]>
1 parent 96cfc75 commit 8717f2f

6 files changed

+78
-23
lines changed

src/librustdoc/passes/strip_hidden.rs

+62-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
33
use std::mem;
44

5+
use rustc_data_structures::fx::FxHashMap;
56
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
67
use rustc_middle::ty::TyCtxt;
78
use rustc_span::symbol::sym;
89
use tracing::debug;
910

10-
use crate::clean;
1111
use crate::clean::utils::inherits_doc_hidden;
12-
use crate::clean::{Item, ItemIdSet};
12+
use crate::clean::{self, Item, ItemId, ItemIdSet};
1313
use crate::core::DocContext;
1414
use crate::fold::{DocFolder, strip_item};
1515
use crate::passes::{ImplStripper, Pass};
@@ -25,6 +25,28 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
2525
let mut retained = ItemIdSet::default();
2626
let is_json_output = cx.is_json_output();
2727

28+
let all_items = collect_items(&krate);
29+
30+
// Collect hidden items
31+
let hidden_items: Vec<_> =
32+
all_items.iter().filter(|item| item.is_doc_hidden()).cloned().collect();
33+
34+
// Map import items to their hidden source items
35+
let source_items: FxHashMap<_, _> = all_items
36+
.iter()
37+
.filter_map(|item| {
38+
if let clean::ItemKind::ImportItem(import) = &item.kind {
39+
let source_item = hidden_items
40+
.iter()
41+
.find(|i| i.item_id.as_def_id() == import.source.did)
42+
.cloned();
43+
Some((item.item_id, source_item))
44+
} else {
45+
None
46+
}
47+
})
48+
.collect();
49+
2850
// strip all #[doc(hidden)] items
2951
let krate = {
3052
let mut stripper = Stripper {
@@ -33,6 +55,7 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
3355
tcx: cx.tcx,
3456
is_in_hidden_item: false,
3557
last_reexport: None,
58+
source_items,
3659
};
3760
stripper.fold_crate(krate)
3861
};
@@ -49,12 +72,30 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
4972
stripper.fold_crate(krate)
5073
}
5174

75+
fn collect_items(krate: &clean::Crate) -> Vec<Item> {
76+
fn collect_items_module(module: &clean::Module) -> Vec<Item> {
77+
let mut items = Vec::new();
78+
for item in &module.items {
79+
items.push(item.clone());
80+
}
81+
items
82+
}
83+
84+
let mut items = Vec::new();
85+
if let clean::ItemKind::ModuleItem(module) = &krate.module.kind {
86+
items.extend(collect_items_module(module));
87+
}
88+
items
89+
}
90+
5291
struct Stripper<'a, 'tcx> {
5392
retained: &'a mut ItemIdSet,
5493
update_retained: bool,
5594
tcx: TyCtxt<'tcx>,
5695
is_in_hidden_item: bool,
5796
last_reexport: Option<LocalDefId>,
97+
/// store the source items of a import item
98+
source_items: FxHashMap<ItemId, Option<Item>>,
5899
}
59100

60101
impl Stripper<'_, '_> {
@@ -89,6 +130,25 @@ impl Stripper<'_, '_> {
89130
impl DocFolder for Stripper<'_, '_> {
90131
fn fold_item(&mut self, i: Item) -> Option<Item> {
91132
let has_doc_hidden = i.is_doc_hidden();
133+
debug!(
134+
"kkkkkkkkkkkkkk {:?}, item: {:?}, has_doc_hidden: {}, is_in_hidden_item: {}\n",
135+
i.name, i, has_doc_hidden, self.is_in_hidden_item
136+
);
137+
138+
// if self.hidden_items.iter().any(|item| item.item_id == i.item_id) {
139+
// return None;
140+
// }
141+
142+
if let clean::ImportItem(clean::Import { .. }) = &i.kind {
143+
if let Some(source_item) = self.source_items.get(&i.item_id) {
144+
if let Some(source_item) = source_item {
145+
if source_item.is_doc_hidden() {
146+
return None;
147+
}
148+
}
149+
}
150+
}
151+
92152
let is_impl_or_exported_macro = match i.kind {
93153
clean::ImplItem(..) => true,
94154
// If the macro has the `#[macro_export]` attribute, it means it's accessible at the

tests/rustdoc/reexport-attr-merge.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@ pub struct Foo;
1212

1313
#[doc(hidden, no_inline, cfg(feature = "bar"))]
1414
pub use Foo as Foo1;
15-
1615
#[doc(hidden, inline)]
1716
pub use Foo1 as Foo2;
18-
1917
// First we ensure that only the reexport `Bar2` and the inlined struct `Bar`
2018
// are inlined.
21-
//@ count - '//a[@class="struct"]' 2
19+
//@ count - '//a[@class="struct"]' 1
2220
// Then we check that `cfg` is displayed for base item, but not for intermediate re-exports.
2321
//@ has - '//*[@class="stab portability"]' 'foo'
2422
//@ !has - '//*[@class="stab portability"]' 'bar'
2523
// And finally we check that the only element displayed is `Bar`.
2624
//@ has - '//a[@class="struct"]' 'Bar'
2725
#[doc(inline)]
2826
pub use Foo2 as Bar;
29-
3027
// This one should appear but `Bar2` won't be linked because there is no
3128
// `#[doc(inline)]`.
32-
//@ has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
29+
//@ !has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
3330
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-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
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;
13-
1413
//@ count - '//*[@id="reexport.Type3"]' 0
1514
#[doc(hidden)]
1615
pub use crate::Type as Type3;
@@ -21,5 +20,5 @@ macro_rules! foo {
2120
() => {};
2221
}
2322

24-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
23+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
2524
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

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ macro_rules! foo {
1212
}
1313

1414
//@ has 'foo/index.html'
15-
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
16-
pub use crate::foo as Macro;
17-
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
18-
pub use crate::foo as Macro2;
19-
//@ has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
15+
//@ !has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
16+
//@ !has - '//*[@id="reexport.Boo"]/code' 'pub use crate::Bar as Boo;'
2017
pub use crate::Bar as Boo;
21-
//@ has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
18+
//@ !has - '//*[@id="reexport.Boo2"]/code' 'pub use crate::Bar as Boo2;'
2219
pub use crate::Bar as Boo2;
20+
pub use crate::foo as Macro;
21+
//@ !has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
22+
pub use crate::foo as Macro2;
2323

2424
pub fn fofo() {}
2525

@@ -31,12 +31,11 @@ pub use crate::fofo as f2;
3131
pub mod sub {
3232
//@ has 'foo/sub/index.html'
3333
//@ has - '//*[@id="reexport.Macro"]/code' 'pub use crate::foo as Macro;'
34-
pub use crate::foo as Macro;
35-
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
36-
pub use crate::foo as Macro2;
37-
3834
//@ has - '//*[@id="reexport.f1"]/code' 'pub use crate::fofo as f1;'
3935
pub use crate::fofo as f1;
4036
//@ has - '//*[@id="reexport.f2"]/code' 'pub use crate::fofo as f2;'
4137
pub use crate::fofo as f2;
38+
pub use crate::foo as Macro;
39+
//@ has - '//*[@id="reexport.Macro2"]/code' 'pub use crate::foo as Macro2;'
40+
pub use crate::foo as Macro2;
4241
}

0 commit comments

Comments
 (0)