about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/util/mod.rs
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2023-06-04 11:09:20 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2023-06-19 01:47:03 -0700
commitc780e55995dd29ac3e6577af8e8e1daa28998a63 (patch)
tree1031c1a117d34b4b11c9e869f78544299c8684ab /compiler/rustc_const_eval/src/util/mod.rs
parent3fd8501823a36b057cc539551655b8adbc4acf76 (diff)
downloadrust-c780e55995dd29ac3e6577af8e8e1daa28998a63.tar.gz
rust-c780e55995dd29ac3e6577af8e8e1daa28998a63.zip
Dedup some type checks in the MIR validator
Diffstat (limited to 'compiler/rustc_const_eval/src/util/mod.rs')
-rw-r--r--compiler/rustc_const_eval/src/util/mod.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/compiler/rustc_const_eval/src/util/mod.rs b/compiler/rustc_const_eval/src/util/mod.rs
index 7641f560714..289e3422595 100644
--- a/compiler/rustc_const_eval/src/util/mod.rs
+++ b/compiler/rustc_const_eval/src/util/mod.rs
@@ -1,3 +1,5 @@
+use rustc_middle::mir;
+
 mod alignment;
 mod check_validity_requirement;
 mod compare_types;
@@ -7,3 +9,27 @@ pub use self::alignment::is_disaligned;
 pub use self::check_validity_requirement::check_validity_requirement;
 pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};
 pub use self::type_name::type_name;
+
+/// Classify whether an operator is "left-homogeneous", i.e., the LHS has the
+/// same type as the result.
+#[inline]
+pub(crate) fn binop_left_homogeneous(op: mir::BinOp) -> bool {
+    use rustc_middle::mir::BinOp::*;
+    match op {
+        Add | AddUnchecked | Sub | SubUnchecked | Mul | MulUnchecked | Div | Rem | BitXor
+        | BitAnd | BitOr | Offset | Shl | ShlUnchecked | Shr | ShrUnchecked => true,
+        Eq | Ne | Lt | Le | Gt | Ge => false,
+    }
+}
+
+/// Classify whether an operator is "right-homogeneous", i.e., the RHS has the
+/// same type as the LHS.
+#[inline]
+pub(crate) fn binop_right_homogeneous(op: mir::BinOp) -> bool {
+    use rustc_middle::mir::BinOp::*;
+    match op {
+        Add | AddUnchecked | Sub | SubUnchecked | Mul | MulUnchecked | Div | Rem | BitXor
+        | BitAnd | BitOr | Eq | Ne | Lt | Le | Gt | Ge => true,
+        Offset | Shl | ShlUnchecked | Shr | ShrUnchecked => false,
+    }
+}