]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/offset_from_ub.rs
Do not suggest `let_else` if no bindings would be introduced
[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;
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     //~| cannot compute offset of 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     //~| 0x10 is not a valid pointer
45 };
46
47 fn main() {}