Skip to content

Commit c8cb091

Browse files
Codegen const panic messages as function calls
This skips emitting extra arguments at every callsite (of which there can be many). For a librustc_driver build with overflow checks enabled, this cuts 0.7MB from the resulting binary.
1 parent 678e624 commit c8cb091

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

example/mini_core.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ pub fn panic(_msg: &'static str) -> ! {
418418
}
419419
}
420420

421+
macro_rules! panic_const {
422+
($($lang:ident = $message:expr,)+) => {
423+
#[cfg(not(bootstrap))]
424+
pub mod panic_const {
425+
use super::*;
426+
427+
$(
428+
#[track_caller]
429+
#[lang = stringify!($lang)]
430+
pub fn $lang() -> ! {
431+
panic($message);
432+
}
433+
)+
434+
}
435+
}
436+
}
437+
438+
panic_const! {
439+
panic_const_add_overflow = "attempt to add with overflow",
440+
panic_const_sub_overflow = "attempt to subtract with overflow",
441+
panic_const_mul_overflow = "attempt to multiply with overflow",
442+
panic_const_div_overflow = "attempt to divide with overflow",
443+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
444+
panic_const_neg_overflow = "attempt to negate with overflow",
445+
panic_const_shr_overflow = "attempt to shift right with overflow",
446+
panic_const_shl_overflow = "attempt to shift left with overflow",
447+
panic_const_div_by_zero = "attempt to divide by zero",
448+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
449+
}
450+
421451
#[lang = "panic_cannot_unwind"]
422452
fn panic_cannot_unwind() -> ! {
423453
unsafe {

0 commit comments

Comments
 (0)