1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 /// Entry point of failure, for details, see std::macros
17 fail!("{}", "explicit failure")
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.
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!()
35 fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
36 static FILE_LINE: (&'static str, uint) = (file!(), line!());
37 ::core::failure::begin_unwind(fmt, &FILE_LINE)
39 format_args!(run_fmt, $fmt, $($arg)*)
43 /// Runtime assertion, for details see std::macros
48 fail!(concat!("assertion failed: ", stringify!($cond)))
51 ($cond:expr, $($arg:tt)*) => (
58 /// Runtime assertion, only without `--cfg ndebug`
60 macro_rules! debug_assert(
62 if cfg!(not(ndebug)) {
68 /// Runtime assertion for equality, for details see std::macros
70 macro_rules! assert_eq(
71 ($cond1:expr, $cond2:expr) => ({
74 if c1 != c2 || c2 != c1 {
75 fail!("expressions not equal, left: {}, right: {}", c1, c2);
80 /// Runtime assertion for equality, only without `--cfg ndebug`
82 macro_rules! debug_assert_eq(
84 if cfg!(not(ndebug)) {
90 /// Runtime assertion, disableable at compile time
92 macro_rules! debug_assert(
93 ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
96 /// Short circuiting evaluation on Err
99 ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
102 /// Writing a formatted string into a writer
105 ($dst:expr, $($arg:tt)*) => (format_args_method!($dst, write_fmt, $($arg)*))
108 /// Writing a formatted string plus a newline into a writer
110 macro_rules! writeln(
111 ($dst:expr, $fmt:expr $($arg:tt)*) => (
112 write!($dst, concat!($fmt, "\n") $($arg)*)
116 /// Write some formatted data into a stream.
118 /// Identical to the macro in `std::macros`
121 ($dst:expr, $($arg:tt)*) => ({
122 format_args_method!($dst, write_fmt, $($arg)*)
127 macro_rules! unreachable( () => (fail!("unreachable code")) )