Skip to content

Commit 1df0c21

Browse files
authored
Rollup merge of #114845 - scottmcm:npo-align, r=WaffleLapkin
Add alignment to the NPO guarantee This PR [changes](#114845 (comment)) "same size" to "same size and alignment" in the option module's null pointer optimization docs in <https://doc.rust-lang.org/std/option/#representation>. As far as I know, this has been true for a long time in the actual rustc implementation, but it's not in the text of those docs, so I figured I'd bring this up to FCP it. I also see no particular reason that we'd ever *want* to have higher alignment on these. In many of the cases it's impossible, as the minimum alignment is already the size of the type, but even if we *could* do things like on 32-bit we could say that `NonZeroU64` is 4-align but `Option<NonZeroU64>` is 8-align, I just don't see any value in doing that, so feel completely fine closing this door for the few things on which the NPO is already guaranteed. These are basically all primitives, and should end up with the same size & alignment as those primitives. (There's no layout guarantee for something like `Option<[u8; 3]>`, where it'd be at least plausible to consider raising the alignment from 1 to 4 on, say, some hypothetical target that doesn't have efficient unaligned 4-byte load/stores. And even if we ever did start to offer some kind of guarantee around such a type, I doubt we'd put it under the "null pointer" optimization header.) Screenshots for the new examples: ![image](https://github.com/rust-lang/rust/assets/18526288/a7dbff42-50b4-462e-9e27-00d511b58763) ![image](https://github.com/rust-lang/rust/assets/18526288/dfd55288-80fb-419a-bc11-26198c27f9f9)
2 parents 4e22318 + 107cd8e commit 1df0c21

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

library/core/src/num/nonzero.rs

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ macro_rules! nonzero_integers {
4141
/// with the exception that `0` is not a valid instance.
4242
#[doc = concat!("`Option<", stringify!($Ty), ">` is guaranteed to be compatible with `", stringify!($Int), "`,")]
4343
/// including in FFI.
44+
///
45+
/// Thanks to the [null pointer optimization],
46+
#[doc = concat!("`", stringify!($Ty), "` and `Option<", stringify!($Ty), ">`")]
47+
/// are guaranteed to have the same size and alignment:
48+
///
49+
/// ```
50+
/// # use std::mem::{size_of, align_of};
51+
#[doc = concat!("use std::num::", stringify!($Ty), ";")]
52+
///
53+
#[doc = concat!("assert_eq!(size_of::<", stringify!($Ty), ">(), size_of::<Option<", stringify!($Ty), ">>());")]
54+
#[doc = concat!("assert_eq!(align_of::<", stringify!($Ty), ">(), align_of::<Option<", stringify!($Ty), ">>());")]
55+
/// ```
56+
///
57+
/// [null pointer optimization]: crate::option#representation
4458
#[$stability]
4559
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
4660
#[repr(transparent)]

library/core/src/option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
//! # Representation
120120
//!
121121
//! Rust guarantees to optimize the following types `T` such that
122-
//! [`Option<T>`] has the same size as `T`:
122+
//! [`Option<T>`] has the same size and alignment as `T`:
123123
//!
124124
//! * [`Box<U>`]
125125
//! * `&U`

library/core/src/ptr/non_null.rs

+18
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ use crate::slice::{self, SliceIndex};
4343
/// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
4444
/// is never used for mutation.
4545
///
46+
/// # Representation
47+
///
48+
/// Thanks to the [null pointer optimization],
49+
/// `NonNull<T>` and `Option<NonNull<T>>`
50+
/// are guaranteed to have the same size and alignment:
51+
///
52+
/// ```
53+
/// # use std::mem::{size_of, align_of};
54+
/// use std::ptr::NonNull;
55+
///
56+
/// assert_eq!(size_of::<NonNull<i16>>(), size_of::<Option<NonNull<i16>>>());
57+
/// assert_eq!(align_of::<NonNull<i16>>(), align_of::<Option<NonNull<i16>>>());
58+
///
59+
/// assert_eq!(size_of::<NonNull<str>>(), size_of::<Option<NonNull<str>>>());
60+
/// assert_eq!(align_of::<NonNull<str>>(), align_of::<Option<NonNull<str>>>());
61+
/// ```
62+
///
4663
/// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
4764
/// [`PhantomData`]: crate::marker::PhantomData
4865
/// [`UnsafeCell<T>`]: crate::cell::UnsafeCell
66+
/// [null pointer optimization]: crate::option#representation
4967
#[stable(feature = "nonnull", since = "1.25.0")]
5068
#[repr(transparent)]
5169
#[rustc_layout_scalar_valid_range_start(1)]

0 commit comments

Comments
 (0)