1 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2 use crate::traits::{self, TraitEngine, TraitEngineExt};
4 use rustc_hir::def_id::DefId;
5 use rustc_hir::lang_items::LangItem;
6 use rustc_infer::traits::ObligationCause;
7 use rustc_middle::arena::ArenaAllocatable;
8 use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse};
9 use rustc_middle::traits::query::Fallible;
10 use rustc_middle::ty::subst::SubstsRef;
11 use rustc_middle::ty::ToPredicate;
12 use rustc_middle::ty::{self, Ty, TypeFoldable, TypeVisitable};
13 use rustc_span::{Span, DUMMY_SP};
17 pub use rustc_infer::infer::*;
19 pub trait InferCtxtExt<'tcx> {
20 fn type_is_copy_modulo_regions(
22 param_env: ty::ParamEnv<'tcx>,
27 fn type_is_sized_modulo_regions(
29 param_env: ty::ParamEnv<'tcx>,
34 fn partially_normalize_associated_types_in<T>(
36 cause: ObligationCause<'tcx>,
37 param_env: ty::ParamEnv<'tcx>,
41 T: TypeFoldable<'tcx>;
43 /// Check whether a `ty` implements given trait(trait_def_id).
46 /// - the def-id of the trait
48 /// - the *other* type parameters of the trait, excluding the self-type
49 /// - the parameter environment
51 /// Invokes `evaluate_obligation`, so in the event that evaluating
52 /// `Ty: Trait` causes overflow, EvaluatedToRecur (or EvaluatedToUnknown)
54 fn type_implements_trait(
58 params: SubstsRef<'tcx>,
59 param_env: ty::ParamEnv<'tcx>,
60 ) -> traits::EvaluationResult;
62 impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
63 fn type_is_copy_modulo_regions(
65 param_env: ty::ParamEnv<'tcx>,
69 let ty = self.resolve_vars_if_possible(ty);
71 if !(param_env, ty).needs_infer() {
72 return ty.is_copy_modulo_regions(self.tcx.at(span), param_env);
75 let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
77 // This can get called from typeck (by euv), and `moves_by_default`
78 // rightly refuses to work with inference variables, but
79 // moves_by_default has a cache, which we want to use in other
81 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
84 fn type_is_sized_modulo_regions(
86 param_env: ty::ParamEnv<'tcx>,
90 let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
91 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, lang_item, span)
94 /// Normalizes associated types in `value`, potentially returning
95 /// new obligations that must further be processed.
96 fn partially_normalize_associated_types_in<T>(
98 cause: ObligationCause<'tcx>,
99 param_env: ty::ParamEnv<'tcx>,
101 ) -> InferOk<'tcx, T>
103 T: TypeFoldable<'tcx>,
105 debug!("partially_normalize_associated_types_in(value={:?})", value);
106 let mut selcx = traits::SelectionContext::new(self);
107 let traits::Normalized { value, obligations } =
108 traits::normalize(&mut selcx, param_env, cause, value);
110 "partially_normalize_associated_types_in: result={:?} predicates={:?}",
113 InferOk { value, obligations }
116 fn type_implements_trait(
120 params: SubstsRef<'tcx>,
121 param_env: ty::ParamEnv<'tcx>,
122 ) -> traits::EvaluationResult {
124 "type_implements_trait: trait_def_id={:?}, type={:?}, params={:?}, param_env={:?}",
125 trait_def_id, ty, params, param_env
129 ty::TraitRef { def_id: trait_def_id, substs: self.tcx.mk_substs_trait(ty, params) };
131 let obligation = traits::Obligation {
132 cause: traits::ObligationCause::dummy(),
135 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
137 self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
141 pub trait InferCtxtBuilderExt<'tcx> {
142 fn enter_canonical_trait_query<K, R>(
144 canonical_key: &Canonical<'tcx, K>,
145 operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
146 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
148 K: TypeFoldable<'tcx>,
149 R: Debug + TypeFoldable<'tcx>,
150 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>;
153 impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
154 /// The "main method" for a canonicalized trait query. Given the
155 /// canonical key `canonical_key`, this method will create a new
156 /// inference context, instantiate the key, and run your operation
157 /// `op`. The operation should yield up a result (of type `R`) as
158 /// well as a set of trait obligations that must be fully
159 /// satisfied. These obligations will be processed and the
160 /// canonical result created.
162 /// Returns `NoSolution` in the event of any error.
164 /// (It might be mildly nicer to implement this on `TyCtxt`, and
165 /// not `InferCtxtBuilder`, but that is a bit tricky right now.
166 /// In part because we would need a `for<'tcx>` sort of
167 /// bound for the closure and in part because it is convenient to
168 /// have `'tcx` be free on this function so that we can talk about
169 /// `K: TypeFoldable<'tcx>`.)
170 fn enter_canonical_trait_query<K, R>(
172 canonical_key: &Canonical<'tcx, K>,
173 operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
174 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
176 K: TypeFoldable<'tcx>,
177 R: Debug + TypeFoldable<'tcx>,
178 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
180 self.enter_with_canonical(
183 |ref infcx, key, canonical_inference_vars| {
184 let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
185 let value = operation(infcx, &mut *fulfill_cx, key)?;
186 infcx.make_canonicalized_query_response(
187 canonical_inference_vars,