about summary refs log tree commit diff
diff options
context:
space:
mode:
authoreconoplas <econoplas@gmail.com>2015-05-25 07:42:16 -0600
committereconoplas <econoplas@gmail.com>2015-05-25 07:42:16 -0600
commit43502adf071392df3dd74f019f6973d4b757e7d7 (patch)
treed7bf12485591f2e6de6fa5ac7f276b6052a2ef36
parent2bc0bf2586952350128b5abf94ae52a80c61efb2 (diff)
downloadrust-43502adf071392df3dd74f019f6973d4b757e7d7.tar.gz
rust-43502adf071392df3dd74f019f6973d4b757e7d7.zip
- Fix regression of literal out of range check for negative i64 values which was introduced by commit 7b1916d25347913fce3e336517ef22025ccd875f.
- Add missing test cases to test/compile-fail/lint-type-overflow.rs which would have detected the regression.
-rw-r--r--src/librustc_lint/builtin.rs6
-rw-r--r--src/test/compile-fail/lint-type-overflow.rs2
2 files changed, 6 insertions, 2 deletions
diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs
index e9db80b71ea..50fdeb39105 100644
--- a/src/librustc_lint/builtin.rs
+++ b/src/librustc_lint/builtin.rs
@@ -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));
diff --git a/src/test/compile-fail/lint-type-overflow.rs b/src/test/compile-fail/lint-type-overflow.rs
index ed6a0bd37eb..eb5b77f7a45 100644
--- a/src/test/compile-fail/lint-type-overflow.rs
+++ b/src/test/compile-fail/lint-type-overflow.rs
@@ -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