Skip to content

Commit 37111ed

Browse files
committed
attempt to further clarify addr_of docs
1 parent ef32456 commit 37111ed

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

library/core/src/ptr/mod.rs

+59-15
Original file line numberDiff line numberDiff line change
@@ -2071,21 +2071,26 @@ impl<F: FnPtr> fmt::Debug for F {
20712071
/// as all other references. This macro can create a raw pointer *without* creating
20722072
/// a reference first.
20732073
///
2074-
/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads
2075-
/// from the place or requires the place to be dereferenceable. This means that
2076-
/// `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2077-
/// Note however that `addr_of!((*ptr).field)` still requires the projection to
2078-
/// `field` to be in-bounds, using the same rules as [`offset`].
2074+
/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads from the
2075+
/// place or requires the place to be dereferenceable. This means that `addr_of!((*ptr).field)`
2076+
/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2077+
/// However, `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
20792078
///
20802079
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
20812080
/// `addr_of!` like everywhere else, in which case a reference is created to call `Deref::deref` or
20822081
/// `Index::index`, respectively. The statements above only apply when no such coercions are
20832082
/// applied.
20842083
///
2084+
/// See [`addr_of_mut`] for how to create a pointer to uninitialized data.
2085+
/// Doing that with `addr_of` would not make much sense since one could only
2086+
/// read the data, and that would be Undefined Behavior.
2087+
///
20852088
/// [`offset`]: pointer::offset
20862089
///
20872090
/// # Example
20882091
///
2092+
/// **Correct usage: Creating a pointer to unaligned data**
2093+
///
20892094
/// ```
20902095
/// use std::ptr;
20912096
///
@@ -2101,9 +2106,27 @@ impl<F: FnPtr> fmt::Debug for F {
21012106
/// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
21022107
/// ```
21032108
///
2104-
/// See [`addr_of_mut`] for how to create a pointer to uninitialized data.
2105-
/// Doing that with `addr_of` would not make much sense since one could only
2106-
/// read the data, and that would be Undefined Behavior.
2109+
/// **Incorrect usage: Out-of-bounds fields projection**
2110+
///
2111+
/// ```rust,no_run
2112+
/// use std::ptr;
2113+
///
2114+
/// #[repr(C)]
2115+
/// struct MyStruct {
2116+
/// field1: i32,
2117+
/// field2: i32,
2118+
/// }
2119+
///
2120+
/// let ptr: *const MyStruct = ptr::null();
2121+
/// let fieldptr = unsafe { ptr::addr_of!((*ptr).field2) }; // Undefined Behavior ⚠️
2122+
/// ```
2123+
///
2124+
/// The field projection `.field2` would offset the pointer by 4 bytes,
2125+
/// but the pointer is not in-bounds of an allocation for 4 bytes,
2126+
/// so this offset is Undefined Behavior.
2127+
/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2128+
/// same requirements apply to field projections, even inside `addr_of!`. (In particular, it makes
2129+
/// no difference whether the pointer is null or dangling.)
21072130
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
21082131
#[rustc_macro_transparency = "semitransparent"]
21092132
#[allow_internal_unstable(raw_ref_op)]
@@ -2120,11 +2143,10 @@ pub macro addr_of($place:expr) {
21202143
/// as all other references. This macro can create a raw pointer *without* creating
21212144
/// a reference first.
21222145
///
2123-
/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads
2124-
/// from the place or requires the place to be dereferenceable. This means that
2125-
/// `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2126-
/// Note however that `addr_of_mut!((*ptr).field)` still requires the projection to
2127-
/// `field` to be in-bounds, using the same rules as [`offset`].
2146+
/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads from the
2147+
/// place or requires the place to be dereferenceable. This means that `addr_of_mut!((*ptr).field)`
2148+
/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2149+
/// However, `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
21282150
///
21292151
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
21302152
/// `addr_of_mut!` like everywhere else, in which case a reference is created to call `Deref::deref`
@@ -2135,7 +2157,7 @@ pub macro addr_of($place:expr) {
21352157
///
21362158
/// # Examples
21372159
///
2138-
/// **Creating a pointer to unaligned data:**
2160+
/// **Correct usage: Creating a pointer to unaligned data**
21392161
///
21402162
/// ```
21412163
/// use std::ptr;
@@ -2153,7 +2175,7 @@ pub macro addr_of($place:expr) {
21532175
/// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference.
21542176
/// ```
21552177
///
2156-
/// **Creating a pointer to uninitialized data:**
2178+
/// **Correct usage: Creating a pointer to uninitialized data**
21572179
///
21582180
/// ```rust
21592181
/// use std::{ptr, mem::MaybeUninit};
@@ -2169,6 +2191,28 @@ pub macro addr_of($place:expr) {
21692191
/// unsafe { f1_ptr.write(true); }
21702192
/// let init = unsafe { uninit.assume_init() };
21712193
/// ```
2194+
///
2195+
/// **Incorrect usage: Out-of-bounds fields projection**
2196+
///
2197+
/// ```rust,no_run
2198+
/// use std::ptr;
2199+
///
2200+
/// #[repr(C)]
2201+
/// struct MyStruct {
2202+
/// field1: i32,
2203+
/// field2: i32,
2204+
/// }
2205+
///
2206+
/// let ptr: *mut MyStruct = ptr::null_mut();
2207+
/// let fieldptr = unsafe { ptr::addr_of_mut!((*ptr).field2) }; // Undefined Behavior ⚠️
2208+
/// ```
2209+
///
2210+
/// The field projection `.field2` would offset the pointer by 4 bytes,
2211+
/// but the pointer is not in-bounds of an allocation for 4 bytes,
2212+
/// so this offset is Undefined Behavior.
2213+
/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2214+
/// same requirements apply to field projections, even inside `addr_of_mut!`. (In particular, it
2215+
/// makes no difference whether the pointer is null or dangling.)
21722216
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
21732217
#[rustc_macro_transparency = "semitransparent"]
21742218
#[allow_internal_unstable(raw_ref_op)]

0 commit comments

Comments
 (0)