Skip to content

Commit 468e2cd

Browse files
committed
libcore: Make fail handlers optional
Some users have no need for formatted fail messages and really can't bear the code sizes that they require. See #17886.
1 parent ce342f5 commit 468e2cd

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

src/libcore/failure.rs

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,69 @@
3030
3131
#![allow(dead_code, missing_doc)]
3232

33-
use fmt;
34-
use intrinsics;
33+
pub use failure::internal::{fail, fail_fmt};
3534

36-
#[cold] #[inline(never)] // this is the slow path, always
37-
#[lang="fail"]
38-
pub fn fail(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
39-
let (expr, file, line) = *expr_file_line;
40-
let ref file_line = (file, line);
41-
format_args!(|args| -> () {
42-
fail_fmt(args, file_line);
43-
}, "{}", expr);
35+
#[cfg(not(no_fail_fmt))]
36+
mod internal {
37+
use fmt;
38+
use intrinsics;
4439

45-
unsafe { intrinsics::abort() }
46-
}
40+
#[cold] #[inline(never)] // this is the slow path, always
41+
#[lang="fail"]
42+
pub fn fail(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
43+
let (expr, file, line) = *expr_file_line;
44+
let ref file_line = (file, line);
45+
format_args!(|args| -> () {
46+
fail_fmt(args, file_line);
47+
}, "{}", expr);
48+
49+
unsafe { intrinsics::abort() }
50+
}
51+
52+
#[cold] #[inline(never)]
53+
#[lang="fail_bounds_check"]
54+
fn fail_bounds_check(file_line: &(&'static str, uint),
55+
index: uint, len: uint) -> ! {
56+
format_args!(|args| -> () {
57+
fail_fmt(args, file_line);
58+
}, "index out of bounds: the len is {} but the index is {}", len, index);
59+
unsafe { intrinsics::abort() }
60+
}
61+
62+
#[cold] #[inline(never)]
63+
pub fn fail_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
64+
#[allow(ctypes)]
65+
extern {
66+
#[lang = "fail_fmt"]
67+
fn fail_impl(fmt: &fmt::Arguments, file: &'static str,
68+
line: uint) -> !;
69+
}
4770

48-
#[cold] #[inline(never)]
49-
#[lang="fail_bounds_check"]
50-
fn fail_bounds_check(file_line: &(&'static str, uint),
51-
index: uint, len: uint) -> ! {
52-
format_args!(|args| -> () {
53-
fail_fmt(args, file_line);
54-
}, "index out of bounds: the len is {} but the index is {}", len, index);
55-
unsafe { intrinsics::abort() }
71+
let (file, line) = *file_line;
72+
unsafe { fail_impl(fmt, file, line) }
73+
}
5674
}
5775

58-
#[cold] #[inline(never)]
59-
pub fn fail_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
60-
#[allow(ctypes)]
61-
extern {
62-
#[lang = "fail_fmt"]
63-
fn fail_impl(fmt: &fmt::Arguments, file: &'static str,
64-
line: uint) -> !;
76+
#[cfg(no_fail_fmt)]
77+
mod internal {
78+
use fmt;
79+
use intrinsics;
80+
81+
#[cold] #[inline(never)] // this is the slow path, always
82+
#[lang="fail"]
83+
pub fn fail(_expr_file_line: &(&'static str, &'static str, uint)) -> ! {
84+
unsafe { intrinsics::abort() }
85+
}
86+
87+
#[cold] #[inline(never)]
88+
#[lang="fail_bounds_check"]
89+
fn fail_bounds_check(_file_line: &(&'static str, uint),
90+
_index: uint, _len: uint) -> ! {
91+
unsafe { intrinsics::abort() }
92+
}
6593

94+
#[cold] #[inline(never)]
95+
pub fn fail_fmt(_fmt: &fmt::Arguments, _file_line: &(&'static str, uint)) -> ! {
96+
unsafe { intrinsics::abort() }
6697
}
67-
let (file, line) = *file_line;
68-
unsafe { fail_impl(fmt, file, line) }
6998
}

0 commit comments

Comments
 (0)