diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-02-22 13:12:38 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-04-09 09:09:37 -0700 |
| commit | b06a88ff61acb08dd4f7502908485a3eaa74d0a6 (patch) | |
| tree | 4f1991bc605ef0ea905ce009d54ae6d37671a640 /src | |
| parent | 50d0ce1b42d67ce98b2ccac55d22a2ff8abe3273 (diff) | |
Add a Miri test for 134713
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/miri/tests/pass/issues/issue-134713-swap_nonoverlapping_untyped.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/tools/miri/tests/pass/issues/issue-134713-swap_nonoverlapping_untyped.rs b/src/tools/miri/tests/pass/issues/issue-134713-swap_nonoverlapping_untyped.rs new file mode 100644 index 00000000000..a1da60ce65c --- /dev/null +++ b/src/tools/miri/tests/pass/issues/issue-134713-swap_nonoverlapping_untyped.rs @@ -0,0 +1,30 @@ +use std::mem::{size_of, align_of}; + +// See <https://github.com/rust-lang/rust/issues/134713> + +#[repr(C)] +struct Foo(usize, u8); + +fn main() { + let buf1: [usize; 2] = [1000, 2000]; + let buf2: [usize; 2] = [3000, 4000]; + + // Foo and [usize; 2] have the same size and alignment, + // so swap_nonoverlapping should treat them the same + assert_eq!(size_of::<Foo>(), size_of::<[usize; 2]>()); + assert_eq!(align_of::<Foo>(), align_of::<[usize; 2]>()); + + let mut b1 = buf1; + let mut b2 = buf2; + // Safety: b1 and b2 are distinct local variables, + // with the same size and alignment as Foo. + unsafe { + std::ptr::swap_nonoverlapping( + b1.as_mut_ptr().cast::<Foo>(), + b2.as_mut_ptr().cast::<Foo>(), + 1, + ); + } + assert_eq!(b1, buf2); + assert_eq!(b2, buf1); +} |
