Skip to content

Commit e89a2cc

Browse files
committed
Always hide private fields in aliased type
1 parent cb93c24 commit e89a2cc

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

src/librustdoc/passes/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::core::DocContext;
88
mod stripper;
99
pub(crate) use stripper::*;
1010

11+
mod strip_aliased_non_local;
12+
pub(crate) use self::strip_aliased_non_local::STRIP_ALIASED_NON_LOCAL;
13+
1114
mod strip_hidden;
1215
pub(crate) use self::strip_hidden::STRIP_HIDDEN;
1316

@@ -71,6 +74,7 @@ pub(crate) enum Condition {
7174
pub(crate) const PASSES: &[Pass] = &[
7275
CHECK_CUSTOM_CODE_CLASSES,
7376
CHECK_DOC_TEST_VISIBILITY,
77+
STRIP_ALIASED_NON_LOCAL,
7478
STRIP_HIDDEN,
7579
STRIP_PRIVATE,
7680
STRIP_PRIV_IMPORTS,
@@ -86,6 +90,7 @@ pub(crate) const DEFAULT_PASSES: &[ConditionalPass] = &[
8690
ConditionalPass::always(CHECK_CUSTOM_CODE_CLASSES),
8791
ConditionalPass::always(COLLECT_TRAIT_IMPLS),
8892
ConditionalPass::always(CHECK_DOC_TEST_VISIBILITY),
93+
ConditionalPass::always(STRIP_ALIASED_NON_LOCAL),
8994
ConditionalPass::new(STRIP_HIDDEN, WhenNotDocumentHidden),
9095
ConditionalPass::new(STRIP_PRIVATE, WhenNotDocumentPrivate),
9196
ConditionalPass::new(STRIP_PRIV_IMPORTS, WhenDocumentPrivate),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use rustc_middle::ty::TyCtxt;
2+
use rustc_middle::ty::Visibility;
3+
4+
use crate::clean;
5+
use crate::clean::Item;
6+
use crate::core::DocContext;
7+
use crate::fold::{strip_item, DocFolder};
8+
use crate::passes::Pass;
9+
10+
pub(crate) const STRIP_ALIASED_NON_LOCAL: Pass = Pass {
11+
name: "strip-aliased-non-local",
12+
run: strip_aliased_non_local,
13+
description: "strips all non-local private aliased items from the output",
14+
};
15+
16+
fn strip_aliased_non_local(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
17+
let mut stripper = AliasedNonLocalStripper { tcx: cx.tcx };
18+
stripper.fold_crate(krate)
19+
}
20+
21+
struct AliasedNonLocalStripper<'tcx> {
22+
tcx: TyCtxt<'tcx>,
23+
}
24+
25+
impl<'tcx> DocFolder for AliasedNonLocalStripper<'tcx> {
26+
fn fold_item(&mut self, i: Item) -> Option<Item> {
27+
Some(match *i.kind {
28+
clean::TypeAliasItem(..) => {
29+
let mut stripper = NonLocalStripper { tcx: self.tcx };
30+
// don't call `fold_item` as that could strip the type-alias it-self
31+
// which we don't want to strip out
32+
stripper.fold_item_recur(i)
33+
}
34+
_ => self.fold_item_recur(i),
35+
})
36+
}
37+
}
38+
39+
struct NonLocalStripper<'tcx> {
40+
tcx: TyCtxt<'tcx>,
41+
}
42+
43+
impl<'tcx> DocFolder for NonLocalStripper<'tcx> {
44+
fn fold_item(&mut self, i: Item) -> Option<Item> {
45+
// If not local, we want to respect the original visibility of
46+
// the field and not the one given by the user for the currrent crate.
47+
//
48+
// FIXME(#125009): Not-local should probably consider same Cargo workspace
49+
if !i.def_id().map_or(true, |did| did.is_local()) {
50+
if i.visibility(self.tcx) != Some(Visibility::Public) || i.is_doc_hidden() {
51+
return Some(strip_item(i));
52+
}
53+
}
54+
55+
Some(self.fold_item_recur(i))
56+
}
57+
}

tests/rustdoc-ui/issues/issue-91713.stdout

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Available passes for running rustdoc:
22
check-custom-code-classes - check for custom code classes without the feature-gate enabled
33
check_doc_test_visibility - run various visibility-related lints on doctests
4+
strip-aliased-non-local - strips all non-local private aliased items from the output
45
strip-hidden - strips all `#[doc(hidden)]` items from the output
56
strip-private - strips all private items from a crate which cannot be seen externally, implies strip-priv-imports
67
strip-priv-imports - strips all private import statements (`use`, `extern crate`) from a crate
@@ -14,6 +15,7 @@ Default passes for rustdoc:
1415
check-custom-code-classes
1516
collect-trait-impls
1617
check_doc_test_visibility
18+
strip-aliased-non-local
1719
strip-hidden (when not --document-hidden-items)
1820
strip-private (when not --document-private-items)
1921
strip-priv-imports (when --document-private-items)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! This test makes sure that with never show the inner fields in the
2+
//! aliased type view of type alias.
3+
4+
//@ compile-flags: -Z unstable-options --document-private-items
5+
6+
#![crate_name = "foo"]
7+
8+
use std::collections::BTreeMap;
9+
10+
// @has 'foo/type.FooBar.html' '//*[@class="rust item-decl"]/code' 'struct FooBar { /* private fields */ }'
11+
pub type FooBar = BTreeMap<u32, String>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! This test makes sure that with never show the inner fields in the
2+
//! aliased type view of type alias.
3+
4+
#![crate_name = "foo"]
5+
6+
use std::collections::BTreeMap;
7+
8+
// @has 'foo/type.FooBar.html' '//*[@class="rust item-decl"]/code' 'struct FooBar { /* private fields */ }'
9+
pub type FooBar = BTreeMap<u32, String>;

0 commit comments

Comments
 (0)