]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/passes/collect_trait_impls.rs
New upstream version 1.44.1+dfsg1
[rustc.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 lang_items.const_slice_ptr_impl(),
66 lang_items.mut_slice_ptr_impl(),
67 ];
68
69 for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
70 if !def_id.is_local() {
71 inline::build_impl(cx, def_id, None, &mut new_items);
72
73 // FIXME(eddyb) is this `doc(hidden)` check needed?
74 if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
75 let self_ty = cx.tcx.type_of(def_id);
76 let impls = get_auto_trait_and_blanket_impls(cx, self_ty, def_id);
77 let mut renderinfo = cx.renderinfo.borrow_mut();
78
79 new_items.extend(impls.filter(|i| renderinfo.inlined.insert(i.def_id)));
80 }
81 }
82 }
83
84 let mut cleaner = BadImplStripper { prims, items: crate_items };
85
86 // scan through included items ahead of time to splice in Deref targets to the "valid" sets
87 for it in &new_items {
88 if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = it.inner {
89 if cleaner.keep_item(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() {
90 let target = items
91 .iter()
92 .filter_map(|item| match item.inner {
93 TypedefItem(ref t, true) => Some(&t.type_),
94 _ => None,
95 })
96 .next()
97 .expect("Deref impl without Target type");
98
99 if let Some(prim) = target.primitive_type() {
100 cleaner.prims.insert(prim);
101 } else if let Some(did) = target.def_id() {
102 cleaner.items.insert(did);
103 }
104 }
105 }
106 }
107
108 new_items.retain(|it| {
109 if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = it.inner {
110 cleaner.keep_item(for_)
111 || trait_.as_ref().map_or(false, |t| cleaner.keep_item(t))
112 || blanket_impl.is_some()
113 } else {
114 true
115 }
116 });
117
118 // `tcx.crates()` doesn't include the local crate, and `tcx.all_trait_implementations`
119 // doesn't work with it anyway, so pull them from the HIR map instead
120 for &trait_did in cx.tcx.all_traits(LOCAL_CRATE).iter() {
121 for &impl_node in cx.tcx.hir().trait_impls(trait_did) {
122 let impl_did = cx.tcx.hir().local_def_id(impl_node);
123 inline::build_impl(cx, impl_did, None, &mut new_items);
124 }
125 }
126
127 if let Some(ref mut it) = krate.module {
128 if let ModuleItem(Module { ref mut items, .. }) = it.inner {
129 items.extend(synth.impls);
130 items.extend(new_items);
131 } else {
132 panic!("collect-trait-impls can't run");
133 }
134 } else {
135 panic!("collect-trait-impls can't run");
136 }
137
138 krate
139 }
140
141 struct SyntheticImplCollector<'a, 'tcx> {
142 cx: &'a DocContext<'tcx>,
143 impls: Vec<Item>,
144 }
145
146 impl<'a, 'tcx> SyntheticImplCollector<'a, 'tcx> {
147 fn new(cx: &'a DocContext<'tcx>) -> Self {
148 SyntheticImplCollector { cx, impls: Vec::new() }
149 }
150 }
151
152 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
153 fn fold_item(&mut self, i: Item) -> Option<Item> {
154 if i.is_struct() || i.is_enum() || i.is_union() {
155 // FIXME(eddyb) is this `doc(hidden)` check needed?
156 if !self.cx.tcx.get_attrs(i.def_id).lists(sym::doc).has_word(sym::hidden) {
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 self.fold_item_recur(i)
166 }
167 }
168
169 #[derive(Default)]
170 struct ItemCollector {
171 items: FxHashSet<DefId>,
172 }
173
174 impl ItemCollector {
175 fn new() -> Self {
176 Self::default()
177 }
178 }
179
180 impl DocFolder for ItemCollector {
181 fn fold_item(&mut self, i: Item) -> Option<Item> {
182 self.items.insert(i.def_id);
183
184 self.fold_item_recur(i)
185 }
186 }
187
188 struct BadImplStripper {
189 prims: FxHashSet<PrimitiveType>,
190 items: FxHashSet<DefId>,
191 }
192
193 impl BadImplStripper {
194 fn keep_item(&self, ty: &Type) -> bool {
195 if let Generic(_) = ty {
196 // keep impls made on generics
197 true
198 } else if let Some(prim) = ty.primitive_type() {
199 self.prims.contains(&prim)
200 } else if let Some(did) = ty.def_id() {
201 self.items.contains(&did)
202 } else {
203 false
204 }
205 }
206 }