Skip into_iter() for arrays before 2021
This commit is contained in:
parent
35b1590223
commit
3dab4e22d4
@ -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;
|
||||
|
@ -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.
|
||||
|
17
src/test/ui/iterators/into-iter-on-arrays-2018.rs
Normal file
17
src/test/ui/iterators/into-iter-on-arrays-2018.rs
Normal file
@ -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);
|
||||
}
|
23
src/test/ui/iterators/into-iter-on-arrays-2018.stderr
Normal file
23
src/test/ui/iterators/into-iter-on-arrays-2018.stderr
Normal file
@ -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>
|
||||
|
15
src/test/ui/iterators/into-iter-on-arrays-2021.rs
Normal file
15
src/test/ui/iterators/into-iter-on-arrays-2021.rs
Normal file
@ -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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user