Skip to content

Commit

Permalink
Fix overflowing_literals lint for large f32s
Browse files Browse the repository at this point in the history
Float literals need to be parsed as the correct type so they can be
rounded correctly.
  • Loading branch information
ollie27 committed Jul 19, 2017
1 parent 83c659e commit 697491c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/libcore/num/f32.rs
Expand Up @@ -10,8 +10,7 @@

//! Operations and constants for 32-bits floats (`f32` type)

// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
#![allow(overflowing_literals)]
#![cfg_attr(stage0, allow(overflowing_literals))]

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
3 changes: 0 additions & 3 deletions src/libcore/num/f64.rs
Expand Up @@ -10,9 +10,6 @@

//! Operations and constants for 64-bits floats (`f64` type)

// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
#![allow(overflowing_literals)]

#![stable(feature = "rust1", since = "1.0.0")]

use intrinsics;
Expand Down
18 changes: 5 additions & 13 deletions src/librustc_lint/types.rs
Expand Up @@ -173,18 +173,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
}
}
ty::TyFloat(t) => {
let (min, max) = float_ty_range(t);
let lit_val: f64 = match lit.node {
let is_infinite = match lit.node {
ast::LitKind::Float(v, _) |
ast::LitKind::FloatUnsuffixed(v) => {
match v.as_str().parse() {
Ok(f) => f,
Err(_) => return,
match t {
ast::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
ast::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
}
}
_ => bug!(),
};
if lit_val < min || lit_val > max {
if is_infinite == Ok(true) {
cx.span_lint(OVERFLOWING_LITERALS,
e.span,
&format!("literal out of range for {:?}", t));
Expand Down Expand Up @@ -242,13 +241,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
}
}

fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
match float_ty {
ast::FloatTy::F32 => (f32::MIN as f64, f32::MAX as f64),
ast::FloatTy::F64 => (f64::MIN, f64::MAX),
}
}

fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
match int_ty {
ast::IntTy::Is => int_ty_bits(target_int_ty, target_int_ty),
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/lint-type-overflow2.rs
Expand Up @@ -17,8 +17,8 @@ fn main() {
let x2: i8 = --128; //~ error: literal out of range for i8
//~^ error: attempt to negate with overflow

let x = -3.40282348e+38_f32; //~ error: literal out of range for f32
let x = 3.40282348e+38_f32; //~ error: literal out of range for f32
let x = -3.40282357e+38_f32; //~ error: literal out of range for f32
let x = 3.40282357e+38_f32; //~ error: literal out of range for f32
let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for f64
let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for f64
}
8 changes: 5 additions & 3 deletions src/test/run-pass/big-literals.rs
Expand Up @@ -8,9 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.


#![feature(core)]

// Catch mistakes in the overflowing literals lint.
#![deny(overflowing_literals)]

Expand All @@ -21,4 +18,9 @@ pub fn main() {
assert_eq!(18446744073709551615, (!0 as u64));

assert_eq!((-2147483648i32).wrapping_sub(1), 2147483647);

assert_eq!(-3.40282356e+38_f32, ::std::f32::MIN);
assert_eq!(3.40282356e+38_f32, ::std::f32::MAX);
assert_eq!(-1.7976931348623158e+308_f64, ::std::f64::MIN);
assert_eq!(1.7976931348623158e+308_f64, ::std::f64::MAX);
}

0 comments on commit 697491c

Please sign in to comment.