2 files changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -157,4 +157,9 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
157
157
158
158
/// Just borrow the contents.
159
159
fn get ( & mut self ) -> & ( dyn Any + Send ) ;
160
+
161
+ /// Try to borrow the contents as `&str`, if possible without doing any allocations.
162
+ fn as_str ( & mut self ) -> Option < & str > {
163
+ None
164
+ }
160
165
}
Original file line number Diff line number Diff line change @@ -641,6 +641,10 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
641
641
fn get ( & mut self ) -> & ( dyn Any + Send ) {
642
642
& self . 0
643
643
}
644
+
645
+ fn as_str ( & mut self ) -> Option < & str > {
646
+ Some ( self . 0 )
647
+ }
644
648
}
645
649
646
650
impl fmt:: Display for StaticStrPayload {
@@ -763,15 +767,8 @@ fn rust_panic_with_hook(
763
767
// Don't try to format the message in this case, perhaps that is causing the
764
768
// recursive panics. However if the message is just a string, no user-defined
765
769
// code is involved in printing it, so that is risk-free.
766
- let msg_str = message. and_then ( |m| m. as_str ( ) ) . map ( |m| [ m] ) ;
767
- let message = msg_str. as_ref ( ) . map ( |m| fmt:: Arguments :: new_const ( m) ) ;
768
- let panicinfo = PanicInfo :: internal_constructor (
769
- message. as_ref ( ) ,
770
- location,
771
- can_unwind,
772
- force_no_backtrace,
773
- ) ;
774
- rtprintpanic ! ( "{panicinfo}\n thread panicked while processing panic. aborting.\n " ) ;
770
+ let message: & str = payload. as_str ( ) . unwrap_or_default ( ) ;
771
+ rtprintpanic ! ( "panicked at {location}:\n {message}\n thread panicked while processing panic. aborting.\n " ) ;
775
772
}
776
773
panic_count:: MustAbort :: AlwaysAbort => {
777
774
// Unfortunately, this does not print a backtrace, because creating
0 commit comments