From 917dafc73a9eb257b9b5c2d357c4abd8bb1c3a8e Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 Dec 2021 13:12:26 +0530 Subject: [PATCH] Add separate impl of unwrap_failed to avoid constructing trait objects --- library/core/src/result.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index e6b8c8ec3385a..ab067d57d082a 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1653,6 +1653,7 @@ impl Result { } // This is a separate function to reduce the code size of the methods +#[cfg(not(feature = "panic_immediate_abort"))] #[inline(never)] #[cold] #[track_caller] @@ -1660,6 +1661,18 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! { panic!("{}: {:?}", msg, error) } +// This is a separate function to avoid constructing a `dyn Debug` +// that gets immediately thrown away, since vtables don't get cleaned up +// by dead code elimination if a trait object is constructed even if it goes +// unused +#[cfg(feature = "panic_immediate_abort")] +#[inline] +#[cold] +#[track_caller] +fn unwrap_failed(_msg: &str, _error: &T) -> ! { + panic!() +} + ///////////////////////////////////////////////////////////////////////////// // Trait implementations /////////////////////////////////////////////////////////////////////////////