Skip to content

Commit b41d047

Browse files
committedAug 3, 2013
make for parse as foreach does
Closes #6997
1 parent b3ad685 commit b41d047

File tree

4 files changed

+15
-42
lines changed

4 files changed

+15
-42
lines changed
 

‎doc/tutorial-container.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,18 @@ dropped when they become unnecessary.
164164
165165
## For loops
166166
167-
The `foreach` keyword is transitional, and is going to replace the current
168-
obsolete `for` loop.
167+
The `for` keyword can be used as sugar for iterating through any iterator:
169168
170169
~~~
171170
let xs = [2, 3, 5, 7, 11, 13, 17];
172171

173172
// print out all the elements in the vector
174-
foreach x in xs.iter() {
173+
for x in xs.iter() {
175174
println(x.to_str())
176175
}
177176

178177
// print out all but the first 3 elements in the vector
179-
foreach x in xs.iter().skip(3) {
178+
for x in xs.iter().skip(3) {
180179
println(x.to_str())
181180
}
182181
~~~
@@ -192,7 +191,7 @@ let ys = ["foo", "bar", "baz", "foobar"];
192191
let mut it = xs.iter().zip(ys.iter());
193192

194193
// print out the pairs of elements up to (&3, &"baz")
195-
foreach (x, y) in it {
194+
for (x, y) in it {
196195
printfln!("%d %s", *x, *y);
197196

198197
if *x == 3 {
@@ -229,7 +228,7 @@ impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
229228
pub fn from_iterator(iterator: &mut T) -> ~[A] {
230229
let (lower, _) = iterator.size_hint();
231230
let mut xs = with_capacity(lower);
232-
foreach x in iterator {
231+
for x in iterator {
233232
xs.push(x);
234233
}
235234
xs
@@ -300,7 +299,7 @@ printfln!("%?", it.next()); // prints `Some(&2)`
300299
printfln!("%?", it.next_back()); // prints `Some(&6)`
301300

302301
// prints `5`, `4` and `3`
303-
foreach &x in it.invert() {
302+
for &x in it.invert() {
304303
printfln!("%?", x)
305304
}
306305
~~~
@@ -319,7 +318,7 @@ let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
319318
printfln!("%?", it.next()); // prints `Some(2)`
320319

321320
// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
322-
foreach x in it.invert() {
321+
for x in it.invert() {
323322
printfln!("%?", x);
324323
}
325324
~~~

‎src/librustc/middle/lint.rs

-27
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub enum lint {
7676
unused_imports,
7777
unnecessary_qualification,
7878
while_true,
79-
deprecated_for_loop,
8079
path_statement,
8180
unrecognized_lint,
8281
non_camel_case_types,
@@ -168,13 +167,6 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
168167
default: warn
169168
}),
170169

171-
("deprecated_for_loop",
172-
LintSpec {
173-
lint: deprecated_for_loop,
174-
desc: "recommend using `foreach` or `do` instead of `for`",
175-
default: deny
176-
}),
177-
178170
("path_statement",
179171
LintSpec {
180172
lint: path_statement,
@@ -613,24 +605,6 @@ fn lint_while_true() -> oldvisit::vt<@mut Context> {
613605
})
614606
}
615607

616-
fn lint_deprecated_for_loop() -> oldvisit::vt<@mut Context> {
617-
oldvisit::mk_vt(@oldvisit::Visitor {
618-
visit_expr: |e, (cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
619-
match e.node {
620-
ast::expr_call(_, _, ast::ForSugar) |
621-
ast::expr_method_call(_, _, _, _, _, ast::ForSugar) => {
622-
cx.span_lint(deprecated_for_loop, e.span,
623-
"`for` is deprecated; use `foreach <pat> in \
624-
<iterator>` or `do`")
625-
}
626-
_ => {}
627-
}
628-
oldvisit::visit_expr(e, (cx, vt));
629-
},
630-
.. *oldvisit::default_visitor()
631-
})
632-
}
633-
634608
fn lint_type_limits() -> oldvisit::vt<@mut Context> {
635609
fn is_valid<T:cmp::Ord>(binop: ast::binop, v: T,
636610
min: T, max: T) -> bool {
@@ -1174,7 +1148,6 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::Crate) {
11741148

11751149
// Register each of the lint passes with the context
11761150
cx.add_oldvisit_lint(lint_while_true());
1177-
cx.add_oldvisit_lint(lint_deprecated_for_loop());
11781151
cx.add_oldvisit_lint(lint_path_statement());
11791152
cx.add_oldvisit_lint(lint_heap());
11801153
cx.add_oldvisit_lint(lint_type_limits());

‎src/libstd/cmp.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ mod test {
236236
fn t(o1: Ordering, o2: Ordering, e: Ordering) {
237237
assert_eq!(lexical_ordering(o1, o2), e);
238238
}
239-
for [Less, Equal, Greater].each |&o| {
239+
240+
let xs = [Less, Equal, Greater];
241+
foreach &o in xs.iter() {
240242
t(Less, o, Less);
241243
t(Equal, o, o);
242244
t(Greater, o, Greater);

‎src/libsyntax/parse/parser.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use abi;
1212
use abi::AbiSet;
1313
use ast::{Sigil, BorrowedSigil, ManagedSigil, OwnedSigil};
14-
use ast::{CallSugar, NoSugar, DoSugar, ForSugar};
14+
use ast::{CallSugar, NoSugar, DoSugar};
1515
use ast::{TyBareFn, TyClosure};
1616
use ast::{RegionTyParamBound, TraitTyParamBound};
1717
use ast::{provided, public, purity};
@@ -24,7 +24,7 @@ use ast::{expr, expr_, expr_addr_of, expr_match, expr_again};
2424
use ast::{expr_assign, expr_assign_op, expr_binary, expr_block};
2525
use ast::{expr_break, expr_call, expr_cast, expr_do_body};
2626
use ast::{expr_field, expr_fn_block, expr_if, expr_index};
27-
use ast::{expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac};
27+
use ast::{expr_lit, expr_log, expr_loop, expr_mac};
2828
use ast::{expr_method_call, expr_paren, expr_path, expr_repeat};
2929
use ast::{expr_ret, expr_self, expr_struct, expr_tup, expr_unary};
3030
use ast::{expr_vec, expr_vstore, expr_vstore_mut_box};
@@ -1626,8 +1626,7 @@ impl Parser {
16261626
} else if self.eat_keyword(keywords::ForEach) {
16271627
return self.parse_for_expr();
16281628
} else if self.eat_keyword(keywords::For) {
1629-
return self.parse_sugary_call_expr(lo, ~"for", ForSugar,
1630-
expr_loop_body);
1629+
return self.parse_for_expr();
16311630
} else if self.eat_keyword(keywords::Do) {
16321631
return self.parse_sugary_call_expr(lo, ~"do", DoSugar,
16331632
expr_do_body);
@@ -2326,9 +2325,9 @@ impl Parser {
23262325
}
23272326
}
23282327

2329-
// parse a 'foreach' .. 'in' expression ('foreach' token already eaten)
2328+
// parse a 'for' .. 'in' expression ('for' token already eaten)
23302329
pub fn parse_for_expr(&self) -> @expr {
2331-
// Parse: `foreach <src_pat> in <src_expr> <src_loop_block>`
2330+
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
23322331

23332332
let lo = self.last_span.lo;
23342333
let pat = self.parse_pat();

0 commit comments

Comments
 (0)
Please sign in to comment.