]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/offset_from_ub.rs
Stabilize File::options()
[rust.git] / src / test / ui / consts / offset_from_ub.rs
1 #![feature(const_raw_ptr_deref)]
2 #![feature(const_ptr_offset_from)]
3 #![feature(core_intrinsics)]
4
5 use std::intrinsics::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     let uninit = std::mem::MaybeUninit::<Struct>::uninit();
15     let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
16     let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
17     let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
18     let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed
19     //~| cannot compute offset of pointers into different allocations.
20     offset as usize
21 };
22
23 pub const NOT_PTR: usize = {
24     unsafe { (42 as *const u8).offset_from(&5u8) as usize }
25 };
26
27 pub const NOT_MULTIPLE_OF_SIZE: isize = {
28     let data = [5u8, 6, 7];
29     let base_ptr = data.as_ptr();
30     let field_ptr = &data[1] as *const u8 as *const u16;
31     unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) } //~ERROR evaluation of constant value failed
32     //~| 1_isize cannot be divided by 2_isize without remainder
33 };
34
35 pub const OFFSET_FROM_NULL: isize = {
36     let ptr = 0 as *const u8;
37     unsafe { ptr_offset_from(ptr, ptr) } //~ERROR evaluation of constant value failed
38     //~| null pointer is not a valid pointer
39 };
40
41 pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
42     let ptr1 = 8 as *const u8;
43     let ptr2 = 16 as *const u8;
44     unsafe { ptr_offset_from(ptr2, ptr1) } //~ERROR evaluation of constant value failed
45     //~| 0x10 is not a valid pointer
46 };
47
48 fn main() {}