Skip to content

Commit d37c75d

Browse files
Fix 07c42af to work on stable
1 parent 6dc2666 commit d37c75d

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/librustc_const_math/float.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ impl ConstFloat {
3838
}
3939

4040
/// Compares the values if they are of the same type
41-
pub fn try_cmp(self, rhs: Self) -> Result<Ordering, ConstMathErr> {
41+
pub fn try_cmp(self, rhs: Self) -> Result<Option<Ordering>, ConstMathErr> {
4242
match (self.ty, rhs.ty) {
4343
(ast::FloatTy::F64, ast::FloatTy::F64) => {
4444
let a = Double::from_bits(self.bits);
4545
let b = Double::from_bits(rhs.bits);
46-
// This is pretty bad but it is the existing behavior.
47-
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
46+
Ok(a.partial_cmp(&b))
4847
}
4948

5049
(ast::FloatTy::F32, ast::FloatTy::F32) => {
5150
let a = Single::from_bits(self.bits);
5251
let b = Single::from_bits(rhs.bits);
53-
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
52+
Ok(a.partial_cmp(&b))
5453
}
5554

5655
_ => Err(CmpBetweenUnequalTypes),

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,12 @@ pub fn compare_const_vals<'a, 'tcx>(
11151115
ty,
11161116
};
11171117
// FIXME(oli-obk): report cmp errors?
1118-
l.try_cmp(r).ok()
1118+
// FIXME: Just returning Ordering::Greater here is the previous behavior but may
1119+
// not be what we want. It means that NaN > NaN is true, which if false.
1120+
match l.try_cmp(r) {
1121+
Ok(opt) => Some(opt.unwrap_or(Ordering::Greater)),
1122+
_ => None,
1123+
}
11191124
},
11201125
ty::TyInt(_) => {
11211126
let a = interpret::sign_extend(tcx, a, ty).expect("layout error for TyInt");

src/librustc_mir/interpret/operator.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::cmp::Ordering;
2+
13
use rustc::mir;
24
use rustc::ty::{self, Ty};
35
use rustc_const_math::ConstFloat;
@@ -133,13 +135,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
133135
bits: r,
134136
ty,
135137
};
138+
let cmp = |l: ConstFloat, r: ConstFloat| -> Option<Ordering> {
139+
l.try_cmp(r).unwrap_or(None)
140+
};
136141
match op {
137-
Eq => PrimVal::from_bool(l == r),
138-
Ne => PrimVal::from_bool(l != r),
139-
Lt => PrimVal::from_bool(l < r),
140-
Le => PrimVal::from_bool(l <= r),
141-
Gt => PrimVal::from_bool(l > r),
142-
Ge => PrimVal::from_bool(l >= r),
142+
Eq => PrimVal::from_bool(cmp(l, r) == Some(Ordering::Equal)),
143+
Ne => PrimVal::from_bool(cmp(l, r) != Some(Ordering::Equal)),
144+
Lt => PrimVal::from_bool(cmp(l, r) == Some(Ordering::Less)),
145+
Gt => PrimVal::from_bool(cmp(l, r) == Some(Ordering::Greater)),
146+
Le => PrimVal::from_bool(match cmp(l, r) {
147+
Some(Ordering::Less) => true,
148+
Some(Ordering::Equal) => true,
149+
_ => false,
150+
}),
151+
Ge => PrimVal::from_bool(match cmp(l, r) {
152+
Some(Ordering::Greater) => true,
153+
Some(Ordering::Equal) => true,
154+
_ => false,
155+
}),
143156
Add => PrimVal::Bytes((l + r).unwrap().bits),
144157
Sub => PrimVal::Bytes((l - r).unwrap().bits),
145158
Mul => PrimVal::Bytes((l * r).unwrap().bits),

0 commit comments

Comments
 (0)