]> git.lizzy.rs Git - rust.git/commitdiff
Add separate impl of unwrap_failed to avoid constructing trait objects
authorManish Goregaokar <manishsmail@gmail.com>
Fri, 10 Dec 2021 07:42:26 +0000 (13:12 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Fri, 10 Dec 2021 07:42:26 +0000 (13:12 +0530)
library/core/src/result.rs

index e6b8c8ec3385a09077f2f743e975ec1d63d63558..ab067d57d082abc66ae74307382457573f88d874 100644 (file)
@@ -1653,6 +1653,7 @@ pub const fn into_ok_or_err(self) -> T {
 }
 
 // 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<T>(_msg: &str, _error: &T) -> ! {
+    panic!()
+}
+
 /////////////////////////////////////////////////////////////////////////////
 // Trait implementations
 /////////////////////////////////////////////////////////////////////////////