Skip to content

Commit 373fb60

Browse files
committed
Impl Display for PanicPayload to simplify things.
1 parent 224d45c commit 373fb60

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

core/src/panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub macro unreachable_2021 {
144144
/// use.
145145
#[unstable(feature = "std_internals", issue = "none")]
146146
#[doc(hidden)]
147-
pub unsafe trait PanicPayload {
147+
pub unsafe trait PanicPayload: crate::fmt::Display {
148148
/// Take full ownership of the contents.
149149
/// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in core.
150150
///

std/src/panicking.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -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

724742
fn payload_as_str(payload: &dyn Any) -> &str {
@@ -738,7 +756,6 @@ fn payload_as_str(payload: &dyn Any) -> &str {
738756
/// abort or unwind.
739757
fn 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

Comments
 (0)