Skip to content

Commit

Permalink
Auto merge of rust-lang#124737 - alion02:litter-less, r=<try>
Browse files Browse the repository at this point in the history
[Experiment] Replace `unreachable_unchecked()` with `uninit().assume_init()` in `unwrap_unchecked()`

[Relevant ACP.](rust-lang/libs-team#378) [Relevant godbolt.](https://godbolt.org/z/WqfcT7Ys9)

Attempt to clean up the LLVM IR we generate based on the assumption that we usually don't want to `assume` the value of the discriminant (because it sticks around in the IR and randomly inhibits optimizations).
  • Loading branch information
bors committed May 5, 2024
2 parents 69ffc0d + fd98988 commit f471a93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ use crate::iter::{self, FusedIterator, TrustedLen};
use crate::panicking::{panic, panic_display};
use crate::pin::Pin;
use crate::{
cmp, convert, hint, mem,
cmp, convert, mem,
ops::{self, ControlFlow, Deref, DerefMut},
slice,
};
Expand Down Expand Up @@ -1037,7 +1037,7 @@ impl<T> Option<T> {
match self {
Some(val) => val,
// SAFETY: the safety contract must be upheld by the caller.
None => unsafe { hint::unreachable_unchecked() },
None => unsafe { mem::MaybeUninit::uninit().assume_init() },
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@

use crate::iter::{self, FusedIterator, TrustedLen};
use crate::ops::{self, ControlFlow, Deref, DerefMut};
use crate::{convert, fmt, hint};
use crate::{convert, fmt, mem};

/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
///
Expand Down Expand Up @@ -1460,7 +1460,7 @@ impl<T, E> Result<T, E> {
match self {
Ok(t) => t,
// SAFETY: the safety contract must be upheld by the caller.
Err(_) => unsafe { hint::unreachable_unchecked() },
Err(_) => unsafe { mem::MaybeUninit::uninit().assume_init() },
}
}

Expand Down Expand Up @@ -1491,7 +1491,7 @@ impl<T, E> Result<T, E> {
debug_assert!(self.is_err());
match self {
// SAFETY: the safety contract must be upheld by the caller.
Ok(_) => unsafe { hint::unreachable_unchecked() },
Ok(_) => unsafe { mem::MaybeUninit::uninit().assume_init() },
Err(e) => e,
}
}
Expand Down

0 comments on commit f471a93

Please sign in to comment.