Skip to content

Commit 917260e

Browse files
committed
Auto merge of rust-lang#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.
2 parents 7c46c6c + 697491c commit 917260e

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

src/libcore/num/f32.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
//! Operations and constants for 32-bits floats (`f32` type)
1212
13-
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
14-
#![allow(overflowing_literals)]
13+
#![cfg_attr(stage0, allow(overflowing_literals))]
1514

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

src/libcore/num/f64.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
//! Operations and constants for 64-bits floats (`f64` type)
1212
13-
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
14-
#![allow(overflowing_literals)]
15-
1613
#![stable(feature = "rust1", since = "1.0.0")]
1714

1815
use intrinsics;

src/librustc_lint/types.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
173173
}
174174
}
175175
ty::TyFloat(t) => {
176-
let (min, max) = float_ty_range(t);
177-
let lit_val: f64 = match lit.node {
176+
let is_infinite = match lit.node {
178177
ast::LitKind::Float(v, _) |
179178
ast::LitKind::FloatUnsuffixed(v) => {
180-
match v.as_str().parse() {
181-
Ok(f) => f,
182-
Err(_) => return,
179+
match t {
180+
ast::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
181+
ast::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
183182
}
184183
}
185184
_ => bug!(),
186185
};
187-
if lit_val < min || lit_val > max {
186+
if is_infinite == Ok(true) {
188187
cx.span_lint(OVERFLOWING_LITERALS,
189188
e.span,
190189
&format!("literal out of range for {:?}", t));
@@ -242,13 +241,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
242241
}
243242
}
244243

245-
fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
246-
match float_ty {
247-
ast::FloatTy::F32 => (f32::MIN as f64, f32::MAX as f64),
248-
ast::FloatTy::F64 => (f64::MIN, f64::MAX),
249-
}
250-
}
251-
252244
fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
253245
match int_ty {
254246
ast::IntTy::Is => int_ty_bits(target_int_ty, target_int_ty),

src/test/compile-fail/lint-type-overflow2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fn main() {
1717
let x2: i8 = --128; //~ error: literal out of range for i8
1818
//~^ error: attempt to negate with overflow
1919

20-
let x = -3.40282348e+38_f32; //~ error: literal out of range for f32
21-
let x = 3.40282348e+38_f32; //~ error: literal out of range for f32
20+
let x = -3.40282357e+38_f32; //~ error: literal out of range for f32
21+
let x = 3.40282357e+38_f32; //~ error: literal out of range for f32
2222
let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for f64
2323
let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for f64
2424
}

src/test/run-pass/big-literals.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
12-
#![feature(core)]
13-
1411
// Catch mistakes in the overflowing literals lint.
1512
#![deny(overflowing_literals)]
1613

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

2320
assert_eq!((-2147483648i32).wrapping_sub(1), 2147483647);
21+
22+
assert_eq!(-3.40282356e+38_f32, ::std::f32::MIN);
23+
assert_eq!(3.40282356e+38_f32, ::std::f32::MAX);
24+
assert_eq!(-1.7976931348623158e+308_f64, ::std::f64::MIN);
25+
assert_eq!(1.7976931348623158e+308_f64, ::std::f64::MAX);
2426
}

0 commit comments

Comments
 (0)