Skip to content

Commit 3fcdb7d

Browse files
committed
Adjust the die macro to only accept ~str and to work in statement position
1 parent ed686ae commit 3fcdb7d

File tree

8 files changed

+66
-22
lines changed

8 files changed

+66
-22
lines changed

src/libcore/rt.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ use gc::{cleanup_stack_for_failure, gc, Word};
2626
pub type rust_task = c_void;
2727

2828
extern mod rustrt {
29-
#[rust_stack]
30-
fn rust_upcall_fail(expr: *c_char, file: *c_char, line: size_t);
31-
3229
#[rust_stack]
3330
fn rust_upcall_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char;
3431

@@ -47,11 +44,7 @@ extern mod rustrt {
4744
// gather_rust_rtcalls.
4845
#[rt(fail_)]
4946
pub fn rt_fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
50-
unsafe {
51-
cleanup_stack_for_failure();
52-
rustrt::rust_upcall_fail(expr, file, line);
53-
cast::transmute(())
54-
}
47+
sys::begin_unwind_(expr, file, line);
5548
}
5649

5750
#[rt(fail_bounds_check)]

src/libcore/sys.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#[forbid(deprecated_pattern)];
1616

1717
use cmp::{Eq, Ord};
18-
use libc::c_void;
18+
use libc::{c_void, c_char, size_t};
1919

2020
pub type FreeGlue = fn(*TypeDesc, *c_void);
2121

@@ -43,6 +43,11 @@ extern mod rusti {
4343
fn min_align_of<T>() -> uint;
4444
}
4545

46+
extern mod rustrt {
47+
#[rust_stack]
48+
fn rust_upcall_fail(expr: *c_char, file: *c_char, line: size_t);
49+
}
50+
4651
/// Compares contents of two pointers using the default method.
4752
/// Equivalent to `*x1 == *x2`. Useful for hashtables.
4853
pub pure fn shape_eq<T:Eq>(x1: &T, x2: &T) -> bool {
@@ -108,6 +113,28 @@ pub pure fn log_str<T>(t: &T) -> ~str {
108113
}
109114
}
110115

116+
/** Initiate task failure */
117+
pub pure fn begin_unwind(msg: ~str, file: ~str, line: uint) -> ! {
118+
do str::as_buf(msg) |msg_buf, _msg_len| {
119+
do str::as_buf(file) |file_buf, _file_len| {
120+
unsafe {
121+
let msg_buf = cast::transmute(msg_buf);
122+
let file_buf = cast::transmute(file_buf);
123+
begin_unwind_(msg_buf, file_buf, line as libc::size_t)
124+
}
125+
}
126+
}
127+
}
128+
129+
// XXX: Temorary until rt::rt_fail_ goes away
130+
pub pure fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
131+
unsafe {
132+
gc::cleanup_stack_for_failure();
133+
rustrt::rust_upcall_fail(msg, file, line);
134+
cast::transmute(())
135+
}
136+
}
137+
111138
#[cfg(test)]
112139
pub mod tests {
113140

src/libsyntax/ext/expand.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -378,21 +378,10 @@ fn core_macros() -> ~str {
378378

379379
macro_rules! die(
380380
($msg: expr) => (
381-
{
382-
do core::str::as_buf($msg) |msg_buf, _msg_len| {
383-
do core::str::as_buf(file!()) |file_buf, _file_len| {
384-
unsafe {
385-
let msg_buf = core::cast::transmute(msg_buf);
386-
let file_buf = core::cast::transmute(file_buf);
387-
let line = line!() as core::libc::size_t;
388-
core::rt::rt_fail_(msg_buf, file_buf, line)
389-
}
390-
}
391-
}
392-
}
381+
core::sys::begin_unwind($msg, file!(), line!())
393382
);
394383
() => (
395-
die!(\"explicit failure\")
384+
die!(~\"explicit failure\")
396385
)
397386
)
398387
}";
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:mismatched types
2+
3+
fn main() {
4+
die!("test");
5+
}

src/test/run-fail/die-macro-expr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:test
2+
3+
fn main() {
4+
let i: int = die!(~"test");
5+
}

src/test/run-fail/die-macro-pure.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// error-pattern:test
2+
3+
pure fn f() {
4+
die!(~"test");
5+
}
6+
7+
fn main() {
8+
f();
9+
}

src/test/run-fail/die-macro.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:test
2+
3+
fn main() {
4+
die!(~"test");
5+
}

src/test/run-pass/die-macro.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Just testing that die!() type checks in statement or expr
2+
3+
fn f() {
4+
die!();
5+
6+
let x: int = die!();
7+
}
8+
9+
fn main() {
10+
11+
}

0 commit comments

Comments
 (0)