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, ItemIdSet};
7 use crate::fold::{strip_item, DocFolder};
9 crate struct Stripper<'a> {
10 crate retained: &'a mut ItemIdSet,
11 crate access_levels: &'a AccessLevels<DefId>,
12 crate update_retained: bool,
15 impl<'a> DocFolder for Stripper<'a> {
16 fn fold_item(&mut self, i: Item) -> Option<Item> {
18 clean::StrippedItem(..) => {
19 // We need to recurse into stripped modules to strip things
20 // like impl methods but when doing so we must not add any
21 // items to the `retained` set.
22 debug!("Stripper: recursing into stripped {:?} {:?}", i.type_(), i.name);
23 let old = mem::replace(&mut self.update_retained, false);
24 let ret = self.fold_item_recur(i);
25 self.update_retained = old;
28 // These items can all get re-exported
29 clean::OpaqueTyItem(..)
30 | clean::TypedefItem(..)
31 | clean::StaticItem(..)
32 | clean::StructItem(..)
34 | clean::TraitItem(..)
35 | clean::FunctionItem(..)
36 | clean::VariantItem(..)
37 | clean::MethodItem(..)
38 | clean::ForeignFunctionItem(..)
39 | clean::ForeignStaticItem(..)
40 | clean::ConstantItem(..)
41 | clean::UnionItem(..)
42 | clean::AssocConstItem(..)
43 | clean::TraitAliasItem(..)
44 | clean::MacroItem(..)
45 | clean::ForeignTypeItem => {
46 if i.def_id.is_local() {
47 if !self.access_levels.is_exported(i.def_id.expect_def_id()) {
48 debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
54 clean::StructFieldItem(..) => {
55 if !i.visibility.is_public() {
56 return Some(strip_item(i));
60 clean::ModuleItem(..) => {
61 if i.def_id.is_local() && !i.visibility.is_public() {
62 debug!("Stripper: stripping module {:?}", i.name);
63 let old = mem::replace(&mut self.update_retained, false);
64 let ret = strip_item(self.fold_item_recur(i));
65 self.update_retained = old;
70 // handled in the `strip-priv-imports` pass
71 clean::ExternCrateItem { .. } | clean::ImportItem(..) => {}
73 clean::ImplItem(..) => {}
75 // tymethods have no control over privacy
76 clean::TyMethodItem(..) => {}
78 // Proc-macros are always public
79 clean::ProcMacroItem(..) => {}
81 // Primitives are never stripped
82 clean::PrimitiveItem(..) => {}
84 // Associated types are never stripped
85 clean::AssocTypeItem(..) => {}
87 // Keywords are never stripped
88 clean::KeywordItem(..) => {}
91 let fastreturn = match *i.kind {
92 // nothing left to do for traits (don't want to filter their
93 // methods out, visibility controlled by the trait)
94 clean::TraitItem(..) => true,
96 // implementations of traits are always public.
97 clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
98 // Variant fields have inherited visibility
99 clean::VariantItem(clean::Variant::Struct(..) | clean::Variant::Tuple(..)) => true,
103 let i = if fastreturn {
104 if self.update_retained {
105 self.retained.insert(i.def_id);
109 self.fold_item_recur(i)
112 if self.update_retained {
113 self.retained.insert(i.def_id);
119 /// This stripper discards all impls which reference stripped items
120 crate struct ImplStripper<'a> {
121 crate retained: &'a ItemIdSet,
124 impl<'a> DocFolder for ImplStripper<'a> {
125 fn fold_item(&mut self, i: Item) -> Option<Item> {
126 if let clean::ImplItem(ref imp) = *i.kind {
127 // emptied none trait impls can be stripped
128 if imp.trait_.is_none() && imp.items.is_empty() {
131 if let Some(did) = imp.for_.def_id_no_primitives() {
132 if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
134 debug!("ImplStripper: impl item for stripped type; removing");
138 if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
139 if did.is_local() && !self.retained.contains(&did.into()) {
140 debug!("ImplStripper: impl item for stripped trait; removing");
144 if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
145 for typaram in generics {
146 if let Some(did) = typaram.def_id_no_primitives() {
147 if did.is_local() && !self.retained.contains(&did.into()) {
149 "ImplStripper: stripped item in trait's generics; removing impl"
157 Some(self.fold_item_recur(i))
161 /// This stripper discards all private import statements (`use`, `extern crate`)
162 crate struct ImportStripper;
164 impl DocFolder for ImportStripper {
165 fn fold_item(&mut self, i: Item) -> Option<Item> {
167 clean::ExternCrateItem { .. } | clean::ImportItem(..) if !i.visibility.is_public() => {
170 _ => Some(self.fold_item_recur(i)),