Skip to content

Commit 8e28f23

Browse files
committed
core: add macro_rules! for "condition! { c: in -> out; }".
1 parent 263136d commit 8e28f23

File tree

9 files changed

+58
-35
lines changed

9 files changed

+58
-35
lines changed

src/libcore/condition.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010

1111
// helper for transmutation, shown below.
1212
type RustClosure = (int,int);
13-
struct Handler<T, U:Copy> {
13+
pub struct Handler<T, U> {
1414
handle: RustClosure,
1515
prev: Option<@Handler<T, U>>,
1616
}
1717

18-
struct Condition<T, U:Copy> {
18+
pub struct Condition<T, U> {
19+
name: &static/str,
1920
key: task::local_data::LocalDataKey<Handler<T,U>>
2021
}
2122

22-
impl<T, U: Copy> Condition<T,U> {
23+
impl<T, U> Condition<T,U> {
2324

2425
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
2526
unsafe {
@@ -32,7 +33,9 @@ impl<T, U: Copy> Condition<T,U> {
3233

3334
fn raise(t:&T) -> U {
3435
do self.raise_default(t) {
35-
fail ~"Unhandled condition";
36+
fail fmt!("Unhandled condition: %s: %?",
37+
self.name,
38+
t);
3639
}
3740
}
3841

@@ -65,13 +68,13 @@ impl<T, U: Copy> Condition<T,U> {
6568

6669

6770

68-
struct Trap<T, U:Copy> {
71+
struct Trap<T, U> {
6972
cond: &Condition<T,U>,
7073
handler: @Handler<T, U>
7174
}
7275

73-
impl<T, U: Copy> Trap<T,U> {
74-
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V {
76+
impl<T, U> Trap<T,U> {
77+
fn in<V>(&self, inner: &self/fn() -> V) -> V {
7578
unsafe {
7679
let _g = Guard { cond: self.cond };
7780
debug!("Trap: pushing handler to TLS");
@@ -81,7 +84,7 @@ impl<T, U: Copy> Trap<T,U> {
8184
}
8285
}
8386

84-
struct Guard<T, U:Copy> {
87+
struct Guard<T, U> {
8588
cond: &Condition<T,U>,
8689
drop {
8790
unsafe {
@@ -105,21 +108,21 @@ struct Guard<T, U:Copy> {
105108
#[cfg(test)]
106109
mod test {
107110

108-
fn sadness_key(_x: @Handler<int,int>) { }
109-
const sadness_condition : Condition<int,int> =
110-
Condition { key: sadness_key };
111+
condition! {
112+
sadness: int -> int;
113+
}
111114

112115
fn trouble(i: int) {
113116
debug!("trouble: raising conition");
114-
let j = sadness_condition.raise(&i);
117+
let j = sadness::cond.raise(&i);
115118
debug!("trouble: handler recovered with %d", j);
116119
}
117120

118121
fn nested_trap_test_inner() {
119122

120123
let mut inner_trapped = false;
121124

122-
do sadness_condition.trap(|_j| {
125+
do sadness::cond.trap(|_j| {
123126
debug!("nested_trap_test_inner: in handler");
124127
inner_trapped = true;
125128
0
@@ -136,7 +139,7 @@ mod test {
136139

137140
let mut outer_trapped = false;
138141

139-
do sadness_condition.trap(|_j| {
142+
do sadness::cond.trap(|_j| {
140143
debug!("nested_trap_test_outer: in handler");
141144
outer_trapped = true; 0
142145
}).in {
@@ -152,12 +155,12 @@ mod test {
152155

153156
let mut inner_trapped = false;
154157

155-
do sadness_condition.trap(|_j| {
158+
do sadness::cond.trap(|_j| {
156159
debug!("nested_reraise_trap_test_inner: in handler");
157160
inner_trapped = true;
158161
let i = 10;
159162
debug!("nested_reraise_trap_test_inner: handler re-raising");
160-
sadness_condition.raise(&i)
163+
sadness::cond.raise(&i)
161164
}).in {
162165
debug!("nested_reraise_trap_test_inner: in protected block");
163166
trouble(1);
@@ -171,7 +174,7 @@ mod test {
171174

172175
let mut outer_trapped = false;
173176

174-
do sadness_condition.trap(|_j| {
177+
do sadness::cond.trap(|_j| {
175178
debug!("nested_reraise_trap_test_outer: in handler");
176179
outer_trapped = true; 0
177180
}).in {
@@ -187,9 +190,9 @@ mod test {
187190

188191
let mut trapped = false;
189192

190-
do sadness_condition.trap(|j| {
193+
do sadness::cond.trap(|j| {
191194
debug!("test_default: in handler");
192-
sadness_condition.raise_default(j, || {trapped=true; 5})
195+
sadness::cond.raise_default(j, || {trapped=true; 5})
193196
}).in {
194197
debug!("test_default: in protected block");
195198
trouble(1);

src/libcore/core.rc

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ mod core {
242242
pub const debug : u32 = 4_u32;
243243

244244
pub use cmp;
245+
pub use condition;
245246
}
246247

247248

src/librustc/front/intrinsic_inject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export inject_intrinsic;
1717
fn inject_intrinsic(sess: Session,
1818
crate: @ast::crate) -> @ast::crate {
1919

20-
let intrinsic_module = @include_str!("intrinsic.rs");
20+
let intrinsic_module = @(include_str!("intrinsic.rs").to_owned());
2121

2222
let item = parse::parse_item_from_source_str(~"<intrinsic>",
2323
intrinsic_module,

src/librusti/rusti.rc

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn run(repl: Repl, input: ~str) -> Repl {
138138
};
139139

140140
debug!("building driver input");
141-
let head = include_str!("wrapper.rs");
141+
let head = include_str!("wrapper.rs").to_owned();
142142
let foot = fmt!("%s\nfn main() {\n%s\n\nprint({\n%s\n})\n}",
143143
repl.view_items, repl.stmts, input);
144144
let wrapped = driver::str_input(head + foot);

src/libsyntax/ext/expand.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,30 @@ fn core_macros() -> ~str {
286286

287287
macro_rules! die(
288288
($msg: expr) => (
289-
core::sys::begin_unwind($msg, file!(), line!())
289+
core::sys::begin_unwind($msg,
290+
file!().to_owned(), line!())
290291
);
291292
() => (
292293
die!(~\"explicit failure\")
293294
)
294295
)
296+
297+
macro_rules! condition (
298+
299+
{ $c:ident: $in:ty -> $out:ty; } => {
300+
301+
mod $c {
302+
fn key(_x: @core::condition::Handler<$in,$out>) { }
303+
304+
pub const cond : core::condition::Condition<$in,$out> =
305+
core::condition::Condition {
306+
name: stringify!(c),
307+
key: key
308+
};
309+
}
310+
}
311+
)
312+
295313
}";
296314
}
297315

src/libsyntax/ext/source_util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use ext::base::*;
1212
use codemap::{span, Loc, FileMap};
1313
use print::pprust;
14-
use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_uniq_str};
14+
use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_base_str};
1515

1616
export expand_line;
1717
export expand_col;
@@ -46,19 +46,19 @@ fn expand_file(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
4646
base::check_zero_tts(cx, sp, tts, "file!");
4747
let Loc { file: @FileMap { name: filename, _ }, _ } =
4848
cx.codemap().lookup_char_pos(sp.lo);
49-
base::mr_expr(mk_uniq_str(cx, sp, filename))
49+
base::mr_expr(mk_base_str(cx, sp, filename))
5050
}
5151

5252
fn expand_stringify(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
5353
-> base::mac_result {
5454
let s = pprust::tts_to_str(tts, cx.parse_sess().interner);
55-
base::mr_expr(mk_uniq_str(cx, sp, s))
55+
base::mr_expr(mk_base_str(cx, sp, s))
5656
}
5757

5858
fn expand_mod(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
5959
-> base::mac_result {
6060
base::check_zero_tts(cx, sp, tts, "module_path!");
61-
base::mr_expr(mk_uniq_str(cx, sp,
61+
base::mr_expr(mk_base_str(cx, sp,
6262
str::connect(cx.mod_path().map(
6363
|x| cx.str_of(*x)), ~"::")))
6464
}
@@ -83,7 +83,7 @@ fn expand_include_str(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
8383
}
8484
}
8585

86-
base::mr_expr(mk_uniq_str(cx, sp, result::unwrap(res)))
86+
base::mr_expr(mk_base_str(cx, sp, result::unwrap(res)))
8787
}
8888

8989
fn expand_include_bin(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])

src/test/bench/core-std.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::map::{Map, HashMap};
1919
use io::{Reader, ReaderUtil};
2020

2121
macro_rules! bench (
22-
($id:ident) => (maybe_run_test(argv, stringify!($id), $id))
22+
($id:ident) => (maybe_run_test(argv, stringify!($id).to_owned(), $id))
2323
)
2424

2525
fn main() {

src/test/run-pass/html-literals.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ macro_rules! parse_node (
3939
) => (
4040
parse_node!(
4141
[$(: $tags ($(:$tag_nodes),*))*];
42-
[$(:$head_nodes,)* :tag(stringify!($head), ~[$($nodes),*])];
42+
[$(:$head_nodes,)* :tag(stringify!($head).to_owned(),
43+
~[$($nodes),*])];
4344
$($rest)*
4445
)
4546
);
@@ -75,7 +76,7 @@ macro_rules! parse_node (
7576
) => (
7677
parse_node!(
7778
[$(: $tags ($(:$tag_nodes),*))*];
78-
[$(:$nodes,)* :text(stringify!($word))];
79+
[$(:$nodes,)* :text(stringify!($word).to_owned())];
7980
$($rest)*
8081
)
8182
);

src/test/run-pass/syntax-extension-source-utils.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ mod m1 {
1616
#[legacy_exports];
1717
mod m2 {
1818
#[legacy_exports];
19-
fn where_am_i() -> ~str { module_path!() }
19+
fn where_am_i() -> ~str { (module_path!()).to_owned() }
2020
}
2121
}
2222

2323
fn main() {
2424
assert(line!() == 24);
2525
assert(col!() == 11);
26-
assert(file!().ends_with(~"syntax-extension-source-utils.rs"));
27-
assert(stringify!((2*3) + 5) == ~"( 2 * 3 ) + 5");
28-
assert(include!("syntax-extension-source-utils-files/includeme.fragment")
26+
assert(file!().to_owned().ends_with(~"syntax-extension-source-utils.rs"));
27+
assert(stringify!((2*3) + 5).to_owned() == ~"( 2 * 3 ) + 5");
28+
assert(include!("syntax-extension-source-utils-files/includeme.fragment").to_owned()
2929
== ~"victory robot 6");
3030

3131
assert(
32-
include_str!("syntax-extension-source-utils-files/includeme.fragment")
32+
include_str!("syntax-extension-source-utils-files/includeme.fragment").to_owned()
3333
.starts_with(~"/* this is for "));
3434
assert(
3535
include_bin!("syntax-extension-source-utils-files/includeme.fragment")

0 commit comments

Comments
 (0)