Skip to content

Commit 3c66e8b

Browse files
committed
Rollup merge of rust-lang#52508 - joshtriplett:dangling-not-sentinel, r=Mark-Simulacrum
Document that Unique::empty() and NonNull::dangling() aren't sentinel values The documentation of Unique::empty() and NonNull::dangling() could potentially suggest that they work as sentinel values indicating a not-yet-initialized pointer. However, they both declare a non-null pointer equal to the alignment of the type, which could potentially reference a valid value of that type (specifically, the first such valid value in memory). Explicitly document that the return value of these functions does not work as a sentinel value.
2 parents 51f50ad + ce75632 commit 3c66e8b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libcore/ptr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2716,6 +2716,11 @@ impl<T: Sized> Unique<T> {
27162716
///
27172717
/// This is useful for initializing types which lazily allocate, like
27182718
/// `Vec::new` does.
2719+
///
2720+
/// Note that the pointer value may potentially represent a valid pointer to
2721+
/// a `T`, which means this must not be used as a "not yet initialized"
2722+
/// sentinel value. Types that lazily allocate must track initialization by
2723+
/// some other means.
27192724
// FIXME: rename to dangling() to match NonNull?
27202725
pub const fn empty() -> Self {
27212726
unsafe {
@@ -2847,6 +2852,11 @@ impl<T: Sized> NonNull<T> {
28472852
///
28482853
/// This is useful for initializing types which lazily allocate, like
28492854
/// `Vec::new` does.
2855+
///
2856+
/// Note that the pointer value may potentially represent a valid pointer to
2857+
/// a `T`, which means this must not be used as a "not yet initialized"
2858+
/// sentinel value. Types that lazily allocate must track initialization by
2859+
/// some other means.
28502860
#[stable(feature = "nonnull", since = "1.25.0")]
28512861
pub fn dangling() -> Self {
28522862
unsafe {

0 commit comments

Comments
 (0)