Skip to content

Commit 0ad474f

Browse files
viischeinkman
authored andcommitted
Add libstd and libcore Cargo features "panic_immediate_abort"
It stop asserts and panics from libstd to automatically include string output and formatting code. Use case: developing static executables smaller than 50 kilobytes, where usual formatting code is excessive while keeping debuggability in debug mode. May resolve rust-lang#54981.
1 parent c94101b commit 0ad474f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libstd/panicking.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,15 @@ pub fn rust_begin_panic(info: &PanicInfo) -> ! {
355355
#[unstable(feature = "libstd_sys_internals",
356356
reason = "used by the panic! macro",
357357
issue = "0")]
358-
#[inline(never)] #[cold]
358+
#[cold]
359+
#[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
360+
#[cfg_attr( feature="panic_immediate_abort" ,inline)]
359361
pub fn begin_panic_fmt(msg: &fmt::Arguments,
360362
file_line_col: &(&'static str, u32, u32)) -> ! {
363+
if cfg!(feature = "panic_immediate_abort") {
364+
unsafe { intrinsics::abort() }
365+
};
366+
361367
let (file, line, col) = *file_line_col;
362368
let info = PanicInfo::internal_constructor(
363369
Some(msg),
@@ -419,8 +425,15 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
419425
reason = "used by the panic! macro",
420426
issue = "0")]
421427
#[cfg_attr(not(test), lang = "begin_panic")]
422-
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
428+
// avoid code bloat at the call sites as much as possible
429+
// inline(never) is required even in panic_immediate_abort mode, lest linker error
430+
#[inline(never)]
431+
#[cold]
423432
pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! {
433+
if cfg!(feature = "panic_immediate_abort") {
434+
unsafe { intrinsics::abort() }
435+
};
436+
424437
// Note that this should be the only allocation performed in this code path.
425438
// Currently this means that panic!() on OOM will invoke this code path,
426439
// but then again we're not really ready for panic on OOM anyway. If

0 commit comments

Comments
 (0)