Skip to content

Commit 0ded400

Browse files
committed
Auto merge of rust-lang#80290 - RalfJung:less-intrinsic-write, r=lcnr
implement ptr::write without dedicated intrinsic This makes `ptr::write` more consistent with `ptr::write_unaligned`, `ptr::read`, `ptr::read_unaligned`, all of which are implemented in terms of `copy_nonoverlapping`. This means we can also remove `move_val_init` implementations in codegen and Miri, and its special handling in the borrow checker. Also see [this Zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/ptr.3A.3Aread.20vs.20ptr.3A.3Awrite).
2 parents e6ad3bc + 12f66c5 commit 0ded400

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

core/src/intrinsics.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,6 @@ extern "rust-intrinsic" {
768768
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
769769
pub fn size_of<T>() -> usize;
770770

771-
/// Moves a value to an uninitialized memory location.
772-
///
773-
/// Drop glue is not run on the destination.
774-
///
775-
/// The stabilized version of this intrinsic is [`core::ptr::write`](crate::ptr::write).
776-
pub fn move_val_init<T>(dst: *mut T, src: T);
777-
778771
/// The minimum alignment of a type.
779772
///
780773
/// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of).

core/src/ptr/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,19 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
883883
#[inline]
884884
#[stable(feature = "rust1", since = "1.0.0")]
885885
pub unsafe fn write<T>(dst: *mut T, src: T) {
886-
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
887-
// Not panicking to keep codegen impact smaller.
888-
abort();
886+
// We are calling the intrinsics directly to avoid function calls in the generated code
887+
// as `intrinsics::copy_nonoverlapping` is a wrapper function.
888+
extern "rust-intrinsic" {
889+
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
890+
}
891+
892+
// SAFETY: the caller must guarantee that `dst` is valid for writes.
893+
// `dst` cannot overlap `src` because the caller has mutable access
894+
// to `dst` while `src` is owned by this function.
895+
unsafe {
896+
copy_nonoverlapping(&src as *const T, dst, 1);
897+
intrinsics::forget(src);
889898
}
890-
// SAFETY: the caller must uphold the safety contract for `move_val_init`.
891-
unsafe { intrinsics::move_val_init(&mut *dst, src) }
892899
}
893900

894901
/// Overwrites a memory location with the given value without reading or

0 commit comments

Comments
 (0)