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