@@ -245,19 +245,24 @@ fn default_hook(info: &PanicInfo<'_>) {
245245
246246 // The current implementation always returns `Some`.
247247 let location = info. location ( ) . unwrap ( ) ;
248-
249- let msg = match info. payload ( ) . downcast_ref :: < & ' static str > ( ) {
250- Some ( s) => * s,
251- None => match info. payload ( ) . downcast_ref :: < String > ( ) {
252- Some ( s) => & s[ ..] ,
253- None => "Box<dyn Any>" ,
254- } ,
255- } ;
256248 let thread = thread_info:: current_thread ( ) ;
257249 let name = thread. as_ref ( ) . and_then ( |t| t. name ( ) ) . unwrap_or ( "<unnamed>" ) ;
258250
259251 let write = |err : & mut dyn crate :: io:: Write | {
260- let _ = writeln ! ( err, "thread '{name}' panicked at '{msg}', {location}" ) ;
252+ // Use the panic message directly if available, otherwise take it from
253+ // the payload.
254+ if let Some ( msg) = info. message ( ) {
255+ let _ = writeln ! ( err, "thread '{name}' panicked at '{msg}', {location}" ) ;
256+ } else {
257+ let msg = if let Some ( s) = info. payload ( ) . downcast_ref :: < & ' static str > ( ) {
258+ * s
259+ } else if let Some ( s) = info. payload ( ) . downcast_ref :: < String > ( ) {
260+ & s[ ..]
261+ } else {
262+ "Box<dyn Any>"
263+ } ;
264+ let _ = writeln ! ( err, "thread '{name}' panicked at '{msg}', {location}" ) ;
265+ }
261266
262267 static FIRST_PANIC : AtomicBool = AtomicBool :: new ( true ) ;
263268
@@ -539,6 +544,8 @@ pub fn panicking() -> bool {
539544#[ cfg( not( test) ) ]
540545#[ panic_handler]
541546pub fn begin_panic_handler ( info : & PanicInfo < ' _ > ) -> ! {
547+ use alloc:: alloc:: AllocErrorPanicPayload ;
548+
542549 struct PanicPayload < ' a > {
543550 inner : & ' a fmt:: Arguments < ' a > ,
544551 string : Option < String > ,
@@ -565,8 +572,7 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
565572 unsafe impl < ' a > BoxMeUp for PanicPayload < ' a > {
566573 fn take_box ( & mut self ) -> * mut ( dyn Any + Send ) {
567574 // We do two allocations here, unfortunately. But (a) they're required with the current
568- // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in
569- // begin_panic below).
575+ // scheme, and (b) OOM uses its own separate payload type which doesn't allocate.
570576 let contents = mem:: take ( self . fill ( ) ) ;
571577 Box :: into_raw ( Box :: new ( contents) )
572578 }
@@ -591,7 +597,14 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
591597 let loc = info. location ( ) . unwrap ( ) ; // The current implementation always returns Some
592598 let msg = info. message ( ) . unwrap ( ) ; // The current implementation always returns Some
593599 crate :: sys_common:: backtrace:: __rust_end_short_backtrace ( move || {
594- if let Some ( msg) = msg. as_str ( ) {
600+ if let Some ( payload) = info. payload ( ) . downcast_ref :: < AllocErrorPanicPayload > ( ) {
601+ rust_panic_with_hook (
602+ & mut payload. internal_clone ( ) ,
603+ info. message ( ) ,
604+ loc,
605+ info. can_unwind ( ) ,
606+ ) ;
607+ } else if let Some ( msg) = msg. as_str ( ) {
595608 rust_panic_with_hook ( & mut StrPanicPayload ( msg) , info. message ( ) , loc, info. can_unwind ( ) ) ;
596609 } else {
597610 rust_panic_with_hook (
@@ -638,11 +651,7 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
638651
639652 unsafe impl < A : Send + ' static > BoxMeUp for PanicPayload < A > {
640653 fn take_box ( & mut self ) -> * mut ( dyn Any + Send ) {
641- // Note that this should be the only allocation performed in this code path. Currently
642- // this means that panic!() on OOM will invoke this code path, but then again we're not
643- // really ready for panic on OOM anyway. If we do start doing this, then we should
644- // propagate this allocation to be performed in the parent of this thread instead of the
645- // thread that's panicking.
654+ // Note that this should be the only allocation performed in this code path.
646655 let data = match self . inner . take ( ) {
647656 Some ( a) => Box :: new ( a) as Box < dyn Any + Send > ,
648657 None => process:: abort ( ) ,
0 commit comments