diff options
| author | Josh Stone <jistone@redhat.com> | 2021-04-12 16:07:28 -0700 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2021-04-16 11:12:01 -0700 |
| commit | 3dab4e22d43c1ec724b23d301c81bdc2b99d50e7 (patch) | |
| tree | 28f83332951c330c70a497df43db18d30616d5fc | |
| parent | 35b1590223b44a7da28c584c97967cc63f3e4e25 (diff) | |
| download | rust-3dab4e22d43c1ec724b23d301c81bdc2b99d50e7.tar.gz rust-3dab4e22d43c1ec724b23d301c81bdc2b99d50e7.zip | |
Skip into_iter() for arrays before 2021
| -rw-r--r-- | library/core/src/array/mod.rs | 4 | ||||
| -rw-r--r-- | library/core/src/iter/traits/collect.rs | 1 | ||||
| -rw-r--r-- | src/test/ui/iterators/into-iter-on-arrays-2018.rs | 17 | ||||
| -rw-r--r-- | src/test/ui/iterators/into-iter-on-arrays-2018.stderr | 23 | ||||
| -rw-r--r-- | src/test/ui/iterators/into-iter-on-arrays-2021.rs | 15 |
5 files changed, 60 insertions, 0 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index bd6f35edfac..ddbdc6a4acf 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -155,6 +155,10 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] { } } +// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator` +// hides this implementation from explicit `.into_iter()` calls on editions < 2021, +// so those calls will still resolve to the slice implementation, by reference. +#[cfg(not(bootstrap))] #[stable(feature = "array_into_iter_impl", since = "1.53.0")] impl<T, const N: usize> IntoIterator for [T; N] { type Item = T; diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 1ae6d15c12d..13a2e24cadd 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -198,6 +198,7 @@ pub trait FromIterator<A>: Sized { /// } /// ``` #[rustc_diagnostic_item = "IntoIterator"] +#[cfg_attr(not(bootstrap), rustc_skip_array_during_method_dispatch)] #[stable(feature = "rust1", since = "1.0.0")] pub trait IntoIterator { /// The type of the elements being iterated over. diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.rs b/src/test/ui/iterators/into-iter-on-arrays-2018.rs new file mode 100644 index 00000000000..cbcfcf2512f --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.rs @@ -0,0 +1,17 @@ +// check-pass + +use std::array::IntoIter; +use std::slice::Iter; + +fn main() { + let array = [0; 10]; + + // Before 2021, the method dispatched to `IntoIterator for &[T]`, + // which we continue to support for compatibility. + let _: Iter<'_, i32> = array.into_iter(); + //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` + //~| WARNING this was previously accepted by the compiler but is being phased out + + // But you can always use the trait method explicitly as an array. + let _: IntoIter<i32, 10> = IntoIterator::into_iter(array); +} diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr new file mode 100644 index 00000000000..7140c01ed0e --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr @@ -0,0 +1,23 @@ +warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. + --> $DIR/into-iter-on-arrays-2018.rs:11:34 + | +LL | let _: Iter<'_, i32> = array.into_iter(); + | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` + | + = note: `#[warn(array_into_iter)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> + +warning: 1 warning emitted + +Future incompatibility report: Future breakage date: None, diagnostic: +warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. + --> $DIR/into-iter-on-arrays-2018.rs:11:34 + | +LL | let _: Iter<'_, i32> = array.into_iter(); + | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` + | + = note: `#[warn(array_into_iter)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> + diff --git a/src/test/ui/iterators/into-iter-on-arrays-2021.rs b/src/test/ui/iterators/into-iter-on-arrays-2021.rs new file mode 100644 index 00000000000..731971484e8 --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2021.rs @@ -0,0 +1,15 @@ +// check-pass +// edition:2021 +// compile-flags: -Zunstable-options + +use std::array::IntoIter; + +fn main() { + let array = [0; 10]; + + // In 2021, the method dispatches to `IntoIterator for [T; N]`. + let _: IntoIter<i32, 10> = array.into_iter(); + + // And you can always use the trait method explicitly as an array. + let _: IntoIter<i32, 10> = IntoIterator::into_iter(array); +} |
