Skip to content

Commit 9c72f16

Browse files
authored
Rollup merge of #97229 - Nilstrieb:doc-box-noalias, r=dtolnay
Document the current aliasing rules for `Box<T>`. Currently, `Box<T>` gets `noalias`, meaning it has the same rules as `&mut T`. This is sparsely documented, even though it can have quite a big impact on unsafe code using box. Therefore, these rules are documented here, with a big warning that they are not normative and subject to change, since we have not yet committed to an aliasing model and the state of `Box<T>` is especially uncertain. If you have any suggestions and improvements, make sure to leave them here. This is mostly intended to inform people about what is currently going on (to prevent misunderstandings such as [Jon Gjengset's Box aliasing](https://www.youtube.com/watch?v=EY7Wi9fV5bk)). This is supposed to _only document current UB_ and not add any new guarantees or rules.
2 parents 94b8036 + ffd7f58 commit 9c72f16

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/alloc/src/boxed.rs

+14
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,21 @@
122122
//! definition is just using `T*` can lead to undefined behavior, as
123123
//! described in [rust-lang/unsafe-code-guidelines#198][ucg#198].
124124
//!
125+
//! # Considerations for unsafe code
126+
//!
127+
//! **Warning: This section is not normative and is subject to change, possibly
128+
//! being relaxed in the future! It is a simplified summary of the rules
129+
//! currently implemented in the compiler.**
130+
//!
131+
//! The aliasing rules for `Box<T>` are the same as for `&mut T`. `Box<T>`
132+
//! asserts uniqueness over its content. Using raw pointers derived from a box
133+
//! after that box has been mutated through, moved or borrowed as `&mut T`
134+
//! is not allowed. For more guidance on working with box from unsafe code, see
135+
//! [rust-lang/unsafe-code-guidelines#326][ucg#326].
136+
//!
137+
//!
125138
//! [ucg#198]: https://github.com/rust-lang/unsafe-code-guidelines/issues/198
139+
//! [ucg#326]: https://github.com/rust-lang/unsafe-code-guidelines/issues/326
126140
//! [dereferencing]: core::ops::Deref
127141
//! [`Box::<T>::from_raw(value)`]: Box::from_raw
128142
//! [`Global`]: crate::alloc::Global

0 commit comments

Comments
 (0)