Skip to content

Commit 87cc16a

Browse files
committed
Improved implementation and comments after code review feedback
1 parent a14d9fb commit 87cc16a

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

core/src/intrinsics.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,16 +2020,12 @@ extern "rust-intrinsic" {
20202020
#[rustc_safe_intrinsic]
20212021
pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
20222022

2023-
/// This is a *typed* read, `copy *p` in MIR.
2023+
/// This is an implementation detail of [`crate::ptr::read`] and should
2024+
/// not be used anywhere else. See its comments for why this exists.
20242025
///
2025-
/// The stabilized form of this intrinsic is [`crate::ptr::read`], so
2026-
/// that can be implemented without needing to do an *untyped* copy
2027-
/// via [`copy_nonoverlapping`], and thus can get proper metadata.
2028-
///
2029-
/// This intrinsic can *only* be called with a copy or move of a local.
2030-
/// (It allows neither constants nor projections.)
2031-
///
2032-
/// To avoid introducing any `noalias` requirements, it just takes a pointer.
2026+
/// This intrinsic can *only* be called where the argument is a local without
2027+
/// projections (`read_via_copy(p)`, not `read_via_copy(*p)`) so that it
2028+
/// trivially obeys runtime-MIR rules about derefs in operands.
20332029
#[cfg(not(bootstrap))]
20342030
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
20352031
pub fn read_via_copy<T>(p: *const T) -> T;

core/src/ptr/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,12 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
11361136
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11371137
pub const unsafe fn read<T>(src: *const T) -> T {
11381138
// It would be semantically correct to implement this via `copy_nonoverlapping`
1139-
// and `MaybeUninit`, as was done before PR #109035.
1139+
// and `MaybeUninit`, as was done before PR #109035. Calling `assume_init`
1140+
// provides enough information to know that this is a typed operation.
11401141

1141-
// However, it switched to intrinsic that lowers to `_0 = *src` in MIR in
1142-
// order to address a few implementation issues:
1142+
// However, as of March 2023 the compiler was not capable of taking advantage
1143+
// of that information. Thus the implementation here switched to an intrinsic,
1144+
// which lowers to `_0 = *src` in MIR, to address a few issues:
11431145
//
11441146
// - Using `MaybeUninit::assume_init` after a `copy_nonoverlapping` was not
11451147
// turning the untyped copy into a typed load. As such, the generated

0 commit comments

Comments
 (0)