Skip to content

Commit 14adb98

Browse files
committed
Bounded protocols work well enough to compile core, but map reduce has too many type parameters, so we have to get fancier.
1 parent 1dde5e7 commit 14adb98

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/libsyntax/ext/pipes/pipec.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ impl compile of gen_send for message {
110110

111111
cx.item_fn_poly(self.name(),
112112
args_ast,
113-
cx.ty_path_ast_builder(path(next.data_name(), span)
113+
cx.ty_path_ast_builder(path(next.data_name(),
114+
span)
114115
.add_tys(next_tys)),
115116
self.get_params(),
116117
cx.expr_block(body))
@@ -230,7 +231,6 @@ impl compile of to_type_decls for state {
230231
self.ty_params));
231232
}
232233
else {
233-
let ext_cx = cx;
234234
vec::push(items,
235235
cx.item_ty_poly(
236236
self.data_name(),
@@ -240,7 +240,7 @@ impl compile of to_type_decls for state {
240240
.add_tys(~[cx.ty_path_ast_builder(
241241
(self.proto.name + self.data_name())
242242
.add_tys(cx.ty_vars(self.ty_params))),
243-
#ast[ty] { buffer }])),
243+
self.proto.buffer_ty_path(cx)])),
244244
self.ty_params));
245245
};
246246
items
@@ -288,7 +288,7 @@ impl compile of gen_init for protocol {
288288

289289
fn gen_buffer_init(ext_cx: ext_ctxt) -> @ast::expr {
290290
ext_cx.rec(self.states.map_to_vec(|s| {
291-
let fty = ext_cx.ty_path_ast_builder(path(s.name, s.span));
291+
let fty = s.to_ty(ext_cx);
292292
ext_cx.field_imm(s.name, #ast { pipes::mk_packet::<$(fty)>() })
293293
}))
294294
}
@@ -318,19 +318,40 @@ impl compile of gen_init for protocol {
318318
}}
319319
}
320320

321+
fn buffer_ty_path(cx: ext_ctxt) -> @ast::ty {
322+
let mut params = ~[];
323+
for (copy self.states).each |s| {
324+
for s.ty_params.each |tp| {
325+
if !params.contains(tp) {
326+
vec::push(params, tp);
327+
}
328+
}
329+
}
330+
331+
cx.ty_path_ast_builder(path(@~"buffer", self.span)
332+
.add_tys(cx.ty_vars(params)))
333+
}
334+
321335
fn gen_buffer_type(cx: ext_ctxt) -> @ast::item {
322336
let ext_cx = cx;
323-
cx.item_ty(
337+
let mut params = ~[];
338+
let fields = do (copy self.states).map_to_vec |s| {
339+
for s.ty_params.each |tp| {
340+
if !params.contains(tp) {
341+
vec::push(params, tp);
342+
}
343+
}
344+
let ty = s.to_ty(cx);
345+
let fty = #ast[ty] {
346+
pipes::packet<$(ty)>
347+
};
348+
cx.ty_field_imm(s.name, fty)
349+
};
350+
351+
cx.item_ty_poly(
324352
@~"buffer",
325-
cx.ty_rec(
326-
(copy self.states).map_to_vec(
327-
|s| {
328-
let ty = cx.ty_path_ast_builder(path(s.name, s.span));
329-
let fty = #ast[ty] {
330-
pipes::packet<$(ty)>
331-
};
332-
cx.ty_field_imm(s.name, fty)
333-
})))
353+
cx.ty_rec(fields),
354+
params)
334355
}
335356

336357
fn compile(cx: ext_ctxt) -> @ast::item {

src/libsyntax/ext/pipes/proto.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ enum state {
7474
}
7575

7676
impl methods for state {
77-
fn add_message(name: ident, span: span,
77+
fn add_message(name: ident, span: span,
7878
+data: ~[@ast::ty], next: next_state) {
7979
self.messages.push(message(name, span, data, self,
8080
next));
@@ -88,6 +88,7 @@ impl methods for state {
8888
self.name
8989
}
9090

91+
/// Returns the type that is used for the messages.
9192
fn to_ty(cx: ext_ctxt) -> @ast::ty {
9293
cx.ty_path_ast_builder
9394
(path(self.name, self.span).add_tys(cx.ty_vars(self.ty_params)))

0 commit comments

Comments
 (0)