Skip to content

Commit 8febe73

Browse files
committed
---
yaml --- r: 6611 b: refs/heads/master c: 3d0610b h: refs/heads/master i: 6609: e93e952 6607: 9ea8842 v: v3
1 parent 244ab38 commit 8febe73

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: e3eca9174b9dc186de4ad44f80f7537f2e9e4c60
2+
refs/heads/master: 3d0610b072537f43946cf685bc9888ec7df44f08

trunk/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

trunk/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
}

trunk/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)