Skip to content

Fixes type range issue during linting (#16684) #17013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,33 +172,24 @@ impl LintPass for TypeLimits {
ast::ExprLit(lit) => {
match ty::get(ty::expr_ty(cx.tcx, e)).sty {
ty::ty_int(t) => {
let int_type = if t == ast::TyI {
cx.sess().targ_cfg.int_type
} else { t };
let (min, max) = int_ty_range(int_type);
let mut lit_val: i64 = match lit.node {
match lit.node {
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
if v > i64::MAX as u64{
let int_type = if t == ast::TyI {
cx.sess().targ_cfg.int_type
} else { t };
let (min, max) = int_ty_range(int_type);
let negative = self.negated_expr_id == e.id;

if (negative && v > (min.abs() as u64)) ||
(!negative && v > (max.abs() as u64)) {
cx.span_lint(TYPE_OVERFLOW, e.span,
"literal out of range for its type");
return;
}
v as i64
}
ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => {
-(v as i64)
}
_ => fail!()
};
if self.negated_expr_id == e.id {
lit_val *= -1;
}
if lit_val < min || lit_val > max {
cx.span_lint(TYPE_OVERFLOW, e.span,
"literal out of range for its type");
}
},
ty::ty_uint(t) => {
let uint_type = if t == ast::TyU {
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/lint-type-overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn main() {
let x = -2147483649_i32; //~ error: literal out of range for its type

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

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