diff --git a/crates/ruff_db/src/panic.rs b/crates/ruff_db/src/panic.rs index f5c176d5f44df..26b62f3f0f707 100644 --- a/crates/ruff_db/src/panic.rs +++ b/crates/ruff_db/src/panic.rs @@ -16,19 +16,23 @@ pub struct PanicError { pub struct Payload(Box); impl Payload { - pub fn as_str(&self) -> Option<&str> { + pub fn downcast_ref(&self) -> Option<&R> { + self.0.downcast_ref::() + } +} + +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::() { - 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::() { + write!(f, "{s}") } else { - None + f.write_str("Box") } } - - pub fn downcast_ref(&self) -> Option<&R> { - self.0.downcast_ref::() - } } impl PanicError { @@ -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 } @@ -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}"); } diff --git a/crates/ty_test/src/lib.rs b/crates/ty_test/src/lib.rs index 63bf45e995db3..1adb338e8ae01 100644 --- a/crates/ty_test/src/lib.rs +++ b/crates/ty_test/src/lib.rs @@ -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") - .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(), ); } } @@ -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")), - } + messages.push(Failure::new(info.payload)); messages.push(Failure::new("")); if let Some(backtrace) = info.backtrace {