2 use rustc_hir::def_id::DefId;
3 use rustc_infer::infer::at::ToTrace;
4 use rustc_infer::infer::canonical::{Canonical, QueryResponse};
5 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
6 use rustc_infer::traits::TraitEngineExt as _;
7 use rustc_middle::ty::query::Providers;
8 use rustc_middle::ty::subst::{GenericArg, Subst, UserSelfTy, UserSubsts};
9 use rustc_middle::ty::{
10 self, FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, Ty, TyCtxt, TypeFoldable,
13 use rustc_span::DUMMY_SP;
14 use rustc_trait_selection::infer::InferCtxtBuilderExt;
15 use rustc_trait_selection::infer::InferCtxtExt;
16 use rustc_trait_selection::traits::query::normalize::AtExt;
17 use rustc_trait_selection::traits::query::type_op::ascribe_user_type::AscribeUserType;
18 use rustc_trait_selection::traits::query::type_op::eq::Eq;
19 use rustc_trait_selection::traits::query::type_op::normalize::Normalize;
20 use rustc_trait_selection::traits::query::type_op::prove_predicate::ProvePredicate;
21 use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
22 use rustc_trait_selection::traits::query::{Fallible, NoSolution};
23 use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, TraitEngine};
26 crate fn provide(p: &mut Providers<'_>) {
28 type_op_ascribe_user_type,
30 type_op_prove_predicate,
33 type_op_normalize_predicate,
34 type_op_normalize_fn_sig,
35 type_op_normalize_poly_fn_sig,
40 fn type_op_ascribe_user_type<'tcx>(
42 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
43 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
44 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
45 let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
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);
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<'tcx> {
102 fn subst<T>(&self, value: T, substs: &[GenericArg<'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> {
115 let UserSubsts { user_self_ty, substs } = user_substs;
116 let tcx = self.tcx();
118 let ty = tcx.type_of(def_id);
119 let ty = self.subst(ty, substs);
120 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
121 let ty = self.normalize(ty);
123 self.relate(mir_ty, Variance::Invariant, ty)?;
125 // Prove the predicates coming along with `def_id`.
127 // Also, normalize the `instantiated_predicates`
128 // because otherwise we wind up with duplicate "type
129 // outlives" error messages.
130 let instantiated_predicates =
131 self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
132 for instantiated_predicate in instantiated_predicates.predicates {
133 let instantiated_predicate = self.normalize(instantiated_predicate);
134 self.prove_predicate(instantiated_predicate);
137 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
138 let impl_self_ty = self.tcx().type_of(impl_def_id);
139 let impl_self_ty = self.subst(impl_self_ty, &substs);
140 let impl_self_ty = self.normalize(impl_self_ty);
142 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
144 self.prove_predicate(ty::PredicateKind::WellFormed(impl_self_ty));
147 // In addition to proving the predicates, we have to
148 // prove that `ty` is well-formed -- this is because
149 // the WF of `ty` is predicated on the substs being
150 // well-formed, and we haven't proven *that*. We don't
151 // want to prove the WF of types from `substs` directly because they
152 // haven't been normalized.
154 // FIXME(nmatsakis): Well, perhaps we should normalize
155 // them? This would only be relevant if some input
156 // type were ill-formed but did not appear in `ty`,
157 // which...could happen with normalization...
158 self.prove_predicate(ty::PredicateKind::WellFormed(ty));
165 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
166 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
167 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
168 let (param_env, Eq { a, b }) = key.into_parts();
170 .at(&ObligationCause::dummy(), param_env)
172 .into_value_registering_obligations(infcx, fulfill_cx);
177 fn type_op_normalize<T>(
178 infcx: &InferCtxt<'_, 'tcx>,
179 fulfill_cx: &mut dyn TraitEngine<'tcx>,
180 key: ParamEnvAnd<'tcx, Normalize<T>>,
183 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
185 let (param_env, Normalize { value }) = key.into_parts();
186 let Normalized { value, obligations } =
187 infcx.at(&ObligationCause::dummy(), param_env).normalize(&value)?;
188 fulfill_cx.register_predicate_obligations(infcx, obligations);
192 fn type_op_normalize_ty(
194 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
195 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
196 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
199 fn type_op_normalize_predicate(
201 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
202 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
203 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
206 fn type_op_normalize_fn_sig(
208 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
209 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
210 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
213 fn type_op_normalize_poly_fn_sig(
215 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
216 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
217 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
220 fn type_op_subtype<'tcx>(
222 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
223 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
224 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
225 let (param_env, Subtype { sub, sup }) = key.into_parts();
227 .at(&ObligationCause::dummy(), param_env)
229 .into_value_registering_obligations(infcx, fulfill_cx);
234 fn type_op_prove_predicate<'tcx>(
236 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
237 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
238 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
239 let (param_env, ProvePredicate { predicate }) = key.into_parts();
240 fulfill_cx.register_predicate_obligation(
242 Obligation::new(ObligationCause::dummy(), param_env, predicate),