From 3da257a98d44efb5f1a7df2e4237935bcf0f2b92 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 6 Aug 2024 12:50:18 -0400 Subject: [PATCH] Use SolverRelating in new solver --- compiler/rustc_infer/src/infer/at.rs | 43 ------------- compiler/rustc_infer/src/infer/context.rs | 24 +------ compiler/rustc_infer/src/infer/mod.rs | 6 -- compiler/rustc_infer/src/infer/relate/mod.rs | 1 - .../src/solve/eval_ctxt/canonical.rs | 1 + .../src/solve/eval_ctxt/mod.rs | 1 + .../src/error_reporting/infer/mod.rs | 6 -- compiler/rustc_type_ir/src/infer_ctxt.rs | 19 +----- .../src/relate/solver_relating.rs | 64 ++++++++++++++++++- 9 files changed, 68 insertions(+), 97 deletions(-) diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 5422761a6cf..ee9f548c1d4 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -208,49 +208,6 @@ impl<'a, 'tcx> At<'a, 'tcx> { } } - /// Used in the new solver since we don't care about tracking an `ObligationCause`. - pub fn relate_no_trace( - self, - expected: T, - variance: ty::Variance, - actual: T, - ) -> Result>>, NoSolution> - where - T: Relate>, - { - let mut op = TypeRelating::new( - self.infcx, - TypeTrace::dummy(self.cause), - self.param_env, - DefineOpaqueTypes::Yes, - StructurallyRelateAliases::No, - variance, - ); - op.relate(expected, actual)?; - Ok(op.into_obligations().into_iter().map(|o| o.into()).collect()) - } - - /// Used in the new solver since we don't care about tracking an `ObligationCause`. - pub fn eq_structurally_relating_aliases_no_trace( - self, - expected: T, - actual: T, - ) -> Result>>, NoSolution> - where - T: Relate>, - { - let mut op = TypeRelating::new( - self.infcx, - TypeTrace::dummy(self.cause), - self.param_env, - DefineOpaqueTypes::Yes, - StructurallyRelateAliases::Yes, - ty::Invariant, - ); - op.relate(expected, actual)?; - Ok(op.into_obligations().into_iter().map(|o| o.into()).collect()) - } - /// Computes the least-upper-bound, or mutual supertype, of two /// values. The order of the arguments doesn't matter, but since /// this can result in an error (e.g., if asked to compute LUB of diff --git a/compiler/rustc_infer/src/infer/context.rs b/compiler/rustc_infer/src/infer/context.rs index 497be6b5404..57007752cad 100644 --- a/compiler/rustc_infer/src/infer/context.rs +++ b/compiler/rustc_infer/src/infer/context.rs @@ -2,10 +2,10 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::infer::unify_key::EffectVarValue; use rustc_middle::traits::ObligationCause; -use rustc_middle::traits::solve::{Goal, NoSolution, SolverMode}; +use rustc_middle::traits::solve::SolverMode; use rustc_middle::ty::fold::TypeFoldable; +use rustc_middle::ty::relate::RelateResult; use rustc_middle::ty::relate::combine::PredicateEmittingRelation; -use rustc_middle::ty::relate::{Relate, RelateResult}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::{DUMMY_SP, ErrorGuaranteed}; @@ -210,26 +210,6 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> { self.set_tainted_by_errors(e) } - fn relate>>( - &self, - param_env: ty::ParamEnv<'tcx>, - lhs: T, - variance: ty::Variance, - rhs: T, - ) -> Result>>, NoSolution> { - self.at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs) - } - - fn eq_structurally_relating_aliases>>( - &self, - param_env: ty::ParamEnv<'tcx>, - lhs: T, - rhs: T, - ) -> Result>>, NoSolution> { - self.at(&ObligationCause::dummy(), param_env) - .eq_structurally_relating_aliases_no_trace(lhs, rhs) - } - fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> { self.shallow_resolve(ty) } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 21f9bf028d5..bc813305ba4 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -32,7 +32,6 @@ use rustc_middle::infer::unify_key::{ use rustc_middle::mir::ConstraintCategory; use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult}; use rustc_middle::traits::select; -use rustc_middle::traits::solve::{Goal, NoSolution}; pub use rustc_middle::ty::IntVarValue; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::fold::{ @@ -340,7 +339,6 @@ pub enum ValuePairs<'tcx> { PolySigs(ExpectedFound>), ExistentialTraitRef(ExpectedFound>), ExistentialProjection(ExpectedFound>), - Dummy, } impl<'tcx> ValuePairs<'tcx> { @@ -1638,10 +1636,6 @@ impl<'tcx> TypeTrace<'tcx> { values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())), } } - - fn dummy(cause: &ObligationCause<'tcx>) -> TypeTrace<'tcx> { - TypeTrace { cause: cause.clone(), values: ValuePairs::Dummy } - } } impl<'tcx> SubregionOrigin<'tcx> { diff --git a/compiler/rustc_infer/src/infer/relate/mod.rs b/compiler/rustc_infer/src/infer/relate/mod.rs index e156f72d78d..e6d1003cab6 100644 --- a/compiler/rustc_infer/src/infer/relate/mod.rs +++ b/compiler/rustc_infer/src/infer/relate/mod.rs @@ -6,7 +6,6 @@ pub use rustc_middle::ty::relate::RelateResult; pub use rustc_type_ir::relate::combine::PredicateEmittingRelation; pub use rustc_type_ir::relate::*; -#[allow(hidden_glob_reexports)] mod generalize; mod higher_ranked; pub(super) mod lattice; diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs index 252a9ed1a2e..3b6197c6759 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs @@ -14,6 +14,7 @@ use std::iter; use rustc_index::IndexVec; use rustc_type_ir::fold::TypeFoldable; use rustc_type_ir::inherent::*; +use rustc_type_ir::relate::solver_relating::RelateExt; use rustc_type_ir::{self as ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner}; use tracing::{instrument, trace}; diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs index ffa800348f2..970ef905dfd 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs @@ -7,6 +7,7 @@ use rustc_type_ir::data_structures::{HashMap, HashSet, ensure_sufficient_stack}; use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_type_ir::inherent::*; use rustc_type_ir::relate::Relate; +use rustc_type_ir::relate::solver_relating::RelateExt; use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; use rustc_type_ir::{self as ty, CanonicalVarValues, InferCtxtLike, Interner}; use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic}; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index c7b3f704330..bd78a6ee3af 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -1285,9 +1285,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ValuePairs::ExistentialProjection(_) => { (false, Mismatch::Fixed("existential projection")) } - ValuePairs::Dummy => { - bug!("do not expect to report a type error from a ValuePairs::Dummy") - } }; let Some(vals) = self.values_str(values) else { // Derived error. Cancel the emitter. @@ -1853,9 +1850,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found); Some((exp, fnd, None)) } - ValuePairs::Dummy => { - bug!("do not expect to report a type error from a ValuePairs::Dummy") - } } } diff --git a/compiler/rustc_type_ir/src/infer_ctxt.rs b/compiler/rustc_type_ir/src/infer_ctxt.rs index 4b44b5a495e..b9f5cde653e 100644 --- a/compiler/rustc_type_ir/src/infer_ctxt.rs +++ b/compiler/rustc_type_ir/src/infer_ctxt.rs @@ -1,7 +1,7 @@ use crate::fold::TypeFoldable; +use crate::relate::RelateResult; use crate::relate::combine::PredicateEmittingRelation; -use crate::relate::{Relate, RelateResult}; -use crate::solve::{Goal, NoSolution, SolverMode}; +use crate::solve::SolverMode; use crate::{self as ty, Interner}; pub trait InferCtxtLike: Sized { @@ -98,21 +98,6 @@ pub trait InferCtxtLike: Sized { fn set_tainted_by_errors(&self, e: ::ErrorGuaranteed); - fn relate>( - &self, - param_env: ::ParamEnv, - lhs: T, - variance: ty::Variance, - rhs: T, - ) -> Result::Predicate>>, NoSolution>; - - fn eq_structurally_relating_aliases>( - &self, - param_env: ::ParamEnv, - lhs: T, - rhs: T, - ) -> Result::Predicate>>, NoSolution>; - fn shallow_resolve( &self, ty: ::Ty, diff --git a/compiler/rustc_type_ir/src/relate/solver_relating.rs b/compiler/rustc_type_ir/src/relate/solver_relating.rs index 18a4d5189bb..a2521eda6dd 100644 --- a/compiler/rustc_type_ir/src/relate/solver_relating.rs +++ b/compiler/rustc_type_ir/src/relate/solver_relating.rs @@ -1,12 +1,57 @@ pub use rustc_type_ir::relate::*; -use rustc_type_ir::solve::Goal; +use rustc_type_ir::solve::{Goal, NoSolution}; use rustc_type_ir::{self as ty, InferCtxtLike, Interner}; use tracing::{debug, instrument}; use self::combine::{InferCtxtCombineExt, PredicateEmittingRelation}; use crate::data_structures::DelayedSet; -#[allow(unused)] +pub trait RelateExt: InferCtxtLike { + fn relate>( + &self, + param_env: ::ParamEnv, + lhs: T, + variance: ty::Variance, + rhs: T, + ) -> Result::Predicate>>, NoSolution>; + + fn eq_structurally_relating_aliases>( + &self, + param_env: ::ParamEnv, + lhs: T, + rhs: T, + ) -> Result::Predicate>>, NoSolution>; +} + +impl RelateExt for Infcx { + fn relate>( + &self, + param_env: ::ParamEnv, + lhs: T, + variance: ty::Variance, + rhs: T, + ) -> Result::Predicate>>, NoSolution> + { + let mut relate = + SolverRelating::new(self, StructurallyRelateAliases::No, variance, param_env); + relate.relate(lhs, rhs)?; + Ok(relate.goals) + } + + fn eq_structurally_relating_aliases>( + &self, + param_env: ::ParamEnv, + lhs: T, + rhs: T, + ) -> Result::Predicate>>, NoSolution> + { + let mut relate = + SolverRelating::new(self, StructurallyRelateAliases::Yes, ty::Invariant, param_env); + relate.relate(lhs, rhs)?; + Ok(relate.goals) + } +} + /// Enforce that `a` is equal to or a subtype of `b`. pub struct SolverRelating<'infcx, Infcx, I: Interner> { infcx: &'infcx Infcx, @@ -46,6 +91,21 @@ where Infcx: InferCtxtLike, I: Interner, { + fn new( + infcx: &'infcx Infcx, + structurally_relate_aliases: StructurallyRelateAliases, + ambient_variance: ty::Variance, + param_env: I::ParamEnv, + ) -> Self { + SolverRelating { + infcx, + structurally_relate_aliases, + ambient_variance, + param_env, + goals: vec![], + cache: Default::default(), + } + } } impl TypeRelation for SolverRelating<'_, Infcx, I>