]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/collect_trait_impls.rs
remove unused return types such as empty Results or Options that would always be...
[rust.git] / src / librustdoc / passes / collect_trait_impls.rs
1 use super::Pass;
2 use crate::clean::*;
3 use crate::core::DocContext;
4 use crate::fold::DocFolder;
5
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8 use rustc_middle::ty::DefIdTree;
9 use rustc_span::symbol::sym;
10
11 crate const COLLECT_TRAIT_IMPLS: Pass = Pass {
12     name: "collect-trait-impls",
13     run: collect_trait_impls,
14     description: "retrieves trait impls for items in the crate",
15 };
16
17 crate fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
18     let mut synth = SyntheticImplCollector::new(cx);
19     let mut krate = cx.sess().time("collect_synthetic_impls", || synth.fold_crate(krate));
20
21     let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
22
23     let crate_items = {
24         let mut coll = ItemCollector::new();
25         krate = cx.sess().time("collect_items_for_trait_impls", || coll.fold_crate(krate));
26         coll.items
27     };
28
29     let mut new_items = Vec::new();
30
31     for &cnum in cx.tcx.crates().iter() {
32         for &(did, _) in cx.tcx.all_trait_implementations(cnum).iter() {
33             cx.tcx.sess.time("build_extern_trait_impl", || {
34                 inline::build_impl(cx, None, did, None, &mut new_items);
35             });
36         }
37     }
38
39     // Also try to inline primitive impls from other crates.
40     for &def_id in PrimitiveType::all_impls(cx.tcx).values().flatten() {
41         if !def_id.is_local() {
42             cx.sess().time("build_primitive_trait_impl", || {
43                 inline::build_impl(cx, None, def_id, None, &mut new_items);
44
45                 // FIXME(eddyb) is this `doc(hidden)` check needed?
46                 if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
47                     let self_ty = cx.tcx.type_of(def_id);
48                     let impls = get_auto_trait_and_blanket_impls(cx, self_ty, def_id);
49                     let mut renderinfo = cx.renderinfo.borrow_mut();
50
51                     new_items.extend(impls.filter(|i| renderinfo.inlined.insert(i.def_id)));
52                 }
53             })
54         }
55     }
56
57     let mut cleaner = BadImplStripper { prims, items: crate_items };
58
59     // scan through included items ahead of time to splice in Deref targets to the "valid" sets
60     for it in &new_items {
61         if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
62             if cleaner.keep_item(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() {
63                 let target = items
64                     .iter()
65                     .find_map(|item| match *item.kind {
66                         TypedefItem(ref t, true) => Some(&t.type_),
67                         _ => None,
68                     })
69                     .expect("Deref impl without Target type");
70
71                 if let Some(prim) = target.primitive_type() {
72                     cleaner.prims.insert(prim);
73                 } else if let Some(did) = target.def_id() {
74                     cleaner.items.insert(did);
75                 }
76             }
77         }
78     }
79
80     new_items.retain(|it| {
81         if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
82             cleaner.keep_item(for_)
83                 || trait_.as_ref().map_or(false, |t| cleaner.keep_item(t))
84                 || blanket_impl.is_some()
85         } else {
86             true
87         }
88     });
89
90     // `tcx.crates()` doesn't include the local crate, and `tcx.all_trait_implementations`
91     // doesn't work with it anyway, so pull them from the HIR map instead
92     for &trait_did in cx.tcx.all_traits(LOCAL_CRATE).iter() {
93         for &impl_node in cx.tcx.hir().trait_impls(trait_did) {
94             let impl_did = cx.tcx.hir().local_def_id(impl_node);
95             cx.tcx.sess.time("build_local_trait_impl", || {
96                 let mut extra_attrs = Vec::new();
97                 let mut parent = cx.tcx.parent(impl_did.to_def_id());
98                 while let Some(did) = parent {
99                     extra_attrs.extend(
100                         cx.tcx
101                             .get_attrs(did)
102                             .iter()
103                             .filter(|attr| attr.has_name(sym::doc))
104                             .filter(|attr| {
105                                 if let Some([attr]) = attr.meta_item_list().as_deref() {
106                                     attr.has_name(sym::cfg)
107                                 } else {
108                                     false
109                                 }
110                             })
111                             .cloned(),
112                     );
113                     parent = cx.tcx.parent(did);
114                 }
115                 inline::build_impl(
116                     cx,
117                     None,
118                     impl_did.to_def_id(),
119                     Some(&extra_attrs),
120                     &mut new_items,
121                 );
122             });
123         }
124     }
125
126     if let Some(ref mut it) = krate.module {
127         if let ModuleItem(Module { ref mut items, .. }) = *it.kind {
128             items.extend(synth.impls);
129             items.extend(new_items);
130         } else {
131             panic!("collect-trait-impls can't run");
132         }
133     } else {
134         panic!("collect-trait-impls can't run");
135     }
136
137     krate
138 }
139
140 struct SyntheticImplCollector<'a, 'tcx> {
141     cx: &'a DocContext<'tcx>,
142     impls: Vec<Item>,
143 }
144
145 impl<'a, 'tcx> SyntheticImplCollector<'a, 'tcx> {
146     fn new(cx: &'a DocContext<'tcx>) -> Self {
147         SyntheticImplCollector { cx, impls: Vec::new() }
148     }
149 }
150
151 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
152     fn fold_item(&mut self, i: Item) -> Option<Item> {
153         if i.is_struct() || i.is_enum() || i.is_union() {
154             // FIXME(eddyb) is this `doc(hidden)` check needed?
155             if !self.cx.tcx.get_attrs(i.def_id).lists(sym::doc).has_word(sym::hidden) {
156                 self.cx.sess().time("get_auto_trait_and_blanket_synthetic_impls", || {
157                     self.impls.extend(get_auto_trait_and_blanket_impls(
158                         self.cx,
159                         self.cx.tcx.type_of(i.def_id),
160                         i.def_id,
161                     ));
162                 });
163             }
164         }
165
166         Some(self.fold_item_recur(i))
167     }
168 }
169
170 #[derive(Default)]
171 struct ItemCollector {
172     items: FxHashSet<DefId>,
173 }
174
175 impl ItemCollector {
176     fn new() -> Self {
177         Self::default()
178     }
179 }
180
181 impl DocFolder for ItemCollector {
182     fn fold_item(&mut self, i: Item) -> Option<Item> {
183         self.items.insert(i.def_id);
184
185         Some(self.fold_item_recur(i))
186     }
187 }
188
189 struct BadImplStripper {
190     prims: FxHashSet<PrimitiveType>,
191     items: FxHashSet<DefId>,
192 }
193
194 impl BadImplStripper {
195     fn keep_item(&self, ty: &Type) -> bool {
196         if let Generic(_) = ty {
197             // keep impls made on generics
198             true
199         } else if let Some(prim) = ty.primitive_type() {
200             self.prims.contains(&prim)
201         } else if let Some(did) = ty.def_id() {
202             self.items.contains(&did)
203         } else {
204             false
205         }
206     }
207 }