Skip to content

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

Open
ArhanChaudhary wants to merge 1 commit into
rust-lang:mainfrom
ArhanChaudhary:nonnull-from-mut
Open

Replace unsafe usage of NonNull::new_unchecked with Box::into_non_null#160251
ArhanChaudhary wants to merge 1 commit into
rust-lang:mainfrom
ArhanChaudhary:nonnull-from-mut

Conversation

@ArhanChaudhary

@ArhanChaudhary ArhanChaudhary 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.

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

rustbot commented Jul 31, 2026

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

@rustbot rustbot assigned jhpratt and unassigned JohnTitor Jul 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

jhpratt commented Aug 1, 2026

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

saethlin commented Aug 1, 2026

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

saethlin commented Aug 1, 2026

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
Contributor Author

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

saethlin commented Aug 1, 2026

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

RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member

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

@ArhanChaudhary

Copy link
Copy Markdown
Contributor Author

Thank you advising. How does this look?

@ArhanChaudhary ArhanChaudhary changed the title Replace unsafe usage of NonNull::new_unchecked with NonNull::from_mut Replace unsafe usage of NonNull::new_unchecked with Box::into_non_null Aug 1, 2026
@jhpratt

jhpratt commented Aug 1, 2026

Copy link
Copy Markdown
Member

r=me if there's no opsem concerns.

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

Copy link
Copy Markdown
Contributor Author

Thank you! How about now?

@RalfJung

RalfJung commented Aug 1, 2026

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-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🔨 3 commits were squashed into c2d84bc.

@rust-bors
rust-bors Bot force-pushed the nonnull-from-mut branch from 0151071 to c2d84bc Compare August 1, 2026 09:45
@RalfJung

RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member

@bors r=jhpratt,RalfJung rollup

@rust-bors

rust-bors Bot 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-bors rust-bors Bot 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-bors rust-bors Bot 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-bors Bot 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)
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-bors Bot 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`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants