1 use rustc::infer::at::ToTrace;
2 use rustc::infer::canonical::{Canonical, QueryResponse};
3 use rustc::infer::InferCtxt;
4 use rustc::hir::def_id::DefId;
5 use rustc::traits::query::type_op::ascribe_user_type::AscribeUserType;
6 use rustc::traits::query::type_op::eq::Eq;
7 use rustc::traits::query::type_op::normalize::Normalize;
8 use rustc::traits::query::type_op::prove_predicate::ProvePredicate;
9 use rustc::traits::query::type_op::subtype::Subtype;
10 use rustc::traits::query::{Fallible, NoSolution};
12 Normalized, Obligation, ObligationCause, TraitEngine, TraitEngineExt,
14 use rustc::ty::query::Providers;
15 use rustc::ty::subst::{Kind, Subst, UserSubsts, UserSelfTy};
17 FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, Ty, TyCtxt, TypeFoldable, Variance,
19 use rustc_data_structures::sync::Lrc;
22 use syntax_pos::DUMMY_SP;
24 crate fn provide(p: &mut Providers<'_>) {
26 type_op_ascribe_user_type,
28 type_op_prove_predicate,
31 type_op_normalize_predicate,
32 type_op_normalize_fn_sig,
33 type_op_normalize_poly_fn_sig,
38 fn type_op_ascribe_user_type<'tcx>(
39 tcx: TyCtxt<'_, 'tcx, 'tcx>,
40 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
41 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
43 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
45 param_env, AscribeUserType { mir_ty, def_id, user_substs }
49 "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
50 mir_ty, def_id, user_substs
53 let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx };
54 cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
60 struct AscribeUserTypeCx<'me, 'gcx: 'tcx, 'tcx: 'me> {
61 infcx: &'me InferCtxt<'me, 'gcx, 'tcx>,
62 param_env: ParamEnv<'tcx>,
63 fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
66 impl AscribeUserTypeCx<'me, 'gcx, 'tcx> {
67 fn normalize<T>(&mut self, value: T) -> T
69 T: TypeFoldable<'tcx>,
72 .partially_normalize_associated_types_in(
78 .into_value_registering_obligations(self.infcx, self.fulfill_cx)
81 fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
86 .at(&ObligationCause::dummy(), self.param_env)
87 .relate(a, variance, b)?
88 .into_value_registering_obligations(self.infcx, self.fulfill_cx))
91 fn prove_predicate(&mut self, predicate: Predicate<'tcx>) {
92 self.fulfill_cx.register_predicate_obligation(
94 Obligation::new(ObligationCause::dummy(), self.param_env, predicate),
98 fn tcx(&self) -> TyCtxt<'me, 'gcx, 'tcx> {
102 fn subst<T>(&self, value: T, substs: &[Kind<'tcx>]) -> T
104 T: TypeFoldable<'tcx>,
106 value.subst(self.tcx(), substs)
109 fn relate_mir_and_user_ty(
113 user_substs: UserSubsts<'tcx>,
114 ) -> Result<(), NoSolution> {
119 let tcx = self.tcx();
121 let ty = tcx.type_of(def_id);
122 let ty = self.subst(ty, substs);
123 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
124 let ty = self.normalize(ty);
126 self.relate(mir_ty, Variance::Invariant, ty)?;
128 // Prove the predicates coming along with `def_id`.
130 // Also, normalize the `instantiated_predicates`
131 // because otherwise we wind up with duplicate "type
132 // outlives" error messages.
133 let instantiated_predicates = self.tcx()
134 .predicates_of(def_id)
135 .instantiate(self.tcx(), substs);
136 for instantiated_predicate in instantiated_predicates.predicates {
137 let instantiated_predicate = self.normalize(instantiated_predicate);
138 self.prove_predicate(instantiated_predicate);
141 if let Some(UserSelfTy {
145 let impl_self_ty = self.tcx().type_of(impl_def_id);
146 let impl_self_ty = self.subst(impl_self_ty, &substs);
147 let impl_self_ty = self.normalize(impl_self_ty);
149 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
151 self.prove_predicate(Predicate::WellFormed(impl_self_ty));
154 // In addition to proving the predicates, we have to
155 // prove that `ty` is well-formed -- this is because
156 // the WF of `ty` is predicated on the substs being
157 // well-formed, and we haven't proven *that*. We don't
158 // want to prove the WF of types from `substs` directly because they
159 // haven't been normalized.
161 // FIXME(nmatsakis): Well, perhaps we should normalize
162 // them? This would only be relevant if some input
163 // type were ill-formed but did not appear in `ty`,
164 // which...could happen with normalization...
165 self.prove_predicate(Predicate::WellFormed(ty));
171 tcx: TyCtxt<'_, 'tcx, 'tcx>,
172 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
173 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
175 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
176 let (param_env, Eq { a, b }) = key.into_parts();
178 .at(&ObligationCause::dummy(), param_env)
180 .into_value_registering_obligations(infcx, fulfill_cx))
184 fn type_op_normalize<T>(
185 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
186 fulfill_cx: &mut dyn TraitEngine<'tcx>,
187 key: ParamEnvAnd<'tcx, Normalize<T>>,
190 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'gcx>,
192 let (param_env, Normalize { value }) = key.into_parts();
193 let Normalized { value, obligations } = infcx
194 .at(&ObligationCause::dummy(), param_env)
196 fulfill_cx.register_predicate_obligations(infcx, obligations);
200 fn type_op_normalize_ty(
201 tcx: TyCtxt<'_, 'tcx, 'tcx>,
202 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
203 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>>, NoSolution> {
205 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
208 fn type_op_normalize_predicate(
209 tcx: TyCtxt<'_, 'tcx, 'tcx>,
210 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
211 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>>, NoSolution> {
213 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
216 fn type_op_normalize_fn_sig(
217 tcx: TyCtxt<'_, 'tcx, 'tcx>,
218 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
219 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>>, NoSolution> {
221 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
224 fn type_op_normalize_poly_fn_sig(
225 tcx: TyCtxt<'_, 'tcx, 'tcx>,
226 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
227 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>>, NoSolution> {
229 .enter_canonical_trait_query(&canonicalized, type_op_normalize)
232 fn type_op_subtype<'tcx>(
233 tcx: TyCtxt<'_, 'tcx, 'tcx>,
234 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
235 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
237 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
238 let (param_env, Subtype { sub, sup }) = key.into_parts();
240 .at(&ObligationCause::dummy(), param_env)
242 .into_value_registering_obligations(infcx, fulfill_cx))
246 fn type_op_prove_predicate<'tcx>(
247 tcx: TyCtxt<'_, 'tcx, 'tcx>,
248 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
249 ) -> Result<Lrc<Canonical<'tcx, QueryResponse<'tcx, ()>>>, NoSolution> {
251 .enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
252 let (param_env, ProvePredicate { predicate }) = key.into_parts();
253 fulfill_cx.register_predicate_obligation(
255 Obligation::new(ObligationCause::dummy(), param_env, predicate),