From 3cf9ae6ff30f2648b6233ede61ad0d1e75a30d14 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 Dec 2021 13:05:06 +0530 Subject: [PATCH 1/3] inline slice panics on panic_immediate_abort --- library/core/src/slice/index.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/library/core/src/slice/index.rs b/library/core/src/slice/index.rs index f7224303549..0298bba8d32 100644 --- a/library/core/src/slice/index.rs +++ b/library/core/src/slice/index.rs @@ -27,35 +27,40 @@ fn index_mut(&mut self, index: I) -> &mut I::Output { } } -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] fn slice_start_index_len_fail(index: usize, len: usize) -> ! { panic!("range start index {} out of range for slice of length {}", index, len); } -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] fn slice_end_index_len_fail(index: usize, len: usize) -> ! { panic!("range end index {} out of range for slice of length {}", index, len); } -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] fn slice_index_order_fail(index: usize, end: usize) -> ! { panic!("slice index starts at {} but ends at {}", index, end); } -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] fn slice_start_index_overflow_fail() -> ! { panic!("attempted to index slice from after maximum usize"); } -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] fn slice_end_index_overflow_fail() -> ! { From 0adee2c01eb5ecb91df3b4ac1c045b18f8a4c2f5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 Dec 2021 13:08:06 +0530 Subject: [PATCH 2/3] inline Option panics on panic_immediate_abort --- library/core/src/option.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 7b9c6e43960..aae3af81a52 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1656,7 +1656,8 @@ pub const fn transpose(self) -> Result, E> { } // This is a separate function to reduce the code size of .expect() itself. -#[inline(never)] +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] const fn expect_failed(msg: &str) -> ! { From 917dafc73a9eb257b9b5c2d357c4abd8bb1c3a8e Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 Dec 2021 13:12:26 +0530 Subject: [PATCH 3/3] 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 e6b8c8ec338..ab067d57d08 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -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(_msg: &str, _error: &T) -> ! { + panic!() +} + ///////////////////////////////////////////////////////////////////////////// // Trait implementations /////////////////////////////////////////////////////////////////////////////