Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement unwrap_unchecked using transmutes when niche-optimizations are in play #102151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
#![feature(extern_types)]
#![feature(fundamental)]
#![feature(if_let_guard)]
#![feature(inline_const)]
#![feature(intra_doc_pointers)]
#![feature(intrinsics)]
#![feature(lang_items)]
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,13 @@

use crate::iter::{self, FromIterator, FusedIterator, TrustedLen};
use crate::marker::Destruct;
use crate::mem::ManuallyDrop;
use crate::panicking::{panic, panic_str};
use crate::pin::Pin;
use crate::{
convert, hint, mem,
ops::{self, ControlFlow, Deref, DerefMut},
ptr,
};

/// The `Option` type. See [the module level documentation](self) for more.
Expand Down Expand Up @@ -891,6 +893,14 @@ impl<T> Option<T> {
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
pub const unsafe fn unwrap_unchecked(self) -> T {
debug_assert!(self.is_some());
// Make things easier for the optimizer when niches are involved
if const { mem::size_of::<T>() == mem::size_of::<Self>() } {
// SAFETY: Size equality implies niches are involved. And with niches
// transmutes are ok because they don't change bits, only make use of invalid values
unsafe {
return ptr::read(&ManuallyDrop::new(self) as *const _ as *const T);
}
}
match self {
Some(val) => val,
// SAFETY: the safety contract must be upheld by the caller.
Expand Down
19 changes: 18 additions & 1 deletion library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,9 @@

use crate::iter::{self, FromIterator, FusedIterator, TrustedLen};
use crate::marker::Destruct;
use crate::mem::ManuallyDrop;
use crate::ops::{self, ControlFlow, Deref, DerefMut};
use crate::{convert, fmt, hint};
use crate::{convert, fmt, hint, mem, ptr};

/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
///
Expand Down Expand Up @@ -1524,6 +1525,14 @@ impl<T, E> Result<T, E> {
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
pub unsafe fn unwrap_unchecked(self) -> T {
debug_assert!(self.is_ok());
// Make things easier for the optimizer when niches are involved
if const { mem::size_of::<T>() == mem::size_of::<Self>() } {
// SAFETY: Size equality implies niches are involved. And with niches
// transmutes are ok because they don't change bits, only make use of invalid values
unsafe {
return ptr::read(&ManuallyDrop::new(self) as *const _ as *const T);
}
}
match self {
Ok(t) => t,
// SAFETY: the safety contract must be upheld by the caller.
Expand Down Expand Up @@ -1556,6 +1565,14 @@ impl<T, E> Result<T, E> {
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
pub unsafe fn unwrap_err_unchecked(self) -> E {
debug_assert!(self.is_err());
// Make things easier for the optimizer when niches are involved
if const { mem::size_of::<E>() == mem::size_of::<Self>() } {
// SAFETY: Size equality implies niches are involved. And with niches
// transmutes are ok because they don't change bits, only make use of invalid values
unsafe {
return ptr::read(&ManuallyDrop::new(self) as *const _ as *const E);
}
}
match self {
// SAFETY: the safety contract must be upheld by the caller.
Ok(_) => unsafe { hint::unreachable_unchecked() },
Expand Down