2 #![feature(const_ptr_offset)]
3 #![feature(const_ptr_offset_from)]
12 static S: Struct = Struct { a: 0, b: 0, c: 0 };
14 // For these tests we use offset_from to check that two pointers are equal.
15 // Rust doesn't currently support comparing pointers in const fn.
17 static OFFSET_NO_CHANGE: bool = unsafe {
18 let p1 = &S.b as *const u32;
19 let p2 = p1.offset(2).offset(-2);
20 p1.offset_from(p2) == 0
22 static OFFSET_MIDDLE: bool = unsafe {
23 let p1 = (&S.a as *const u32).offset(1);
24 let p2 = (&S.c as *const u32).offset(-1);
25 p1.offset_from(p2) == 0
27 // Pointing to the end of the allocation is OK
28 static OFFSET_END: bool = unsafe {
29 let p1 = (&S.a as *const u32).offset(3);
30 let p2 = (&S.c as *const u32).offset(1);
31 p1.offset_from(p2) == 0
33 // Casting though a differently sized type is OK
34 static OFFSET_U8_PTR: bool = unsafe {
35 let p1 = (&S.a as *const u32 as *const u8).offset(5);
36 let p2 = (&S.c as *const u32 as *const u8).offset(-3);
37 p1.offset_from(p2) == 0
39 // Any offset with a ZST does nothing
40 const OFFSET_ZST: bool = unsafe {
41 let pz = &() as *const ();
42 // offset_from can't work with ZSTs, so cast to u8 ptr
43 let p1 = pz.offset(5) as *const u8;
44 let p2 = pz.offset(isize::MIN) as *const u8;
45 p1.offset_from(p2) == 0
47 const OFFSET_ZERO: bool = unsafe {
48 let p = [0u8; 0].as_ptr();
49 p.offset(0).offset_from(p) == 0
51 const OFFSET_ONE: bool = unsafe {
52 let p = &42u32 as *const u32;
53 p.offset(1).offset_from(p) == 1
55 const OFFSET_DANGLING: bool = unsafe {
56 let p = ptr::NonNull::<u8>::dangling().as_ptr();
57 p.offset(0).offset_from(p) == 0
59 const OFFSET_UNALIGNED: bool = unsafe {
61 let p1 = arr.as_ptr();
62 let p2 = (p1.offset(2) as *const u32).offset(1);
63 (p2 as *const u8).offset_from(p1) == 6
66 const WRAP_OFFSET_NO_CHANGE: bool = unsafe {
67 let p1 = &42u32 as *const u32;
68 let p2 = p1.wrapping_offset(1000).wrapping_offset(-1000);
69 let p3 = p1.wrapping_offset(-1000).wrapping_offset(1000);
70 (p1.offset_from(p2) == 0) & (p1.offset_from(p3) == 0)
72 const WRAP_ADDRESS_SPACE: bool = unsafe {
73 let p1 = &42u8 as *const u8;
74 let p2 = p1.wrapping_offset(isize::MIN).wrapping_offset(isize::MIN);
75 p1.offset_from(p2) == 0
77 // Wrap on the count*size_of::<T>() calculation.
78 const WRAP_SIZE_OF: bool = unsafe {
79 // Make sure that if p1 moves backwards, we are still in range
81 let p = &arr[1] as *const u32;
82 // With wrapping arithmetic, isize::MAX * 4 == -4
83 let wrapped = p.wrapping_offset(isize::MAX);
84 let backward = p.wrapping_offset(-1);
85 wrapped.offset_from(backward) == 0
87 const WRAP_INTEGER_POINTER: bool = unsafe {
88 let p1 = (0x42 as *const u32).wrapping_offset(4);
89 let p2 = 0x52 as *const u32;
90 p1.offset_from(p2) == 0
92 const WRAP_NULL: bool = unsafe {
93 let p1 = ptr::null::<u32>().wrapping_offset(1);
94 let p2 = 0x4 as *const u32;
95 p1.offset_from(p2) == 0
99 assert!(OFFSET_NO_CHANGE);
100 assert!(OFFSET_MIDDLE);
102 assert!(OFFSET_U8_PTR);
104 assert!(OFFSET_ZERO);
106 assert!(OFFSET_DANGLING);
107 assert!(OFFSET_UNALIGNED);
109 assert!(WRAP_OFFSET_NO_CHANGE);
110 assert!(WRAP_ADDRESS_SPACE);
111 assert!(WRAP_SIZE_OF);
112 assert!(WRAP_INTEGER_POINTER);