Skip to content

Commit f80a7c5

Browse files
committed
---
yaml --- r: 12131 b: refs/heads/master c: a477aab h: refs/heads/master i: 12129: 60910eb 12127: c6acd51 v: v3
1 parent 55f9dac commit f80a7c5

File tree

9 files changed

+181
-179
lines changed

9 files changed

+181
-179
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: b7a741b5d0df5a57680aab14b4b6150803082983
2+
refs/heads/master: a477aaba08dcb560163ffa940e34898c15d0eccc
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/rustc/middle/check_alt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import syntax::ast::*;
3-
import syntax::ast_util::{variant_def_ids, dummy_sp, compare_lit_exprs,
4-
lit_expr_eq, unguarded_pat};
3+
import syntax::ast_util::{variant_def_ids, dummy_sp, unguarded_pat};
4+
import middle::const_eval::{compare_lit_exprs, lit_expr_eq};
55
import syntax::codemap::span;
66
import pat_util::*;
77
import syntax::visit;

trunk/src/rustc/middle/const_eval.rs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import syntax::ast::*;
2+
3+
// FIXME this doesn't handle big integer/float literals correctly (nor does
4+
// the rest of our literal handling)
5+
enum const_val {
6+
const_float(float),
7+
const_int(i64),
8+
const_uint(u64),
9+
const_str(str),
10+
}
11+
12+
// FIXME: issue #1417
13+
fn eval_const_expr(tcx: middle::ty::ctxt, e: @expr) -> const_val {
14+
import middle::ty;
15+
fn fromb(b: bool) -> const_val { const_int(b as i64) }
16+
alt check e.node {
17+
expr_unary(neg, inner) {
18+
alt check eval_const_expr(tcx, inner) {
19+
const_float(f) { const_float(-f) }
20+
const_int(i) { const_int(-i) }
21+
const_uint(i) { const_uint(-i) }
22+
}
23+
}
24+
expr_unary(not, inner) {
25+
alt check eval_const_expr(tcx, inner) {
26+
const_int(i) { const_int(!i) }
27+
const_uint(i) { const_uint(!i) }
28+
}
29+
}
30+
expr_binary(op, a, b) {
31+
alt check (eval_const_expr(tcx, a), eval_const_expr(tcx, b)) {
32+
(const_float(a), const_float(b)) {
33+
alt check op {
34+
add { const_float(a + b) } subtract { const_float(a - b) }
35+
mul { const_float(a * b) } div { const_float(a / b) }
36+
rem { const_float(a % b) } eq { fromb(a == b) }
37+
lt { fromb(a < b) } le { fromb(a <= b) } ne { fromb(a != b) }
38+
ge { fromb(a >= b) } gt { fromb(a > b) }
39+
}
40+
}
41+
(const_int(a), const_int(b)) {
42+
alt check op {
43+
add { const_int(a + b) } subtract { const_int(a - b) }
44+
mul { const_int(a * b) } div { const_int(a / b) }
45+
rem { const_int(a % b) } and | bitand { const_int(a & b) }
46+
or | bitor { const_int(a | b) } bitxor { const_int(a ^ b) }
47+
lsl { const_int(a << b) } lsr { const_int(a >> b) }
48+
asr { const_int(a >>> b) }
49+
eq { fromb(a == b) } lt { fromb(a < b) }
50+
le { fromb(a <= b) } ne { fromb(a != b) }
51+
ge { fromb(a >= b) } gt { fromb(a > b) }
52+
}
53+
54+
}
55+
(const_uint(a), const_uint(b)) {
56+
alt check op {
57+
add { const_uint(a + b) } subtract { const_uint(a - b) }
58+
mul { const_uint(a * b) } div { const_uint(a / b) }
59+
rem { const_uint(a % b) } and | bitand { const_uint(a & b) }
60+
or | bitor { const_uint(a | b) } bitxor { const_uint(a ^ b) }
61+
lsl { const_int((a << b) as i64) }
62+
lsr { const_int((a >> b) as i64) }
63+
asr { const_int((a >>> b) as i64) }
64+
eq { fromb(a == b) } lt { fromb(a < b) }
65+
le { fromb(a <= b) } ne { fromb(a != b) }
66+
ge { fromb(a >= b) } gt { fromb(a > b) }
67+
}
68+
}
69+
}
70+
}
71+
expr_cast(base, _) {
72+
let ety = ty::expr_ty(tcx, e);
73+
let base = eval_const_expr(tcx, base);
74+
alt check ty::get(ety).struct {
75+
ty::ty_float(_) {
76+
alt check base {
77+
const_uint(u) { const_float(u as f64) }
78+
const_int(i) { const_float(i as f64) }
79+
const_float(_) { base }
80+
}
81+
}
82+
ty::ty_uint(_) {
83+
alt check base {
84+
const_uint(_) { base }
85+
const_int(i) { const_uint(i as u64) }
86+
const_float(f) { const_uint(f as u64) }
87+
}
88+
}
89+
ty::ty_int(_) | ty::ty_bool {
90+
alt check base {
91+
const_uint(u) { const_int(u as i64) }
92+
const_int(_) { base }
93+
const_float(f) { const_int(f as i64) }
94+
}
95+
}
96+
}
97+
}
98+
expr_lit(lit) { lit_to_const(lit) }
99+
}
100+
}
101+
102+
fn lit_to_const(lit: @lit) -> const_val {
103+
alt lit.node {
104+
lit_str(s) { const_str(s) }
105+
lit_int(n, _) { const_int(n) }
106+
lit_uint(n, _) { const_uint(n) }
107+
lit_float(n, _) { const_float(option::get(float::from_str(n))) }
108+
lit_nil { const_int(0i64) }
109+
lit_bool(b) { const_int(b as i64) }
110+
}
111+
}
112+
113+
fn compare_const_vals(a: const_val, b: const_val) -> int {
114+
alt (a, b) {
115+
(const_int(a), const_int(b)) {
116+
if a == b {
117+
0
118+
} else if a < b {
119+
-1
120+
} else {
121+
1
122+
}
123+
}
124+
(const_uint(a), const_uint(b)) {
125+
if a == b {
126+
0
127+
} else if a < b {
128+
-1
129+
} else {
130+
1
131+
}
132+
}
133+
(const_float(a), const_float(b)) {
134+
if a == b {
135+
0
136+
} else if a < b {
137+
-1
138+
} else {
139+
1
140+
}
141+
}
142+
(const_str(a), const_str(b)) {
143+
if a == b {
144+
0
145+
} else if a < b {
146+
-1
147+
} else {
148+
1
149+
}
150+
}
151+
_ {
152+
fail "compare_const_vals: ill-typed comparison";
153+
}
154+
}
155+
}
156+
157+
fn compare_lit_exprs(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> int {
158+
compare_const_vals(eval_const_expr(tcx, a), eval_const_expr(tcx, b))
159+
}
160+
161+
fn lit_expr_eq(tcx: middle::ty::ctxt, a: @expr, b: @expr) -> bool {
162+
compare_lit_exprs(tcx, a, b) == 0
163+
}
164+
165+
fn lit_eq(a: @lit, b: @lit) -> bool {
166+
compare_const_vals(lit_to_const(a), lit_to_const(b)) == 0
167+
}

trunk/src/rustc/middle/trans/alt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ enum opt {
2525
}
2626
fn opt_eq(tcx: ty::ctxt, a: opt, b: opt) -> bool {
2727
alt (a, b) {
28-
(lit(a), lit(b)) { ast_util::compare_lit_exprs(tcx, a, b) == 0 }
28+
(lit(a), lit(b)) { const_eval::compare_lit_exprs(tcx, a, b) == 0 }
2929
(range(a1, a2), range(b1, b2)) {
30-
ast_util::compare_lit_exprs(tcx, a1, b1) == 0 &&
31-
ast_util::compare_lit_exprs(tcx, a2, b2) == 0
30+
const_eval::compare_lit_exprs(tcx, a1, b1) == 0 &&
31+
const_eval::compare_lit_exprs(tcx, a2, b2) == 0
3232
}
3333
(var(a, _), var(b, _)) { a == b }
3434
_ { false }

trunk/src/rustc/middle/tstate/auxiliary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ fn pred_args_matches(pattern: [constr_arg_general_<inst>], desc: pred_args) ->
676676
carg_base { if n != carg_base { ret false; } }
677677
carg_lit(l) {
678678
alt n {
679-
carg_lit(m) { if !lit_eq(l, m) { ret false; } }
679+
carg_lit(m) { if !const_eval::lit_eq(l, m) { ret false; } }
680680
_ { ret false; }
681681
}
682682
}

trunk/src/rustc/middle/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ fn arg_eq<T>(eq: fn(T, T) -> bool,
13061306
}
13071307
ast::carg_lit(l) {
13081308
alt b.node {
1309-
ast::carg_lit(m) { ret ast_util::lit_eq(l, m); } _ { ret false; }
1309+
ast::carg_lit(m) { ret const_eval::lit_eq(l, m); } _ { ret false; }
13101310
}
13111311
}
13121312
}
@@ -1894,8 +1894,8 @@ fn enum_variants(cx: ctxt, id: ast::def_id) -> @[variant_info] {
18941894
alt variant.node.disr_expr {
18951895
some (ex) {
18961896
// FIXME: issue #1417
1897-
disr_val = alt syntax::ast_util::eval_const_expr(cx, ex) {
1898-
ast_util::const_int(val) {val as int}
1897+
disr_val = alt const_eval::eval_const_expr(cx, ex) {
1898+
const_eval::const_int(val) {val as int}
18991899
_ { cx.sess.bug("tag_variants: bad disr expr"); }
19001900
}
19011901
}

trunk/src/rustc/middle/typeck.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ fn check_lit(ccx: @crate_ctxt, lit: @ast::lit) -> ty::t {
16451645

16461646
fn valid_range_bounds(tcx: ty::ctxt, from: @ast::expr, to: @ast::expr)
16471647
-> bool {
1648-
ast_util::compare_lit_exprs(tcx, from, to) <= 0
1648+
const_eval::compare_lit_exprs(tcx, from, to) <= 0
16491649
}
16501650

16511651
type pat_ctxt = {
@@ -3404,8 +3404,8 @@ fn check_enum_variants(ccx: @crate_ctxt, sp: span, vs: [ast::variant],
34043404
// Also, check_expr (from check_const pass) doesn't guarantee that
34053405
// the expression in an form that eval_const_expr can handle, so
34063406
// we may still get an internal compiler error
3407-
alt syntax::ast_util::eval_const_expr(ccx.tcx, e) {
3408-
syntax::ast_util::const_int(val) {
3407+
alt const_eval::eval_const_expr(ccx.tcx, e) {
3408+
const_eval::const_int(val) {
34093409
disr_val = val as int;
34103410
}
34113411
_ {

trunk/src/rustc/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod middle {
5050
mod pat_util;
5151
mod region;
5252
mod regionck;
53+
mod const_eval;
5354

5455
mod tstate {
5556
mod ck;

0 commit comments

Comments
 (0)