1
Fork 0

Fallout from removing a_is_expected

This commit is contained in:
Michael Goulet 2024-02-28 17:41:18 +00:00
parent 04e22627f5
commit b1536568db
12 changed files with 46 additions and 127 deletions

View file

@ -2654,10 +2654,6 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
"SameTypeModuloInfer"
}
fn a_is_expected(&self) -> bool {
true
}
fn relate_with_variance<T: relate::Relate<'tcx>>(
&mut self,
_variance: ty::Variance,

View file

@ -78,9 +78,7 @@ impl<'tcx> InferCtxt<'tcx> {
span,
});
obligations.extend(
self.handle_opaque_type(ty, ty_var, true, &cause, param_env)
.unwrap()
.obligations,
self.handle_opaque_type(ty, ty_var, &cause, param_env).unwrap().obligations,
);
ty_var
}
@ -94,14 +92,12 @@ impl<'tcx> InferCtxt<'tcx> {
&self,
a: Ty<'tcx>,
b: Ty<'tcx>,
a_is_expected: bool,
cause: &ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> InferResult<'tcx, ()> {
if a.references_error() || b.references_error() {
return Ok(InferOk { value: (), obligations: vec![] });
}
let (a, b) = if a_is_expected { (a, b) } else { (b, a) };
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) if def_id.is_local() => {
let def_id = def_id.expect_local();

View file

@ -144,10 +144,6 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
self.tcx
}
fn a_is_expected(&self) -> bool {
true
} // irrelevant
#[instrument(level = "trace", skip(self))]
fn relate_with_variance<T: Relate<'tcx>>(
&mut self,

View file

@ -17,12 +17,6 @@
//!
//! On success, the LUB/GLB operations return the appropriate bound. The
//! return value of `Equate` or `Sub` shouldn't really be used.
//!
//! ## Contravariance
//!
//! We explicitly track which argument is expected using
//! [TypeRelation::a_is_expected], so when dealing with contravariance
//! this should be correctly updated.
use super::glb::Glb;
use super::lub::Lub;
@ -57,7 +51,6 @@ impl<'tcx> InferCtxt<'tcx> {
where
R: ObligationEmittingRelation<'tcx>,
{
let a_is_expected = relation.a_is_expected();
debug_assert!(!a.has_escaping_bound_vars());
debug_assert!(!b.has_escaping_bound_vars());
@ -68,20 +61,20 @@ impl<'tcx> InferCtxt<'tcx> {
.borrow_mut()
.int_unification_table()
.unify_var_var(a_id, b_id)
.map_err(|e| int_unification_error(a_is_expected, e))?;
.map_err(|e| int_unification_error(true, e))?;
Ok(a)
}
(&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
self.unify_integral_variable(a_is_expected, v_id, IntType(v))
self.unify_integral_variable(true, v_id, IntType(v))
}
(&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
self.unify_integral_variable(!a_is_expected, v_id, IntType(v))
self.unify_integral_variable(false, v_id, IntType(v))
}
(&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
self.unify_integral_variable(a_is_expected, v_id, UintType(v))
self.unify_integral_variable(true, v_id, UintType(v))
}
(&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
self.unify_integral_variable(!a_is_expected, v_id, UintType(v))
self.unify_integral_variable(false, v_id, UintType(v))
}
// Relate floating-point variables to other types
@ -90,14 +83,14 @@ impl<'tcx> InferCtxt<'tcx> {
.borrow_mut()
.float_unification_table()
.unify_var_var(a_id, b_id)
.map_err(|e| float_unification_error(a_is_expected, e))?;
.map_err(|e| float_unification_error(true, e))?;
Ok(a)
}
(&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
self.unify_float_variable(a_is_expected, v_id, v)
self.unify_float_variable(true, v_id, v)
}
(&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
self.unify_float_variable(!a_is_expected, v_id, v)
self.unify_float_variable(false, v_id, v)
}
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
@ -130,7 +123,7 @@ impl<'tcx> InferCtxt<'tcx> {
// All other cases of inference are errors
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
Err(TypeError::Sorts(ty::relate::expected_found(relation, a, b)))
Err(TypeError::Sorts(ty::relate::expected_found(a, b)))
}
// During coherence, opaque types should be treated as *possibly*
@ -228,12 +221,12 @@ impl<'tcx> InferCtxt<'tcx> {
}
(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
self.instantiate_const_var(relation, relation.a_is_expected(), vid, b)?;
self.instantiate_const_var(relation, true, vid, b)?;
Ok(b)
}
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
self.instantiate_const_var(relation, !relation.a_is_expected(), vid, a)?;
self.instantiate_const_var(relation, false, vid, a)?;
Ok(a)
}
@ -250,8 +243,6 @@ impl<'tcx> InferCtxt<'tcx> {
{
match relation.structurally_relate_aliases() {
StructurallyRelateAliases::No => {
let (a, b) = if relation.a_is_expected() { (a, b) } else { (b, a) };
relation.register_predicates([if self.next_trait_solver() {
ty::PredicateKind::AliasRelate(
a.into(),

View file

@ -130,7 +130,7 @@ impl<'tcx> InferCtxt<'tcx> {
// instantiate_ty_var(?b, A) # expected and variance flipped
// A rel A'
// ```
if target_is_expected == relation.a_is_expected() {
if target_is_expected {
relation.relate(generalized_ty, source_ty)?;
} else {
debug!("flip relation");
@ -204,9 +204,9 @@ impl<'tcx> InferCtxt<'tcx> {
.const_unification_table()
.union_value(target_vid, ConstVariableValue::Known { value: generalized_ct });
// HACK: make sure that we `a_is_expected` continues to be
// correct when relating the generalized type with the source.
if target_is_expected == relation.a_is_expected() {
// Make sure that the order is correct when relating the
// generalized const and the source.
if target_is_expected {
relation.relate_with_variance(
ty::Variance::Invariant,
ty::VarianceDiagInfo::default(),
@ -398,10 +398,6 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
"Generalizer"
}
fn a_is_expected(&self) -> bool {
true
}
fn relate_item_args(
&mut self,
item_def_id: DefId,

View file

@ -30,10 +30,6 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
self.fields.tcx()
}
fn a_is_expected(&self) -> bool {
true
}
fn relate_with_variance<T: Relate<'tcx>>(
&mut self,
variance: ty::Variance,

View file

@ -116,9 +116,7 @@ where
&& !this.infcx().next_trait_solver() =>
{
this.register_obligations(
infcx
.handle_opaque_type(a, b, this.a_is_expected(), this.cause(), this.param_env())?
.obligations,
infcx.handle_opaque_type(a, b, this.cause(), this.param_env())?.obligations,
);
Ok(a)
}

View file

@ -30,10 +30,6 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
self.fields.tcx()
}
fn a_is_expected(&self) -> bool {
true
}
fn relate_with_variance<T: Relate<'tcx>>(
&mut self,
variance: ty::Variance,

View file

@ -35,10 +35,6 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
self.fields.infcx.tcx
}
fn a_is_expected(&self) -> bool {
true
}
fn relate_with_variance<T: Relate<'tcx>>(
&mut self,
variance: ty::Variance,
@ -139,7 +135,7 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
{
self.fields.obligations.extend(
infcx
.handle_opaque_type(a, b, true, &self.fields.trace.cause, self.param_env())?
.handle_opaque_type(a, b, &self.fields.trace.cause, self.param_env())?
.obligations,
);
}
@ -158,10 +154,6 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
b: ty::Region<'tcx>,
) -> RelateResult<'tcx, ty::Region<'tcx>> {
debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
// FIXME -- we have more fine-grained information available
// from the "cause" field, we could perhaps give more tailored
// error messages.
let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
match self.ambient_variance {
@ -184,7 +176,6 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
.make_subregion(origin, a, b);
}
ty::Invariant => {
// The order of `make_eqregion` apparently matters.
self.fields
.infcx
.inner