diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 8d40faf3bc6..7174661d281 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -295,10 +295,16 @@ impl Option { pub fn expect(self, msg: &str) -> T { match self { Some(val) => val, - None => panic!("{}", msg), + None => Self::expect_failed(msg), } } + #[inline(never)] + #[cold] + fn expect_failed(msg: &str) -> ! { + panic!("{}", msg) + } + /// Moves the value `v` out of the `Option` if it is `Some(v)`. /// /// # Panics diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 6ec76c821b3..b91cc1b3e6a 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -684,11 +684,16 @@ impl Result { pub fn unwrap(self) -> T { match self { Ok(t) => t, - Err(e) => - panic!("called `Result::unwrap()` on an `Err` value: {:?}", e) + Err(e) => Self::unwrap_failed(e), } } + #[inline(never)] + #[cold] + fn unwrap_failed(error: E) -> ! { + panic!("called `Result::unwrap()` on an `Err` value: {:?}", error) + } + /// Unwraps a result, yielding the content of an `Ok`. /// /// # Panics @@ -706,9 +711,15 @@ impl Result { pub fn expect(self, msg: &str) -> T { match self { Ok(t) => t, - Err(e) => panic!("{}: {:?}", msg, e), + Err(e) => Self::expect_failed(msg, e), } } + + #[inline(never)] + #[cold] + fn expect_failed(msg: &str, error: E) -> ! { + panic!("{}: {:?}", msg, error) + } } impl Result { @@ -734,11 +745,17 @@ impl Result { #[stable(feature = "rust1", since = "1.0.0")] pub fn unwrap_err(self) -> E { match self { - Ok(t) => - panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t), - Err(e) => e + Ok(t) => Self::unwrap_err_failed(t), + Err(e) => e, } } + + #[inline(never)] + #[cold] + fn unwrap_err_failed(t: T) -> ! { + panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t) + } + } /////////////////////////////////////////////////////////////////////////////