From 7d982fdcf439799efbcc9f0cfcda99fa87b07460 Mon Sep 17 00:00:00 2001 From: Oliver Schneider 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 afa28ae319cba..387a6d5f35c44 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 dee8813e86f28..5d442ee7b9721 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 9d820ea8cbed2..61e9b34f06a2c 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, } } diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index c2da8c11d87e9..776b24a8648e7 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 dfc0c4a824a84..0c748f818ccc8 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),