Skip to content

Commit 917dafc

Browse files
committed
Add separate impl of unwrap_failed to avoid constructing trait objects
1 parent 0adee2c commit 917dafc

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/core/src/result.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1653,13 +1653,26 @@ impl<T> Result<T, T> {
16531653
}
16541654

16551655
// This is a separate function to reduce the code size of the methods
1656+
#[cfg(not(feature = "panic_immediate_abort"))]
16561657
#[inline(never)]
16571658
#[cold]
16581659
#[track_caller]
16591660
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
16601661
panic!("{}: {:?}", msg, error)
16611662
}
16621663

1664+
// This is a separate function to avoid constructing a `dyn Debug`
1665+
// that gets immediately thrown away, since vtables don't get cleaned up
1666+
// by dead code elimination if a trait object is constructed even if it goes
1667+
// unused
1668+
#[cfg(feature = "panic_immediate_abort")]
1669+
#[inline]
1670+
#[cold]
1671+
#[track_caller]
1672+
fn unwrap_failed<T>(_msg: &str, _error: &T) -> ! {
1673+
panic!()
1674+
}
1675+
16631676
/////////////////////////////////////////////////////////////////////////////
16641677
// Trait implementations
16651678
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)