From bff5a0e24d5bad22ea088d3779e16c2efd03a110 Mon Sep 17 00:00:00 2001 From: Valentyn Kit Date: Mon, 31 Aug 2026 15:41:10 +0300 Subject: [PATCH 1/2] core: fix the docs of PanicInfo::location After `core::panic::PanicInfo` and `std::panic::PanicHookInfo` were split, the docs for `location` still showed incorrect usage of `std::panic::set_hook` for `core::panic::PanicInfo` location, leftover from before the split. Also notes for location methods of `PanicInfo` and `PanicHookInfo` were updated, explaining that if it is ever changed to return None, it should be addressed where the location is created and where it's unwrapped, to avoid unexpected behaviour when callers may expect to always receive location. For `PanicInfo`, `panic_fmt` builds it, and `panic_handler` unwraps it. For `PanicHookInfo`, `panic_with_hook` builds it, and `default_hook` unwraps it. --- library/core/src/panic/panic_info.rs | 23 ++++++++--------------- library/std/src/panic.rs | 2 +- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index ee22521cb014c..6812e2758ed02 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -68,29 +68,22 @@ impl<'a> PanicInfo<'a> { /// This method will currently always return [`Some`], but this may change /// in future versions. /// - /// # Examples - /// - /// ```should_panic - /// use std::panic; + /// # Example /// - /// panic::set_hook(Box::new(|panic_info| { + /// ```ignore (no_std) + /// #[panic_handler] + /// fn panic_handler(panic_info: &PanicInfo<'_>) -> ! { /// 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..."); + /// write!(DEBUG_OUTPUT, "panicked at {}", location); /// } - /// })); - /// - /// panic!("Normal panic"); + /// loop {} + /// } /// ``` #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn location(&self) -> Option<&'static Location<'static>> { // NOTE: If this is changed to sometimes return None, - // deal with that case in std::panicking::default_hook and core::panicking::panic_fmt. + // deal with that case in std::panicking::panic_handler and core::panicking::panic_fmt. Some(self.location) } diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index a07a5fb6290ca..23a4cabc320b0 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -162,7 +162,7 @@ impl<'a> PanicHookInfo<'a> { #[stable(feature = "panic_hooks", since = "1.10.0")] pub fn location(&self) -> Option<&'static Location<'static>> { // NOTE: If this is changed to sometimes return None, - // deal with that case in std::panicking::default_hook and core::panicking::panic_fmt. + // deal with that case in std::panicking::default_hook and std::panicking::panic_with_hook. Some(self.location) } From 5040a040c961df16814ca4101d6485da91bee594 Mon Sep 17 00:00:00 2001 From: Valentyn Kit Date: Tue, 15 Sep 2026 22:19:41 +0300 Subject: [PATCH 2/2] docs: PanicInfo::location handling example with no location --- library/core/src/panic/panic_info.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/panic/panic_info.rs b/library/core/src/panic/panic_info.rs index 6812e2758ed02..d8d4073c83b9e 100644 --- a/library/core/src/panic/panic_info.rs +++ b/library/core/src/panic/panic_info.rs @@ -75,6 +75,8 @@ impl<'a> PanicInfo<'a> { /// fn panic_handler(panic_info: &PanicInfo<'_>) -> ! { /// if let Some(location) = panic_info.location() { /// write!(DEBUG_OUTPUT, "panicked at {}", location); + /// } else { + /// write!(DEBUG_OUTPUT, "panicked at unknown location"); /// } /// loop {} /// }