mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:49:50 +00:00
[red-knot] Print non-string panic payloads and (sometimes) backtraces (#15363)
More refinements to the panic messages for failing mdtests to mimic the output of the default panic hook more closely: - We now print out `Box<dyn Any>` if the panic payload is not a string (which is typically the case for salsa panics). - We now include the panic's backtrace if you set the `RUST_BACKTRACE` environment variable.
This commit is contained in:
parent
b6562ed57e
commit
5f5eb7c0dd
2 changed files with 38 additions and 12 deletions
|
@ -1,20 +1,27 @@
|
|||
use std::cell::Cell;
|
||||
use std::panic::Location;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct PanicError {
|
||||
pub info: String,
|
||||
pub location: Option<String>,
|
||||
pub payload: Option<String>,
|
||||
pub backtrace: Option<std::backtrace::Backtrace>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PanicError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
writeln!(f, "{}", self.info)?;
|
||||
if let Some(backtrace) = &self.backtrace {
|
||||
writeln!(f, "Backtrace: {backtrace}")
|
||||
} else {
|
||||
Ok(())
|
||||
writeln!(f, "panicked at")?;
|
||||
if let Some(location) = &self.location {
|
||||
write!(f, " {location}")?;
|
||||
}
|
||||
if let Some(payload) = &self.payload {
|
||||
write!(f, ":\n{payload}")?;
|
||||
}
|
||||
if let Some(backtrace) = &self.backtrace {
|
||||
writeln!(f, "\nBacktrace: {backtrace}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,11 +41,17 @@ fn install_hook() {
|
|||
if !should_capture {
|
||||
return (*prev)(info);
|
||||
}
|
||||
let info = info.to_string();
|
||||
let payload = if let Some(s) = info.payload().downcast_ref::<&str>() {
|
||||
Some(s.to_string())
|
||||
} else {
|
||||
info.payload().downcast_ref::<String>().cloned()
|
||||
};
|
||||
let location = info.location().map(Location::to_string);
|
||||
let backtrace = std::backtrace::Backtrace::force_capture();
|
||||
LAST_PANIC.with(|cell| {
|
||||
cell.set(Some(PanicError {
|
||||
info,
|
||||
payload,
|
||||
location,
|
||||
backtrace: Some(backtrace),
|
||||
}));
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue