Skip to content

Commit 5eea93a

Browse files
Robert Gawdzik ☢robertg
Robert Gawdzik ☢
authored andcommitted
Fixes type range issue during linting (#16684)
- Ensures the propagated negation sign is properly utilized during type checking. - Removed redundant type checking, specifically regarding the out of bounds checking on a bounded type. - Closes #16684
1 parent e024017 commit 5eea93a

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/librustc/lint/builtin.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -172,33 +172,24 @@ impl LintPass for TypeLimits {
172172
ast::ExprLit(lit) => {
173173
match ty::get(ty::expr_ty(cx.tcx, e)).sty {
174174
ty::ty_int(t) => {
175-
let int_type = if t == ast::TyI {
176-
cx.sess().targ_cfg.int_type
177-
} else { t };
178-
let (min, max) = int_ty_range(int_type);
179-
let mut lit_val: i64 = match lit.node {
175+
match lit.node {
180176
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
181177
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
182-
if v > i64::MAX as u64{
178+
let int_type = if t == ast::TyI {
179+
cx.sess().targ_cfg.int_type
180+
} else { t };
181+
let (min, max) = int_ty_range(int_type);
182+
let negative = self.negated_expr_id == e.id;
183+
184+
if (negative && v > (min.abs() as u64)) ||
185+
(!negative && v > (max.abs() as u64)) {
183186
cx.span_lint(TYPE_OVERFLOW, e.span,
184187
"literal out of range for its type");
185188
return;
186189
}
187-
v as i64
188-
}
189-
ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
190-
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => {
191-
-(v as i64)
192190
}
193191
_ => fail!()
194192
};
195-
if self.negated_expr_id == e.id {
196-
lit_val *= -1;
197-
}
198-
if lit_val < min || lit_val > max {
199-
cx.span_lint(TYPE_OVERFLOW, e.span,
200-
"literal out of range for its type");
201-
}
202193
},
203194
ty::ty_uint(t) => {
204195
let uint_type = if t == ast::TyU {

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

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn main() {
4949
let x = -2147483649_i32; //~ error: literal out of range for its type
5050

5151
let x = 9223372036854775808_i64; //~ error: literal out of range for its type
52+
let x = -9223372036854775808_i64; // should be OK
5253
let x = 18446744073709551615_i64; //~ error: literal out of range for its type
5354

5455
let x = -3.40282348e+38_f32; //~ error: literal out of range for its type

0 commit comments

Comments
 (0)