2 use rustc_infer::infer::canonical::{Canonical, QueryResponse};
3 use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
4 use rustc_infer::traits::ObligationCauseCode;
5 use rustc_middle::ty::query::Providers;
6 use rustc_middle::ty::{self, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable};
7 use rustc_middle::ty::{ParamEnvAnd, Predicate};
8 use rustc_middle::ty::{UserSelfTy, UserSubsts, UserType};
9 use rustc_span::def_id::CRATE_DEF_ID;
10 use rustc_span::{Span, DUMMY_SP};
11 use rustc_trait_selection::infer::InferCtxtBuilderExt;
12 use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
13 use rustc_trait_selection::traits::query::type_op::ascribe_user_type::AscribeUserType;
14 use rustc_trait_selection::traits::query::type_op::eq::Eq;
15 use rustc_trait_selection::traits::query::type_op::normalize::Normalize;
16 use rustc_trait_selection::traits::query::type_op::prove_predicate::ProvePredicate;
17 use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
18 use rustc_trait_selection::traits::query::{Fallible, NoSolution};
19 use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, ObligationCtxt};
22 pub(crate) fn provide(p: &mut Providers) {
24 type_op_ascribe_user_type,
26 type_op_prove_predicate,
29 type_op_normalize_predicate,
30 type_op_normalize_fn_sig,
31 type_op_normalize_poly_fn_sig,
36 fn type_op_ascribe_user_type<'tcx>(
38 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
39 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
40 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |ocx, key| {
41 type_op_ascribe_user_type_with_span(ocx, key, None)
45 /// The core of the `type_op_ascribe_user_type` query: for diagnostics purposes in NLL HRTB errors,
46 /// this query can be re-run to better track the span of the obligation cause, and improve the error
47 /// message. Do not call directly unless you're in that very specific context.
48 pub fn type_op_ascribe_user_type_with_span<'tcx>(
49 ocx: &ObligationCtxt<'_, 'tcx>,
50 key: ParamEnvAnd<'tcx, AscribeUserType<'tcx>>,
52 ) -> Result<(), NoSolution> {
53 let (param_env, AscribeUserType { mir_ty, user_ty }) = key.into_parts();
54 debug!("type_op_ascribe_user_type: mir_ty={:?} user_ty={:?}", mir_ty, user_ty);
55 let span = span.unwrap_or(DUMMY_SP);
57 UserType::Ty(user_ty) => relate_mir_and_user_ty(ocx, param_env, span, mir_ty, user_ty)?,
58 UserType::TypeOf(def_id, user_substs) => {
59 relate_mir_and_user_substs(ocx, param_env, span, mir_ty, def_id, user_substs)?
65 #[instrument(level = "debug", skip(ocx, param_env, span))]
66 fn relate_mir_and_user_ty<'tcx>(
67 ocx: &ObligationCtxt<'_, 'tcx>,
68 param_env: ty::ParamEnv<'tcx>,
72 ) -> Result<(), NoSolution> {
73 let cause = ObligationCause::dummy_with_span(span);
74 let user_ty = ocx.normalize(&cause, param_env, user_ty);
75 ocx.eq(&cause, param_env, mir_ty, user_ty)?;
77 // FIXME(#104764): We should check well-formedness before normalization.
78 let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(user_ty.into()));
79 ocx.register_obligation(Obligation::new(ocx.infcx.tcx, cause, param_env, predicate));
83 #[instrument(level = "debug", skip(ocx, param_env, span))]
84 fn relate_mir_and_user_substs<'tcx>(
85 ocx: &ObligationCtxt<'_, 'tcx>,
86 param_env: ty::ParamEnv<'tcx>,
89 def_id: hir::def_id::DefId,
90 user_substs: UserSubsts<'tcx>,
91 ) -> Result<(), NoSolution> {
92 let UserSubsts { user_self_ty, substs } = user_substs;
93 let tcx = ocx.infcx.tcx;
94 let cause = ObligationCause::dummy_with_span(span);
96 let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
97 let ty = ocx.normalize(&cause, param_env, ty);
98 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
100 ocx.eq(&cause, param_env, mir_ty, ty)?;
102 // Prove the predicates coming along with `def_id`.
104 // Also, normalize the `instantiated_predicates`
105 // because otherwise we wind up with duplicate "type
106 // outlives" error messages.
107 let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
109 debug!(?instantiated_predicates);
110 for (instantiated_predicate, predicate_span) in instantiated_predicates {
111 let span = if span == DUMMY_SP { predicate_span } else { span };
112 let cause = ObligationCause::new(
115 ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
117 let instantiated_predicate =
118 ocx.normalize(&cause.clone(), param_env, instantiated_predicate);
120 ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate));
123 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
124 let self_ty = ocx.normalize(&cause, param_env, self_ty);
125 let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs);
126 let impl_self_ty = ocx.normalize(&cause, param_env, impl_self_ty);
128 ocx.eq(&cause, param_env, self_ty, impl_self_ty)?;
129 let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()));
130 ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
133 // In addition to proving the predicates, we have to
134 // prove that `ty` is well-formed -- this is because
135 // the WF of `ty` is predicated on the substs being
136 // well-formed, and we haven't proven *that*. We don't
137 // want to prove the WF of types from `substs` directly because they
138 // haven't been normalized.
140 // FIXME(nmatsakis): Well, perhaps we should normalize
141 // them? This would only be relevant if some input
142 // type were ill-formed but did not appear in `ty`,
143 // which...could happen with normalization...
144 let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()));
145 ocx.register_obligation(Obligation::new(tcx, cause, param_env, predicate));
151 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
152 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
153 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |ocx, key| {
154 let (param_env, Eq { a, b }) = key.into_parts();
155 Ok(ocx.eq(&ObligationCause::dummy(), param_env, a, b)?)
159 fn type_op_normalize<'tcx, T>(
160 ocx: &ObligationCtxt<'_, 'tcx>,
161 key: ParamEnvAnd<'tcx, Normalize<T>>,
164 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
166 let (param_env, Normalize { value }) = key.into_parts();
167 let Normalized { value, obligations } =
168 ocx.infcx.at(&ObligationCause::dummy(), param_env).query_normalize(value)?;
169 ocx.register_obligations(obligations);
173 fn type_op_normalize_ty<'tcx>(
175 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
176 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
177 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
180 fn type_op_normalize_predicate<'tcx>(
182 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
183 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
184 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
187 fn type_op_normalize_fn_sig<'tcx>(
189 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
190 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
191 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
194 fn type_op_normalize_poly_fn_sig<'tcx>(
196 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
197 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
198 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
201 fn type_op_subtype<'tcx>(
203 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
204 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
205 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |ocx, key| {
206 let (param_env, Subtype { sub, sup }) = key.into_parts();
207 Ok(ocx.sup(&ObligationCause::dummy(), param_env, sup, sub)?)
211 fn type_op_prove_predicate<'tcx>(
213 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
214 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
215 // HACK This bubble is required for this test to pass:
216 // impl-trait/issue-99642.rs
217 tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).enter_canonical_trait_query(
220 type_op_prove_predicate_with_cause(ocx, key, ObligationCause::dummy());
226 /// The core of the `type_op_prove_predicate` query: for diagnostics purposes in NLL HRTB errors,
227 /// this query can be re-run to better track the span of the obligation cause, and improve the error
228 /// message. Do not call directly unless you're in that very specific context.
229 pub fn type_op_prove_predicate_with_cause<'tcx>(
230 ocx: &ObligationCtxt<'_, 'tcx>,
231 key: ParamEnvAnd<'tcx, ProvePredicate<'tcx>>,
232 cause: ObligationCause<'tcx>,
234 let (param_env, ProvePredicate { predicate }) = key.into_parts();
235 ocx.register_obligation(Obligation::new(ocx.infcx.tcx, cause, param_env, predicate));