1 use rustc::infer::at::ToTrace;
2 use rustc::infer::canonical::{Canonical, QueryResponse};
3 use rustc::infer::InferCtxt;
5 use rustc::hir::def_id::DefId;
6 use rustc::traits::query::type_op::ascribe_user_type::AscribeUserType;
7 use rustc::traits::query::type_op::eq::Eq;
8 use rustc::traits::query::type_op::normalize::Normalize;
9 use rustc::traits::query::type_op::prove_predicate::ProvePredicate;
10 use rustc::traits::query::type_op::subtype::Subtype;
11 use rustc::traits::query::{Fallible, NoSolution};
13 Normalized, Obligation, ObligationCause, TraitEngine, TraitEngineExt,
15 use rustc::ty::query::Providers;
16 use rustc::ty::subst::{GenericArg, Subst, UserSubsts, UserSelfTy};
18 FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, Ty, TyCtxt, TypeFoldable, Variance,
21 use syntax_pos::DUMMY_SP;
23 crate fn provide(p: &mut Providers<'_>) {
25 type_op_ascribe_user_type,
27 type_op_prove_predicate,
30 type_op_normalize_predicate,
31 type_op_normalize_fn_sig,
32 type_op_normalize_poly_fn_sig,
37 fn type_op_ascribe_user_type<'tcx>(
39 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
40 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
42 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
44 param_env, AscribeUserType { mir_ty, def_id, user_substs }
48 "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
49 mir_ty, def_id, user_substs
52 let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx };
53 cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
59 struct AscribeUserTypeCx<'me, 'tcx> {
60 infcx: &'me InferCtxt<'me, 'tcx>,
61 param_env: ParamEnv<'tcx>,
62 fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
65 impl AscribeUserTypeCx<'me, 'tcx> {
66 fn normalize<T>(&mut self, value: T) -> T
68 T: TypeFoldable<'tcx>,
71 .partially_normalize_associated_types_in(
77 .into_value_registering_obligations(self.infcx, self.fulfill_cx)
80 fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
85 .at(&ObligationCause::dummy(), self.param_env)
86 .relate(a, variance, b)?
87 .into_value_registering_obligations(self.infcx, self.fulfill_cx))
90 fn prove_predicate(&mut self, predicate: Predicate<'tcx>) {
91 self.fulfill_cx.register_predicate_obligation(
93 Obligation::new(ObligationCause::dummy(), self.param_env, predicate),
97 fn tcx(&self) -> TyCtxt<'tcx> {
101 fn subst<T>(&self, value: T, substs: &[GenericArg<'tcx>]) -> T
103 T: TypeFoldable<'tcx>,
105 value.subst(self.tcx(), substs)
108 fn relate_mir_and_user_ty(
112 user_substs: UserSubsts<'tcx>,
113 ) -> Result<(), NoSolution> {
118 let tcx = self.tcx();
120 let ty = tcx.type_of(def_id);
121 let ty = self.subst(ty, substs);
122 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
123 let ty = self.normalize(ty);
125 self.relate(mir_ty, Variance::Invariant, ty)?;
127 // Prove the predicates coming along with `def_id`.
129 // Also, normalize the `instantiated_predicates`
130 // because otherwise we wind up with duplicate "type
131 // outlives" error messages.
132 let instantiated_predicates = self.tcx()
133 .predicates_of(def_id)
134 .instantiate(self.tcx(), substs);
135 for instantiated_predicate in instantiated_predicates.predicates {
136 let instantiated_predicate = self.normalize(instantiated_predicate);
137 self.prove_predicate(instantiated_predicate);
140 if let Some(UserSelfTy {
144 let impl_self_ty = self.tcx().type_of(impl_def_id);
145 let impl_self_ty = self.subst(impl_self_ty, &substs);
146 let impl_self_ty = self.normalize(impl_self_ty);
148 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
150 self.prove_predicate(Predicate::WellFormed(impl_self_ty));
153 // In addition to proving the predicates, we have to
154 // prove that `ty` is well-formed -- this is because
155 // the WF of `ty` is predicated on the substs being
156 // well-formed, and we haven't proven *that*. We don't
157 // want to prove the WF of types from `substs` directly because they
158 // haven't been normalized.
160 // FIXME(nmatsakis): Well, perhaps we should normalize
161 // them? This would only be relevant if some input
162 // type were ill-formed but did not appear in `ty`,
163 // which...could happen with normalization...
164 self.prove_predicate(Predicate::WellFormed(ty));
171 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
172 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
174 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
175 let (param_env, Eq { a, b }) = key.into_parts();
177 .at(&ObligationCause::dummy(), param_env)
179 .into_value_registering_obligations(infcx, fulfill_cx))
183 fn type_op_normalize<T>(
184 infcx: &InferCtxt<'_, 'tcx>,
185 fulfill_cx: &mut dyn TraitEngine<'tcx>,
186 key: ParamEnvAnd<'tcx, Normalize<T>>,
189 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
191 let (param_env, Normalize { value }) = key.into_parts();
192 let Normalized { value, obligations } = infcx
193 .at(&ObligationCause::dummy(), param_env)
195 fulfill_cx.register_predicate_obligations(infcx, obligations);
199 fn type_op_normalize_ty(
201 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
202 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
204 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
207 fn type_op_normalize_predicate(
209 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
210 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
212 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
215 fn type_op_normalize_fn_sig(
217 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
218 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
220 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
223 fn type_op_normalize_poly_fn_sig(
225 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
226 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
228 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
231 fn type_op_subtype<'tcx>(
233 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
234 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
236 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
237 let (param_env, Subtype { sub, sup }) = key.into_parts();
239 .at(&ObligationCause::dummy(), param_env)
241 .into_value_registering_obligations(infcx, fulfill_cx))
245 fn type_op_prove_predicate<'tcx>(
247 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
248 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
250 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
251 let (param_env, ProvePredicate { predicate }) = key.into_parts();
252 fulfill_cx.register_predicate_obligation(
254 Obligation::new(ObligationCause::dummy(), param_env, predicate),