Skip to content

Commit deddb00

Browse files
committed
Merge pull request #8244 from thestinger/for
make `for` parse as `foreach` does r=huonw, bors is acting up and this has been run through the try bots
2 parents 2a7be1c + 87cf286 commit deddb00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+787
-843
lines changed

doc/tutorial-container.md

Lines changed: 7 additions & 8 deletions
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
~~~

doc/tutorial.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,6 @@ loop {
567567
This code prints out a weird sequence of numbers and stops as soon as
568568
it finds one that can be divided by five.
569569

570-
Rust also has a `for` construct. It's different from C's `for` and it works
571-
best when iterating over collections. See the section on [closures](#closures)
572-
to find out how to use `for` and higher-order functions for enumerating
573-
elements of a collection.
574-
575570
# Data structures
576571

577572
## Structs
@@ -1397,8 +1392,8 @@ assert!(crayons.len() == 3);
13971392
assert!(!crayons.is_empty());
13981393
13991394
// Iterate over a vector, obtaining a pointer to each element
1400-
// (`for` is explained in the next section)
1401-
foreach crayon in crayons.iter() {
1395+
// (`for` is explained in the container/iterator tutorial)
1396+
for crayon in crayons.iter() {
14021397
let delicious_crayon_wax = unwrap_crayon(*crayon);
14031398
eat_crayon_wax(delicious_crayon_wax);
14041399
}
@@ -1749,7 +1744,7 @@ of `vector`:
17491744
~~~~
17501745
fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
17511746
let mut accumulator = ~[];
1752-
foreach element in vector.iter() {
1747+
for element in vector.iter() {
17531748
accumulator.push(function(element));
17541749
}
17551750
return accumulator;
@@ -2027,7 +2022,7 @@ generic types.
20272022
~~~~
20282023
# trait Printable { fn print(&self); }
20292024
fn print_all<T: Printable>(printable_things: ~[T]) {
2030-
foreach thing in printable_things.iter() {
2025+
for thing in printable_things.iter() {
20312026
thing.print();
20322027
}
20332028
}
@@ -2073,7 +2068,7 @@ However, consider this function:
20732068
trait Drawable { fn draw(&self); }
20742069
20752070
fn draw_all<T: Drawable>(shapes: ~[T]) {
2076-
foreach shape in shapes.iter() { shape.draw(); }
2071+
for shape in shapes.iter() { shape.draw(); }
20772072
}
20782073
# let c: Circle = new_circle();
20792074
# draw_all(~[c]);
@@ -2088,7 +2083,7 @@ an _object_.
20882083
~~~~
20892084
# trait Drawable { fn draw(&self); }
20902085
fn draw_all(shapes: &[@Drawable]) {
2091-
foreach shape in shapes.iter() { shape.draw(); }
2086+
for shape in shapes.iter() { shape.draw(); }
20922087
}
20932088
~~~~
20942089

src/compiletest/header.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
4141
let mut pp_exact = None;
4242
let mut debugger_cmds = ~[];
4343
let mut check_lines = ~[];
44-
for iter_header(testfile) |ln| {
44+
do iter_header(testfile) |ln| {
4545
match parse_error_pattern(ln) {
4646
Some(ep) => error_patterns.push(ep),
4747
None => ()
@@ -74,6 +74,8 @@ pub fn load_props(testfile: &Path) -> TestProps {
7474
Some(cl) => check_lines.push(cl),
7575
None => ()
7676
};
77+
78+
true
7779
};
7880
return TestProps {
7981
error_patterns: error_patterns,
@@ -87,17 +89,19 @@ pub fn load_props(testfile: &Path) -> TestProps {
8789
}
8890

8991
pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
90-
for iter_header(testfile) |ln| {
91-
if parse_name_directive(ln, "xfail-test") { return true; }
92-
if parse_name_directive(ln, xfail_target()) { return true; }
93-
if config.mode == common::mode_pretty &&
94-
parse_name_directive(ln, "xfail-pretty") { return true; }
95-
};
96-
return false;
97-
9892
fn xfail_target() -> ~str {
9993
~"xfail-" + os::SYSNAME
10094
}
95+
96+
let val = do iter_header(testfile) |ln| {
97+
if parse_name_directive(ln, "xfail-test") { false }
98+
else if parse_name_directive(ln, xfail_target()) { false }
99+
else if config.mode == common::mode_pretty &&
100+
parse_name_directive(ln, "xfail-pretty") { false }
101+
else { true }
102+
};
103+
104+
!val
101105
}
102106

103107
fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
@@ -109,7 +113,7 @@ fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
109113
// module or function. This doesn't seem to be an optimization
110114
// with a warm page cache. Maybe with a cold one.
111115
if ln.starts_with("fn") || ln.starts_with("mod") {
112-
return false;
116+
return true;
113117
} else { if !(it(ln)) { return false; } }
114118
}
115119
return true;

src/libextra/bitv.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,14 @@ impl BigBitv {
206206
#[inline]
207207
pub fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
208208
let len = b.storage.len();
209-
for uint::iterate(0, len) |i| {
209+
do uint::iterate(0, len) |i| {
210210
let mask = big_mask(nbits, i);
211211
if mask & self.storage[i] != mask & b.storage[i] {
212-
return false;
212+
false
213+
} else {
214+
true
213215
}
214216
}
215-
return true;
216217
}
217218
}
218219

@@ -358,7 +359,7 @@ impl Bitv {
358359
pub fn clear(&mut self) {
359360
match self.rep {
360361
Small(ref mut b) => b.clear(),
361-
Big(ref mut s) => for s.each_storage() |w| { *w = 0u }
362+
Big(ref mut s) => { do s.each_storage() |w| { *w = 0u; true }; }
362363
}
363364
}
364365

@@ -367,15 +368,17 @@ impl Bitv {
367368
pub fn set_all(&mut self) {
368369
match self.rep {
369370
Small(ref mut b) => b.set_all(),
370-
Big(ref mut s) => for s.each_storage() |w| { *w = !0u } }
371+
Big(ref mut s) => { do s.each_storage() |w| { *w = !0u; true }; }
372+
}
371373
}
372374

373375
/// Invert all bits
374376
#[inline]
375377
pub fn negate(&mut self) {
376378
match self.rep {
377379
Small(ref mut b) => b.negate(),
378-
Big(ref mut s) => for s.each_storage() |w| { *w = !*w } }
380+
Big(ref mut s) => { do s.each_storage() |w| { *w = !*w; true }; }
381+
}
379382
}
380383

381384
/**
@@ -718,11 +721,11 @@ impl BitvSet {
718721
}
719722

720723
pub fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
721-
for self.common_iter(other).advance |(i, w1, w2)| {
724+
foreach (i, w1, w2) in self.common_iter(other) {
722725
if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
723-
return false;
726+
return false
724727
}
725-
}
728+
};
726729
/* everything we have that they don't also shows up */
727730
self.outlier_iter(other).advance(|(mine, i, w)|
728731
!mine || iterate_bits(i, w, |b| f(&b))
@@ -731,11 +734,11 @@ impl BitvSet {
731734

732735
pub fn symmetric_difference(&self, other: &BitvSet,
733736
f: &fn(&uint) -> bool) -> bool {
734-
for self.common_iter(other).advance |(i, w1, w2)| {
737+
foreach (i, w1, w2) in self.common_iter(other) {
735738
if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
736-
return false;
739+
return false
737740
}
738-
}
741+
};
739742
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
740743
}
741744

@@ -744,11 +747,11 @@ impl BitvSet {
744747
}
745748

746749
pub fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
747-
for self.common_iter(other).advance |(i, w1, w2)| {
750+
foreach (i, w1, w2) in self.common_iter(other) {
748751
if !iterate_bits(i, w1 | w2, |b| f(&b)) {
749-
return false;
752+
return false
750753
}
751-
}
754+
};
752755
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
753756
}
754757
}
@@ -758,12 +761,12 @@ impl cmp::Eq for BitvSet {
758761
if self.size != other.size {
759762
return false;
760763
}
761-
for self.common_iter(other).advance |(_, w1, w2)| {
764+
foreach (_, w1, w2) in self.common_iter(other) {
762765
if w1 != w2 {
763766
return false;
764767
}
765768
}
766-
for self.outlier_iter(other).advance |(_, _, w)| {
769+
foreach (_, _, w) in self.outlier_iter(other) {
767770
if w != 0 {
768771
return false;
769772
}
@@ -798,15 +801,15 @@ impl Set<uint> for BitvSet {
798801
}
799802

800803
fn is_subset(&self, other: &BitvSet) -> bool {
801-
for self.common_iter(other).advance |(_, w1, w2)| {
804+
foreach (_, w1, w2) in self.common_iter(other) {
802805
if w1 & w2 != w1 {
803806
return false;
804807
}
805808
}
806809
/* If anything is not ours, then everything is not ours so we're
807810
definitely a subset in that case. Otherwise if there's any stray
808811
ones that 'other' doesn't have, we're not a subset. */
809-
for self.outlier_iter(other).advance |(mine, _, w)| {
812+
foreach (mine, _, w) in self.outlier_iter(other) {
810813
if !mine {
811814
return true;
812815
} else if w != 0 {

src/libextra/crypto/sha2.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Engine512State {
112112

113113
// Putting the message schedule inside the same loop as the round calculations allows for
114114
// the compiler to generate better code.
115-
for uint::range_step(0, 64, 8) |t| {
115+
do uint::range_step(0, 64, 8) |t| {
116116
schedule_round!(t + 16);
117117
schedule_round!(t + 17);
118118
schedule_round!(t + 18);
@@ -130,9 +130,10 @@ impl Engine512State {
130130
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
131131
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
132132
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
133-
}
133+
true
134+
};
134135

135-
for uint::range_step(64, 80, 8) |t| {
136+
do uint::range_step(64, 80, 8) |t| {
136137
sha2_round!(a, b, c, d, e, f, g, h, K64, t);
137138
sha2_round!(h, a, b, c, d, e, f, g, K64, t + 1);
138139
sha2_round!(g, h, a, b, c, d, e, f, K64, t + 2);
@@ -141,7 +142,8 @@ impl Engine512State {
141142
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
142143
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
143144
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
144-
}
145+
true
146+
};
145147

146148
self.H0 += a;
147149
self.H1 += b;
@@ -507,7 +509,7 @@ impl Engine256State {
507509

508510
// Putting the message schedule inside the same loop as the round calculations allows for
509511
// the compiler to generate better code.
510-
for uint::range_step(0, 48, 8) |t| {
512+
do uint::range_step(0, 48, 8) |t| {
511513
schedule_round!(t + 16);
512514
schedule_round!(t + 17);
513515
schedule_round!(t + 18);
@@ -525,9 +527,10 @@ impl Engine256State {
525527
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
526528
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
527529
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
528-
}
530+
true
531+
};
529532

530-
for uint::range_step(48, 64, 8) |t| {
533+
do uint::range_step(48, 64, 8) |t| {
531534
sha2_round!(a, b, c, d, e, f, g, h, K32, t);
532535
sha2_round!(h, a, b, c, d, e, f, g, K32, t + 1);
533536
sha2_round!(g, h, a, b, c, d, e, f, K32, t + 2);
@@ -536,7 +539,8 @@ impl Engine256State {
536539
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
537540
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
538541
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
539-
}
542+
true
543+
};
540544

541545
self.H0 += a;
542546
self.H1 += b;

0 commit comments

Comments
 (0)