1 // compile-flags: --crate-type=lib
2 // normalize-stderr-32bit: "offset 8" -> "offset $$TWO_WORDS"
3 // normalize-stderr-64bit: "offset 16" -> "offset $$TWO_WORDS"
4 // normalize-stderr-32bit: "size 4" -> "size $$WORD"
5 // normalize-stderr-64bit: "size 8" -> "size $$WORD"
10 const_raw_ptr_comparison
,
15 const FOO
: &usize = &42;
18 (eq
, $a
:expr
, $b
:expr
) => {
20 assert
!(std
::intrinsics
::ptr_guaranteed_eq($a
as *const u8, $b
as *const u8));
22 (ne
, $a
:expr
, $b
:expr
) => {
24 assert
!(std
::intrinsics
::ptr_guaranteed_ne($a
as *const u8, $b
as *const u8));
26 (!eq
, $a
:expr
, $b
:expr
) => {
28 assert
!(!std
::intrinsics
::ptr_guaranteed_eq($a
as *const u8, $b
as *const u8));
30 (!ne
, $a
:expr
, $b
:expr
) => {
32 assert
!(!std
::intrinsics
::ptr_guaranteed_ne($a
as *const u8, $b
as *const u8));
40 check
!(ne
, FOO
as *const _
, 0);
41 check
!(!eq
, FOO
as *const _
, 0);
42 // We want pointers to be equal to themselves, but aren't checking this yet because
43 // there are some open questions (e.g. whether function pointers to the same function
44 // compare equal, they don't necessarily at runtime).
45 // The case tested here should work eventually, but does not work yet.
46 check
!(!eq
, FOO
as *const _
, FOO
as *const _
);
47 check
!(ne
, unsafe { (FOO as *const usize).offset(1) }
, 0);
48 check
!(!eq
, unsafe { (FOO as *const usize).offset(1) }
, 0);
50 check
!(ne
, unsafe { (FOO as *const usize as *const u8).offset(3) }
, 0);
51 check
!(!eq
, unsafe { (FOO as *const usize as *const u8).offset(3) }
, 0);
53 ///////////////////////////////////////////////////////////////////////////////
54 // If any of the below start compiling, make sure to add a `check` test for it.
55 // These invocations exist as canaries so we don't forget to check that the
56 // behaviour of `guaranteed_eq` and `guaranteed_ne` is still correct.
57 // All of these try to obtain an out of bounds pointer in some manner. If we
58 // can create out of bounds pointers, we can offset a pointer far enough that
59 // at runtime it would be zero and at compile-time it would not be zero.
61 const _
: *const usize = unsafe { (FOO as *const usize).offset(2) }
;
66 unsafe { std::ptr::addr_of!((*(FOO as *const usize as *const [u8; 1000]))[999]) }
;
67 //~^ ERROR any use of this value will cause an error
69 //~| WARN this was previously accepted by the compiler but is being phased out
72 const _
: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 }
;
73 //~^ ERROR any use of this value will cause an error
74 //~| NOTE cannot cast pointer to integer
76 //~| WARN this was previously accepted by the compiler but is being phased out
79 const _
: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 }
;
80 //~^ ERROR any use of this value will cause an error
81 //~| NOTE cannot cast pointer to integer
83 //~| WARN this was previously accepted by the compiler but is being phased out