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