Skip to content

Commit 3d0610b

Browse files
committed
Check for literals that are out of range for their type
1 parent e3eca91 commit 3d0610b

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/comp/middle/check_const.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import syntax::ast::*;
2-
import syntax::visit;
2+
import syntax::{visit, ast_util};
33
import driver::session::session;
44

55
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>) {
5656
}
5757
}
5858
}
59+
alt e.node {
60+
expr_lit(@{node: lit_int(v, t), _}) {
61+
if t != ty_char {
62+
if (v as u64) > ast_util::int_ty_max(
63+
t == ty_i ? sess.get_targ_cfg().int_type : t) {
64+
sess.span_err(e.span, "literal out of range for its type");
65+
}
66+
}
67+
}
68+
expr_lit(@{node: lit_uint(v, t), _}) {
69+
if v > ast_util::uint_ty_max(
70+
t == ty_u ? sess.get_targ_cfg().uint_type : t) {
71+
sess.span_err(e.span, "literal out of range for its type");
72+
}
73+
}
74+
_ {}
75+
}
5976
visit::visit_expr(e, is_const, v);
6077
}
6178

src/comp/syntax/ast_util.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,31 @@ fn int_ty_to_str(t: int_ty) -> str {
132132
}
133133
}
134134

135+
fn int_ty_max(t: int_ty) -> u64 {
136+
alt t {
137+
ty_i8. { 0x80u64 }
138+
ty_i16. { 0x800u64 }
139+
ty_char. | ty_i32. { 0x80000000u64 }
140+
ty_i64. { 0x8000000000000000u64 }
141+
}
142+
}
143+
135144
fn uint_ty_to_str(t: uint_ty) -> str {
136145
alt t {
137146
ty_u. { "" } ty_u8. { "u8" } ty_u16. { "u16" }
138147
ty_u32. { "u32" } ty_u64. { "u64" }
139148
}
140149
}
141150

151+
fn uint_ty_max(t: uint_ty) -> u64 {
152+
alt t {
153+
ty_u8. { 0xffu64 }
154+
ty_u16. { 0xffffu64 }
155+
ty_u32. { 0xffffffffu64 }
156+
ty_u64. { 0xffffffffffffffffu64 }
157+
}
158+
}
159+
142160
fn float_ty_to_str(t: float_ty) -> str {
143161
alt t { ty_f. { "" } ty_f32. { "f32" } ty_f64. { "f64" } }
144162
}

src/libstd/u32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Const: max_value
1414

1515
Return the maximal value for a u32
1616
*/
17-
const max_value: u32 = 4294967296u32;
17+
const max_value: u32 = 0xffff_ffffu32;
1818

1919
//
2020
// Local Variables:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// error-pattern:literal out of range
2+
3+
fn main() { log 300u8; }

0 commit comments

Comments
 (0)