Implement PartialCmp
for ConstFloat
This commit is contained in:
parent
7def638e42
commit
7d982fdcf4
5 changed files with 17 additions and 15 deletions
|
@ -17,7 +17,6 @@ impl_stable_hash_for!(struct ::rustc_const_math::ConstFloat {
|
||||||
});
|
});
|
||||||
|
|
||||||
impl_stable_hash_for!(enum ::rustc_const_math::ConstMathErr {
|
impl_stable_hash_for!(enum ::rustc_const_math::ConstMathErr {
|
||||||
CmpBetweenUnequalTypes,
|
|
||||||
UnequalTypes(op),
|
UnequalTypes(op),
|
||||||
Overflow(op),
|
Overflow(op),
|
||||||
DivisionByZero,
|
DivisionByZero,
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)]
|
#[derive(Debug, PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)]
|
||||||
pub enum ConstMathErr {
|
pub enum ConstMathErr {
|
||||||
CmpBetweenUnequalTypes,
|
|
||||||
UnequalTypes(Op),
|
UnequalTypes(Op),
|
||||||
Overflow(Op),
|
Overflow(Op),
|
||||||
DivisionByZero,
|
DivisionByZero,
|
||||||
|
@ -37,7 +36,6 @@ impl ConstMathErr {
|
||||||
pub fn description(&self) -> &'static str {
|
pub fn description(&self) -> &'static str {
|
||||||
use self::Op::*;
|
use self::Op::*;
|
||||||
match *self {
|
match *self {
|
||||||
CmpBetweenUnequalTypes => "compared two values of different types",
|
|
||||||
UnequalTypes(Add) => "tried to add 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(Sub) => "tried to subtract two values of different types",
|
||||||
UnequalTypes(Mul) => "tried to multiply 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,
|
pub bits: u128,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialOrd<ConstFloat> for ConstFloat {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
self.try_cmp(*other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ConstFloat {
|
impl ConstFloat {
|
||||||
/// Description of the type, not the value
|
/// Description of the type, not the value
|
||||||
pub fn description(&self) -> &'static str {
|
pub fn description(&self) -> &'static str {
|
||||||
|
@ -38,22 +44,22 @@ impl ConstFloat {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compares the values if they are of the same type
|
/// 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) {
|
match (self.ty, rhs.ty) {
|
||||||
(ast::FloatTy::F64, ast::FloatTy::F64) => {
|
(ast::FloatTy::F64, ast::FloatTy::F64) => {
|
||||||
let a = Double::from_bits(self.bits);
|
let a = Double::from_bits(self.bits);
|
||||||
let b = Double::from_bits(rhs.bits);
|
let b = Double::from_bits(rhs.bits);
|
||||||
// This is pretty bad but it is the existing behavior.
|
// 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) => {
|
(ast::FloatTy::F32, ast::FloatTy::F32) => {
|
||||||
let a = Single::from_bits(self.bits);
|
let a = Single::from_bits(self.bits);
|
||||||
let b = Single::from_bits(rhs.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,
|
bits: b,
|
||||||
ty,
|
ty,
|
||||||
};
|
};
|
||||||
// FIXME(oli-obk): report cmp errors?
|
l.partial_cmp(&r)
|
||||||
l.try_cmp(r).ok()
|
|
||||||
},
|
},
|
||||||
ty::TyInt(_) => {
|
ty::TyInt(_) => {
|
||||||
let a = interpret::sign_extend(tcx, a, ty).expect("layout error for 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,
|
ty,
|
||||||
};
|
};
|
||||||
match op {
|
match op {
|
||||||
Eq => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Equal),
|
Eq => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Equal),
|
||||||
Ne => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Equal),
|
Ne => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Equal),
|
||||||
Lt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Less),
|
Lt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Less),
|
||||||
Le => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Greater),
|
Le => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Greater),
|
||||||
Gt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Greater),
|
Gt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Greater),
|
||||||
Ge => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Less),
|
Ge => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Less),
|
||||||
Add => PrimVal::Bytes((l + r).unwrap().bits),
|
Add => PrimVal::Bytes((l + r).unwrap().bits),
|
||||||
Sub => PrimVal::Bytes((l - r).unwrap().bits),
|
Sub => PrimVal::Bytes((l - r).unwrap().bits),
|
||||||
Mul => PrimVal::Bytes((l * r).unwrap().bits),
|
Mul => PrimVal::Bytes((l * r).unwrap().bits),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue