1 //! A collection of utility functions for the `strip_*` passes.
2 use rustc_hir::def_id::DefId;
3 use rustc_middle::middle::privacy::AccessLevels;
6 use crate::clean::{self, Item, ItemId, ItemIdSet};
7 use crate::fold::{strip_item, DocFolder};
8 use crate::formats::cache::Cache;
10 pub(crate) struct Stripper<'a> {
11 pub(crate) retained: &'a mut ItemIdSet,
12 pub(crate) access_levels: &'a AccessLevels<DefId>,
13 pub(crate) update_retained: bool,
14 pub(crate) is_json_output: bool,
17 impl<'a> Stripper<'a> {
18 // We need to handle this differently for the JSON output because some non exported items could
19 // be used in public API. And so, we need these items as well. `is_exported` only checks if they
20 // are in the public API, which is not enough.
22 fn is_item_reachable(&self, item_id: ItemId) -> bool {
23 if self.is_json_output {
24 self.access_levels.is_reachable(item_id.expect_def_id())
26 self.access_levels.is_exported(item_id.expect_def_id())
31 impl<'a> DocFolder for Stripper<'a> {
32 fn fold_item(&mut self, i: Item) -> Option<Item> {
34 clean::StrippedItem(..) => {
35 // We need to recurse into stripped modules to strip things
36 // like impl methods but when doing so we must not add any
37 // items to the `retained` set.
38 debug!("Stripper: recursing into stripped {:?} {:?}", i.type_(), i.name);
39 let old = mem::replace(&mut self.update_retained, false);
40 let ret = self.fold_item_recur(i);
41 self.update_retained = old;
44 // These items can all get re-exported
45 clean::OpaqueTyItem(..)
46 | clean::TypedefItem(..)
47 | clean::StaticItem(..)
48 | clean::StructItem(..)
50 | clean::TraitItem(..)
51 | clean::FunctionItem(..)
52 | clean::VariantItem(..)
53 | clean::MethodItem(..)
54 | clean::ForeignFunctionItem(..)
55 | clean::ForeignStaticItem(..)
56 | clean::ConstantItem(..)
57 | clean::UnionItem(..)
58 | clean::AssocConstItem(..)
59 | clean::AssocTypeItem(..)
60 | clean::TraitAliasItem(..)
61 | clean::MacroItem(..)
62 | clean::ForeignTypeItem => {
63 let item_id = i.item_id;
64 if item_id.is_local() && !self.is_item_reachable(item_id) {
65 debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
70 clean::StructFieldItem(..) => {
71 if !i.visibility.is_public() {
72 return Some(strip_item(i));
76 clean::ModuleItem(..) => {
77 if i.item_id.is_local() && !i.visibility.is_public() {
78 debug!("Stripper: stripping module {:?}", i.name);
79 let old = mem::replace(&mut self.update_retained, false);
80 let ret = strip_item(self.fold_item_recur(i));
81 self.update_retained = old;
86 // handled in the `strip-priv-imports` pass
87 clean::ExternCrateItem { .. } | clean::ImportItem(..) => {}
89 clean::ImplItem(..) => {}
91 // tymethods etc. have no control over privacy
92 clean::TyMethodItem(..) | clean::TyAssocConstItem(..) | clean::TyAssocTypeItem(..) => {}
94 // Proc-macros are always public
95 clean::ProcMacroItem(..) => {}
97 // Primitives are never stripped
98 clean::PrimitiveItem(..) => {}
100 // Keywords are never stripped
101 clean::KeywordItem => {}
104 let fastreturn = match *i.kind {
105 // nothing left to do for traits (don't want to filter their
106 // methods out, visibility controlled by the trait)
107 clean::TraitItem(..) => true,
109 // implementations of traits are always public.
110 clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
111 // Variant fields have inherited visibility
112 clean::VariantItem(clean::Variant::Struct(..) | clean::Variant::Tuple(..)) => true,
116 let i = if fastreturn {
117 if self.update_retained {
118 self.retained.insert(i.item_id);
122 self.fold_item_recur(i)
125 if self.update_retained {
126 self.retained.insert(i.item_id);
132 /// This stripper discards all impls which reference stripped items
133 pub(crate) struct ImplStripper<'a> {
134 pub(crate) retained: &'a ItemIdSet,
135 pub(crate) cache: &'a Cache,
138 impl<'a> DocFolder for ImplStripper<'a> {
139 fn fold_item(&mut self, i: Item) -> Option<Item> {
140 if let clean::ImplItem(ref imp) = *i.kind {
141 // Impl blocks can be skipped if they are: empty; not a trait impl; and have no
143 if imp.trait_.is_none() && imp.items.is_empty() && i.doc_value().is_none() {
146 if let Some(did) = imp.for_.def_id(self.cache) {
147 if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
149 debug!("ImplStripper: impl item for stripped type; removing");
153 if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
154 if did.is_local() && !self.retained.contains(&did.into()) {
155 debug!("ImplStripper: impl item for stripped trait; removing");
159 if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
160 for typaram in generics {
161 if let Some(did) = typaram.def_id(self.cache) {
162 if did.is_local() && !self.retained.contains(&did.into()) {
164 "ImplStripper: stripped item in trait's generics; removing impl"
172 Some(self.fold_item_recur(i))
176 /// This stripper discards all private import statements (`use`, `extern crate`)
177 pub(crate) struct ImportStripper;
179 impl DocFolder for ImportStripper {
180 fn fold_item(&mut self, i: Item) -> Option<Item> {
182 clean::ExternCrateItem { .. } | clean::ImportItem(..) if !i.visibility.is_public() => {
185 _ => Some(self.fold_item_recur(i)),