]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/offset.rs
Do not suggest `let_else` if no bindings would be introduced
[rust.git] / src / test / ui / consts / offset.rs
1 // run-pass
2 #![feature(const_ptr_offset)]
3 #![feature(const_ptr_offset_from)]
4 use std::ptr;
5
6 #[repr(C)]
7 struct Struct {
8     a: u32,
9     b: u32,
10     c: u32,
11 }
12 static S: Struct = Struct { a: 0, b: 0, c: 0 };
13
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.
16
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
21 };
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
26 };
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
32 };
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
38 };
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
46 };
47 const OFFSET_ZERO: bool = unsafe {
48     let p = [0u8; 0].as_ptr();
49     p.offset(0).offset_from(p) == 0
50 };
51 const OFFSET_ONE: bool = unsafe {
52     let p = &42u32 as *const u32;
53     p.offset(1).offset_from(p) == 1
54 };
55 const OFFSET_DANGLING: bool = unsafe {
56     let p = ptr::NonNull::<u8>::dangling().as_ptr();
57     p.offset(0).offset_from(p) == 0
58 };
59 const OFFSET_UNALIGNED: bool = unsafe {
60     let arr = [0u8; 32];
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
64 };
65
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)
71 };
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
76 };
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
80     let arr = [0u32; 2];
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
86 };
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
91 };
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
96 };
97
98 fn main() {
99     assert!(OFFSET_NO_CHANGE);
100     assert!(OFFSET_MIDDLE);
101     assert!(OFFSET_END);
102     assert!(OFFSET_U8_PTR);
103     assert!(OFFSET_ZST);
104     assert!(OFFSET_ZERO);
105     assert!(OFFSET_ONE);
106     assert!(OFFSET_DANGLING);
107     assert!(OFFSET_UNALIGNED);
108
109     assert!(WRAP_OFFSET_NO_CHANGE);
110     assert!(WRAP_ADDRESS_SPACE);
111     assert!(WRAP_SIZE_OF);
112     assert!(WRAP_INTEGER_POINTER);
113     assert!(WRAP_NULL);
114 }