diff options
| author | bors <bors@rust-lang.org> | 2023-11-10 08:04:47 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-10 08:04:47 +0000 |
| commit | 17d0a45f5df31f218b84c3275bb05c3227348b5b (patch) | |
| tree | f1c5de1f4c2fa63ee4827a4583513947ce5bf758 /library/core/src | |
| parent | 0a1e5598b0c2349d4e7dcb94e69ade13b1a1ed64 (diff) | |
| parent | e30f8ae8672c89ba473aea8662fd5f22992810fb (diff) | |
| download | rust-17d0a45f5df31f218b84c3275bb05c3227348b5b.tar.gz rust-17d0a45f5df31f218b84c3275bb05c3227348b5b.zip | |
Auto merge of #117572 - RalfJung:addr_of, r=cuviper
update and clarify addr_of docs This updates the docs to match https://github.com/rust-lang/reference/pull/1387. Cc `@rust-lang/opsem` `@chorman0773` not sure if you had anything else you wanted to say here, I'd be happy to get your feedback. :) Fixes https://github.com/rust-lang/rust/issues/114902, so Cc `@joshlf`
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/ptr/mod.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 63e42a8784c..d71079dd09c 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1999,9 +1999,18 @@ impl<F: FnPtr> fmt::Debug for F { /// as all other references. This macro can create a raw pointer *without* creating /// a reference first. /// -/// Note, however, that the `expr` in `addr_of!(expr)` is still subject to all -/// the usual rules. In particular, `addr_of!(*ptr::null())` is Undefined -/// Behavior because it dereferences a null pointer. +/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads +/// from the place or requires the place to be dereferenceable. This means that +/// `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned. +/// Note however that `addr_of!((*ptr).field)` still requires the projection to +/// `field` to be in-bounds, using the same rules as [`offset`]. +/// +/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside +/// `addr_of!` like everywhere else, in which case a reference is created to call `Deref::deref` or +/// `Index::index`, respectively. The statements above only apply when no such coercions are +/// applied. +/// +/// [`offset`]: pointer::offset /// /// # Example /// @@ -2039,9 +2048,18 @@ pub macro addr_of($place:expr) { /// as all other references. This macro can create a raw pointer *without* creating /// a reference first. /// -/// Note, however, that the `expr` in `addr_of_mut!(expr)` is still subject to all -/// the usual rules. In particular, `addr_of_mut!(*ptr::null_mut())` is Undefined -/// Behavior because it dereferences a null pointer. +/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads +/// from the place or requires the place to be dereferenceable. This means that +/// `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned. +/// Note however that `addr_of_mut!((*ptr).field)` still requires the projection to +/// `field` to be in-bounds, using the same rules as [`offset`]. +/// +/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside +/// `addr_of_mut!` like everywhere else, in which case a reference is created to call `Deref::deref` +/// or `Index::index`, respectively. The statements above only apply when no such coercions are +/// applied. +/// +/// [`offset`]: pointer::offset /// /// # Examples /// |
