Skip to content

Improve the documentation around layout gurantees given by Box#155597

Open
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:docs/better_box_layout_docs
Open

Improve the documentation around layout gurantees given by Box#155597
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:docs/better_box_layout_docs

Conversation

@weiznich
Copy link
Copy Markdown
Contributor

This change extends the memory layout section in the documentation of Box to explicitly state that it is sound to convert between Box<A> and Box<B> as long as it is sound to convert between A and B directly.

See
https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Is.20transmuting.20between.20.60Box.3CA.3E.60.20and.20.60Box.3CB.3E.60.20UB.3F/with/585350243 for the relevant discussion.

@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 Apr 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 6 candidates
  • Random selection from Mark-Simulacrum, jhpratt

//! convert between `Box<A>` and `Box<B>` using unsafe code if its sound to do the same conversion
//! between `A` and `B`.
//!
//! For zero-sized values, the `Box` pointer has to be non-null and sufficiently aligned. The
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 21, 2026

Choose a reason for hiding this comment

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

The existing text has two paragraphs covering the two cases "non-zero-sized" and "zero-sized". Please don't break that structure by adding an entirely unrelated paragraph in the middle.

View changes since the review

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.

I put it a bit further down. If that's still a bad location please suggest a better one.

Comment thread library/alloc/src/boxed.rs Outdated
//! obtained from [`Box::<T>::into_raw`] may be deallocated using the [`Global`] allocator with
//! [`Layout::for_value(&*value)`].
//!
//! It can be assumed that the layout of `Box<A>` is compatible to the layout of `Box<B>` if
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 21, 2026

Choose a reason for hiding this comment

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

I'm a bit concerned that we have never defined what "compatible layout" actually means. I don't think we are using it in existing docs, are we?

View changes since the review

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.

What about going with "The layout is the same for Box<A> and Box<B> if it's the same for A and B instead?

Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 22, 2026

Choose a reason for hiding this comment

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

That uses a different word but doesn't fix the problem that we haven't actually defined the terms you are using here.

Fundamentally the problem is that we have basically no documentation of the sort you want to add, and adding the first pieces of such documentation needs a lot of infrastructure (like term definitions) to be established. Cc @traviscross @ehuss

Also it occurs to me that it seems odd to make this guarantee for Box but not for raw pointers or references.

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.

This is using the wording out of the nomicon:

When transmuting between different compound types, you have to make sure they are laid out the same way! If layouts differ, the wrong fields are going to get filled with the wrong data, which will make you unhappy and can also be Undefined Behavior (see above).

So how do you know if the layouts are the same? For repr(C) types and repr(transparent) types, layout is precisely defined. But for your run-of-the-mill repr(Rust), it is not. Even different instances of the same generic type can have wildly different layout. Vec and Vec might have their fields in the same order, or they might not. The details of what exactly is and is not guaranteed for data layout are still being worked out over at the UCG WG.

For me that reads like there is some existing usage of "the same layout" defined there, so I'm rather surprised to hear that "same layout" is not defined yet.

This change extends the memory layout section in the documentation of  `Box`
to explicitly state that it is sound to convert between `Box<A>` and
`Box<B>` as long as it is sound to convert between `A` and `B` directly.

See
https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Is.20transmuting.20between.20.60Box.3CA.3E.60.20and.20.60Box.3CB.3E.60.20UB.3F/with/585350243
for the relevant discussion.
@weiznich weiznich force-pushed the docs/better_box_layout_docs branch from b17e07b to ad7e61c Compare April 22, 2026 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants