2 #![feature(const_ptr_offset_from)]
11 static S: Struct = Struct { a: 0, b: 0, c: 0 };
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.
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
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
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
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
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
46 const OFFSET_ZERO: bool = unsafe {
47 let p = [0u8; 0].as_ptr();
48 p.offset(0).offset_from(p) == 0
50 const OFFSET_ONE: bool = unsafe {
51 let p = &42u32 as *const u32;
52 p.offset(1).offset_from(p) == 1
54 const OFFSET_DANGLING: bool = unsafe {
55 let p = ptr::NonNull::<u8>::dangling().as_ptr();
56 p.offset(0).offset_from(p) == 0
58 const OFFSET_UNALIGNED: bool = unsafe {
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
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)
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
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
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
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
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
98 assert!(OFFSET_NO_CHANGE);
99 assert!(OFFSET_MIDDLE);
101 assert!(OFFSET_U8_PTR);
103 assert!(OFFSET_ZERO);
105 assert!(OFFSET_DANGLING);
106 assert!(OFFSET_UNALIGNED);
108 assert!(WRAP_OFFSET_NO_CHANGE);
109 assert!(WRAP_ADDRESS_SPACE);
110 assert!(WRAP_SIZE_OF);
111 assert!(WRAP_INTEGER_POINTER);