about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github35764891676564198441@oli-obk.de>2021-03-02 12:21:10 +0000
committerOli Scherer <github35764891676564198441@oli-obk.de>2021-03-12 12:23:50 +0000
commitb729cc9d6129c11c1cd71a61f6eb745dd98531c6 (patch)
tree37c3f045abc96b5f8c77a342a1ba66e58a341cfd
parent5e8a89b2e5a036688d12eb6da4a1b97cadb6949f (diff)
downloadrust-b729cc9d6129c11c1cd71a61f6eb745dd98531c6.tar.gz
rust-b729cc9d6129c11c1cd71a61f6eb745dd98531c6.zip
Pull out ConstValue relating into its own function
-rw-r--r--compiler/rustc_middle/src/ty/relate.rs97
1 files changed, 51 insertions, 46 deletions
diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs
index 8814a334fbb..436ca4c0578 100644
--- a/compiler/rustc_middle/src/ty/relate.rs
+++ b/compiler/rustc_middle/src/ty/relate.rs
@@ -524,52 +524,7 @@ 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::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
         (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
-            match (a_val, b_val) {
-                (
-                    ConstValue::Scalar(Scalar::Int(a_val)),
-                    ConstValue::Scalar(Scalar::Int(b_val)),
-                ) => a_val == b_val,
-                (
-                    ConstValue::Scalar(Scalar::Ptr(a_val)),
-                    ConstValue::Scalar(Scalar::Ptr(b_val)),
-                ) => {
-                    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),
-                            ) => a_instance == b_instance,
-                            _ => false,
-                        }
-                }
-
-                (ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
-                    get_slice_bytes(&tcx, a_val) == get_slice_bytes(&tcx, b_val)
-                }
-
-                (ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
-                    let a_destructured = tcx.destructure_const(relation.param_env().and(a));
-                    let b_destructured = tcx.destructure_const(relation.param_env().and(b));
-
-                    // Both the variant and each field have to be equal.
-                    if a_destructured.variant == b_destructured.variant {
-                        for (a_field, b_field) in
-                            a_destructured.fields.iter().zip(b_destructured.fields.iter())
-                        {
-                            relation.consts(a_field, b_field)?;
-                        }
-
-                        true
-                    } else {
-                        false
-                    }
-                }
-
-                _ => false,
-            }
+            check_const_value_eq(relation, a_val, b_val, a, b)?
         }
 
         (
@@ -598,6 +553,56 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
     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
+        }
+        (ConstValue::Scalar(Scalar::Ptr(a_val)), ConstValue::Scalar(Scalar::Ptr(b_val))) => {
+            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)) => {
+                        a_instance == b_instance
+                    }
+                    _ => false,
+                }
+        }
+
+        (ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
+            get_slice_bytes(&tcx, a_val) == get_slice_bytes(&tcx, b_val)
+        }
+
+        (ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
+            let a_destructured = tcx.destructure_const(relation.param_env().and(a));
+            let b_destructured = tcx.destructure_const(relation.param_env().and(b));
+
+            // Both the variant and each field have to be equal.
+            if a_destructured.variant == b_destructured.variant {
+                for (a_field, b_field) in
+                    a_destructured.fields.iter().zip(b_destructured.fields.iter())
+                {
+                    relation.consts(a_field, b_field)?;
+                }
+
+                true
+            } else {
+                false
+            }
+        }
+
+        _ => false,
+    })
+}
+
 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {
     fn relate<R: TypeRelation<'tcx>>(
         relation: &mut R,