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