diff --git a/src/doc/trpl/unsafe.md b/src/doc/trpl/unsafe.md index 3acd1eefe89..2bd86fa987f 100644 --- a/src/doc/trpl/unsafe.md +++ b/src/doc/trpl/unsafe.md @@ -576,6 +576,10 @@ extern fn panic_fmt(args: &core::fmt::Arguments, #[lang = "eh_personality"] extern fn eh_personality() {} # #[start] fn start(argc: isize, argv: *const *const u8) -> isize { 0 } # fn main() {} +# mod std { // for-loops +# pub use core::iter; +# pub use core::option; +# } ``` Note that there is one extra lang item here which differs from the examples diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index d676cfca929..99172dcdef3 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -104,7 +104,9 @@ type MatchWords<'a> = Chain>, Skip u8 { let mut result = 0; - for i in 0..u8::BITS { + // FIXME(#21245) use a for loop + let mut iter = 0..u8::BITS; + while let Some(i) = iter.next() { result |= ((byte >> i) & 1) << (u8::BITS - 1 - i); } result diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index ea167348a64..8d6f06b25c5 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -271,7 +271,7 @@ impl DoubleEndedIterator for RawItems { #[unsafe_destructor] impl Drop for RawItems { fn drop(&mut self) { - for _ in *self {} + for _ in self.by_ref() {} } } @@ -1374,9 +1374,9 @@ impl Drop for MoveTraversalImpl { fn drop(&mut self) { // We need to cleanup the stored values manually, as the RawItems destructor would run // after our deallocation. - for _ in self.keys {} - for _ in self.vals {} - for _ in self.edges {} + for _ in self.keys.by_ref() {} + for _ in self.vals.by_ref() {} + for _ in self.edges.by_ref() {} let (alignment, size) = calculate_allocation_generic::(self.capacity, self.is_leaf); diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 82b92d26d28..f28262dc0fe 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -34,6 +34,8 @@ #![feature(unicode)] #![feature(hash)] #![cfg_attr(test, feature(test))] +// NOTE(stage0): remove after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] #[macro_use] extern crate core; @@ -114,6 +116,8 @@ mod std { pub use core::marker; // derive(Copy) pub use core::hash; // derive(Hash) pub use core::ops; // RangeFull + // for-loops + pub use core::iter; } #[cfg(test)] diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index 5b5e3be12e3..a9b31da8250 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -1510,7 +1510,7 @@ pub struct Drain<'a, T: 'a> { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T: 'a> Drop for Drain<'a, T> { fn drop(&mut self) { - for _ in *self {} + for _ in self.by_ref() {} self.inner.head = 0; self.inner.tail = 0; } @@ -1793,7 +1793,9 @@ mod tests { fn bench_push_back_100(b: &mut test::Bencher) { let mut deq = RingBuf::with_capacity(101); b.iter(|| { - for i in 0i..100 { + // FIXME(#21245) use a for loop + let mut iter = 0i..100; + while let Some(i) = iter.next() { deq.push_back(i); } deq.head = 0; @@ -1805,7 +1807,9 @@ mod tests { fn bench_push_front_100(b: &mut test::Bencher) { let mut deq = RingBuf::with_capacity(101); b.iter(|| { - for i in 0i..100 { + // FIXME(#21245) use a for loop + let mut iter = 0i..100; + while let Some(i) = iter.next() { deq.push_front(i); } deq.head = 0; diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 56d969b8946..57ee4704a9e 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1958,7 +1958,7 @@ mod tests { let mut amt = 0; let mut it = v.permutations(); let (min_size, max_opt) = it.size_hint(); - for _perm in it { + for _perm in it.by_ref() { amt += 1; } assert_eq!(amt, it.swaps.swaps_made); diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 35591a5e9ef..bbed943fc70 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -279,7 +279,7 @@ impl<'a> Iterator for Recompositions<'a> { loop { match self.state { Composing => { - for ch in self.iter { + for ch in self.iter.by_ref() { let ch_class = unicode::char::canonical_combining_class(ch); if self.composee.is_none() { if ch_class != 0 { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index ac6d7936f28..4ea8267135f 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1567,7 +1567,9 @@ impl Drop for Vec { // zeroed (when moving out, because of #[unsafe_no_drop_flag]). if self.cap != 0 { unsafe { - for x in self.iter() { + // FIXME(#21245) use a for loop + let mut iter = self.iter(); + while let Some(x) = iter.next() { ptr::read(x); } dealloc(*self.ptr, self.cap) @@ -1648,7 +1650,7 @@ impl IntoIter { #[unstable(feature = "collections")] pub fn into_inner(mut self) -> Vec { unsafe { - for _x in self { } + for _x in self.by_ref() { } let IntoIter { allocation, cap, ptr: _ptr, end: _end } = self; mem::forget(self); Vec { ptr: NonZero::new(allocation), cap: cap, len: 0 } @@ -1726,7 +1728,7 @@ impl Drop for IntoIter { fn drop(&mut self) { // destroy the remaining elements if self.cap != 0 { - for _x in *self {} + for _x in self.by_ref() {} unsafe { dealloc(self.allocation, self.cap); } @@ -1816,7 +1818,7 @@ impl<'a, T> Drop for Drain<'a, T> { // so we can use #[unsafe_no_drop_flag]. // destroy the remaining elements - for _x in *self {} + for _x in self.by_ref() {} } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index f9c6e0758e6..7847ed13ca9 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -195,7 +195,7 @@ pub trait IteratorExt: Iterator + Sized { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn nth(&mut self, mut n: usize) -> Option { - for x in *self { + for x in self.by_ref() { if n == 0 { return Some(x) } n -= 1; } @@ -492,7 +492,7 @@ pub trait IteratorExt: Iterator + Sized { /// fn process>(it: U) -> isize { /// let mut it = it.fuse(); /// let mut sum = 0; - /// for x in it { + /// for x in it.by_ref() { /// if x > 5 { /// break; /// } @@ -660,7 +660,7 @@ pub trait IteratorExt: Iterator + Sized { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn any(&mut self, mut f: F) -> bool where F: FnMut(Self::Item) -> bool { - for x in *self { if f(x) { return true; } } + for x in self.by_ref() { if f(x) { return true; } } false } @@ -680,7 +680,7 @@ pub trait IteratorExt: Iterator + Sized { fn find

(&mut self, mut predicate: P) -> Option where P: FnMut(&Self::Item) -> bool, { - for x in *self { + for x in self.by_ref() { if predicate(&x) { return Some(x) } } None @@ -703,7 +703,7 @@ pub trait IteratorExt: Iterator + Sized { P: FnMut(Self::Item) -> bool, { let mut i = 0; - for x in *self { + for x in self.by_ref() { if predicate(x) { return Some(i); } @@ -1664,7 +1664,7 @@ impl Iterator for Filter where I: Iterator, P: FnMut(& #[inline] fn next(&mut self) -> Option { - for x in self.iter { + for x in self.iter.by_ref() { if (self.predicate)(&x) { return Some(x); } else { @@ -1728,7 +1728,7 @@ impl Iterator for FilterMap where #[inline] fn next(&mut self) -> Option { - for x in self.iter { + for x in self.iter.by_ref() { match (self.f)(x) { Some(y) => return Some(y), None => () @@ -1914,7 +1914,7 @@ impl Iterator for SkipWhile where I: Iterator, P: FnMu #[inline] fn next(&mut self) -> Option { - for x in self.iter { + for x in self.iter.by_ref() { if self.flag || !(self.predicate)(&x) { self.flag = true; return Some(x); @@ -2207,7 +2207,7 @@ impl Iterator for FlatMap where fn next(&mut self) -> Option { loop { for inner in self.frontiter.iter_mut() { - for x in *inner { + for x in inner.by_ref() { return Some(x) } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 1032c56fa22..353d4252dfb 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -65,6 +65,8 @@ #![allow(unknown_features)] #![feature(int_uint)] #![feature(on_unimplemented)] #![deny(missing_docs)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] #[macro_use] mod macros; @@ -158,4 +160,6 @@ mod std { pub use marker; pub use ops; pub use option; + // for-loops + pub use iter; } diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dd9cc553c7c..1be7a0fb066 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1524,7 +1524,7 @@ macro_rules! from_str_radix_float_impl { let mut exp_info = None::<(char, uint)>; // Parse the integer part of the significand - for (i, c) in cs { + for (i, c) in cs.by_ref() { match c.to_digit(radix) { Some(digit) => { // shift significand one digit left @@ -1572,7 +1572,7 @@ macro_rules! from_str_radix_float_impl { // part of the significand if exp_info.is_none() { let mut power = 1.0; - for (i, c) in cs { + for (i, c) in cs.by_ref() { match c.to_digit(radix) { Some(digit) => { // Decrease power one order of magnitude diff --git a/src/librand/lib.rs b/src/librand/lib.rs index 76258151850..8c50550a6e8 100644 --- a/src/librand/lib.rs +++ b/src/librand/lib.rs @@ -498,6 +498,8 @@ mod std { pub use core::{option, fmt}; // panic!() pub use core::clone; // derive Clone pub use core::marker; + // for-loops + pub use core::iter; } #[cfg(test)] diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 580e55f78a9..e99b203cce7 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -41,6 +41,9 @@ #![feature(unicode)] #![feature(hash)] #![cfg_attr(test, feature(test))] +#![allow(unstable)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] extern crate arena; extern crate flate; diff --git a/src/librustc/middle/dataflow.rs b/src/librustc/middle/dataflow.rs index 01d42523f35..d5ee0e57b79 100644 --- a/src/librustc/middle/dataflow.rs +++ b/src/librustc/middle/dataflow.rs @@ -352,7 +352,9 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> { for (word_index, &word) in words.iter().enumerate() { if word != 0 { let base_index = word_index * uint::BITS; - for offset in 0u..uint::BITS { + // FIXME(#21245) use a for loop + let mut iter = 0u..uint::BITS; + while let Some(offset) = iter.next() { let bit = 1 << offset; if (word & bit) != 0 { // NB: we round up the total number of bits diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index bb026e237df..5a2ff926669 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -41,6 +41,9 @@ #![feature(std_misc)] #![feature(unicode)] #![feature(hash)] +#![allow(unstable)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] extern crate arena; extern crate flate; diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 31c9f996126..4772fac8bd4 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -84,6 +84,9 @@ This API is completely unstable and subject to change. #![feature(core)] #![feature(rustc_private)] #![feature(std_misc)] +#![allow(unstable)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] #[macro_use] extern crate log; #[macro_use] extern crate syntax; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index f8ba2dc2a74..974d8b96d32 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -32,6 +32,9 @@ #![feature(test)] #![feature(unicode)] #![feature(hash)] +#![allow(unstable)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] extern crate arena; extern crate getopts; diff --git a/src/libserialize/collection_impls.rs b/src/libserialize/collection_impls.rs index 8b39d91ffae..77308d0a66f 100644 --- a/src/libserialize/collection_impls.rs +++ b/src/libserialize/collection_impls.rs @@ -148,7 +148,9 @@ impl< fn decode(d: &mut D) -> Result, D::Error> { let bits = try!(d.read_uint()); let mut set = EnumSet::new(); - for bit in 0..uint::BITS { + // FIXME(#21245) use a for loop + let mut iter = 0..uint::BITS; + while let Some(bit) = iter.next() { if bits & (1 << bit) != 0 { set.insert(CLike::from_uint(1 << bit)); } diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 9aa38e711e7..ffc4c5c6bac 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -232,7 +232,10 @@ pub fn escape_default(c: u8, mut f: F) where _ => { f(b'\\'); f(b'x'); - for &offset in [4u, 0u].iter() { + // FIXME(#21245) use a for loop + let arr = [4u, 0u]; + let mut iter = arr.iter(); + while let ::option::Option::Some(&offset) = ::iter::Iterator::next(&mut iter) { match ((c as i32) >> offset) & 0xf { i @ 0 ... 9 => f(b'0' + (i as u8)), i => f(b'a' + (i as u8 - 10)), diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 9e6a45d8bf0..141c51d8363 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -15,7 +15,7 @@ use self::BucketState::*; use clone::Clone; use cmp; use hash::{Hash, Hasher}; -use iter::{Iterator, ExactSizeIterator, count}; +use iter::{Iterator, IteratorExt, ExactSizeIterator, count}; use marker::{Copy, Sized, self}; use mem::{min_align_of, size_of}; use mem; @@ -921,7 +921,7 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> { #[unsafe_destructor] impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> { fn drop(&mut self) { - for _ in *self {} + for _ in self.by_ref() {} } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index a016ba8fb0c..534ece52ff5 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -123,6 +123,9 @@ #![feature(rand)] #![feature(hash)] #![cfg_attr(test, feature(test))] +#![allow(unstable)] +// NOTE(stage0): remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] // Don't link to std. We are std. #![no_std] @@ -310,4 +313,6 @@ mod std { pub use slice; pub use boxed; // used for vec![] + // for-loops + pub use iter; } diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 88db27013ac..98e0320cd14 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -557,7 +557,7 @@ impl GenericPath for Path { } (Some(a), Some(_)) => { comps.push(".."); - for _ in itb { + for _ in itb.by_ref() { comps.push(".."); } comps.push(a); diff --git a/src/libstd/rand/os.rs b/src/libstd/rand/os.rs index 992afb2d10f..05677e1379d 100644 --- a/src/libstd/rand/os.rs +++ b/src/libstd/rand/os.rs @@ -404,7 +404,9 @@ mod test { } // start all the tasks - for tx in txs.iter() { + // FIXME(#21245) use a for loop + let mut iter = txs.iter(); + while let Some(tx) = iter.next() { tx.send(()).unwrap(); } } diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 6a43eccbaba..b503b92b00f 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -1476,7 +1476,9 @@ mod test { let _t = Thread::spawn(move|| { let mut count = 0; - for x in rx.iter() { + // FIXME(#21245) use a for loop + let mut iter = rx.iter(); + while let Some(x) = iter.next() { if count >= 3 { break; } else { @@ -1940,7 +1942,9 @@ mod sync_tests { let _t = Thread::spawn(move|| { let mut count = 0; - for x in rx.iter() { + // FIXME(#21245) use a for loop + let mut iter = rx.iter(); + while let Some(x) = iter.next() { if count >= 3 { break; } else { diff --git a/src/libstd/sync/mpsc/mpsc_queue.rs b/src/libstd/sync/mpsc/mpsc_queue.rs index 53eba131674..cc09cfd665c 100644 --- a/src/libstd/sync/mpsc/mpsc_queue.rs +++ b/src/libstd/sync/mpsc/mpsc_queue.rs @@ -185,7 +185,9 @@ mod tests { let tx = tx.clone(); let q = q.clone(); Thread::spawn(move|| { - for i in 0..nmsgs { + // FIXME(#21245) use a for loop + let mut iter = 0..nmsgs; + while let Some(i) = iter.next() { q.push(i); } tx.send(()).unwrap(); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index a1362f5382c..d739e5fe6a0 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -37,10 +37,14 @@ pub trait MoveMap { impl MoveMap for Vec { fn move_map(mut self, mut f: F) -> Vec where F: FnMut(T) -> T { - for p in self.iter_mut() { - unsafe { - // FIXME(#5016) this shouldn't need to zero to be safe. - ptr::write(p, f(ptr::read_and_zero(p))); + // FIXME(#21245) use a for loop + { + let mut iter = self.iter_mut(); + while let Some(p) = iter.next() { + unsafe { + // FIXME(#5016) this shouldn't need to zero to be safe. + ptr::write(p, f(ptr::read_and_zero(p))); + } } } self diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index ff4c7b565cb..456c6f73ce1 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -39,6 +39,9 @@ #![feature(rustc_private)] #![feature(std_misc)] #![feature(unicode)] +#![allow(unstable)] +// NOTE(stage0) remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] extern crate arena; extern crate fmt_macros; diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 8fea6bb539f..af8d5b86f4b 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -44,6 +44,9 @@ #![feature(rustc_private)] #![feature(std_misc)] #![feature(hash)] +#![allow(unstable)] +// NOTE(stage0): remove cfg_attr after a snapshot +#![cfg_attr(not(stage0), allow(unused_mut))] extern crate getopts; extern crate serialize; diff --git a/src/libunicode/lib.rs b/src/libunicode/lib.rs index 02f738c9d29..659d57b7292 100644 --- a/src/libunicode/lib.rs +++ b/src/libunicode/lib.rs @@ -84,4 +84,7 @@ mod std { pub use core::cmp; pub use core::fmt; pub use core::marker; + // for-loops + pub use core::iter; + pub use core::option; } diff --git a/src/test/bench/shootout-mandelbrot.rs b/src/test/bench/shootout-mandelbrot.rs index 4a9c5a91dcf..e84e6ac5699 100644 --- a/src/test/bench/shootout-mandelbrot.rs +++ b/src/test/bench/shootout-mandelbrot.rs @@ -133,7 +133,9 @@ fn mandelbrot(w: uint, mut out: W) -> old_io::IoResult<()> { (i + 1) * chunk_size }; - for &init_i in vec_init_i[start..end].iter() { + // FIXME(#21245) use a for loop + let mut iter = vec_init_i[start..end].iter(); + while let Some(&init_i) = iter.next() { write_line(init_i, init_r_slice, &mut res); } @@ -142,7 +144,9 @@ fn mandelbrot(w: uint, mut out: W) -> old_io::IoResult<()> { }).collect::>(); try!(writeln!(&mut out as &mut Writer, "P4\n{} {}", w, h)); - for res in data.into_iter() { + // FIXME(#21245) use a for loop + let mut iter = data.into_iter(); + while let Some(res) = iter.next() { try!(out.write(res.join().ok().unwrap().as_slice())); } out.flush() diff --git a/src/test/bench/shootout-meteor.rs b/src/test/bench/shootout-meteor.rs index e6ef58cba35..209cc985383 100644 --- a/src/test/bench/shootout-meteor.rs +++ b/src/test/bench/shootout-meteor.rs @@ -180,7 +180,9 @@ fn make_masks() -> Vec > > { // all unused piece can be placed on the board. fn is_board_unfeasible(board: u64, masks: &Vec>>) -> bool { let mut coverable = board; - for (i, masks_at) in masks.iter().enumerate() { + // FIXME(#21245) use a for loop + let mut iter = masks.iter().enumerate(); + while let Some((i, masks_at)) = iter.next() { if board & 1 << i != 0 { continue; } for (cur_id, pos_masks) in masks_at.iter().enumerate() { if board & 1 << (50 + cur_id) != 0 { continue; } @@ -222,7 +224,9 @@ fn to_vec(raw_sol: &List) -> Vec { let mut sol = repeat('.' as u8).take(50).collect::>(); for &m in raw_sol.iter() { let id = '0' as u8 + get_id(m); - for i in 0u..50 { + // FIXME(#21245) use a for loop + let mut iter = 0u..50; + while let Some(i) = iter.next() { if m & 1 << i != 0 { sol[i] = id; }