From aaa1db63ce2b92240272a1db303fa292d58373ee Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Nov 2022 15:07:19 -0300 Subject: [PATCH 1/2] Remove AscribeUserTypeCx --- compiler/rustc_traits/src/type_op.rs | 159 +++++++++------------------ 1 file changed, 52 insertions(+), 107 deletions(-) diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index 9eceae8b44f8e..fffa666cd5834 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -1,6 +1,4 @@ use rustc_hir as hir; -use rustc_hir::def_id::DefId; -use rustc_infer::infer::at::ToTrace; use rustc_infer::infer::canonical::{Canonical, QueryResponse}; use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt}; use rustc_infer::traits::ObligationCauseCode; @@ -57,122 +55,69 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}", mir_ty, def_id, user_substs ); - let cx = AscribeUserTypeCx { ocx, param_env, span: span.unwrap_or(DUMMY_SP) }; - cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?; - Ok(()) -} - -struct AscribeUserTypeCx<'me, 'tcx> { - ocx: &'me ObligationCtxt<'me, 'tcx>, - param_env: ty::ParamEnv<'tcx>, - span: Span, -} + let span = span.unwrap_or(DUMMY_SP); -impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> { - fn normalize(&self, value: T) -> T - where - T: TypeFoldable<'tcx>, - { - self.normalize_with_cause(value, ObligationCause::misc(self.span, hir::CRATE_HIR_ID)) - } + let UserSubsts { user_self_ty, substs } = user_substs; + let tcx = ocx.infcx.tcx; - fn normalize_with_cause(&self, value: T, cause: ObligationCause<'tcx>) -> T - where - T: TypeFoldable<'tcx>, - { - self.ocx.normalize(cause, self.param_env, value) - } + let ty = tcx.bound_type_of(def_id).subst(tcx, substs); + let ty = ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, ty); + debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); - fn eq(&self, a: T, b: T) -> Result<(), NoSolution> - where - T: ToTrace<'tcx>, - { - Ok(self.ocx.eq(&ObligationCause::dummy_with_span(self.span), self.param_env, a, b)?) - } + ocx.eq(&ObligationCause::dummy_with_span(span), param_env, mir_ty, ty)?; - fn prove_predicate(&self, predicate: Predicate<'tcx>, cause: ObligationCause<'tcx>) { - self.ocx.register_obligation(Obligation::new( - self.ocx.infcx.tcx, - cause, - self.param_env, - predicate, - )); - } + // Prove the predicates coming along with `def_id`. + // + // Also, normalize the `instantiated_predicates` + // because otherwise we wind up with duplicate "type + // outlives" error messages. + let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); - fn tcx(&self) -> TyCtxt<'tcx> { - self.ocx.infcx.tcx - } + let cause = ObligationCause::dummy_with_span(span); - #[instrument(level = "debug", skip(self))] - fn relate_mir_and_user_ty( - &self, - mir_ty: Ty<'tcx>, - def_id: DefId, - user_substs: UserSubsts<'tcx>, - ) -> Result<(), NoSolution> { - let UserSubsts { user_self_ty, substs } = user_substs; - let tcx = self.tcx(); - - let ty = tcx.bound_type_of(def_id).subst(tcx, substs); - let ty = self.normalize(ty); - debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); - - self.eq(mir_ty, ty)?; - - // Prove the predicates coming along with `def_id`. - // - // Also, normalize the `instantiated_predicates` - // because otherwise we wind up with duplicate "type - // outlives" error messages. - let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); - - let cause = ObligationCause::dummy_with_span(self.span); - - debug!(?instantiated_predicates); - for (instantiated_predicate, predicate_span) in - zip(instantiated_predicates.predicates, instantiated_predicates.spans) - { - let span = if self.span == DUMMY_SP { predicate_span } else { self.span }; - let cause = ObligationCause::new( - span, - hir::CRATE_HIR_ID, - ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), - ); - let instantiated_predicate = - self.normalize_with_cause(instantiated_predicate, cause.clone()); - self.prove_predicate(instantiated_predicate, cause); - } + debug!(?instantiated_predicates); + for (instantiated_predicate, predicate_span) in + zip(instantiated_predicates.predicates, instantiated_predicates.spans) + { + let span = if span == DUMMY_SP { predicate_span } else { span }; + let cause = ObligationCause::new( + span, + hir::CRATE_HIR_ID, + ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), + ); + let instantiated_predicate = + ocx.normalize(cause.clone(), param_env, instantiated_predicate); - if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { - let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); - let impl_self_ty = self.normalize(impl_self_ty); + ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate)); + } - self.eq(self_ty, impl_self_ty)?; + if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { + let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); + let impl_self_ty = + ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, impl_self_ty); - self.prove_predicate( - ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())) - .to_predicate(tcx), - cause.clone(), - ); - } + ocx.eq(&ObligationCause::dummy_with_span(span), param_env, self_ty, impl_self_ty)?; - // In addition to proving the predicates, we have to - // prove that `ty` is well-formed -- this is because - // the WF of `ty` is predicated on the substs being - // well-formed, and we haven't proven *that*. We don't - // want to prove the WF of types from `substs` directly because they - // haven't been normalized. - // - // FIXME(nmatsakis): Well, perhaps we should normalize - // them? This would only be relevant if some input - // type were ill-formed but did not appear in `ty`, - // which...could happen with normalization... - self.prove_predicate( - ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx), - cause, - ); - Ok(()) + let predicate: Predicate<'tcx> = + ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())).to_predicate(tcx); + ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate)); } + + // In addition to proving the predicates, we have to + // prove that `ty` is well-formed -- this is because + // the WF of `ty` is predicated on the substs being + // well-formed, and we haven't proven *that*. We don't + // want to prove the WF of types from `substs` directly because they + // haven't been normalized. + // + // FIXME(nmatsakis): Well, perhaps we should normalize + // them? This would only be relevant if some input + // type were ill-formed but did not appear in `ty`, + // which...could happen with normalization... + let predicate: Predicate<'tcx> = + ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx); + ocx.register_obligation(Obligation::new(tcx, cause, param_env, predicate)); + Ok(()) } fn type_op_eq<'tcx>( From 3c9b30e65876090b2e1e4944dac24d7e9e182d1f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 25 Nov 2022 15:36:15 -0300 Subject: [PATCH 2/2] Define all clauses as dummy_with_span as the usages are all equivalent --- compiler/rustc_traits/src/type_op.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index fffa666cd5834..c6c072ea3d2bd 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -59,12 +59,13 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( let UserSubsts { user_self_ty, substs } = user_substs; let tcx = ocx.infcx.tcx; + let cause = ObligationCause::dummy_with_span(span); let ty = tcx.bound_type_of(def_id).subst(tcx, substs); - let ty = ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, ty); + let ty = ocx.normalize(cause.clone(), param_env, ty); debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); - ocx.eq(&ObligationCause::dummy_with_span(span), param_env, mir_ty, ty)?; + ocx.eq(&cause, param_env, mir_ty, ty)?; // Prove the predicates coming along with `def_id`. // @@ -73,8 +74,6 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( // outlives" error messages. let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); - let cause = ObligationCause::dummy_with_span(span); - debug!(?instantiated_predicates); for (instantiated_predicate, predicate_span) in zip(instantiated_predicates.predicates, instantiated_predicates.spans) @@ -93,10 +92,9 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); - let impl_self_ty = - ocx.normalize(ObligationCause::misc(span, hir::CRATE_HIR_ID), param_env, impl_self_ty); + let impl_self_ty = ocx.normalize(cause.clone(), param_env, impl_self_ty); - ocx.eq(&ObligationCause::dummy_with_span(span), param_env, self_ty, impl_self_ty)?; + ocx.eq(&cause, param_env, self_ty, impl_self_ty)?; let predicate: Predicate<'tcx> = ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())).to_predicate(tcx);