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::{DefiningAnchor, InferCtxt, TyCtxtInferExt};
6 use rustc_infer::traits::{ObligationCauseCode, TraitEngineExt as _};
7 use rustc_middle::ty::query::Providers;
8 use rustc_middle::ty::{
9 self, EarlyBinder, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable, Variance,
11 use rustc_middle::ty::{GenericArg, UserSelfTy, UserSubsts};
12 use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Predicate, ToPredicate};
13 use rustc_span::{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};
27 pub(crate) fn provide(p: &mut Providers) {
29 type_op_ascribe_user_type,
31 type_op_prove_predicate,
34 type_op_normalize_predicate,
35 type_op_normalize_fn_sig,
36 type_op_normalize_poly_fn_sig,
41 fn type_op_ascribe_user_type<'tcx>(
43 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>,
44 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
45 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
46 type_op_ascribe_user_type_with_span(infcx, fulfill_cx, key, None)
50 /// The core of the `type_op_ascribe_user_type` query: for diagnostics purposes in NLL HRTB errors,
51 /// this query can be re-run to better track the span of the obligation cause, and improve the error
52 /// message. Do not call directly unless you're in that very specific context.
53 pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>(
54 infcx: &'a InferCtxt<'a, 'tcx>,
55 fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
56 key: ParamEnvAnd<'tcx, AscribeUserType<'tcx>>,
58 ) -> Result<(), NoSolution> {
59 let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
61 "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
62 mir_ty, def_id, user_substs
65 let mut cx = AscribeUserTypeCx { infcx, param_env, span: span.unwrap_or(DUMMY_SP), fulfill_cx };
66 cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?;
70 struct AscribeUserTypeCx<'me, 'tcx> {
71 infcx: &'me InferCtxt<'me, 'tcx>,
72 param_env: ParamEnv<'tcx>,
74 fulfill_cx: &'me mut dyn TraitEngine<'tcx>,
77 impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
78 fn normalize<T>(&mut self, value: T) -> T
80 T: TypeFoldable<'tcx>,
82 self.normalize_with_cause(value, ObligationCause::misc(self.span, hir::CRATE_HIR_ID))
85 fn normalize_with_cause<T>(&mut self, value: T, cause: ObligationCause<'tcx>) -> T
87 T: TypeFoldable<'tcx>,
90 .partially_normalize_associated_types_in(cause, self.param_env, value)
91 .into_value_registering_obligations(self.infcx, self.fulfill_cx)
94 fn relate<T>(&mut self, a: T, variance: Variance, b: T) -> Result<(), NoSolution>
99 .at(&ObligationCause::dummy_with_span(self.span), self.param_env)
100 .relate(a, variance, b)?
101 .into_value_registering_obligations(self.infcx, self.fulfill_cx);
105 fn prove_predicate(&mut self, predicate: Predicate<'tcx>, cause: ObligationCause<'tcx>) {
106 self.fulfill_cx.register_predicate_obligation(
108 Obligation::new(cause, self.param_env, predicate),
112 fn tcx(&self) -> TyCtxt<'tcx> {
116 fn subst<T>(&self, value: T, substs: &[GenericArg<'tcx>]) -> T
118 T: TypeFoldable<'tcx>,
120 EarlyBinder(value).subst(self.tcx(), substs)
123 #[instrument(level = "debug", skip(self))]
124 fn relate_mir_and_user_ty(
128 user_substs: UserSubsts<'tcx>,
129 ) -> Result<(), NoSolution> {
130 let UserSubsts { user_self_ty, substs } = user_substs;
131 let tcx = self.tcx();
133 let ty = tcx.type_of(def_id);
134 let ty = self.subst(ty, substs);
135 let ty = self.normalize(ty);
136 debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
138 self.relate(mir_ty, Variance::Invariant, ty)?;
140 // Prove the predicates coming along with `def_id`.
142 // Also, normalize the `instantiated_predicates`
143 // because otherwise we wind up with duplicate "type
144 // outlives" error messages.
145 let instantiated_predicates =
146 self.tcx().predicates_of(def_id).instantiate(self.tcx(), substs);
148 let cause = ObligationCause::dummy_with_span(self.span);
150 debug!(?instantiated_predicates);
151 for (instantiated_predicate, predicate_span) in
152 zip(instantiated_predicates.predicates, instantiated_predicates.spans)
154 let span = if self.span == DUMMY_SP { predicate_span } else { self.span };
155 let cause = ObligationCause::new(
158 ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
160 let instantiated_predicate =
161 self.normalize_with_cause(instantiated_predicate, cause.clone());
162 self.prove_predicate(instantiated_predicate, cause);
165 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
166 let impl_self_ty = self.tcx().type_of(impl_def_id);
167 let impl_self_ty = self.subst(impl_self_ty, &substs);
168 let impl_self_ty = self.normalize(impl_self_ty);
170 self.relate(self_ty, Variance::Invariant, impl_self_ty)?;
172 self.prove_predicate(
173 ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()))
174 .to_predicate(self.tcx()),
179 // In addition to proving the predicates, we have to
180 // prove that `ty` is well-formed -- this is because
181 // the WF of `ty` is predicated on the substs being
182 // well-formed, and we haven't proven *that*. We don't
183 // want to prove the WF of types from `substs` directly because they
184 // haven't been normalized.
186 // FIXME(nmatsakis): Well, perhaps we should normalize
187 // them? This would only be relevant if some input
188 // type were ill-formed but did not appear in `ty`,
189 // which...could happen with normalization...
190 self.prove_predicate(
191 ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(self.tcx()),
200 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
201 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
202 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
203 let (param_env, Eq { a, b }) = key.into_parts();
205 .at(&ObligationCause::dummy(), param_env)
207 .into_value_registering_obligations(infcx, fulfill_cx);
212 fn type_op_normalize<'tcx, T>(
213 infcx: &InferCtxt<'_, 'tcx>,
214 fulfill_cx: &mut dyn TraitEngine<'tcx>,
215 key: ParamEnvAnd<'tcx, Normalize<T>>,
218 T: fmt::Debug + TypeFoldable<'tcx> + Lift<'tcx>,
220 let (param_env, Normalize { value }) = key.into_parts();
221 let Normalized { value, obligations } =
222 infcx.at(&ObligationCause::dummy(), param_env).normalize(value)?;
223 fulfill_cx.register_predicate_obligations(infcx, obligations);
227 fn type_op_normalize_ty<'tcx>(
229 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Ty<'tcx>>>>,
230 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, NoSolution> {
231 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
234 fn type_op_normalize_predicate<'tcx>(
236 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Predicate<'tcx>>>>,
237 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, Predicate<'tcx>>>, NoSolution> {
238 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
241 fn type_op_normalize_fn_sig<'tcx>(
243 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<FnSig<'tcx>>>>,
244 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, FnSig<'tcx>>>, NoSolution> {
245 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
248 fn type_op_normalize_poly_fn_sig<'tcx>(
250 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<PolyFnSig<'tcx>>>>,
251 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, PolyFnSig<'tcx>>>, NoSolution> {
252 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, type_op_normalize)
255 fn type_op_subtype<'tcx>(
257 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
258 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
259 tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
260 let (param_env, Subtype { sub, sup }) = key.into_parts();
262 .at(&ObligationCause::dummy(), param_env)
264 .into_value_registering_obligations(infcx, fulfill_cx);
269 fn type_op_prove_predicate<'tcx>(
271 canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
272 ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
273 // HACK This bubble is required for this test to pass:
274 // impl-trait/issue-99642.rs
275 tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).enter_canonical_trait_query(
277 |infcx, fulfill_cx, key| {
278 type_op_prove_predicate_with_cause(infcx, fulfill_cx, key, ObligationCause::dummy());
284 /// The core of the `type_op_prove_predicate` query: for diagnostics purposes in NLL HRTB errors,
285 /// this query can be re-run to better track the span of the obligation cause, and improve the error
286 /// message. Do not call directly unless you're in that very specific context.
287 pub fn type_op_prove_predicate_with_cause<'a, 'tcx: 'a>(
288 infcx: &'a InferCtxt<'a, 'tcx>,
289 fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
290 key: ParamEnvAnd<'tcx, ProvePredicate<'tcx>>,
291 cause: ObligationCause<'tcx>,
293 let (param_env, ProvePredicate { predicate }) = key.into_parts();
294 fulfill_cx.register_predicate_obligation(infcx, Obligation::new(cause, param_env, predicate));