From 7d982fdcf439799efbcc9f0cfcda99fa87b07460 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 24 Apr 2018 14:42:30 +0200 Subject: Implement `PartialCmp` for `ConstFloat` --- src/librustc_const_math/err.rs | 2 -- src/librustc_const_math/float.rs | 14 ++++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src/librustc_const_math') 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 for ConstFloat { + fn partial_cmp(&self, other: &Self) -> Option { + 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 { + fn try_cmp(self, rhs: Self) -> Option { 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, } } -- cgit 1.4.1-3-g733a5