auto merge of #14519 : hirschenberger/rust/issue-10934, r=alexcrichton

Issue #10934
This commit is contained in:
bors 2014-07-10 17:16:30 +00:00
commit 345886cfdd
4 changed files with 32 additions and 9 deletions

View File

@ -11,6 +11,8 @@
//! Operations and constants for 32-bits floats (`f32` type)
#![doc(primitive = "f32")]
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
#![allow(type_overflow)]
use intrinsics;
use mem;

View File

@ -11,6 +11,8 @@
//! Operations and constants for 64-bits floats (`f64` type)
#![doc(primitive = "f64")]
// FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
#![allow(type_overflow)]
use intrinsics;
use mem;

View File

@ -37,14 +37,7 @@ use lint::{Context, LintPass, LintArray};
use std::cmp;
use std::collections::HashMap;
use std::i16;
use std::i32;
use std::i64;
use std::i8;
use std::u16;
use std::u32;
use std::u64;
use std::u8;
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
use std::gc::Gc;
use syntax::abi;
use syntax::ast_map;
@ -214,7 +207,21 @@ impl LintPass for TypeLimits {
"literal out of range for its type");
}
},
ty::ty_float(t) => {
let (min, max) = float_ty_range(t);
let lit_val: f64 = match lit.node {
ast::LitFloat(ref v, _) |
ast::LitFloatUnsuffixed(ref v) => match from_str(v.get()) {
Some(f) => f,
None => return
},
_ => fail!()
};
if lit_val < min || lit_val > max {
cx.span_lint(TYPE_OVERFLOW, e.span,
"literal out of range for its type");
}
},
_ => ()
};
},
@ -265,6 +272,13 @@ impl LintPass for TypeLimits {
}
}
fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
match float_ty {
ast::TyF32 => (f32::MIN_VALUE as f64, f32::MAX_VALUE as f64),
ast::TyF64 => (f64::MIN_VALUE, f64::MAX_VALUE)
}
}
fn check_limits(tcx: &ty::ctxt, binop: ast::BinOp,
l: &ast::Expr, r: &ast::Expr) -> bool {
let (lit, expr, swap) = match (&l.node, &r.node) {

View File

@ -47,4 +47,9 @@ fn main() {
let x = -2147483648_i32; // should be OK
let x: i32 = -2147483649; //~ error: literal out of range for its type
let x = -2147483649_i32; //~ error: literal out of range for its type
let x = -3.40282348e+38_f32; //~ error: literal out of range for its type
let x = 3.40282348e+38_f32; //~ error: literal out of range for its type
let x = -1.7976931348623159e+308_f64; //~ error: literal out of range for its type
let x = 1.7976931348623159e+308_f64; //~ error: literal out of range for its type
}