Skip to content

Error in panic #103169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
11 changes: 10 additions & 1 deletion library/core/src/panic/panic_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::any::Any;
use crate::error::Error;
use crate::fmt;
use crate::panic::Location;

Expand Down Expand Up @@ -30,6 +31,7 @@ use crate::panic::Location;
pub struct PanicInfo<'a> {
payload: &'a (dyn Any + Send),
message: Option<&'a fmt::Arguments<'a>>,
source: Option<&'a (dyn Error + 'static)>,
location: &'a Location<'a>,
can_unwind: bool,
}
Expand All @@ -45,10 +47,11 @@ impl<'a> PanicInfo<'a> {
pub fn internal_constructor(
message: Option<&'a fmt::Arguments<'a>>,
location: &'a Location<'a>,
source: Option<&'a (dyn Error + 'static)>,
can_unwind: bool,
) -> Self {
struct NoPayload;
PanicInfo { location, message, payload: &NoPayload, can_unwind }
PanicInfo { location, message, payload: &NoPayload, source, can_unwind }
}

#[unstable(
Expand All @@ -62,6 +65,12 @@ impl<'a> PanicInfo<'a> {
self.payload = info;
}

#[unstable(feature = "error_in_panic", issue = "none")]
Copy link
Member

Choose a reason for hiding this comment

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

Can you open a tracking issue for this feature? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tracking issue is here: #103820

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[unstable(feature = "error_in_panic", issue = "none")]
#[unstable(feature = "error_in_panic", issue = "103820")]

/// The [`Error`] that caused this panic, if known.
pub fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source
Comment on lines +70 to +71
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pub fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source
pub fn error(&self) -> Option<&(dyn Error + 'static)> {
self.error

(etc.)

See comment above.

}

/// Returns the payload associated with the panic.
///
/// This will commonly, but not always, be a `&'static str` or [`String`].
Expand Down
40 changes: 26 additions & 14 deletions library/core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
issue = "none"
)]

use crate::error::Error;
use crate::fmt;
use crate::panic::{Location, PanicInfo};

Expand Down Expand Up @@ -54,19 +55,37 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
super::intrinsics::abort()
}

// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
// that gets resolved to the `#[panic_handler]` function.
extern "Rust" {
#[lang = "panic_impl"]
fn panic_impl(pi: &PanicInfo<'_>) -> !;
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), None, true);

// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
unsafe { panic_impl(&pi) }
}

#[cold]
// If panic_immediate_abort, inline the abort call,
// otherwise avoid inlining because of it is cold path.
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[track_caller]
#[unstable(feature = "error_in_panic", issue = "none")]
Copy link
Member

Choose a reason for hiding this comment

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

This is not part of the public error_in_panic API that might be stabilized in the future, right? (But just used internally, like the other functions in this module.) In that case:

Suggested change
#[unstable(feature = "error_in_panic", issue = "none")]
#[unstable(feature = "core_panic", issue = "none")]

pub fn panic_source(fmt: fmt::Arguments<'_>, source: &(dyn Error + 'static)) -> ! {
if cfg!(feature = "panic_immediate_abort") {
super::intrinsics::abort()
}

let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), true);
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), Some(source), true);

// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
unsafe { panic_impl(&pi) }
}

// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
// that gets resolved to the `#[panic_handler]` function.
extern "Rust" {
#[lang = "panic_impl"]
fn panic_impl(pi: &PanicInfo<'_>) -> !;
}

/// Like `panic_fmt`, but for non-unwinding panics.
///
/// Has to be a separate function so that it can carry the `rustc_nounwind` attribute.
Expand All @@ -82,15 +101,8 @@ pub fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>) -> ! {
super::intrinsics::abort()
}

// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
// that gets resolved to the `#[panic_handler]` function.
extern "Rust" {
#[lang = "panic_impl"]
fn panic_impl(pi: &PanicInfo<'_>) -> !;
}

// PanicInfo with the `can_unwind` flag set to false forces an abort.
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false);
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), None, false);

// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
unsafe { panic_impl(&pi) }
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
#![feature(duration_constants)]
#![feature(error_generic_member_access)]
#![feature(error_in_core)]
#![feature(error_in_panic)]
#![feature(error_iter)]
#![feature(exact_size_is_empty)]
#![feature(exclusive_wrapper)]
Expand Down
22 changes: 17 additions & 5 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::panic::BacktraceStyle;
use core::panic::{BoxMeUp, Location, PanicInfo};

use crate::any::Any;
use crate::error::{Error, Report};
use crate::fmt;
use crate::intrinsics;
use crate::mem::{self, ManuallyDrop};
Expand Down Expand Up @@ -258,7 +259,10 @@ fn default_hook(info: &PanicInfo<'_>) {

let write = |err: &mut dyn crate::io::Write| {
let _ = writeln!(err, "thread '{name}' panicked at '{msg}', {location}");

if let Some(source) = info.source() {
let report = Report::new(source).pretty(true);
let _ = writeln!(err, "\nSource: {report}\n");
}
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

match backtrace {
Expand Down Expand Up @@ -577,12 +581,19 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
let msg = info.message().unwrap(); // The current implementation always returns Some
crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
if let Some(msg) = msg.as_str() {
rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc, info.can_unwind());
rust_panic_with_hook(
&mut StrPanicPayload(msg),
info.message(),
loc,
info.source(),
info.can_unwind(),
);
} else {
rust_panic_with_hook(
&mut PanicPayload::new(msg),
info.message(),
loc,
info.source(),
info.can_unwind(),
);
}
Expand All @@ -608,7 +619,7 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {

let loc = Location::caller();
return crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc, true)
rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc, None, true)
});

struct PanicPayload<A> {
Expand Down Expand Up @@ -653,6 +664,7 @@ fn rust_panic_with_hook(
payload: &mut dyn BoxMeUp,
message: Option<&fmt::Arguments<'_>>,
location: &Location<'_>,
source: Option<&(dyn Error + 'static)>,
can_unwind: bool,
) -> ! {
let (must_abort, panics) = panic_count::increase();
Expand All @@ -670,13 +682,13 @@ fn rust_panic_with_hook(
} else {
// Unfortunately, this does not print a backtrace, because creating
// a `Backtrace` will allocate, which we must to avoid here.
let panicinfo = PanicInfo::internal_constructor(message, location, can_unwind);
let panicinfo = PanicInfo::internal_constructor(message, location, source, can_unwind);
rtprintpanic!("{panicinfo}\npanicked after panic::always_abort(), aborting.\n");
}
crate::sys::abort_internal();
}

let mut info = PanicInfo::internal_constructor(message, location, can_unwind);
let mut info = PanicInfo::internal_constructor(message, location, source, can_unwind);
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
Expand Down
50 changes: 50 additions & 0 deletions tests/ui/errors/issue-103169-error-in-panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// run-fail
// needs-run-enabled
// check-run-results

#![allow(unused_imports)]
#![feature(core_panic)]
#![feature(error_in_core)]
#![feature(error_in_panic)]

extern crate core;

use core::error;
use core::panicking::panic_source;

use std::error::Error;

#[derive(Debug)]
struct MyErr {
super_source: SourceError,
}

#[derive(Debug)]
struct SourceError {}

use std::fmt;
impl fmt::Display for MyErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "my source error message")
}
}

impl error::Error for MyErr {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.super_source)
}
}

impl fmt::Display for SourceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "my source's source error message")
}
}

impl error::Error for SourceError {}

fn main() {
let source_error = SourceError {};
let source = MyErr { super_source: source_error };
panic_source(format_args!("here's my panic error message"), &source);
}
8 changes: 8 additions & 0 deletions tests/ui/errors/issue-103169-error-in-panic.run.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
thread 'main' panicked at 'here's my panic error message', $DIR/issue-103169-error-in-panic.rs:49:5

Source: my source error message

Caused by:
my source's source error message

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace