Skip to content

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

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
RalfJung:replace-zst-null-ptr
Feb 25, 2026
Merged

ptr::replace: make calls on ZST null ptr not UB#149169
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
RalfJung:replace-zst-null-ptr

Conversation

@RalfJung
Copy link
Member

See #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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2025

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

@RalfJung RalfJung force-pushed the replace-zst-null-ptr branch from bb81fb2 to 43aa6a7 Compare November 21, 2025 12:07
@RalfJung
Copy link
Member Author

RalfJung commented Dec 7, 2025

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

@RalfJung
Copy link
Member Author

r? libs

@Amanieu
Copy link
Member

Amanieu commented Feb 14, 2026

@rfcbot merge libs-api

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Feb 14, 2026

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Feb 14, 2026
@Amanieu Amanieu added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Feb 14, 2026
@rust-rfcbot rust-rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Feb 14, 2026
@rust-rfcbot
Copy link
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@rust-rfcbot rust-rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Feb 14, 2026
@Mark-Simulacrum
Copy link
Member

r=me with FCP complete

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2026
@the8472
Copy link
Member

the8472 commented Feb 14, 2026

This doesn't seem like a API change to me, other than by implication of how #138351 gets addressed and what we might have to tell users about it, but it looks like #152615 is the one that's trying to update the documentation for that.

@RalfJung
Copy link
Member Author

Yeah technically this PR just changes the implementation, the API already got changed a while ago.

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Feb 17, 2026
@RalfJung RalfJung force-pushed the replace-zst-null-ptr branch from 43aa6a7 to 2484cfe Compare February 19, 2026 07:39
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-rfcbot rust-rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Feb 24, 2026
@rust-rfcbot rust-rfcbot added to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 24, 2026
@rust-rfcbot
Copy link
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@RalfJung
Copy link
Member Author

@bors r=Mark-Simulacrum

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 24, 2026

📌 Commit 2484cfe has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 24, 2026
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 24, 2026
…ark-Simulacrum

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

See rust-lang#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.
rust-bors bot pushed a commit that referenced this pull request Feb 25, 2026
Rollup of 8 pull requests

Successful merges:

 - #149169 (ptr::replace: make calls on ZST null ptr not UB)
 - #150562 (Fix doc link used in suggestion for pinning self)
 - #152679 (rustc_expand: improve diagnostics for non-repeatable metavars)
 - #153017 (Implement debuginfo for unsafe binder types)
 - #152868 (delete some very old trivial `Box` tests)
 - #152922 (rustc_public: Make fields that shouldn't be exposed visible only in `rustc_public`)
 - #153029 (Rename `rustc::pass_by_value` lint as `rustc::disallowed_pass_by_ref`.)
 - #153051 (Migration of `LintDiagnostic` - part 3)
rust-bors bot pushed a commit that referenced this pull request Feb 25, 2026
Rollup of 12 pull requests

Successful merges:

 - #149169 (ptr::replace: make calls on ZST null ptr not UB)
 - #150562 (Fix doc link used in suggestion for pinning self)
 - #152418 (`BTreeMap::merge` optimized)
 - #152679 (rustc_expand: improve diagnostics for non-repeatable metavars)
 - #152952 (mGCA: improve ogca diagnostic message )
 - #152977 (Fix relative path handling for --extern-html-root-url)
 - #153017 (Implement debuginfo for unsafe binder types)
 - #152868 (delete some very old trivial `Box` tests)
 - #152922 (rustc_public: Make fields that shouldn't be exposed visible only in `rustc_public`)
 - #153032 (Fix attribute parser and kind names.)
 - #153051 (Migration of `LintDiagnostic` - part 3)
 - #153060 (Give a better error when updating a submodule fails)
@rust-bors rust-bors bot merged commit 651db87 into rust-lang:main Feb 25, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 25, 2026
rust-timer added a commit that referenced this pull request Feb 25, 2026
Rollup merge of #149169 - RalfJung:replace-zst-null-ptr, r=Mark-Simulacrum

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

See #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.
@Kobzol
Copy link
Member

Kobzol commented Feb 25, 2026

@rust-timer build 6e1951f

For #153074.

@rust-timer
Copy link
Collaborator

Missing artifact for sha 6e1951f11c05f55619bc18afac75d8827ca127a9 (https://ci-artifacts.rust-lang.org/rustc-builds/6e1951f11c05f55619bc18afac75d8827ca127a9/rustc-nightly-x86_64-unknown-linux-gnu.tar.xz); not built yet, try again later.

@JonathanBrouwer
Copy link
Contributor

@rust-timer build 6e1951f

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6e1951f): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.5% [0.5%, 0.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -1.0%, secondary -4.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.1% [2.5%, 3.7%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.8% [-4.2%, -3.6%] 3
Improvements ✅
(secondary)
-4.1% [-4.1%, -4.1%] 1
All ❌✅ (primary) -1.0% [-4.2%, 3.7%] 5

Cycles

Results (secondary -3.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.5% [-3.5%, -3.5%] 1
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 482.035s -> 480.395s (-0.34%)
Artifact size: 397.81 MiB -> 397.78 MiB (-0.01%)

@JonathanBrouwer
Copy link
Contributor

Noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.