Skip to content

Commit 7fcdb93

Browse files
committed
Note that NonNull does not launder shared references for mutation
1 parent 7486b9c commit 7fcdb93

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libcore/ptr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,15 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
28742874
/// Usually this won't be necessary; covariance is correct for most safe abstractions,
28752875
/// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they
28762876
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
2877+
///
2878+
/// Notice that `NonNull<T>` has a `From` instance for `&T`. However, this does
2879+
/// not change the fact that mutating through a (pointer derived from a) shared
2880+
/// reference is undefined behavior unless the mutation happens inside an
2881+
/// [`UnsafeCell<T>`]. When using this `From` instance without an `UnsafeCell<T>`,
2882+
/// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
2883+
/// is never used for mutation.
2884+
///
2885+
/// [`UnsafeCell<T>`]: ../cell/struct.UnsafeCell.html
28772886
#[stable(feature = "nonnull", since = "1.25.0")]
28782887
#[repr(transparent)]
28792888
#[rustc_layout_scalar_valid_range_start(1)]

0 commit comments

Comments
 (0)