Skip to content
Merged
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
30 changes: 16 additions & 14 deletions crates/ruff_db/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ pub struct PanicError {
pub struct Payload(Box<dyn std::any::Any + Send>);

impl Payload {
pub fn as_str(&self) -> Option<&str> {
pub fn downcast_ref<R: Any>(&self) -> Option<&R> {
self.0.downcast_ref::<R>()
}
}

impl std::fmt::Display for Payload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(s) = self.0.downcast_ref::<String>() {
Some(s)
f.write_str(s)
} else if let Some(s) = self.0.downcast_ref::<&str>() {
Some(s)
f.write_str(s)
} else if let Some(s) = self.0.downcast_ref::<salsa::Cancelled>() {
write!(f, "{s}")
} else {
None
f.write_str("Box<dyn Any>")
}
}

pub fn downcast_ref<R: Any>(&self) -> Option<&R> {
self.0.downcast_ref::<R>()
}
}

impl PanicError {
Expand All @@ -50,9 +54,7 @@ impl PanicError {
let _ = write!(&mut message, " when checking `{path}`");
}

if let Some(payload) = self.payload.as_str() {
let _ = write!(&mut message, ": `{payload}`");
}
let _ = write!(&mut message, ": `{payload}`", payload = self.payload);

message
}
Expand All @@ -64,9 +66,9 @@ impl std::fmt::Display for PanicError {
if let Some(location) = &self.location {
write!(f, " {location}")?;
}
if let Some(payload) = self.payload.as_str() {
write!(f, ":\n{payload}")?;
}

write!(f, ":\n{payload}", payload = self.payload)?;

if let Some(query_trace) = self.salsa_backtrace.as_ref() {
let _ = writeln!(f, "{query_trace}");
}
Expand Down
16 changes: 3 additions & 13 deletions crates/ty_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,12 @@ fn run_test(
.should_expect_panic()
.expect("panic_info is only set when `should_expect_panic` is `Ok`");

let message = panic_info
.payload
.as_str()
.unwrap_or("Box<dyn Any>")
.to_string();

if let Some(expected_message) = expected_message {
let message = panic_info.payload.to_string();
assert!(
message.contains(expected_message),
"Test `{}` is expected to panic with `{expected_message}`, but panicked with `{message}` instead.",
test.name()
test.name(),
);
}
}
Expand Down Expand Up @@ -740,12 +735,7 @@ impl AttemptTestError<'_> {
messages.push(Failure::new(clarification));
}
messages.push(Failure::new(""));
match info.payload.as_str() {
Some(message) => messages.push(Failure::new(message)),
// Mimic the default panic hook's rendering of the panic payload if it's
// not a string.
None => messages.push(Failure::new("Box<dyn Any>")),
}
messages.push(Failure::new(info.payload));
messages.push(Failure::new(""));

if let Some(backtrace) = info.backtrace {
Expand Down
Loading