Skip to content

Commit

Permalink
Split out option::unwrap_failed like we have result::unwrap_failed
Browse files Browse the repository at this point in the history
...and like `option::expect_failed`
  • Loading branch information
scottmcm committed Jan 14, 2024
1 parent 62d7ed4 commit 2348366
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 10 deletions.
12 changes: 10 additions & 2 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,14 @@ impl<T> Option<T> {
/// let x: Option<&str> = None;
/// assert_eq!(x.unwrap(), "air"); // fails
/// ```
#[inline]
#[inline(always)]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn unwrap(self) -> T {
match self {
Some(val) => val,
None => panic("called `Option::unwrap()` on a `None` value"),
None => unwrap_failed(),
}
}

Expand Down Expand Up @@ -1970,6 +1970,14 @@ impl<T, E> Option<Result<T, E>> {
}
}

#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[cold]
#[track_caller]
const fn unwrap_failed() -> ! {
panic("called `Option::unwrap()` on a `None` value")
}

// This is a separate function to reduce the code size of .expect() itself.
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
Expand Down
4 changes: 2 additions & 2 deletions tests/codegen/debuginfo-inline-callsite-location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// can correctly merge the debug info if it merges the inlined code (e.g., for merging of tail
// calls to panic.

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

// CHECK-DAG: ![[#func_dbg:]] = distinct !DISubprogram(name: "unwrap<i32>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn num_to_digit(_1: char) -> u32 {
}

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

bb5: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn num_to_digit(_1: char) -> u32 {
}

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

bb5: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}

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

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
}

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

bb3: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}

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

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
}

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

bb3: {
Expand Down

0 comments on commit 2348366

Please sign in to comment.