Skip to content

Rename InferCtxtLike to SolverDelegate, use a newtype to define an impl in rustc_next_trait_solver #126498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 6 additions & 141 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use BoundRegionConversionTime::*;
pub use RegionVariableOrigin::*;
pub use SubregionOrigin::*;

use crate::infer::relate::{Relate, RelateResult};
use crate::infer::relate::RelateResult;
use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine};
use error_reporting::TypeErrCtxt;
use free_regions::RegionRelations;
Expand Down Expand Up @@ -44,7 +44,7 @@ use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef};
use rustc_middle::{bug, span_bug};
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;
use snapshot::undo_log::InferCtxtUndoLogs;
use std::cell::{Cell, RefCell};
use std::fmt;
Expand Down Expand Up @@ -334,145 +334,6 @@ pub struct InferCtxt<'tcx> {
pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
}

impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
type Interner = TyCtxt<'tcx>;

fn interner(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn universe_of_ty(&self, vid: TyVid) -> Option<ty::UniverseIndex> {
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
// ty infers will give you the universe of the var it resolved to not the universe
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
// try to print out `?0.1` it will just print `?0`.
match self.probe_ty_var(vid) {
Err(universe) => Some(universe),
Ok(_) => None,
}
}

fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
match self.inner.borrow_mut().unwrap_region_constraints().probe_value(lt) {
Err(universe) => Some(universe),
Ok(_) => None,
}
}

fn universe_of_ct(&self, ct: ConstVid) -> Option<ty::UniverseIndex> {
// Same issue as with `universe_of_ty`
match self.probe_const_var(ct) {
Err(universe) => Some(universe),
Ok(_) => None,
}
}

fn root_ty_var(&self, var: TyVid) -> TyVid {
self.root_var(var)
}

fn root_const_var(&self, var: ConstVid) -> ConstVid {
self.root_const_var(var)
}

fn opportunistic_resolve_ty_var(&self, vid: TyVid) -> Ty<'tcx> {
match self.probe_ty_var(vid) {
Ok(ty) => ty,
Err(_) => Ty::new_var(self.tcx, self.root_var(vid)),
}
}

fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'tcx> {
self.opportunistic_resolve_int_var(vid)
}

fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'tcx> {
self.opportunistic_resolve_float_var(vid)
}

fn opportunistic_resolve_ct_var(&self, vid: ConstVid) -> ty::Const<'tcx> {
match self.probe_const_var(vid) {
Ok(ct) => ct,
Err(_) => ty::Const::new_var(self.tcx, self.root_const_var(vid)),
}
}

fn opportunistic_resolve_effect_var(&self, vid: EffectVid) -> ty::Const<'tcx> {
match self.probe_effect_var(vid) {
Some(ct) => ct,
None => {
ty::Const::new_infer(self.tcx, InferConst::EffectVar(self.root_effect_var(vid)))
}
}
}

fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> {
self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid)
}

fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
self.defining_opaque_types
}

fn next_ty_infer(&self) -> Ty<'tcx> {
self.next_ty_var(DUMMY_SP)
}

fn next_const_infer(&self) -> ty::Const<'tcx> {
self.next_const_var(DUMMY_SP)
}

fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
self.fresh_args_for_item(DUMMY_SP, def_id)
}

fn instantiate_binder_with_infer<T: TypeFoldable<Self::Interner> + Copy>(
&self,
value: ty::Binder<'tcx, T>,
) -> T {
self.instantiate_binder_with_fresh_vars(
DUMMY_SP,
BoundRegionConversionTime::HigherRankedType,
value,
)
}

fn enter_forall<T: TypeFoldable<TyCtxt<'tcx>> + Copy, U>(
&self,
value: ty::Binder<'tcx, T>,
f: impl FnOnce(T) -> U,
) -> U {
self.enter_forall(value, f)
}

fn relate<T: Relate<TyCtxt<'tcx>>>(
&self,
param_env: ty::ParamEnv<'tcx>,
lhs: T,
variance: ty::Variance,
rhs: T,
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
self.at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs)
}

fn eq_structurally_relating_aliases<T: Relate<TyCtxt<'tcx>>>(
&self,
param_env: ty::ParamEnv<'tcx>,
lhs: T,
rhs: T,
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
self.at(&ObligationCause::dummy(), param_env)
.eq_structurally_relating_aliases_no_trace(lhs, rhs)
}

fn resolve_vars_if_possible<T>(&self, value: T) -> T
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
self.resolve_vars_if_possible(value)
}
}

/// See the `error_reporting` module for more details.
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
pub enum ValuePairs<'tcx> {
Expand Down Expand Up @@ -826,6 +687,10 @@ impl<'tcx> InferCtxt<'tcx> {
self.tcx.dcx()
}

pub fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
self.defining_opaque_types
}

pub fn next_trait_solver(&self) -> bool {
self.next_trait_solver
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_next_trait_solver/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_type_ir::inherent::*;
use rustc_type_ir::visit::TypeVisitableExt;
use rustc_type_ir::{
self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, InferCtxtLike,
Interner,
self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, Interner,
};

use crate::delegate::SolverDelegate;

/// Whether we're canonicalizing a query input or the query response.
///
/// When canonicalizing an input we're in the context of the caller
Expand Down Expand Up @@ -37,7 +38,7 @@ pub enum CanonicalizeMode {
},
}

pub struct Canonicalizer<'a, Infcx: InferCtxtLike<Interner = I>, I: Interner> {
pub struct Canonicalizer<'a, Infcx: SolverDelegate<Interner = I>, I: Interner> {
infcx: &'a Infcx,
canonicalize_mode: CanonicalizeMode,

Expand All @@ -46,7 +47,7 @@ pub struct Canonicalizer<'a, Infcx: InferCtxtLike<Interner = I>, I: Interner> {
binder_index: ty::DebruijnIndex,
}

impl<'a, Infcx: InferCtxtLike<Interner = I>, I: Interner> Canonicalizer<'a, Infcx, I> {
impl<'a, Infcx: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, Infcx, I> {
pub fn canonicalize<T: TypeFoldable<I>>(
infcx: &'a Infcx,
canonicalize_mode: CanonicalizeMode,
Expand Down Expand Up @@ -210,7 +211,7 @@ impl<'a, Infcx: InferCtxtLike<Interner = I>, I: Interner> Canonicalizer<'a, Infc
}
}

impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
impl<Infcx: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I>
for Canonicalizer<'_, Infcx, I>
{
fn interner(&self) -> I {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::fold::TypeFoldable;
use crate::relate::Relate;
use crate::solve::{Goal, NoSolution};
use crate::{self as ty, Interner};
use rustc_type_ir::fold::TypeFoldable;
use rustc_type_ir::relate::Relate;
use rustc_type_ir::solve::{Goal, NoSolution};
use rustc_type_ir::{self as ty, Interner};

pub trait InferCtxtLike: Sized {
pub trait SolverDelegate: Sized {
type Interner: Interner;

fn interner(&self) -> Self::Interner;
Expand Down Expand Up @@ -72,4 +72,6 @@ pub trait InferCtxtLike: Sized {
fn resolve_vars_if_possible<T>(&self, value: T) -> T
where
T: TypeFoldable<Self::Interner>;

fn probe<T>(&self, probe: impl FnOnce() -> T) -> T;
}
1 change: 1 addition & 0 deletions compiler/rustc_next_trait_solver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod canonicalizer;
pub mod delegate;
pub mod resolve;
pub mod solve;
12 changes: 7 additions & 5 deletions compiler/rustc_next_trait_solver/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_type_ir::inherent::*;
use rustc_type_ir::visit::TypeVisitableExt;
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
use rustc_type_ir::{self as ty, Interner};

use crate::delegate::SolverDelegate;

///////////////////////////////////////////////////////////////////////////
// EAGER RESOLUTION

/// Resolves ty, region, and const vars to their inferred values or their root vars.
pub struct EagerResolver<'a, Infcx, I = <Infcx as InferCtxtLike>::Interner>
pub struct EagerResolver<'a, Infcx, I = <Infcx as SolverDelegate>::Interner>
where
Infcx: InferCtxtLike<Interner = I>,
Infcx: SolverDelegate<Interner = I>,
I: Interner,
{
infcx: &'a Infcx,
}

impl<'a, Infcx: InferCtxtLike> EagerResolver<'a, Infcx> {
impl<'a, Infcx: SolverDelegate> EagerResolver<'a, Infcx> {
pub fn new(infcx: &'a Infcx) -> Self {
EagerResolver { infcx }
}
}

impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for EagerResolver<'_, Infcx> {
impl<Infcx: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolver<'_, Infcx> {
fn interner(&self) -> I {
self.infcx.interner()
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/solve/alias_relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
//! relate them structurally.

use super::EvalCtxt;
use rustc_infer::infer::InferCtxt;
use crate::solve::infcx::RustcSolverDelegate;
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
use rustc_middle::ty;

impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
impl<'tcx> EvalCtxt<'_, RustcSolverDelegate<'tcx>> {
#[instrument(level = "trace", skip(self), ret)]
pub(super) fn compute_alias_relate_goal(
&mut self,
Expand Down
Loading
Loading