]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/offset_from_ub.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / test / ui / consts / offset_from_ub.rs
1 // ignore-x86 FIXME: missing sysroot spans (#53081)
2
3 #![feature(const_raw_ptr_deref)]
4 #![feature(const_ptr_offset_from)]
5 #![feature(ptr_offset_from)]
6
7 #[repr(C)]
8 struct Struct {
9 data: u8,
10 field: u8,
11 }
12
13 pub const DIFFERENT_ALLOC: usize = {
14 //~^ NOTE
15 let uninit = std::mem::MaybeUninit::<Struct>::uninit();
16 let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
17 let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
18 let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
19 let offset = unsafe { field_ptr.offset_from(base_ptr) };
20 offset as usize
21 };
22
23 pub const NOT_PTR: usize = {
24 //~^ NOTE
25 unsafe { (42 as *const u8).offset_from(&5u8) as usize }
26 };
27
28 pub const NOT_MULTIPLE_OF_SIZE: isize = {
29 //~^ NOTE
30 let data = [5u8, 6, 7];
31 let base_ptr = data.as_ptr();
32 let field_ptr = &data[1] as *const u8 as *const u16;
33 unsafe { field_ptr.offset_from(base_ptr as *const u16) }
34 };
35
36 pub const OFFSET_FROM_NULL: isize = {
37 //~^ NOTE
38 let ptr = 0 as *const u8;
39 unsafe { ptr.offset_from(ptr) }
40 };
41
42 pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
43 //~^ NOTE
44 let ptr1 = 8 as *const u8;
45 let ptr2 = 16 as *const u8;
46 unsafe { ptr2.offset_from(ptr1) }
47 };
48
49 fn main() {}