Skip to content

Commit 0daa3ea

Browse files
committed
Auto merge of rust-lang#84147 - cuviper:array-method-dispatch, r=nikomatsakis,m-ou-se
Cautiously add IntoIterator for arrays by value Add the attribute described in rust-lang#84133, `#[rustc_skip_array_during_method_dispatch]`, which effectively hides a trait from method dispatch when the receiver type is an array. Then cherry-pick `IntoIterator for [T; N]` from rust-lang#65819 and gate it with that attribute. Arrays can now be used as `IntoIterator` normally, but `array.into_iter()` has edition-dependent behavior, returning `slice::Iter` for 2015 and 2018 editions, or `array::IntoIter` for 2021 and later. r? `@nikomatsakis` cc `@LukasKalbertodt` `@rust-lang/libs`
2 parents 8e86e02 + ede1ae8 commit 0daa3ea

File tree

3 files changed

+80
-20
lines changed

3 files changed

+80
-20
lines changed

core/src/array/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
155155
}
156156
}
157157

158+
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
159+
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
160+
// so those calls will still resolve to the slice implementation, by reference.
161+
#[cfg(not(bootstrap))]
162+
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
163+
impl<T, const N: usize> IntoIterator for [T; N] {
164+
type Item = T;
165+
type IntoIter = IntoIter<T, N>;
166+
167+
/// Creates a consuming iterator, that is, one that moves each value out of
168+
/// the array (from start to end). The array cannot be used after calling
169+
/// this unless `T` implements `Copy`, so the whole array is copied.
170+
///
171+
/// Arrays have special behavior when calling `.into_iter()` prior to the
172+
/// 2021 edition -- see the [array] Editions section for more information.
173+
///
174+
/// [array]: prim@array
175+
fn into_iter(self) -> Self::IntoIter {
176+
IntoIter::new(self)
177+
}
178+
}
179+
158180
#[stable(feature = "rust1", since = "1.0.0")]
159181
impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
160182
type Item = &'a T;

core/src/iter/traits/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ pub trait FromIterator<A>: Sized {
198198
/// }
199199
/// ```
200200
#[rustc_diagnostic_item = "IntoIterator"]
201+
#[cfg_attr(not(bootstrap), rustc_skip_array_during_method_dispatch)]
201202
#[stable(feature = "rust1", since = "1.0.0")]
202203
pub trait IntoIterator {
203204
/// The type of the elements being iterated over.

std/src/primitive_docs.rs

+57-20
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ mod prim_pointer {}
498498
/// - [`Copy`]
499499
/// - [`Clone`]
500500
/// - [`Debug`]
501-
/// - [`IntoIterator`] (implemented for `&[T; N]` and `&mut [T; N]`)
501+
/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`)
502502
/// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`]
503503
/// - [`Hash`]
504504
/// - [`AsRef`], [`AsMut`]
@@ -517,7 +517,8 @@ mod prim_pointer {}
517517
///
518518
/// # Examples
519519
///
520-
/// ```
520+
#[cfg_attr(bootstrap, doc = "```ignore")]
521+
#[cfg_attr(not(bootstrap), doc = "```")]
521522
/// let mut array: [i32; 3] = [0; 3];
522523
///
523524
/// array[1] = 1;
@@ -526,31 +527,16 @@ mod prim_pointer {}
526527
/// assert_eq!([1, 2], &array[1..]);
527528
///
528529
/// // This loop prints: 0 1 2
529-
/// for x in &array {
530+
/// for x in array {
530531
/// print!("{} ", x);
531532
/// }
532533
/// ```
533534
///
534-
/// An array itself is not iterable:
535-
///
536-
/// ```compile_fail,E0277
537-
/// let array: [i32; 3] = [0; 3];
538-
///
539-
/// for x in array { }
540-
/// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied
541-
/// ```
542-
///
543-
/// The solution is to coerce the array to a slice by calling a slice method:
535+
/// You can also iterate over reference to the array's elements:
544536
///
545537
/// ```
546-
/// # let array: [i32; 3] = [0; 3];
547-
/// for x in array.iter() { }
548-
/// ```
549-
///
550-
/// You can also use the array reference's [`IntoIterator`] implementation:
538+
/// let array: [i32; 3] = [0; 3];
551539
///
552-
/// ```
553-
/// # let array: [i32; 3] = [0; 3];
554540
/// for x in &array { }
555541
/// ```
556542
///
@@ -564,6 +550,57 @@ mod prim_pointer {}
564550
/// move_away(roa);
565551
/// ```
566552
///
553+
/// # Editions
554+
///
555+
/// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call
556+
/// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the
557+
/// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value.
558+
///
559+
#[cfg_attr(bootstrap, doc = "```rust,edition2018,ignore")]
560+
#[cfg_attr(not(bootstrap), doc = "```rust,edition2018")]
561+
/// # #![allow(array_into_iter)] // override our `deny(warnings)`
562+
/// let array: [i32; 3] = [0; 3];
563+
///
564+
/// // This creates a slice iterator, producing references to each value.
565+
/// for item in array.into_iter().enumerate() {
566+
/// let (i, x): (usize, &i32) = item;
567+
/// println!("array[{}] = {}", i, x);
568+
/// }
569+
///
570+
/// // The `array_into_iter` lint suggests this change for future compatibility:
571+
/// for item in array.iter().enumerate() {
572+
/// let (i, x): (usize, &i32) = item;
573+
/// println!("array[{}] = {}", i, x);
574+
/// }
575+
///
576+
/// // You can explicitly iterate an array by value using
577+
/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`:
578+
/// for item in IntoIterator::into_iter(array).enumerate() {
579+
/// let (i, x): (usize, i32) = item;
580+
/// println!("array[{}] = {}", i, x);
581+
/// }
582+
/// ```
583+
///
584+
/// Starting in the 2021 edition, `array.into_iter()` will use `IntoIterator` normally to iterate
585+
/// by value, and `iter()` should be used to iterate by reference like previous editions.
586+
///
587+
/// ```rust,edition2021,ignore
588+
/// # // FIXME: ignored because 2021 testing is still unstable
589+
/// let array: [i32; 3] = [0; 3];
590+
///
591+
/// // This iterates by reference:
592+
/// for item in array.iter().enumerate() {
593+
/// let (i, x): (usize, &i32) = item;
594+
/// println!("array[{}] = {}", i, x);
595+
/// }
596+
///
597+
/// // This iterates by value:
598+
/// for item in array.into_iter().enumerate() {
599+
/// let (i, x): (usize, i32) = item;
600+
/// println!("array[{}] = {}", i, x);
601+
/// }
602+
/// ```
603+
///
567604
/// [slice]: prim@slice
568605
/// [`Debug`]: fmt::Debug
569606
/// [`Hash`]: hash::Hash

0 commit comments

Comments
 (0)