Skip to content

Commit 37efd55

Browse files
authored
Rollup merge of #99511 - RalfJung:raw_eq, r=wesleywiser
make raw_eq precondition more restrictive Specifically, don't allow comparing pointers that way. Comparing pointers is subtle because you have to talk about what happens to the provenance. This matches what [Miri already implements](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9eb1dfb8a61b5a2d4a7cee43df2717af), and all existing users are fine with this. If raw_eq on pointers is ever desired, we can adjust the intrinsic spec and Miri implementation as needed, but for now that seems just unnecessary. Also, this is a const intrinsic, and in const, comparing pointers this way is *not possible* -- so if we allow the intrinsic to compare pointers in general, we need to impose an extra restrictions saying that in const-context, pointers are *not* okay.
2 parents 92b32e3 + 338d7c2 commit 37efd55

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

library/core/src/array/equality.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ macro_rules! is_raw_eq_comparable {
173173
)+};
174174
}
175175

176-
// SAFETY: All the ordinary integer types allow all bit patterns as distinct values
176+
// SAFETY: All the ordinary integer types have no padding, and are not pointers.
177177
is_raw_eq_comparable!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
178178

179-
// SAFETY: bool and char have *niches*, but no *padding*, so this is sound
179+
// SAFETY: bool and char have *niches*, but no *padding* (and these are not pointer types), so this
180+
// is sound
180181
is_raw_eq_comparable!(bool, char);
181182

182-
// SAFETY: Similarly, the non-zero types have a niche, but no undef,
183+
// SAFETY: Similarly, the non-zero types have a niche, but no undef and no pointers,
183184
// and they compare like their underlying numeric type.
184185
is_raw_eq_comparable!(
185186
NonZeroU8,

library/core/src/intrinsics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,8 @@ extern "rust-intrinsic" {
22822282
///
22832283
/// # Safety
22842284
///
2285-
/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
2285+
/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2286+
/// pointer value.
22862287
/// Note that this is a stricter criterion than just the *values* being
22872288
/// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
22882289
///

0 commit comments

Comments
 (0)