Implement PartialCmp
for ConstFloat
This commit is contained in:
parent
7def638e42
commit
7d982fdcf4
@ -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,
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
|
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user