Skip to content

Commit fc202ca

Browse files
committed
Remove support for old-style for
Closes #1619
1 parent c902eaf commit fc202ca

File tree

21 files changed

+37
-164
lines changed

21 files changed

+37
-164
lines changed

doc/rust.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,11 @@ the function name.
10001000

10011001
~~~~
10021002
fn iter<T>(seq: [T], f: fn(T)) {
1003-
for elt: T in seq { f(elt); }
1003+
for seq.each {|elt| f(elt); }
10041004
}
10051005
fn map<T, U>(seq: [T], f: fn(T) -> U) -> [U] {
10061006
let mut acc = [];
1007-
for elt in seq { acc += [f(elt)]; }
1007+
for seq.each {|elt| acc += [f(elt)]; }
10081008
acc
10091009
}
10101010
~~~~
@@ -2113,7 +2113,7 @@ An example a for loop:
21132113
21142114
let v: [foo] = [a, b, c];
21152115
2116-
for e: foo in v {
2116+
for v.each {|e|
21172117
bar(e);
21182118
}
21192119
~~~~
@@ -2228,7 +2228,7 @@ fn main() {
22282228
~~~~
22292229

22302230
Multiple alternative patterns may be joined with the `|` operator. A
2231-
range of values may be specified with `to`. For example:
2231+
range of values may be specified with `to`. For example:
22322232

22332233
~~~~
22342234
# let x = 2;

doc/tutorial.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -690,19 +690,6 @@ do {
690690
} while any_cake_left();
691691
~~~~
692692

693-
When iterating over a vector, use `for` instead.
694-
695-
~~~~
696-
for elt in ["red", "green", "blue"] {
697-
io::println(elt);
698-
}
699-
~~~~
700-
701-
This will go over each element in the given vector (a three-element
702-
vector of strings, in this case), and repeatedly execute the body with
703-
`elt` bound to the current element. You may add an optional type
704-
declaration (`elt: str`) for the iteration variable if you want.
705-
706693
For more involved iteration, such as going over the elements of a hash
707694
table, Rust uses higher-order functions. We'll come back to those in a
708695
moment.
@@ -1095,8 +1082,8 @@ enum color {
10951082
~~~~
10961083

10971084
If an explicit discriminator is not specified for a variant, the value
1098-
defaults to the value of the previous variant plus one. If the first
1099-
variant does not have a discriminator, it defaults to 0. For example,
1085+
defaults to the value of the previous variant plus one. If the first
1086+
variant does not have a discriminator, it defaults to 0. For example,
11001087
the value of `north` is 0, `east` is 1, etc.
11011088

11021089
When an enum is C-like the `as` cast operator can be used to get the
@@ -1399,7 +1386,7 @@ not sure.
13991386

14001387
~~~~
14011388
fn for_each(v: [mut @int], iter: fn(@int)) {
1402-
for elt in v { iter(elt); }
1389+
for v.each {|elt| iter(elt); }
14031390
}
14041391
~~~~
14051392

@@ -1422,7 +1409,7 @@ with the `copy` operator:
14221409
~~~~
14231410
type mutrec = {mut x: int};
14241411
fn for_each(v: [mut mutrec], iter: fn(mutrec)) {
1425-
for elt in v { iter(copy elt); }
1412+
for v.each {|elt| iter(copy elt); }
14261413
}
14271414
~~~~
14281415

@@ -1509,7 +1496,7 @@ fn for_rev<T>(v: [T], act: fn(T)) {
15091496
15101497
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
15111498
let mut acc = [];
1512-
for elt in v { acc += [f(elt)]; }
1499+
for v.each {|elt| acc += [f(elt)]; }
15131500
ret acc;
15141501
}
15151502
~~~~
@@ -1987,7 +1974,7 @@ parameters.
19871974
# iface to_str { fn to_str() -> str; }
19881975
fn comma_sep<T: to_str>(elts: [T]) -> str {
19891976
let mut result = "", first = true;
1990-
for elt in elts {
1977+
for elts.each {|elt|
19911978
if first { first = false; }
19921979
else { result += ", "; }
19931980
result += elt.to_str();
@@ -2017,7 +2004,7 @@ iface seq<T> {
20172004
impl <T> of seq<T> for [T] {
20182005
fn len() -> uint { vec::len(self) }
20192006
fn iter(b: fn(T)) {
2020-
for elt in self { b(elt); }
2007+
for self.each {|elt| b(elt); }
20212008
}
20222009
}
20232010
~~~~
@@ -2037,7 +2024,7 @@ However, consider this function:
20372024
~~~~
20382025
# iface drawable { fn draw(); }
20392026
fn draw_all<T: drawable>(shapes: [T]) {
2040-
for shape in shapes { shape.draw(); }
2027+
for shapes.each {|shape| shape.draw(); }
20412028
}
20422029
~~~~
20432030

@@ -2051,7 +2038,7 @@ the function to be written simply like this:
20512038
~~~~
20522039
# iface drawable { fn draw(); }
20532040
fn draw_all(shapes: [drawable]) {
2054-
for shape in shapes { shape.draw(); }
2041+
for shapes.each {|shape| shape.draw(); }
20552042
}
20562043
~~~~
20572044

@@ -2136,7 +2123,7 @@ native mod crypto {
21362123
21372124
fn as_hex(data: [u8]) -> str {
21382125
let mut acc = "";
2139-
for byte in data { acc += #fmt("%02x", byte as uint); }
2126+
for data.each {|byte| acc += #fmt("%02x", byte as uint); }
21402127
ret acc;
21412128
}
21422129

src/compiletest/runtest.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,20 @@ fn check_error_patterns(props: test_props,
196196

197197
let mut next_err_idx = 0u;
198198
let mut next_err_pat = props.error_patterns[next_err_idx];
199+
let mut done = false;
199200
for str::split_char(procres.stderr, '\n').each {|line|
200201
if str::contains(line, next_err_pat) {
201202
#debug("found error pattern %s", next_err_pat);
202203
next_err_idx += 1u;
203204
if next_err_idx == vec::len(props.error_patterns) {
204205
#debug("found all error patterns");
205-
ret;
206+
done = true;
207+
break;
206208
}
207209
next_err_pat = props.error_patterns[next_err_idx];
208210
}
209211
}
212+
if done { ret; }
210213

211214
let missing_patterns =
212215
vec::slice(props.error_patterns, next_err_idx,

src/fuzzer/fuzzer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ pure fn safe_to_use_expr(e: ast::expr, tm: test_mode) -> bool {
7171
ast::expr_if_check(_, _, _) { false }
7272
ast::expr_block(_) { false }
7373
ast::expr_alt(_, _, _) { false }
74-
ast::expr_for(_, _, _) { false }
7574
ast::expr_while(_, _) { false }
7675

7776
// https://github.com/mozilla/rust/issues/955

src/libcore/os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ mod tests {
717717
}
718718

719719
#[test]
720+
#[ignore]
720721
fn test_env_getenv() {
721722
let e = env();
722723
assert vec::len(e) > 0u;

src/librustsyntax/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ enum expr_ {
289289
expr_cast(@expr, @ty),
290290
expr_if(@expr, blk, option<@expr>),
291291
expr_while(@expr, blk),
292-
expr_for(@local, @expr, blk),
293292
expr_do_while(blk, @expr),
294293
/* Conditionless loop (can be exited with break, cont, ret, or fail)
295294
Same semantics as while(true) { body }, but typestate knows that the

src/librustsyntax/fold.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,6 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
416416
expr_while(cond, body) {
417417
expr_while(fld.fold_expr(cond), fld.fold_block(body))
418418
}
419-
expr_for(decl, expr, blk) {
420-
expr_for(fld.fold_local(decl), fld.fold_expr(expr),
421-
fld.fold_block(blk))
422-
}
423419
expr_do_while(blk, expr) {
424420
expr_do_while(fld.fold_block(blk), fld.fold_expr(expr))
425421
}

src/librustsyntax/parse/parser.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,36 +1405,18 @@ fn parse_else_expr(p: parser) -> @ast::expr {
14051405

14061406
fn parse_for_expr(p: parser) -> @ast::expr {
14071407
let lo = p.last_span;
1408-
// FIXME remove this kludge after migration and snapshotting (#1619)
1409-
let new_style = alt p.token {
1410-
token::IDENT(_, false) { alt p.look_ahead(1u) {
1411-
token::DOT | token::LPAREN { true }
1412-
_ { false }
1413-
} }
1414-
token::IDENT(_, true) { true }
1415-
_ { false }
1416-
};
1417-
if new_style {
1418-
let call = parse_expr_res(p, RESTRICT_STMT_EXPR);
1419-
alt call.node {
1420-
ast::expr_call(f, args, true) {
1421-
let b_arg = vec::last(args);
1422-
let last = mk_expr(p, b_arg.span.lo, b_arg.span.hi,
1423-
ast::expr_loop_body(b_arg));
1424-
@{node: ast::expr_call(f, vec::init(args) + [last], true)
1425-
with *call}
1426-
}
1427-
_ {
1428-
p.span_fatal(lo, "`for` must be followed by a block call");
1429-
}
1430-
}
1431-
} else {
1432-
p.warn("old-style for");
1433-
let decl = parse_local(p, false, false);
1434-
expect_word(p, "in");
1435-
let seq = parse_expr(p);
1436-
let body = parse_block_no_value(p);
1437-
mk_expr(p, lo.lo, body.span.hi, ast::expr_for(decl, seq, body))
1408+
let call = parse_expr_res(p, RESTRICT_STMT_EXPR);
1409+
alt call.node {
1410+
ast::expr_call(f, args, true) {
1411+
let b_arg = vec::last(args);
1412+
let last = mk_expr(p, b_arg.span.lo, b_arg.span.hi,
1413+
ast::expr_loop_body(b_arg));
1414+
@{node: ast::expr_call(f, vec::init(args) + [last], true)
1415+
with *call}
1416+
}
1417+
_ {
1418+
p.span_fatal(lo, "`for` must be followed by a block call");
1419+
}
14381420
}
14391421
}
14401422

@@ -1755,8 +1737,7 @@ fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
17551737
ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _)
17561738
| ast::expr_alt(_, _, _) | ast::expr_block(_)
17571739
| ast::expr_do_while(_, _) | ast::expr_while(_, _)
1758-
| ast::expr_loop(_) | ast::expr_for(_, _, _)
1759-
| ast::expr_call(_, _, true) {
1740+
| ast::expr_loop(_) | ast::expr_call(_, _, true) {
17601741
false
17611742
}
17621743
_ { true }

src/librustsyntax/print/pprust.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,6 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
935935
space(s.s);
936936
print_block(s, blk);
937937
}
938-
ast::expr_for(decl, expr, blk) {
939-
head(s, "for");
940-
print_for_decl(s, decl, expr);
941-
space(s.s);
942-
print_block(s, blk);
943-
}
944938
ast::expr_do_while(blk, expr) {
945939
head(s, "do");
946940
space(s.s);

src/librustsyntax/visit.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
359359
}
360360
expr_while(x, b) { v.visit_expr(x, e, v); v.visit_block(b, e, v); }
361361
expr_loop(b) { v.visit_block(b, e, v); }
362-
expr_for(dcl, x, b) {
363-
v.visit_local(dcl, e, v);
364-
v.visit_expr(x, e, v);
365-
v.visit_block(b, e, v);
366-
}
367362
expr_do_while(b, x) { v.visit_block(b, e, v); v.visit_expr(x, e, v); }
368363
expr_alt(x, arms, _) {
369364
v.visit_expr(x, e, v);

src/rustc/middle/alias.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ fn visit_expr(cx: @ctx, ex: @ast::expr, sc: scope, v: vt<scope>) {
103103
visit_expr(cx, f, sc, v);
104104
}
105105
ast::expr_alt(input, arms, _) { check_alt(*cx, input, arms, sc, v); }
106-
ast::expr_for(decl, seq, blk) {
107-
visit_expr(cx, seq, sc, v);
108-
check_loop(*cx, sc) {|| check_for(*cx, decl, seq, blk, sc, v); }
109-
}
110106
ast::expr_path(pt) {
111107
check_var(*cx, ex, pt, ex.id, false, sc);
112108
handled = false;

src/rustc/middle/check_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn check_crate(tcx: ty::ctxt, crate: @crate) {
1111
},
1212
visit_expr: {|e: @expr, cx: ctx, v: visit::vt<ctx>|
1313
alt e.node {
14-
expr_for(_, e, b) | expr_while(e, b) | expr_do_while(b, e) {
14+
expr_while(e, b) | expr_do_while(b, e) {
1515
v.visit_expr(e, cx, v);
1616
v.visit_block(b, {in_loop: true with cx}, v);
1717
}

src/rustc/middle/last_use.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
106106
expr_while(_, _) | expr_do_while(_, _) | expr_loop(_) {
107107
visit_block(lp, cx) {|| visit::visit_expr(ex, cx, v);}
108108
}
109-
expr_for(_, coll, blk) {
110-
v.visit_expr(coll, cx, v);
111-
visit_block(lp, cx) {|| visit::visit_block(blk, cx, v);}
112-
}
113109
expr_alt(input, arms, _) {
114110
v.visit_expr(input, cx, v);
115111
let before = cx.current;

src/rustc/middle/resolve.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ fn resolve_names(e: @env, c: @ast::crate) {
408408
e.sess.abort_if_errors();
409409

410410
fn walk_expr(e: @env, exp: @ast::expr, sc: scopes, v: vt<scopes>) {
411-
visit_expr_with_scope(exp, sc, v);
411+
visit::visit_expr(exp, sc, v);
412412
alt exp.node {
413413
ast::expr_path(p) {
414414
maybe_insert(e, exp.id,
@@ -613,18 +613,6 @@ fn visit_arm_with_scope(a: ast::arm, sc: scopes, v: vt<scopes>) {
613613
v.visit_block(a.body, sc_inner, v);
614614
}
615615

616-
fn visit_expr_with_scope(x: @ast::expr, sc: scopes, v: vt<scopes>) {
617-
alt x.node {
618-
ast::expr_for(decl, coll, blk) {
619-
let new_sc = cons(scope_loop(decl), @sc);
620-
v.visit_expr(coll, sc, v);
621-
v.visit_local(decl, new_sc, v);
622-
v.visit_block(blk, new_sc, v);
623-
}
624-
_ { visit::visit_expr(x, sc, v); }
625-
}
626-
}
627-
628616
// This is only for irrefutable patterns (e.g. ones that appear in a let)
629617
// So if x occurs, and x is already known to be a enum, that's always an error
630618
fn visit_local_with_scope(e: @env, loc: @local, sc:scopes, v:vt<scopes>) {

src/rustc/middle/trans/base.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,39 +1685,6 @@ fn trans_if(cx: block, cond: @ast::expr, thn: ast::blk,
16851685
ret join_returns(cx, [then_bcx, else_bcx], [then_dest, else_dest], dest);
16861686
}
16871687

1688-
fn trans_for(cx: block, local: @ast::local, seq: @ast::expr,
1689-
body: ast::blk) -> block {
1690-
let _icx = cx.insn_ctxt("trans_for");
1691-
fn inner(bcx: block, local: @ast::local, curr: ValueRef, t: ty::t,
1692-
body: ast::blk, outer_next_cx: block) -> block {
1693-
let next_cx = sub_block(bcx, "next");
1694-
let scope_cx = loop_scope_block(bcx, cont_other(next_cx),
1695-
outer_next_cx, "for loop scope",
1696-
body.span);
1697-
Br(bcx, scope_cx.llbb);
1698-
let curr = PointerCast(bcx, curr,
1699-
T_ptr(type_of(bcx.ccx(), t)));
1700-
let bcx = alt::bind_irrefutable_pat(scope_cx, local.node.pat,
1701-
curr, false);
1702-
let bcx = trans_block(bcx, body, ignore);
1703-
cleanup_and_Br(bcx, scope_cx, next_cx.llbb);
1704-
ret next_cx;
1705-
}
1706-
let ccx = cx.ccx();
1707-
let next_cx = sub_block(cx, "next");
1708-
let seq_ty = expr_ty(cx, seq);
1709-
let {bcx: bcx, val: seq} = trans_temp_expr(cx, seq);
1710-
let seq = PointerCast(bcx, seq, T_ptr(ccx.opaque_vec_type));
1711-
let mut fill = tvec::get_fill(bcx, seq);
1712-
if ty::type_is_str(seq_ty) {
1713-
fill = Sub(bcx, fill, C_int(ccx, 1));
1714-
}
1715-
let bcx = tvec::iter_vec_raw(bcx, seq, seq_ty, fill,
1716-
bind inner(_, local, _, _, body, next_cx));
1717-
Br(bcx, next_cx.llbb);
1718-
ret next_cx;
1719-
}
1720-
17211688
fn trans_while(cx: block, cond: @ast::expr, body: ast::blk)
17221689
-> block {
17231690
let _icx = cx.insn_ctxt("trans_while");
@@ -3148,10 +3115,6 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
31483115
trans_check_expr(bcx, a, "Claim")
31493116
};
31503117
}
3151-
ast::expr_for(decl, seq, body) {
3152-
assert dest == ignore;
3153-
ret trans_for(bcx, decl, seq, body);
3154-
}
31553118
ast::expr_while(cond, body) {
31563119
assert dest == ignore;
31573120
ret trans_while(bcx, cond, body);

src/rustc/middle/trans/type_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
190190
}
191191
}
192192
}
193-
expr_for(_, _, _) | expr_do_while(_, _) | expr_alt(_, _, _) |
193+
expr_do_while(_, _) | expr_alt(_, _, _) |
194194
expr_block(_) | expr_if(_, _, _) | expr_while(_, _) |
195195
expr_fail(_) | expr_break | expr_cont | expr_unary(_, _) |
196196
expr_lit(_) | expr_assert(_) | expr_check(_, _) |

0 commit comments

Comments
 (0)