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`
This commit is contained in:
bors 2023-07-18 02:47:03 +00:00
commit ec362f0ae8
16 changed files with 107 additions and 24 deletions

View File

@ -473,7 +473,7 @@ pub(crate) fn build_impl(
associated_trait.def_id, associated_trait.def_id,
) )
.unwrap(); // corresponding associated item has to exist .unwrap(); // corresponding associated item has to exist
!tcx.is_doc_hidden(trait_item.def_id) document_hidden || !tcx.is_doc_hidden(trait_item.def_id)
} else { } else {
item.visibility(tcx).is_public() item.visibility(tcx).is_public()
} }
@ -496,7 +496,7 @@ pub(crate) fn build_impl(
let mut stack: Vec<&Type> = vec![&for_]; let mut stack: Vec<&Type> = vec![&for_];
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) { if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
if tcx.is_doc_hidden(did) { if !document_hidden && tcx.is_doc_hidden(did) {
return; return;
} }
} }
@ -505,7 +505,7 @@ pub(crate) fn build_impl(
} }
while let Some(ty) = stack.pop() { while let Some(ty) = stack.pop() {
if let Some(did) = ty.def_id(&cx.cache) && tcx.is_doc_hidden(did) { if let Some(did) = ty.def_id(&cx.cache) && !document_hidden && tcx.is_doc_hidden(did) {
return; return;
} }
if let Some(generics) = ty.generics() { if let Some(generics) = ty.generics() {

View File

@ -53,7 +53,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
let mut inserted = FxHashSet::default(); let mut inserted = FxHashSet::default();
items.extend(doc.foreigns.iter().map(|(item, renamed)| { items.extend(doc.foreigns.iter().map(|(item, renamed)| {
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed); let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
if let Some(name) = item.name && !item.is_doc_hidden() { if let Some(name) = item.name && (cx.render_options.document_hidden || !item.is_doc_hidden()) {
inserted.insert((item.type_(), name)); inserted.insert((item.type_(), name));
} }
item item
@ -63,7 +63,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
return None; return None;
} }
let item = clean_doc_module(x, cx); let item = clean_doc_module(x, cx);
if item.is_doc_hidden() { if !cx.render_options.document_hidden && item.is_doc_hidden() {
// Hidden modules are stripped at a later stage. // Hidden modules are stripped at a later stage.
// If a hidden module has the same name as a visible one, we want // If a hidden module has the same name as a visible one, we want
// to keep both of them around. // to keep both of them around.
@ -84,7 +84,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
} }
let v = clean_maybe_renamed_item(cx, item, *renamed, *import_id); let v = clean_maybe_renamed_item(cx, item, *renamed, *import_id);
for item in &v { for item in &v {
if let Some(name) = item.name && !item.is_doc_hidden() { if let Some(name) = item.name && (cx.render_options.document_hidden || !item.is_doc_hidden()) {
inserted.insert((item.type_(), name)); inserted.insert((item.type_(), name));
} }
} }
@ -2326,7 +2326,7 @@ fn get_all_import_attributes<'hir>(
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect(); attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
first = false; first = false;
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`. // We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
} else if !cx.tcx.is_doc_hidden(def_id) { } else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id)); add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
} }
} }

View File

@ -73,7 +73,7 @@ fn should_not_trim() {
fn is_same_generic() { fn is_same_generic() {
use crate::clean::types::{PrimitiveType, Type}; use crate::clean::types::{PrimitiveType, Type};
use crate::formats::cache::Cache; use crate::formats::cache::Cache;
let cache = Cache::new(false); let cache = Cache::new(false, false);
let generic = Type::Generic(rustc_span::symbol::sym::Any); let generic = Type::Generic(rustc_span::symbol::sym::Any);
let unit = Type::Primitive(PrimitiveType::Unit); let unit = Type::Primitive(PrimitiveType::Unit);
assert!(!generic.is_doc_subtype_of(&unit, &cache)); assert!(!generic.is_doc_subtype_of(&unit, &cache));

View File

@ -345,7 +345,7 @@ pub(crate) fn run_global_ctxt(
impl_trait_bounds: Default::default(), impl_trait_bounds: Default::default(),
generated_synthetics: Default::default(), generated_synthetics: Default::default(),
auto_traits, auto_traits,
cache: Cache::new(render_options.document_private), cache: Cache::new(render_options.document_private, render_options.document_hidden),
inlined: FxHashSet::default(), inlined: FxHashSet::default(),
output_format, output_format,
render_options, render_options,

View File

@ -86,6 +86,9 @@ pub(crate) struct Cache {
/// Whether to document private items. /// Whether to document private items.
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions. /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
pub(crate) document_private: bool, pub(crate) document_private: bool,
/// Whether to document hidden items.
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
pub(crate) document_hidden: bool,
/// Crates marked with [`#[doc(masked)]`][doc_masked]. /// Crates marked with [`#[doc(masked)]`][doc_masked].
/// ///
@ -137,8 +140,8 @@ struct CacheBuilder<'a, 'tcx> {
} }
impl Cache { impl Cache {
pub(crate) fn new(document_private: bool) -> Self { pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
Cache { document_private, ..Cache::default() } Cache { document_private, document_hidden, ..Cache::default() }
} }
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was /// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was

View File

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

View File

@ -92,8 +92,8 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
return false; return false;
} }
if cx.tcx.is_doc_hidden(def_id.to_def_id()) if (!cx.render_options.document_hidden
|| inherits_doc_hidden(cx.tcx, def_id, None) && (cx.tcx.is_doc_hidden(def_id.to_def_id()) || inherits_doc_hidden(cx.tcx, def_id, None)))
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion() || cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
{ {
return false; return false;

View File

@ -42,6 +42,7 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
cache: &cx.cache, cache: &cx.cache,
is_json_output, is_json_output,
document_private: cx.render_options.document_private, document_private: cx.render_options.document_private,
document_hidden: cx.render_options.document_hidden,
}; };
stripper.fold_crate(krate) stripper.fold_crate(krate)
} }

View File

@ -13,5 +13,10 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate { pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
let is_json_output = cx.output_format.is_json() && !cx.show_coverage; let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(krate) ImportStripper {
tcx: cx.tcx,
is_json_output,
document_hidden: cx.render_options.document_hidden,
}
.fold_crate(krate)
} }

View File

@ -28,8 +28,12 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
is_json_output, is_json_output,
tcx: cx.tcx, tcx: cx.tcx,
}; };
krate = krate = ImportStripper {
ImportStripper { tcx: cx.tcx, is_json_output }.fold_crate(stripper.fold_crate(krate)); tcx: cx.tcx,
is_json_output,
document_hidden: cx.render_options.document_hidden,
}
.fold_crate(stripper.fold_crate(krate));
} }
// strip all impls referencing private items // strip all impls referencing private items
@ -39,6 +43,7 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
cache: &cx.cache, cache: &cx.cache,
is_json_output, is_json_output,
document_private: cx.render_options.document_private, document_private: cx.render_options.document_private,
document_hidden: cx.render_options.document_hidden,
}; };
stripper.fold_crate(krate) stripper.fold_crate(krate)
} }

View File

@ -6,6 +6,7 @@ use std::mem;
use crate::clean::{self, Item, ItemId, ItemIdSet}; use crate::clean::{self, Item, ItemId, ItemIdSet};
use crate::fold::{strip_item, DocFolder}; use crate::fold::{strip_item, DocFolder};
use crate::formats::cache::Cache; use crate::formats::cache::Cache;
use crate::visit_ast::inherits_doc_hidden;
use crate::visit_lib::RustdocEffectiveVisibilities; use crate::visit_lib::RustdocEffectiveVisibilities;
pub(crate) struct Stripper<'a, 'tcx> { pub(crate) struct Stripper<'a, 'tcx> {
@ -151,6 +152,7 @@ pub(crate) struct ImplStripper<'a, 'tcx> {
pub(crate) cache: &'a Cache, pub(crate) cache: &'a Cache,
pub(crate) is_json_output: bool, pub(crate) is_json_output: bool,
pub(crate) document_private: bool, pub(crate) document_private: bool,
pub(crate) document_hidden: bool,
} }
impl<'a> ImplStripper<'a, '_> { impl<'a> ImplStripper<'a, '_> {
@ -162,7 +164,13 @@ impl<'a> ImplStripper<'a, '_> {
// If the "for" item is exported and the impl block isn't `#[doc(hidden)]`, then we // If the "for" item is exported and the impl block isn't `#[doc(hidden)]`, then we
// need to keep it. // need to keep it.
self.cache.effective_visibilities.is_exported(self.tcx, for_def_id) self.cache.effective_visibilities.is_exported(self.tcx, for_def_id)
&& !item.is_doc_hidden() && (self.document_hidden
|| ((!item.is_doc_hidden()
&& for_def_id
.as_local()
.map(|def_id| !inherits_doc_hidden(self.tcx, def_id, None))
.unwrap_or(true))
|| self.cache.inlined_items.contains(&for_def_id)))
} else { } else {
false false
} }
@ -231,6 +239,7 @@ impl<'a> DocFolder for ImplStripper<'a, '_> {
pub(crate) struct ImportStripper<'tcx> { pub(crate) struct ImportStripper<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>, pub(crate) tcx: TyCtxt<'tcx>,
pub(crate) is_json_output: bool, pub(crate) is_json_output: bool,
pub(crate) document_hidden: bool,
} }
impl<'tcx> ImportStripper<'tcx> { impl<'tcx> ImportStripper<'tcx> {
@ -247,8 +256,12 @@ impl<'tcx> ImportStripper<'tcx> {
impl<'tcx> DocFolder for ImportStripper<'tcx> { impl<'tcx> DocFolder for ImportStripper<'tcx> {
fn fold_item(&mut self, i: Item) -> Option<Item> { fn fold_item(&mut self, i: Item) -> Option<Item> {
match *i.kind { match *i.kind {
clean::ImportItem(imp) if self.import_should_be_hidden(&i, &imp) => None, clean::ImportItem(imp)
clean::ImportItem(_) if i.is_doc_hidden() => None, if !self.document_hidden && self.import_should_be_hidden(&i, &imp) =>
{
None
}
// clean::ImportItem(_) if !self.document_hidden && i.is_doc_hidden() => None,
clean::ExternCrateItem { .. } | clean::ImportItem(..) clean::ExternCrateItem { .. } | clean::ImportItem(..)
if i.visibility(self.tcx) != Some(Visibility::Public) => if i.visibility(self.tcx) != Some(Visibility::Public) =>
{ {

View File

@ -262,10 +262,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
return false; return false;
}; };
let document_hidden = self.cx.render_options.document_hidden;
let use_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id)); let use_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage. // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline) let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
|| use_attrs.lists(sym::doc).has_word(sym::hidden); || (document_hidden && use_attrs.lists(sym::doc).has_word(sym::hidden));
if is_no_inline { if is_no_inline {
return false; return false;
@ -285,11 +286,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
}; };
let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did); let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did);
let is_hidden = tcx.is_doc_hidden(ori_res_did); let is_hidden = !document_hidden && tcx.is_doc_hidden(ori_res_did);
let item = tcx.hir().get_by_def_id(res_did); let item = tcx.hir().get_by_def_id(res_did);
if !please_inline { if !please_inline {
let inherits_hidden = inherits_doc_hidden(tcx, res_did, None); let inherits_hidden = !document_hidden && inherits_doc_hidden(tcx, res_did, None);
// Only inline if requested or if the item would otherwise be stripped. // Only inline if requested or if the item would otherwise be stripped.
if (!is_private && !inherits_hidden) || ( if (!is_private && !inherits_hidden) || (
is_hidden && is_hidden &&
@ -359,6 +360,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
import_def_id: LocalDefId, import_def_id: LocalDefId,
target_def_id: LocalDefId, target_def_id: LocalDefId,
) -> bool { ) -> bool {
if self.cx.render_options.document_hidden {
return true;
}
let tcx = self.cx.tcx; let tcx = self.cx.tcx;
let item_def_id = reexport_chain(tcx, import_def_id, target_def_id) let item_def_id = reexport_chain(tcx, import_def_id, target_def_id)
.iter() .iter()

View File

@ -33,6 +33,7 @@ pub(crate) fn lib_embargo_visit_item(cx: &mut DocContext<'_>, def_id: DefId) {
tcx: cx.tcx, tcx: cx.tcx,
extern_public: &mut cx.cache.effective_visibilities.extern_public, extern_public: &mut cx.cache.effective_visibilities.extern_public,
visited_mods: Default::default(), visited_mods: Default::default(),
document_hidden: cx.render_options.document_hidden,
} }
.visit_item(def_id) .visit_item(def_id)
} }
@ -45,6 +46,7 @@ struct LibEmbargoVisitor<'a, 'tcx> {
extern_public: &'a mut DefIdSet, extern_public: &'a mut DefIdSet,
// Keeps track of already visited modules, in case a module re-exports its parent // Keeps track of already visited modules, in case a module re-exports its parent
visited_mods: DefIdSet, visited_mods: DefIdSet,
document_hidden: bool,
} }
impl LibEmbargoVisitor<'_, '_> { impl LibEmbargoVisitor<'_, '_> {
@ -63,7 +65,7 @@ impl LibEmbargoVisitor<'_, '_> {
} }
fn visit_item(&mut self, def_id: DefId) { fn visit_item(&mut self, def_id: DefId) {
if !self.tcx.is_doc_hidden(def_id) { if self.document_hidden || !self.tcx.is_doc_hidden(def_id) {
self.extern_public.insert(def_id); self.extern_public.insert(def_id);
if self.tcx.def_kind(def_id) == DefKind::Mod { if self.tcx.def_kind(def_id) == DefKind::Mod {
self.visit_mod(def_id); self.visit_mod(def_id);

View File

@ -0,0 +1,15 @@
#![feature(no_core)]
#![no_core]
// @count "$.index[*][?(@.inner.impl)]" 1
// @!has "$.index[*][?(@.name == 'HiddenPubStruct')]"
// @has "$.index[*][?(@.name == 'NotHiddenPubStruct')]"
// @has "$.index[*][?(@.name=='PubTrait')]"
pub trait PubTrait {}
#[doc(hidden)]
pub struct HiddenPubStruct;
pub struct NotHiddenPubStruct;
impl PubTrait for HiddenPubStruct {}
impl PubTrait for NotHiddenPubStruct {}

View File

@ -0,0 +1,14 @@
// compile-flags: --document-hidden-items
#![feature(no_core)]
#![no_core]
// @has "$.index[*][?(@.name == 'HiddenPubStruct')]"
// @has "$.index[*][?(@.inner.impl)]"
// @has "$.index[*][?(@.name=='PubTrait')]"
pub trait PubTrait {}
#[doc(hidden)]
pub struct HiddenPubStruct;
impl PubTrait for HiddenPubStruct {}

View File

@ -0,0 +1,21 @@
#![feature(no_core)]
#![no_core]
// @count "$.index[*][?(@.inner.impl)]" 1
// @!has "$.index[*][?(@.name == 'HiddenPubStruct')]"
// @has "$.index[*][?(@.name == 'NotHiddenPubStruct')]"
// @has "$.index[*][?(@.name=='PubTrait')]"
pub trait PubTrait {}
#[doc(hidden)]
pub mod hidden {
pub struct HiddenPubStruct;
impl crate::PubTrait for HiddenPubStruct {}
}
pub mod not_hidden {
pub struct NotHiddenPubStruct;
impl crate::PubTrait for NotHiddenPubStruct {}
}