diff options
| author | Ralf Jung <post@ralfj.de> | 2023-09-04 09:32:23 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-09-26 15:32:57 +0200 |
| commit | bd33846253b77ec876e130d8e010c221c80182e0 (patch) | |
| tree | a0237ad03e0cdfa80ee805c84bb660e481aa015b /tests/ui/consts/const-eval/raw-pointer-ub.rs | |
| parent | a993a8bf3f9221c84120a6f111fd1b64a5f19ea4 (diff) | |
| download | rust-bd33846253b77ec876e130d8e010c221c80182e0.tar.gz rust-bd33846253b77ec876e130d8e010c221c80182e0.zip | |
add misalignment const-eval test
and some other raw pointer shenanigans while we are at it
Diffstat (limited to 'tests/ui/consts/const-eval/raw-pointer-ub.rs')
| -rw-r--r-- | tests/ui/consts/const-eval/raw-pointer-ub.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/ui/consts/const-eval/raw-pointer-ub.rs b/tests/ui/consts/const-eval/raw-pointer-ub.rs new file mode 100644 index 00000000000..e53865309eb --- /dev/null +++ b/tests/ui/consts/const-eval/raw-pointer-ub.rs @@ -0,0 +1,36 @@ +// normalize-stderr-test "alloc\d+" -> "allocN" +#![feature(const_pointer_byte_offsets)] +#![feature(pointer_byte_offsets)] +#![feature(const_mut_refs)] + +const MISALIGNED_LOAD: () = unsafe { + let mem = [0u32; 8]; + let ptr = mem.as_ptr().byte_add(1); + let _val = *ptr; //~ERROR: evaluation of constant value failed + //~^NOTE: accessing memory with alignment 1, but alignment 4 is required +}; + +const MISALIGNED_STORE: () = unsafe { + let mut mem = [0u32; 8]; + let ptr = mem.as_mut_ptr().byte_add(1); + *ptr = 0; //~ERROR: evaluation of constant value failed + //~^NOTE: accessing memory with alignment 1, but alignment 4 is required +}; + +const MISALIGNED_COPY: () = unsafe { + let x = &[0_u8; 4]; + let y = x.as_ptr().cast::<u32>(); + let mut z = 123; + y.copy_to_nonoverlapping(&mut z, 1); + //~^NOTE + // The actual error points into the implementation of `copy_to_nonoverlapping`. +}; + +const OOB: () = unsafe { + let mem = [0u32; 1]; + let ptr = mem.as_ptr().cast::<u64>(); + let _val = *ptr; //~ERROR: evaluation of constant value failed + //~^NOTE: size 4, so pointer to 8 bytes starting at offset 0 is out-of-bounds +}; + +fn main() {} |
