summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorVitaly _Vi Shukela <vi0oss@gmail.com>2018-11-30 00:06:10 +0300
committerVitaly _Vi Shukela <vi0oss@gmail.com>2018-11-30 00:56:41 +0300
commitfdef3848a01c4567abd326043b797f6bdc3f3e76 (patch)
tree2e00c6d19977a4ba5f3ce5569fb229a9920dc387 /src/libstd
parentf1e2fa8f0469aac1ea69dd5b6164e1d198d57934 (diff)
downloadrust-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/libstd')
-rw-r--r--src/libstd/Cargo.toml3
-rw-r--r--src/libstd/panicking.rs17
2 files changed, 18 insertions, 2 deletions
diff --git a/src/libstd/Cargo.toml b/src/libstd/Cargo.toml
index 2b1d515c83b..c1446218367 100644
--- a/src/libstd/Cargo.toml
+++ b/src/libstd/Cargo.toml
@@ -47,6 +47,9 @@ backtrace = []
 panic-unwind = ["panic_unwind"]
 profiler = ["profiler_builtins"]
 
+# Make panics and failed asserts immediately abort without formatting any message
+panic_immediate_abort = ["core/panic_immediate_abort"]
+
 # An off-by-default feature which enables a linux-syscall-like ABI for libstd to
 # interoperate with the host environment. Currently not well documented and
 # requires rebuilding the standard library to use it.
diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs
index a0590a800d3..29a0b3feefd 100644
--- a/src/libstd/panicking.rs
+++ b/src/libstd/panicking.rs
@@ -334,9 +334,15 @@ pub fn rust_begin_panic(info: &PanicInfo) -> ! {
 #[unstable(feature = "libstd_sys_internals",
            reason = "used by the panic! macro",
            issue = "0")]
-#[inline(never)] #[cold]
+#[cold]
+#[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
+#[cfg_attr(    feature="panic_immediate_abort" ,inline)]
 pub fn begin_panic_fmt(msg: &fmt::Arguments,
                        file_line_col: &(&'static str, u32, u32)) -> ! {
+    if cfg!(feature = "panic_immediate_abort") {
+        unsafe { intrinsics::abort() }
+    };
+
     let (file, line, col) = *file_line_col;
     let info = PanicInfo::internal_constructor(
         Some(msg),
@@ -398,8 +404,15 @@ fn continue_panic_fmt(info: &PanicInfo) -> ! {
            reason = "used by the panic! macro",
            issue = "0")]
 #[cfg_attr(not(test), lang = "begin_panic")]
-#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
+// avoid code bloat at the call sites as much as possible
+// inline(never) is required even in panic_immediate_abort mode, lest linker error
+#[inline(never)]
+#[cold]
 pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! {
+    if cfg!(feature = "panic_immediate_abort") {
+        unsafe { intrinsics::abort() }
+    };
+
     // Note that this should be the only allocation performed in this code path.
     // Currently this means that panic!() on OOM will invoke this code path,
     // but then again we're not really ready for panic on OOM anyway. If