Skip to content

Commit 17d0a45

Browse files
committed
Auto merge of #117572 - RalfJung:addr_of, r=cuviper
update and clarify addr_of docs This updates the docs to match rust-lang/reference#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 #114902, so Cc `@joshlf`
2 parents 0a1e559 + e30f8ae commit 17d0a45

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

library/core/src/ptr/mod.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -1999,9 +1999,18 @@ impl<F: FnPtr> fmt::Debug for F {
19991999
/// as all other references. This macro can create a raw pointer *without* creating
20002000
/// a reference first.
20012001
///
2002-
/// Note, however, that the `expr` in `addr_of!(expr)` is still subject to all
2003-
/// the usual rules. In particular, `addr_of!(*ptr::null())` is Undefined
2004-
/// Behavior because it dereferences a null pointer.
2002+
/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads
2003+
/// from the place or requires the place to be dereferenceable. This means that
2004+
/// `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2005+
/// Note however that `addr_of!((*ptr).field)` still requires the projection to
2006+
/// `field` to be in-bounds, using the same rules as [`offset`].
2007+
///
2008+
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
2009+
/// `addr_of!` like everywhere else, in which case a reference is created to call `Deref::deref` or
2010+
/// `Index::index`, respectively. The statements above only apply when no such coercions are
2011+
/// applied.
2012+
///
2013+
/// [`offset`]: pointer::offset
20052014
///
20062015
/// # Example
20072016
///
@@ -2039,9 +2048,18 @@ pub macro addr_of($place:expr) {
20392048
/// as all other references. This macro can create a raw pointer *without* creating
20402049
/// a reference first.
20412050
///
2042-
/// Note, however, that the `expr` in `addr_of_mut!(expr)` is still subject to all
2043-
/// the usual rules. In particular, `addr_of_mut!(*ptr::null_mut())` is Undefined
2044-
/// Behavior because it dereferences a null pointer.
2051+
/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads
2052+
/// from the place or requires the place to be dereferenceable. This means that
2053+
/// `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2054+
/// Note however that `addr_of_mut!((*ptr).field)` still requires the projection to
2055+
/// `field` to be in-bounds, using the same rules as [`offset`].
2056+
///
2057+
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
2058+
/// `addr_of_mut!` like everywhere else, in which case a reference is created to call `Deref::deref`
2059+
/// or `Index::index`, respectively. The statements above only apply when no such coercions are
2060+
/// applied.
2061+
///
2062+
/// [`offset`]: pointer::offset
20452063
///
20462064
/// # Examples
20472065
///

0 commit comments

Comments
 (0)