@@ -625,6 +625,12 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
625625 }
626626 }
627627
628+ impl fmt:: Display for FormatStringPayload < ' _ > {
629+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
630+ if let Some ( s) = & self . string { f. write_str ( s) } else { f. write_fmt ( * self . inner ) }
631+ }
632+ }
633+
628634 struct StaticStrPayload ( & ' static str ) ;
629635
630636 unsafe impl PanicPayload for StaticStrPayload {
@@ -637,21 +643,25 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
637643 }
638644 }
639645
646+ impl fmt:: Display for StaticStrPayload {
647+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
648+ f. write_str ( self . 0 )
649+ }
650+ }
651+
640652 let loc = info. location ( ) . unwrap ( ) ; // The current implementation always returns Some
641653 let msg = info. message ( ) ;
642654 crate :: sys_common:: backtrace:: __rust_end_short_backtrace ( move || {
643655 if let Some ( s) = msg. as_str ( ) {
644656 rust_panic_with_hook (
645657 & mut StaticStrPayload ( s) ,
646- Some ( msg) ,
647658 loc,
648659 info. can_unwind ( ) ,
649660 info. force_no_backtrace ( ) ,
650661 ) ;
651662 } else {
652663 rust_panic_with_hook (
653664 & mut FormatStringPayload { inner : & msg, string : None } ,
654- Some ( msg) ,
655665 loc,
656666 info. can_unwind ( ) ,
657667 info. force_no_backtrace ( ) ,
@@ -681,7 +691,6 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
681691 return crate :: sys_common:: backtrace:: __rust_end_short_backtrace ( move || {
682692 rust_panic_with_hook (
683693 & mut Payload :: new ( msg) ,
684- None ,
685694 loc,
686695 /* can_unwind */ true ,
687696 /* force_no_backtrace */ false ,
@@ -719,6 +728,15 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
719728 }
720729 }
721730 }
731+
732+ impl < A : Send + ' static > fmt:: Display for Payload < A > {
733+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
734+ match & self . inner {
735+ Some ( a) => f. write_str ( payload_as_str ( a) ) ,
736+ None => process:: abort ( ) ,
737+ }
738+ }
739+ }
722740}
723741
724742fn payload_as_str ( payload : & dyn Any ) -> & str {
@@ -738,7 +756,6 @@ fn payload_as_str(payload: &dyn Any) -> &str {
738756/// abort or unwind.
739757fn rust_panic_with_hook (
740758 payload : & mut dyn PanicPayload ,
741- message : Option < fmt:: Arguments < ' _ > > ,
742759 location : & Location < ' _ > ,
743760 can_unwind : bool ,
744761 force_no_backtrace : bool ,
@@ -765,11 +782,7 @@ fn rust_panic_with_hook(
765782 panic_count:: MustAbort :: AlwaysAbort => {
766783 // Unfortunately, this does not print a backtrace, because creating
767784 // a `Backtrace` will allocate, which we must avoid here.
768- if let Some ( message) = message {
769- rtprintpanic ! ( "aborting due to panic at {location}:\n {message}\n " ) ;
770- } else {
771- rtprintpanic ! ( "aborting due to panic at {location}\n " ) ;
772- }
785+ rtprintpanic ! ( "aborting due to panic at {location}:\n {payload}\n " ) ;
773786 }
774787 }
775788 crate :: sys:: abort_internal ( ) ;
@@ -825,6 +838,12 @@ pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
825838 }
826839 }
827840
841+ impl fmt:: Display for RewrapBox {
842+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
843+ f. write_str ( payload_as_str ( & self . 0 ) )
844+ }
845+ }
846+
828847 rust_panic ( & mut RewrapBox ( payload) )
829848}
830849
0 commit comments