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