Skip to content

Commit f2af4ca

Browse files
committed
core: Allow formatted failure and assert in core
With std::fmt having migrated, the failure macro can be expressed in its full glory.
1 parent c255568 commit f2af4ca

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/libcore/macros.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,29 @@
1414
#[macro_export]
1515
macro_rules! fail(
1616
() => (
17-
fail!("explicit failure")
17+
fail!("{}", "explicit failure")
1818
);
1919
($msg:expr) => (
20-
::core::failure::begin_unwind($msg, file!(), line!())
20+
fail!("{}", $msg)
2121
);
22+
($fmt:expr, $($arg:tt)*) => ({
23+
// a closure can't have return type !, so we need a full
24+
// function to pass to format_args!, *and* we need the
25+
// file and line numbers right here; so an inner bare fn
26+
// is our only choice.
27+
//
28+
// LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
29+
// is #[cold] and #[inline(never)] and because this is flagged as cold
30+
// as returning !. We really do want this to be inlined, however,
31+
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
32+
// were seen when forcing this to be inlined, and that number just goes
33+
// up with the number of calls to fail!()
34+
#[inline(always)]
35+
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
36+
::core::failure::begin_unwind(fmt, file!(), line!())
37+
}
38+
format_args!(run_fmt, $fmt, $($arg)*)
39+
});
2240
)
2341

2442
/// Runtime assertion, for details see std::macros
@@ -29,6 +47,11 @@ macro_rules! assert(
2947
fail!(concat!("assertion failed: ", stringify!($cond)))
3048
}
3149
);
50+
($cond:expr, $($arg:tt)*) => (
51+
if !$cond {
52+
fail!($($arg)*)
53+
}
54+
);
3255
)
3356

3457
/// Runtime assertion, disableable at compile time

0 commit comments

Comments
 (0)