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.
This commit is contained in:
commit
917260ebc2
5 changed files with 13 additions and 23 deletions
|
|
@ -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")]
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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,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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue