]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/offset_from_ub.rs
Rollup merge of #98609 - TaKO8Ki:fix-ice-for-associated-constant-generics, r=lcnr
[rust.git] / src / test / ui / consts / offset_from_ub.rs
1 #![feature(const_ptr_offset_from)]
2 #![feature(core_intrinsics)]
3
4 use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned};
5
6 #[repr(C)]
7 struct Struct {
8     data: u8,
9     field: u8,
10 }
11
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
19     offset as usize
20 };
21
22 pub const NOT_PTR: usize = {
23     unsafe { (42 as *const u8).offset_from(&5u8) as usize }
24 };
25
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
32 };
33
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
38 };
39
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
45 };
46
47 const OUT_OF_BOUNDS_1: isize = {
48     let start_ptr = &4 as *const _ as *const u8;
49     let length = 10;
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
54 };
55
56 const OUT_OF_BOUNDS_2: isize = {
57     let start_ptr = &4 as *const _ as *const u8;
58     let length = 10;
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
63 };
64
65 const OUT_OF_BOUNDS_SAME: isize = {
66     let start_ptr = &4 as *const _ as *const u8;
67     let length = 10;
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
71 };
72
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
80     offset as usize
81 };
82
83 const WRONG_ORDER_UNSIGNED: usize = {
84     let a = ['a', 'b', 'c'];
85     let p = a.as_ptr();
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
88 };
89
90 fn main() {}