Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,19 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
// SAFETY: same as for advance_by()
self.end = unsafe { self.end.sub(step_size) };
}
let to_drop = ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size);
let to_drop = if T::IS_ZST {
//ZST may cause unalignment
ptr::slice_from_raw_parts_mut(ptr::NonNull::<T>::dangling().as_ptr(), step_size)
} else {
ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size)
};
// SAFETY: same as for advance_by()
unsafe {
ptr::drop_in_place(to_drop);
}
NonZero::new(n - step_size).map_or(Ok(()), Err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
fn is_empty(&self) -> bool {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/iterators/ZST-nthback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ check-pass
// test Intolter::nth_back does not cause UB for ZSTs with high alignment

#[repr(align(8))]
struct Thing;

fn main() {
let v = vec![Thing, Thing];
let _ = v.into_iter().nth_back(1);
}
Loading