Skip to content

Commit e4f7384

Browse files
authored
Rollup merge of rust-lang#137323 - joshlf:transmute-npo, r=RalfJung
Guarantee behavior of transmuting `Option::<T>::None` subject to NPO In rust-lang#115333, we added a guarantee that transmuting from `[0u8; N]` to `Option<P>` is sound where `P` is a pointer type subject to the null pointer optimization (NPO). It would be useful to be able to guarantee the inverse - that a `None::<P>` value can be transmutes to an array and that will yield `[0u8; N]`. Closes rust-lang#117591
2 parents 04c1a04 + 58be659 commit e4f7384

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

core/src/option.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,22 @@
120120
//!
121121
//! Rust guarantees to optimize the following types `T` such that
122122
//! [`Option<T>`] has the same size, alignment, and [function call ABI] as `T`. In some
123-
//! of these cases, Rust further guarantees that
124-
//! `transmute::<_, Option<T>>([0u8; size_of::<T>()])` is sound and
125-
//! produces `Option::<T>::None`. These cases are identified by the
126-
//! second column:
127-
//!
128-
//! | `T` | `transmute::<_, Option<T>>([0u8; size_of::<T>()])` sound? |
129-
//! |---------------------------------------------------------------------|----------------------------------------------------------------------|
130-
//! | [`Box<U>`] (specifically, only `Box<U, Global>`) | when `U: Sized` |
131-
//! | `&U` | when `U: Sized` |
132-
//! | `&mut U` | when `U: Sized` |
133-
//! | `fn`, `extern "C" fn`[^extern_fn] | always |
134-
//! | [`num::NonZero*`] | always |
135-
//! | [`ptr::NonNull<U>`] | when `U: Sized` |
136-
//! | `#[repr(transparent)]` struct around one of the types in this list. | when it holds for the inner type |
123+
//! of these cases, Rust further guarantees the following:
124+
//! - `transmute::<_, Option<T>>([0u8; size_of::<T>()])` is sound and produces
125+
//! `Option::<T>::None`
126+
//! - `transmute::<_, [u8; size_of::<T>()]>(Option::<T>::None)` is sound and produces
127+
//! `[0u8; size_of::<T>()]`
128+
//! These cases are identified by the second column:
129+
//!
130+
//! | `T` | Transmuting between `[0u8; size_of::<T>()]` and `Option::<T>::None` sound? |
131+
//! |---------------------------------------------------------------------|----------------------------------------------------------------------------|
132+
//! | [`Box<U>`] (specifically, only `Box<U, Global>`) | when `U: Sized` |
133+
//! | `&U` | when `U: Sized` |
134+
//! | `&mut U` | when `U: Sized` |
135+
//! | `fn`, `extern "C" fn`[^extern_fn] | always |
136+
//! | [`num::NonZero*`] | always |
137+
//! | [`ptr::NonNull<U>`] | when `U: Sized` |
138+
//! | `#[repr(transparent)]` struct around one of the types in this list. | when it holds for the inner type |
137139
//!
138140
//! [^extern_fn]: this remains true for any argument/return types and any other ABI: `extern "abi" fn` (_e.g._, `extern "system" fn`)
139141
//!

0 commit comments

Comments
 (0)