Skip to content

Commit 050c325

Browse files
committed
---
yaml --- r: 14840 b: refs/heads/try c: de79caa h: refs/heads/master v: v3
1 parent 7f8a50e commit 050c325

File tree

9 files changed

+130
-74
lines changed

9 files changed

+130
-74
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: c988800cf5611130e468761caa4d2f5adbdc6781
5+
refs/heads/try: de79caa97ebfb86adaaae1c54237a94a610f94c0
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/rustc/driver/driver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
148148
time(time_passes, "typechecking",
149149
bind typeck::check_crate(ty_cx, impl_map, crate));
150150
time(time_passes, "const checking",
151-
bind middle::check_const::check_crate(sess, crate, method_map));
151+
bind middle::check_const::check_crate(sess, crate, method_map,
152+
ty_cx));
152153

153154
if upto == cu_typeck { ret {crate: crate, tcx: some(ty_cx)}; }
154155

branches/try/src/rustc/middle/check_alt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,19 @@ fn pattern_supersedes(tcx: ty::ctxt, a: @pat, b: @pat) -> bool {
258258
}
259259
pat_lit(la) {
260260
alt b.node {
261-
pat_lit(lb) { lit_expr_eq(la, lb) }
261+
pat_lit(lb) { lit_expr_eq(tcx, la, lb) }
262262
_ { false }
263263
}
264264
}
265265
pat_range(begina, enda) {
266266
alt b.node {
267267
pat_lit(lb) {
268-
compare_lit_exprs(begina, lb) <= 0 &&
269-
compare_lit_exprs(enda, lb) >= 0
268+
compare_lit_exprs(tcx, begina, lb) <= 0 &&
269+
compare_lit_exprs(tcx, enda, lb) >= 0
270270
}
271271
pat_range(beginb, endb) {
272-
compare_lit_exprs(begina, beginb) <= 0 &&
273-
compare_lit_exprs(enda, endb) >= 0
272+
compare_lit_exprs(tcx, begina, beginb) <= 0 &&
273+
compare_lit_exprs(tcx, enda, endb) >= 0
274274
}
275275
_ { false }
276276
}

branches/try/src/rustc/middle/check_const.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import syntax::{visit, ast_util};
33
import driver::session::session;
44
import std::map::hashmap;
55

6-
fn check_crate(sess: session, crate: @crate, method_map: typeck::method_map) {
6+
fn check_crate(sess: session, crate: @crate, method_map: typeck::method_map,
7+
tcx: ty::ctxt) {
78
visit::visit_crate(*crate, false, visit::mk_vt(@{
89
visit_item: check_item,
910
visit_pat: check_pat,
10-
visit_expr: bind check_expr(sess, method_map, _, _, _)
11+
visit_expr: bind check_expr(sess, method_map, tcx, _, _, _)
1112
with *visit::default_visitor()
1213
}));
1314
sess.abort_if_errors();
@@ -42,8 +43,8 @@ fn check_pat(p: @pat, &&_is_const: bool, v: visit::vt<bool>) {
4243
}
4344
}
4445

45-
fn check_expr(sess: session, method_map: typeck::method_map, e: @expr,
46-
&&is_const: bool, v: visit::vt<bool>) {
46+
fn check_expr(sess: session, method_map: typeck::method_map, tcx: ty::ctxt,
47+
e: @expr, &&is_const: bool, v: visit::vt<bool>) {
4748
if is_const {
4849
alt e.node {
4950
expr_unary(box(_), _) | expr_unary(uniq(_), _) |
@@ -63,6 +64,14 @@ fn check_expr(sess: session, method_map: typeck::method_map, e: @expr,
6364
}
6465
}
6566
expr_lit(_) {}
67+
expr_cast(_, _) {
68+
let ety = ty::expr_ty(tcx, e);
69+
if !ty::type_is_numeric(ety) {
70+
sess.span_err(e.span, "can not cast to `" +
71+
util::ppaux::ty_to_str(tcx, ety) +
72+
"` in a constant expression");
73+
}
74+
}
6675
_ {
6776
sess.span_err(e.span,
6877
"constant contains unimplemented expression type");

branches/try/src/rustc/middle/trans/alt.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ enum opt {
2323
var(/* disr val */int, /* variant dids */{enm: def_id, var: def_id}),
2424
range(@ast::expr, @ast::expr)
2525
}
26-
fn opt_eq(a: opt, b: opt) -> bool {
26+
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(a, b) == 0 }
28+
(lit(a), lit(b)) { ast_util::compare_lit_exprs(tcx, a, b) == 0 }
2929
(range(a1, a2), range(b1, b2)) {
30-
ast_util::compare_lit_exprs(a1, b1) == 0 &&
31-
ast_util::compare_lit_exprs(a2, b2) == 0
30+
ast_util::compare_lit_exprs(tcx, a1, b1) == 0 &&
31+
ast_util::compare_lit_exprs(tcx, a2, b2) == 0
3232
}
3333
(var(a, _), var(b, _)) { a == b }
3434
_ { false }
@@ -161,18 +161,18 @@ fn enter_opt(tcx: ty::ctxt, m: match, opt: opt, col: uint,
161161
enter_match(tcx.def_map, m, col, val) {|p|
162162
alt p.node {
163163
ast::pat_enum(_, subpats) {
164-
if opt_eq(variant_opt(tcx, p.id), opt) { some(subpats) }
164+
if opt_eq(tcx, variant_opt(tcx, p.id), opt) { some(subpats) }
165165
else { none }
166166
}
167167
ast::pat_ident(_, none) if pat_is_variant(tcx.def_map, p) {
168-
if opt_eq(variant_opt(tcx, p.id), opt) { some([]) }
168+
if opt_eq(tcx, variant_opt(tcx, p.id), opt) { some([]) }
169169
else { none }
170170
}
171171
ast::pat_lit(l) {
172-
if opt_eq(lit(l), opt) { some([]) } else { none }
172+
if opt_eq(tcx, lit(l), opt) { some([]) } else { none }
173173
}
174174
ast::pat_range(l1, l2) {
175-
if opt_eq(range(l1, l2), opt) { some([]) } else { none }
175+
if opt_eq(tcx, range(l1, l2), opt) { some([]) } else { none }
176176
}
177177
_ { some(vec::from_elem(variant_size, dummy)) }
178178
}
@@ -232,21 +232,21 @@ fn enter_uniq(dm: def_map, m: match, col: uint, val: ValueRef) -> match {
232232
}
233233

234234
fn get_options(ccx: crate_ctxt, m: match, col: uint) -> [opt] {
235-
fn add_to_set(&set: [opt], val: opt) {
236-
for l in set { if opt_eq(l, val) { ret; } }
235+
fn add_to_set(tcx: ty::ctxt, &set: [opt], val: opt) {
236+
for l in set { if opt_eq(tcx, l, val) { ret; } }
237237
set += [val];
238238
}
239239

240240
let found = [];
241241
for br in m {
242242
let cur = br.pats[col];
243243
if pat_is_variant(ccx.tcx.def_map, cur) {
244-
add_to_set(found, variant_opt(ccx.tcx, br.pats[col].id));
244+
add_to_set(ccx.tcx, found, variant_opt(ccx.tcx, br.pats[col].id));
245245
} else {
246246
alt cur.node {
247-
ast::pat_lit(l) { add_to_set(found, lit(l)); }
247+
ast::pat_lit(l) { add_to_set(ccx.tcx, found, lit(l)); }
248248
ast::pat_range(l1, l2) {
249-
add_to_set(found, range(l1, l2));
249+
add_to_set(ccx.tcx, found, range(l1, l2));
250250
}
251251
_ {}
252252
}

branches/try/src/rustc/middle/trans/base.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,17 @@ fn float_cast(bcx: block, lldsttype: TypeRef, llsrctype: TypeRef,
25922592
} else { llsrc };
25932593
}
25942594

2595+
enum cast_kind { cast_pointer, cast_integral, cast_float,
2596+
cast_enum, cast_other, }
2597+
fn cast_type_kind(t: ty::t) -> cast_kind {
2598+
if ty::type_is_fp(t) { cast_float }
2599+
else if ty::type_is_unsafe_ptr(t) { cast_pointer }
2600+
else if ty::type_is_integral(t) { cast_integral }
2601+
else if ty::type_is_enum(t) { cast_enum }
2602+
else { cast_other }
2603+
}
2604+
2605+
25952606
fn trans_cast(cx: block, e: @ast::expr, id: ast::node_id,
25962607
dest: dest) -> block {
25972608
let ccx = cx.ccx();
@@ -2605,55 +2616,48 @@ fn trans_cast(cx: block, e: @ast::expr, id: ast::node_id,
26052616
let t_in = expr_ty(cx, e);
26062617
let ll_t_out = type_of(ccx, t_out);
26072618

2608-
enum kind { pointer, integral, float, enum_, other, }
2609-
fn t_kind(t: ty::t) -> kind {
2610-
ret if ty::type_is_fp(t) { float }
2611-
else if ty::type_is_unsafe_ptr(t) { pointer }
2612-
else if ty::type_is_integral(t) { integral }
2613-
else if ty::type_is_enum(t) { enum_ }
2614-
else { other };
2615-
}
2616-
let k_in = t_kind(t_in);
2617-
let k_out = t_kind(t_out);
2618-
let s_in = k_in == integral && ty::type_is_signed(t_in);
2619+
let k_in = cast_type_kind(t_in);
2620+
let k_out = cast_type_kind(t_out);
2621+
let s_in = k_in == cast_integral && ty::type_is_signed(t_in);
26192622

26202623
let newval =
26212624
alt {in: k_in, out: k_out} {
2622-
{in: integral, out: integral} {
2625+
{in: cast_integral, out: cast_integral} {
26232626
int_cast(e_res.bcx, ll_t_out, ll_t_in, e_res.val, s_in)
26242627
}
2625-
{in: float, out: float} {
2628+
{in: cast_float, out: cast_float} {
26262629
float_cast(e_res.bcx, ll_t_out, ll_t_in, e_res.val)
26272630
}
2628-
{in: integral, out: float} {
2631+
{in: cast_integral, out: cast_float} {
26292632
if s_in {
26302633
SIToFP(e_res.bcx, e_res.val, ll_t_out)
26312634
} else { UIToFP(e_res.bcx, e_res.val, ll_t_out) }
26322635
}
2633-
{in: float, out: integral} {
2636+
{in: cast_float, out: cast_integral} {
26342637
if ty::type_is_signed(t_out) {
26352638
FPToSI(e_res.bcx, e_res.val, ll_t_out)
26362639
} else { FPToUI(e_res.bcx, e_res.val, ll_t_out) }
26372640
}
2638-
{in: integral, out: pointer} {
2641+
{in: cast_integral, out: cast_pointer} {
26392642
IntToPtr(e_res.bcx, e_res.val, ll_t_out)
26402643
}
2641-
{in: pointer, out: integral} {
2644+
{in: cast_pointer, out: cast_integral} {
26422645
PtrToInt(e_res.bcx, e_res.val, ll_t_out)
26432646
}
2644-
{in: pointer, out: pointer} {
2647+
{in: cast_pointer, out: cast_pointer} {
26452648
PointerCast(e_res.bcx, e_res.val, ll_t_out)
26462649
}
2647-
{in: enum_, out: integral} | {in: enum_, out: float} {
2650+
{in: cast_enum, out: cast_integral} |
2651+
{in: cast_enum, out: cast_float} {
26482652
let cx = e_res.bcx;
26492653
let llenumty = T_opaque_enum_ptr(ccx);
26502654
let av_enum = PointerCast(cx, e_res.val, llenumty);
26512655
let lldiscrim_a_ptr = GEPi(cx, av_enum, [0, 0]);
26522656
let lldiscrim_a = Load(cx, lldiscrim_a_ptr);
26532657
alt k_out {
2654-
integral {int_cast(e_res.bcx, ll_t_out,
2655-
val_ty(lldiscrim_a), lldiscrim_a, true)}
2656-
float {SIToFP(e_res.bcx, lldiscrim_a, ll_t_out)}
2658+
cast_integral {int_cast(e_res.bcx, ll_t_out,
2659+
val_ty(lldiscrim_a), lldiscrim_a, true)}
2660+
cast_float {SIToFP(e_res.bcx, lldiscrim_a, ll_t_out)}
26572661
_ { ccx.sess.bug("translating unsupported cast.") }
26582662
}
26592663
}
@@ -4335,6 +4339,26 @@ fn trans_const_expr(cx: crate_ctxt, e: @ast::expr) -> ValueRef {
43354339
}
43364340
}
43374341
}
4342+
ast::expr_cast(base, tp) {
4343+
let ety = ty::expr_ty(cx.tcx, e), llty = type_of(cx, ety);
4344+
let basety = ty::expr_ty(cx.tcx, base);
4345+
let v = trans_const_expr(cx, base);
4346+
alt check (cast_type_kind(basety), cast_type_kind(ety)) {
4347+
(cast_integral, cast_integral) {
4348+
let s = if ty::type_is_signed(basety) { True } else { False };
4349+
llvm::LLVMConstIntCast(v, llty, s)
4350+
}
4351+
(cast_integral, cast_float) {
4352+
if ty::type_is_signed(basety) { llvm::LLVMConstSIToFP(v, llty) }
4353+
else { llvm::LLVMConstUIToFP(v, llty) }
4354+
}
4355+
(cast_float, cast_float) { llvm::LLVMConstFPCast(v, llty) }
4356+
(cast_float, cast_integral) {
4357+
if ty::type_is_signed(ety) { llvm::LLVMConstFPToSI(v, llty) }
4358+
else { llvm::LLVMConstFPToUI(v, llty) }
4359+
}
4360+
}
4361+
}
43384362
_ { cx.sess.span_bug(e.span,
43394363
"bad constant expression type in trans_const_expr"); }
43404364
}

branches/try/src/rustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ fn enum_variants(cx: ctxt, id: ast::def_id) -> @[variant_info] {
23732373
alt variant.node.disr_expr {
23742374
some (ex) {
23752375
// FIXME: issue #1417
2376-
disr_val = alt syntax::ast_util::eval_const_expr(ex) {
2376+
disr_val = alt syntax::ast_util::eval_const_expr(cx, ex) {
23772377
ast_util::const_int(val) {val as int}
23782378
_ { cx.sess.bug("tag_variants: bad disr expr"); }
23792379
}

branches/try/src/rustc/middle/typeck.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,8 +1437,9 @@ fn check_lit(ccx: @crate_ctxt, lit: @ast::lit) -> ty::t {
14371437
}
14381438
}
14391439

1440-
fn valid_range_bounds(from: @ast::expr, to: @ast::expr) -> bool {
1441-
ast_util::compare_lit_exprs(from, to) <= 0
1440+
fn valid_range_bounds(tcx: ty::ctxt, from: @ast::expr, to: @ast::expr)
1441+
-> bool {
1442+
ast_util::compare_lit_exprs(tcx, from, to) <= 0
14421443
}
14431444

14441445
fn check_pat_variant(fcx: @fn_ctxt, map: pat_util::pat_id_map,
@@ -1516,7 +1517,7 @@ fn check_pat(fcx: @fn_ctxt, map: pat_util::pat_id_map, pat: @ast::pat,
15161517
tcx.sess.span_err(pat.span, "mismatched types in range");
15171518
} else if !ty::type_is_numeric(b_ty) {
15181519
tcx.sess.span_err(pat.span, "non-numeric type used in range");
1519-
} else if !valid_range_bounds(begin, end) {
1520+
} else if !valid_range_bounds(tcx, begin, end) {
15201521
tcx.sess.span_err(begin.span, "lower range bound must be less \
15211522
than upper");
15221523
}
@@ -2913,14 +2914,14 @@ fn check_enum_variants(ccx: @crate_ctxt, sp: span, vs: [ast::variant],
29132914
alt v.node.disr_expr {
29142915
some(e) {
29152916
check_expr(fcx, e);
2916-
let cty = expr_ty(fcx.ccx.tcx, e);
2917-
let declty = ty::mk_int(fcx.ccx.tcx);
2917+
let cty = expr_ty(ccx.tcx, e);
2918+
let declty = ty::mk_int(ccx.tcx);
29182919
demand::simple(fcx, e.span, declty, cty);
29192920
// FIXME: issue #1417
29202921
// Also, check_expr (from check_const pass) doesn't guarantee that
29212922
// the expression in an form that eval_const_expr can handle, so
29222923
// we may still get an internal compiler error
2923-
alt syntax::ast_util::eval_const_expr(e) {
2924+
alt syntax::ast_util::eval_const_expr(ccx.tcx, e) {
29242925
syntax::ast_util::const_int(val) {
29252926
disr_val = val as int;
29262927
}

0 commit comments

Comments
 (0)