Skip to content

Commit dba9c26

Browse files
committed
remove panic_str, inline into the only caller
1 parent 60b5ca6 commit dba9c26

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

library/core/src/option.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@
554554
#![stable(feature = "rust1", since = "1.0.0")]
555555

556556
use crate::iter::{self, FromIterator, FusedIterator, TrustedLen};
557-
use crate::panicking::{panic, panic_str};
557+
use crate::panicking::{panic, panic_display};
558558
use crate::pin::Pin;
559559
use crate::{
560560
cmp, convert, hint, mem,
@@ -1991,7 +1991,7 @@ const fn unwrap_failed() -> ! {
19911991
#[track_caller]
19921992
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
19931993
const fn expect_failed(msg: &str) -> ! {
1994-
panic_str(msg)
1994+
panic_display(&msg)
19951995
}
19961996

19971997
/////////////////////////////////////////////////////////////////////////////

library/core/src/panicking.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo
124124
// above.
125125

126126
/// The underlying implementation of core's `panic!` macro when no formatting is used.
127-
// never inline unless panic_immediate_abort to avoid code
128-
// bloat at the call sites as much as possible
127+
// Never inline unless panic_immediate_abort to avoid code
128+
// bloat at the call sites as much as possible.
129129
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
130130
#[cfg_attr(feature = "panic_immediate_abort", inline)]
131131
#[track_caller]
@@ -138,6 +138,11 @@ pub const fn panic(expr: &'static str) -> ! {
138138
// truncation and padding (even though none is used here). Using
139139
// Arguments::new_const may allow the compiler to omit Formatter::pad from the
140140
// output binary, saving up to a few kilobytes.
141+
// However, this optimization only works for `'static` strings: `new_const` also makes this
142+
// message return `Some` from `Arguments::as_str`, which means it can become part of the panic
143+
// payload without any allocation or copying. Shorter-lived strings would become invalid as
144+
// stack frames get popped during unwinding, and couldn't be directly referenced from the
145+
// payload.
141146
panic_fmt(fmt::Arguments::new_const(&[expr]));
142147
}
143148

@@ -160,14 +165,6 @@ pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
160165
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ true);
161166
}
162167

163-
#[inline]
164-
#[track_caller]
165-
#[rustc_diagnostic_item = "panic_str"]
166-
#[rustc_const_unstable(feature = "panic_internals", issue = "none")]
167-
pub const fn panic_str(expr: &str) -> ! {
168-
panic_display(&expr);
169-
}
170-
171168
#[track_caller]
172169
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
173170
#[cfg_attr(feature = "panic_immediate_abort", inline)]

0 commit comments

Comments
 (0)