Skip to content

Commit 7d982fd

Browse files
committed
Implement PartialCmp for ConstFloat
1 parent 7def638 commit 7d982fd

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/librustc/ich/impls_const_math.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ impl_stable_hash_for!(struct ::rustc_const_math::ConstFloat {
1717
});
1818

1919
impl_stable_hash_for!(enum ::rustc_const_math::ConstMathErr {
20-
CmpBetweenUnequalTypes,
2120
UnequalTypes(op),
2221
Overflow(op),
2322
DivisionByZero,

src/librustc_const_math/err.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#[derive(Debug, PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)]
1212
pub enum ConstMathErr {
13-
CmpBetweenUnequalTypes,
1413
UnequalTypes(Op),
1514
Overflow(Op),
1615
DivisionByZero,
@@ -37,7 +36,6 @@ impl ConstMathErr {
3736
pub fn description(&self) -> &'static str {
3837
use self::Op::*;
3938
match *self {
40-
CmpBetweenUnequalTypes => "compared two values of different types",
4139
UnequalTypes(Add) => "tried to add two values of different types",
4240
UnequalTypes(Sub) => "tried to subtract two values of different types",
4341
UnequalTypes(Mul) => "tried to multiply two values of different types",

src/librustc_const_math/float.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,35 @@ pub struct ConstFloat {
3131
pub bits: u128,
3232
}
3333

34+
impl PartialOrd<ConstFloat> for ConstFloat {
35+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
36+
self.try_cmp(*other)
37+
}
38+
}
39+
3440
impl ConstFloat {
3541
/// Description of the type, not the value
3642
pub fn description(&self) -> &'static str {
3743
self.ty.ty_to_string()
3844
}
3945

4046
/// Compares the values if they are of the same type
41-
pub fn try_cmp(self, rhs: Self) -> Result<Ordering, ConstMathErr> {
47+
fn try_cmp(self, rhs: Self) -> Option<Ordering> {
4248
match (self.ty, rhs.ty) {
4349
(ast::FloatTy::F64, ast::FloatTy::F64) => {
4450
let a = Double::from_bits(self.bits);
4551
let b = Double::from_bits(rhs.bits);
4652
// This is pretty bad but it is the existing behavior.
47-
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
53+
Some(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
4854
}
4955

5056
(ast::FloatTy::F32, ast::FloatTy::F32) => {
5157
let a = Single::from_bits(self.bits);
5258
let b = Single::from_bits(rhs.bits);
53-
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
59+
Some(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
5460
}
5561

56-
_ => Err(CmpBetweenUnequalTypes),
62+
_ => None,
5763
}
5864
}
5965

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,7 @@ pub fn compare_const_vals<'a, 'tcx>(
10691069
bits: b,
10701070
ty,
10711071
};
1072-
// FIXME(oli-obk): report cmp errors?
1073-
l.try_cmp(r).ok()
1072+
l.partial_cmp(&r)
10741073
},
10751074
ty::TyInt(_) => {
10761075
let a = interpret::sign_extend(tcx, a, ty).expect("layout error for TyInt");

src/librustc_mir/interpret/operator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
135135
ty,
136136
};
137137
match op {
138-
Eq => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Equal),
139-
Ne => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Equal),
140-
Lt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Less),
141-
Le => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Greater),
142-
Gt => PrimVal::from_bool(l.try_cmp(r).unwrap() == Ordering::Greater),
143-
Ge => PrimVal::from_bool(l.try_cmp(r).unwrap() != Ordering::Less),
138+
Eq => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Equal),
139+
Ne => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Equal),
140+
Lt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Less),
141+
Le => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Greater),
142+
Gt => PrimVal::from_bool(l.partial_cmp(&r).unwrap() == Ordering::Greater),
143+
Ge => PrimVal::from_bool(l.partial_cmp(&r).unwrap() != Ordering::Less),
144144
Add => PrimVal::Bytes((l + r).unwrap().bits),
145145
Sub => PrimVal::Bytes((l - r).unwrap().bits),
146146
Mul => PrimVal::Bytes((l * r).unwrap().bits),

0 commit comments

Comments
 (0)