Skip to content

Commit edbafa2

Browse files
committed
Add more ptr_comparisons tests:
One that compares two non-null integer-valued pointers. One that compares an out-of-bounds pointer with null, that cannot be null due to alignment instead of in-bounds-ness. One that compares the one-past-the-end pointer of two statics, for which we conservatively say we don't know.
1 parent da2af7e commit edbafa2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tests/ui/consts/ptr_comparisons.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ const VTABLE_PTR_2: *const () = {
8282
do_test!(0 as *const u8, 0 as *const u8, Some(true));
8383
do_test!(0 as *const u8, 1 as *const u8, Some(false));
8484

85+
// Integer-valued pointers can always be compared.
86+
do_test!(1 as *const u8, 1 as *const u8, Some(true));
87+
do_test!(1 as *const u8, 2 as *const u8, Some(false));
88+
8589
// Cannot be `None`: `static`s' addresses, references, (and within and one-past-the-end of those),
8690
// and `fn` pointers cannot be null, and `is_null` is stable with strong guarantees, and
8791
// `is_null` is implemented using `guaranteed_cmp`.
@@ -93,6 +97,9 @@ do_test!(&(), 0 as *const u8, Some(false));
9397
do_test!(const { &() }, 0 as *const u8, Some(false));
9498
do_test!(FN_PTR, 0 as *const u8, Some(false));
9599

100+
// This pointer is out-of-bounds, but still cannot be equal to 0 because of alignment.
101+
do_test!((&raw const A).cast::<u8>().wrapping_add(size_of::<T>() + 1), 0 as *const u8, Some(false));
102+
96103
// aside from 0, these pointers might end up pretty much anywhere.
97104
do_test!(&A, align_of::<T>() as *const u8, None);
98105
do_test!((&raw const A).wrapping_byte_add(1), (align_of::<T>() + 1) as *const u8, None);
@@ -149,9 +156,11 @@ do_test!(&A, (&T(42) as *const T).wrapping_byte_add(1), Some(false));
149156
do_test!(&A, (const { &T(42) } as *const T).wrapping_byte_add(1), Some(false));
150157
do_test!(&A, ({ const X: T = T(42); &X } as *const T).wrapping_byte_add(1), Some(false));
151158

152-
// We could return `Some(false)` here, as pointers to different statics can never be equal if that
153-
// would require the statics to overlap, even if the pointers themselves are offset out of bounds.
154-
// We currently only check strictly in-bounds pointers, however.
159+
// We could return `Some(false)` for these, as pointers to different statics can never be equal if
160+
// that would require the statics to overlap, even if the pointers themselves are offset out of
161+
// bounds or one-past-the-end. We currently only check strictly in-bounds pointers when comparing
162+
// pointers to different statics, however.
163+
do_test!((&raw const A).wrapping_add(1), (&raw const B).wrapping_add(1), None);
155164
do_test!(
156165
(&raw const LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(2),
157166
(&raw const MUT_LARGE_WORD_ALIGNED).cast::<usize>().wrapping_add(1),

0 commit comments

Comments
 (0)