From 1930c77de179e68e5594d86e116726a67c620658 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 23 Nov 2022 17:06:56 -0300 Subject: [PATCH] Remove normalize_projection_type --- compiler/rustc_infer/src/traits/engine.rs | 8 ---- .../rustc_trait_selection/src/autoderef.rs | 15 ++++--- .../src/traits/chalk_fulfill.rs | 16 ++----- .../src/traits/fulfill.rs | 42 ++----------------- 4 files changed, 13 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_infer/src/traits/engine.rs b/compiler/rustc_infer/src/traits/engine.rs index 54224c9b5de2..d3519f4b37b8 100644 --- a/compiler/rustc_infer/src/traits/engine.rs +++ b/compiler/rustc_infer/src/traits/engine.rs @@ -8,14 +8,6 @@ use super::FulfillmentError; use super::{ObligationCause, PredicateObligation}; pub trait TraitEngine<'tcx>: 'tcx { - fn normalize_projection_type( - &mut self, - infcx: &InferCtxt<'tcx>, - param_env: ty::ParamEnv<'tcx>, - projection_ty: ty::ProjectionTy<'tcx>, - cause: ObligationCause<'tcx>, - ) -> Ty<'tcx>; - /// Requires that `ty` must implement the trait with `def_id` in /// the given environment. This trait must not have any type /// parameters (except for `Self`). diff --git a/compiler/rustc_trait_selection/src/autoderef.rs b/compiler/rustc_trait_selection/src/autoderef.rs index 9b39a9401149..1102f44a82ef 100644 --- a/compiler/rustc_trait_selection/src/autoderef.rs +++ b/compiler/rustc_trait_selection/src/autoderef.rs @@ -1,4 +1,5 @@ use crate::errors::AutoDerefReachedRecursionLimit; +use crate::infer::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt; use crate::traits::{self, TraitEngine, TraitEngineExt}; use rustc_hir as hir; @@ -137,16 +138,14 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> { return None; } - let mut fulfillcx = >::new_in_snapshot(tcx); - let normalized_ty = fulfillcx.normalize_projection_type( - &self.infcx, - self.param_env, - ty::ProjectionTy { - item_def_id: tcx.lang_items().deref_target()?, - substs: trait_ref.substs, - }, + let normalized_ty = self.infcx.partially_normalize_associated_types_in( cause, + self.param_env, + tcx.mk_projection(tcx.lang_items().deref_target()?, trait_ref.substs), ); + let mut fulfillcx = >::new_in_snapshot(tcx); + let normalized_ty = + normalized_ty.into_value_registering_obligations(self.infcx, &mut *fulfillcx); let errors = fulfillcx.select_where_possible(&self.infcx); if !errors.is_empty() { // This shouldn't happen, except for evaluate/fulfill mismatches, diff --git a/compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs b/compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs index 8f9d5eaac9d1..e88950523537 100644 --- a/compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs @@ -4,11 +4,11 @@ use crate::infer::canonical::OriginalQueryValues; use crate::infer::InferCtxt; use crate::traits::query::NoSolution; use crate::traits::{ - ChalkEnvironmentAndGoal, FulfillmentError, FulfillmentErrorCode, ObligationCause, - PredicateObligation, SelectionError, TraitEngine, + ChalkEnvironmentAndGoal, FulfillmentError, FulfillmentErrorCode, PredicateObligation, + SelectionError, TraitEngine, }; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; -use rustc_middle::ty::{self, Ty, TypeVisitable}; +use rustc_middle::ty::{self, TypeVisitable}; pub struct FulfillmentContext<'tcx> { obligations: FxIndexSet>, @@ -33,16 +33,6 @@ impl FulfillmentContext<'_> { } impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> { - fn normalize_projection_type( - &mut self, - infcx: &InferCtxt<'tcx>, - _param_env: ty::ParamEnv<'tcx>, - projection_ty: ty::ProjectionTy<'tcx>, - _cause: ObligationCause<'tcx>, - ) -> Ty<'tcx> { - infcx.tcx.mk_ty(ty::Projection(projection_ty)) - } - fn register_predicate_obligation( &mut self, infcx: &InferCtxt<'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index d238e7556aea..e7513255dc40 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -4,12 +4,12 @@ use rustc_data_structures::obligation_forest::ProcessResult; use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome}; use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor}; use rustc_infer::traits::ProjectionCacheKey; -use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation}; +use rustc_infer::traits::{SelectionError, TraitEngine, TraitObligation}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::abstract_const::NotConstEvaluatable; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::subst::SubstsRef; -use rustc_middle::ty::{self, Binder, Const, Ty, TypeVisitable}; +use rustc_middle::ty::{self, Binder, Const, TypeVisitable}; use std::marker::PhantomData; use super::const_evaluatable; @@ -20,9 +20,9 @@ use super::CodeAmbiguity; use super::CodeProjectionError; use super::CodeSelectionError; use super::EvaluationResult; +use super::PredicateObligation; use super::Unimplemented; use super::{FulfillmentError, FulfillmentErrorCode}; -use super::{ObligationCause, PredicateObligation}; use crate::traits::project::PolyProjectionObligation; use crate::traits::project::ProjectionCacheKeyExt as _; @@ -126,42 +126,6 @@ impl<'a, 'tcx> FulfillmentContext<'tcx> { } impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> { - /// "Normalize" a projection type `::X` by - /// creating a fresh type variable `$0` as well as a projection - /// predicate `::X == $0`. When the - /// inference engine runs, it will attempt to find an impl of - /// `SomeTrait` or a where-clause that lets us unify `$0` with - /// something concrete. If this fails, we'll unify `$0` with - /// `projection_ty` again. - #[instrument(level = "debug", skip(self, infcx, param_env, cause))] - fn normalize_projection_type( - &mut self, - infcx: &InferCtxt<'tcx>, - param_env: ty::ParamEnv<'tcx>, - projection_ty: ty::ProjectionTy<'tcx>, - cause: ObligationCause<'tcx>, - ) -> Ty<'tcx> { - debug_assert!(!projection_ty.has_escaping_bound_vars()); - - // FIXME(#20304) -- cache - - let mut selcx = SelectionContext::new(infcx); - let mut obligations = vec![]; - let normalized_ty = project::normalize_projection_type( - &mut selcx, - param_env, - projection_ty, - cause, - 0, - &mut obligations, - ); - self.register_predicate_obligations(infcx, obligations); - - debug!(?normalized_ty); - - normalized_ty.ty().unwrap() - } - fn register_predicate_obligation( &mut self, infcx: &InferCtxt<'tcx>,