1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::infer::at::ToTrace;
12 use rustc::infer::canonical::{Canonical, QueryResponse};
13 use rustc::infer::InferCtxt;
14 use rustc::hir::def_id::DefId;
15 use rustc::traits::query::type_op::ascribe_user_type::AscribeUserType;
16 use rustc::traits::query::type_op::eq::Eq;
17 use rustc::traits::query::type_op::normalize::Normalize;
18 use rustc::traits::query::type_op::prove_predicate::ProvePredicate;
19 use rustc::traits::query::type_op::subtype::Subtype;
20 use rustc::traits::query::{Fallible, NoSolution};
22 FulfillmentContext, Normalized, Obligation, ObligationCause, TraitEngine, TraitEngineExt,
24 use rustc::ty::query::Providers;
25 use rustc::ty::subst::{Kind, Subst, UserSelfTy, UserSubsts};
27 FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, Ty, TyCtxt, TypeFoldable, Variance,
29 use rustc_data_structures::sync::Lrc;
32 use syntax_pos::DUMMY_SP;
34 crate fn provide(p: &mut Providers) {
36 type_op_ascribe_user_type,
38 type_op_prove_predicate,
41 type_op_normalize_predicate,
42 type_op_normalize_fn_sig,
43 type_op_normalize_poly_fn_sig,
48 fn type_op_ascribe_user_type<'tcx>(
49 tcx: TyCtxt<'_, 'tcx, 'tcx>,
50 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
51 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
53 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
65 "type_op_ascribe_user_type(\
66 mir_ty={:?}, variance={:?}, def_id={:?}, user_substs={:?}\
68 mir_ty, variance, def_id, user_substs,
71 let mut cx = AscribeUserTypeCx {
76 cx.relate_mir_and_user_ty(mir_ty, variance, def_id, user_substs)?;
82 struct AscribeUserTypeCx<'me, 'gcx: 'tcx, 'tcx: 'me> {
83 infcx: &'me InferCtxt<'me, 'gcx, 'tcx>,
84 param_env: ParamEnv<'tcx>,
85 fulfill_cx: &'me mut FulfillmentContext<'tcx>,
88 impl AscribeUserTypeCx<'me, 'gcx, 'tcx> {
89 fn normalize<T>(&mut self, value: T) -> T
91 T: TypeFoldable<'tcx>,
94 .partially_normalize_associated_types_in(
100 .into_value_registering_obligations(self.infcx, self.fulfill_cx)
103 fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
108 .at(&ObligationCause::dummy(), self.param_env)
109 .relate(a, variance, b)?
110 .into_value_registering_obligations(self.infcx, self.fulfill_cx))
113 fn prove_predicate(&mut self, predicate: Predicate<'tcx>) {
114 self.fulfill_cx.register_predicate_obligation(
116 Obligation::new(ObligationCause::dummy(), self.param_env, predicate),
120 fn tcx(&self) -> TyCtxt<'me, 'gcx, 'tcx> {
124 fn subst<T>(&self, value: T, substs: &[Kind<'tcx>]) -> T
126 T: TypeFoldable<'tcx>,
128 value.subst(self.tcx(), substs)
131 fn relate_mir_and_user_ty(
136 user_substs: UserSubsts<'tcx>,
137 ) -> Result<(), NoSolution> {
143 let ty = self.tcx().type_of(def_id);
144 let ty = self.subst(ty, substs);
145 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
146 let ty = self.normalize(ty);
148 self.relate(mir_ty, variance, ty)?;
150 if let Some(UserSelfTy {
155 let impl_self_ty = self.tcx().type_of(impl_def_id);
156 let impl_self_ty = self.subst(impl_self_ty, &substs);
157 let impl_self_ty = self.normalize(impl_self_ty);
159 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
161 self.prove_predicate(Predicate::WellFormed(impl_self_ty));
164 // Prove the predicates coming along with `def_id`.
166 // Also, normalize the `instantiated_predicates`
167 // because otherwise we wind up with duplicate "type
168 // outlives" error messages.
169 let instantiated_predicates = self.tcx()
170 .predicates_of(def_id)
171 .instantiate(self.tcx(), substs);
172 for instantiated_predicate in instantiated_predicates.predicates {
173 let instantiated_predicate = self.normalize(instantiated_predicate);
174 self.prove_predicate(instantiated_predicate);
177 // In addition to proving the predicates, we have to
178 // prove that `ty` is well-formed -- this is because
179 // the WF of `ty` is predicated on the substs being
180 // well-formed, and we haven't proven *that*. We don't
181 // want to prove the WF of types from `substs` directly because they
182 // haven't been normalized.
184 // FIXME(nmatsakis): Well, perhaps we should normalize
185 // them? This would only be relevant if some input
186 // type were ill-formed but did not appear in `ty`,
187 // which...could happen with normalization...
188 self.prove_predicate(Predicate::WellFormed(ty));
195 tcx: TyCtxt<'_, 'tcx, 'tcx>,
196 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
197 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
199 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
200 let (param_env, Eq { a, b }) = key.into_parts();
202 .at(&ObligationCause::dummy(), param_env)
204 .into_value_registering_obligations(infcx, fulfill_cx))
208 fn type_op_normalize<T>(
209 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
210 fulfill_cx: &mut FulfillmentContext<'tcx>,
211 key: ParamEnvAnd<'tcx, Normalize<T>>,
214 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'gcx>,
216 let (param_env, Normalize { value }) = key.into_parts();
217 let Normalized { value, obligations } = infcx
218 .at(&ObligationCause::dummy(), param_env)
220 fulfill_cx.register_predicate_obligations(infcx, obligations);
224 fn type_op_normalize_ty(
225 tcx: TyCtxt<'_, 'tcx, 'tcx>,
226 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
227 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>>, NoSolution> {
229 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
232 fn type_op_normalize_predicate(
233 tcx: TyCtxt<'_, 'tcx, 'tcx>,
234 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
235 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>>, NoSolution> {
237 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
240 fn type_op_normalize_fn_sig(
241 tcx: TyCtxt<'_, 'tcx, 'tcx>,
242 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
243 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>>, NoSolution> {
245 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
248 fn type_op_normalize_poly_fn_sig(
249 tcx: TyCtxt<'_, 'tcx, 'tcx>,
250 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
251 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>>, NoSolution> {
253 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
256 fn type_op_subtype<'tcx>(
257 tcx: TyCtxt<'_, 'tcx, 'tcx>,
258 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
259 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
261 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
262 let (param_env, Subtype { sub, sup }) = key.into_parts();
264 .at(&ObligationCause::dummy(), param_env)
266 .into_value_registering_obligations(infcx, fulfill_cx))
270 fn type_op_prove_predicate<'tcx>(
271 tcx: TyCtxt<'_, 'tcx, 'tcx>,
272 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
273 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
275 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
276 let (param_env, ProvePredicate { predicate }) = key.into_parts();
277 fulfill_cx.register_predicate_obligation(
279 Obligation::new(ObligationCause::dummy(), param_env, predicate),