diff options
| author | Vitaly _Vi Shukela <vi0oss@gmail.com> | 2018-11-30 00:06:10 +0300 |
|---|---|---|
| committer | Vitaly _Vi Shukela <vi0oss@gmail.com> | 2018-11-30 00:56:41 +0300 |
| commit | fdef3848a01c4567abd326043b797f6bdc3f3e76 (patch) | |
| tree | 2e00c6d19977a4ba5f3ce5569fb229a9920dc387 /src/libcore | |
| parent | f1e2fa8f0469aac1ea69dd5b6164e1d198d57934 (diff) | |
| download | rust-fdef3848a01c4567abd326043b797f6bdc3f3e76.tar.gz rust-fdef3848a01c4567abd326043b797f6bdc3f3e76.zip | |
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 #54981.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/Cargo.toml | 4 | ||||
| -rw-r--r-- | src/libcore/panicking.rs | 24 |
2 files changed, 25 insertions, 3 deletions
diff --git a/src/libcore/Cargo.toml b/src/libcore/Cargo.toml index 0b01cfc488b..7fd61f07d5e 100644 --- a/src/libcore/Cargo.toml +++ b/src/libcore/Cargo.toml @@ -21,3 +21,7 @@ path = "../libcore/benches/lib.rs" [dev-dependencies] rand = "0.5" + +[features] +# Make panics and failed asserts immediately abort without formatting any message +panic_immediate_abort = [] diff --git a/src/libcore/panicking.rs b/src/libcore/panicking.rs index 58407de9566..67c0b6ada90 100644 --- a/src/libcore/panicking.rs +++ b/src/libcore/panicking.rs @@ -39,9 +39,15 @@ use fmt; use panic::{Location, PanicInfo}; -#[cold] #[inline(never)] // this is the slow path, always +#[cold] +// inline(never) is required even in panic_immediate_abort mode, lest linker error +#[inline(never)] #[lang = "panic"] pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! { + if cfg!(feature = "panic_immediate_abort") { + unsafe { super::intrinsics::abort() } + }; + // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially // reduce size overhead. The format_args! macro uses str's Display trait to // write expr, which calls Formatter::pad, which must accommodate string @@ -52,16 +58,28 @@ pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! { panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line, col)) } -#[cold] #[inline(never)] +#[cold] +// inline(never) is required even in panic_immediate_abort mode, lest linker error +#[inline(never)] #[lang = "panic_bounds_check"] fn panic_bounds_check(file_line_col: &(&'static str, u32, u32), index: usize, len: usize) -> ! { + if cfg!(feature = "panic_immediate_abort") { + unsafe { super::intrinsics::abort() } + }; + panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}", len, index), file_line_col) } -#[cold] #[inline(never)] +#[cold] +#[cfg_attr(not(feature="panic_immediate_abort"),inline(never))] +#[cfg_attr( feature="panic_immediate_abort" ,inline)] pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! { + if cfg!(feature = "panic_immediate_abort") { + unsafe { super::intrinsics::abort() } + }; + // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call #[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe extern "Rust" { |
