From 7d982fdcf439799efbcc9f0cfcda99fa87b07460 Mon Sep 17 00:00:00 2001
From: Oliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Date: Tue, 24 Apr 2018 14:42:30 +0200
Subject: [PATCH] Implement `PartialCmp` for `ConstFloat`

---
 src/librustc/ich/impls_const_math.rs   |  1 -
 src/librustc_const_math/err.rs         |  2 --
 src/librustc_const_math/float.rs       | 14 ++++++++++----
 src/librustc_mir/hair/pattern/mod.rs   |  3 +--
 src/librustc_mir/interpret/operator.rs | 12 ++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/librustc/ich/impls_const_math.rs b/src/librustc/ich/impls_const_math.rs
index afa28ae319c..387a6d5f35c 100644
--- a/src/librustc/ich/impls_const_math.rs
+++ b/src/librustc/ich/impls_const_math.rs
@@ -17,7 +17,6 @@ impl_stable_hash_for!(struct ::rustc_const_math::ConstFloat {
 });
 
 impl_stable_hash_for!(enum ::rustc_const_math::ConstMathErr {
-    CmpBetweenUnequalTypes,
     UnequalTypes(op),
     Overflow(op),
     DivisionByZero,
diff --git a/src/librustc_const_math/err.rs b/src/librustc_const_math/err.rs
index dee8813e86f..5d442ee7b97 100644
--- a/src/librustc_const_math/err.rs
+++ b/src/librustc_const_math/err.rs
@@ -10,7 +10,6 @@
 
 #[derive(Debug, PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)]
 pub enum ConstMathErr {
-    CmpBetweenUnequalTypes,
     UnequalTypes(Op),
     Overflow(Op),
     DivisionByZero,
@@ -37,7 +36,6 @@ impl ConstMathErr {
     pub fn description(&self) -> &'static str {
         use self::Op::*;
         match *self {
-            CmpBetweenUnequalTypes => "compared two values of different types",
             UnequalTypes(Add) => "tried to add two values of different types",
             UnequalTypes(Sub) => "tried to subtract two values of different types",
             UnequalTypes(Mul) => "tried to multiply two values of different types",
diff --git a/src/librustc_const_math/float.rs b/src/librustc_const_math/float.rs
index 9d820ea8cbe..61e9b34f06a 100644
--- a/src/librustc_const_math/float.rs
+++ b/src/librustc_const_math/float.rs
@@ -31,6 +31,12 @@ pub struct ConstFloat {
     pub bits: u128,
 }
 
+impl PartialOrd<ConstFloat> for ConstFloat {
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        self.try_cmp(*other)
+    }
+}
+
 impl ConstFloat {
     /// Description of the type, not the value
     pub fn description(&self) -> &'static str {
@@ -38,22 +44,22 @@ impl ConstFloat {
     }
 
     /// Compares the values if they are of the same type
-    pub fn try_cmp(self, rhs: Self) -> Result<Ordering, ConstMathErr> {
+    fn try_cmp(self, rhs: Self) -> Option<Ordering> {
         match (self.ty, rhs.ty) {
             (ast::FloatTy::F64, ast::FloatTy::F64)  => {
                 let a = Double::from_bits(self.bits);
                 let b = Double::from_bits(rhs.bits);
                 // This is pretty bad but it is the existing behavior.
-                Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
+                Some(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
             }
 
             (ast::FloatTy::F32, ast::FloatTy::F32) => {
                 let a = Single::from_bits(self.bits);
                 let b = Single::from_bits(rhs.bits);
-                Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
+                Some(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
             }
 
-            _ => Err(CmpBetweenUnequalTypes),
+            _ => None,
         }
     }
 
diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs
index c2da8c11d87..776b24a8648 100644
--- a/src/librustc_mir/hair/pattern/mod.rs
+++ b/src/librustc_mir/hair/pattern/mod.rs
@@ -1069,8 +1069,7 @@ pub fn compare_const_vals<'a, 'tcx>(
                         bits: b,
                         ty,
                     };
-                    // FIXME(oli-obk): report cmp errors?
-                    l.try_cmp(r).ok()
+                    l.partial_cmp(&r)
                 },
                 ty::TyInt(_) => {
                     let a = interpret::sign_extend(tcx, a, ty).expect("layout error for TyInt");
diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs
index dfc0c4a824a..0c748f818cc 100644
--- a/src/librustc_mir/interpret/operator.rs
+++ b/src/librustc_mir/interpret/operator.rs
@@ -135,12 +135,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
                 ty,
             };
             match op {
-                Eq => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Equal),
-                Ne => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Equal),
-                Lt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Less),
-                Le => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Greater),
-                Gt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Greater),
-                Ge => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Less),
+                Eq => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Equal),
+                Ne => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Equal),
+                Lt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Less),
+                Le => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Greater),
+                Gt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Greater),
+                Ge => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Less),
                 Add => PrimVal::Bytes((l + r).unwrap().bits),
                 Sub => PrimVal::Bytes((l - r).unwrap().bits),
                 Mul => PrimVal::Bytes((l * r).unwrap().bits),