Skip to content

Commit ec362f0

Browse files
committed
Auto merge of #113574 - GuillaumeGomez:rustdoc-json-strip-hidden-impl, r=aDotInTheVoid,notriddle
Strip impl if not re-exported and is doc(hidden) Part of #112852. r? `@aDotInTheVoid`
2 parents 745efcc + c132cdb commit ec362f0

16 files changed

+107
-24
lines changed

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ pub(crate) fn build_impl(
473473
associated_trait.def_id,
474474
)
475475
.unwrap(); // corresponding associated item has to exist
476-
!tcx.is_doc_hidden(trait_item.def_id)
476+
document_hidden || !tcx.is_doc_hidden(trait_item.def_id)
477477
} else {
478478
item.visibility(tcx).is_public()
479479
}
@@ -496,7 +496,7 @@ pub(crate) fn build_impl(
496496
let mut stack: Vec<&Type> = vec![&for_];
497497

498498
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
499-
if tcx.is_doc_hidden(did) {
499+
if !document_hidden && tcx.is_doc_hidden(did) {
500500
return;
501501
}
502502
}
@@ -505,7 +505,7 @@ pub(crate) fn build_impl(
505505
}
506506

507507
while let Some(ty) = stack.pop() {
508-
if let Some(did) = ty.def_id(&cx.cache) && tcx.is_doc_hidden(did) {
508+
if let Some(did) = ty.def_id(&cx.cache) && !document_hidden && tcx.is_doc_hidden(did) {
509509
return;
510510
}
511511
if let Some(generics) = ty.generics() {

src/librustdoc/clean/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
5353
let mut inserted = FxHashSet::default();
5454
items.extend(doc.foreigns.iter().map(|(item, renamed)| {
5555
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
56-
if let Some(name) = item.name && !item.is_doc_hidden() {
56+
if let Some(name) = item.name && (cx.render_options.document_hidden || !item.is_doc_hidden()) {
5757
inserted.insert((item.type_(), name));
5858
}
5959
item
@@ -63,7 +63,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
6363
return None;
6464
}
6565
let item = clean_doc_module(x, cx);
66-
if item.is_doc_hidden() {
66+
if !cx.render_options.document_hidden && item.is_doc_hidden() {
6767
// Hidden modules are stripped at a later stage.
6868
// If a hidden module has the same name as a visible one, we want
6969
// to keep both of them around.
@@ -84,7 +84,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
8484
}
8585
let v = clean_maybe_renamed_item(cx, item, *renamed, *import_id);
8686
for item in &v {
87-
if let Some(name) = item.name && !item.is_doc_hidden() {
87+
if let Some(name) = item.name && (cx.render_options.document_hidden || !item.is_doc_hidden()) {
8888
inserted.insert((item.type_(), name));
8989
}
9090
}
@@ -2326,7 +2326,7 @@ fn get_all_import_attributes<'hir>(
23262326
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
23272327
first = false;
23282328
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
2329-
} else if !cx.tcx.is_doc_hidden(def_id) {
2329+
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
23302330
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
23312331
}
23322332
}

src/librustdoc/clean/types/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn should_not_trim() {
7373
fn is_same_generic() {
7474
use crate::clean::types::{PrimitiveType, Type};
7575
use crate::formats::cache::Cache;
76-
let cache = Cache::new(false);
76+
let cache = Cache::new(false, false);
7777
let generic = Type::Generic(rustc_span::symbol::sym::Any);
7878
let unit = Type::Primitive(PrimitiveType::Unit);
7979
assert!(!generic.is_doc_subtype_of(&unit, &cache));

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub(crate) fn run_global_ctxt(
345345
impl_trait_bounds: Default::default(),
346346
generated_synthetics: Default::default(),
347347
auto_traits,
348-
cache: Cache::new(render_options.document_private),
348+
cache: Cache::new(render_options.document_private, render_options.document_hidden),
349349
inlined: FxHashSet::default(),
350350
output_format,
351351
render_options,

src/librustdoc/formats/cache.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub(crate) struct Cache {
8686
/// Whether to document private items.
8787
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
8888
pub(crate) document_private: bool,
89+
/// Whether to document hidden items.
90+
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
91+
pub(crate) document_hidden: bool,
8992

9093
/// Crates marked with [`#[doc(masked)]`][doc_masked].
9194
///
@@ -137,8 +140,8 @@ struct CacheBuilder<'a, 'tcx> {
137140
}
138141

139142
impl Cache {
140-
pub(crate) fn new(document_private: bool) -> Self {
141-
Cache { document_private, ..Cache::default() }
143+
pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
144+
Cache { document_private, document_hidden, ..Cache::default() }
142145
}
143146

144147
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
798798
if let Some(def_id) = item.def_id() && self.cache().inlined_items.contains(&def_id) {
799799
self.is_inside_inlined_module = true;
800800
}
801-
} else if item.is_doc_hidden() {
801+
} else if !self.cache().document_hidden && item.is_doc_hidden() {
802802
// We're not inside an inlined module anymore since this one cannot be re-exported.
803803
self.is_inside_inlined_module = false;
804804
}

src/librustdoc/passes/check_doc_test_visibility.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
9292
return false;
9393
}
9494

95-
if cx.tcx.is_doc_hidden(def_id.to_def_id())
96-
|| inherits_doc_hidden(cx.tcx, def_id, None)
95+
if (!cx.render_options.document_hidden
96+
&& (cx.tcx.is_doc_hidden(def_id.to_def_id()) || inherits_doc_hidden(cx.tcx, def_id, None)))
9797
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
9898
{
9999
return false;

src/librustdoc/passes/strip_hidden.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
4242
cache: &cx.cache,
4343
is_json_output,
4444
document_private: cx.render_options.document_private,
45+
document_hidden: cx.render_options.document_hidden,
4546
};
4647
stripper.fold_crate(krate)
4748
}

src/librustdoc/passes/strip_priv_imports.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
1313

1414
pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1515
let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
16-
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(krate)
16+
ImportStripper {
17+
tcx: cx.tcx,
18+
is_json_output,
19+
document_hidden: cx.render_options.document_hidden,
20+
}
21+
.fold_crate(krate)
1722
}

src/librustdoc/passes/strip_private.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
2828
is_json_output,
2929
tcx: cx.tcx,
3030
};
31-
krate =
32-
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(stripper.fold_crate(krate));
31+
krate = ImportStripper {
32+
tcx: cx.tcx,
33+
is_json_output,
34+
document_hidden: cx.render_options.document_hidden,
35+
}
36+
.fold_crate(stripper.fold_crate(krate));
3337
}
3438

3539
// strip all impls referencing private items
@@ -39,6 +43,7 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
3943
cache: &cx.cache,
4044
is_json_output,
4145
document_private: cx.render_options.document_private,
46+
document_hidden: cx.render_options.document_hidden,
4247
};
4348
stripper.fold_crate(krate)
4449
}

src/librustdoc/passes/stripper.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::mem;
66
use crate::clean::{self, Item, ItemId, ItemIdSet};
77
use crate::fold::{strip_item, DocFolder};
88
use crate::formats::cache::Cache;
9+
use crate::visit_ast::inherits_doc_hidden;
910
use crate::visit_lib::RustdocEffectiveVisibilities;
1011

1112
pub(crate) struct Stripper<'a, 'tcx> {
@@ -151,6 +152,7 @@ pub(crate) struct ImplStripper<'a, 'tcx> {
151152
pub(crate) cache: &'a Cache,
152153
pub(crate) is_json_output: bool,
153154
pub(crate) document_private: bool,
155+
pub(crate) document_hidden: bool,
154156
}
155157

156158
impl<'a> ImplStripper<'a, '_> {
@@ -162,7 +164,13 @@ impl<'a> ImplStripper<'a, '_> {
162164
// If the "for" item is exported and the impl block isn't `#[doc(hidden)]`, then we
163165
// need to keep it.
164166
self.cache.effective_visibilities.is_exported(self.tcx, for_def_id)
165-
&& !item.is_doc_hidden()
167+
&& (self.document_hidden
168+
|| ((!item.is_doc_hidden()
169+
&& for_def_id
170+
.as_local()
171+
.map(|def_id| !inherits_doc_hidden(self.tcx, def_id, None))
172+
.unwrap_or(true))
173+
|| self.cache.inlined_items.contains(&for_def_id)))
166174
} else {
167175
false
168176
}
@@ -231,6 +239,7 @@ impl<'a> DocFolder for ImplStripper<'a, '_> {
231239
pub(crate) struct ImportStripper<'tcx> {
232240
pub(crate) tcx: TyCtxt<'tcx>,
233241
pub(crate) is_json_output: bool,
242+
pub(crate) document_hidden: bool,
234243
}
235244

236245
impl<'tcx> ImportStripper<'tcx> {
@@ -247,8 +256,12 @@ impl<'tcx> ImportStripper<'tcx> {
247256
impl<'tcx> DocFolder for ImportStripper<'tcx> {
248257
fn fold_item(&mut self, i: Item) -> Option<Item> {
249258
match *i.kind {
250-
clean::ImportItem(imp) if self.import_should_be_hidden(&i, &imp) => None,
251-
clean::ImportItem(_) if i.is_doc_hidden() => None,
259+
clean::ImportItem(imp)
260+
if !self.document_hidden && self.import_should_be_hidden(&i, &imp) =>
261+
{
262+
None
263+
}
264+
// clean::ImportItem(_) if !self.document_hidden && i.is_doc_hidden() => None,
252265
clean::ExternCrateItem { .. } | clean::ImportItem(..)
253266
if i.visibility(self.tcx) != Some(Visibility::Public) =>
254267
{

src/librustdoc/visit_ast.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
262262
return false;
263263
};
264264

265+
let document_hidden = self.cx.render_options.document_hidden;
265266
let use_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
266267
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
267268
let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
268-
|| use_attrs.lists(sym::doc).has_word(sym::hidden);
269+
|| (document_hidden && use_attrs.lists(sym::doc).has_word(sym::hidden));
269270

270271
if is_no_inline {
271272
return false;
@@ -285,11 +286,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
285286
};
286287

287288
let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did);
288-
let is_hidden = tcx.is_doc_hidden(ori_res_did);
289+
let is_hidden = !document_hidden && tcx.is_doc_hidden(ori_res_did);
289290
let item = tcx.hir().get_by_def_id(res_did);
290291

291292
if !please_inline {
292-
let inherits_hidden = inherits_doc_hidden(tcx, res_did, None);
293+
let inherits_hidden = !document_hidden && inherits_doc_hidden(tcx, res_did, None);
293294
// Only inline if requested or if the item would otherwise be stripped.
294295
if (!is_private && !inherits_hidden) || (
295296
is_hidden &&
@@ -359,6 +360,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
359360
import_def_id: LocalDefId,
360361
target_def_id: LocalDefId,
361362
) -> bool {
363+
if self.cx.render_options.document_hidden {
364+
return true;
365+
}
362366
let tcx = self.cx.tcx;
363367
let item_def_id = reexport_chain(tcx, import_def_id, target_def_id)
364368
.iter()

src/librustdoc/visit_lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) fn lib_embargo_visit_item(cx: &mut DocContext<'_>, def_id: DefId) {
3333
tcx: cx.tcx,
3434
extern_public: &mut cx.cache.effective_visibilities.extern_public,
3535
visited_mods: Default::default(),
36+
document_hidden: cx.render_options.document_hidden,
3637
}
3738
.visit_item(def_id)
3839
}
@@ -45,6 +46,7 @@ struct LibEmbargoVisitor<'a, 'tcx> {
4546
extern_public: &'a mut DefIdSet,
4647
// Keeps track of already visited modules, in case a module re-exports its parent
4748
visited_mods: DefIdSet,
49+
document_hidden: bool,
4850
}
4951

5052
impl LibEmbargoVisitor<'_, '_> {
@@ -63,7 +65,7 @@ impl LibEmbargoVisitor<'_, '_> {
6365
}
6466

6567
fn visit_item(&mut self, def_id: DefId) {
66-
if !self.tcx.is_doc_hidden(def_id) {
68+
if self.document_hidden || !self.tcx.is_doc_hidden(def_id) {
6769
self.extern_public.insert(def_id);
6870
if self.tcx.def_kind(def_id) == DefKind::Mod {
6971
self.visit_mod(def_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
// @count "$.index[*][?(@.inner.impl)]" 1
5+
// @!has "$.index[*][?(@.name == 'HiddenPubStruct')]"
6+
// @has "$.index[*][?(@.name == 'NotHiddenPubStruct')]"
7+
// @has "$.index[*][?(@.name=='PubTrait')]"
8+
pub trait PubTrait {}
9+
10+
#[doc(hidden)]
11+
pub struct HiddenPubStruct;
12+
pub struct NotHiddenPubStruct;
13+
14+
impl PubTrait for HiddenPubStruct {}
15+
impl PubTrait for NotHiddenPubStruct {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: --document-hidden-items
2+
3+
#![feature(no_core)]
4+
#![no_core]
5+
6+
// @has "$.index[*][?(@.name == 'HiddenPubStruct')]"
7+
// @has "$.index[*][?(@.inner.impl)]"
8+
// @has "$.index[*][?(@.name=='PubTrait')]"
9+
pub trait PubTrait {}
10+
11+
#[doc(hidden)]
12+
pub struct HiddenPubStruct;
13+
14+
impl PubTrait for HiddenPubStruct {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
// @count "$.index[*][?(@.inner.impl)]" 1
5+
// @!has "$.index[*][?(@.name == 'HiddenPubStruct')]"
6+
// @has "$.index[*][?(@.name == 'NotHiddenPubStruct')]"
7+
// @has "$.index[*][?(@.name=='PubTrait')]"
8+
pub trait PubTrait {}
9+
10+
#[doc(hidden)]
11+
pub mod hidden {
12+
pub struct HiddenPubStruct;
13+
14+
impl crate::PubTrait for HiddenPubStruct {}
15+
}
16+
17+
pub mod not_hidden {
18+
pub struct NotHiddenPubStruct;
19+
20+
impl crate::PubTrait for NotHiddenPubStruct {}
21+
}

0 commit comments

Comments
 (0)