Skip to content

Commit d63b8e5

Browse files
committed
Auto merge of #31116 - bluss:expect-out-cold, r=alexcrichton
Use cold functions for panic formatting Option::expect, Result::unwrap, expect These methods are marked inline, but insert a big chunk of formatting code, as well as other error path related code, such as deallocating a std::io::Error if you have one. We can explicitly separate out that code path into a function that is never inline, since the panicking case should always be rare.
2 parents c2740b6 + 257bff3 commit d63b8e5

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/libcore/option.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<T> Option<T> {
295295
pub fn expect(self, msg: &str) -> T {
296296
match self {
297297
Some(val) => val,
298-
None => panic!("{}", msg),
298+
None => expect_failed(msg),
299299
}
300300
}
301301

@@ -697,6 +697,14 @@ impl<T: Default> Option<T> {
697697
}
698698
}
699699

700+
// This is a separate function to reduce the code size of .expect() itself.
701+
#[inline(never)]
702+
#[cold]
703+
fn expect_failed(msg: &str) -> ! {
704+
panic!("{}", msg)
705+
}
706+
707+
700708
/////////////////////////////////////////////////////////////////////////////
701709
// Trait implementations
702710
/////////////////////////////////////////////////////////////////////////////

src/libcore/result.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
684684
pub fn unwrap(self) -> T {
685685
match self {
686686
Ok(t) => t,
687-
Err(e) =>
688-
panic!("called `Result::unwrap()` on an `Err` value: {:?}", e)
687+
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", e),
689688
}
690689
}
691690

@@ -706,7 +705,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
706705
pub fn expect(self, msg: &str) -> T {
707706
match self {
708707
Ok(t) => t,
709-
Err(e) => panic!("{}: {:?}", msg, e),
708+
Err(e) => unwrap_failed(msg, e),
710709
}
711710
}
712711
}
@@ -734,13 +733,19 @@ impl<T: fmt::Debug, E> Result<T, E> {
734733
#[stable(feature = "rust1", since = "1.0.0")]
735734
pub fn unwrap_err(self) -> E {
736735
match self {
737-
Ok(t) =>
738-
panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t),
739-
Err(e) => e
736+
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", t),
737+
Err(e) => e,
740738
}
741739
}
742740
}
743741

742+
// This is a separate function to reduce the code size of the methods
743+
#[inline(never)]
744+
#[cold]
745+
fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! {
746+
panic!("{}: {:?}", msg, error)
747+
}
748+
744749
/////////////////////////////////////////////////////////////////////////////
745750
// Trait implementations
746751
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)