]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/stripper.rs
Rollup merge of #79120 - calebcartwright:update-rustfmt, r=Mark-Simulacrum
[rust.git] / src / librustdoc / passes / stripper.rs
1 use rustc_hir::def_id::{DefId, DefIdSet};
2 use rustc_middle::middle::privacy::AccessLevels;
3 use std::mem;
4
5 use crate::clean::{self, GetDefId, Item};
6 use crate::fold::{DocFolder, StripItem};
7
8 crate struct Stripper<'a> {
9     crate retained: &'a mut DefIdSet,
10     crate access_levels: &'a AccessLevels<DefId>,
11     crate update_retained: bool,
12 }
13
14 impl<'a> DocFolder for Stripper<'a> {
15     fn fold_item(&mut self, i: Item) -> Option<Item> {
16         match i.kind {
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;
25                 return ret;
26             }
27             // These items can all get re-exported
28             clean::OpaqueTyItem(..)
29             | clean::TypedefItem(..)
30             | clean::StaticItem(..)
31             | clean::StructItem(..)
32             | clean::EnumItem(..)
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::ForeignTypeItem => {
44                 if i.def_id.is_local() {
45                     if !self.access_levels.is_exported(i.def_id) {
46                         debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
47                         return None;
48                     }
49                 }
50             }
51
52             clean::StructFieldItem(..) => {
53                 if i.visibility != clean::Public {
54                     return StripItem(i).strip();
55                 }
56             }
57
58             clean::ModuleItem(..) => {
59                 if i.def_id.is_local() && i.visibility != clean::Public {
60                     debug!("Stripper: stripping module {:?}", i.name);
61                     let old = mem::replace(&mut self.update_retained, false);
62                     let ret = StripItem(self.fold_item_recur(i).unwrap()).strip();
63                     self.update_retained = old;
64                     return ret;
65                 }
66             }
67
68             // handled in the `strip-priv-imports` pass
69             clean::ExternCrateItem(..) | clean::ImportItem(..) => {}
70
71             clean::ImplItem(..) => {}
72
73             // tymethods/macros have no control over privacy
74             clean::MacroItem(..) | clean::TyMethodItem(..) => {}
75
76             // Proc-macros are always public
77             clean::ProcMacroItem(..) => {}
78
79             // Primitives are never stripped
80             clean::PrimitiveItem(..) => {}
81
82             // Associated types are never stripped
83             clean::AssocTypeItem(..) => {}
84
85             // Keywords are never stripped
86             clean::KeywordItem(..) => {}
87         }
88
89         let fastreturn = match i.kind {
90             // nothing left to do for traits (don't want to filter their
91             // methods out, visibility controlled by the trait)
92             clean::TraitItem(..) => true,
93
94             // implementations of traits are always public.
95             clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
96             // Struct variant fields have inherited visibility
97             clean::VariantItem(clean::Variant { kind: clean::VariantKind::Struct(..) }) => true,
98             _ => false,
99         };
100
101         let i = if fastreturn {
102             if self.update_retained {
103                 self.retained.insert(i.def_id);
104             }
105             return Some(i);
106         } else {
107             self.fold_item_recur(i)
108         };
109
110         if let Some(ref i) = i {
111             if self.update_retained {
112                 self.retained.insert(i.def_id);
113             }
114         }
115         i
116     }
117 }
118
119 /// This stripper discards all impls which reference stripped items
120 crate struct ImplStripper<'a> {
121     crate retained: &'a DefIdSet,
122 }
123
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() {
129                 return None;
130             }
131             if let Some(did) = imp.for_.def_id() {
132                 if did.is_local() && !imp.for_.is_generic() && !self.retained.contains(&did) {
133                     debug!("ImplStripper: impl item for stripped type; removing");
134                     return None;
135                 }
136             }
137             if let Some(did) = imp.trait_.def_id() {
138                 if did.is_local() && !self.retained.contains(&did) {
139                     debug!("ImplStripper: impl item for stripped trait; removing");
140                     return None;
141                 }
142             }
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) {
147                             debug!(
148                                 "ImplStripper: stripped item in trait's generics; removing impl"
149                             );
150                             return None;
151                         }
152                     }
153                 }
154             }
155         }
156         self.fold_item_recur(i)
157     }
158 }
159
160 /// This stripper discards all private import statements (`use`, `extern crate`)
161 crate struct ImportStripper;
162
163 impl DocFolder for ImportStripper {
164     fn fold_item(&mut self, i: Item) -> Option<Item> {
165         match i.kind {
166             clean::ExternCrateItem(..) | clean::ImportItem(..) if i.visibility != clean::Public => {
167                 None
168             }
169             _ => self.fold_item_recur(i),
170         }
171     }
172 }