1 use rustc::ty::query::Providers;
2 use rustc::ty::subst::{GenericArg, Subst, UserSelfTy, UserSubsts};
4 FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, Ty, TyCtxt, TypeFoldable, Variance,
7 use rustc_hir::def_id::DefId;
8 use rustc_infer::infer::at::ToTrace;
9 use rustc_infer::infer::canonical::{Canonical, QueryResponse};
10 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
11 use rustc_infer::traits::query::type_op::ascribe_user_type::AscribeUserType;
12 use rustc_infer::traits::query::type_op::eq::Eq;
13 use rustc_infer::traits::query::type_op::normalize::Normalize;
14 use rustc_infer::traits::query::type_op::prove_predicate::ProvePredicate;
15 use rustc_infer::traits::query::type_op::subtype::Subtype;
16 use rustc_infer::traits::query::{Fallible, NoSolution};
17 use rustc_infer::traits::{Normalized, Obligation, ObligationCause, TraitEngine, TraitEngineExt};
18 use rustc_span::DUMMY_SP;
21 crate fn provide(p: &mut Providers<'_>) {
23 type_op_ascribe_user_type,
25 type_op_prove_predicate,
28 type_op_normalize_predicate,
29 type_op_normalize_fn_sig,
30 type_op_normalize_poly_fn_sig,
35 fn type_op_ascribe_user_type<'tcx>(
37 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
38 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
39 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
40 let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
43 "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
44 mir_ty, def_id, user_substs
47 let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx };
48 cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
54 struct AscribeUserTypeCx<'me, 'tcx> {
55 infcx: &'me InferCtxt<'me, 'tcx>,
56 param_env: ParamEnv<'tcx>,
57 fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
60 impl AscribeUserTypeCx<'me, 'tcx> {
61 fn normalize<T>(&mut self, value: T) -> T
63 T: TypeFoldable<'tcx>,
66 .partially_normalize_associated_types_in(
72 .into_value_registering_obligations(self.infcx, self.fulfill_cx)
75 fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
81 .at(&ObligationCause::dummy(), self.param_env)
82 .relate(a, variance, b)?
83 .into_value_registering_obligations(self.infcx, self.fulfill_cx))
86 fn prove_predicate(&mut self, predicate: Predicate<'tcx>) {
87 self.fulfill_cx.register_predicate_obligation(
89 Obligation::new(ObligationCause::dummy(), self.param_env, predicate),
93 fn tcx(&self) -> TyCtxt<'tcx> {
97 fn subst<T>(&self, value: T, substs: &[GenericArg<'tcx>]) -> T
99 T: TypeFoldable<'tcx>,
101 value.subst(self.tcx(), substs)
104 fn relate_mir_and_user_ty(
108 user_substs: UserSubsts<'tcx>,
109 ) -> Result<(), NoSolution> {
110 let UserSubsts { user_self_ty, substs } = user_substs;
111 let tcx = self.tcx();
113 let ty = tcx.type_of(def_id);
114 let ty = self.subst(ty, substs);
115 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
116 let ty = self.normalize(ty);
118 self.relate(mir_ty, Variance::Invariant, ty)?;
120 // Prove the predicates coming along with `def_id`.
122 // Also, normalize the `instantiated_predicates`
123 // because otherwise we wind up with duplicate "type
124 // outlives" error messages.
125 let instantiated_predicates =
126 self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
127 for instantiated_predicate in instantiated_predicates.predicates {
128 let instantiated_predicate = self.normalize(instantiated_predicate);
129 self.prove_predicate(instantiated_predicate);
132 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
133 let impl_self_ty = self.tcx().type_of(impl_def_id);
134 let impl_self_ty = self.subst(impl_self_ty, &substs);
135 let impl_self_ty = self.normalize(impl_self_ty);
137 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
139 self.prove_predicate(Predicate::WellFormed(impl_self_ty));
142 // In addition to proving the predicates, we have to
143 // prove that `ty` is well-formed -- this is because
144 // the WF of `ty` is predicated on the substs being
145 // well-formed, and we haven't proven *that*. We don't
146 // want to prove the WF of types from `substs` directly because they
147 // haven't been normalized.
149 // FIXME(nmatsakis): Well, perhaps we should normalize
150 // them? This would only be relevant if some input
151 // type were ill-formed but did not appear in `ty`,
152 // which...could happen with normalization...
153 self.prove_predicate(Predicate::WellFormed(ty));
160 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
161 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
162 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
163 let (param_env, Eq { a, b }) = key.into_parts();
165 .at(&ObligationCause::dummy(), param_env)
167 .into_value_registering_obligations(infcx, fulfill_cx))
171 fn type_op_normalize<T>(
172 infcx: &InferCtxt<'_, 'tcx>,
173 fulfill_cx: &mut dyn TraitEngine<'tcx>,
174 key: ParamEnvAnd<'tcx, Normalize<T>>,
177 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
179 let (param_env, Normalize { value }) = key.into_parts();
180 let Normalized { value, obligations } =
181 infcx.at(&ObligationCause::dummy(), param_env).normalize(&value)?;
182 fulfill_cx.register_predicate_obligations(infcx, obligations);
186 fn type_op_normalize_ty(
188 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
189 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
190 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
193 fn type_op_normalize_predicate(
195 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
196 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
197 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
200 fn type_op_normalize_fn_sig(
202 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
203 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
204 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
207 fn type_op_normalize_poly_fn_sig(
209 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
210 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
211 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
214 fn type_op_subtype<'tcx>(
216 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
217 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
218 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
219 let (param_env, Subtype { sub, sup }) = key.into_parts();
221 .at(&ObligationCause::dummy(), param_env)
223 .into_value_registering_obligations(infcx, fulfill_cx))
227 fn type_op_prove_predicate<'tcx>(
229 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
230 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
231 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
232 let (param_env, ProvePredicate { predicate }) = key.into_parts();
233 fulfill_cx.register_predicate_obligation(
235 Obligation::new(ObligationCause::dummy(), param_env, predicate),