diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index ae16a8401552d..aa891fbba675c 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -218,6 +218,8 @@ mod iter; /// [`ThinBox`] implementation. mod thin; +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +pub use iter::BoxedArrayIntoIter; #[unstable(feature = "thin_box", issue = "92791")] pub use thin::ThinBox; diff --git a/library/alloc/src/boxed/iter.rs b/library/alloc/src/boxed/iter.rs index 90582aa49c6d7..0a470b5a40a61 100644 --- a/library/alloc/src/boxed/iter.rs +++ b/library/alloc/src/boxed/iter.rs @@ -1,18 +1,19 @@ use core::async_iter::AsyncIterator; -use core::iter::FusedIterator; +use core::iter::{FusedIterator, TrustedLen}; +use core::num::NonZero; use core::pin::Pin; use core::slice; use core::task::{Context, Poll}; -use crate::alloc::Allocator; +use crate::alloc::{Allocator, Global}; #[cfg(not(no_global_oom_handling))] use crate::borrow::Cow; use crate::boxed::Box; #[cfg(not(no_global_oom_handling))] use crate::string::String; -use crate::vec; #[cfg(not(no_global_oom_handling))] use crate::vec::Vec; +use crate::{fmt, vec}; #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Box { @@ -192,3 +193,154 @@ impl<'a> FromIterator> for Box { String::from_iter(iter).into_boxed_str() } } + +/// This implementation is required to make sure that the `Box<[I; N]>: IntoIterator` +/// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl !Iterator for Box<[I; N], A> {} + +/// This implementation is required to make sure that the `&Box<[I; N]>: IntoIterator` +/// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl<'a, const N: usize, I, A: Allocator> !Iterator for &'a Box<[I; N], A> {} + +/// This implementation is required to make sure that the `&mut Box<[I; N]>: IntoIterator` +/// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl<'a, const N: usize, I, A: Allocator> !Iterator for &'a mut Box<[I; N], A> {} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl<'a, T, const N: usize, A: Allocator> IntoIterator for &'a Box<[T; N], A> { + type IntoIter = slice::Iter<'a, T>; + type Item = &'a T; + fn into_iter(self) -> slice::Iter<'a, T> { + self.iter() + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl<'a, T, const N: usize, A: Allocator> IntoIterator for &'a mut Box<[T; N], A> { + type IntoIter = slice::IterMut<'a, T>; + type Item = &'a mut T; + fn into_iter(self) -> slice::IterMut<'a, T> { + self.iter_mut() + } +} + +/// A by-value `Box<[T; N]>` iterator. +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +#[rustc_insignificant_dtor] +pub struct BoxedArrayIntoIter { + // FIXME: make a more efficient implementation (without the need to store capacity) + inner: vec::IntoIter, +} + +impl BoxedArrayIntoIter { + /// Returns an immutable slice of all elements that have not been yielded + /// yet. + #[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] + pub fn as_slice(&self) -> &[T] { + self.inner.as_slice() + } + + /// Returns a mutable slice of all elements that have not been yielded yet. + #[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] + pub fn as_mut_slice(&mut self) -> &mut [T] { + self.inner.as_mut_slice() + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl Iterator for BoxedArrayIntoIter { + type Item = T; + fn next(&mut self) -> Option { + self.inner.next() + } + + fn size_hint(&self) -> (usize, Option) { + let len = self.len(); + (len, Some(len)) + } + + #[inline] + fn fold(self, init: Acc, fold: Fold) -> Acc + where + Fold: FnMut(Acc, Self::Item) -> Acc, + { + self.inner.fold(init, fold) + } + + fn count(self) -> usize { + self.len() + } + + fn last(mut self) -> Option { + self.next_back() + } + + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { + self.inner.advance_by(n) + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl DoubleEndedIterator for BoxedArrayIntoIter { + fn next_back(&mut self) -> Option { + self.inner.next_back() + } + + #[inline] + fn rfold(self, init: Acc, rfold: Fold) -> Acc + where + Fold: FnMut(Acc, Self::Item) -> Acc, + { + self.inner.rfold(init, rfold) + } + + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { + self.inner.advance_back_by(n) + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl ExactSizeIterator for BoxedArrayIntoIter { + fn len(&self) -> usize { + self.inner.len() + } + + fn is_empty(&self) -> bool { + self.inner.is_empty() + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl FusedIterator for BoxedArrayIntoIter {} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +unsafe impl TrustedLen for BoxedArrayIntoIter {} + +#[cfg(not(no_global_oom_handling))] +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl Clone for BoxedArrayIntoIter { + fn clone(&self) -> Self { + Self { inner: self.inner.clone() } + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl fmt::Debug for BoxedArrayIntoIter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Only print the elements that were not yielded yet: we cannot + // access the yielded elements anymore. + f.debug_tuple("IntoIter").field(&self.as_slice()).finish() + } +} + +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl IntoIterator for Box<[T; N], A> { + type IntoIter = BoxedArrayIntoIter; + type Item = T; + fn into_iter(self) -> BoxedArrayIntoIter { + BoxedArrayIntoIter { inner: (self as Box<[T], A>).into_vec().into_iter() } + } +} diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 4f67a2c04fefc..87cc03b5644f1 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -466,7 +466,7 @@ where #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] #[rustc_unsafe_specialization_marker] -pub trait NonDrop {} +trait NonDrop {} // T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr // and thus we can't implement drop-handling diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index cd2a9e00b5a6e..bbd3469d93238 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -32,6 +32,9 @@ impl IntoIter { } } +#[stable(feature = "boxed_array_value_iter", since = "CURRENT_RUSTC_VERSION")] +impl !Iterator for [T; N] {} + // Note: the `#[rustc_skip_during_method_dispatch(array)]` 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. @@ -362,7 +365,7 @@ unsafe impl TrustedLen for IntoIter {} #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] #[rustc_unsafe_specialization_marker] -pub trait NonDrop {} +trait NonDrop {} // T: Copy as approximation for !Drop since get_unchecked does not advance self.alive // and thus we can't implement drop-handling diff --git a/tests/ui/iterators/into-iter-on-arrays-2018.rs b/tests/ui/iterators/into-iter-on-arrays-2018.rs index d23544052362f..2999d085c8738 100644 --- a/tests/ui/iterators/into-iter-on-arrays-2018.rs +++ b/tests/ui/iterators/into-iter-on-arrays-2018.rs @@ -5,6 +5,7 @@ use std::array::IntoIter; use std::ops::Deref; use std::rc::Rc; use std::slice::Iter; +use std::boxed::BoxedArrayIntoIter; fn main() { let array = [0; 10]; @@ -15,9 +16,7 @@ fn main() { //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` //~| WARNING this changes meaning - let _: Iter<'_, i32> = Box::new(array).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning + let _: BoxedArrayIntoIter = Box::new(array).into_iter(); let _: Iter<'_, i32> = Rc::new(array).into_iter(); //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` diff --git a/tests/ui/iterators/into-iter-on-arrays-2018.stderr b/tests/ui/iterators/into-iter-on-arrays-2018.stderr index 6419d779b4f57..769ea451a4f14 100644 --- a/tests/ui/iterators/into-iter-on-arrays-2018.stderr +++ b/tests/ui/iterators/into-iter-on-arrays-2018.stderr @@ -1,5 +1,5 @@ warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-2018.rs:14:34 + --> $DIR/into-iter-on-arrays-2018.rs:15:34 | LL | let _: Iter<'_, i32> = array.into_iter(); | ^^^^^^^^^ @@ -19,16 +19,7 @@ LL + let _: Iter<'_, i32> = IntoIterator::into_iter(array); | warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-2018.rs:18:44 - | -LL | let _: Iter<'_, i32> = Box::new(array).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-2018.rs:22:43 + --> $DIR/into-iter-on-arrays-2018.rs:21:43 | LL | let _: Iter<'_, i32> = Rc::new(array).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -37,7 +28,7 @@ LL | let _: Iter<'_, i32> = Rc::new(array).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-2018.rs:25:41 + --> $DIR/into-iter-on-arrays-2018.rs:24:41 | LL | let _: Iter<'_, i32> = Array(array).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -46,7 +37,7 @@ LL | let _: Iter<'_, i32> = Array(array).into_iter(); = note: for more information, see warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-2018.rs:32:24 + --> $DIR/into-iter-on-arrays-2018.rs:31:24 | LL | for _ in [1, 2, 3].into_iter() {} | ^^^^^^^^^ @@ -64,5 +55,5 @@ LL - for _ in [1, 2, 3].into_iter() {} LL + for _ in [1, 2, 3] {} | -warning: 5 warnings emitted +warning: 4 warnings emitted diff --git a/tests/ui/iterators/into-iter-on-arrays-2021.rs b/tests/ui/iterators/into-iter-on-arrays-2021.rs index 1bda0ebf6cb8e..918cc4cf0801f 100644 --- a/tests/ui/iterators/into-iter-on-arrays-2021.rs +++ b/tests/ui/iterators/into-iter-on-arrays-2021.rs @@ -4,13 +4,14 @@ use std::array::IntoIter; use std::ops::Deref; use std::rc::Rc; +use std::boxed::BoxedArrayIntoIter; fn main() { let array = [0; 10]; // In 2021, the method dispatches to `IntoIterator for [T; N]`. let _: IntoIter = array.into_iter(); - let _: IntoIter = Box::new(array).into_iter(); + let _: BoxedArrayIntoIter = Box::new(array).into_iter(); // The `array_into_iter` lint doesn't cover other wrappers that deref to an array. let _: IntoIter = Rc::new(array).into_iter(); diff --git a/tests/ui/iterators/into-iter-on-arrays-lint.fixed b/tests/ui/iterators/into-iter-on-arrays-lint.fixed index 848b13750d77a..0c105a55871b4 100644 --- a/tests/ui/iterators/into-iter-on-arrays-lint.fixed +++ b/tests/ui/iterators/into-iter-on-arrays-lint.fixed @@ -22,31 +22,15 @@ fn main() { //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` //~| WARNING this changes meaning - Box::new(small).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new([1, 2]).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new(big).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new([0u8; 33]).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning + Box::new(small).into_iter(); + Box::new([1, 2]).into_iter(); + Box::new(big).into_iter(); + Box::new([0u8; 33]).into_iter(); - Box::new(Box::new(small)).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new(Box::new([1, 2])).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new(Box::new(big)).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning - Box::new(Box::new([0u8; 33])).iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning + Box::new(Box::new(small)).into_iter(); + Box::new(Box::new([1, 2])).into_iter(); + Box::new(Box::new(big)).into_iter(); + Box::new(Box::new([0u8; 33])).into_iter(); // Expressions that should not (&[1, 2]).into_iter(); diff --git a/tests/ui/iterators/into-iter-on-arrays-lint.rs b/tests/ui/iterators/into-iter-on-arrays-lint.rs index a505ba8313f56..6b9d840f138fd 100644 --- a/tests/ui/iterators/into-iter-on-arrays-lint.rs +++ b/tests/ui/iterators/into-iter-on-arrays-lint.rs @@ -23,30 +23,14 @@ fn main() { //~| WARNING this changes meaning Box::new(small).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new([1, 2]).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new(big).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new([0u8; 33]).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new(Box::new(small)).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new(Box::new([1, 2])).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new(Box::new(big)).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning Box::new(Box::new([0u8; 33])).into_iter(); - //~^ WARNING this method call resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this changes meaning // Expressions that should not (&[1, 2]).into_iter(); diff --git a/tests/ui/iterators/into-iter-on-arrays-lint.stderr b/tests/ui/iterators/into-iter-on-arrays-lint.stderr index b173eb01d22ec..44375d20717b8 100644 --- a/tests/ui/iterators/into-iter-on-arrays-lint.stderr +++ b/tests/ui/iterators/into-iter-on-arrays-lint.stderr @@ -75,77 +75,5 @@ LL - [0u8; 33].into_iter(); LL + IntoIterator::into_iter([0u8; 33]); | -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:25:21 - | -LL | Box::new(small).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:28:22 - | -LL | Box::new([1, 2]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:31:19 - | -LL | Box::new(big).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:34:25 - | -LL | Box::new([0u8; 33]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:38:31 - | -LL | Box::new(Box::new(small)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:41:32 - | -LL | Box::new(Box::new([1, 2])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:44:29 - | -LL | Box::new(Box::new(big)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021 - --> $DIR/into-iter-on-arrays-lint.rs:47:35 - | -LL | Box::new(Box::new([0u8; 33])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this changes meaning in Rust 2021 - = note: for more information, see - -warning: 12 warnings emitted +warning: 4 warnings emitted