Skip to content

Commit 2780e35

Browse files
committed
Throw core::panic!("message") as &str instead of String.
This makes it consistent with std::panic!("message"), which also throws a &str, not a String.
1 parent ad268bd commit 2780e35

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
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

0 commit comments

Comments
 (0)