Skip to content

Commit 5398b83

Browse files
Expand addr_of[_mut] UB documentation
Explicitly list the things you aren't allowed to do with `expr`, and add a link to the exact definition of a dangling pointer. Fixes #94473
1 parent 3153584 commit 5398b83

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

library/core/src/ptr/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,11 @@ fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
14511451
/// as all other references. This macro can create a raw pointer *without* creating
14521452
/// a reference first.
14531453
///
1454-
/// Note, however, that the `expr` in `addr_of!(expr)` is still subject to all
1455-
/// the usual rules. In particular, `addr_of!(*ptr::null())` is Undefined
1456-
/// Behavior because it dereferences a null pointer.
1454+
/// Note, however, that the `expr` in `addr_of!(expr)` is still subject to other rules:
1455+
/// * A [dangling] pointer cannot be dereferenced. In particular, `addr_of!(*ptr::null_mut())`
1456+
/// is undefined behavior because it dereferences a null pointer.
1457+
/// * An [unaligned] pointer cannot be dereferenced. For example, `addr_of!((*an_unaligned_ptr).field)`
1458+
/// is undefined behavior.
14571459
///
14581460
/// # Example
14591461
///
@@ -1475,6 +1477,9 @@ fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }
14751477
/// See [`addr_of_mut`] for how to create a pointer to unininitialized data.
14761478
/// Doing that with `addr_of` would not make much sense since one could only
14771479
/// read the data, and that would be Undefined Behavior.
1480+
///
1481+
/// [dangling]: ../../reference/behavior-considered-undefined.html#dangling-pointers
1482+
/// [unaligned]: ../../reference/type-layout.html
14781483
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
14791484
#[rustc_macro_transparency = "semitransparent"]
14801485
#[allow_internal_unstable(raw_ref_op)]
@@ -1491,9 +1496,11 @@ pub macro addr_of($place:expr) {
14911496
/// as all other references. This macro can create a raw pointer *without* creating
14921497
/// a reference first.
14931498
///
1494-
/// Note, however, that the `expr` in `addr_of_mut!(expr)` is still subject to all
1495-
/// the usual rules. In particular, `addr_of_mut!(*ptr::null_mut())` is Undefined
1496-
/// Behavior because it dereferences a null pointer.
1499+
/// Note, however, that the `expr` in `addr_of_mut!(expr)` is still subject to other rules:
1500+
/// * A [dangling] pointer cannot be dereferenced. In particular, `addr_of_mut!(*ptr::null_mut())`
1501+
/// is undefined behavior because it dereferences a null pointer.
1502+
/// * An [unaligned] pointer cannot be dereferenced. For example, `addr_of_mut!((*an_unaligned_ptr).field)`
1503+
/// is undefined behavior.
14971504
///
14981505
/// # Examples
14991506
///
@@ -1531,6 +1538,9 @@ pub macro addr_of($place:expr) {
15311538
/// unsafe { f1_ptr.write(true); }
15321539
/// let init = unsafe { uninit.assume_init() };
15331540
/// ```
1541+
///
1542+
/// [dangling]: ../../reference/behavior-considered-undefined.html#dangling-pointers
1543+
/// [unaligned]: ../../reference/type-layout.html
15341544
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
15351545
#[rustc_macro_transparency = "semitransparent"]
15361546
#[allow_internal_unstable(raw_ref_op)]

0 commit comments

Comments
 (0)