Skip to content

Commit f26f981

Browse files
committed
clarify interactions with MaybeUninit and UnsafeCell
1 parent 394c864 commit f26f981

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Diff for: core/src/ptr/mod.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@
6969
//!
7070
//! * It must be "dereferenceable" in the sense defined above.
7171
//!
72-
//! * The pointer must point to a valid instance of `T`.
72+
//! * The pointer must point to a valid value of type `T`.
7373
//! This means that the created reference can only refer to
74-
//! uninitialized memory through careful use of `MaybeUninit`.
74+
//! uninitialized memory through careful use of `MaybeUninit`,
75+
//! or if the uninitialized memory is entirly contained within
76+
//! padding bytes, since
77+
//! [padding has the same validity invariant as `MaybeUninit`][ucg-pad].
7578
//!
7679
//! * You must enforce Rust's aliasing rules, since the lifetime of the
7780
//! created reference is arbitrarily chosen,
7881
//! and does not necessarily reflect the actual lifetime of the data.
7982
//! In particular, while this reference exists,
8083
//! the memory the pointer points to must
8184
//! not get accessed (read or written) through any raw pointer,
82-
//! except for data inside an `UnsafeCell`
83-
// ^ previous documentation was somewhat unclear on if modifications through
84-
// an UnsafeCell are safe even if they would seemingly violate the exclusivity
85-
// of a mut ref.
85+
//! except for data inside an `UnsafeCell`.
86+
//! Note that aliased writes are always UB for mutable references,
87+
//! even if they only modify `UnsafeCell` data.
8688
//!
8789
//! If a pointer follows all of these rules, it is said to be
8890
//! *convertable to a reference*.
@@ -98,6 +100,8 @@
98100
//! An example of the implications of the above rules is that an expression such
99101
//! as `unsafe { &*(0 as *const u8) }` is Immediate Undefined Behavior.
100102
//!
103+
//! [ucgpad]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#padding
104+
//!
101105
//! ## Allocated object
102106
//!
103107
//! An *allocated object* is a subset of program memory which is addressable

0 commit comments

Comments
 (0)