Made fail! and assert! accept both &'static str and ~str, as well as a fmt! like format list.
Unwinding through macros now happens as a call to the trait function `FailWithCause::fail_with()`, which consumes self, allowing to use a more generic failure object in the future.
This commit is contained in:
parent
1d53babd2f
commit
e1be9ae224
9 changed files with 90 additions and 26 deletions
|
|
@ -1101,7 +1101,7 @@ fn mk_enum_deser_body(
|
|||
};
|
||||
|
||||
let quoted_expr = copy quote_expr!(
|
||||
::core::sys::begin_unwind(~"explicit failure", ~"empty", 1);
|
||||
::core::sys::FailWithCause::fail_with("explicit failure", "empty", 1);
|
||||
).node;
|
||||
|
||||
let impossible_case = ast::arm {
|
||||
|
|
|
|||
|
|
@ -474,11 +474,12 @@ pub fn mk_unreachable(cx: @ext_ctxt, span: span) -> @ast::expr {
|
|||
~[
|
||||
cx.ident_of(~"core"),
|
||||
cx.ident_of(~"sys"),
|
||||
cx.ident_of(~"begin_unwind"),
|
||||
cx.ident_of(~"FailWithCause"),
|
||||
cx.ident_of(~"fail_with"),
|
||||
],
|
||||
~[
|
||||
mk_uniq_str(cx, span, ~"internal error: entered unreachable code"),
|
||||
mk_uniq_str(cx, span, loc.file.name),
|
||||
mk_base_str(cx, span, ~"internal error: entered unreachable code"),
|
||||
mk_base_str(cx, span, loc.file.name),
|
||||
mk_uint(cx, span, loc.line),
|
||||
]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -415,6 +415,7 @@ pub fn core_macros() -> ~str {
|
|||
__log(1u32, fmt!( $($arg),+ ))
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! warn (
|
||||
($arg:expr) => (
|
||||
__log(2u32, fmt!( \"%?\", $arg ))
|
||||
|
|
@ -423,6 +424,7 @@ pub fn core_macros() -> ~str {
|
|||
__log(2u32, fmt!( $($arg),+ ))
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! info (
|
||||
($arg:expr) => (
|
||||
__log(3u32, fmt!( \"%?\", $arg ))
|
||||
|
|
@ -431,6 +433,7 @@ pub fn core_macros() -> ~str {
|
|||
__log(3u32, fmt!( $($arg),+ ))
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! debug (
|
||||
($arg:expr) => (
|
||||
__log(4u32, fmt!( \"%?\", $arg ))
|
||||
|
|
@ -441,35 +444,48 @@ pub fn core_macros() -> ~str {
|
|||
)
|
||||
|
||||
macro_rules! fail(
|
||||
($msg: expr) => (
|
||||
::core::sys::begin_unwind($msg, file!().to_owned(), line!())
|
||||
);
|
||||
() => (
|
||||
fail!(~\"explicit failure\")
|
||||
fail!(\"explicit failure\")
|
||||
);
|
||||
($msg:expr) => (
|
||||
::core::sys::FailWithCause::fail_with($msg, file!(), line!())
|
||||
);
|
||||
($( $arg:expr ),+) => (
|
||||
::core::sys::FailWithCause::fail_with(fmt!( $($arg),+ ), file!(), line!())
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! assert(
|
||||
($cond:expr) => {
|
||||
if !$cond {
|
||||
::core::sys::fail_assert(stringify!($cond), file!(), line!())
|
||||
::core::sys::FailWithCause::fail_with(
|
||||
~\"assertion failed: \" + stringify!($cond), file!(), line!())
|
||||
}
|
||||
};
|
||||
($cond:expr, $msg:expr) => {
|
||||
if !$cond {
|
||||
::core::sys::fail_assert($msg, file!(), line!())
|
||||
::core::sys::FailWithCause::fail_with($msg, file!(), line!())
|
||||
}
|
||||
};
|
||||
($cond:expr, $( $arg:expr ),+) => {
|
||||
if !$cond {
|
||||
::core::sys::FailWithCause::fail_with(fmt!( $($arg),+ ), file!(), line!())
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
macro_rules! assert_eq (
|
||||
($given:expr , $expected:expr) =>
|
||||
({let given_val = $given;
|
||||
let expected_val = $expected;
|
||||
// check both directions of equality....
|
||||
if !((given_val == expected_val) && (expected_val == given_val)) {
|
||||
fail!(fmt!(\"expected: %?, given: %?\",expected_val,given_val));
|
||||
}}))
|
||||
($given:expr , $expected:expr) => (
|
||||
{
|
||||
let given_val = $given;
|
||||
let expected_val = $expected;
|
||||
// check both directions of equality....
|
||||
if !((given_val == expected_val) && (expected_val == given_val)) {
|
||||
fail!(fmt!(\"left: %? != right: %?\", given_val, expected_val));
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! condition (
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue