rust icon indicating copy to clipboard operation
rust copied to clipboard

ptr::replace: make calls on ZST null ptr not UB

Open RalfJung opened this issue 1 month ago • 2 comments

See https://github.com/rust-lang/rust/issues/138351 for context.

We made ptr::read and ptr::write not UB on ZST null pointers. This does the same with ptr::replace. Since we're just adding a branch on a constant, this should come at no runtime cost.

RalfJung avatar Nov 21 '25 07:11 RalfJung

r? @ibraheemdev

rustbot has assigned @ibraheemdev. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar Nov 21 '25 07:11 rustbot

@rust-lang/libs any thoughts on this? Independent of what exactly we decide in https://github.com/rust-lang/rust/issues/138351, this seems like a basically free way to make replace have less UB and be more consistent with read and write.

RalfJung avatar Dec 07 '25 08:12 RalfJung