Skip to content

Commit b0074c5

Browse files
committed
Disallow rebinding / matching against consts in alts
As per Issue #1193. Closes #1193. I had to rename a few variables ("info" and "epsilon") to avoid clashing with in-scope constants, which is responsible for all the changes other than resolve and issue-1193.rs.
1 parent 1e51196 commit b0074c5

File tree

7 files changed

+65
-48
lines changed

7 files changed

+65
-48
lines changed

src/libcore/float.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> str {
5151
if (frac < epsilon && !exact) || digits == 0u { ret accum; }
5252
accum += ".";
5353
let mut i = digits;
54-
let mut epsilon = 1. / pow_with_uint(10u, i);
55-
while i > 0u && (frac >= epsilon || exact) {
54+
let mut epsilon_prime = 1. / pow_with_uint(10u, i);
55+
while i > 0u && (frac >= epsilon_prime || exact) {
5656
frac *= 10.0;
57-
epsilon *= 10.0;
57+
epsilon_prime *= 10.0;
5858
let digit = frac as uint;
5959
accum += uint::str(digit);
6060
frac -= digit as float;

src/librustsyntax/ext/expand.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt,
3434
some(normal({expander: exp, span: exp_sp})) {
3535
let expanded = exp(cx, pth.span, args, body);
3636

37-
let info = {call_site: s,
38-
callie: {name: extname, span: exp_sp}};
39-
cx.bt_push(expanded_from(info));
37+
cx.bt_push(expanded_from({call_site: s,
38+
callie: {name: extname, span: exp_sp}}));
4039
//keep going, outside-in
4140
let fully_expanded = fld.fold_expr(expanded).node;
4241
cx.bt_pop();

src/rustc/middle/resolve.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,19 +500,23 @@ fn resolve_names(e: @env, c: @ast::crate) {
500500
}
501501
_ {
502502
e.sess.span_err(p.span,
503-
"not a enum variant: " +
503+
"not an enum variant: " +
504504
ast_util::path_name(p));
505505
}
506506
}
507507
}
508508
/* Here we determine whether a given pat_ident binds a new
509-
variable a refers to a nullary enum. */
509+
variable or refers to a nullary enum. */
510510
ast::pat_ident(p, none) {
511511
alt lookup_in_scope(*e, sc, p.span, path_to_ident(p),
512512
ns_val, false) {
513513
some(fnd@ast::def_variant(_,_)) {
514514
e.def_map.insert(pat.id, fnd);
515515
}
516+
some(fnd@ast::def_const(_)) {
517+
e.sess.span_err(p.span, "Sorry, rebinding or matching \
518+
against symbolic constants is not allowed.");
519+
}
516520
// Binds a var -- nothing needs to be done
517521
_ {}
518522
}
@@ -1446,16 +1450,16 @@ fn list_search<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>)
14461450

14471451
fn lookup_in_local_mod(e: env, node_id: node_id, sp: span, id: ident,
14481452
ns: namespace, dr: dir) -> option<def> {
1449-
let info = alt e.mod_map.find(node_id) {
1453+
let inf = alt e.mod_map.find(node_id) {
14501454
some(x) { x }
14511455
none { e.sess.span_bug(sp, #fmt("lookup_in_local_mod: \
14521456
module %d not in mod_map", node_id)); }
14531457
};
1454-
if dr == outside && !is_exported(e, id, info) {
1455-
// if we're in a native mod, then dr==inside, so info.m is some _mod
1458+
if dr == outside && !is_exported(e, id, inf) {
1459+
// if we're in a native mod, then dr==inside, so inf.m is some _mod
14561460
ret none; // name is not visible
14571461
}
1458-
alt info.index.find(id) {
1462+
alt inf.index.find(id) {
14591463
none { }
14601464
some(lst) {
14611465
let found = list_search(lst, bind lookup_in_mie(e, _, ns));
@@ -1465,7 +1469,7 @@ fn lookup_in_local_mod(e: env, node_id: node_id, sp: span, id: ident,
14651469
}
14661470
}
14671471
// not local or explicitly imported; try globs:
1468-
ret lookup_glob_in_mod(e, info, sp, id, ns, outside);
1472+
ret lookup_glob_in_mod(e, inf, sp, id, ns, outside);
14691473
}
14701474

14711475
fn lookup_in_globs(e: env, globs: [glob_imp_def], sp: span, id: ident,

src/rustc/middle/trans/base.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,19 @@ fn get_tydesc(ccx: @crate_ctxt, t: ty::t,
371371
&static_ti: option<@tydesc_info>) -> ValueRef {
372372
assert !ty::type_has_params(t);
373373
// Otherwise, generate a tydesc if necessary, and return it.
374-
let info = get_static_tydesc(ccx, t);
375-
static_ti = some(info);
376-
info.tydesc
374+
let inf = get_static_tydesc(ccx, t);
375+
static_ti = some(inf);
376+
inf.tydesc
377377
}
378378

379379
fn get_static_tydesc(ccx: @crate_ctxt, t: ty::t) -> @tydesc_info {
380380
alt ccx.tydescs.find(t) {
381-
some(info) { ret info; }
381+
some(inf) { ret inf; }
382382
none {
383383
ccx.stats.n_static_tydescs += 1u;
384-
let info = declare_tydesc(ccx, t);
385-
ccx.tydescs.insert(t, info);
386-
ret info;
384+
let inf = declare_tydesc(ccx, t);
385+
ccx.tydescs.insert(t, inf);
386+
ret inf;
387387
}
388388
}
389389
}
@@ -455,7 +455,7 @@ fn declare_tydesc(ccx: @crate_ctxt, t: ty::t) -> @tydesc_info {
455455
let gvar = str::as_c_str(name, {|buf|
456456
llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type, buf)
457457
});
458-
let info =
458+
let inf =
459459
@{ty: t,
460460
tydesc: gvar,
461461
size: llsize,
@@ -464,7 +464,7 @@ fn declare_tydesc(ccx: @crate_ctxt, t: ty::t) -> @tydesc_info {
464464
mut drop_glue: none,
465465
mut free_glue: none};
466466
log(debug, "--- declare_tydesc " + ty_to_str(ccx.tcx, t));
467-
ret info;
467+
ret inf;
468468
}
469469

470470
type glue_helper = fn@(block, ValueRef, ty::t);
@@ -2870,8 +2870,8 @@ fn need_invoke(bcx: block) -> bool {
28702870
let mut cur = bcx;
28712871
loop {
28722872
alt cur.kind {
2873-
block_scope(info) {
2874-
for info.cleanups.each {|cleanup|
2873+
block_scope(inf) {
2874+
for inf.cleanups.each {|cleanup|
28752875
alt cleanup {
28762876
clean(_, cleanup_type) | clean_temp(_, _, cleanup_type) {
28772877
if cleanup_type == normal_exit_and_unwind {
@@ -2892,8 +2892,8 @@ fn need_invoke(bcx: block) -> bool {
28922892

28932893
fn have_cached_lpad(bcx: block) -> bool {
28942894
let mut res = false;
2895-
in_lpad_scope_cx(bcx) {|info|
2896-
alt info.landing_pad {
2895+
in_lpad_scope_cx(bcx) {|inf|
2896+
alt inf.landing_pad {
28972897
some(_) { res = true; }
28982898
none { res = false; }
28992899
}
@@ -2905,9 +2905,9 @@ fn in_lpad_scope_cx(bcx: block, f: fn(scope_info)) {
29052905
let mut bcx = bcx;
29062906
loop {
29072907
alt bcx.kind {
2908-
block_scope(info) {
2909-
if info.cleanups.len() > 0u || bcx.parent == parent_none {
2910-
f(info); ret;
2908+
block_scope(inf) {
2909+
if inf.cleanups.len() > 0u || bcx.parent == parent_none {
2910+
f(inf); ret;
29112911
}
29122912
}
29132913
_ {}
@@ -2920,13 +2920,13 @@ fn get_landing_pad(bcx: block) -> BasicBlockRef {
29202920
let _icx = bcx.insn_ctxt("get_landing_pad");
29212921

29222922
let mut cached = none, pad_bcx = bcx; // Guaranteed to be set below
2923-
in_lpad_scope_cx(bcx) {|info|
2923+
in_lpad_scope_cx(bcx) {|inf|
29242924
// If there is a valid landing pad still around, use it
2925-
alt info.landing_pad {
2925+
alt inf.landing_pad {
29262926
some(target) { cached = some(target); }
29272927
none {
29282928
pad_bcx = sub_block(bcx, "unwind");
2929-
info.landing_pad = some(pad_bcx.llbb);
2929+
inf.landing_pad = some(pad_bcx.llbb);
29302930
}
29312931
}
29322932
}
@@ -3792,16 +3792,16 @@ fn cleanup_and_leave(bcx: block, upto: option<BasicBlockRef>,
37923792
let mut done = false;
37933793
loop {
37943794
alt cur.kind {
3795-
block_scope(info) if info.cleanups.len() > 0u {
3796-
option::iter(vec::find(info.cleanup_paths,
3795+
block_scope(inf) if inf.cleanups.len() > 0u {
3796+
option::iter(vec::find(inf.cleanup_paths,
37973797
{|cp| cp.target == leave})) {|cp|
37983798
Br(bcx, cp.dest);
37993799
done = true;
38003800
}
38013801
if done { ret; }
38023802
let sub_cx = sub_block(bcx, "cleanup");
38033803
Br(bcx, sub_cx.llbb);
3804-
info.cleanup_paths += [{target: leave, dest: sub_cx.llbb}];
3804+
inf.cleanup_paths += [{target: leave, dest: sub_cx.llbb}];
38053805
bcx = trans_block_cleanups_(sub_cx, cur, is_lpad);
38063806
}
38073807
_ {}

src/rustc/middle/trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn in_scope_cx(cx: block, f: fn(scope_info)) {
381381
let mut cur = cx;
382382
loop {
383383
alt cur.kind {
384-
block_scope(info) { f(info); ret; }
384+
block_scope(inf) { f(inf); ret; }
385385
_ {}
386386
}
387387
cur = block_parent(cur);

src/rustc/middle/trans/shape.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,16 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
514514
// to each variant shape). As we do so, build up the header.
515515

516516
let mut header = [];
517-
let mut info = [];
517+
let mut inf = [];
518518
let header_sz = 2u16 * ccx.shape_cx.next_tag_id;
519519
let data_sz = vec::len(data) as u16;
520520

521-
let mut info_sz = 0u16;
521+
let mut inf_sz = 0u16;
522522
for ccx.shape_cx.tag_order.each {|did_|
523523
let did = did_; // Satisfy alias checker.
524524
let num_variants = vec::len(*ty::enum_variants(ccx.tcx, did)) as u16;
525-
add_u16(header, header_sz + info_sz);
526-
info_sz += 2u16 * (num_variants + 2u16) + 3u16;
525+
add_u16(header, header_sz + inf_sz);
526+
inf_sz += 2u16 * (num_variants + 2u16) + 3u16;
527527
}
528528

529529
// Construct the info tables, which contain offsets to the shape of each
@@ -535,11 +535,11 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
535535
for ccx.shape_cx.tag_order.each {|did_|
536536
let did = did_; // Satisfy alias checker.
537537
let variants = ty::enum_variants(ccx.tcx, did);
538-
add_u16(info, vec::len(*variants) as u16);
538+
add_u16(inf, vec::len(*variants) as u16);
539539

540540
// Construct the largest-variants table.
541-
add_u16(info,
542-
header_sz + info_sz + data_sz + (vec::len(lv_table) as u16));
541+
add_u16(inf,
542+
header_sz + inf_sz + data_sz + (vec::len(lv_table) as u16));
543543

544544
let lv = largest_variants(ccx, did);
545545
add_u16(lv_table, vec::len(lv) as u16);
@@ -555,22 +555,22 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
555555
let size_align = if dynamic { {size: 0u16, align: 0u8} }
556556
else { compute_static_enum_size(ccx, lv, did) };
557557
// Write in the static size and alignment of the enum.
558-
add_u16(info, size_align.size);
559-
info += [size_align.align];
558+
add_u16(inf, size_align.size);
559+
inf += [size_align.align];
560560

561561
// Now write in the offset of each variant.
562562
for vec::each(*variants) {|_v|
563-
add_u16(info, header_sz + info_sz + offsets[i]);
563+
add_u16(inf, header_sz + inf_sz + offsets[i]);
564564
i += 1u;
565565
}
566566
}
567567

568568
assert (i == vec::len(offsets));
569569
assert (header_sz == vec::len(header) as u16);
570-
assert (info_sz == vec::len(info) as u16);
570+
assert (inf_sz == vec::len(inf) as u16);
571571
assert (data_sz == vec::len(data) as u16);
572572

573-
header += info;
573+
header += inf;
574574
header += data;
575575
header += lv_table;
576576

src/test/compile-fail/issue-1193.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// error-pattern: Sorry, rebinding or matching against symbolic
2+
mod foo {
3+
type t = u8;
4+
5+
const a : t = 0u8;
6+
const b : t = 1u8;
7+
8+
fn bar(v: t) -> bool {
9+
alt v {
10+
a { ret true; }
11+
b { ret false; }
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)