Skip to content

Commit

Permalink
Throw core::panic!("message") as &str instead of String.
Browse files Browse the repository at this point in the history
This makes it consistent with std::panic!("message"), which also throws
a &str, not a String.
  • Loading branch information
m-ou-se committed Oct 19, 2020
1 parent ad268bd commit 2780e35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Expand Up @@ -259,6 +259,7 @@
#![feature(exhaustive_patterns)]
#![feature(extend_one)]
#![feature(external_doc)]
#![feature(fmt_as_str)]
#![feature(fn_traits)]
#![feature(format_args_nl)]
#![feature(gen_future)]
Expand Down
18 changes: 17 additions & 1 deletion library/std/src/panicking.rs
Expand Up @@ -478,10 +478,26 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
}
}

struct StrPanicPayload(&'static str);

unsafe impl BoxMeUp for StrPanicPayload {
fn take_box(&mut self) -> *mut (dyn Any + Send) {
Box::into_raw(Box::new(self.0))
}

fn get(&mut self) -> &(dyn Any + Send) {
&self.0
}
}

let loc = info.location().unwrap(); // The current implementation always returns Some
let msg = info.message().unwrap(); // The current implementation always returns Some
crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
if let Some(msg) = msg.as_str() {
rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc);
} else {
rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
}
})
}

Expand Down

0 comments on commit 2780e35

Please sign in to comment.