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