Skip to content

Commit 252f0e0

Browse files
committed
Update tests/ui/const-ptr/guaranteed_cmp.rs to prepare to combine it with tests/ui/consts/ptr_comparisons.rs
1 parent 0947a94 commit 252f0e0

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

tests/ui/const-ptr/guaranteed_cmp.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//@ build-pass
1+
//@ compile-flags: --crate-type=lib
2+
//@ check-pass
23
//@ edition: 2024
34
#![feature(const_raw_ptr_comparison)]
45
#![feature(fn_align)]
@@ -77,14 +78,28 @@ const VTABLE_PTR_2: *const () = {
7778
vtable
7879
};
7980

80-
// Cannot be `None`: static's address, references, and `fn` pointers cannot be null,
81-
// and `is_null` is stable with strong guarantees, and `is_null` is implemented using
82-
// `guaranteed_cmp`.
83-
do_test!(&A, std::ptr::null::<()>(), Some(false));
84-
do_test!(&ZST, std::ptr::null::<()>(), Some(false));
85-
do_test!(&(), std::ptr::null::<()>(), Some(false));
86-
do_test!(const { &() }, std::ptr::null::<()>(), Some(false));
87-
do_test!(FN_PTR, std::ptr::null::<()>(), Some(false));
81+
// Cannot be `None`: `is_null` is stable with strong guarantees about integer-valued pointers.
82+
do_test!(0 as *const u8, 0 as *const u8, Some(true));
83+
do_test!(0 as *const u8, 1 as *const u8, Some(false));
84+
85+
// Cannot be `None`: `static`s' addresses, references, (and within and one-past-the-end of those),
86+
// and `fn` pointers cannot be null, and `is_null` is stable with strong guarantees, and
87+
// `is_null` is implemented using `guaranteed_cmp`.
88+
do_test!(&A, 0 as *const u8, Some(false));
89+
do_test!((&raw const A).cast::<u8>().wrapping_add(1), 0 as *const u8, Some(false));
90+
do_test!((&raw const A).wrapping_add(1), 0 as *const u8, Some(false));
91+
do_test!(&ZST, 0 as *const u8, Some(false));
92+
do_test!(&(), 0 as *const u8, Some(false));
93+
do_test!(const { &() }, 0 as *const u8, Some(false));
94+
do_test!(FN_PTR, 0 as *const u8, Some(false));
95+
96+
// aside from 0, these pointers might end up pretty much anywhere.
97+
do_test!(&A, align_of::<T>() as *const u8, None);
98+
do_test!(&A, 1 as *const u8, Some(false)); // this one takes into account alignment, so we know that
99+
100+
// When pointers go out-of-bounds, they *might* become null, so these comparions cannot work.
101+
do_test!((&raw const A).wrapping_add(2), 0 as *const u8, None);
102+
do_test!((&raw const A).wrapping_sub(1), 0 as *const u8, None);
88103

89104
// Statics cannot be duplicated
90105
do_test!(&A, &A, Some(true));
@@ -167,5 +182,3 @@ do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE
167182
do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE_PTR_1, None);
168183
do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None);
169184
do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None);
170-
171-
fn main() {}

0 commit comments

Comments
 (0)