From 64e56db72aa2b1f112504504a7dceba63881cdef Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 31 Jan 2024 10:27:29 +0100 Subject: [PATCH] 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::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 @@ /// 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 @@ /// /// 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 @@ pub fn location(&self) -> Option<&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`], + /// 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::{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 @@ /// 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 @@ pub(crate) fn new( 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 @@ pub fn force_no_backtrace(&self) -> bool { } #[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 @@ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { /// 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 @@ fn default() -> Hook { /// /// [`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 @@ fn default() -> Hook { /// 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 @@ pub fn update_hook(hook_fn: F) } /// 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 @@ pub mod test { 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 {}