Skip to content

Commit

Permalink
Implement PartialCmp for ConstFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 30, 2018
1 parent 7def638 commit 7d982fd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/librustc/ich/impls_const_math.rs
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_const_math/err.rs
Expand Up @@ -10,7 +10,6 @@

#[derive(Debug, PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)]
pub enum ConstMathErr {
CmpBetweenUnequalTypes,
UnequalTypes(Op),
Overflow(Op),
DivisionByZero,
Expand All @@ -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",
Expand Down
14 changes: 10 additions & 4 deletions src/librustc_const_math/float.rs
Expand Up @@ -31,29 +31,35 @@ 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 {
self.ty.ty_to_string()
}

/// 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,
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc_mir/hair/pattern/mod.rs
Expand Up @@ -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");
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_mir/interpret/operator.rs
Expand Up @@ -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),
Expand Down

0 comments on commit 7d982fd

Please sign in to comment.