]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Rollup merge of #102038 - jyn514:rustdoc-normalize-test, r=compiler-errors
[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_infer::infer::{InferOk, TyCtxtInferExt};
4 use rustc_infer::traits;
5 use rustc_middle::ty::ToPredicate;
6 use rustc_span::DUMMY_SP;
7
8 use super::*;
9
10 pub(crate) struct BlanketImplFinder<'a, 'tcx> {
11     pub(crate) cx: &'a mut core::DocContext<'tcx>,
12 }
13
14 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
15     pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
16         let param_env = self.cx.tcx.param_env(item_def_id);
17         let ty = self.cx.tcx.bound_type_of(item_def_id);
18
19         trace!("get_blanket_impls({:?})", ty);
20         let mut impls = Vec::new();
21         self.cx.with_all_traits(|cx, all_traits| {
22             for &trait_def_id in all_traits {
23                 if !cx.cache.access_levels.is_public(trait_def_id)
24                     || cx.generated_synthetics.get(&(ty.0, trait_def_id)).is_some()
25                 {
26                     continue;
27                 }
28                 // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
29                 let trait_impls = cx.tcx.trait_impls_of(trait_def_id);
30                 for &impl_def_id in trait_impls.blanket_impls() {
31                     trace!(
32                         "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
33                         trait_def_id,
34                         impl_def_id
35                     );
36                     let trait_ref = cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap();
37                     let is_param = matches!(trait_ref.0.self_ty().kind(), ty::Param(_));
38                     let may_apply = is_param && cx.tcx.infer_ctxt().enter(|infcx| {
39                         let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
40                         let ty = ty.subst(infcx.tcx, substs);
41                         let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
42
43                         let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
44                         let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
45
46                         // Require the type the impl is implemented on to match
47                         // our type, and ignore the impl if there was a mismatch.
48                         let cause = traits::ObligationCause::dummy();
49                         let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
50                         if let Ok(InferOk { value: (), obligations }) = eq_result {
51                             // FIXME(eddyb) ignoring `obligations` might cause false positives.
52                             drop(obligations);
53
54                             trace!(
55                                 "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
56                                 param_env,
57                                 trait_ref,
58                                 ty
59                             );
60                             let predicates = cx
61                                 .tcx
62                                 .predicates_of(impl_def_id)
63                                 .instantiate(cx.tcx, impl_substs)
64                                 .predicates
65                                 .into_iter()
66                                 .chain(Some(
67                                     ty::Binder::dummy(trait_ref)
68                                         .to_poly_trait_predicate()
69                                         .map_bound(ty::PredicateKind::Trait)
70                                         .to_predicate(infcx.tcx),
71                                 ));
72                             for predicate in predicates {
73                                 debug!("testing predicate {:?}", predicate);
74                                 let obligation = traits::Obligation::new(
75                                     traits::ObligationCause::dummy(),
76                                     param_env,
77                                     predicate,
78                                 );
79                                 match infcx.evaluate_obligation(&obligation) {
80                                     Ok(eval_result) if eval_result.may_apply() => {}
81                                     Err(traits::OverflowError::Canonical) => {}
82                                     Err(traits::OverflowError::ErrorReporting) => {}
83                                     _ => {
84                                         return false;
85                                     }
86                                 }
87                             }
88                             true
89                         } else {
90                             false
91                         }
92                     });
93                     debug!(
94                         "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
95                         may_apply, trait_ref, ty
96                     );
97                     if !may_apply {
98                         continue;
99                     }
100
101                     cx.generated_synthetics.insert((ty.0, trait_def_id));
102
103                     impls.push(Item {
104                         name: None,
105                         attrs: Default::default(),
106                         visibility: Inherited,
107                         item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
108                         kind: Box::new(ImplItem(Box::new(Impl {
109                             unsafety: hir::Unsafety::Normal,
110                             generics: clean_ty_generics(
111                                 cx,
112                                 cx.tcx.generics_of(impl_def_id),
113                                 cx.tcx.explicit_predicates_of(impl_def_id),
114                             ),
115                             // FIXME(eddyb) compute both `trait_` and `for_` from
116                             // the post-inference `trait_ref`, as it's more accurate.
117                             trait_: Some(clean_trait_ref_with_bindings(cx, trait_ref.0, ThinVec::new())),
118                             for_: clean_middle_ty(ty.0, cx, None),
119                             items: cx.tcx
120                                 .associated_items(impl_def_id)
121                                 .in_definition_order()
122                                 .map(|x| clean_middle_assoc_item(x, cx))
123                                 .collect::<Vec<_>>(),
124                             polarity: ty::ImplPolarity::Positive,
125                             kind: ImplKind::Blanket(Box::new(clean_middle_ty(trait_ref.0.self_ty(), cx, None))),
126                         }))),
127                         cfg: None,
128                     });
129                 }
130             }
131         });
132
133         impls
134     }
135 }