]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/copy-intrinsic.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / ui / consts / copy-intrinsic.rs
1 #![stable(feature = "dummy", since = "1.0.0")]
2
3 // ignore-tidy-linelength
4 #![feature(intrinsics, staged_api)]
5 #![feature(const_mut_refs, const_intrinsic_copy, const_ptr_offset)]
6 use std::mem;
7
8 extern "rust-intrinsic" {
9 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
10 fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
11
12 #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
13 fn copy<T>(src: *const T, dst: *mut T, count: usize);
14 }
15
16 const COPY_ZERO: () = unsafe {
17 // Since we are not copying anything, this should be allowed.
18 let src = ();
19 let mut dst = ();
20 copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
21 };
22
23 const COPY_OOB_1: () = unsafe {
24 let mut x = 0i32;
25 let dangle = (&mut x as *mut i32).wrapping_add(10);
26 // Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
27 copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ ERROR evaluation of constant value failed [E0080]
28 //~| pointer at offset 40 is out-of-bounds
29 };
30 const COPY_OOB_2: () = unsafe {
31 let x = 0i32;
32 let dangle = (&x as *const i32).wrapping_add(10);
33 // Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
34 copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ ERROR evaluation of constant value failed [E0080]
35 //~| pointer at offset 40 is out-of-bounds
36 };
37
38 const COPY_SIZE_OVERFLOW: () = unsafe {
39 let x = 0;
40 let mut y = 0;
41 copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR evaluation of constant value failed [E0080]
42 //~| overflow computing total size of `copy`
43 };
44 const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
45 let x = 0;
46 let mut y = 0;
47 copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ evaluation of constant value failed [E0080]
48 //~| overflow computing total size of `copy_nonoverlapping`
49 };
50
51 fn main() {
52 }