1 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2 use crate::traits::query::outlives_bounds::InferCtxtExt as _;
3 use crate::traits::{self, TraitEngine, TraitEngineExt};
6 use rustc_hir::def_id::DefId;
7 use rustc_hir::lang_items::LangItem;
8 use rustc_infer::infer::outlives::env::OutlivesEnvironment;
9 use rustc_infer::traits::ObligationCause;
10 use rustc_middle::arena::ArenaAllocatable;
11 use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse};
12 use rustc_middle::traits::query::Fallible;
13 use rustc_middle::ty::subst::SubstsRef;
14 use rustc_middle::ty::ToPredicate;
15 use rustc_middle::ty::WithConstness;
16 use rustc_middle::ty::{self, Ty, TypeFoldable};
17 use rustc_span::{Span, DUMMY_SP};
21 pub use rustc_infer::infer::*;
23 pub trait InferCtxtExt<'tcx> {
24 fn type_is_copy_modulo_regions(
26 param_env: ty::ParamEnv<'tcx>,
31 fn partially_normalize_associated_types_in<T>(
33 cause: ObligationCause<'tcx>,
34 param_env: ty::ParamEnv<'tcx>,
38 T: TypeFoldable<'tcx>;
40 /// Check whether a `ty` implements given trait(trait_def_id).
43 /// - the def-id of the trait
45 /// - the *other* type parameters of the trait, excluding the self-type
46 /// - the parameter environment
47 fn type_implements_trait(
51 params: SubstsRef<'tcx>,
52 param_env: ty::ParamEnv<'tcx>,
53 ) -> traits::EvaluationResult;
55 impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
56 fn type_is_copy_modulo_regions(
58 param_env: ty::ParamEnv<'tcx>,
62 let ty = self.resolve_vars_if_possible(ty);
64 if !(param_env, ty).needs_infer() {
65 return ty.is_copy_modulo_regions(self.tcx.at(span), param_env);
68 let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
70 // This can get called from typeck (by euv), and `moves_by_default`
71 // rightly refuses to work with inference variables, but
72 // moves_by_default has a cache, which we want to use in other
74 traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
77 /// Normalizes associated types in `value`, potentially returning
78 /// new obligations that must further be processed.
79 fn partially_normalize_associated_types_in<T>(
81 cause: ObligationCause<'tcx>,
82 param_env: ty::ParamEnv<'tcx>,
86 T: TypeFoldable<'tcx>,
88 debug!("partially_normalize_associated_types_in(value={:?})", value);
89 let mut selcx = traits::SelectionContext::new(self);
90 let traits::Normalized { value, obligations } =
91 traits::normalize(&mut selcx, param_env, cause, value);
93 "partially_normalize_associated_types_in: result={:?} predicates={:?}",
96 InferOk { value, obligations }
99 fn type_implements_trait(
103 params: SubstsRef<'tcx>,
104 param_env: ty::ParamEnv<'tcx>,
105 ) -> traits::EvaluationResult {
107 "type_implements_trait: trait_def_id={:?}, type={:?}, params={:?}, param_env={:?}",
108 trait_def_id, ty, params, param_env
112 ty::TraitRef { def_id: trait_def_id, substs: self.tcx.mk_substs_trait(ty, params) };
114 let obligation = traits::Obligation {
115 cause: traits::ObligationCause::dummy(),
118 predicate: trait_ref.without_const().to_predicate(self.tcx),
120 self.evaluate_obligation_no_overflow(&obligation)
124 pub trait InferCtxtBuilderExt<'tcx> {
125 fn enter_canonical_trait_query<K, R>(
127 canonical_key: &Canonical<'tcx, K>,
128 operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
129 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
131 K: TypeFoldable<'tcx>,
132 R: Debug + TypeFoldable<'tcx>,
133 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>;
136 impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
137 /// The "main method" for a canonicalized trait query. Given the
138 /// canonical key `canonical_key`, this method will create a new
139 /// inference context, instantiate the key, and run your operation
140 /// `op`. The operation should yield up a result (of type `R`) as
141 /// well as a set of trait obligations that must be fully
142 /// satisfied. These obligations will be processed and the
143 /// canonical result created.
145 /// Returns `NoSolution` in the event of any error.
147 /// (It might be mildly nicer to implement this on `TyCtxt`, and
148 /// not `InferCtxtBuilder`, but that is a bit tricky right now.
149 /// In part because we would need a `for<'tcx>` sort of
150 /// bound for the closure and in part because it is convenient to
151 /// have `'tcx` be free on this function so that we can talk about
152 /// `K: TypeFoldable<'tcx>`.)
153 fn enter_canonical_trait_query<K, R>(
155 canonical_key: &Canonical<'tcx, K>,
156 operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
157 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
159 K: TypeFoldable<'tcx>,
160 R: Debug + TypeFoldable<'tcx>,
161 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
163 self.enter_with_canonical(
166 |ref infcx, key, canonical_inference_vars| {
167 let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
168 let value = operation(infcx, &mut *fulfill_cx, key)?;
169 infcx.make_canonicalized_query_response(
170 canonical_inference_vars,
179 pub trait OutlivesEnvironmentExt<'tcx> {
180 fn add_implied_bounds(
182 infcx: &InferCtxt<'a, 'tcx>,
183 fn_sig_tys: &[Ty<'tcx>],
189 impl<'tcx> OutlivesEnvironmentExt<'tcx> for OutlivesEnvironment<'tcx> {
190 /// This method adds "implied bounds" into the outlives environment.
191 /// Implied bounds are outlives relationships that we can deduce
192 /// on the basis that certain types must be well-formed -- these are
193 /// either the types that appear in the function signature or else
194 /// the input types to an impl. For example, if you have a function
198 /// fn foo<'a, 'b, T>(x: &'a &'b [T]) { }
201 /// we can assume in the caller's body that `'b: 'a` and that `T:
202 /// 'b` (and hence, transitively, that `T: 'a`). This method would
203 /// add those assumptions into the outlives-environment.
205 /// Tests: `src/test/ui/regions/regions-free-region-ordering-*.rs`
206 fn add_implied_bounds(
208 infcx: &InferCtxt<'a, 'tcx>,
209 fn_sig_tys: &[Ty<'tcx>],
213 debug!("add_implied_bounds()");
215 for &ty in fn_sig_tys {
216 let ty = infcx.resolve_vars_if_possible(ty);
217 debug!("add_implied_bounds: ty = {}", ty);
218 let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty, span);
219 self.add_outlives_bounds(Some(infcx), implied_bounds)