Skip to content

Commit 781f8be

Browse files
authored
Rollup merge of #59130 - RalfJung:non-null, r=rkruppe
Note that NonNull does not launder shared references for mutation See https://users.rust-lang.org/t/relative-pointer-an-abstraction-to-build-movable-self-referential-types/26186/6
2 parents 247aa67 + 8ec8639 commit 781f8be

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libcore/ptr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,16 @@ 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>`]. The same goes for creating a mutable reference from a shared
2882+
/// reference. When using this `From` instance without an `UnsafeCell<T>`,
2883+
/// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
2884+
/// is never used for mutation.
2885+
///
2886+
/// [`UnsafeCell<T>`]: ../cell/struct.UnsafeCell.html
28772887
#[stable(feature = "nonnull", since = "1.25.0")]
28782888
#[repr(transparent)]
28792889
#[rustc_layout_scalar_valid_range_start(1)]

0 commit comments

Comments
 (0)