Skip to content

Commit

Permalink
- Fix regression of literal out of range check for negative i64 value…
Browse files Browse the repository at this point in the history
…s which was introduced by commit 7b1916d.

- Add missing test cases to test/compile-fail/lint-type-overflow.rs which would have detected the regression.
  • Loading branch information
econoplas committed May 25, 2015
1 parent 2bc0bf2 commit 43502ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/librustc_lint/builtin.rs
Expand Up @@ -203,10 +203,12 @@ impl LintPass for TypeLimits {
} else {
t
};
let (min, max) = int_ty_range(int_type);
let (_, max) = int_ty_range(int_type);
let negative = self.negated_expr_id == e.id;

if (negative && min != i64::MIN && v > -min as u64) ||
// Detect literal value out of range [min, max] inclusive
// avoiding use of -min to prevent overflow/panic
if (negative && v > max as u64 + 1) ||
(!negative && v > max as u64) {
cx.span_lint(OVERFLOWING_LITERALS, e.span,
&*format!("literal out of range for {:?}", t));
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/lint-type-overflow.rs
Expand Up @@ -52,6 +52,8 @@ fn main() {
let x = 9223372036854775808_i64; //~ error: literal out of range for i64
let x = -9223372036854775808_i64; // should be OK
let x = 18446744073709551615_i64; //~ error: literal out of range for i64
let x: i64 = -9223372036854775809; //~ error: literal out of range for i64
let x = -9223372036854775809_i64; //~ error: literal out of range for i64

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
Expand Down

0 comments on commit 43502ad

Please sign in to comment.