Skip to content

Commit 3dab4e2

Browse files
committed
Skip into_iter() for arrays before 2021
1 parent 35b1590 commit 3dab4e2

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

library/core/src/array/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ 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))]
158162
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
159163
impl<T, const N: usize> IntoIterator for [T; N] {
160164
type Item = T;

library/core/src/iter/traits/collect.rs

Lines changed: 1 addition & 0 deletions
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.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
3+
use std::array::IntoIter;
4+
use std::slice::Iter;
5+
6+
fn main() {
7+
let array = [0; 10];
8+
9+
// Before 2021, the method dispatched to `IntoIterator for &[T]`,
10+
// which we continue to support for compatibility.
11+
let _: Iter<'_, i32> = array.into_iter();
12+
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter`
13+
//~| WARNING this was previously accepted by the compiler but is being phased out
14+
15+
// But you can always use the trait method explicitly as an array.
16+
let _: IntoIter<i32, 10> = IntoIterator::into_iter(array);
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
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.
2+
--> $DIR/into-iter-on-arrays-2018.rs:11:34
3+
|
4+
LL | let _: Iter<'_, i32> = array.into_iter();
5+
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
6+
|
7+
= note: `#[warn(array_into_iter)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145>
10+
11+
warning: 1 warning emitted
12+
13+
Future incompatibility report: Future breakage date: None, diagnostic:
14+
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.
15+
--> $DIR/into-iter-on-arrays-2018.rs:11:34
16+
|
17+
LL | let _: Iter<'_, i32> = array.into_iter();
18+
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
19+
|
20+
= note: `#[warn(array_into_iter)]` on by default
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145>
23+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// edition:2021
3+
// compile-flags: -Zunstable-options
4+
5+
use std::array::IntoIter;
6+
7+
fn main() {
8+
let array = [0; 10];
9+
10+
// In 2021, the method dispatches to `IntoIterator for [T; N]`.
11+
let _: IntoIter<i32, 10> = array.into_iter();
12+
13+
// And you can always use the trait method explicitly as an array.
14+
let _: IntoIter<i32, 10> = IntoIterator::into_iter(array);
15+
}

0 commit comments

Comments
 (0)