Skip to content

Commit

Permalink
[red-knot] Print non-string panic payloads and (sometimes) backtraces (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
dcreager authored Jan 8, 2025
1 parent b6562ed commit 5f5eb7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
21 changes: 17 additions & 4 deletions crates/red_knot_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,23 @@ fn run_test(db: &mut db::Db, test: &parser::MarkdownTest) -> Result<(), Failures
Ok(type_diagnostics) => type_diagnostics,
Err(info) => {
let mut by_line = matcher::FailuresByLine::default();
by_line.push(
OneIndexed::from_zero_indexed(0),
info.info.split('\n').map(String::from).collect(),
);
let mut messages = vec![];
match info.location {
Some(location) => messages.push(format!("panicked at {location}")),
None => messages.push("panicked at unknown location".to_string()),
};
match info.payload {
Some(payload) => messages.push(payload),
// Mimic the default panic hook's rendering of the panic payload if it's
// not a string.
None => messages.push("Box<dyn Any>".to_string()),
};
if let Some(backtrace) = info.backtrace {
if std::env::var("RUST_BACKTRACE").is_ok() {
messages.extend(backtrace.to_string().split('\n').map(String::from));
}
}
by_line.push(OneIndexed::from_zero_indexed(0), messages);
return Some(FileFailures {
backtick_offset: test_file.backtick_offset,
by_line,
Expand Down
27 changes: 20 additions & 7 deletions crates/ruff_db/src/panic.rs
Original file line number Diff line number Diff line change
@@ -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)?;
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, "Backtrace: {backtrace}")
} else {
Ok(())
writeln!(f, "\nBacktrace: {backtrace}")?;
}
Ok(())
}
}

Expand All @@ -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),
}));
});
Expand Down

0 comments on commit 5f5eb7c

Please sign in to comment.