about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-09-05 19:43:47 +0200
committerGitHub <noreply@github.com>2024-09-05 19:43:47 +0200
commit85d15d292d897c6ee618b697cdba73272bb50415 (patch)
tree1abbbe79529fd68dd0aeff1c2fa386e098da9a5c
parent3daa015f825034c47f61d94eef7fbfe8837bfd0b (diff)
parentb5bd0fe48a0427a82804759a5acd795851c65e4b (diff)
downloadrust-85d15d292d897c6ee618b697cdba73272bb50415.tar.gz
rust-85d15d292d897c6ee618b697cdba73272bb50415.zip
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 https://github.com/rust-lang/rust/issues/56604 and other UCG discussions. Also see [this collection of links](https://github.com/Rust-for-Linux/linux/pull/950#discussion_r1104759431) where rustc treats `addr_of!` as a "non-mutating use".

So, let's better be careful for now.
-rw-r--r--library/core/src/ptr/mod.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index d7ed4edcc00..08d06cad55d 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -2277,6 +2277,14 @@ impl<F: FnPtr> fmt::Debug for F {
 /// `addr_of!(expr)` is equivalent to `&raw const expr`. The macro is *soft-deprecated*;
 /// use `&raw const` instead.
 ///
+/// It is still an open question under which conditions writing through an `addr_of!`-created
+/// pointer is permitted. If the place `expr` evaluates to is based on a raw pointer, then the
+/// result of `addr_of!` inherits all permissions from that raw pointer. However, if the place is
+/// based on a reference, local variable, or `static`, then until all details are decided, the same
+/// rules as for shared references apply: it is UB to write through a pointer created with this
+/// operation, except for bytes located inside an `UnsafeCell`. Use `&raw mut` (or [`addr_of_mut`])
+/// to create a raw pointer that definitely permits mutation.
+///
 /// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
 /// and points to initialized data. For cases where those requirements do not hold,
 /// raw pointers should be used instead. However, `&expr as *const _` creates a reference