From 3dab4e22d43c1ec724b23d301c81bdc2b99d50e7 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 12 Apr 2021 16:07:28 -0700 Subject: [PATCH] Skip into_iter() for arrays before 2021 --- library/core/src/array/mod.rs | 4 ++++ library/core/src/iter/traits/collect.rs | 1 + .../ui/iterators/into-iter-on-arrays-2018.rs | 17 ++++++++++++++ .../iterators/into-iter-on-arrays-2018.stderr | 23 +++++++++++++++++++ .../ui/iterators/into-iter-on-arrays-2021.rs | 15 ++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2018.rs create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2018.stderr create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2021.rs 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 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 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: 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 = 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 + +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 + 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 = array.into_iter(); + + // And you can always use the trait method explicitly as an array. + let _: IntoIter = IntoIterator::into_iter(array); +}