Correctly handle usage of private items in public API for JSON output format

This commit is contained in:
Guillaume Gomez 2022-07-15 18:10:52 +02:00
parent 1a15c7147f
commit 3c55a26cb4
3 changed files with 22 additions and 4 deletions

View File

@ -81,6 +81,8 @@ pub(crate) struct DocContext<'tcx> {
pub(crate) inlined: FxHashSet<ItemId>, pub(crate) inlined: FxHashSet<ItemId>,
/// Used by `calculate_doc_coverage`. /// Used by `calculate_doc_coverage`.
pub(crate) output_format: OutputFormat, pub(crate) output_format: OutputFormat,
/// Used by `strip_private`.
pub(crate) show_coverage: bool,
} }
impl<'tcx> DocContext<'tcx> { impl<'tcx> DocContext<'tcx> {
@ -381,6 +383,7 @@ pub(crate) fn run_global_ctxt(
inlined: FxHashSet::default(), inlined: FxHashSet::default(),
output_format, output_format,
render_options, render_options,
show_coverage,
}; };
// Small hack to force the Sized trait to be present. // Small hack to force the Sized trait to be present.

View File

@ -24,6 +24,7 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
retained: &mut retained, retained: &mut retained,
access_levels: &cx.cache.access_levels, access_levels: &cx.cache.access_levels,
update_retained: true, update_retained: true,
is_json_output: cx.output_format.is_json() && !cx.show_coverage,
}; };
krate = ImportStripper.fold_crate(stripper.fold_crate(krate)); krate = ImportStripper.fold_crate(stripper.fold_crate(krate));
} }

View File

@ -3,7 +3,7 @@
use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::middle::privacy::AccessLevels;
use std::mem; use std::mem;
use crate::clean::{self, Item, 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;
@ -11,6 +11,21 @@ pub(crate) struct Stripper<'a> {
pub(crate) retained: &'a mut ItemIdSet, pub(crate) retained: &'a mut ItemIdSet,
pub(crate) access_levels: &'a AccessLevels<DefId>, pub(crate) access_levels: &'a AccessLevels<DefId>,
pub(crate) update_retained: bool, pub(crate) update_retained: bool,
pub(crate) is_json_output: bool,
}
impl<'a> Stripper<'a> {
// We need to handle this differently for the JSON output because some non exported items could
// be used in public API. And so, we need these items as well. `is_exported` only checks if they
// are in the public API, which is not enough.
#[inline]
fn is_item_reachable(&self, item_id: ItemId) -> bool {
if self.is_json_output {
self.access_levels.is_reachable(item_id.expect_def_id())
} else {
self.access_levels.is_exported(item_id.expect_def_id())
}
}
} }
impl<'a> DocFolder for Stripper<'a> { impl<'a> DocFolder for Stripper<'a> {
@ -45,9 +60,8 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
| clean::TraitAliasItem(..) | clean::TraitAliasItem(..)
| clean::MacroItem(..) | clean::MacroItem(..)
| clean::ForeignTypeItem => { | clean::ForeignTypeItem => {
if i.item_id.is_local() let item_id = i.item_id;
&& !self.access_levels.is_exported(i.item_id.expect_def_id()) if item_id.is_local() && !self.is_item_reachable(item_id) {
{
debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name); debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
return None; return None;
} }