Skip to content

Commit 7d659c9

Browse files
Adjust docs and tests for new IntoIterator impl for arrays
1 parent 30c4a25 commit 7d659c9

9 files changed

+16
-191
lines changed

src/libstd/primitive_docs.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ mod prim_pointer { }
464464
/// the element type allows it:
465465
///
466466
/// - [`Debug`][debug]
467-
/// - [`IntoIterator`][intoiterator] (implemented for `&[T; N]` and `&mut [T; N]`)
467+
/// - [`IntoIterator`][intoiterator] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`)
468468
/// - [`PartialEq`][partialeq], [`PartialOrd`][partialord], [`Eq`][eq], [`Ord`][ord]
469469
/// - [`Hash`][hash]
470470
/// - [`AsRef`][asref], [`AsMut`][asmut]
@@ -500,32 +500,16 @@ mod prim_pointer { }
500500
/// assert_eq!([1, 2], &array[1..]);
501501
///
502502
/// // This loop prints: 0 1 2
503-
/// for x in &array {
503+
/// for x in array {
504504
/// print!("{} ", x);
505505
/// }
506506
/// ```
507507
///
508-
/// An array itself is not iterable:
509-
///
510-
/// ```compile_fail,E0277
511-
/// let array: [i32; 3] = [0; 3];
512-
///
513-
/// for x in array { }
514-
/// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied
515-
/// ```
516-
///
517-
/// The solution is to coerce the array to a slice by calling a slice method:
508+
/// You can also iterate over reference to the array's elements:
518509
///
519510
/// ```
520-
/// # let array: [i32; 3] = [0; 3];
521-
/// for x in array.iter() { }
522-
/// ```
523-
///
524-
/// If the array has 32 or fewer elements (see above), you can also use the
525-
/// array reference's [`IntoIterator`] implementation:
511+
/// let array: [i32; 3] = [0; 3];
526512
///
527-
/// ```
528-
/// # let array: [i32; 3] = [0; 3];
529513
/// for x in &array { }
530514
/// ```
531515
///

src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ LL | for _ in &[0_usize; 33] {
4646
<&'a [T] as std::iter::IntoIterator>
4747
<&'a mut [T; _] as std::iter::IntoIterator>
4848
<&'a mut [T] as std::iter::IntoIterator>
49+
<[T; _] as std::iter::IntoIterator>
4950
= note: required by `std::iter::IntoIterator::into_iter`
5051

5152
error: aborting due to 5 previous errors
+2-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1+
// check-pass
2+
13
fn main() {
24
for _ in [0..1] {}
3-
//~^ ERROR is not an iterator
45
for _ in [0..=1] {}
5-
//~^ ERROR is not an iterator
66
for _ in [0..] {}
7-
//~^ ERROR is not an iterator
87
for _ in [..1] {}
9-
//~^ ERROR is not an iterator
108
for _ in [..=1] {}
11-
//~^ ERROR is not an iterator
129
let start = 0;
1310
let end = 0;
1411
for _ in [start..end] {}
15-
//~^ ERROR is not an iterator
1612
let array_of_range = [start..end];
1713
for _ in array_of_range {}
18-
//~^ ERROR is not an iterator
1914
for _ in [0..1, 2..3] {}
20-
//~^ ERROR is not an iterator
2115
for _ in [0..=1] {}
22-
//~^ ERROR is not an iterator
2316
}

src/test/ui/iterators/array-of-ranges.stderr

-93
This file was deleted.

src/test/ui/iterators/array.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
// check-pass
2+
13
fn main() {
24
for _ in [1, 2] {}
3-
//~^ ERROR is not an iterator
45
let x = [1, 2];
56
for _ in x {}
6-
//~^ ERROR is not an iterator
77
for _ in [1.0, 2.0] {}
8-
//~^ ERROR is not an iterator
98
}

src/test/ui/iterators/array.stderr

-33
This file was deleted.

src/test/ui/iterators/into-iter-on-arrays-lint.fixed

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ fn main() {
66
let big = [0u8; 33];
77

88
// Expressions that should trigger the lint
9-
small.iter();
10-
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter`
11-
//~| WARNING this was previously accepted by the compiler but is being phased out
12-
[1, 2].iter();
13-
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter`
14-
//~| WARNING this was previously accepted by the compiler but is being phased out
159
big.iter();
1610
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter`
1711
//~| WARNING this was previously accepted by the compiler but is being phased out
@@ -21,6 +15,8 @@ fn main() {
2115

2216

2317
// Expressions that should not
18+
small.into_iter();
19+
[1, 2].into_iter();
2420
(&[1, 2]).into_iter();
2521
(&small).into_iter();
2622
(&[0u8; 33]).into_iter();

src/test/ui/iterators/into-iter-on-arrays-lint.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ fn main() {
66
let big = [0u8; 33];
77

88
// Expressions that should trigger the lint
9-
small.into_iter();
10-
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter`
11-
//~| WARNING this was previously accepted by the compiler but is being phased out
12-
[1, 2].into_iter();
13-
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter`
14-
//~| WARNING this was previously accepted by the compiler but is being phased out
159
big.into_iter();
1610
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter`
1711
//~| WARNING this was previously accepted by the compiler but is being phased out
@@ -21,6 +15,8 @@ fn main() {
2115

2216

2317
// Expressions that should not
18+
small.into_iter();
19+
[1, 2].into_iter();
2420
(&[1, 2]).into_iter();
2521
(&small).into_iter();
2622
(&[0u8; 33]).into_iter();

src/test/ui/iterators/into-iter-on-arrays-lint.stderr

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
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-lint.rs:9:11
3-
|
4-
LL | small.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: 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.
12-
--> $DIR/into-iter-on-arrays-lint.rs:12:12
13-
|
14-
LL | [1, 2].into_iter();
15-
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
16-
|
17-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
18-
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145>
19-
201
warning: this method call currently resolves to `<&[T] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
21-
--> $DIR/into-iter-on-arrays-lint.rs:15:9
2+
--> $DIR/into-iter-on-arrays-lint.rs:9:9
223
|
234
LL | big.into_iter();
245
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
256
|
7+
= note: `#[warn(array_into_iter)]` on by default
268
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
279
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145>
2810

2911
warning: this method call currently resolves to `<&[T] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
30-
--> $DIR/into-iter-on-arrays-lint.rs:18:15
12+
--> $DIR/into-iter-on-arrays-lint.rs:12:15
3113
|
3214
LL | [0u8; 33].into_iter();
3315
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`

0 commit comments

Comments
 (0)