1
Fork 0

Pull out ConstValue relating into its own function

This commit is contained in:
Oli Scherer 2021-03-02 12:21:10 +00:00
parent 5e8a89b2e5
commit b729cc9d61

View file

@ -524,24 +524,55 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) => a_p.index == b_p.index, (ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) => a_p.index == b_p.index,
(ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2, (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => { (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
match (a_val, b_val) { check_const_value_eq(relation, a_val, b_val, a, b)?
}
( (
ConstValue::Scalar(Scalar::Int(a_val)), ty::ConstKind::Unevaluated(a_def, a_substs, None),
ConstValue::Scalar(Scalar::Int(b_val)), ty::ConstKind::Unevaluated(b_def, b_substs, None),
) => a_val == b_val, ) if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() => {
tcx.try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs)))
}
// While this is slightly incorrect, it shouldn't matter for `min_const_generics`
// and is the better alternative to waiting until `const_evaluatable_checked` can
// be stabilized.
( (
ConstValue::Scalar(Scalar::Ptr(a_val)), ty::ConstKind::Unevaluated(a_def, a_substs, a_promoted),
ConstValue::Scalar(Scalar::Ptr(b_val)), ty::ConstKind::Unevaluated(b_def, b_substs, b_promoted),
) => { ) if a_def == b_def && a_promoted == b_promoted => {
let substs =
relation.relate_with_variance(ty::Variance::Invariant, a_substs, b_substs)?;
return Ok(tcx.mk_const(ty::Const {
val: ty::ConstKind::Unevaluated(a_def, substs, a_promoted),
ty: a.ty,
}));
}
_ => false,
};
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(expected_found(relation, a, b))) }
}
fn check_const_value_eq<R: TypeRelation<'tcx>>(
relation: &mut R,
a_val: ConstValue<'tcx>,
b_val: ConstValue<'tcx>,
// FIXME(oli-obk): these arguments should go away with valtrees
a: &'tcx ty::Const<'tcx>,
b: &'tcx ty::Const<'tcx>,
// FIXME(oli-obk): this should just be `bool` with valtrees
) -> RelateResult<'tcx, bool> {
let tcx = relation.tcx();
Ok(match (a_val, b_val) {
(ConstValue::Scalar(Scalar::Int(a_val)), ConstValue::Scalar(Scalar::Int(b_val))) => {
a_val == b_val a_val == b_val
|| match ( }
tcx.global_alloc(a_val.alloc_id), (ConstValue::Scalar(Scalar::Ptr(a_val)), ConstValue::Scalar(Scalar::Ptr(b_val))) => {
tcx.global_alloc(b_val.alloc_id), a_val == b_val
) { || match (tcx.global_alloc(a_val.alloc_id), tcx.global_alloc(b_val.alloc_id)) {
( (GlobalAlloc::Function(a_instance), GlobalAlloc::Function(b_instance)) => {
GlobalAlloc::Function(a_instance), a_instance == b_instance
GlobalAlloc::Function(b_instance), }
) => a_instance == b_instance,
_ => false, _ => false,
} }
} }
@ -569,33 +600,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
} }
_ => false, _ => false,
} })
}
(
ty::ConstKind::Unevaluated(a_def, a_substs, None),
ty::ConstKind::Unevaluated(b_def, b_substs, None),
) if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() => {
tcx.try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs)))
}
// While this is slightly incorrect, it shouldn't matter for `min_const_generics`
// and is the better alternative to waiting until `const_evaluatable_checked` can
// be stabilized.
(
ty::ConstKind::Unevaluated(a_def, a_substs, a_promoted),
ty::ConstKind::Unevaluated(b_def, b_substs, b_promoted),
) if a_def == b_def && a_promoted == b_promoted => {
let substs =
relation.relate_with_variance(ty::Variance::Invariant, a_substs, b_substs)?;
return Ok(tcx.mk_const(ty::Const {
val: ty::ConstKind::Unevaluated(a_def, substs, a_promoted),
ty: a.ty,
}));
}
_ => false,
};
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(expected_found(relation, a, b))) }
} }
impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> { impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {