Skip to content

Commit 345886c

Browse files
committed
auto merge of #14519 : hirschenberger/rust/issue-10934, r=alexcrichton
Issue #10934
2 parents 7ab9bfa + f8bc571 commit 345886c

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/libcore/num/f32.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! Operations and constants for 32-bits floats (`f32` type)
1212
1313
#![doc(primitive = "f32")]
14+
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
15+
#![allow(type_overflow)]
1416

1517
use intrinsics;
1618
use mem;

src/libcore/num/f64.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! Operations and constants for 64-bits floats (`f64` type)
1212
1313
#![doc(primitive = "f64")]
14+
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
15+
#![allow(type_overflow)]
1416

1517
use intrinsics;
1618
use mem;

src/librustc/lint/builtin.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ use lint::{Context, LintPass, LintArray};
3737

3838
use std::cmp;
3939
use std::collections::HashMap;
40-
use std::i16;
41-
use std::i32;
42-
use std::i64;
43-
use std::i8;
44-
use std::u16;
45-
use std::u32;
46-
use std::u64;
47-
use std::u8;
40+
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
4841
use std::gc::Gc;
4942
use syntax::abi;
5043
use syntax::ast_map;
@@ -214,7 +207,21 @@ impl LintPass for TypeLimits {
214207
"literal out of range for its type");
215208
}
216209
},
217-
210+
ty::ty_float(t) => {
211+
let (min, max) = float_ty_range(t);
212+
let lit_val: f64 = match lit.node {
213+
ast::LitFloat(ref v, _) |
214+
ast::LitFloatUnsuffixed(ref v) => match from_str(v.get()) {
215+
Some(f) => f,
216+
None => return
217+
},
218+
_ => fail!()
219+
};
220+
if lit_val < min || lit_val > max {
221+
cx.span_lint(TYPE_OVERFLOW, e.span,
222+
"literal out of range for its type");
223+
}
224+
},
218225
_ => ()
219226
};
220227
},
@@ -265,6 +272,13 @@ impl LintPass for TypeLimits {
265272
}
266273
}
267274

275+
fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
276+
match float_ty {
277+
ast::TyF32 => (f32::MIN_VALUE as f64, f32::MAX_VALUE as f64),
278+
ast::TyF64 => (f64::MIN_VALUE, f64::MAX_VALUE)
279+
}
280+
}
281+
268282
fn check_limits(tcx: &ty::ctxt, binop: ast::BinOp,
269283
l: &ast::Expr, r: &ast::Expr) -> bool {
270284
let (lit, expr, swap) = match (&l.node, &r.node) {

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

+5
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ fn main() {
4747
let x = -2147483648_i32; // should be OK
4848
let x: i32 = -2147483649; //~ error: literal out of range for its type
4949
let x = -2147483649_i32; //~ error: literal out of range for its type
50+
51+
let x = -3.40282348e+38_f32; //~ error: literal out of range for its type
52+
let x = 3.40282348e+38_f32; //~ error: literal out of range for its type
53+
let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for its type
54+
let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for its type
5055
}

0 commit comments

Comments
 (0)