Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
// SAFETY: the caller must guarantee that `dst` is valid to be
// cast to a mutable reference (valid for writes, aligned, initialized),
// and cannot overlap `src` since `dst` must point to a distinct
// allocation.
// allocation. We are excluding null (with a ZST check) before creating a reference.
unsafe {
ub_checks::assert_unsafe_precondition!(
check_language_ub,
Expand All @@ -1552,6 +1552,13 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
is_zst: bool = T::IS_ZST,
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
);
if T::IS_ZST {
// `dst` may be valid for read and writes while also being null, in which case we cannot
// call `mem::replace`. However, we also don't have to actually do anything since there
// isn't actually any data to be copied anyway. All values of type `T` are
// bit-identical, so we can just return `src`` here.
return src;
}
mem::replace(&mut *dst, src)
}
}
Expand Down
Loading