]>
Commit | Line | Data |
---|---|---|
1 | // compile-flags: --crate-type=lib | |
2 | // normalize-stderr-32bit: "8 bytes" -> "$$TWO_WORDS bytes" | |
3 | // normalize-stderr-64bit: "16 bytes" -> "$$TWO_WORDS bytes" | |
4 | // normalize-stderr-32bit: "size 4" -> "size $$WORD" | |
5 | // normalize-stderr-64bit: "size 8" -> "size $$WORD" | |
6 | ||
7 | #![feature( | |
8 | core_intrinsics, | |
9 | const_raw_ptr_comparison, | |
10 | const_ptr_offset, | |
11 | const_raw_ptr_deref | |
12 | )] | |
13 | ||
14 | const FOO: &usize = &42; | |
15 | ||
16 | macro_rules! check { | |
17 | (eq, $a:expr, $b:expr) => { | |
18 | pub const _: () = | |
19 | assert!(std::intrinsics::ptr_guaranteed_eq($a as *const u8, $b as *const u8)); | |
20 | }; | |
21 | (ne, $a:expr, $b:expr) => { | |
22 | pub const _: () = | |
23 | assert!(std::intrinsics::ptr_guaranteed_ne($a as *const u8, $b as *const u8)); | |
24 | }; | |
25 | (!eq, $a:expr, $b:expr) => { | |
26 | pub const _: () = | |
27 | assert!(!std::intrinsics::ptr_guaranteed_eq($a as *const u8, $b as *const u8)); | |
28 | }; | |
29 | (!ne, $a:expr, $b:expr) => { | |
30 | pub const _: () = | |
31 | assert!(!std::intrinsics::ptr_guaranteed_ne($a as *const u8, $b as *const u8)); | |
32 | }; | |
33 | } | |
34 | ||
35 | check!(eq, 0, 0); | |
36 | check!(ne, 0, 1); | |
37 | check!(!eq, 0, 1); | |
38 | check!(!ne, 0, 0); | |
39 | check!(ne, FOO as *const _, 0); | |
40 | check!(!eq, FOO as *const _, 0); | |
41 | // We want pointers to be equal to themselves, but aren't checking this yet because | |
42 | // there are some open questions (e.g. whether function pointers to the same function | |
43 | // compare equal, they don't necessarily at runtime). | |
44 | // The case tested here should work eventually, but does not work yet. | |
45 | check!(!eq, FOO as *const _, FOO as *const _); | |
46 | check!(ne, unsafe { (FOO as *const usize).offset(1) }, 0); | |
47 | check!(!eq, unsafe { (FOO as *const usize).offset(1) }, 0); | |
48 | ||
49 | check!(ne, unsafe { (FOO as *const usize as *const u8).offset(3) }, 0); | |
50 | check!(!eq, unsafe { (FOO as *const usize as *const u8).offset(3) }, 0); | |
51 | ||
52 | /////////////////////////////////////////////////////////////////////////////// | |
53 | // If any of the below start compiling, make sure to add a `check` test for it. | |
54 | // These invocations exist as canaries so we don't forget to check that the | |
55 | // behaviour of `guaranteed_eq` and `guaranteed_ne` is still correct. | |
56 | // All of these try to obtain an out of bounds pointer in some manner. If we | |
57 | // can create out of bounds pointers, we can offset a pointer far enough that | |
58 | // at runtime it would be zero and at compile-time it would not be zero. | |
59 | ||
60 | const _: *const usize = unsafe { (FOO as *const usize).offset(2) }; | |
61 | ||
62 | const _: *const u8 = | |
63 | unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; | |
64 | //~^ ERROR evaluation of constant value failed | |
65 | //~| out-of-bounds | |
66 | ||
67 | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 }; | |
68 | //~^ ERROR any use of this value will cause an error | |
69 | //~| unable to turn pointer into raw bytes | |
70 | //~| WARN this was previously accepted by the compiler but is being phased out | |
71 | ||
72 | const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 }; | |
73 | //~^ ERROR any use of this value will cause an error | |
74 | //~| unable to turn pointer into raw bytes | |
75 | //~| WARN this was previously accepted by the compiler but is being phased out |