1 use rustc_hir::def_id::DefId;
2 use rustc_middle::middle::privacy::AccessLevels;
5 use crate::clean::{self, GetDefId, Item, ItemIdSet};
6 use crate::fold::{strip_item, DocFolder};
8 crate struct Stripper<'a> {
9 crate retained: &'a mut ItemIdSet,
10 crate access_levels: &'a AccessLevels<DefId>,
11 crate update_retained: bool,
14 impl<'a> DocFolder for Stripper<'a> {
15 fn fold_item(&mut self, i: Item) -> Option<Item> {
17 clean::StrippedItem(..) => {
18 // We need to recurse into stripped modules to strip things
19 // like impl methods but when doing so we must not add any
20 // items to the `retained` set.
21 debug!("Stripper: recursing into stripped {:?} {:?}", i.type_(), i.name);
22 let old = mem::replace(&mut self.update_retained, false);
23 let ret = self.fold_item_recur(i);
24 self.update_retained = old;
27 // These items can all get re-exported
28 clean::OpaqueTyItem(..)
29 | clean::TypedefItem(..)
30 | clean::StaticItem(..)
31 | clean::StructItem(..)
33 | clean::TraitItem(..)
34 | clean::FunctionItem(..)
35 | clean::VariantItem(..)
36 | clean::MethodItem(..)
37 | clean::ForeignFunctionItem(..)
38 | clean::ForeignStaticItem(..)
39 | clean::ConstantItem(..)
40 | clean::UnionItem(..)
41 | clean::AssocConstItem(..)
42 | clean::TraitAliasItem(..)
43 | clean::MacroItem(..)
44 | clean::ForeignTypeItem => {
45 if i.def_id.is_local() {
46 if !self.access_levels.is_exported(i.def_id.expect_def_id()) {
47 debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
53 clean::StructFieldItem(..) => {
54 if !i.visibility.is_public() {
55 return Some(strip_item(i));
59 clean::ModuleItem(..) => {
60 if i.def_id.is_local() && !i.visibility.is_public() {
61 debug!("Stripper: stripping module {:?}", i.name);
62 let old = mem::replace(&mut self.update_retained, false);
63 let ret = strip_item(self.fold_item_recur(i));
64 self.update_retained = old;
69 // handled in the `strip-priv-imports` pass
70 clean::ExternCrateItem { .. } | clean::ImportItem(..) => {}
72 clean::ImplItem(..) => {}
74 // tymethods have no control over privacy
75 clean::TyMethodItem(..) => {}
77 // Proc-macros are always public
78 clean::ProcMacroItem(..) => {}
80 // Primitives are never stripped
81 clean::PrimitiveItem(..) => {}
83 // Associated types are never stripped
84 clean::AssocTypeItem(..) => {}
86 // Keywords are never stripped
87 clean::KeywordItem(..) => {}
90 let fastreturn = match *i.kind {
91 // nothing left to do for traits (don't want to filter their
92 // methods out, visibility controlled by the trait)
93 clean::TraitItem(..) => true,
95 // implementations of traits are always public.
96 clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
97 // Variant fields have inherited visibility
98 clean::VariantItem(clean::Variant::Struct(..) | clean::Variant::Tuple(..)) => true,
102 let i = if fastreturn {
103 if self.update_retained {
104 self.retained.insert(i.def_id);
108 self.fold_item_recur(i)
111 if self.update_retained {
112 self.retained.insert(i.def_id);
118 /// This stripper discards all impls which reference stripped items
119 crate struct ImplStripper<'a> {
120 crate retained: &'a ItemIdSet,
123 impl<'a> DocFolder for ImplStripper<'a> {
124 fn fold_item(&mut self, i: Item) -> Option<Item> {
125 if let clean::ImplItem(ref imp) = *i.kind {
126 // emptied none trait impls can be stripped
127 if imp.trait_.is_none() && imp.items.is_empty() {
130 if let Some(did) = imp.for_.def_id() {
131 if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
133 debug!("ImplStripper: impl item for stripped type; removing");
137 if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
138 if did.is_local() && !self.retained.contains(&did.into()) {
139 debug!("ImplStripper: impl item for stripped trait; removing");
143 if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
144 for typaram in generics {
145 if let Some(did) = typaram.def_id() {
146 if did.is_local() && !self.retained.contains(&did.into()) {
148 "ImplStripper: stripped item in trait's generics; removing impl"
156 Some(self.fold_item_recur(i))
160 /// This stripper discards all private import statements (`use`, `extern crate`)
161 crate struct ImportStripper;
163 impl DocFolder for ImportStripper {
164 fn fold_item(&mut self, i: Item) -> Option<Item> {
166 clean::ExternCrateItem { .. } | clean::ImportItem(..) if !i.visibility.is_public() => {
169 _ => Some(self.fold_item_recur(i)),