]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/offset.rs
Auto merge of #99028 - tmiasko:inline, r=estebank
[rust.git] / src / test / ui / consts / offset.rs
1 // run-pass
2 #![feature(const_ptr_offset_from)]
3 use std::ptr;
4
5 #[repr(C)]
6 struct Struct {
7     a: u32,
8     b: u32,
9     c: u32,
10 }
11 static S: Struct = Struct { a: 0, b: 0, c: 0 };
12
13 // For these tests we use offset_from to check that two pointers are equal.
14 // Rust doesn't currently support comparing pointers in const fn.
15
16 static OFFSET_NO_CHANGE: bool = unsafe {
17     let p1 = &S.b as *const u32;
18     let p2 = p1.offset(2).offset(-2);
19     p1.offset_from(p2) == 0
20 };
21 static OFFSET_MIDDLE: bool = unsafe {
22     let p1 = (&S.a as *const u32).offset(1);
23     let p2 = (&S.c as *const u32).offset(-1);
24     p1.offset_from(p2) == 0
25 };
26 // Pointing to the end of the allocation is OK
27 static OFFSET_END: bool = unsafe {
28     let p1 = (&S.a as *const u32).offset(3);
29     let p2 = (&S.c as *const u32).offset(1);
30     p1.offset_from(p2) == 0
31 };
32 // Casting though a differently sized type is OK
33 static OFFSET_U8_PTR: bool = unsafe {
34     let p1 = (&S.a as *const u32 as *const u8).offset(5);
35     let p2 = (&S.c as *const u32 as *const u8).offset(-3);
36     p1.offset_from(p2) == 0
37 };
38 // Any offset with a ZST does nothing
39 const OFFSET_ZST: bool = unsafe {
40     let pz = &() as *const ();
41     // offset_from can't work with ZSTs, so cast to u8 ptr
42     let p1 = pz.offset(5) as *const u8;
43     let p2 = pz.offset(isize::MIN) as *const u8;
44     p1.offset_from(p2) == 0
45 };
46 const OFFSET_ZERO: bool = unsafe {
47     let p = [0u8; 0].as_ptr();
48     p.offset(0).offset_from(p) == 0
49 };
50 const OFFSET_ONE: bool = unsafe {
51     let p = &42u32 as *const u32;
52     p.offset(1).offset_from(p) == 1
53 };
54 const OFFSET_DANGLING: bool = unsafe {
55     let p = ptr::NonNull::<u8>::dangling().as_ptr();
56     p.offset(0).offset_from(p) == 0
57 };
58 const OFFSET_UNALIGNED: bool = unsafe {
59     let arr = [0u8; 32];
60     let p1 = arr.as_ptr();
61     let p2 = (p1.offset(2) as *const u32).offset(1);
62     (p2 as *const u8).offset_from(p1) == 6
63 };
64
65 const WRAP_OFFSET_NO_CHANGE: bool = unsafe {
66     let p1 = &42u32 as *const u32;
67     let p2 = p1.wrapping_offset(1000).wrapping_offset(-1000);
68     let p3 = p1.wrapping_offset(-1000).wrapping_offset(1000);
69     (p1.offset_from(p2) == 0) & (p1.offset_from(p3) == 0)
70 };
71 const WRAP_ADDRESS_SPACE: bool = unsafe {
72     let p1 = &42u8 as *const u8;
73     let p2 = p1.wrapping_offset(isize::MIN).wrapping_offset(isize::MIN);
74     p1.offset_from(p2) == 0
75 };
76 // Wrap on the count*size_of::<T>() calculation.
77 const WRAP_SIZE_OF: bool = unsafe {
78     // Make sure that if p1 moves backwards, we are still in range
79     let arr = [0u32; 2];
80     let p = &arr[1] as *const u32;
81     // With wrapping arithmetic, isize::MAX * 4 == -4
82     let wrapped = p.wrapping_offset(isize::MAX);
83     let backward = p.wrapping_offset(-1);
84     wrapped.offset_from(backward) == 0
85 };
86 const WRAP_INTEGER_POINTER: bool = unsafe {
87     let p1 = (0x42 as *const u32).wrapping_offset(4);
88     let p2 = 0x52 as *const u32;
89     p1.offset_from(p2) == 0
90 };
91 const WRAP_NULL: bool = unsafe {
92     let p1 = ptr::null::<u32>().wrapping_offset(1);
93     let p2 = 0x4 as *const u32;
94     p1.offset_from(p2) == 0
95 };
96
97 fn main() {
98     assert!(OFFSET_NO_CHANGE);
99     assert!(OFFSET_MIDDLE);
100     assert!(OFFSET_END);
101     assert!(OFFSET_U8_PTR);
102     assert!(OFFSET_ZST);
103     assert!(OFFSET_ZERO);
104     assert!(OFFSET_ONE);
105     assert!(OFFSET_DANGLING);
106     assert!(OFFSET_UNALIGNED);
107
108     assert!(WRAP_OFFSET_NO_CHANGE);
109     assert!(WRAP_ADDRESS_SPACE);
110     assert!(WRAP_SIZE_OF);
111     assert!(WRAP_INTEGER_POINTER);
112     assert!(WRAP_NULL);
113 }