@@ -124,8 +124,8 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo
124
124
// above.
125
125
126
126
/// 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.
129
129
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
130
130
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
131
131
#[ track_caller]
@@ -138,6 +138,11 @@ pub const fn panic(expr: &'static str) -> ! {
138
138
// truncation and padding (even though none is used here). Using
139
139
// Arguments::new_const may allow the compiler to omit Formatter::pad from the
140
140
// 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.
141
146
panic_fmt ( fmt:: Arguments :: new_const ( & [ expr] ) ) ;
142
147
}
143
148
@@ -160,14 +165,6 @@ pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
160
165
panic_nounwind_fmt ( fmt:: Arguments :: new_const ( & [ expr] ) , /* force_no_backtrace */ true ) ;
161
166
}
162
167
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
-
171
168
#[ track_caller]
172
169
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
173
170
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
@@ -183,6 +180,16 @@ pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
183
180
panic_fmt ( format_args ! ( "internal error: entered unreachable code: {}" , * x) ) ;
184
181
}
185
182
183
+ /// This exists solely for the 2015 edition `panic!` macro to trigger
184
+ /// a lint on `panic!(my_str_variable);`.
185
+ #[ inline]
186
+ #[ track_caller]
187
+ #[ rustc_diagnostic_item = "panic_str_2015" ]
188
+ #[ rustc_const_unstable( feature = "panic_internals" , issue = "none" ) ]
189
+ pub const fn panic_str_2015 ( expr : & str ) -> ! {
190
+ panic_display ( & expr) ;
191
+ }
192
+
186
193
#[ inline]
187
194
#[ track_caller]
188
195
#[ rustc_do_not_const_check] // hooked by const-eval
0 commit comments