Skip to content

add lib tests for vec::IntoIter alignment issues #106112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2022
Merged
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
29 changes: 27 additions & 2 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::borrow::Cow;
use std::cell::Cell;
use std::collections::TryReserveErrorKind::*;
use std::fmt::Debug;
use std::hint;
use std::iter::InPlaceIterable;
use std::mem;
use std::mem::{size_of, swap};
use std::ops::Bound::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
Expand Down Expand Up @@ -1107,8 +1109,31 @@ fn test_into_iter_drop_allocator() {

#[test]
fn test_into_iter_zst() {
for _ in vec![[0u64; 0]].into_iter() {}
for _ in vec![[0u64; 0]; 5].into_iter().rev() {}
#[derive(Debug, Clone)]
struct AlignedZstWithDrop([u64; 0]);
impl Drop for AlignedZstWithDrop {
fn drop(&mut self) {
let addr = self as *mut _ as usize;
assert!(hint::black_box(addr) % mem::align_of::<u64>() == 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable enough. Good thing we didn't call it hint::bench_black_box :p

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess it is useful for some opportunistic UB-detection tests as well. ;)

}
}

const C: AlignedZstWithDrop = AlignedZstWithDrop([0u64; 0]);

for _ in vec![C].into_iter() {}
for _ in vec![C; 5].into_iter().rev() {}

let mut it = vec![C, C].into_iter();
it.advance_by(1).unwrap();
drop(it);

let mut it = vec![C, C].into_iter();
it.next_chunk::<1>().unwrap();
drop(it);

let mut it = vec![C, C].into_iter();
it.next_chunk::<4>().unwrap_err();
drop(it);
}

#[test]
Expand Down