Skip to content

Commit 4b223d7

Browse files
authored
Rollup merge of rust-lang#78119 - fusion-engineering-forks:panic-use-as-str, r=Amanieu
Throw core::panic!("message") as &str instead of String. This makes `core::panic!("message")` consistent with `std::panic!("message")`, which throws a `&str` and not a `String`. This also makes any other panics from `core::panicking::panic` result in a `&str` rather than a `String`, which includes compiler-generated panics such as the panics generated for `mem::zeroed()`. --- Demonstration: ```rust use std::panic; use std::any::Any; fn main() { panic::set_hook(Box::new(|panic_info| check(panic_info.payload()))); check(&*panic::catch_unwind(|| core::panic!("core")).unwrap_err()); check(&*panic::catch_unwind(|| std::panic!("std")).unwrap_err()); } fn check(msg: &(dyn Any + Send)) { if let Some(s) = msg.downcast_ref::<String>() { println!("Got a String: {:?}", s); } else if let Some(s) = msg.downcast_ref::<&str>() { println!("Got a &str: {:?}", s); } } ``` Before: ``` Got a String: "core" Got a String: "core" Got a &str: "std" Got a &str: "std" ``` After: ``` Got a &str: "core" Got a &str: "core" Got a &str: "std" Got a &str: "std" ```
2 parents d08f484 + 9890217 commit 4b223d7

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
#![feature(exhaustive_patterns)]
260260
#![feature(extend_one)]
261261
#![feature(external_doc)]
262+
#![feature(fmt_as_str)]
262263
#![feature(fn_traits)]
263264
#![feature(format_args_nl)]
264265
#![feature(gen_future)]

library/std/src/panicking.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,26 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
478478
}
479479
}
480480

481+
struct StrPanicPayload(&'static str);
482+
483+
unsafe impl BoxMeUp for StrPanicPayload {
484+
fn take_box(&mut self) -> *mut (dyn Any + Send) {
485+
Box::into_raw(Box::new(self.0))
486+
}
487+
488+
fn get(&mut self) -> &(dyn Any + Send) {
489+
&self.0
490+
}
491+
}
492+
481493
let loc = info.location().unwrap(); // The current implementation always returns Some
482494
let msg = info.message().unwrap(); // The current implementation always returns Some
483495
crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
484-
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
496+
if let Some(msg) = msg.as_str() {
497+
rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc);
498+
} else {
499+
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
500+
}
485501
})
486502
}
487503

src/test/ui/intrinsics/panic-uninitialized-zeroed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ enum LR_NonZero {
5353
fn test_panic_msg<T>(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) {
5454
let err = panic::catch_unwind(op).err();
5555
assert_eq!(
56-
err.as_ref().and_then(|a| a.downcast_ref::<String>()).map(|s| &**s),
57-
Some(msg)
56+
err.as_ref().and_then(|a| a.downcast_ref::<&str>()),
57+
Some(&msg)
5858
);
5959
}
6060

0 commit comments

Comments
 (0)