Skip to content

Replace unsafe usage of NonNull::new_unchecked with Box::into_non_null - #160251

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ArhanChaudhary:nonnull-from-mut
Aug 1, 2026
Merged

Replace unsafe usage of NonNull::new_unchecked with Box::into_non_null#160251
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ArhanChaudhary:nonnull-from-mut

Conversation

@ArhanChaudhary

@ArhanChaudharyArhanChaudhary commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

View all comments

We can avoid unsafe by replacing unsafe { NonNull::new_unchecked(Box::into_raw(..)) } with NonNull::from_mut(Self::leak(..)). A simple test to demonstrate the code generation is equivalent: https://godbolt.org/z/P5q9bzPPK

We can avoid unsafe by replacing unsafe { NonNull::new_unchecked(Box::into_raw(..)) } with the (soon-to-be-stable) Box::into_non_null(..)

Additionally, slightly tweak documentation.

@rustbotrustbot 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 Jul 31, 2026
@rustbot

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 8 candidates

@JohnTitor

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbotrustbot assigned jhpratt and unassigned JohnTitorJul 31, 2026
@asquared31415

Copy link
Copy Markdown
Contributor

Unfortunately, all four cases here must be Box::into_raw, because they are all intended to be used to reconstruct the Box later using Box::from_raw or Box::from_non_null. "un-leaking" a Box is not permitted.

@asquared31415

Copy link
Copy Markdown
Contributor

oh, it might not be forbidden entirely? it appears to be an open question: rust-lang/unsafe-code-guidelines#316

I think that the consensus from that issue is that it has to be sound for the global allocator, but might not be for custom allocators? I'm unsure, someone from opsem should probably weigh in on this.

@jhpratt

Copy link
Copy Markdown
Member

cc @rust-lang/opsem ^^

Personally, the PR looks fine as far as semantics, but that's not my area of expertise. I have to say that I do find the unsafe version clearer, as leak feels a bit odd here.

@saethlin

Copy link
Copy Markdown
Member

In addition to the above, there was recent discussion about this topic on Zulip: #t-opsem > Custom allocators vs Box::leak and "unleaking"

@saethlin

Copy link
Copy Markdown
Member

It's pretty scary that clippy is now suggesting the more dangerous pattern. I would much prefer the lint be removed from clippy until the open questions are settled.

@ArhanChaudhary

ArhanChaudhary commented Aug 1, 2026

Copy link
Copy Markdown
ContributorAuthor

It's pretty scary that clippy is now suggesting the more dangerous pattern. I would much prefer the lint be removed from clippy until the open questions are settled.

Haha... I know right. Whoever contributed that lint clearly should have thought about it more.

( it was me :3 )

@saethlin

Copy link
Copy Markdown
Member

Yeah, the lint looks reasonable and I did see you cite a comment that predates rust-lang/unsafe-code-guidelines#316, and the opsem implications aren't obvious from the code because the impact isn't local. And we don't backlink to all previous discussions of a topic when we find a lurking problem.

@RalfJung

Copy link
Copy Markdown
Member

Agreed with @saethlin . Please use Box::into_non_null.

@ArhanChaudhary

Copy link
Copy Markdown
ContributorAuthor

Thank you advising. How does this look?

@ArhanChaudharyArhanChaudhary changed the title Replace unsafe usage of NonNull::new_unchecked with NonNull::from_mutReplace unsafe usage of NonNull::new_unchecked with Box::into_non_nullAug 1, 2026
@jhpratt

Copy link
Copy Markdown
Member

r=me if there's no opsem concerns.

Comment threadlibrary/alloc/src/boxed.rs Outdated
Comment threadlibrary/core/src/ptr/non_null.rs Outdated
Comment threadlibrary/core/src/ptr/non_null.rs Outdated
@ArhanChaudhary

Copy link
Copy Markdown
ContributorAuthor

Thank you! How about now?

@RalfJung

Copy link
Copy Markdown
Member

LGTM, thanks!
@bors squash

@rust-bors

This comment has been minimized.

…null`
* replace unsafe usage of `NonNull::new_unchecked` with `NonNull::from_mut`
* apply review feedback
* apply review feedback again!
@rust-bors

rust-borsBot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🔨 3 commits were squashed into c2d84bc.

@RalfJung

Copy link
Copy Markdown
Member

@bors r=jhpratt,RalfJung rollup

@rust-bors

rust-borsBot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c2d84bc has been approved by jhpratt,RalfJung

It is now in the queue for this repository.

@rust-borsrust-borsBot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 1, 2026
@rust-borsrust-borsBot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 1, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 1, 2026
…=jhpratt,RalfJung
Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`
~~We can avoid unsafe by replacing `unsafe { NonNull::new_unchecked(Box::into_raw(..)) }` with `NonNull::from_mut(Self::leak(..))`. A simple test to demonstrate the code generation is equivalent: https://godbolt.org/z/P5q9bzPPK~~
We can avoid unsafe by replacing `unsafe { NonNull::new_unchecked(Box::into_raw(..)) }` with the (soon-to-be-stable) `Box::into_non_null(..)`
Additionally, slightly tweak documentation.
rust-borsBot pushed a commit that referenced this pull request Aug 1, 2026
…uwer
Rollup of 10 pull requests
Successful merges:
- #157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
- #160012 (miri: ensure validity of references and pointers we dereference and cast)
- #160294 (Update Enzyme to resolve one of the open bugs)
- #159503 (allocations: document that they can be read-only)
- #160250 (When issuing suggestions for missing trait items, label unstable items)
- #160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
- #160311 (Remove final use of sealed traits from stdlib)
- #160313 (Make the noundef-on-Cast size guard explicit)
- #160323 (Box::leak: tell people to avoid unleaking)
- #160328 (Move `check_track_caller` into the attribute parser)
rust-borsBot pushed a commit that referenced this pull request Aug 1, 2026
…uwer
Rollup of 12 pull requests
Successful merges:
- #157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
- #160012 (miri: ensure validity of references and pointers we dereference and cast)
- #160294 (Update Enzyme to resolve one of the open bugs)
- #159503 (allocations: document that they can be read-only)
- #160179 (std: Update `wasip3` crate dependency)
- #160250 (When issuing suggestions for missing trait items, label unstable items)
- #160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
- #160311 (Remove final use of sealed traits from stdlib)
- #160313 (Make the noundef-on-Cast size guard explicit)
- #160323 (Box::leak: tell people to avoid unleaking)
- #160328 (Move `check_track_caller` into the attribute parser)
- #160333 (Remove itertools dependency from `rustc_ast_pretty`)
@rust-bors
rust-borsBot merged commit ac499eb into rust-lang:mainAug 1, 2026
13 checks passed
@rustbotrustbot added this to the 1.99.0 milestone Aug 1, 2026
rust-timer added a commit that referenced this pull request Aug 1, 2026
Rollup merge of #160251 - ArhanChaudhary:nonnull-from-mut, r=jhpratt,RalfJung
Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`
~~We can avoid unsafe by replacing `unsafe { NonNull::new_unchecked(Box::into_raw(..)) }` with `NonNull::from_mut(Self::leak(..))`. A simple test to demonstrate the code generation is equivalent: https://godbolt.org/z/P5q9bzPPK~~
We can avoid unsafe by replacing `unsafe { NonNull::new_unchecked(Box::into_raw(..)) }` with the (soon-to-be-stable) `Box::into_non_null(..)`
Additionally, slightly tweak documentation.
pullBot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Aug 2, 2026
…uwer
Rollup of 12 pull requests
Successful merges:
- rust-lang/rust#157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
- rust-lang/rust#160012 (miri: ensure validity of references and pointers we dereference and cast)
- rust-lang/rust#160294 (Update Enzyme to resolve one of the open bugs)
- rust-lang/rust#159503 (allocations: document that they can be read-only)
- rust-lang/rust#160179 (std: Update `wasip3` crate dependency)
- rust-lang/rust#160250 (When issuing suggestions for missing trait items, label unstable items)
- rust-lang/rust#160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
- rust-lang/rust#160311 (Remove final use of sealed traits from stdlib)
- rust-lang/rust#160313 (Make the noundef-on-Cast size guard explicit)
- rust-lang/rust#160323 (Box::leak: tell people to avoid unleaking)
- rust-lang/rust#160328 (Move `check_track_caller` into the attribute parser)
- rust-lang/rust#160333 (Remove itertools dependency from `rustc_ast_pretty`)
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 17, 2026
…uwer
Rollup of 12 pull requests
Successful merges:
- rust-lang/rust#157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
- rust-lang/rust#160012 (miri: ensure validity of references and pointers we dereference and cast)
- rust-lang/rust#160294 (Update Enzyme to resolve one of the open bugs)
- rust-lang/rust#159503 (allocations: document that they can be read-only)
- rust-lang/rust#160179 (std: Update `wasip3` crate dependency)
- rust-lang/rust#160250 (When issuing suggestions for missing trait items, label unstable items)
- rust-lang/rust#160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
- rust-lang/rust#160311 (Remove final use of sealed traits from stdlib)
- rust-lang/rust#160313 (Make the noundef-on-Cast size guard explicit)
- rust-lang/rust#160323 (Box::leak: tell people to avoid unleaking)
- rust-lang/rust#160328 (Move `check_track_caller` into the attribute parser)
- rust-lang/rust#160333 (Remove itertools dependency from `rustc_ast_pretty`)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@ArhanChaudhary@rustbot@JohnTitor@asquared31415@jhpratt@saethlin@RalfJung