Impl Display for PanicPayload to simplify things. · model-checking/verify-rust-std@373fb60 (original) (raw)

`@@ -625,6 +625,12 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {

`

625

625

`}

`

626

626

`}

`

627

627

``

``

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

+

628

634

`struct StaticStrPayload(&'static str);

`

629

635

``

630

636

`unsafe impl PanicPayload for StaticStrPayload {

`

`@@ -637,21 +643,25 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {

`

637

643

`}

`

638

644

`}

`

639

645

``

``

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

+

640

652

`let loc = info.location().unwrap(); // The current implementation always returns Some

`

641

653

`let msg = info.message();

`

642

654

`crate::sys_common::backtrace::__rust_end_short_backtrace(move || {

`

643

655

`if let Some(s) = msg.as_str() {

`

644

656

`rust_panic_with_hook(

`

645

657

`&mut StaticStrPayload(s),

`

646

``

`-

Some(msg),

`

647

658

` loc,

`

648

659

` info.can_unwind(),

`

649

660

` info.force_no_backtrace(),

`

650

661

`);

`

651

662

`} else {

`

652

663

`rust_panic_with_hook(

`

653

664

`&mut FormatStringPayload { inner: &msg, string: None },

`

654

``

`-

Some(msg),

`

655

665

` loc,

`

656

666

` info.can_unwind(),

`

657

667

` info.force_no_backtrace(),

`

`@@ -681,7 +691,6 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {

`

681

691

`return crate::sys_common::backtrace::__rust_end_short_backtrace(move || {

`

682

692

`rust_panic_with_hook(

`

683

693

`&mut Payload::new(msg),

`

684

``

`-

None,

`

685

694

` loc,

`

686

695

`/* can_unwind */ true,

`

687

696

`/* force_no_backtrace */ false,

`

`@@ -719,6 +728,15 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {

`

719

728

`}

`

720

729

`}

`

721

730

`}

`

``

731

+

``

732

`+

impl<A: Send + 'static> fmt::Display for Payload {

`

``

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

`+

}

`

722

740

`}

`

723

741

``

724

742

`fn payload_as_str(payload: &dyn Any) -> &str {

`

`@@ -738,7 +756,6 @@ fn payload_as_str(payload: &dyn Any) -> &str {

`

738

756

`/// abort or unwind.

`

739

757

`fn rust_panic_with_hook(

`

740

758

`payload: &mut dyn PanicPayload,

`

741

``

`-

message: Option<fmt::Arguments<'_>>,

`

742

759

`location: &Location<'_>,

`

743

760

`can_unwind: bool,

`

744

761

`force_no_backtrace: bool,

`

`@@ -765,11 +782,7 @@ fn rust_panic_with_hook(

`

765

782

` panic_count::MustAbort::AlwaysAbort => {

`

766

783

`// Unfortunately, this does not print a backtrace, because creating

`

767

784

`` // 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");

`

773

786

`}

`

774

787

`}

`

775

788

`crate::sys::abort_internal();

`

`@@ -825,6 +838,12 @@ pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {

`

825

838

`}

`

826

839

`}

`

827

840

``

``

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

+

828

847

`rust_panic(&mut RewrapBox(payload))

`

829

848

`}

`

830

849

``