Skip to content

Commit 85d15d2

Browse files
authored
Rollup merge of #129653 - RalfJung:addr-of-read-only, r=scottmcm
clarify that addr_of creates read-only pointers Stacked Borrows does make this UB, but Tree Borrows does not. This is tied up with #56604 and other UCG discussions. Also see [this collection of links](Rust-for-Linux/linux#950 (comment)) where rustc treats `addr_of!` as a "non-mutating use". So, let's better be careful for now.
2 parents 3daa015 + b5bd0fe commit 85d15d2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

library/core/src/ptr/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2277,6 +2277,14 @@ impl<F: FnPtr> fmt::Debug for F {
22772277
/// `addr_of!(expr)` is equivalent to `&raw const expr`. The macro is *soft-deprecated*;
22782278
/// use `&raw const` instead.
22792279
///
2280+
/// It is still an open question under which conditions writing through an `addr_of!`-created
2281+
/// pointer is permitted. If the place `expr` evaluates to is based on a raw pointer, then the
2282+
/// result of `addr_of!` inherits all permissions from that raw pointer. However, if the place is
2283+
/// based on a reference, local variable, or `static`, then until all details are decided, the same
2284+
/// rules as for shared references apply: it is UB to write through a pointer created with this
2285+
/// operation, except for bytes located inside an `UnsafeCell`. Use `&raw mut` (or [`addr_of_mut`])
2286+
/// to create a raw pointer that definitely permits mutation.
2287+
///
22802288
/// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
22812289
/// and points to initialized data. For cases where those requirements do not hold,
22822290
/// raw pointers should be used instead. However, `&expr as *const _` creates a reference

0 commit comments

Comments
 (0)