Skip to content

Commit 631e58c

Browse files
committed
Add a Miri test for 134713
1 parent 7ffb24c commit 631e58c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::mem::{size_of, align_of};
2+
3+
// See <https://github.com/rust-lang/rust/issues/134713>
4+
5+
#[repr(C)]
6+
struct Foo(usize, u8);
7+
8+
fn main() {
9+
let buf1: [usize; 2] = [1000, 2000];
10+
let buf2: [usize; 2] = [3000, 4000];
11+
12+
// Foo and [usize; 2] have the same size and alignment,
13+
// so swap_nonoverlapping should treat them the same
14+
assert_eq!(size_of::<Foo>(), size_of::<[usize; 2]>());
15+
assert_eq!(align_of::<Foo>(), align_of::<[usize; 2]>());
16+
17+
let mut b1 = buf1;
18+
let mut b2 = buf2;
19+
// Safety: b1 and b2 are distinct local variables,
20+
// with the same size and alignment as Foo.
21+
unsafe {
22+
std::ptr::swap_nonoverlapping(
23+
b1.as_mut_ptr().cast::<Foo>(),
24+
b2.as_mut_ptr().cast::<Foo>(),
25+
1,
26+
);
27+
}
28+
assert_eq!(b1, buf2);
29+
assert_eq!(b2, buf1);
30+
}

0 commit comments

Comments
 (0)