Skip to content

Commit

Permalink
Auto merge of #131343 - compiler-errors:remove-combine-fields, r=lcnr
Browse files Browse the repository at this point in the history
Remove `CombineFields`

This conflicts with #131263, but if this one lands first then perhaps #131263 could then go ahead and remove all the branching on solver in `TypeRelating`. We could perhaps then rename `TypeRelating` to `OldSolverRelating` or something, idk.

r? lcnr
  • Loading branch information
bors committed Oct 7, 2024
2 parents baaf3e6 + 0c5d2f9 commit 3ae715c
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 281 deletions.
26 changes: 7 additions & 19 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
let at = self.at(&self.cause, self.fcx.param_env);

let res = if self.use_lub {
at.lub(DefineOpaqueTypes::Yes, b, a)
at.lub(b, a)
} else {
at.sup(DefineOpaqueTypes::Yes, b, a)
.map(|InferOk { value: (), obligations }| InferOk { value: b, obligations })
Expand Down Expand Up @@ -1190,13 +1190,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(ty::FnDef(..), ty::FnDef(..)) => {
// Don't reify if the function types have a LUB, i.e., they
// are the same function and their parameters have a LUB.
match self.commit_if_ok(|_| {
self.at(cause, self.param_env).lub(
DefineOpaqueTypes::Yes,
prev_ty,
new_ty,
)
}) {
match self
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
{
// We have a LUB of prev_ty and new_ty, just return it.
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
Err(_) => {
Expand Down Expand Up @@ -1239,7 +1235,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
let sig = self
.at(cause, self.param_env)
.lub(DefineOpaqueTypes::Yes, a_sig, b_sig)
.lub(a_sig, b_sig)
.map(|ok| self.register_infer_ok_obligations(ok))?;

// Reify both sides and return the reified fn pointer type.
Expand Down Expand Up @@ -1330,9 +1326,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);

return Err(self
.commit_if_ok(|_| {
self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty)
})
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
.unwrap_err());
}
}
Expand All @@ -1344,13 +1338,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Err(e)
} else {
Err(self
.commit_if_ok(|_| {
self.at(cause, self.param_env).lub(
DefineOpaqueTypes::Yes,
prev_ty,
new_ty,
)
})
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
.unwrap_err())
}
}
Expand Down
120 changes: 34 additions & 86 deletions compiler/rustc_infer/src/infer/at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
//! sometimes useful when the types of `c` and `d` are not traceable
//! things. (That system should probably be refactored.)

use relate::lattice::{LatticeOp, LatticeOpKind};
use rustc_middle::bug;
use rustc_middle::ty::{Const, ImplSubject};

use super::*;
use crate::infer::relate::type_relating::TypeRelating;
use crate::infer::relate::{Relate, StructurallyRelateAliases, TypeRelation};
use crate::traits::Obligation;

/// Whether we should define opaque types or just treat them opaquely.
///
Expand Down Expand Up @@ -108,14 +109,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
where
T: ToTrace<'tcx>,
{
let mut fields = CombineFields::new(
let mut op = TypeRelating::new(
self.infcx,
ToTrace::to_trace(self.cause, expected, actual),
self.param_env,
define_opaque_types,
StructurallyRelateAliases::No,
ty::Contravariant,
);
fields.sup().relate(expected, actual)?;
Ok(InferOk { value: (), obligations: fields.into_obligations() })
op.relate(expected, actual)?;
Ok(InferOk { value: (), obligations: op.into_obligations() })
}

/// Makes `expected <: actual`.
Expand All @@ -128,14 +131,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
where
T: ToTrace<'tcx>,
{
let mut fields = CombineFields::new(
let mut op = TypeRelating::new(
self.infcx,
ToTrace::to_trace(self.cause, expected, actual),
self.param_env,
define_opaque_types,
StructurallyRelateAliases::No,
ty::Covariant,
);
fields.sub().relate(expected, actual)?;
Ok(InferOk { value: (), obligations: fields.into_obligations() })
op.relate(expected, actual)?;
Ok(InferOk { value: (), obligations: op.into_obligations() })
}

/// Makes `expected == actual`.
Expand Down Expand Up @@ -167,45 +172,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
where
T: Relate<TyCtxt<'tcx>>,
{
let mut fields = CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types);
fields.equate(StructurallyRelateAliases::No).relate(expected, actual)?;
Ok(InferOk {
value: (),
obligations: fields
.goals
.into_iter()
.map(|goal| {
Obligation::new(
self.infcx.tcx,
fields.trace.cause.clone(),
goal.param_env,
goal.predicate,
)
})
.collect(),
})
}

/// Equates `expected` and `found` while structurally relating aliases.
/// This should only be used inside of the next generation trait solver
/// when relating rigid aliases.
pub fn eq_structurally_relating_aliases<T>(
self,
expected: T,
actual: T,
) -> InferResult<'tcx, ()>
where
T: ToTrace<'tcx>,
{
assert!(self.infcx.next_trait_solver());
let mut fields = CombineFields::new(
let mut op = TypeRelating::new(
self.infcx,
ToTrace::to_trace(self.cause, expected, actual),
trace,
self.param_env,
DefineOpaqueTypes::Yes,
define_opaque_types,
StructurallyRelateAliases::No,
ty::Invariant,
);
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
Ok(InferOk { value: (), obligations: fields.into_obligations() })
op.relate(expected, actual)?;
Ok(InferOk { value: (), obligations: op.into_obligations() })
}

pub fn relate<T>(
Expand Down Expand Up @@ -242,19 +218,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
where
T: Relate<TyCtxt<'tcx>>,
{
let mut fields = CombineFields::new(
let mut op = TypeRelating::new(
self.infcx,
TypeTrace::dummy(self.cause),
self.param_env,
DefineOpaqueTypes::Yes,
);
fields.sub().relate_with_variance(
StructurallyRelateAliases::No,
variance,
ty::VarianceDiagInfo::default(),
expected,
actual,
)?;
Ok(fields.goals)
);
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`.
Expand All @@ -266,60 +239,35 @@ impl<'a, 'tcx> At<'a, 'tcx> {
where
T: Relate<TyCtxt<'tcx>>,
{
let mut fields = CombineFields::new(
let mut op = TypeRelating::new(
self.infcx,
TypeTrace::dummy(self.cause),
self.param_env,
DefineOpaqueTypes::Yes,
StructurallyRelateAliases::Yes,
ty::Invariant,
);
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
Ok(fields.goals)
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
/// u32 and i32), it is meaningful to call one of them the
/// "expected type".
pub fn lub<T>(
self,
define_opaque_types: DefineOpaqueTypes,
expected: T,
actual: T,
) -> InferResult<'tcx, T>
pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
where
T: ToTrace<'tcx>,
{
let mut fields = CombineFields::new(
let mut op = LatticeOp::new(
self.infcx,
ToTrace::to_trace(self.cause, expected, actual),
self.param_env,
define_opaque_types,
);
let value = fields.lub().relate(expected, actual)?;
Ok(InferOk { value, obligations: fields.into_obligations() })
}

/// Computes the greatest-lower-bound, or mutual subtype, of two
/// values. As with `lub` order doesn't matter, except for error
/// cases.
pub fn glb<T>(
self,
define_opaque_types: DefineOpaqueTypes,
expected: T,
actual: T,
) -> InferResult<'tcx, T>
where
T: ToTrace<'tcx>,
{
let mut fields = CombineFields::new(
self.infcx,
ToTrace::to_trace(self.cause, expected, actual),
self.param_env,
define_opaque_types,
LatticeOpKind::Lub,
);
let value = fields.glb().relate(expected, actual)?;
Ok(InferOk { value, obligations: fields.into_obligations() })
let value = op.relate(expected, actual)?;
Ok(InferOk { value, obligations: op.into_obligations() })
}
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use region_constraints::{
GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
};
pub use relate::StructurallyRelateAliases;
use relate::combine::CombineFields;
pub use relate::combine::PredicateEmittingRelation;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
Expand Down
96 changes: 3 additions & 93 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! There are four type combiners: [TypeRelating], `Lub`, and `Glb`,
//! There are four type combiners: `TypeRelating`, `Lub`, and `Glb`,
//! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL.
//!
//! Each implements the trait [TypeRelation] and contains methods for
Expand All @@ -20,56 +20,13 @@

use rustc_middle::bug;
use rustc_middle::infer::unify_key::EffectVarValue;
use rustc_middle::traits::solve::Goal;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, InferConst, IntType, Ty, TyCtxt, TypeVisitableExt, UintType, Upcast};
use rustc_middle::ty::{self, InferConst, IntType, Ty, TypeVisitableExt, UintType};
pub use rustc_next_trait_solver::relate::combine::*;
use tracing::debug;

use super::lattice::{LatticeOp, LatticeOpKind};
use super::type_relating::TypeRelating;
use super::{RelateResult, StructurallyRelateAliases};
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
use crate::traits::{Obligation, PredicateObligation};

#[derive(Clone)]
pub(crate) struct CombineFields<'infcx, 'tcx> {
pub infcx: &'infcx InferCtxt<'tcx>,
// Immutable fields
pub trace: TypeTrace<'tcx>,
pub param_env: ty::ParamEnv<'tcx>,
pub define_opaque_types: DefineOpaqueTypes,
// Mutable fields
//
// Adding any additional field likely requires
// changes to the cache of `TypeRelating`.
pub goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
}

impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
pub(crate) fn new(
infcx: &'infcx InferCtxt<'tcx>,
trace: TypeTrace<'tcx>,
param_env: ty::ParamEnv<'tcx>,
define_opaque_types: DefineOpaqueTypes,
) -> Self {
Self { infcx, trace, param_env, define_opaque_types, goals: vec![] }
}

pub(crate) fn into_obligations(self) -> Vec<PredicateObligation<'tcx>> {
self.goals
.into_iter()
.map(|goal| {
Obligation::new(
self.infcx.tcx,
self.trace.cause.clone(),
goal.param_env,
goal.predicate,
)
})
.collect()
}
}
use crate::infer::{InferCtxt, relate};

impl<'tcx> InferCtxt<'tcx> {
pub fn super_combine_tys<R>(
Expand Down Expand Up @@ -281,50 +238,3 @@ impl<'tcx> InferCtxt<'tcx> {
val
}
}

impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
pub(crate) fn tcx(&self) -> TyCtxt<'tcx> {
self.infcx.tcx
}

pub(crate) fn equate<'a>(
&'a mut self,
structurally_relate_aliases: StructurallyRelateAliases,
) -> TypeRelating<'a, 'infcx, 'tcx> {
TypeRelating::new(self, structurally_relate_aliases, ty::Invariant)
}

pub(crate) fn sub<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Covariant)
}

pub(crate) fn sup<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Contravariant)
}

pub(crate) fn lub<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
LatticeOp::new(self, LatticeOpKind::Lub)
}

pub(crate) fn glb<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
LatticeOp::new(self, LatticeOpKind::Glb)
}

pub(crate) fn register_obligations(
&mut self,
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
) {
self.goals.extend(obligations);
}

pub(crate) fn register_predicates(
&mut self,
obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
) {
self.goals.extend(
obligations
.into_iter()
.map(|to_pred| Goal::new(self.infcx.tcx, self.param_env, to_pred)),
)
}
}
Loading

0 comments on commit 3ae715c

Please sign in to comment.