Skip to content

Commit a1f68b5

Browse files
committed
Remove unsound TrustedRandomAccess implementations
Removes the implementations that depend on the user-definable trait `Copy`.
1 parent d08a471 commit a1f68b5

File tree

3 files changed

+3
-84
lines changed

3 files changed

+3
-84
lines changed

library/alloc/src/collections/vec_deque/into_iter.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt;
2-
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
2+
use core::iter::{FusedIterator, TrustedLen};
33

44
use super::VecDeque;
55

@@ -36,23 +36,6 @@ impl<T> Iterator for IntoIter<T> {
3636
let len = self.inner.len();
3737
(len, Some(len))
3838
}
39-
40-
#[inline]
41-
#[doc(hidden)]
42-
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
43-
where
44-
Self: TrustedRandomAccess,
45-
{
46-
// Safety: The TrustedRandomAccess contract requires that callers only pass an index
47-
// that is in bounds.
48-
// Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even
49-
// multiple repeated reads of the same index would be safe and the
50-
// values are !Drop, thus won't suffer from double drops.
51-
unsafe {
52-
let idx = self.inner.wrap_add(self.inner.tail, idx);
53-
self.inner.buffer_read(idx)
54-
}
55-
}
5639
}
5740

5841
#[stable(feature = "rust1", since = "1.0.0")]
@@ -75,14 +58,3 @@ impl<T> FusedIterator for IntoIter<T> {}
7558

7659
#[unstable(feature = "trusted_len", issue = "37572")]
7760
unsafe impl<T> TrustedLen for IntoIter<T> {}
78-
79-
#[doc(hidden)]
80-
#[unstable(feature = "trusted_random_access", issue = "none")]
81-
// T: Copy as approximation for !Drop since get_unchecked does not update the pointers
82-
// and thus we can't implement drop-handling
83-
unsafe impl<T> TrustedRandomAccess for IntoIter<T>
84-
where
85-
T: Copy,
86-
{
87-
const MAY_HAVE_SIDE_EFFECT: bool = false;
88-
}

library/alloc/src/vec/into_iter.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::alloc::{Allocator, Global};
22
use crate::raw_vec::RawVec;
33
use core::fmt;
44
use core::intrinsics::arith_offset;
5-
use core::iter::{FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccess};
5+
use core::iter::{FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
66
use core::marker::PhantomData;
77
use core::mem::{self};
88
use core::ptr::{self, NonNull};
@@ -162,24 +162,6 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
162162
fn count(self) -> usize {
163163
self.len()
164164
}
165-
166-
#[doc(hidden)]
167-
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> Self::Item
168-
where
169-
Self: TrustedRandomAccess,
170-
{
171-
// SAFETY: the caller must guarantee that `i` is in bounds of the
172-
// `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
173-
// is guaranteed to pointer to an element of the `Vec<T>` and
174-
// thus guaranteed to be valid to dereference.
175-
//
176-
// Also note the implementation of `Self: TrustedRandomAccess` requires
177-
// that `T: Copy` so reading elements from the buffer doesn't invalidate
178-
// them for `Drop`.
179-
unsafe {
180-
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
181-
}
182-
}
183165
}
184166

185167
#[stable(feature = "rust1", since = "1.0.0")]
@@ -215,17 +197,6 @@ impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
215197
#[unstable(feature = "trusted_len", issue = "37572")]
216198
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
217199

218-
#[doc(hidden)]
219-
#[unstable(issue = "none", feature = "std_internals")]
220-
// T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr
221-
// and thus we can't implement drop-handling
222-
unsafe impl<T, A: Allocator> TrustedRandomAccess for IntoIter<T, A>
223-
where
224-
T: Copy,
225-
{
226-
const MAY_HAVE_SIDE_EFFECT: bool = false;
227-
}
228-
229200
#[cfg(not(no_global_oom_handling))]
230201
#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
231202
impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> {

library/core/src/array/iter.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
fmt,
5-
iter::{self, ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess},
5+
iter::{self, ExactSizeIterator, FusedIterator, TrustedLen},
66
mem::{self, MaybeUninit},
77
ops::Range,
88
ptr,
@@ -130,19 +130,6 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
130130
fn last(mut self) -> Option<Self::Item> {
131131
self.next_back()
132132
}
133-
134-
#[inline]
135-
#[doc(hidden)]
136-
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
137-
where
138-
Self: TrustedRandomAccess,
139-
{
140-
// SAFETY: Callers are only allowed to pass an index that is in bounds
141-
// Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even
142-
// multiple repeated reads of the same index would be safe and the
143-
// values are !Drop, thus won't suffer from double drops.
144-
unsafe { self.data.get_unchecked(self.alive.start + idx).assume_init_read() }
145-
}
146133
}
147134

148135
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
@@ -197,17 +184,6 @@ impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
197184
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
198185
unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {}
199186

200-
#[doc(hidden)]
201-
#[unstable(feature = "trusted_random_access", issue = "none")]
202-
// T: Copy as approximation for !Drop since get_unchecked does not update the pointers
203-
// and thus we can't implement drop-handling
204-
unsafe impl<T, const N: usize> TrustedRandomAccess for IntoIter<T, N>
205-
where
206-
T: Copy,
207-
{
208-
const MAY_HAVE_SIDE_EFFECT: bool = false;
209-
}
210-
211187
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
212188
impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
213189
fn clone(&self) -> Self {

0 commit comments

Comments
 (0)