Skip to content

Commit b729cc9

Browse files
committed
Pull out ConstValue relating into its own function
1 parent 5e8a89b commit b729cc9

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -524,52 +524,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
524524
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) => a_p.index == b_p.index,
525525
(ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
526526
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
527-
match (a_val, b_val) {
528-
(
529-
ConstValue::Scalar(Scalar::Int(a_val)),
530-
ConstValue::Scalar(Scalar::Int(b_val)),
531-
) => a_val == b_val,
532-
(
533-
ConstValue::Scalar(Scalar::Ptr(a_val)),
534-
ConstValue::Scalar(Scalar::Ptr(b_val)),
535-
) => {
536-
a_val == b_val
537-
|| match (
538-
tcx.global_alloc(a_val.alloc_id),
539-
tcx.global_alloc(b_val.alloc_id),
540-
) {
541-
(
542-
GlobalAlloc::Function(a_instance),
543-
GlobalAlloc::Function(b_instance),
544-
) => a_instance == b_instance,
545-
_ => false,
546-
}
547-
}
548-
549-
(ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
550-
get_slice_bytes(&tcx, a_val) == get_slice_bytes(&tcx, b_val)
551-
}
552-
553-
(ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
554-
let a_destructured = tcx.destructure_const(relation.param_env().and(a));
555-
let b_destructured = tcx.destructure_const(relation.param_env().and(b));
556-
557-
// Both the variant and each field have to be equal.
558-
if a_destructured.variant == b_destructured.variant {
559-
for (a_field, b_field) in
560-
a_destructured.fields.iter().zip(b_destructured.fields.iter())
561-
{
562-
relation.consts(a_field, b_field)?;
563-
}
564-
565-
true
566-
} else {
567-
false
568-
}
569-
}
570-
571-
_ => false,
572-
}
527+
check_const_value_eq(relation, a_val, b_val, a, b)?
573528
}
574529

575530
(
@@ -598,6 +553,56 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
598553
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(expected_found(relation, a, b))) }
599554
}
600555

556+
fn check_const_value_eq<R: TypeRelation<'tcx>>(
557+
relation: &mut R,
558+
a_val: ConstValue<'tcx>,
559+
b_val: ConstValue<'tcx>,
560+
// FIXME(oli-obk): these arguments should go away with valtrees
561+
a: &'tcx ty::Const<'tcx>,
562+
b: &'tcx ty::Const<'tcx>,
563+
// FIXME(oli-obk): this should just be `bool` with valtrees
564+
) -> RelateResult<'tcx, bool> {
565+
let tcx = relation.tcx();
566+
Ok(match (a_val, b_val) {
567+
(ConstValue::Scalar(Scalar::Int(a_val)), ConstValue::Scalar(Scalar::Int(b_val))) => {
568+
a_val == b_val
569+
}
570+
(ConstValue::Scalar(Scalar::Ptr(a_val)), ConstValue::Scalar(Scalar::Ptr(b_val))) => {
571+
a_val == b_val
572+
|| match (tcx.global_alloc(a_val.alloc_id), tcx.global_alloc(b_val.alloc_id)) {
573+
(GlobalAlloc::Function(a_instance), GlobalAlloc::Function(b_instance)) => {
574+
a_instance == b_instance
575+
}
576+
_ => false,
577+
}
578+
}
579+
580+
(ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
581+
get_slice_bytes(&tcx, a_val) == get_slice_bytes(&tcx, b_val)
582+
}
583+
584+
(ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
585+
let a_destructured = tcx.destructure_const(relation.param_env().and(a));
586+
let b_destructured = tcx.destructure_const(relation.param_env().and(b));
587+
588+
// Both the variant and each field have to be equal.
589+
if a_destructured.variant == b_destructured.variant {
590+
for (a_field, b_field) in
591+
a_destructured.fields.iter().zip(b_destructured.fields.iter())
592+
{
593+
relation.consts(a_field, b_field)?;
594+
}
595+
596+
true
597+
} else {
598+
false
599+
}
600+
}
601+
602+
_ => false,
603+
})
604+
}
605+
601606
impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {
602607
fn relate<R: TypeRelation<'tcx>>(
603608
relation: &mut R,

0 commit comments

Comments
 (0)