Skip to content

Commit 05783c8

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 06ef32c commit 05783c8

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

example/mini_core.rs

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

468+
macro_rules! panic_const {
469+
($($lang:ident = $message:expr,)+) => {
470+
#[cfg(not(bootstrap))]
471+
pub mod panic_const {
472+
use super::*;
473+
474+
$(
475+
#[track_caller]
476+
#[lang = stringify!($lang)]
477+
pub fn $lang() -> ! {
478+
panic($message);
479+
}
480+
)+
481+
}
482+
}
483+
}
484+
485+
panic_const! {
486+
panic_const_add_overflow = "attempt to add with overflow",
487+
panic_const_sub_overflow = "attempt to subtract with overflow",
488+
panic_const_mul_overflow = "attempt to multiply with overflow",
489+
panic_const_div_overflow = "attempt to divide with overflow",
490+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
491+
panic_const_neg_overflow = "attempt to negate with overflow",
492+
panic_const_shr_overflow = "attempt to shift right with overflow",
493+
panic_const_shl_overflow = "attempt to shift left with overflow",
494+
panic_const_div_by_zero = "attempt to divide by zero",
495+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
496+
}
497+
468498
#[lang = "panic_bounds_check"]
469499
#[track_caller]
470500
fn panic_bounds_check(index: usize, len: usize) -> ! {

src/base.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,14 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
369369
);
370370
}
371371
_ => {
372-
let msg_str = msg.description();
373-
codegen_panic(fx, msg_str, source_info);
372+
let location = fx.get_caller_location(source_info).load_scalar(fx);
373+
374+
codegen_panic_inner(
375+
fx,
376+
msg.panic_function(),
377+
&[location],
378+
Some(source_info.span),
379+
);
374380
}
375381
}
376382
}
@@ -954,20 +960,6 @@ pub(crate) fn codegen_operand<'tcx>(
954960
}
955961
}
956962

957-
pub(crate) fn codegen_panic<'tcx>(
958-
fx: &mut FunctionCx<'_, '_, 'tcx>,
959-
msg_str: &str,
960-
source_info: mir::SourceInfo,
961-
) {
962-
let location = fx.get_caller_location(source_info).load_scalar(fx);
963-
964-
let msg_ptr = fx.anonymous_str(msg_str);
965-
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
966-
let args = [msg_ptr, msg_len, location];
967-
968-
codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, Some(source_info.span));
969-
}
970-
971963
pub(crate) fn codegen_panic_nounwind<'tcx>(
972964
fx: &mut FunctionCx<'_, '_, 'tcx>,
973965
msg_str: &str,

0 commit comments

Comments
 (0)