]> git.proxmox.com Git - rustc.git/blame - src/test/ui/consts/ptr_comparisons.rs
New upstream version 1.57.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,
10 const_ptr_offset,
5869c6ff 11 const_raw_ptr_deref
3dfed10e
XL
12)]
13
14const FOO: &usize = &42;
15
16macro_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
35check!(eq, 0, 0);
36check!(ne, 0, 1);
37check!(!eq, 0, 1);
38check!(!ne, 0, 0);
39check!(ne, FOO as *const _, 0);
40check!(!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.
45check!(!eq, FOO as *const _, FOO as *const _);
46check!(ne, unsafe { (FOO as *const usize).offset(1) }, 0);
47check!(!eq, unsafe { (FOO as *const usize).offset(1) }, 0);
48
49check!(ne, unsafe { (FOO as *const usize as *const u8).offset(3) }, 0);
50check!(!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
60const _: *const usize = unsafe { (FOO as *const usize).offset(2) };
3dfed10e
XL
61
62const _: *const u8 =
5869c6ff 63 unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) };
136023e0
XL
64//~^ ERROR evaluation of constant value failed
65//~| out-of-bounds
3dfed10e
XL
66
67const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 };
68//~^ ERROR any use of this value will cause an error
136023e0 69//~| unable to turn pointer into raw bytes
5869c6ff 70//~| WARN this was previously accepted by the compiler but is being phased out
3dfed10e
XL
71
72const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 };
73//~^ ERROR any use of this value will cause an error
136023e0 74//~| unable to turn pointer into raw bytes
5869c6ff 75//~| WARN this was previously accepted by the compiler but is being phased out