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::ToPredicate;
6 use rustc_span::DUMMY_SP;
10 pub(crate) struct BlanketImplFinder<'a, 'tcx> {
11 pub(crate) cx: &'a mut core::DocContext<'tcx>,
14 impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
15 pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
16 let cx = &mut self.cx;
17 let param_env = cx.tcx.param_env(item_def_id);
18 let ty = cx.tcx.bound_type_of(item_def_id);
20 trace!("get_blanket_impls({:?})", ty);
21 let mut impls = Vec::new();
22 for trait_def_id in cx.tcx.all_traits() {
23 if !cx.cache.effective_visibilities.is_directly_public(cx.tcx, trait_def_id)
24 || cx.generated_synthetics.get(&(ty.0, 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 = cx.tcx.trait_impls_of(trait_def_id);
30 'blanket_impls: for &impl_def_id in trait_impls.blanket_impls() {
32 "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
36 let trait_ref = cx.tcx.impl_trait_ref(impl_def_id).unwrap();
37 if !matches!(trait_ref.0.self_ty().kind(), ty::Param(_)) {
40 let infcx = cx.tcx.infer_ctxt().build();
41 let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
42 let impl_ty = ty.subst(infcx.tcx, substs);
43 let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
45 let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
46 let impl_trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
48 // Require the type the impl is implemented on to match
49 // our type, and ignore the impl if there was a mismatch.
50 let cause = traits::ObligationCause::dummy();
51 let Ok(eq_result) = infcx.at(&cause, param_env).eq(impl_trait_ref.self_ty(), impl_ty) else {
54 let InferOk { value: (), obligations } = eq_result;
55 // FIXME(eddyb) ignoring `obligations` might cause false positives.
59 "invoking predicate_may_hold: param_env={:?}, impl_trait_ref={:?}, impl_ty={:?}",
66 .predicates_of(impl_def_id)
67 .instantiate(cx.tcx, impl_substs)
70 .chain(Some(ty::Binder::dummy(impl_trait_ref).to_predicate(infcx.tcx)));
71 for predicate in predicates {
72 debug!("testing predicate {:?}", predicate);
73 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::Canonical) => {}
82 Err(traits::OverflowError::ErrorReporting) => {}
83 _ => continue 'blanket_impls,
87 "get_blanket_impls: found applicable impl for trait_ref={:?}, ty={:?}",
91 cx.generated_synthetics.insert((ty.0, trait_def_id));
95 attrs: Default::default(),
96 item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
97 kind: Box::new(ImplItem(Box::new(Impl {
98 unsafety: hir::Unsafety::Normal,
99 generics: clean_ty_generics(
101 cx.tcx.generics_of(impl_def_id),
102 cx.tcx.explicit_predicates_of(impl_def_id),
104 // FIXME(eddyb) compute both `trait_` and `for_` from
105 // the post-inference `trait_ref`, as it's more accurate.
106 trait_: Some(clean_trait_ref_with_bindings(
108 ty::Binder::dummy(trait_ref.0),
111 for_: clean_middle_ty(ty::Binder::dummy(ty.0), cx, None),
114 .associated_items(impl_def_id)
115 .in_definition_order()
116 .map(|x| clean_middle_assoc_item(x, cx))
117 .collect::<Vec<_>>(),
118 polarity: ty::ImplPolarity::Positive,
119 kind: ImplKind::Blanket(Box::new(clean_middle_ty(
120 ty::Binder::dummy(trait_ref.0.self_ty()),
126 inline_stmt_id: None,