|
1 | | -//@ build-pass |
| 1 | +//@ compile-flags: --crate-type=lib |
| 2 | +//@ check-pass |
2 | 3 | //@ edition: 2024 |
3 | 4 | #![feature(const_raw_ptr_comparison)] |
4 | 5 | #![feature(fn_align)] |
@@ -77,14 +78,28 @@ const VTABLE_PTR_2: *const () = { |
77 | 78 | vtable |
78 | 79 | }; |
79 | 80 |
|
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); |
88 | 103 |
|
89 | 104 | // Statics cannot be duplicated |
90 | 105 | do_test!(&A, &A, Some(true)); |
@@ -167,5 +182,3 @@ do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE |
167 | 182 | do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), VTABLE_PTR_1, None); |
168 | 183 | do_test!((&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None); |
169 | 184 | do_test!((&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1), FN_PTR, None); |
170 | | - |
171 | | -fn main() {} |
|
0 commit comments