]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/copy-intrinsic.rs
9dc595f37faaeb1488026123baa62321909fec08
[rustc.git] / src / test / ui / consts / copy-intrinsic.rs
1 // ignore-tidy-linelength
2 #![feature(const_mut_refs, const_intrinsic_copy, const_ptr_offset)]
3 use std::{ptr, mem};
4
5 const COPY_ZERO: () = unsafe {
6 // Since we are not copying anything, this should be allowed.
7 let src = ();
8 let mut dst = ();
9 ptr::copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
10 };
11
12 const COPY_OOB_1: () = unsafe {
13 let mut x = 0i32;
14 let dangle = (&mut x as *mut i32).wrapping_add(10);
15 // Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
16 ptr::copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ ERROR any use of this value will cause an error
17 //~| memory access failed: pointer must be in-bounds
18 //~| previously accepted
19 };
20 const COPY_OOB_2: () = unsafe {
21 let x = 0i32;
22 let dangle = (&x as *const i32).wrapping_add(10);
23 // Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
24 ptr::copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ ERROR any use of this value will cause an error
25 //~| memory access failed: pointer must be in-bounds
26 //~| previously accepted
27 };
28
29 const COPY_SIZE_OVERFLOW: () = unsafe {
30 let x = 0;
31 let mut y = 0;
32 ptr::copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR any use of this value will cause an error
33 //~| overflow computing total size of `copy`
34 //~| previously accepted
35 };
36 const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
37 let x = 0;
38 let mut y = 0;
39 ptr::copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR any use of this value will cause an error
40 //~| overflow computing total size of `copy_nonoverlapping`
41 //~| previously accepted
42 };
43
44 fn main() {
45 }