about summary refs log tree commit diff
path: root/src/librustc_const_math
diff options
context:
space:
mode:
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2018-04-24 14:42:30 +0200
committerOliver Schneider <git-no-reply-9879165716479413131@oli-obk.de>2018-04-30 18:18:32 +0200
commit7d982fdcf439799efbcc9f0cfcda99fa87b07460 (patch)
tree76f20bd4c608cf62102aab106122713678635f8e /src/librustc_const_math
parent7def638e42b70ee204cc84458b3d3775bbeba055 (diff)
Implement `PartialCmp` for `ConstFloat`
Diffstat (limited to 'src/librustc_const_math')
-rw-r--r--src/librustc_const_math/err.rs2
-rw-r--r--src/librustc_const_math/float.rs14
2 files changed, 10 insertions, 6 deletions
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,
         }
     }