Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions library/core/src/panic/panic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,24 @@ 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(),
/// );
/// write!(DEBUG_OUTPUT, "panicked at {}", location);

@Mark-Simulacrum Mark-Simulacrum Sep 13, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a well-behaved panic handler would have an else branch here, so we shouldn't lose that. It should be uncommon to want no output printed for a future version of Rust that returned None.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added else branch, but using slightly different message than the original for this example in else branch, let me know if you would prefer to have the original line:
panic occurred but can't get location information...

/// } else {
/// println!("panic occurred but can't get location information...");
/// write!(DEBUG_OUTPUT, "panicked at unknown 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)
}

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading