]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/collect_trait_impls.rs
Rollup merge of #79834 - m-ou-se:bye-linked-list-extras, r=Mark-Simulacrum
[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 = 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 = 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             inline::build_impl(cx, None, def_id, None, &mut new_items);
43
44             // FIXME(eddyb) is this `doc(hidden)` check needed?
45             if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
46                 let self_ty = cx.tcx.type_of(def_id);
47                 let impls = get_auto_trait_and_blanket_impls(cx, self_ty, def_id);
48                 let mut renderinfo = cx.renderinfo.borrow_mut();
49
50                 new_items.extend(impls.filter(|i| renderinfo.inlined.insert(i.def_id)));
51             }
52         }
53     }
54
55     let mut cleaner = BadImplStripper { prims, items: crate_items };
56
57     // scan through included items ahead of time to splice in Deref targets to the "valid" sets
58     for it in &new_items {
59         if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = it.kind {
60             if cleaner.keep_item(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() {
61                 let target = items
62                     .iter()
63                     .find_map(|item| match item.kind {
64                         TypedefItem(ref t, true) => Some(&t.type_),
65                         _ => None,
66                     })
67                     .expect("Deref impl without Target type");
68
69                 if let Some(prim) = target.primitive_type() {
70                     cleaner.prims.insert(prim);
71                 } else if let Some(did) = target.def_id() {
72                     cleaner.items.insert(did);
73                 }
74             }
75         }
76     }
77
78     new_items.retain(|it| {
79         if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = it.kind {
80             cleaner.keep_item(for_)
81                 || trait_.as_ref().map_or(false, |t| cleaner.keep_item(t))
82                 || blanket_impl.is_some()
83         } else {
84             true
85         }
86     });
87
88     // `tcx.crates()` doesn't include the local crate, and `tcx.all_trait_implementations`
89     // doesn't work with it anyway, so pull them from the HIR map instead
90     for &trait_did in cx.tcx.all_traits(LOCAL_CRATE).iter() {
91         for &impl_node in cx.tcx.hir().trait_impls(trait_did) {
92             let impl_did = cx.tcx.hir().local_def_id(impl_node);
93             cx.tcx.sess.time("build_local_trait_impl", || {
94                 let mut extra_attrs = Vec::new();
95                 let mut parent = cx.tcx.parent(impl_did.to_def_id());
96                 while let Some(did) = parent {
97                     extra_attrs.extend(
98                         cx.tcx
99                             .get_attrs(did)
100                             .iter()
101                             .filter(|attr| attr.has_name(sym::doc))
102                             .filter(|attr| {
103                                 if let Some([attr]) = attr.meta_item_list().as_deref() {
104                                     attr.has_name(sym::cfg)
105                                 } else {
106                                     false
107                                 }
108                             })
109                             .cloned(),
110                     );
111                     parent = cx.tcx.parent(did);
112                 }
113                 inline::build_impl(
114                     cx,
115                     None,
116                     impl_did.to_def_id(),
117                     Some(&extra_attrs),
118                     &mut new_items,
119                 );
120             });
121         }
122     }
123
124     if let Some(ref mut it) = krate.module {
125         if let ModuleItem(Module { ref mut items, .. }) = it.kind {
126             items.extend(synth.impls);
127             items.extend(new_items);
128         } else {
129             panic!("collect-trait-impls can't run");
130         }
131     } else {
132         panic!("collect-trait-impls can't run");
133     }
134
135     krate
136 }
137
138 struct SyntheticImplCollector<'a, 'tcx> {
139     cx: &'a DocContext<'tcx>,
140     impls: Vec<Item>,
141 }
142
143 impl<'a, 'tcx> SyntheticImplCollector<'a, 'tcx> {
144     fn new(cx: &'a DocContext<'tcx>) -> Self {
145         SyntheticImplCollector { cx, impls: Vec::new() }
146     }
147 }
148
149 impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
150     fn fold_item(&mut self, i: Item) -> Option<Item> {
151         if i.is_struct() || i.is_enum() || i.is_union() {
152             // FIXME(eddyb) is this `doc(hidden)` check needed?
153             if !self.cx.tcx.get_attrs(i.def_id).lists(sym::doc).has_word(sym::hidden) {
154                 self.impls.extend(get_auto_trait_and_blanket_impls(
155                     self.cx,
156                     self.cx.tcx.type_of(i.def_id),
157                     i.def_id,
158                 ));
159             }
160         }
161
162         Some(self.fold_item_recur(i))
163     }
164 }
165
166 #[derive(Default)]
167 struct ItemCollector {
168     items: FxHashSet<DefId>,
169 }
170
171 impl ItemCollector {
172     fn new() -> Self {
173         Self::default()
174     }
175 }
176
177 impl DocFolder for ItemCollector {
178     fn fold_item(&mut self, i: Item) -> Option<Item> {
179         self.items.insert(i.def_id);
180
181         Some(self.fold_item_recur(i))
182     }
183 }
184
185 struct BadImplStripper {
186     prims: FxHashSet<PrimitiveType>,
187     items: FxHashSet<DefId>,
188 }
189
190 impl BadImplStripper {
191     fn keep_item(&self, ty: &Type) -> bool {
192         if let Generic(_) = ty {
193             // keep impls made on generics
194             true
195         } else if let Some(prim) = ty.primitive_type() {
196             self.prims.contains(&prim)
197         } else if let Some(did) = ty.def_id() {
198             self.items.contains(&did)
199         } else {
200             false
201         }
202     }
203 }