From e3e815370e824a9d7e74024da54acbe5ac477786 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 19 Sep 2023 18:06:22 +0200 Subject: [PATCH 01/24] Split core's PanicInfo and std's PanicInfo. --- library/core/src/panic/panic_info.rs | 80 ++------------ library/core/src/panicking.rs | 4 +- library/std/src/panic.rs | 153 ++++++++++++++++++++++++++- library/std/src/panicking.rs | 37 +++---- 4 files changed, 177 insertions(+), 97 deletions(-) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 40326221258..2b7bc76fbc2 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -1,31 +1,14 @@ -use crate::any::Any; use crate::fmt; use crate::panic::Location; /// A struct providing information about a panic. /// -/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] -/// function. -/// -/// [`set_hook`]: ../../std/panic/fn.set_hook.html -/// -/// # Examples -/// -/// ```should_panic -/// use std::panic; -/// -/// panic::set_hook(Box::new(|panic_info| { -/// println!("panic occurred: {panic_info}"); -/// })); -/// -/// panic!("critical system failure"); -/// ``` +/// A `PanicInfo` structure is passed to the panic handler defined by `#[panic_handler]`. #[lang = "panic_info"] #[stable(feature = "panic_hooks", since = "1.10.0")] #[derive(Debug)] pub struct PanicInfo<'a> { - payload: &'a (dyn Any + Send), - message: Option<&'a fmt::Arguments<'a>>, + message: fmt::Arguments<'a>, location: &'a Location<'a>, can_unwind: bool, force_no_backtrace: bool, @@ -40,51 +23,12 @@ impl<'a> PanicInfo<'a> { #[doc(hidden)] #[inline] pub fn internal_constructor( - message: Option<&'a fmt::Arguments<'a>>, + message: fmt::Arguments<'a>, location: &'a Location<'a>, can_unwind: bool, force_no_backtrace: bool, ) -> Self { - struct NoPayload; - PanicInfo { location, message, payload: &NoPayload, can_unwind, force_no_backtrace } - } - - #[unstable( - feature = "panic_internals", - reason = "internal details of the implementation of the `panic!` and related macros", - issue = "none" - )] - #[doc(hidden)] - #[inline] - pub fn set_payload(&mut self, info: &'a (dyn Any + Send)) { - self.payload = info; - } - - /// Returns the payload associated with the panic. - /// - /// This will commonly, but not always, be a `&'static str` or [`String`]. - /// - /// [`String`]: ../../std/string/struct.String.html - /// - /// # Examples - /// - /// ```should_panic - /// use std::panic; - /// - /// panic::set_hook(Box::new(|panic_info| { - /// if let Some(s) = panic_info.payload().downcast_ref::<&str>() { - /// println!("panic occurred: {s:?}"); - /// } else { - /// println!("panic occurred"); - /// } - /// })); - /// - /// panic!("Normal panic"); - /// ``` - #[must_use] - #[stable(feature = "panic_hooks", since = "1.10.0")] - pub fn payload(&self) -> &(dyn Any + Send) { - self.payload + PanicInfo { location, message, can_unwind, force_no_backtrace } } /// If the `panic!` macro from the `core` crate (not from `std`) @@ -92,7 +36,7 @@ impl<'a> PanicInfo<'a> { /// returns that message ready to be used for example with [`fmt::write`] #[must_use] #[unstable(feature = "panic_info_message", issue = "66745")] - pub fn message(&self) -> Option<&fmt::Arguments<'_>> { + pub fn message(&self) -> fmt::Arguments<'_> { self.message } @@ -161,18 +105,8 @@ impl fmt::Display for PanicInfo<'_> { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("panicked at ")?; self.location.fmt(formatter)?; - formatter.write_str(":")?; - if let Some(message) = self.message { - formatter.write_str("\n")?; - formatter.write_fmt(*message)?; - } else if let Some(payload) = self.payload.downcast_ref::<&'static str>() { - formatter.write_str("\n")?; - formatter.write_str(payload)?; - } - // NOTE: we cannot use downcast_ref::() here - // since String is not available in core! - // The payload is a String when `std::panic!` is called with multiple arguments, - // but in that case the message is also available. + formatter.write_str(":\n")?; + formatter.write_fmt(self.message)?; Ok(()) } } diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index ca06e059b75..683894a5ab4 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -62,7 +62,7 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { } let pi = PanicInfo::internal_constructor( - Some(&fmt), + fmt, Location::caller(), /* can_unwind */ true, /* force_no_backtrace */ false, @@ -100,7 +100,7 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo // PanicInfo with the `can_unwind` flag set to false forces an abort. let pi = PanicInfo::internal_constructor( - Some(&fmt), + &fmt, Location::caller(), /* can_unwind */ false, force_no_backtrace, diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index e63b46ab705..18072f6fe3d 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -4,11 +4,162 @@ use crate::any::Any; use crate::collections; +use crate::fmt; use crate::panicking; use crate::sync::atomic::{AtomicU8, Ordering}; use crate::sync::{Condvar, Mutex, RwLock}; use crate::thread::Result; +/// A struct providing information about a panic. +/// +/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] +/// function. +/// +/// [`set_hook`]: ../../std/panic/fn.set_hook.html +/// +/// # Examples +/// +/// ```should_panic +/// use std::panic; +/// +/// panic::set_hook(Box::new(|panic_info| { +/// println!("panic occurred: {panic_info}"); +/// })); +/// +/// panic!("critical system failure"); +/// ``` +#[stable(feature = "panic_hooks", since = "1.10.0")] +#[derive(Debug)] +pub struct PanicInfo<'a> { + payload: &'a (dyn Any + Send), + location: &'a Location<'a>, + can_unwind: bool, + force_no_backtrace: bool, +} + +impl<'a> PanicInfo<'a> { + #[unstable(feature = "panic_internals", issue = "none")] + #[doc(hidden)] + #[inline] + pub fn internal_constructor( + location: &'a Location<'a>, + can_unwind: bool, + force_no_backtrace: bool, + ) -> Self { + struct NoPayload; + PanicInfo { payload: &NoPayload, location, can_unwind, force_no_backtrace } + } + + #[unstable(feature = "panic_internals", issue = "none")] + #[doc(hidden)] + #[inline] + pub fn set_payload(&mut self, info: &'a (dyn Any + Send)) { + self.payload = info; + } + + /// Returns the payload associated with the panic. + /// + /// This will commonly, but not always, be a `&'static str` or [`String`]. + /// + /// [`String`]: ../../std/string/struct.String.html + /// + /// # Examples + /// + /// ```should_panic + /// use std::panic; + /// + /// panic::set_hook(Box::new(|panic_info| { + /// if let Some(s) = panic_info.payload().downcast_ref::<&str>() { + /// println!("panic occurred: {s:?}"); + /// } else { + /// println!("panic occurred"); + /// } + /// })); + /// + /// panic!("Normal panic"); + /// ``` + #[must_use] + #[stable(feature = "panic_hooks", since = "1.10.0")] + pub fn payload(&self) -> &(dyn Any + Send) { + self.payload + } + + /// Returns information about the location from which the panic originated, + /// if available. + /// + /// This method will currently always return [`Some`], but this may change + /// in future versions. + /// + /// # Examples + /// + /// ```should_panic + /// use std::panic; + /// + /// panic::set_hook(Box::new(|panic_info| { + /// if let Some(location) = panic_info.location() { + /// println!("panic occurred in file '{}' at line {}", + /// location.file(), + /// location.line(), + /// ); + /// } else { + /// println!("panic occurred but can't get location information..."); + /// } + /// })); + /// + /// panic!("Normal panic"); + /// ``` + #[must_use] + #[stable(feature = "panic_hooks", since = "1.10.0")] + pub fn location(&self) -> Option<&Location<'_>> { + // NOTE: If this is changed to sometimes return None, + // deal with that case in std::panicking::default_hook and core::panicking::panic_fmt. + Some(&self.location) + } + + /// Returns whether the panic handler is allowed to unwind the stack from + /// the point where the panic occurred. + /// + /// This is true for most kinds of panics with the exception of panics + /// caused by trying to unwind out of a `Drop` implementation or a function + /// whose ABI does not support unwinding. + /// + /// It is safe for a panic handler to unwind even when this function returns + /// false, however this will simply cause the panic handler to be called + /// again. + #[must_use] + #[unstable(feature = "panic_can_unwind", issue = "92988")] + pub fn can_unwind(&self) -> bool { + self.can_unwind + } + + #[unstable( + feature = "panic_internals", + reason = "internal details of the implementation of the `panic!` and related macros", + issue = "none" + )] + #[doc(hidden)] + #[inline] + pub fn force_no_backtrace(&self) -> bool { + self.force_no_backtrace + } +} + +#[stable(feature = "panic_hook_display", since = "1.26.0")] +impl fmt::Display for PanicInfo<'_> { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("panicked at ")?; + self.location.fmt(formatter)?; + if let Some(payload) = self.payload.downcast_ref::<&'static str>() { + formatter.write_str(":\n")?; + formatter.write_str(payload)?; + } else if let Some(payload) = self.payload.downcast_ref::() { + formatter.write_str(":\n")?; + formatter.write_str(payload)?; + } + Ok(()) + } +} + #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")] #[allow_internal_unstable(libstd_sys_internals, const_format_args, panic_internals, rt)] @@ -43,7 +194,7 @@ pub use crate::panicking::{set_hook, take_hook}; pub use crate::panicking::update_hook; #[stable(feature = "panic_hooks", since = "1.10.0")] -pub use core::panic::{Location, PanicInfo}; +pub use core::panic::Location; #[stable(feature = "catch_unwind", since = "1.9.0")] pub use core::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 5699937cdb4..d6ef8f0dbbe 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -9,8 +9,8 @@ #![deny(unsafe_op_in_unsafe_fn)] -use crate::panic::BacktraceStyle; -use core::panic::{Location, PanicInfo, PanicPayload}; +use crate::panic::{BacktraceStyle, PanicInfo}; +use core::panic::{Location, PanicPayload}; use crate::any::Any; use crate::fmt; @@ -597,7 +597,7 @@ pub fn panicking() -> bool { /// Entry point of panics from the core crate (`panic_impl` lang item). #[cfg(not(any(test, doctest)))] #[panic_handler] -pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { +pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { struct FormatStringPayload<'a> { inner: &'a fmt::Arguments<'a>, string: Option, @@ -648,22 +648,20 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! { } let loc = info.location().unwrap(); // The current implementation always returns Some - let msg = info.message().unwrap(); // The current implementation always returns Some + let msg = info.message(); crate::sys_common::backtrace::__rust_end_short_backtrace(move || { - // FIXME: can we just pass `info` along rather than taking it apart here, only to have - // `rust_panic_with_hook` construct a new `PanicInfo`? - if let Some(msg) = msg.as_str() { + if let Some(s) = msg.as_str() { rust_panic_with_hook( - &mut StaticStrPayload(msg), - info.message(), + &mut StaticStrPayload(s), + Some(msg), loc, info.can_unwind(), info.force_no_backtrace(), ); } else { rust_panic_with_hook( - &mut FormatStringPayload::new(msg), - info.message(), + &mut FormatStringPayload::new(&msg), + Some(msg), loc, info.can_unwind(), info.force_no_backtrace(), @@ -740,7 +738,7 @@ pub const fn begin_panic(msg: M) -> ! { /// abort or unwind. fn rust_panic_with_hook( payload: &mut dyn PanicPayload, - message: Option<&fmt::Arguments<'_>>, + message: Option>, location: &Location<'_>, can_unwind: bool, force_no_backtrace: bool, @@ -767,20 +765,17 @@ fn rust_panic_with_hook( panic_count::MustAbort::AlwaysAbort => { // Unfortunately, this does not print a backtrace, because creating // a `Backtrace` will allocate, which we must avoid here. - let panicinfo = PanicInfo::internal_constructor( - message, - location, - can_unwind, - force_no_backtrace, - ); - rtprintpanic!("{panicinfo}\npanicked after panic::always_abort(), aborting.\n"); + if let Some(message) = message { + rtprintpanic!("aborting due to panic at {location}:\n{message}\n"); + } else { + rtprintpanic!("aborting due to panic at {location}\n"); + } } } crate::sys::abort_internal(); } - let mut info = - PanicInfo::internal_constructor(message, location, can_unwind, force_no_backtrace); + let mut info = PanicInfo::internal_constructor(location, can_unwind, force_no_backtrace); let hook = HOOK.read().unwrap_or_else(PoisonError::into_inner); match *hook { // Some platforms (like wasm) know that printing to stderr won't ever actually From a519dc85f8aa1348ec9cdc46906bbcbb9d115981 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 26 Sep 2023 14:40:36 +0200 Subject: [PATCH 02/24] Document difference between core and std's PanicInfo. --- library/core/src/panic/panic_info.rs | 9 +++++++++ library/std/src/panic.rs | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 2b7bc76fbc2..6a600e2d431 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -4,6 +4,15 @@ use crate::panic::Location; /// A struct providing information about a panic. /// /// A `PanicInfo` structure is passed to the panic handler defined by `#[panic_handler]`. +/// +/// There two `PanicInfo` types: +/// - `core::panic::PanicInfo`, which is used as an argument to a `#[panic_handler]` in `#![no_std]` programs. +/// - [`std::panic::PanicInfo`], which is used as an argument to a panic hook set by [`std::panic::set_hook`]. +/// +/// This is the first one. +/// +/// [`std::panic::set_hook`]: ../../std/panic/fn.set_hook.html +/// [`std::panic::PanicInfo`]: ../../std/panic/struct.PanicInfo.html #[lang = "panic_info"] #[stable(feature = "panic_hooks", since = "1.10.0")] #[derive(Debug)] diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 18072f6fe3d..54ab433d820 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -12,11 +12,16 @@ use crate::thread::Result; /// A struct providing information about a panic. /// -/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] -/// function. +/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] function. /// /// [`set_hook`]: ../../std/panic/fn.set_hook.html /// +/// There two `PanicInfo` types: +/// - [`core::panic::PanicInfo`], which is used as an argument to a `#[panic_handler]` in `#![no_std]` programs. +/// - `std::panic::PanicInfo`, which is used as an argument to a panic hook set by [`std::panic::set_hook`]. +/// +/// This is the second one. +/// /// # Examples /// /// ```should_panic @@ -28,6 +33,8 @@ use crate::thread::Result; /// /// panic!("critical system failure"); /// ``` +/// +/// [`core::panic::PanicInfo`]: ../../core/panic/struct.PanicInfo.html #[stable(feature = "panic_hooks", since = "1.10.0")] #[derive(Debug)] pub struct PanicInfo<'a> { From 16dfc6ddc114efa7174e2daa972916bbd2f67d87 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 26 Sep 2023 14:40:50 +0200 Subject: [PATCH 03/24] Add core::panic::PanicInfo::payload() for compatibility. --- library/core/src/panic/panic_info.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 6a600e2d431..5b784ff4f80 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -81,6 +81,24 @@ impl<'a> PanicInfo<'a> { Some(&self.location) } + /// Returns the payload associated with the panic. + /// + /// On `core::panic::PanicInfo`, this method never returns anything useful. + /// It only exists because of compatibility with [`std::panic::PanicInfo`], + /// which used to be the same type. + /// + /// See [`std::panic::PanicInfo::payload`]. + /// + /// [`std::panic::PanicInfo`]: ../../std/panic/struct.PanicInfo.html + /// [`std::panic::PanicInfo::payload`]: ../../std/panic/struct.PanicInfo.html#method.payload + #[deprecated(since = "1.74.0", note = "this never returns anything useful")] + #[stable(feature = "panic_hooks", since = "1.10.0")] + #[allow(deprecated, deprecated_in_future)] + pub fn payload(&self) -> &(dyn crate::any::Any + Send) { + struct NoPayload; + &NoPayload + } + /// Returns whether the panic handler is allowed to unwind the stack from /// the point where the panic occurred. /// From 331b8a3edde67eb2418e7d9c5c6ef8d9a2a8c0b8 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 26 Sep 2023 15:02:34 +0200 Subject: [PATCH 04/24] Fix doc link. --- library/std/src/panic.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 54ab433d820..e2d182b4ba0 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -14,11 +14,9 @@ use crate::thread::Result; /// /// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] function. /// -/// [`set_hook`]: ../../std/panic/fn.set_hook.html -/// /// There two `PanicInfo` types: /// - [`core::panic::PanicInfo`], which is used as an argument to a `#[panic_handler]` in `#![no_std]` programs. -/// - `std::panic::PanicInfo`, which is used as an argument to a panic hook set by [`std::panic::set_hook`]. +/// - `std::panic::PanicInfo`, which is used as an argument to a panic hook set by [`set_hook`]. /// /// This is the second one. /// @@ -35,6 +33,7 @@ use crate::thread::Result; /// ``` /// /// [`core::panic::PanicInfo`]: ../../core/panic/struct.PanicInfo.html +/// [`set_hook`]: ../../std/panic/fn.set_hook.html #[stable(feature = "panic_hooks", since = "1.10.0")] #[derive(Debug)] pub struct PanicInfo<'a> { From 83dd214f0651c680cea647c0ce7ccf451840f549 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:12:37 +0200 Subject: [PATCH 05/24] Update doc comment about core::panicking. --- library/core/src/panicking.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 683894a5ab4..aa11ae28dcb 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -1,7 +1,14 @@ //! Panic support for core //! -//! The core library cannot define panicking, but it does *declare* panicking. This -//! means that the functions inside of core are allowed to panic, but to be +//! In core, panicking is always done with a message, resulting in a core::panic::PanicInfo +//! containing a fmt::Arguments. In std, however, panicking can be done with panic_any, which throws +//! a Box containing any type of value. Because of this, std::panic::PanicInfo is a +//! different type, which contains a &dyn Any instead of a fmt::Arguments. +//! std's panic handler will convert the fmt::Arguments to a &dyn Any containing either a +//! &'static str or String containing the formatted message. +//! +//! The core library cannot define any panic handler, but it can invoke it. +//! This means that the functions inside of core are allowed to panic, but to be //! useful an upstream crate must define panicking for core to use. The current //! interface for panicking is: //! @@ -10,11 +17,6 @@ //! # { loop {} } //! ``` //! -//! This definition allows for panicking with any general message, but it does not -//! allow for failing with a `Box` value. (`PanicInfo` just contains a `&(dyn Any + Send)`, -//! for which we fill in a dummy value in `PanicInfo::internal_constructor`.) -//! The reason for this is that core is not allowed to allocate. -//! //! This module contains a few other panicking functions, but these are just the //! necessary lang items for the compiler. All panics are funneled through this //! one function. The actual symbol is declared through the `#[panic_handler]` attribute. From 0266bbf6e4be7b025f4798046121607c587e94cb Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:13:05 +0200 Subject: [PATCH 06/24] Remove core::panic::PanicInfo::internal_constructor. It no longer needs to be public. --- library/core/src/panic/panic_info.rs | 8 +------- library/core/src/panicking.rs | 10 +++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 5b784ff4f80..2a0cd8fda61 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -24,14 +24,8 @@ pub struct PanicInfo<'a> { } impl<'a> PanicInfo<'a> { - #[unstable( - feature = "panic_internals", - reason = "internal details of the implementation of the `panic!` and related macros", - issue = "none" - )] - #[doc(hidden)] #[inline] - pub fn internal_constructor( + pub(crate) fn new( message: fmt::Arguments<'a>, location: &'a Location<'a>, can_unwind: bool, diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index aa11ae28dcb..a6e55280946 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -63,7 +63,7 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { fn panic_impl(pi: &PanicInfo<'_>) -> !; } - let pi = PanicInfo::internal_constructor( + let pi = PanicInfo::new( fmt, Location::caller(), /* can_unwind */ true, @@ -101,12 +101,8 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo } // PanicInfo with the `can_unwind` flag set to false forces an abort. - let pi = PanicInfo::internal_constructor( - &fmt, - Location::caller(), - /* can_unwind */ false, - force_no_backtrace, - ); + let pi = + PanicInfo::new(fmt, Location::caller(), /* can_unwind */ false, force_no_backtrace); // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. unsafe { panic_impl(&pi) } From 0642cb2994c192b9e910855b7c9a258376c8e0ff Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:14:15 +0200 Subject: [PATCH 07/24] Remove std::panic::PanicInfo::internal_constructor+set_payload. We can just set the payload immediately in the constructor, and the constructor does not need to be public. --- library/std/src/panic.rs | 15 +++------------ library/std/src/panicking.rs | 13 ++++--------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index e2d182b4ba0..7adbce859f5 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -44,23 +44,14 @@ pub struct PanicInfo<'a> { } impl<'a> PanicInfo<'a> { - #[unstable(feature = "panic_internals", issue = "none")] - #[doc(hidden)] #[inline] - pub fn internal_constructor( + pub(crate) fn new( location: &'a Location<'a>, + payload: &'a (dyn Any + Send), can_unwind: bool, force_no_backtrace: bool, ) -> Self { - struct NoPayload; - PanicInfo { payload: &NoPayload, location, can_unwind, force_no_backtrace } - } - - #[unstable(feature = "panic_internals", issue = "none")] - #[doc(hidden)] - #[inline] - pub fn set_payload(&mut self, info: &'a (dyn Any + Send)) { - self.payload = info; + PanicInfo { payload, location, can_unwind, force_no_backtrace } } /// Returns the payload associated with the panic. diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index d6ef8f0dbbe..6d6aca01561 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -775,9 +775,7 @@ fn rust_panic_with_hook( crate::sys::abort_internal(); } - let mut info = PanicInfo::internal_constructor(location, can_unwind, force_no_backtrace); - let hook = HOOK.read().unwrap_or_else(PoisonError::into_inner); - match *hook { + match *HOOK.read().unwrap_or_else(PoisonError::into_inner) { // Some platforms (like wasm) know that printing to stderr won't ever actually // print anything, and if that's the case we can skip the default // hook. Since string formatting happens lazily when calling `payload` @@ -786,15 +784,12 @@ fn rust_panic_with_hook( // formatting.) Hook::Default if panic_output().is_none() => {} Hook::Default => { - info.set_payload(payload.get()); - default_hook(&info); + default_hook(&PanicInfo::new(location, payload.get(), can_unwind, force_no_backtrace)); } Hook::Custom(ref hook) => { - info.set_payload(payload.get()); - hook(&info); + hook(&PanicInfo::new(location, payload.get(), can_unwind, force_no_backtrace)); } - }; - drop(hook); + } // Indicate that we have finished executing the panic hook. After this point // it is fine if there is a panic while executing destructors, as long as it From 0087d899835cbcfa7c52e32c1825f8740621a485 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:14:55 +0200 Subject: [PATCH 08/24] Mark some PanicInfo methods as #[inline] for consistency. --- library/std/src/panic.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 7adbce859f5..ca0ec12a600 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -76,6 +76,7 @@ impl<'a> PanicInfo<'a> { /// panic!("Normal panic"); /// ``` #[must_use] + #[inline] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn payload(&self) -> &(dyn Any + Send) { self.payload @@ -106,6 +107,7 @@ impl<'a> PanicInfo<'a> { /// panic!("Normal panic"); /// ``` #[must_use] + #[inline] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn location(&self) -> Option<&Location<'_>> { // NOTE: If this is changed to sometimes return None, @@ -124,6 +126,7 @@ impl<'a> PanicInfo<'a> { /// false, however this will simply cause the panic handler to be called /// again. #[must_use] + #[inline] #[unstable(feature = "panic_can_unwind", issue = "92988")] pub fn can_unwind(&self) -> bool { self.can_unwind From 4e356f3184ff43aeb68968a8ab96a0faacf6281b Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:15:21 +0200 Subject: [PATCH 09/24] Move downcasting panic payload to str to a function. --- library/std/src/panicking.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 6d6aca01561..53df0bb40b5 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -248,13 +248,7 @@ fn default_hook(info: &PanicInfo<'_>) { // The current implementation always returns `Some`. let location = info.location().unwrap(); - let msg = match info.payload().downcast_ref::<&'static str>() { - Some(s) => *s, - None => match info.payload().downcast_ref::() { - Some(s) => &s[..], - None => "Box", - }, - }; + let msg = payload_as_str(info.payload()); let thread = thread::try_current(); let name = thread.as_ref().and_then(|t| t.name()).unwrap_or(""); @@ -731,6 +725,16 @@ pub const fn begin_panic(msg: M) -> ! { } } +fn payload_as_str(payload: &dyn Any) -> &str { + if let Some(&s) = payload.downcast_ref::<&'static str>() { + s + } else if let Some(s) = payload.downcast_ref::() { + s.as_str() + } else { + "Box" + } +} + /// Central point for dispatching panics. /// /// Executes the primary logic for a panic, including checking for recursive From 22f7399b32dcf1bd4d38b7e88a9b482fba889a8f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:16:10 +0200 Subject: [PATCH 10/24] Use unnamed lifetimes for [..]Payload impl blocks. --- library/std/src/panicking.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 53df0bb40b5..a460f41b93d 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -597,11 +597,7 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { string: Option, } - impl<'a> FormatStringPayload<'a> { - fn new(inner: &'a fmt::Arguments<'a>) -> Self { - Self { inner, string: None } - } - + impl FormatStringPayload<'_> { fn fill(&mut self) -> &mut String { use crate::fmt::Write; @@ -615,7 +611,7 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { } } - unsafe impl<'a> PanicPayload for FormatStringPayload<'a> { + unsafe impl PanicPayload for FormatStringPayload<'_> { fn take_box(&mut self) -> *mut (dyn Any + Send) { // We do two allocations here, unfortunately. But (a) they're required with the current // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in @@ -654,7 +650,7 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { ); } else { rust_panic_with_hook( - &mut FormatStringPayload::new(&msg), + &mut FormatStringPayload { inner: &msg, string: None }, Some(msg), loc, info.can_unwind(), From 1642de33d3c4e59f3b4a766d0ad1abe046f0433f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:17:16 +0200 Subject: [PATCH 11/24] Impl Display for PanicPayload to simplify things. --- library/core/src/panic.rs | 2 +- library/std/src/panicking.rs | 37 +++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 8771f40f9b4..c9f3702e178 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -144,7 +144,7 @@ pub macro unreachable_2021 { /// use. #[unstable(feature = "std_internals", issue = "none")] #[doc(hidden)] -pub unsafe trait PanicPayload { +pub unsafe trait PanicPayload: crate::fmt::Display { /// Take full ownership of the contents. /// The return type is actually `Box`, but we cannot use `Box` in core. /// diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index a460f41b93d..362aa8d06f3 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -625,6 +625,12 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { } } + impl fmt::Display for FormatStringPayload<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(s) = &self.string { f.write_str(s) } else { f.write_fmt(*self.inner) } + } + } + struct StaticStrPayload(&'static str); unsafe impl PanicPayload for StaticStrPayload { @@ -637,13 +643,18 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { } } + impl fmt::Display for StaticStrPayload { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.0) + } + } + let loc = info.location().unwrap(); // The current implementation always returns Some let msg = info.message(); crate::sys_common::backtrace::__rust_end_short_backtrace(move || { if let Some(s) = msg.as_str() { rust_panic_with_hook( &mut StaticStrPayload(s), - Some(msg), loc, info.can_unwind(), info.force_no_backtrace(), @@ -651,7 +662,6 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { } else { rust_panic_with_hook( &mut FormatStringPayload { inner: &msg, string: None }, - Some(msg), loc, info.can_unwind(), info.force_no_backtrace(), @@ -681,7 +691,6 @@ pub const fn begin_panic(msg: M) -> ! { return crate::sys_common::backtrace::__rust_end_short_backtrace(move || { rust_panic_with_hook( &mut Payload::new(msg), - None, loc, /* can_unwind */ true, /* force_no_backtrace */ false, @@ -719,6 +728,15 @@ pub const fn begin_panic(msg: M) -> ! { } } } + + impl fmt::Display for Payload { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self.inner { + Some(a) => f.write_str(payload_as_str(a)), + None => process::abort(), + } + } + } } fn payload_as_str(payload: &dyn Any) -> &str { @@ -738,7 +756,6 @@ fn payload_as_str(payload: &dyn Any) -> &str { /// abort or unwind. fn rust_panic_with_hook( payload: &mut dyn PanicPayload, - message: Option>, location: &Location<'_>, can_unwind: bool, force_no_backtrace: bool, @@ -765,11 +782,7 @@ fn rust_panic_with_hook( panic_count::MustAbort::AlwaysAbort => { // Unfortunately, this does not print a backtrace, because creating // a `Backtrace` will allocate, which we must avoid here. - if let Some(message) = message { - rtprintpanic!("aborting due to panic at {location}:\n{message}\n"); - } else { - rtprintpanic!("aborting due to panic at {location}\n"); - } + rtprintpanic!("aborting due to panic at {location}:\n{payload}\n"); } } crate::sys::abort_internal(); @@ -825,6 +838,12 @@ pub fn rust_panic_without_hook(payload: Box) -> ! { } } + impl fmt::Display for RewrapBox { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(payload_as_str(&self.0)) + } + } + rust_panic(&mut RewrapBox(payload)) } From bab26b02c78f46265d59dda4cf558918008617b2 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:29:58 +0200 Subject: [PATCH 12/24] Reorder body of begin_panic for consistency. In the other functions, we put the struct and impl blocks first, such that the return expression can be at the end of the body as usual. --- library/std/src/panicking.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 362aa8d06f3..15544de9ff8 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -687,26 +687,10 @@ pub const fn begin_panic(msg: M) -> ! { intrinsics::abort() } - let loc = Location::caller(); - return crate::sys_common::backtrace::__rust_end_short_backtrace(move || { - rust_panic_with_hook( - &mut Payload::new(msg), - loc, - /* can_unwind */ true, - /* force_no_backtrace */ false, - ) - }); - struct Payload { inner: Option, } - impl Payload { - fn new(inner: A) -> Payload { - Payload { inner: Some(inner) } - } - } - unsafe impl PanicPayload for Payload { fn take_box(&mut self) -> *mut (dyn Any + Send) { // Note that this should be the only allocation performed in this code path. Currently @@ -729,7 +713,7 @@ pub const fn begin_panic(msg: M) -> ! { } } - impl fmt::Display for Payload { + impl fmt::Display for Payload { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.inner { Some(a) => f.write_str(payload_as_str(a)), @@ -737,6 +721,16 @@ pub const fn begin_panic(msg: M) -> ! { } } } + + let loc = Location::caller(); + crate::sys_common::backtrace::__rust_end_short_backtrace(move || { + rust_panic_with_hook( + &mut Payload { inner: Some(msg) }, + loc, + /* can_unwind */ true, + /* force_no_backtrace */ false, + ) + }) } fn payload_as_str(payload: &dyn Any) -> &str { From 6b2d7c47072a466b938fac75db416c9adbf59892 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 2 Oct 2023 14:58:31 +0200 Subject: [PATCH 13/24] Fix invalid markdown/html. --- library/core/src/panicking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index a6e55280946..0793c1b0f29 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -2,7 +2,7 @@ //! //! In core, panicking is always done with a message, resulting in a core::panic::PanicInfo //! containing a fmt::Arguments. In std, however, panicking can be done with panic_any, which throws -//! a Box containing any type of value. Because of this, std::panic::PanicInfo is a +//! a `Box` containing any type of value. Because of this, std::panic::PanicInfo is a //! different type, which contains a &dyn Any instead of a fmt::Arguments. //! std's panic handler will convert the fmt::Arguments to a &dyn Any containing either a //! &'static str or String containing the formatted message. From b6180a918538b3afa04bbc1c5515c0be2154e1b6 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 31 Jan 2024 09:35:17 +0100 Subject: [PATCH 14/24] Formatting. --- library/core/src/panicking.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 0793c1b0f29..94b4113b245 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -101,8 +101,12 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo } // PanicInfo with the `can_unwind` flag set to false forces an abort. - let pi = - PanicInfo::new(fmt, Location::caller(), /* can_unwind */ false, force_no_backtrace); + let pi = PanicInfo::new( + fmt, + Location::caller(), + /* can_unwind */ false, + force_no_backtrace, + ); // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. unsafe { panic_impl(&pi) } From 64e56db72aa2b1f112504504a7dceba63881cdef Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 31 Jan 2024 10:27:29 +0100 Subject: [PATCH 15/24] Rename std::panic::PanicInfo to PanicHookInfo. --- compiler/rustc_driver_impl/src/lib.rs | 9 +++--- library/core/src/error.md | 4 +-- library/core/src/panic/location.rs | 3 +- library/core/src/panic/panic_info.rs | 17 ++++------ library/core/src/panicking.rs | 12 +++---- library/std/src/panic.rs | 31 +++++++++++-------- library/std/src/panicking.rs | 25 +++++++++------ library/test/src/lib.rs | 6 ++-- tests/ui/duplicate_entry_error.rs | 4 ++- tests/ui/duplicate_entry_error.stderr | 2 +- tests/ui/panic-handler/panic-handler-std.rs | 3 +- .../ui/panic-handler/panic-handler-std.stderr | 2 +- 12 files changed, 63 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 93a65290602..fef4ce0b471 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -52,7 +52,7 @@ use std::ffi::OsString; use std::fmt::Write as _; use std::fs::{self, File}; use std::io::{self, IsTerminal, Read, Write}; -use std::panic::{self, catch_unwind, PanicInfo}; +use std::panic::{self, catch_unwind, PanicHookInfo}; use std::path::PathBuf; use std::process::{self, Command, Stdio}; use std::str; @@ -1366,11 +1366,10 @@ pub fn install_ice_hook( let using_internal_features = Arc::new(std::sync::atomic::AtomicBool::default()); let using_internal_features_hook = using_internal_features.clone(); panic::update_hook(Box::new( - move |default_hook: &(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static), - info: &PanicInfo<'_>| { + move |default_hook: &(dyn Fn(&PanicHookInfo<'_>) + Send + Sync + 'static), + info: &PanicHookInfo<'_>| { // Lock stderr to prevent interleaving of concurrent panics. let _guard = io::stderr().lock(); - // If the error was caused by a broken pipe then this is not a bug. // Write the error and return immediately. See #98700. #[cfg(windows)] @@ -1431,7 +1430,7 @@ pub fn install_ice_hook( /// When `install_ice_hook` is called, this function will be called as the panic /// hook. fn report_ice( - info: &panic::PanicInfo<'_>, + info: &panic::PanicHookInfo<'_>, bug_report_url: &str, extra_info: fn(&DiagCtxt), using_internal_features: &AtomicBool, diff --git a/library/core/src/error.md b/library/core/src/error.md index a5deb71e6b8..8d5a623df4b 100644 --- a/library/core/src/error.md +++ b/library/core/src/error.md @@ -17,7 +17,7 @@ The following are the primary interfaces of the panic system and the responsibilities they cover: * [`panic!`] and [`panic_any`] (Constructing, Propagated automatically) -* [`PanicInfo`] (Reporting) +* [`PanicHookInfo`] (Reporting) * [`set_hook`], [`take_hook`], and [`#[panic_handler]`][panic-handler] (Reporting) * [`catch_unwind`] and [`resume_unwind`] (Discarding, Propagating) @@ -125,7 +125,7 @@ expect-as-precondition style error messages remember to focus on the word should be available and executable by the current user". [`panic_any`]: ../../std/panic/fn.panic_any.html -[`PanicInfo`]: crate::panic::PanicInfo +[`PanicHookInfo`]: ../../std/panic/struct.PanicHookInfo.html [`catch_unwind`]: ../../std/panic/fn.catch_unwind.html [`resume_unwind`]: ../../std/panic/fn.resume_unwind.html [`downcast`]: crate::error::Error diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index eb27da1724e..8c04994ac0f 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -2,9 +2,10 @@ use crate::fmt; /// A struct containing information about the location of a panic. /// -/// This structure is created by [`PanicInfo::location()`]. +/// This structure is created by [`PanicHookInfo::location()`] and [`PanicInfo::location()`]. /// /// [`PanicInfo::location()`]: crate::panic::PanicInfo::location +/// [`PanicHookInfo::location()`]: ../../std/panic/struct.PanicHookInfo.html#method.location /// /// # Examples /// diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 2a0cd8fda61..7c792a58d2a 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -5,14 +5,9 @@ use crate::panic::Location; /// /// A `PanicInfo` structure is passed to the panic handler defined by `#[panic_handler]`. /// -/// There two `PanicInfo` types: -/// - `core::panic::PanicInfo`, which is used as an argument to a `#[panic_handler]` in `#![no_std]` programs. -/// - [`std::panic::PanicInfo`], which is used as an argument to a panic hook set by [`std::panic::set_hook`]. +/// For the type used by the panic hook mechanism in `std`, see [`std::panic::PanicHookInfo`]. /// -/// This is the first one. -/// -/// [`std::panic::set_hook`]: ../../std/panic/fn.set_hook.html -/// [`std::panic::PanicInfo`]: ../../std/panic/struct.PanicInfo.html +/// [`std::panic::PanicHookInfo`]: ../../std/panic/struct.PanicHookInfo.html #[lang = "panic_info"] #[stable(feature = "panic_hooks", since = "1.10.0")] #[derive(Debug)] @@ -78,13 +73,13 @@ impl<'a> PanicInfo<'a> { /// Returns the payload associated with the panic. /// /// On `core::panic::PanicInfo`, this method never returns anything useful. - /// It only exists because of compatibility with [`std::panic::PanicInfo`], + /// It only exists because of compatibility with [`std::panic::PanicHookInfo`], /// which used to be the same type. /// - /// See [`std::panic::PanicInfo::payload`]. + /// See [`std::panic::PanicHookInfo::payload`]. /// - /// [`std::panic::PanicInfo`]: ../../std/panic/struct.PanicInfo.html - /// [`std::panic::PanicInfo::payload`]: ../../std/panic/struct.PanicInfo.html#method.payload + /// [`std::panic::PanicHookInfo`]: ../../std/panic/struct.PanicHookInfo.html + /// [`std::panic::PanicHookInfo::payload`]: ../../std/panic/struct.PanicHookInfo.html#method.payload #[deprecated(since = "1.74.0", note = "this never returns anything useful")] #[stable(feature = "panic_hooks", since = "1.10.0")] #[allow(deprecated, deprecated_in_future)] diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 94b4113b245..97fb1d6b732 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -1,11 +1,11 @@ //! Panic support for core //! -//! In core, panicking is always done with a message, resulting in a core::panic::PanicInfo -//! containing a fmt::Arguments. In std, however, panicking can be done with panic_any, which throws -//! a `Box` containing any type of value. Because of this, std::panic::PanicInfo is a -//! different type, which contains a &dyn Any instead of a fmt::Arguments. -//! std's panic handler will convert the fmt::Arguments to a &dyn Any containing either a -//! &'static str or String containing the formatted message. +//! In core, panicking is always done with a message, resulting in a `core::panic::PanicInfo` +//! containing a `fmt::Arguments`. In std, however, panicking can be done with panic_any, which +//! throws a `Box` containing any type of value. Because of this, +//! `std::panic::PanicHookInfo` is a different type, which contains a `&dyn Any` instead of a +//! `fmt::Arguments`. std's panic handler will convert the `fmt::Arguments` to a `&dyn Any` +//! containing either a `&'static str` or `String` containing the formatted message. //! //! The core library cannot define any panic handler, but it can invoke it. //! This means that the functions inside of core are allowed to panic, but to be diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index ca0ec12a600..77defe14b70 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -10,15 +10,21 @@ use crate::sync::atomic::{AtomicU8, Ordering}; use crate::sync::{Condvar, Mutex, RwLock}; use crate::thread::Result; +#[stable(feature = "panic_hooks", since = "1.10.0")] +#[deprecated( + since = "1.77.0", + note = "use `PanicHookInfo` instead", + suggestion = "std::panic::PanicHookInfo" +)] /// A struct providing information about a panic. /// -/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`] function. +/// `PanicInfo` has been renamed to [`PanicHookInfo`] to avoid confusion with +/// [`core::panic::PanicInfo`]. +pub type PanicInfo<'a> = PanicHookInfo<'a>; + +/// A struct providing information about a panic. /// -/// There two `PanicInfo` types: -/// - [`core::panic::PanicInfo`], which is used as an argument to a `#[panic_handler]` in `#![no_std]` programs. -/// - `std::panic::PanicInfo`, which is used as an argument to a panic hook set by [`set_hook`]. -/// -/// This is the second one. +/// `PanicHookInfo` structure is passed to a panic hook set by the [`set_hook`] function. /// /// # Examples /// @@ -32,18 +38,17 @@ use crate::thread::Result; /// panic!("critical system failure"); /// ``` /// -/// [`core::panic::PanicInfo`]: ../../core/panic/struct.PanicInfo.html /// [`set_hook`]: ../../std/panic/fn.set_hook.html -#[stable(feature = "panic_hooks", since = "1.10.0")] +#[stable(feature = "panic_hook_info", since = "CURRENT_RUSTC_VERSION")] #[derive(Debug)] -pub struct PanicInfo<'a> { +pub struct PanicHookInfo<'a> { payload: &'a (dyn Any + Send), location: &'a Location<'a>, can_unwind: bool, force_no_backtrace: bool, } -impl<'a> PanicInfo<'a> { +impl<'a> PanicHookInfo<'a> { #[inline] pub(crate) fn new( location: &'a Location<'a>, @@ -51,7 +56,7 @@ impl<'a> PanicInfo<'a> { can_unwind: bool, force_no_backtrace: bool, ) -> Self { - PanicInfo { payload, location, can_unwind, force_no_backtrace } + PanicHookInfo { payload, location, can_unwind, force_no_backtrace } } /// Returns the payload associated with the panic. @@ -145,7 +150,7 @@ impl<'a> PanicInfo<'a> { } #[stable(feature = "panic_hook_display", since = "1.26.0")] -impl fmt::Display for PanicInfo<'_> { +impl fmt::Display for PanicHookInfo<'_> { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("panicked at ")?; self.location.fmt(formatter)?; @@ -204,7 +209,7 @@ pub use core::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; /// The message can be of any (`Any + Send`) type, not just strings. /// /// The message is wrapped in a `Box<'static + Any + Send>`, which can be -/// accessed later using [`PanicInfo::payload`]. +/// accessed later using [`PanicHookInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. #[stable(feature = "panic_any", since = "1.51.0")] diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 15544de9ff8..caab7ed4b81 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -9,7 +9,7 @@ #![deny(unsafe_op_in_unsafe_fn)] -use crate::panic::{BacktraceStyle, PanicInfo}; +use crate::panic::{BacktraceStyle, PanicHookInfo}; use core::panic::{Location, PanicPayload}; use crate::any::Any; @@ -70,12 +70,12 @@ extern "C" fn __rust_foreign_exception() -> ! { enum Hook { Default, - Custom(Box) + 'static + Sync + Send>), + Custom(Box) + 'static + Sync + Send>), } impl Hook { #[inline] - fn into_box(self) -> Box) + 'static + Sync + Send> { + fn into_box(self) -> Box) + 'static + Sync + Send> { match self { Hook::Default => Box::new(default_hook), Hook::Custom(hook) => hook, @@ -105,7 +105,7 @@ static HOOK: RwLock = RwLock::new(Hook::Default); /// /// [`take_hook`]: ./fn.take_hook.html /// -/// The hook is provided with a `PanicInfo` struct which contains information +/// The hook is provided with a `PanicHookInfo` struct which contains information /// about the origin of the panic, including the payload passed to `panic!` and /// the source code location from which the panic originated. /// @@ -129,7 +129,7 @@ static HOOK: RwLock = RwLock::new(Hook::Default); /// panic!("Normal panic"); /// ``` #[stable(feature = "panic_hooks", since = "1.10.0")] -pub fn set_hook(hook: Box) + 'static + Sync + Send>) { +pub fn set_hook(hook: Box) + 'static + Sync + Send>) { if thread::panicking() { panic!("cannot modify the panic hook from a panicking thread"); } @@ -173,7 +173,7 @@ pub fn set_hook(hook: Box) + 'static + Sync + Send>) { /// ``` #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] -pub fn take_hook() -> Box) + 'static + Sync + Send> { +pub fn take_hook() -> Box) + 'static + Sync + Send> { if thread::panicking() { panic!("cannot modify the panic hook from a panicking thread"); } @@ -219,7 +219,7 @@ pub fn take_hook() -> Box) + 'static + Sync + Send> { #[unstable(feature = "panic_update_hook", issue = "92649")] pub fn update_hook(hook_fn: F) where - F: Fn(&(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static), &PanicInfo<'_>) + F: Fn(&(dyn Fn(&PanicHookInfo<'_>) + Send + Sync + 'static), &PanicHookInfo<'_>) + Sync + Send + 'static, @@ -234,7 +234,7 @@ where } /// The default panic handler. -fn default_hook(info: &PanicInfo<'_>) { +fn default_hook(info: &PanicHookInfo<'_>) { // If this is a double panic, make sure that we print a backtrace // for this panic. Otherwise only print it if logging is enabled. let backtrace = if info.force_no_backtrace() { @@ -791,10 +791,15 @@ fn rust_panic_with_hook( // formatting.) Hook::Default if panic_output().is_none() => {} Hook::Default => { - default_hook(&PanicInfo::new(location, payload.get(), can_unwind, force_no_backtrace)); + default_hook(&PanicHookInfo::new( + location, + payload.get(), + can_unwind, + force_no_backtrace, + )); } Hook::Custom(ref hook) => { - hook(&PanicInfo::new(location, payload.get(), can_unwind, force_no_backtrace)); + hook(&PanicHookInfo::new(location, payload.get(), can_unwind, force_no_backtrace)); } } diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index 7bd08a0605f..7aff7fe1fdd 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -58,7 +58,7 @@ use std::{ env, io, io::prelude::Write, mem::ManuallyDrop, - panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo}, + panic::{self, catch_unwind, AssertUnwindSafe, PanicHookInfo}, process::{self, Command, Termination}, sync::mpsc::{channel, Sender}, sync::{Arc, Mutex}, @@ -123,7 +123,7 @@ pub fn test_main(args: &[String], tests: Vec, options: Option| { + move |info: &'_ PanicHookInfo<'_>| { if !info.can_unwind() { std::mem::forget(std::io::stderr().lock()); let mut stdout = ManuallyDrop::new(std::io::stdout().lock()); @@ -726,7 +726,7 @@ fn spawn_test_subprocess( fn run_test_in_spawned_subprocess(desc: TestDesc, runnable_test: RunnableTest) -> ! { let builtin_panic_hook = panic::take_hook(); - let record_result = Arc::new(move |panic_info: Option<&'_ PanicInfo<'_>>| { + let record_result = Arc::new(move |panic_info: Option<&'_ PanicHookInfo<'_>>| { let test_result = match panic_info { Some(info) => calc_result(&desc, Err(info.payload()), &None, &None), None => calc_result(&desc, Ok(()), &None, &None), diff --git a/tests/ui/duplicate_entry_error.rs b/tests/ui/duplicate_entry_error.rs index 7ebbab47095..8e49f57a3df 100644 --- a/tests/ui/duplicate_entry_error.rs +++ b/tests/ui/duplicate_entry_error.rs @@ -5,7 +5,9 @@ #![feature(lang_items)] -use std::panic::PanicInfo; +extern crate core; + +use core::panic::PanicInfo; #[lang = "panic_impl"] fn panic_impl(info: &PanicInfo) -> ! { diff --git a/tests/ui/duplicate_entry_error.stderr b/tests/ui/duplicate_entry_error.stderr index 3b5998df353..958e9c7527d 100644 --- a/tests/ui/duplicate_entry_error.stderr +++ b/tests/ui/duplicate_entry_error.stderr @@ -1,5 +1,5 @@ error[E0152]: found duplicate lang item `panic_impl` - --> $DIR/duplicate_entry_error.rs:11:1 + --> $DIR/duplicate_entry_error.rs:13:1 | LL | / fn panic_impl(info: &PanicInfo) -> ! { LL | | diff --git a/tests/ui/panic-handler/panic-handler-std.rs b/tests/ui/panic-handler/panic-handler-std.rs index 91b3997819c..051828ec880 100644 --- a/tests/ui/panic-handler/panic-handler-std.rs +++ b/tests/ui/panic-handler/panic-handler-std.rs @@ -1,8 +1,9 @@ //@ normalize-stderr-test "loaded from .*libstd-.*.rlib" -> "loaded from SYSROOT/libstd-*.rlib" //@ error-pattern: found duplicate lang item `panic_impl` +extern crate core; -use std::panic::PanicInfo; +use core::panic::PanicInfo; #[panic_handler] fn panic(info: PanicInfo) -> ! { diff --git a/tests/ui/panic-handler/panic-handler-std.stderr b/tests/ui/panic-handler/panic-handler-std.stderr index 48c216ce27e..caae16118ef 100644 --- a/tests/ui/panic-handler/panic-handler-std.stderr +++ b/tests/ui/panic-handler/panic-handler-std.stderr @@ -1,5 +1,5 @@ error[E0152]: found duplicate lang item `panic_impl` - --> $DIR/panic-handler-std.rs:8:1 + --> $DIR/panic-handler-std.rs:9:1 | LL | / fn panic(info: PanicInfo) -> ! { LL | | loop {} From 3854357ad2c164a87f7e625874d4cdc3ccd9cc33 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 31 Jan 2024 10:28:00 +0100 Subject: [PATCH 16/24] Fix deprecation version. --- library/core/src/panic/panic_info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 7c792a58d2a..df8f441bf35 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -80,7 +80,7 @@ impl<'a> PanicInfo<'a> { /// /// [`std::panic::PanicHookInfo`]: ../../std/panic/struct.PanicHookInfo.html /// [`std::panic::PanicHookInfo::payload`]: ../../std/panic/struct.PanicHookInfo.html#method.payload - #[deprecated(since = "1.74.0", note = "this never returns anything useful")] + #[deprecated(since = "1.77.0", note = "this never returns anything useful")] #[stable(feature = "panic_hooks", since = "1.10.0")] #[allow(deprecated, deprecated_in_future)] pub fn payload(&self) -> &(dyn crate::any::Any + Send) { From f5fe82fdcab33b4137b94014f05dd2986ea396ba Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 31 Jan 2024 11:32:53 +0100 Subject: [PATCH 17/24] Move deprecation of std::panic::PanicInfo to 1.80.0. --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 77defe14b70..0f999f6a93e 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -12,7 +12,7 @@ use crate::thread::Result; #[stable(feature = "panic_hooks", since = "1.10.0")] #[deprecated( - since = "1.77.0", + since = "1.80.0", note = "use `PanicHookInfo` instead", suggestion = "std::panic::PanicHookInfo" )] From ce0bc8bd588f9a2a4ba149ae6a9d3490f401c376 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 11:49:55 +0200 Subject: [PATCH 18/24] Downcast panic payload to String too in example. --- library/std/src/panic.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 0f999f6a93e..51228cc9907 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -73,6 +73,8 @@ impl<'a> PanicHookInfo<'a> { /// panic::set_hook(Box::new(|panic_info| { /// if let Some(s) = panic_info.payload().downcast_ref::<&str>() { /// println!("panic occurred: {s:?}"); + /// } else if let Some(s) = panic_info.payload().downcast_ref::() { + /// println!("panic occurred: {s:?}"); /// } else { /// println!("panic occurred"); /// } From 32bfe703e2f8b5062a769aa1b3467c80d5bb0b95 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 11:54:11 +0200 Subject: [PATCH 19/24] Add note on panic payload type. --- library/std/src/panic.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 51228cc9907..d5566caa369 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -63,6 +63,13 @@ impl<'a> PanicHookInfo<'a> { /// /// This will commonly, but not always, be a `&'static str` or [`String`]. /// + /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a + /// panic payload of type `&'static str` or `String`. + /// + /// Only an invocation of [`panic_any`] + /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string) + /// can result in a panic payload other than a `&'static str` or `String`. + /// /// [`String`]: ../../std/string/struct.String.html /// /// # Examples From 877a26f6c9cbd9878ea08c94cd5a698a53556511 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 12:58:46 +0200 Subject: [PATCH 20/24] Mention core's PanicInfo in error.md. --- library/core/src/error.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/core/src/error.md b/library/core/src/error.md index 8d5a623df4b..4b62391cafc 100644 --- a/library/core/src/error.md +++ b/library/core/src/error.md @@ -17,8 +17,8 @@ The following are the primary interfaces of the panic system and the responsibilities they cover: * [`panic!`] and [`panic_any`] (Constructing, Propagated automatically) -* [`PanicHookInfo`] (Reporting) -* [`set_hook`], [`take_hook`], and [`#[panic_handler]`][panic-handler] (Reporting) +* [`set_hook`], [`take_hook`], and [`PanicHookInfo`] (Reporting) +* [`#[panic_handler]`][panic-handler] and [`PanicInfo`] (Reporting in no_std) * [`catch_unwind`] and [`resume_unwind`] (Discarding, Propagating) The following are the primary interfaces of the error system and the @@ -126,6 +126,7 @@ should be available and executable by the current user". [`panic_any`]: ../../std/panic/fn.panic_any.html [`PanicHookInfo`]: ../../std/panic/struct.PanicHookInfo.html +[`PanicInfo`]: crate::panic::PanicInfo [`catch_unwind`]: ../../std/panic/fn.catch_unwind.html [`resume_unwind`]: ../../std/panic/fn.resume_unwind.html [`downcast`]: crate::error::Error From fb0990d1e1e67a512930665bf7df5fb91f55982c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 12:59:09 +0200 Subject: [PATCH 21/24] Fix display of panic message in recursive panic. --- library/core/src/panic.rs | 5 +++++ library/std/src/panicking.rs | 15 ++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index c9f3702e178..b5a0932221a 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -157,4 +157,9 @@ pub unsafe trait PanicPayload: crate::fmt::Display { /// Just borrow the contents. fn get(&mut self) -> &(dyn Any + Send); + + /// Try to borrow the contents as `&str`, if possible without doing any allocations. + fn as_str(&mut self) -> Option<&str> { + None + } } diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index caab7ed4b81..e4d2ee7d986 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -641,6 +641,10 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! { fn get(&mut self) -> &(dyn Any + Send) { &self.0 } + + fn as_str(&mut self) -> Option<&str> { + Some(self.0) + } } impl fmt::Display for StaticStrPayload { @@ -763,15 +767,8 @@ fn rust_panic_with_hook( // Don't try to format the message in this case, perhaps that is causing the // recursive panics. However if the message is just a string, no user-defined // code is involved in printing it, so that is risk-free. - let msg_str = message.and_then(|m| m.as_str()).map(|m| [m]); - let message = msg_str.as_ref().map(|m| fmt::Arguments::new_const(m)); - let panicinfo = PanicInfo::internal_constructor( - message.as_ref(), - location, - can_unwind, - force_no_backtrace, - ); - rtprintpanic!("{panicinfo}\nthread panicked while processing panic. aborting.\n"); + let message: &str = payload.as_str().unwrap_or_default(); + rtprintpanic!("panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n"); } panic_count::MustAbort::AlwaysAbort => { // Unfortunately, this does not print a backtrace, because creating From de07c1a928b3a1e6e12946c9d9d0ff47c2310ead Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 13:08:27 +0200 Subject: [PATCH 22/24] Add PanicHookInfo::payload_as_str(). --- library/std/src/panic.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index d5566caa369..f1b45b18630 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -96,6 +96,45 @@ impl<'a> PanicHookInfo<'a> { self.payload } + /// Returns the payload associated with the panic, if it is a string. + /// + /// This returns the payload if it is of type `&'static str` or `String`. + /// + /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a + /// panic payload where `payload_as_str` returns `Some`. + /// + /// Only an invocation of [`panic_any`] + /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string) + /// can result in a panic payload where `payload_as_str` returns `None`. + /// + /// # Example + /// + /// ```should_panic + /// #![feature(panic_payload_as_str)] + /// + /// std::panic::set_hook(Box::new(|panic_info| { + /// if let Some(s) = panic_info.payload_as_str() { + /// println!("panic occurred: {s:?}"); + /// } else { + /// println!("panic occurred"); + /// } + /// })); + /// + /// panic!("Normal panic"); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "panic_payload_as_str", issue = "125175")] + pub fn payload_as_str(&self) -> Option<&str> { + if let Some(s) = self.payload.downcast_ref::<&str>() { + Some(s) + } else if let Some(s) = self.payload.downcast_ref::() { + Some(s) + } else { + None + } + } + /// Returns information about the location from which the panic originated, /// if available. /// From a345c3daf65857fb12926c3c5f4c9d8e2ca00f00 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 16 May 2024 13:08:40 +0200 Subject: [PATCH 23/24] Bump deprecation of std's PanicInfo alias to 1.82.0. --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index f1b45b18630..5282c00fcca 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -12,7 +12,7 @@ use crate::thread::Result; #[stable(feature = "panic_hooks", since = "1.10.0")] #[deprecated( - since = "1.80.0", + since = "1.82.0", note = "use `PanicHookInfo` instead", suggestion = "std::panic::PanicHookInfo" )] From a6e23b126b925b270663436a1d2f4d24cc5f4711 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 11 Jun 2024 15:46:15 +0200 Subject: [PATCH 24/24] Formatting. --- library/std/src/panicking.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index e4d2ee7d986..8fd8134c111 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -768,7 +768,9 @@ fn rust_panic_with_hook( // recursive panics. However if the message is just a string, no user-defined // code is involved in printing it, so that is risk-free. let message: &str = payload.as_str().unwrap_or_default(); - rtprintpanic!("panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n"); + rtprintpanic!( + "panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n" + ); } panic_count::MustAbort::AlwaysAbort => { // Unfortunately, this does not print a backtrace, because creating