diff --git a/doc/tutorial-container.md b/doc/tutorial-container.md index b7465ddb4df..19cd58bd3b4 100644 --- a/doc/tutorial-container.md +++ b/doc/tutorial-container.md @@ -164,19 +164,18 @@ dropped when they become unnecessary. ## For loops -The `foreach` keyword is transitional, and is going to replace the current -obsolete `for` loop. +The `for` keyword can be used as sugar for iterating through any iterator: ~~~ let xs = [2, 3, 5, 7, 11, 13, 17]; // print out all the elements in the vector -foreach x in xs.iter() { +for x in xs.iter() { println(x.to_str()) } // print out all but the first 3 elements in the vector -foreach x in xs.iter().skip(3) { +for x in xs.iter().skip(3) { println(x.to_str()) } ~~~ @@ -192,7 +191,7 @@ let ys = ["foo", "bar", "baz", "foobar"]; let mut it = xs.iter().zip(ys.iter()); // print out the pairs of elements up to (&3, &"baz") -foreach (x, y) in it { +for (x, y) in it { printfln!("%d %s", *x, *y); if *x == 3 { @@ -229,7 +228,7 @@ impl> FromIterator for ~[A] { pub fn from_iterator(iterator: &mut T) -> ~[A] { let (lower, _) = iterator.size_hint(); let mut xs = with_capacity(lower); - foreach x in iterator { + for x in iterator { xs.push(x); } xs @@ -300,7 +299,7 @@ printfln!("%?", it.next()); // prints `Some(&2)` printfln!("%?", it.next_back()); // prints `Some(&6)` // prints `5`, `4` and `3` -foreach &x in it.invert() { +for &x in it.invert() { printfln!("%?", x) } ~~~ @@ -319,7 +318,7 @@ let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2); printfln!("%?", it.next()); // prints `Some(2)` // prints `16`, `14`, `12`, `10`, `8`, `6`, `4` -foreach x in it.invert() { +for x in it.invert() { printfln!("%?", x); } ~~~ diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 924a9c22eb0..f867790ca13 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -76,7 +76,6 @@ pub enum lint { unused_imports, unnecessary_qualification, while_true, - deprecated_for_loop, path_statement, unrecognized_lint, non_camel_case_types, @@ -168,13 +167,6 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[ default: warn }), - ("deprecated_for_loop", - LintSpec { - lint: deprecated_for_loop, - desc: "recommend using `foreach` or `do` instead of `for`", - default: deny - }), - ("path_statement", LintSpec { lint: path_statement, @@ -613,24 +605,6 @@ fn lint_while_true() -> oldvisit::vt<@mut Context> { }) } -fn lint_deprecated_for_loop() -> oldvisit::vt<@mut Context> { - oldvisit::mk_vt(@oldvisit::Visitor { - visit_expr: |e, (cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| { - match e.node { - ast::expr_call(_, _, ast::ForSugar) | - ast::expr_method_call(_, _, _, _, _, ast::ForSugar) => { - cx.span_lint(deprecated_for_loop, e.span, - "`for` is deprecated; use `foreach in \ - ` or `do`") - } - _ => {} - } - oldvisit::visit_expr(e, (cx, vt)); - }, - .. *oldvisit::default_visitor() - }) -} - fn lint_type_limits() -> oldvisit::vt<@mut Context> { fn is_valid(binop: ast::binop, v: T, min: T, max: T) -> bool { @@ -1174,7 +1148,6 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::Crate) { // Register each of the lint passes with the context cx.add_oldvisit_lint(lint_while_true()); - cx.add_oldvisit_lint(lint_deprecated_for_loop()); cx.add_oldvisit_lint(lint_path_statement()); cx.add_oldvisit_lint(lint_heap()); cx.add_oldvisit_lint(lint_type_limits()); diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 7991e1cb56e..abb04e637ca 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -236,7 +236,9 @@ mod test { fn t(o1: Ordering, o2: Ordering, e: Ordering) { assert_eq!(lexical_ordering(o1, o2), e); } - for [Less, Equal, Greater].each |&o| { + + let xs = [Less, Equal, Greater]; + foreach &o in xs.iter() { t(Less, o, Less); t(Equal, o, o); t(Greater, o, Greater); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 386f027d6e1..ac3c50a53de 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -11,7 +11,7 @@ use abi; use abi::AbiSet; use ast::{Sigil, BorrowedSigil, ManagedSigil, OwnedSigil}; -use ast::{CallSugar, NoSugar, DoSugar, ForSugar}; +use ast::{CallSugar, NoSugar, DoSugar}; use ast::{TyBareFn, TyClosure}; use ast::{RegionTyParamBound, TraitTyParamBound}; use ast::{provided, public, purity}; @@ -24,7 +24,7 @@ use ast::{expr, expr_, expr_addr_of, expr_match, expr_again}; use ast::{expr_assign, expr_assign_op, expr_binary, expr_block}; use ast::{expr_break, expr_call, expr_cast, expr_do_body}; use ast::{expr_field, expr_fn_block, expr_if, expr_index}; -use ast::{expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac}; +use ast::{expr_lit, expr_log, expr_loop, expr_mac}; use ast::{expr_method_call, expr_paren, expr_path, expr_repeat}; use ast::{expr_ret, expr_self, expr_struct, expr_tup, expr_unary}; use ast::{expr_vec, expr_vstore, expr_vstore_mut_box}; @@ -1626,8 +1626,7 @@ impl Parser { } else if self.eat_keyword(keywords::ForEach) { return self.parse_for_expr(); } else if self.eat_keyword(keywords::For) { - return self.parse_sugary_call_expr(lo, ~"for", ForSugar, - expr_loop_body); + return self.parse_for_expr(); } else if self.eat_keyword(keywords::Do) { return self.parse_sugary_call_expr(lo, ~"do", DoSugar, expr_do_body); @@ -2326,9 +2325,9 @@ impl Parser { } } - // parse a 'foreach' .. 'in' expression ('foreach' token already eaten) + // parse a 'for' .. 'in' expression ('for' token already eaten) pub fn parse_for_expr(&self) -> @expr { - // Parse: `foreach in ` + // Parse: `for in ` let lo = self.last_span.lo; let pat = self.parse_pat();