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