1 #![feature(const_ptr_offset_from)]
2 #![feature(core_intrinsics)]
4 use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned};
12 pub const DIFFERENT_ALLOC: usize = {
13 let uninit = std::mem::MaybeUninit::<Struct>::uninit();
14 let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
15 let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
16 let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
17 let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed
18 //~| pointers into different allocations
22 pub const NOT_PTR: usize = {
23 unsafe { (42 as *const u8).offset_from(&5u8) as usize }
26 pub const NOT_MULTIPLE_OF_SIZE: isize = {
27 let data = [5u8, 6, 7];
28 let base_ptr = data.as_ptr();
29 let field_ptr = &data[1] as *const u8 as *const u16;
30 unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) } //~ERROR evaluation of constant value failed
31 //~| 1_isize cannot be divided by 2_isize without remainder
34 pub const OFFSET_FROM_NULL: isize = {
35 let ptr = 0 as *const u8;
36 unsafe { ptr_offset_from(ptr, ptr) } //~ERROR evaluation of constant value failed
37 //~| null pointer is not a valid pointer
40 pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
41 let ptr1 = 8 as *const u8;
42 let ptr2 = 16 as *const u8;
43 unsafe { ptr_offset_from(ptr2, ptr1) } //~ERROR evaluation of constant value failed
44 //~| 0x8 is not a valid pointer
47 const OUT_OF_BOUNDS_1: isize = {
48 let start_ptr = &4 as *const _ as *const u8;
50 let end_ptr = (start_ptr).wrapping_add(length);
51 // First ptr is out of bounds
52 unsafe { ptr_offset_from(end_ptr, start_ptr) } //~ERROR evaluation of constant value failed
53 //~| pointer to 10 bytes starting at offset 0 is out-of-bounds
56 const OUT_OF_BOUNDS_2: isize = {
57 let start_ptr = &4 as *const _ as *const u8;
59 let end_ptr = (start_ptr).wrapping_add(length);
60 // Second ptr is out of bounds
61 unsafe { ptr_offset_from(start_ptr, end_ptr) } //~ERROR evaluation of constant value failed
62 //~| pointer to 10 bytes starting at offset 0 is out-of-bounds
65 const OUT_OF_BOUNDS_SAME: isize = {
66 let start_ptr = &4 as *const _ as *const u8;
68 let end_ptr = (start_ptr).wrapping_add(length);
69 unsafe { ptr_offset_from(end_ptr, end_ptr) } //~ERROR evaluation of constant value failed
70 //~| pointer at offset 10 is out-of-bounds
73 pub const DIFFERENT_ALLOC_UNSIGNED: usize = {
74 let uninit = std::mem::MaybeUninit::<Struct>::uninit();
75 let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
76 let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
77 let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
78 let offset = unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed
79 //~| pointers into different allocations
83 const WRONG_ORDER_UNSIGNED: usize = {
84 let a = ['a', 'b', 'c'];
86 unsafe { ptr_offset_from_unsigned(p, p.add(2) ) } //~ERROR evaluation of constant value failed
87 //~| first pointer has smaller offset than second: 0 < 8