]> git.lizzy.rs Git - rust.git/blob - tests/ui/consts/offset_from_ub.rs
Rollup merge of #107004 - compiler-errors:new-solver-new-candidates-2, r=lcnr
[rust.git] / tests / ui / consts / offset_from_ub.rs
1 #![feature(const_ptr_sub_ptr)]
2 #![feature(core_intrinsics)]
3
4 use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned};
5 use std::ptr;
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     //~| 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 a dangling 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     //~| 0x8[noalloc] is a dangling pointer
46 };
47
48 const OUT_OF_BOUNDS_1: isize = {
49     let start_ptr = &4 as *const _ as *const u8;
50     let length = 10;
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
55 };
56
57 const OUT_OF_BOUNDS_2: isize = {
58     let start_ptr = &4 as *const _ as *const u8;
59     let length = 10;
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
64 };
65
66 const OUT_OF_BOUNDS_SAME: isize = {
67     let start_ptr = &4 as *const _ as *const u8;
68     let length = 10;
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
72 };
73
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
81 };
82
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
87     //~| too far ahead
88 };
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
93     //~| too far before
94 };
95
96 const WRONG_ORDER_UNSIGNED: usize = {
97     let a = ['a', 'b', 'c'];
98     let p = a.as_ptr();
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
101 };
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
107     //~| too far ahead
108 };
109
110 // These do NOT complain that pointers are too far apart; they pass that check (to then fail the
111 // next one).
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) }
116     //~^ inside
117 };
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)) }
122     //~^ inside
123 };
124
125 fn main() {}