diff options
| author | bors <bors@rust-lang.org> | 2017-07-25 04:00:19 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-07-25 04:00:19 +0000 |
| commit | 917260ebc28acbe162bc2d91c0b3c28b9b2ebfc3 (patch) | |
| tree | 4673ad746cd66aae3877d163234c5f75bdd8ff77 | |
| parent | 7c46c6c59dbee8d6385f8924fe27cc5a7893841f (diff) | |
| parent | 697491c14dc841d6f34809ad6522f0ccff4905f4 (diff) | |
| download | rust-917260ebc28acbe162bc2d91c0b3c28b9b2ebfc3.tar.gz rust-917260ebc28acbe162bc2d91c0b3c28b9b2ebfc3.zip | |
Auto merge of #43325 - ollie27:overflowing_literals, r=arielb1
Fix overflowing_literals lint for large f32s Float literals need to be parsed as the correct type so they can be rounded correctly.
| -rw-r--r-- | src/libcore/num/f32.rs | 3 | ||||
| -rw-r--r-- | src/libcore/num/f64.rs | 3 | ||||
| -rw-r--r-- | src/librustc_lint/types.rs | 18 | ||||
| -rw-r--r-- | src/test/compile-fail/lint-type-overflow2.rs | 4 | ||||
| -rw-r--r-- | src/test/run-pass/big-literals.rs | 8 |
5 files changed, 13 insertions, 23 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index ebc30dc8b61..50683753684 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -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")] diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 1a1fe4d86e0..4ff80a2f05d 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -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; diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index ac3977bd216..1237132f615 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -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)); @@ -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), diff --git a/src/test/compile-fail/lint-type-overflow2.rs b/src/test/compile-fail/lint-type-overflow2.rs index a2971f23a79..d399fda3286 100644 --- a/src/test/compile-fail/lint-type-overflow2.rs +++ b/src/test/compile-fail/lint-type-overflow2.rs @@ -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 } diff --git a/src/test/run-pass/big-literals.rs b/src/test/run-pass/big-literals.rs index 19c0e7baaa0..56e0039f66d 100644 --- a/src/test/run-pass/big-literals.rs +++ b/src/test/run-pass/big-literals.rs @@ -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)] @@ -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); } |
