Skip to content

Commit f97aba2

Browse files
committed
raw_eq: using it on bytes with provenance is not UB (outside const-eval)
1 parent 99322d8 commit f97aba2

File tree

7 files changed

+9
-35
lines changed

7 files changed

+9
-35
lines changed

compiler/rustc_const_eval/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,6 @@ const_eval_range_upper = less or equal to {$hi}
301301
const_eval_range_wrapping = less or equal to {$hi}, or greater or equal to {$lo}
302302
const_eval_raw_bytes = the raw bytes of the constant (size: {$size}, align: {$align}) {"{"}{$bytes}{"}"}
303303
304-
const_eval_raw_eq_with_provenance =
305-
`raw_eq` on bytes with provenance
306-
307304
const_eval_raw_ptr_comparison =
308305
pointers cannot be reliably compared during const eval
309306
.note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information

compiler/rustc_const_eval/src/interpret/intrinsics.rs

-3
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
693693
// zero-sized access
694694
return Ok(&[]);
695695
};
696-
if alloc_ref.has_provenance() {
697-
throw_ub_custom!(fluent::const_eval_raw_eq_with_provenance);
698-
}
699696
alloc_ref.get_bytes_strip_provenance()
700697
};
701698

library/core/src/intrinsics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2436,11 +2436,13 @@ extern "rust-intrinsic" {
24362436
///
24372437
/// # Safety
24382438
///
2439-
/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2440-
/// pointer value.
2439+
/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
24412440
/// Note that this is a stricter criterion than just the *values* being
24422441
/// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
24432442
///
2443+
/// At compile-time, it is furthermore UB to call this if any of the bytes
2444+
/// in `*a` or `*b` have provenance.
2445+
///
24442446
/// (The implementation is allowed to branch on the results of comparisons,
24452447
/// which is UB if any of their inputs are `undef`.)
24462448
#[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]

src/tools/miri/tests/fail/intrinsics/raw_eq_on_ptr.rs

-10
This file was deleted.

src/tools/miri/tests/fail/intrinsics/raw_eq_on_ptr.stderr

-15
This file was deleted.

tests/ui/intrinsics/intrinsic-raw_eq-const-bad.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const RAW_EQ_PADDING: bool = unsafe {
1010
const RAW_EQ_PTR: bool = unsafe {
1111
std::intrinsics::raw_eq(&(&0), &(&1))
1212
//~^ ERROR evaluation of constant value failed
13-
//~| `raw_eq` on bytes with provenance
13+
//~| unable to turn pointer into integer
1414
};
1515

1616
pub fn main() {

tests/ui/intrinsics/intrinsic-raw_eq-const-bad.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ error[E0080]: evaluation of constant value failed
88
--> $DIR/intrinsic-raw_eq-const-bad.rs:11:5
99
|
1010
LL | std::intrinsics::raw_eq(&(&0), &(&1))
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `raw_eq` on bytes with provenance
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
12+
|
13+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
14+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
1215

1316
error: aborting due to 2 previous errors
1417

0 commit comments

Comments
 (0)