Skip to content

Commit 72e8fb4

Browse files
authored
Rollup merge of rust-lang#123050 - RalfJung:panic_str, r=m-ou-se
panic_str only exists for the migration to 2021 panic macros The only caller is `expect_failed`, which is already a cold inline(never) function, so inlining into that function should be fine. (And indeed `panic_str` was `#[inline]` anyway.) The existence of panic_str risks someone calling it when they should call `panic` instead, and I can't see a reason why this footgun should exist. I also extended the comment in `panic` to explain why it needs a `'static` string -- I know I've wondered about this in the past and it took me quite a while to understand.
2 parents c672773 + 132921e commit 72e8fb4

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt {
5353

5454
if Some(def_id) == cx.tcx.lang_items().begin_panic_fn()
5555
|| Some(def_id) == cx.tcx.lang_items().panic_fn()
56-
|| f_diagnostic_name == Some(sym::panic_str)
56+
|| f_diagnostic_name == Some(sym::panic_str_2015)
5757
{
5858
if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id {
5959
if matches!(

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ symbols! {
13481348
panic_misaligned_pointer_dereference,
13491349
panic_nounwind,
13501350
panic_runtime,
1351-
panic_str,
1351+
panic_str_2015,
13521352
panic_unwind,
13531353
panicking,
13541354
param_attrs,

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, 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/panic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub macro panic_2015 {
2727
($msg:literal $(,)?) => (
2828
$crate::panicking::panic($msg)
2929
),
30-
// Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint.
30+
// Use `panic_str_2015` instead of `panic_display::<&str>` for non_fmt_panic lint.
3131
($msg:expr $(,)?) => ({
32-
$crate::panicking::panic_str($msg);
32+
$crate::panicking::panic_str_2015($msg);
3333
}),
3434
// Special-case the single-argument case for const_panic.
3535
("{}", $arg:expr $(,)?) => ({

library/core/src/panicking.rs

+17-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

@@ -223,14 +228,6 @@ pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
223228
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ true);
224229
}
225230

226-
#[inline]
227-
#[track_caller]
228-
#[rustc_diagnostic_item = "panic_str"]
229-
#[rustc_const_unstable(feature = "panic_internals", issue = "none")]
230-
pub const fn panic_str(expr: &str) -> ! {
231-
panic_display(&expr);
232-
}
233-
234231
#[track_caller]
235232
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
236233
#[cfg_attr(feature = "panic_immediate_abort", inline)]
@@ -246,6 +243,16 @@ pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
246243
panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
247244
}
248245

246+
/// This exists solely for the 2015 edition `panic!` macro to trigger
247+
/// a lint on `panic!(my_str_variable);`.
248+
#[inline]
249+
#[track_caller]
250+
#[rustc_diagnostic_item = "panic_str_2015"]
251+
#[rustc_const_unstable(feature = "panic_internals", issue = "none")]
252+
pub const fn panic_str_2015(expr: &str) -> ! {
253+
panic_display(&expr);
254+
}
255+
249256
#[inline]
250257
#[track_caller]
251258
#[rustc_do_not_const_check] // hooked by const-eval

tests/ui-fulldeps/stable-mir/check_transform.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ fn generate_input(path: &str) -> std::io::Result<()> {
137137
write!(
138138
file,
139139
r#"
140-
#![feature(panic_internals)]
140+
fn panic_str(msg: &str) {{ panic!("{{}}", msg); }}
141141
pub fn dummy() {{
142-
core::panicking::panic_str("oops");
142+
panic_str("oops");
143143
}}
144144
"#
145145
)?;

0 commit comments

Comments
 (0)