Skip to content

Commit cdc266e

Browse files
committed
Fix deriving(IterBytes) to use the new for-loop protocol
1 parent b01a40d commit cdc266e

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/libsyntax/ext/deriving/iter_bytes.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{meta_item, item, expr};
11+
use ast::{meta_item, item, expr, and};
1212
use codemap::span;
1313
use ext::base::ext_ctxt;
1414
use ext::build;
@@ -31,7 +31,7 @@ pub fn expand_deriving_iter_bytes(cx: @ext_ctxt,
3131
Literal(Path::new(~[~"bool"])),
3232
Literal(Path::new(~[~"core", ~"to_bytes", ~"Cb"]))
3333
],
34-
ret_ty: nil_ty(),
34+
ret_ty: Literal(Path::new(~[~"bool"])),
3535
const_nonmatching: false,
3636
combine_substructure: iter_bytes_substructure
3737
}
@@ -58,13 +58,11 @@ fn iter_bytes_substructure(cx: @ext_ctxt, span: span, substr: &Substructure) ->
5858
};
5959
let iter_bytes_ident = substr.method_ident;
6060
let call_iterbytes = |thing_expr| {
61-
build::mk_stmt(
62-
cx, span,
63-
build::mk_method_call(cx, span,
64-
thing_expr, iter_bytes_ident,
65-
copy lsb0_f))
61+
build::mk_method_call(cx, span,
62+
thing_expr, iter_bytes_ident,
63+
copy lsb0_f)
6664
};
67-
let mut stmts = ~[];
65+
let mut exprs = ~[];
6866
let fields;
6967
match *substr.fields {
7068
Struct(ref fs) => {
@@ -78,16 +76,22 @@ fn iter_bytes_substructure(cx: @ext_ctxt, span: span, substr: &Substructure) ->
7876
None => build::mk_uint(cx, span, index)
7977
};
8078

81-
stmts.push(call_iterbytes(discriminant));
79+
exprs.push(call_iterbytes(discriminant));
8280

8381
fields = fs;
8482
}
8583
_ => cx.span_bug(span, "Impossible substructure in `deriving(IterBytes)`")
8684
}
8785

8886
for fields.each |&(_, field, _)| {
89-
stmts.push(call_iterbytes(field));
87+
exprs.push(call_iterbytes(field));
9088
}
9189

92-
build::mk_block(cx, span, ~[], stmts, None)
90+
if exprs.len() == 0 {
91+
cx.span_bug(span, "#[deriving(IterBytes)] needs at least one field");
92+
}
93+
94+
do vec::foldl(exprs[0], exprs.slice(1, exprs.len())) |prev, me| {
95+
build::mk_binary(cx, span, and, prev, *me)
96+
}
9397
}

0 commit comments

Comments
 (0)