]> git.lizzy.rs Git - rust.git/commitdiff
add lib tests for vec::IntoIter alignment issues
authorRalf Jung <post@ralfj.de>
Sat, 24 Dec 2022 08:09:34 +0000 (09:09 +0100)
committerRalf Jung <post@ralfj.de>
Sat, 24 Dec 2022 09:08:27 +0000 (10:08 +0100)
library/alloc/tests/vec.rs

index 7ebed0d5ca6994085d63c3ce2631e9c6d3cbd85d..87adcead8f62d69ba7dbcdcb4c5f331aa276b414 100644 (file)
@@ -7,7 +7,9 @@
 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};
@@ -1107,8 +1109,31 @@ unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
 
 #[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);
+        }
+    }
+
+    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]