Skip to content

fix: add aliasing rules for Box#146870

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
DiuDiu777:box-doc-fix
Apr 18, 2026
Merged

fix: add aliasing rules for Box#146870
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
DiuDiu777:box-doc-fix

Conversation

@DiuDiu777
Copy link
Copy Markdown
Contributor

This is a new revised version for the PR 139857, sorry for the delayed reply. I've rewritten the sentence to closely mirror the wording from Vec::from_raw_parts, which clearly states the transfer of ownership and its consequences. This should make the aliasing requirements much clearer.

I opted not to include a note about mem::forget by default to keep the documentation focused on the primary contract, similar to Vec.

@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 Sep 22, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Sep 22, 2025

r? @Mark-Simulacrum

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

Comment thread library/alloc/src/boxed.rs Outdated
/// The ownership of `raw` is effectively transferred to the resulting
/// `Box<T>` which may then deallocate or change the contents of memory
/// pointed to by the pointer at will. You must ensure that nothing else uses
/// the pointer after calling this function.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @RalfJung mentions in the other PR, this is perhaps too strict a condition with regards to forget (or not strict enough).

I'm also not sure if we've settled on this rule -- maybe this should instead link to https://doc.rust-lang.org/nightly/std/boxed/index.html#considerations-for-unsafe-code? That also notes the disclaimer that Box may be less restrictive than this makes it out to be.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. In fact, the interaction with forget was a key point of uncertainty in my previous changes.

I agree with the method of adding the link to the "considerations for unsafe code" section. But the forget concerns do not seem to be clearly indicated in this link either. Should I also explicitly clarify the rules regarding mem::forget in this PR?

If I do this for Box, should I also update the documentation for APIs like Vec::from_raw_parts for consistency?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, this sentence would rule out code like this which is accepted by Miri:

let ptr = Box::into_raw(Box::new(0));
mem::forget(Box::from_raw(ptr));
drop(Box::from_raw(ptr));

Whether we want to allow such code or not is not something we have decided I think. The existing docs already rule out code like this which is accepted by Miri:

let mut a = 0;
mem::forget(Box::from_raw(&raw mut a));

That said, the corresponding Vec docs you mention are also quite restrictive here and don't allow anything like that. Vec isn't even subject to aliasing requirements. The docs there are written like this entirely because the destructor frees the memory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RalfJung Thanks for your detailed clarification! I can get your point.

As for the next step, can I add a note to the documentation mentioning that certain uses with mem::forget (like the examples you provided) are allowed? Or do you have a better suggestion for how to address this?

@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 Sep 28, 2025
@SpriteOvO
Copy link
Copy Markdown
Member

Hi, ping from triage team. This PR has been inactive for a while. Are there any updates on this?

@rustbot

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 30, 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-log-analyzer

This comment has been minimized.

@DiuDiu777
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 9, 2026
Comment thread library/alloc/src/boxed.rs Outdated
///
/// The safety conditions are described in the [memory layout] section.
/// Additionally, transferring ownership of the pointer
/// to the `Box<T>` introduces specific aliasing rules. Please see the
Copy link
Copy Markdown
Member

@Mark-Simulacrum Mark-Simulacrum Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

Strictly speaking, the ownership transfer is of the "memory", not the pointer (which is Copy).

Maybe we can just say "Note that the [considerations for unsafe code] apply to all Box<T> values"? That seems like it's both sufficient and avoids needing to get into much detail here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that's more straightforward. I'll update it~

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 11, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 11, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 11, 2026
@DiuDiu777
Copy link
Copy Markdown
Contributor Author

@rustbot ready

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

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 18, 2026

📌 Commit c6befd8 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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 18, 2026
…lacrum

fix: add aliasing rules for Box

This is a new revised version for the PR [139857](rust-lang#139857), sorry for the delayed reply. I've rewritten the sentence to closely mirror the wording from `Vec::from_raw_parts`, which clearly states the transfer of ownership and its consequences. This should make the aliasing requirements much clearer.

I opted not to include a note about `mem::forget` by default to keep the documentation focused on the primary contract, similar to `Vec`.
rust-bors Bot pushed a commit that referenced this pull request Apr 18, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #150230 (spec next chunk for trustedlen)
 - #155284 (net::tcp/udp: fix docs about how set_nonblocking is implemented)
 - #146870 (fix: add aliasing rules for Box)
 - #154003 (Make std::fs::File Send on UEFI)
 - #155187 (std: fix HashMap RNG docs wording)
 - #155255 (Document why `layout.align() + layout.size()` doesn't overflow)
 - #155351 (Reorganize tests from `tests/ui/issues/`)
 - #155406 (std: Update dependency on `wasi` crate)
 - #155447 (Simplify `parse_limited`)
 - #155481 (Delete `SizeSkeleton::Generic`)
@rust-bors rust-bors Bot merged commit 92a2343 into rust-lang:main Apr 18, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 18, 2026
rust-timer added a commit that referenced this pull request Apr 18, 2026
Rollup merge of #146870 - DiuDiu777:box-doc-fix, r=Mark-Simulacrum

fix: add aliasing rules for Box

This is a new revised version for the PR [139857](#139857), sorry for the delayed reply. I've rewritten the sentence to closely mirror the wording from `Vec::from_raw_parts`, which clearly states the transfer of ownership and its consequences. This should make the aliasing requirements much clearer.

I opted not to include a note about `mem::forget` by default to keep the documentation focused on the primary contract, similar to `Vec`.
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.

6 participants