diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 6d247681c6661..9c229665c7e91 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1386,7 +1386,7 @@ impl Rc { Self::allocate_for_layout( Layout::for_value(&*ptr), |layout| Global.allocate(layout), - |mem| mem.with_metadata_of(ptr as *mut RcBox), + |mem| mem.with_metadata_of(ptr as *const RcBox), ) } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index df315dad89324..e8d9de4fb3ce5 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1204,7 +1204,7 @@ impl Arc { Self::allocate_for_layout( Layout::for_value(&*ptr), |layout| Global.allocate(layout), - |mem| mem.with_metadata_of(ptr as *mut ArcInner), + |mem| mem.with_metadata_of(ptr as *const ArcInner), ) } } diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index bbcc7c699e036..0bb2566fd4c98 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -80,10 +80,14 @@ impl *mut T { #[unstable(feature = "set_ptr_value", issue = "75091")] #[must_use = "returns a new pointer rather than modifying its argument"] #[inline] - pub fn with_metadata_of(self, mut val: *mut U) -> *mut U + pub fn with_metadata_of(self, val: *const U) -> *mut U where U: ?Sized, { + // Prepare in the type system that we will replace the pointer value with a mutable + // pointer, taking the mutable provenance from the `self` pointer. + let mut val = val as *mut U; + // Pointer to the pointer value within the value. let target = &mut val as *mut *mut U as *mut *mut u8; // SAFETY: In case of a thin pointer, this operations is identical // to a simple assignment. In case of a fat pointer, with the current