Skip to content

Commit 1597728

Browse files
committed
Auto merge of #88611 - m-ou-se:array-into-iter-new-deprecate, r=joshtriplett
Deprecate array::IntoIter::new.
2 parents cafc458 + 27d3935 commit 1597728

File tree

25 files changed

+69
-99
lines changed

25 files changed

+69
-99
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ use smallvec::SmallVec;
7070
use tracing::{debug, trace};
7171

7272
macro_rules! arena_vec {
73-
($this:expr; $($x:expr),*) => ({
74-
let a = [$($x),*];
75-
$this.arena.alloc_from_iter(std::array::IntoIter::new(a))
76-
});
73+
($this:expr; $($x:expr),*) => (
74+
$this.arena.alloc_from_iter([$($x),*])
75+
);
7776
}
7877

7978
mod asm;

compiler/rustc_codegen_cranelift/scripts/cargo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() {
4242
"RUSTFLAGS",
4343
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
4444
);
45-
std::array::IntoIter::new(["rustc".to_string()])
45+
IntoIterator::into_iter(["rustc".to_string()])
4646
.chain(env::args().skip(2))
4747
.chain([
4848
"--".to_string(),
@@ -56,7 +56,7 @@ fn main() {
5656
"RUSTFLAGS",
5757
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
5858
);
59-
std::array::IntoIter::new(["rustc".to_string()])
59+
IntoIterator::into_iter(["rustc".to_string()])
6060
.chain(env::args().skip(2))
6161
.chain([
6262
"--".to_string(),

compiler/rustc_expand/src/proc_macro_server.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,12 @@ impl server::TokenStream for Rustc<'_, '_> {
466466
ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind {
467467
ast::ExprKind::Lit(l) => match l.token {
468468
token::Lit { kind: token::Integer | token::Float, .. } => {
469-
Ok(std::array::IntoIter::new([
469+
Ok(Self::TokenStream::from_iter([
470470
// FIXME: The span of the `-` token is lost when
471471
// parsing, so we cannot faithfully recover it here.
472472
tokenstream::TokenTree::token(token::BinOp(token::Minus), e.span),
473473
tokenstream::TokenTree::token(token::Literal(l.token), l.span),
474-
])
475-
.collect())
474+
]))
476475
}
477476
_ => Err(()),
478477
},

compiler/rustc_hir/src/def.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ impl<T> PerNS<T> {
443443
}
444444

445445
pub fn into_iter(self) -> IntoIter<T, 3> {
446-
IntoIter::new([self.value_ns, self.type_ns, self.macro_ns])
446+
[self.value_ns, self.type_ns, self.macro_ns].into_iter()
447447
}
448448

449449
pub fn iter(&self) -> IntoIter<&T, 3> {
450-
IntoIter::new([&self.value_ns, &self.type_ns, &self.macro_ns])
450+
[&self.value_ns, &self.type_ns, &self.macro_ns].into_iter()
451451
}
452452
}
453453

@@ -481,7 +481,7 @@ impl<T> PerNS<Option<T>> {
481481

482482
/// Returns an iterator over the items which are `Some`.
483483
pub fn present_items(self) -> impl Iterator<Item = T> {
484-
IntoIter::new([self.type_ns, self.value_ns, self.macro_ns]).flatten()
484+
[self.type_ns, self.value_ns, self.macro_ns].into_iter().flatten()
485485
}
486486
}
487487

compiler/rustc_session/src/filesearch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub use self::FileMatch::*;
44

55
use std::env;
66
use std::fs;
7+
use std::iter::FromIterator;
78
use std::path::{Path, PathBuf};
89

910
use crate::search_paths::{PathKind, SearchPath, SearchPathFile};
@@ -91,8 +92,7 @@ impl<'a> FileSearch<'a> {
9192

9293
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
9394
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
94-
std::array::IntoIter::new([sysroot, Path::new(&rustlib_path), Path::new("lib")])
95-
.collect::<PathBuf>()
95+
PathBuf::from_iter([sysroot, Path::new(&rustlib_path), Path::new("lib")])
9696
}
9797

9898
/// This function checks if sysroot is found using env::args().next(), and if it

compiler/rustc_session/src/session.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,11 @@ impl Session {
792792
/// Returns a list of directories where target-specific tool binaries are located.
793793
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
794794
let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, &config::host_triple());
795-
let p = std::array::IntoIter::new([
795+
let p = PathBuf::from_iter([
796796
Path::new(&self.sysroot),
797797
Path::new(&rustlib_path),
798798
Path::new("bin"),
799-
])
800-
.collect::<PathBuf>();
799+
]);
801800
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
802801
}
803802

compiler/rustc_target/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![feature(min_specialization)]
1717
#![feature(step_trait)]
1818

19+
use std::iter::FromIterator;
1920
use std::path::{Path, PathBuf};
2021

2122
#[macro_use]
@@ -47,12 +48,11 @@ const RUST_LIB_DIR: &str = "rustlib";
4748
/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
4849
pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
4950
let libdir = find_libdir(sysroot);
50-
std::array::IntoIter::new([
51+
PathBuf::from_iter([
5152
Path::new(libdir.as_ref()),
5253
Path::new(RUST_LIB_DIR),
5354
Path::new(target_triple),
5455
])
55-
.collect::<PathBuf>()
5656
}
5757

5858
/// The name of the directory rustc expects libraries to be located.

compiler/rustc_target/src/spec/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use rustc_serialize::json::{Json, ToJson};
4242
use rustc_span::symbol::{sym, Symbol};
4343
use std::collections::BTreeMap;
4444
use std::convert::TryFrom;
45+
use std::iter::FromIterator;
4546
use std::ops::{Deref, DerefMut};
4647
use std::path::{Path, PathBuf};
4748
use std::str::FromStr;
@@ -2173,12 +2174,11 @@ impl Target {
21732174
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
21742175
// as a fallback.
21752176
let rustlib_path = crate::target_rustlib_path(&sysroot, &target_triple);
2176-
let p = std::array::IntoIter::new([
2177+
let p = PathBuf::from_iter([
21772178
Path::new(sysroot),
21782179
Path::new(&rustlib_path),
21792180
Path::new("target.json"),
2180-
])
2181-
.collect::<PathBuf>();
2181+
]);
21822182
if p.is_file() {
21832183
return load_file(&p);
21842184
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc_span::symbol::Symbol;
2525
use rustc_span::{MultiSpan, Span};
2626
use smallvec::SmallVec;
2727

28-
use std::array;
2928
use std::iter;
3029
use std::ops::ControlFlow;
3130

@@ -692,11 +691,8 @@ fn receiver_is_dispatchable<'tcx>(
692691
.to_predicate(tcx)
693692
};
694693

695-
let caller_bounds: Vec<Predicate<'tcx>> = param_env
696-
.caller_bounds()
697-
.iter()
698-
.chain(array::IntoIter::new([unsize_predicate, trait_predicate]))
699-
.collect();
694+
let caller_bounds: Vec<Predicate<'tcx>> =
695+
param_env.caller_bounds().iter().chain([unsize_predicate, trait_predicate]).collect();
700696

701697
ty::ParamEnv::new(tcx.intern_predicates(&caller_bounds), param_env.reveal())
702698
};

compiler/rustc_typeck/src/astconv/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use rustc_trait_selection::traits::error_reporting::report_object_safety_error;
3535
use rustc_trait_selection::traits::wf::object_region_bounds;
3636

3737
use smallvec::SmallVec;
38-
use std::array;
3938
use std::collections::BTreeSet;
4039
use std::slice;
4140

@@ -1635,7 +1634,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16351634
debug!("one_bound_for_assoc_type: bound2 = {:?}", bound2);
16361635

16371636
let is_equality = is_equality();
1638-
let bounds = array::IntoIter::new([bound, bound2]).chain(matching_candidates);
1637+
let bounds = IntoIterator::into_iter([bound, bound2]).chain(matching_candidates);
16391638
let mut err = if is_equality.is_some() {
16401639
// More specific Error Index entry.
16411640
struct_span_err!(

compiler/rustc_typeck/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18221822
// Inherent impl: take implied bounds from the `self` type.
18231823
let self_ty = self.tcx.type_of(impl_def_id);
18241824
let self_ty = self.normalize_associated_types_in(span, self_ty);
1825-
std::array::IntoIter::new([self_ty]).collect()
1825+
FxHashSet::from_iter([self_ty])
18261826
}
18271827
}
18281828
}

library/alloc/src/collections/binary_heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
15001500
/// }
15011501
/// ```
15021502
fn from(arr: [T; N]) -> Self {
1503-
core::array::IntoIter::new(arr).collect()
1503+
Self::from_iter(arr)
15041504
}
15051505
}
15061506

library/alloc/src/collections/btree/map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1305,11 +1305,11 @@ impl<K, V> BTreeMap<K, V> {
13051305
pub(crate) fn bulk_build_from_sorted_iter<I>(iter: I) -> Self
13061306
where
13071307
K: Ord,
1308-
I: Iterator<Item = (K, V)>,
1308+
I: IntoIterator<Item = (K, V)>,
13091309
{
13101310
let mut root = Root::new();
13111311
let mut length = 0;
1312-
root.bulk_push(DedupSortedIter::new(iter), &mut length);
1312+
root.bulk_push(DedupSortedIter::new(iter.into_iter()), &mut length);
13131313
BTreeMap { root: Some(root), length }
13141314
}
13151315
}
@@ -1944,7 +1944,7 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
19441944

19451945
// use stable sort to preserve the insertion order.
19461946
inputs.sort_by(|a, b| a.0.cmp(&b.0));
1947-
BTreeMap::bulk_build_from_sorted_iter(inputs.into_iter())
1947+
BTreeMap::bulk_build_from_sorted_iter(inputs)
19481948
}
19491949
}
19501950

@@ -2061,7 +2061,7 @@ impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
20612061

20622062
// use stable sort to preserve the insertion order.
20632063
arr.sort_by(|a, b| a.0.cmp(&b.0));
2064-
BTreeMap::bulk_build_from_sorted_iter(core::array::IntoIter::new(arr))
2064+
BTreeMap::bulk_build_from_sorted_iter(arr)
20652065
}
20662066
}
20672067

library/alloc/src/collections/btree/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
11061106

11071107
// use stable sort to preserve the insertion order.
11081108
arr.sort();
1109-
let iter = core::array::IntoIter::new(arr).map(|k| (k, ()));
1109+
let iter = IntoIterator::into_iter(arr).map(|k| (k, ()));
11101110
let map = BTreeMap::bulk_build_from_sorted_iter(iter);
11111111
BTreeSet { map }
11121112
}

library/alloc/src/collections/linked_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ impl<T, const N: usize> From<[T; N]> for LinkedList<T> {
19611961
/// assert_eq!(list1, list2);
19621962
/// ```
19631963
fn from(arr: [T; N]) -> Self {
1964-
core::array::IntoIter::new(arr).collect()
1964+
Self::from_iter(arr)
19651965
}
19661966
}
19671967

library/core/src/array/iter.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,23 @@ pub struct IntoIter<T, const N: usize> {
3434
alive: Range<usize>,
3535
}
3636

37-
impl<T, const N: usize> IntoIter<T, N> {
38-
/// Creates a new iterator over the given `array`.
39-
///
40-
/// *Note*: this method might be deprecated in the future,
41-
/// since [`IntoIterator`] is now implemented for arrays.
42-
///
43-
/// # Examples
44-
///
45-
/// ```
46-
/// use std::array;
37+
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
38+
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
39+
// so those calls will still resolve to the slice implementation, by reference.
40+
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
41+
impl<T, const N: usize> IntoIterator for [T; N] {
42+
type Item = T;
43+
type IntoIter = IntoIter<T, N>;
44+
45+
/// Creates a consuming iterator, that is, one that moves each value out of
46+
/// the array (from start to end). The array cannot be used after calling
47+
/// this unless `T` implements `Copy`, so the whole array is copied.
4748
///
48-
/// for value in array::IntoIter::new([1, 2, 3, 4, 5]) {
49-
/// // The type of `value` is an `i32` here, instead of `&i32`
50-
/// let _: i32 = value;
51-
/// }
49+
/// Arrays have special behavior when calling `.into_iter()` prior to the
50+
/// 2021 edition -- see the [array] Editions section for more information.
5251
///
53-
/// // Since Rust 1.53, arrays implement IntoIterator directly:
54-
/// for value in [1, 2, 3, 4, 5] {
55-
/// // The type of `value` is an `i32` here, instead of `&i32`
56-
/// let _: i32 = value;
57-
/// }
58-
/// ```
59-
#[stable(feature = "array_value_iter", since = "1.51.0")]
60-
pub fn new(array: [T; N]) -> Self {
52+
/// [array]: prim@array
53+
fn into_iter(self) -> Self::IntoIter {
6154
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
6255
// promise:
6356
//
@@ -76,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> {
7669
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
7770
// as a different type, then forget `array` so that it is not dropped.
7871
unsafe {
79-
let iter = Self { data: mem::transmute_copy(&array), alive: 0..N };
80-
mem::forget(array);
72+
let iter = IntoIter { data: mem::transmute_copy(&self), alive: 0..N };
73+
mem::forget(self);
8174
iter
8275
}
8376
}
77+
}
78+
79+
impl<T, const N: usize> IntoIter<T, N> {
80+
/// Creates a new iterator over the given `array`.
81+
#[stable(feature = "array_value_iter", since = "1.51.0")]
82+
#[rustc_deprecated(since = "1.59.0", reason = "use `IntoIterator::into_iter` instead")]
83+
pub fn new(array: [T; N]) -> Self {
84+
IntoIterator::into_iter(array)
85+
}
8486

8587
/// Returns an immutable slice of all elements that have not been yielded
8688
/// yet.

library/core/src/array/mod.rs

-21
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,6 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
243243
}
244244
}
245245

246-
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
247-
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
248-
// so those calls will still resolve to the slice implementation, by reference.
249-
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
250-
impl<T, const N: usize> IntoIterator for [T; N] {
251-
type Item = T;
252-
type IntoIter = IntoIter<T, N>;
253-
254-
/// Creates a consuming iterator, that is, one that moves each value out of
255-
/// the array (from start to end). The array cannot be used after calling
256-
/// this unless `T` implements `Copy`, so the whole array is copied.
257-
///
258-
/// Arrays have special behavior when calling `.into_iter()` prior to the
259-
/// 2021 edition -- see the [array] Editions section for more information.
260-
///
261-
/// [array]: prim@array
262-
fn into_iter(self) -> Self::IntoIter {
263-
IntoIter::new(self)
264-
}
265-
}
266-
267246
#[stable(feature = "rust1", since = "1.0.0")]
268247
impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
269248
type Item = &'a T;

library/core/src/primitive_docs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,7 @@ mod prim_pointer {}
606606
/// println!("array[{}] = {}", i, x);
607607
/// }
608608
///
609-
/// // You can explicitly iterate an array by value using
610-
/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`:
609+
/// // You can explicitly iterate an array by value using `IntoIterator::into_iter`
611610
/// for item in IntoIterator::into_iter(array).enumerate() {
612611
/// let (i, x): (usize, i32) = item;
613612
/// println!("array[{}] = {}", i, x);

0 commit comments

Comments
 (0)