Skip to content

Commit bf2637f

Browse files
committed
Auto merge of #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 533cfde + 2348366 commit bf2637f

8 files changed

+18
-10
lines changed

library/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)]

tests/codegen/debuginfo-inline-callsite-location.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// can correctly merge the debug info if it merges the inlined code (e.g., for merging of tail
55
// calls to panic.
66

7-
// CHECK: tail call void @_ZN4core9panicking5panic17h{{([0-9a-z]{16})}}E
7+
// CHECK: tail call void @_ZN4core6option13unwrap_failed17h{{([0-9a-z]{16})}}E
88
// CHECK-SAME: !dbg ![[#first_dbg:]]
9-
// CHECK: tail call void @_ZN4core9panicking5panic17h{{([0-9a-z]{16})}}E
9+
// CHECK: tail call void @_ZN4core6option13unwrap_failed17h{{([0-9a-z]{16})}}E
1010
// CHECK-SAME: !dbg ![[#second_dbg:]]
1111

1212
// CHECK-DAG: ![[#func_dbg:]] = distinct !DISubprogram(name: "unwrap<i32>"

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn num_to_digit(_1: char) -> u32 {
5151
}
5252

5353
bb4: {
54-
_7 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind unreachable;
54+
_7 = option::unwrap_failed() -> unwind unreachable;
5555
}
5656

5757
bb5: {

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn num_to_digit(_1: char) -> u32 {
5151
}
5252

5353
bb4: {
54-
_7 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind continue;
54+
_7 = option::unwrap_failed() -> unwind continue;
5555
}
5656

5757
bb5: {

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565

6666
bb1: {
67-
_11 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind unreachable;
67+
_11 = option::unwrap_failed() -> unwind unreachable;
6868
}
6969

7070
bb2: {

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565

6666
bb2: {
67-
_11 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind continue;
67+
_11 = option::unwrap_failed() -> unwind continue;
6868
}
6969

7070
bb3: {

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565

6666
bb1: {
67-
_11 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind unreachable;
67+
_11 = option::unwrap_failed() -> unwind unreachable;
6868
}
6969

7070
bb2: {

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565

6666
bb2: {
67-
_11 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value") -> unwind continue;
67+
_11 = option::unwrap_failed() -> unwind continue;
6868
}
6969

7070
bb3: {

0 commit comments

Comments
 (0)