1 #![feature(const_ptr_sub_ptr)]
2 #![feature(core_intrinsics)]
4 use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned};
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 //~| pointers into different allocations
23 pub const NOT_PTR: usize = {
24 unsafe { (42 as *const u8).offset_from(&5u8) as usize }
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
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 a dangling pointer
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 //~| 0x8[noalloc] is a dangling pointer
48 const OUT_OF_BOUNDS_1: isize = {
49 let start_ptr = &4 as *const _ as *const u8;
51 let end_ptr = (start_ptr).wrapping_add(length);
52 // First ptr is out of bounds
53 unsafe { ptr_offset_from(end_ptr, start_ptr) } //~ERROR evaluation of constant value failed
54 //~| pointer to 10 bytes starting at offset 0 is out-of-bounds
57 const OUT_OF_BOUNDS_2: isize = {
58 let start_ptr = &4 as *const _ as *const u8;
60 let end_ptr = (start_ptr).wrapping_add(length);
61 // Second ptr is out of bounds
62 unsafe { ptr_offset_from(start_ptr, end_ptr) } //~ERROR evaluation of constant value failed
63 //~| pointer to 10 bytes starting at offset 0 is out-of-bounds
66 const OUT_OF_BOUNDS_SAME: isize = {
67 let start_ptr = &4 as *const _ as *const u8;
69 let end_ptr = (start_ptr).wrapping_add(length);
70 unsafe { ptr_offset_from(end_ptr, end_ptr) } //~ERROR evaluation of constant value failed
71 //~| pointer at offset 10 is out-of-bounds
74 pub const DIFFERENT_ALLOC_UNSIGNED: usize = {
75 let uninit = std::mem::MaybeUninit::<Struct>::uninit();
76 let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
77 let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
78 let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
79 unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) } //~ERROR evaluation of constant value failed
80 //~| pointers into different allocations
83 pub const TOO_FAR_APART1: isize = {
84 let ptr1 = ptr::null::<u8>();
85 let ptr2 = ptr1.wrapping_add(isize::MAX as usize + 42);
86 unsafe { ptr_offset_from(ptr2, ptr1) } //~ERROR evaluation of constant value failed
89 pub const TOO_FAR_APART2: isize = {
90 let ptr1 = ptr::null::<u8>();
91 let ptr2 = ptr1.wrapping_add(isize::MAX as usize + 42);
92 unsafe { ptr_offset_from(ptr1, ptr2) } //~ERROR evaluation of constant value failed
96 const WRONG_ORDER_UNSIGNED: usize = {
97 let a = ['a', 'b', 'c'];
99 unsafe { ptr_offset_from_unsigned(p, p.add(2) ) } //~ERROR evaluation of constant value failed
100 //~| first pointer has smaller offset than second: 0 < 8
102 pub const TOO_FAR_APART_UNSIGNED: usize = {
103 let ptr1 = ptr::null::<u8>();
104 let ptr2 = ptr1.wrapping_add(isize::MAX as usize + 42);
105 // This would fit into a `usize` but we still don't allow it.
106 unsafe { ptr_offset_from_unsigned(ptr2, ptr1) } //~ERROR evaluation of constant value failed
110 // These do NOT complain that pointers are too far apart; they pass that check (to then fail the
112 pub const OFFSET_VERY_FAR1: isize = {
113 let ptr1 = ptr::null::<u8>();
114 let ptr2 = ptr1.wrapping_offset(isize::MAX);
115 unsafe { ptr2.offset_from(ptr1) }
118 pub const OFFSET_VERY_FAR2: isize = {
119 let ptr1 = ptr::null::<u8>();
120 let ptr2 = ptr1.wrapping_offset(isize::MAX);
121 unsafe { ptr1.offset_from(ptr2.wrapping_offset(1)) }