Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c8a9e0

Browse files
committedJun 11, 2024
Fix display of panic message in recursive panic.
1 parent 2f85702 commit 0c8a9e0

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed
 

‎core/src/panic.rs

+5
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,9 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
157157

158158
/// Just borrow the contents.
159159
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+
}
160165
}

‎std/src/panicking.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
641641
fn get(&mut self) -> &(dyn Any + Send) {
642642
&self.0
643643
}
644+
645+
fn as_str(&mut self) -> Option<&str> {
646+
Some(self.0)
647+
}
644648
}
645649

646650
impl fmt::Display for StaticStrPayload {
@@ -763,15 +767,8 @@ fn rust_panic_with_hook(
763767
// Don't try to format the message in this case, perhaps that is causing the
764768
// recursive panics. However if the message is just a string, no user-defined
765769
// 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}\nthread panicked while processing panic. aborting.\n");
770+
let message: &str = payload.as_str().unwrap_or_default();
771+
rtprintpanic!("panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n");
775772
}
776773
panic_count::MustAbort::AlwaysAbort => {
777774
// Unfortunately, this does not print a backtrace, because creating

0 commit comments

Comments
 (0)
Failed to load comments.