auto merge of #10018 : fhahn/rust/check-inferred-ints, r=alexcrichton
I've started working on this issue and pushed a small commit, which adds a range check for integer literals in `middle::const_eval` (no `uint` at the moment)
At the moment, this patch is just a proof of concept, I'm not sure if there is a better function for the checks in `middle::const_eval`. This patch does not check for overflows after constant folding, eg:
let x: i8 = 99 + 99;
This commit is contained in:
commit
ade310cbb6
8 changed files with 126 additions and 21 deletions
|
|
@ -24,11 +24,13 @@ fn bar() -> i8 {
|
|||
|
||||
fn baz() -> bool {
|
||||
128 > bar() //~ ERROR comparison is useless due to type limits
|
||||
//~^ WARNING literal out of range for its type
|
||||
}
|
||||
|
||||
fn qux() {
|
||||
let mut i = 1i8;
|
||||
while 200 != i { //~ ERROR comparison is useless due to type limits
|
||||
//~^ WARNING literal out of range for its type
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
50
src/test/compile-fail/lint-type-overflow.rs
Normal file
50
src/test/compile-fail/lint-type-overflow.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
|
||||
#[deny(type_overflow)];
|
||||
|
||||
fn test(x: i8) {
|
||||
println!("x {}", x);
|
||||
}
|
||||
|
||||
#[allow(unused_variable)]
|
||||
fn main() {
|
||||
let x1: u8 = 255; // should be OK
|
||||
let x1: u8 = 256; //~ error: literal out of range for its type
|
||||
|
||||
let x1 = 255_u8; // should be OK
|
||||
let x1 = 256_u8; //~ error: literal out of range for its type
|
||||
|
||||
let x2: i8 = -128; // should be OK
|
||||
let x1: i8 = 128; //~ error: literal out of range for its type
|
||||
let x2: i8 = --128; //~ error: literal out of range for its type
|
||||
|
||||
let x3: i8 = -129; //~ error: literal out of range for its type
|
||||
let x3: i8 = -(129); //~ error: literal out of range for its type
|
||||
let x3: i8 = -{129}; //~ error: literal out of range for its type
|
||||
|
||||
test(1000); //~ error: literal out of range for its type
|
||||
|
||||
let x = 128_i8; //~ error: literal out of range for its type
|
||||
let x = 127_i8;
|
||||
let x = -128_i8;
|
||||
let x = -(128_i8);
|
||||
let x = -129_i8; //~ error: literal out of range for its type
|
||||
|
||||
let x: i32 = 2147483647; // should be OK
|
||||
let x = 2147483647_i32; // should be OK
|
||||
let x: i32 = 2147483648; //~ error: literal out of range for its type
|
||||
let x = 2147483648_i32; //~ error: literal out of range for its type
|
||||
let x: i32 = -2147483648; // should be OK
|
||||
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
|
||||
}
|
||||
|
|
@ -8,6 +8,6 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// error-pattern:literal out of range
|
||||
// compile-flags: -D type-overflow
|
||||
|
||||
fn main() { info!("{}", 300u8); }
|
||||
fn main() { info!("{}", 300u8); } //~ error: literal out of range for its type
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue