Skip to content

Commit 8a87294

Browse files
committed
Auto merge of #25778 - econoplas:master, r=pnkfelix
A regression was introduced by commit 7b1916d #25612. Negative signed integer literals less than -9223372036854775808i64 were no longer properly reported as #[warn(overflowing_literals)]. Also adding missing test cases to test/compile-fail/lint-type-overflow.rs which could have detected the regression. Further explanation: The expression `(negative && v > max as u64 + 1)` relies on the fact that algebraically speaking `-min == max + 1` to avoid negation and removing the need for `min` completely. If i128 or i256 are ever added, it should also work for these types without requiring a change to `min != i64::MIN &&` also simplifying maintenance. r? @pnkfelix
2 parents 7cb9914 + 43502ad commit 8a87294

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/librustc_lint/builtin.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ impl LintPass for TypeLimits {
203203
} else {
204204
t
205205
};
206-
let (min, max) = int_ty_range(int_type);
206+
let (_, max) = int_ty_range(int_type);
207207
let negative = self.negated_expr_id == e.id;
208208

209-
if (negative && min != i64::MIN && v > -min as u64) ||
209+
// Detect literal value out of range [min, max] inclusive
210+
// avoiding use of -min to prevent overflow/panic
211+
if (negative && v > max as u64 + 1) ||
210212
(!negative && v > max as u64) {
211213
cx.span_lint(OVERFLOWING_LITERALS, e.span,
212214
&*format!("literal out of range for {:?}", t));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ fn main() {
5252
let x = 9223372036854775808_i64; //~ error: literal out of range for i64
5353
let x = -9223372036854775808_i64; // should be OK
5454
let x = 18446744073709551615_i64; //~ error: literal out of range for i64
55+
let x: i64 = -9223372036854775809; //~ error: literal out of range for i64
56+
let x = -9223372036854775809_i64; //~ error: literal out of range for i64
5557

5658
let x = -3.40282348e+38_f32; //~ error: literal out of range for f32
5759
let x = 3.40282348e+38_f32; //~ error: literal out of range for f32

0 commit comments

Comments
 (0)