]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/infer.rs
Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup
[rust.git] / compiler / rustc_trait_selection / src / infer.rs
1 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2 use crate::traits::{self, ObligationCtxt};
3
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};
14
15 use std::fmt::Debug;
16
17 pub use rustc_infer::infer::*;
18
19 pub trait InferCtxtExt<'tcx> {
20     fn type_is_copy_modulo_regions(
21         &self,
22         param_env: ty::ParamEnv<'tcx>,
23         ty: Ty<'tcx>,
24         span: Span,
25     ) -> bool;
26
27     fn type_is_sized_modulo_regions(
28         &self,
29         param_env: ty::ParamEnv<'tcx>,
30         ty: Ty<'tcx>,
31         span: Span,
32     ) -> bool;
33
34     fn partially_normalize_associated_types_in<T>(
35         &self,
36         cause: ObligationCause<'tcx>,
37         param_env: ty::ParamEnv<'tcx>,
38         value: T,
39     ) -> InferOk<'tcx, T>
40     where
41         T: TypeFoldable<'tcx>;
42
43     /// Check whether a `ty` implements given trait(trait_def_id).
44     /// The inputs are:
45     ///
46     /// - the def-id of the trait
47     /// - the self type
48     /// - the *other* type parameters of the trait, excluding the self-type
49     /// - the parameter environment
50     ///
51     /// Invokes `evaluate_obligation`, so in the event that evaluating
52     /// `Ty: Trait` causes overflow, EvaluatedToRecur (or EvaluatedToUnknown)
53     /// will be returned.
54     fn type_implements_trait(
55         &self,
56         trait_def_id: DefId,
57         ty: Ty<'tcx>,
58         params: SubstsRef<'tcx>,
59         param_env: ty::ParamEnv<'tcx>,
60     ) -> traits::EvaluationResult;
61 }
62 impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
63     fn type_is_copy_modulo_regions(
64         &self,
65         param_env: ty::ParamEnv<'tcx>,
66         ty: Ty<'tcx>,
67         span: Span,
68     ) -> bool {
69         let ty = self.resolve_vars_if_possible(ty);
70
71         if !(param_env, ty).needs_infer() {
72             return ty.is_copy_modulo_regions(self.tcx, param_env);
73         }
74
75         let copy_def_id = self.tcx.require_lang_item(LangItem::Copy, None);
76
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
80         // cases.
81         traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
82     }
83
84     fn type_is_sized_modulo_regions(
85         &self,
86         param_env: ty::ParamEnv<'tcx>,
87         ty: Ty<'tcx>,
88         span: Span,
89     ) -> bool {
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)
92     }
93
94     /// Normalizes associated types in `value`, potentially returning
95     /// new obligations that must further be processed.
96     #[instrument(level = "debug", skip(self, cause, param_env), ret)]
97     fn partially_normalize_associated_types_in<T>(
98         &self,
99         cause: ObligationCause<'tcx>,
100         param_env: ty::ParamEnv<'tcx>,
101         value: T,
102     ) -> InferOk<'tcx, T>
103     where
104         T: TypeFoldable<'tcx>,
105     {
106         let mut selcx = traits::SelectionContext::new(self);
107         let traits::Normalized { value, obligations } =
108             traits::normalize(&mut selcx, param_env, cause, value);
109         InferOk { value, obligations }
110     }
111
112     #[instrument(level = "debug", skip(self), ret)]
113     fn type_implements_trait(
114         &self,
115         trait_def_id: DefId,
116         ty: Ty<'tcx>,
117         params: SubstsRef<'tcx>,
118         param_env: ty::ParamEnv<'tcx>,
119     ) -> traits::EvaluationResult {
120         let trait_ref =
121             ty::TraitRef { def_id: trait_def_id, substs: self.tcx.mk_substs_trait(ty, params) };
122
123         let obligation = traits::Obligation {
124             cause: traits::ObligationCause::dummy(),
125             param_env,
126             recursion_depth: 0,
127             predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
128         };
129         self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
130     }
131 }
132
133 pub trait InferCtxtBuilderExt<'tcx> {
134     fn enter_canonical_trait_query<K, R>(
135         &mut self,
136         canonical_key: &Canonical<'tcx, K>,
137         operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Fallible<R>,
138     ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
139     where
140         K: TypeFoldable<'tcx>,
141         R: Debug + TypeFoldable<'tcx>,
142         Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>;
143 }
144
145 impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
146     /// The "main method" for a canonicalized trait query. Given the
147     /// canonical key `canonical_key`, this method will create a new
148     /// inference context, instantiate the key, and run your operation
149     /// `op`. The operation should yield up a result (of type `R`) as
150     /// well as a set of trait obligations that must be fully
151     /// satisfied. These obligations will be processed and the
152     /// canonical result created.
153     ///
154     /// Returns `NoSolution` in the event of any error.
155     ///
156     /// (It might be mildly nicer to implement this on `TyCtxt`, and
157     /// not `InferCtxtBuilder`, but that is a bit tricky right now.
158     /// In part because we would need a `for<'tcx>` sort of
159     /// bound for the closure and in part because it is convenient to
160     /// have `'tcx` be free on this function so that we can talk about
161     /// `K: TypeFoldable<'tcx>`.)
162     fn enter_canonical_trait_query<K, R>(
163         &mut self,
164         canonical_key: &Canonical<'tcx, K>,
165         operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Fallible<R>,
166     ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
167     where
168         K: TypeFoldable<'tcx>,
169         R: Debug + TypeFoldable<'tcx>,
170         Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
171     {
172         let (infcx, key, canonical_inference_vars) =
173             self.build_with_canonical(DUMMY_SP, canonical_key);
174         let ocx = ObligationCtxt::new(&infcx);
175         let value = operation(&ocx, key)?;
176         ocx.make_canonicalized_query_response(canonical_inference_vars, value)
177     }
178 }