1 use crate::rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
3 use rustc_infer::infer::{InferOk, TyCtxtInferExt};
4 use rustc_infer::traits;
5 use rustc_middle::ty::subst::Subst;
6 use rustc_middle::ty::{ToPredicate, WithConstness};
7 use rustc_span::DUMMY_SP;
11 crate struct BlanketImplFinder<'a, 'tcx> {
12 crate cx: &'a mut core::DocContext<'tcx>,
15 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
16 crate fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
17 let param_env = self.cx.tcx.param_env(item_def_id);
18 let ty = self.cx.tcx.type_of(item_def_id);
20 trace!("get_blanket_impls({:?})", ty);
21 let mut impls = Vec::new();
22 for &trait_def_id in self.cx.tcx.all_traits(()).iter() {
23 if !self.cx.cache.access_levels.is_public(trait_def_id)
24 || self.cx.generated_synthetics.get(&(ty, trait_def_id)).is_some()
28 // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
29 let trait_impls = self.cx.tcx.trait_impls_of(trait_def_id);
30 for &impl_def_id in trait_impls.blanket_impls() {
32 "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
36 let trait_ref = self.cx.tcx.impl_trait_ref(impl_def_id).unwrap();
37 let is_param = matches!(trait_ref.self_ty().kind(), ty::Param(_));
38 let may_apply = is_param && self.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 = param_env.subst(infcx.tcx, substs);
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);
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.
55 "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
63 .predicates_of(impl_def_id)
64 .instantiate(self.cx.tcx, impl_substs)
68 ty::Binder::dummy(trait_ref)
70 .to_predicate(infcx.tcx),
72 for predicate in predicates {
73 debug!("testing predicate {:?}", predicate);
74 let obligation = traits::Obligation::new(
75 traits::ObligationCause::dummy(),
79 match infcx.evaluate_obligation(&obligation) {
80 Ok(eval_result) if eval_result.may_apply() => {}
81 Err(traits::OverflowError) => {}
93 "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
94 may_apply, trait_ref, ty
100 self.cx.generated_synthetics.insert((ty, trait_def_id));
104 attrs: Default::default(),
105 visibility: Inherited,
106 def_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
107 kind: box ImplItem(Impl {
108 span: Span::new(self.cx.tcx.def_span(impl_def_id)),
109 unsafety: hir::Unsafety::Normal,
111 self.cx.tcx.generics_of(impl_def_id),
112 self.cx.tcx.explicit_predicates_of(impl_def_id),
115 // FIXME(eddyb) compute both `trait_` and `for_` from
116 // the post-inference `trait_ref`, as it's more accurate.
117 trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
118 for_: ty.clean(self.cx),
122 .associated_items(impl_def_id)
123 .in_definition_order()
126 negative_polarity: false,
128 blanket_impl: Some(box trait_ref.self_ty().clean(self.cx)),