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