Skip to content

Commit 1970a3c

Browse files
committed
fix Pointer to reference conversion docs
1 parent bf662eb commit 1970a3c

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

library/core/src/ptr/mod.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,44 @@
5757
//! [`NonNull::dangling`] in such cases.
5858
//!
5959
//! ## Pointer to reference conversion
60-
//! When converting a pointer to a reference `&T` using `&*`,
60+
//!
61+
//! When converting a pointer to a reference (e.g. via `&*ptr` or `&mut *ptr`),
6162
//! there are several rules that must be followed:
6263
//!
6364
//! * The pointer must be properly aligned.
6465
//!
65-
// some microprocessors may use address 0 for an interrupt vector.
66-
// users of these microprocessors must always read/write address 0 through
67-
// a raw pointer, not a reference.
6866
//! * It must be non-null.
6967
//!
7068
//! * It must be "dereferenceable" in the sense defined above.
7169
//!
72-
//! * The pointer must point to a valid value of type `T`.
73-
//! This means that the created reference can only refer to
74-
//! uninitialized memory through careful use of `MaybeUninit`,
75-
//! or if the uninitialized memory is entirely contained within
76-
//! padding bytes, since
77-
//! [padding has the same validity invariant as `MaybeUninit`][ucg-pad].
70+
//! * The pointer must point to a [valid value] of type `T`.
7871
//!
79-
//! * You must enforce Rust's aliasing rules, since the lifetime of the
72+
//! * You must enforce Rust's aliasing rules. The lifetime of the
8073
//! created reference is arbitrarily chosen,
8174
//! and does not necessarily reflect the actual lifetime of the data.
82-
//! In particular, while this reference exists,
83-
//! the memory the pointer points to must
84-
//! not get accessed (read or written) through any raw pointer,
85-
//! except for data inside an `UnsafeCell`.
86-
//! Note that aliased writes are always UB for mutable references,
87-
//! even if they only modify `UnsafeCell` data.
75+
//! The exact aliasing rules are not decided yet, and they depend
76+
//! on whether a mutable or a shared reference is being created.
77+
//! * When creating a mutable reference, then while this reference exists, the memory it points to
78+
//! must not get accessed (read or written) through any other pointer or reference not derived
79+
//! from this reference.
80+
//! * When creating a shared reference, then while this reference exists, the memory it points to
81+
//! must not get mutated (except inside `UnsafeCell`).
8882
//!
8983
//! If a pointer follows all of these rules, it is said to be
90-
//! *convertible to a reference*.
84+
//! *convertible to a (mutable or shared) reference*.
9185
// ^ we use this term instead of saying that the produced reference must
9286
// be valid, as the validity of a reference is easily confused for the
9387
// validity of the thing it refers to, and while the two concepts are
9488
// closly related, they are not identical.
9589
//!
96-
//! These apply even if the result is unused!
90+
//! These rules apply even if the result is unused!
9791
//! (The part about being initialized is not yet fully decided, but until
9892
//! it is, the only safe approach is to ensure that they are indeed initialized.)
9993
//!
10094
//! An example of the implications of the above rules is that an expression such
10195
//! as `unsafe { &*(0 as *const u8) }` is Immediate Undefined Behavior.
10296
//!
103-
//! [ucgpad]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#padding
97+
//! [valid value]: ../../behavior-considered-undefined.html#invalid-values
10498
//!
10599
//! ## Allocated object
106100
//!

0 commit comments

Comments
 (0)