Skip to content

Commit b4bb8c0

Browse files
committed
std: add begin_unwind_fmt that reduces codesize for formatted fail!().
This ends up saving a single `call` instruction in the optimised code, but saves a few hundred lines of non-optimised IR for `fn main() { fail!("foo {}", "bar"); }` (comparing against the minimal generic baseline from the parent commit).
1 parent e5abe66 commit b4bb8c0

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/libstd/macros.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,18 @@ macro_rules! fail(
4848
::std::rt::begin_unwind($msg, file!(), line!())
4949
);
5050
($fmt:expr, $($arg:tt)*) => (
51-
::std::rt::begin_unwind(format!($fmt, $($arg)*), file!(), line!())
52-
)
51+
{
52+
// a closure can't have return type !, so we need a full
53+
// function to pass to format_args!, *and* we need the
54+
// file and line numbers right here; so an inner bare fn
55+
// is our only choice.
56+
#[inline]
57+
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
58+
::std::rt::begin_unwind_fmt(fmt, file!(), line!())
59+
}
60+
format_args!(run_fmt, $fmt, $($arg)*)
61+
}
62+
)
5363
)
5464

5565
#[macro_export]

src/libstd/rt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use self::task::{Task, BlockedTask};
6969
pub use self::util::default_sched_threads;
7070

7171
// Export unwinding facilities used by the failure macros
72-
pub use self::unwind::{begin_unwind, begin_unwind_raw};
72+
pub use self::unwind::{begin_unwind, begin_unwind_raw, begin_unwind_fmt};
7373

7474
// FIXME: these probably shouldn't be public...
7575
#[doc(hidden)]

src/libstd/rt/unwind.rs

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use any::{Any, AnyRefExt};
5959
use c_str::CString;
6060
use cast;
61+
use fmt;
6162
use kinds::Send;
6263
use option::{Some, None, Option};
6364
use prelude::drop;
@@ -382,6 +383,17 @@ pub fn begin_unwind_raw(msg: *u8, file: *u8, line: uint) -> ! {
382383
begin_unwind(msg, file, line as uint)
383384
}
384385

386+
/// The entry point for unwinding with a formatted message.
387+
///
388+
/// This is designed to reduce the amount of code required at the call
389+
/// site as much as possible (so that `fail!()` has as low an implact
390+
/// on (e.g.) the inlining of other functions as possible), by moving
391+
/// the actual formatting into this shared place.
392+
#[inline(never)] #[cold]
393+
pub fn begin_unwind_fmt(msg: &fmt::Arguments, file: &'static str, line: uint) -> ! {
394+
begin_unwind_inner(~fmt::format(msg), file, line)
395+
}
396+
385397
/// This is the entry point of unwinding for fail!() and assert!().
386398
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
387399
pub fn begin_unwind<M: Any + Send>(msg: M, file: &'static str, line: uint) -> ! {

0 commit comments

Comments
 (0)