Skip to content

Commit bcb48a1

Browse files
committed
Auto merge of rust-lang#119954 - scottmcm:option-unwrap-failed, r=WaffleLapkin
Split out `option::unwrap_failed` like we have `result::unwrap_failed` ...and like `option::expect_failed`
2 parents cebd2c1 + 07b7834 commit bcb48a1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

core/src/option.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -921,14 +921,14 @@ impl<T> Option<T> {
921921
/// let x: Option<&str> = None;
922922
/// assert_eq!(x.unwrap(), "air"); // fails
923923
/// ```
924-
#[inline]
924+
#[inline(always)]
925925
#[track_caller]
926926
#[stable(feature = "rust1", since = "1.0.0")]
927927
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
928928
pub const fn unwrap(self) -> T {
929929
match self {
930930
Some(val) => val,
931-
None => panic("called `Option::unwrap()` on a `None` value"),
931+
None => unwrap_failed(),
932932
}
933933
}
934934

@@ -1970,6 +1970,14 @@ impl<T, E> Option<Result<T, E>> {
19701970
}
19711971
}
19721972

1973+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
1974+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
1975+
#[cold]
1976+
#[track_caller]
1977+
const fn unwrap_failed() -> ! {
1978+
panic("called `Option::unwrap()` on a `None` value")
1979+
}
1980+
19731981
// This is a separate function to reduce the code size of .expect() itself.
19741982
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
19751983
#[cfg_attr(feature = "panic_immediate_abort", inline)]

0 commit comments

Comments
 (0)