]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Auto merge of #80454 - JulianKnodt:ob_forest_op, r=matthewjasper
[rust.git] / src / librustdoc / clean / blanket_impl.rs
1 use crate::rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2 use rustc_hir as hir;
3 use rustc_hir::def_id::LOCAL_CRATE;
4 use rustc_infer::infer::{InferOk, TyCtxtInferExt};
5 use rustc_infer::traits;
6 use rustc_middle::ty::subst::Subst;
7 use rustc_middle::ty::{ToPredicate, WithConstness};
8 use rustc_span::DUMMY_SP;
9
10 use super::*;
11
12 crate struct BlanketImplFinder<'a, 'tcx> {
13     crate cx: &'a mut core::DocContext<'tcx>,
14 }
15
16 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
17     // FIXME(eddyb) figure out a better way to pass information about
18     // parametrization of `ty` than `param_env_def_id`.
19     crate fn get_blanket_impls(&mut self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
20         let param_env = self.cx.tcx.param_env(param_env_def_id);
21
22         debug!("get_blanket_impls({:?})", ty);
23         let mut impls = Vec::new();
24         for &trait_def_id in self.cx.tcx.all_traits(LOCAL_CRATE).iter() {
25             if !self.cx.renderinfo.access_levels.is_public(trait_def_id)
26                 || self.cx.generated_synthetics.get(&(ty, trait_def_id)).is_some()
27             {
28                 continue;
29             }
30             self.cx.tcx.for_each_relevant_impl(trait_def_id, ty, |impl_def_id| {
31                 debug!(
32                     "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
33                     trait_def_id, impl_def_id
34                 );
35                 let trait_ref = self.cx.tcx.impl_trait_ref(impl_def_id).unwrap();
36                 let may_apply = self.cx.tcx.infer_ctxt().enter(|infcx| {
37                     match trait_ref.self_ty().kind() {
38                         ty::Param(_) => {}
39                         _ => return false,
40                     }
41
42                     let substs = infcx.fresh_substs_for_item(DUMMY_SP, param_env_def_id);
43                     let ty = ty.subst(infcx.tcx, substs);
44                     let param_env = param_env.subst(infcx.tcx, substs);
45
46                     let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
47                     let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
48
49                     // Require the type the impl is implemented on to match
50                     // our type, and ignore the impl if there was a mismatch.
51                     let cause = traits::ObligationCause::dummy();
52                     let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
53                     if let Ok(InferOk { value: (), obligations }) = eq_result {
54                         // FIXME(eddyb) ignoring `obligations` might cause false positives.
55                         drop(obligations);
56
57                         debug!(
58                             "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
59                             param_env, trait_ref, ty
60                         );
61                         let predicates = self
62                             .cx
63                             .tcx
64                             .predicates_of(impl_def_id)
65                             .instantiate(self.cx.tcx, impl_substs)
66                             .predicates
67                             .into_iter()
68                             .chain(Some(trait_ref.without_const().to_predicate(infcx.tcx)));
69                         for predicate in predicates {
70                             debug!("testing predicate {:?}", predicate);
71                             let obligation = traits::Obligation::new(
72                                 traits::ObligationCause::dummy(),
73                                 param_env,
74                                 predicate,
75                             );
76                             match infcx.evaluate_obligation(&obligation) {
77                                 Ok(eval_result) if eval_result.may_apply() => {}
78                                 Err(traits::OverflowError) => {}
79                                 _ => {
80                                     return false;
81                                 }
82                             }
83                         }
84                         true
85                     } else {
86                         false
87                     }
88                 });
89                 debug!(
90                     "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
91                     may_apply, trait_ref, ty
92                 );
93                 if !may_apply {
94                     return;
95                 }
96
97                 self.cx.generated_synthetics.insert((ty, trait_def_id));
98                 let provided_trait_methods = self
99                     .cx
100                     .tcx
101                     .provided_trait_methods(trait_def_id)
102                     .map(|meth| meth.ident.name)
103                     .collect();
104
105                 impls.push(Item {
106                     source: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
107                     name: None,
108                     attrs: Default::default(),
109                     visibility: Inherited,
110                     def_id: self.cx.next_def_id(impl_def_id.krate),
111                     kind: box ImplItem(Impl {
112                         unsafety: hir::Unsafety::Normal,
113                         generics: (
114                             self.cx.tcx.generics_of(impl_def_id),
115                             self.cx.tcx.explicit_predicates_of(impl_def_id),
116                         )
117                             .clean(self.cx),
118                         provided_trait_methods,
119                         // FIXME(eddyb) compute both `trait_` and `for_` from
120                         // the post-inference `trait_ref`, as it's more accurate.
121                         trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
122                         for_: ty.clean(self.cx),
123                         items: self
124                             .cx
125                             .tcx
126                             .associated_items(impl_def_id)
127                             .in_definition_order()
128                             .collect::<Vec<_>>()
129                             .clean(self.cx),
130                         negative_polarity: false,
131                         synthetic: false,
132                         blanket_impl: Some(trait_ref.self_ty().clean(self.cx)),
133                     }),
134                 });
135             });
136         }
137         impls
138     }
139 }