Check for literals that are out of range for their type

This commit is contained in:
Marijn Haverbeke 2011-12-07 21:53:05 +01:00
parent e3eca9174b
commit 3d0610b072
4 changed files with 40 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import syntax::ast::*;
import syntax::visit;
import syntax::{visit, ast_util};
import driver::session::session;
fn check_crate(sess: session, crate: @crate) {
@ -56,6 +56,23 @@ fn check_expr(sess: session, e: @expr, &&is_const: bool, v: visit::vt<bool>) {
}
}
}
alt e.node {
expr_lit(@{node: lit_int(v, t), _}) {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
t == ty_i ? sess.get_targ_cfg().int_type : t) {
sess.span_err(e.span, "literal out of range for its type");
}
}
}
expr_lit(@{node: lit_uint(v, t), _}) {
if v > ast_util::uint_ty_max(
t == ty_u ? sess.get_targ_cfg().uint_type : t) {
sess.span_err(e.span, "literal out of range for its type");
}
}
_ {}
}
visit::visit_expr(e, is_const, v);
}

View File

@ -132,6 +132,15 @@ fn int_ty_to_str(t: int_ty) -> str {
}
}
fn int_ty_max(t: int_ty) -> u64 {
alt t {
ty_i8. { 0x80u64 }
ty_i16. { 0x800u64 }
ty_char. | ty_i32. { 0x80000000u64 }
ty_i64. { 0x8000000000000000u64 }
}
}
fn uint_ty_to_str(t: uint_ty) -> str {
alt t {
ty_u. { "" } ty_u8. { "u8" } ty_u16. { "u16" }
@ -139,6 +148,15 @@ fn uint_ty_to_str(t: uint_ty) -> str {
}
}
fn uint_ty_max(t: uint_ty) -> u64 {
alt t {
ty_u8. { 0xffu64 }
ty_u16. { 0xffffu64 }
ty_u32. { 0xffffffffu64 }
ty_u64. { 0xffffffffffffffffu64 }
}
}
fn float_ty_to_str(t: float_ty) -> str {
alt t { ty_f. { "" } ty_f32. { "f32" } ty_f64. { "f64" } }
}

View File

@ -14,7 +14,7 @@ Const: max_value
Return the maximal value for a u32
*/
const max_value: u32 = 4294967296u32;
const max_value: u32 = 0xffff_ffffu32;
//
// Local Variables:

View File

@ -0,0 +1,3 @@
// error-pattern:literal out of range
fn main() { log 300u8; }