From 07f4031bb40967f9c5c0ee61b71e1d9cffd34b58 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 27 Nov 2012 18:05:20 -0800 Subject: [PATCH] libsyntax: Implement a macro `die!` to replace the `fail` expression. r=brson --- src/libcore/rt.rs | 9 ++++++--- src/libsyntax/ext/expand.rs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libcore/rt.rs b/src/libcore/rt.rs index 9bc83a5f904..04a8ee30235 100644 --- a/src/libcore/rt.rs +++ b/src/libcore/rt.rs @@ -34,9 +34,12 @@ extern mod rustrt { // 'rt_', otherwise the compiler won't find it. To fix this, see // gather_rust_rtcalls. #[rt(fail_)] -pub fn rt_fail_(expr: *c_char, file: *c_char, line: size_t) { - cleanup_stack_for_failure(); - rustrt::rust_upcall_fail(expr, file, line); +pub fn rt_fail_(expr: *c_char, file: *c_char, line: size_t) -> ! { + unsafe { + cleanup_stack_for_failure(); + rustrt::rust_upcall_fail(expr, file, line); + cast::transmute(()) + } } #[rt(fail_bounds_check)] diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 443a937d4eb..c09655d73d3 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -248,6 +248,26 @@ fn core_macros() -> ~str { #macro[[#warn[f, ...], log(core::warn, #fmt[f, ...])]]; #macro[[#info[f, ...], log(core::info, #fmt[f, ...])]]; #macro[[#debug[f, ...], log(core::debug, #fmt[f, ...])]]; + + macro_rules! die( + ($msg: expr) => ( + { + do core::str::as_buf($msg) |msg_buf, _msg_len| { + do core::str::as_buf(file!()) |file_buf, _file_len| { + unsafe { + let msg_buf = core::cast::transmute(msg_buf); + let file_buf = core::cast::transmute(file_buf); + let line = line!() as core::libc::size_t; + core::rt::rt_fail_(msg_buf, file_buf, line) + } + } + } + } + ); + () => ( + die!(\"explicit failure\") + ) + ) }"; }