Skip to content

Commit 6e7b8c8

Browse files
authored
Create ZST-nthback.rs
1 parent 8ef3419 commit 6e7b8c8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: -Zmiri-symbolic-alignment-check
2+
//@ run-pass
3+
4+
use std::ptr;
5+
6+
// This is a ZST (Zero-Sized Type) that requires high alignment (alignment 8).
7+
// The original bug occurred because IntoIter::nth_back tried to calculate an
8+
// unaligned pointer for drop_in_place based on this high-aligned ZST, leading to UB in Miri.
9+
#[repr(align(8))]
10+
struct Thing;
11+
12+
fn main() {
13+
// Create a Vec containing two high-aligned ZSTs.
14+
let v = vec![Thing, Thing];
15+
16+
// Calling IntoIter::nth_back(1) triggers the problematic drop_in_place logic
17+
// within Vec::IntoIter. The fix (using ptr::NonNull::dangling()) prevents the UB.
18+
let mut iter = v.into_iter();
19+
let _ = iter.nth_back(1);
20+
21+
// Also test nth_back(0) for completeness.
22+
let v2 = vec![Thing, Thing];
23+
let mut iter2 = v2.into_iter();
24+
let _ = iter2.nth_back(0);
25+
26+
// The main assertion is that the program runs to completion without Miri reporting UB.
27+
}

0 commit comments

Comments
 (0)