Skip to content

Commit e7c468d

Browse files
committed
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.
1 parent 1851f08 commit e7c468d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/alloc/src/boxed.rs

Lines changed: 14 additions & 0 deletions
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 uniqeness 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)