Skip to content

Commit 6e2ae2c

Browse files
committed
remove support for records from auto_encode
1 parent 5ff6bee commit 6e2ae2c

File tree

2 files changed

+91
-194
lines changed

2 files changed

+91
-194
lines changed

src/libsyntax/ext/auto_encode.rs

Lines changed: 75 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,6 @@ fn expand_auto_encode(
130130
do vec::flat_map(in_items) |item| {
131131
if item.attrs.any(is_auto_encode) {
132132
match item.node {
133-
ast::item_ty(
134-
@ast::Ty {node: ast::ty_rec(ref fields), _},
135-
tps
136-
) => {
137-
let ser_impl = mk_rec_ser_impl(
138-
cx,
139-
item.span,
140-
item.ident,
141-
(*fields),
142-
tps
143-
);
144-
145-
~[filter_attrs(*item), ser_impl]
146-
},
147133
ast::item_struct(@ast::struct_def { fields, _}, tps) => {
148134
let ser_impl = mk_struct_ser_impl(
149135
cx,
@@ -199,20 +185,6 @@ fn expand_auto_decode(
199185
do vec::flat_map(in_items) |item| {
200186
if item.attrs.any(is_auto_decode) {
201187
match item.node {
202-
ast::item_ty(
203-
@ast::Ty {node: ast::ty_rec(ref fields), _},
204-
tps
205-
) => {
206-
let deser_impl = mk_rec_deser_impl(
207-
cx,
208-
item.span,
209-
item.ident,
210-
(*fields),
211-
tps
212-
);
213-
214-
~[filter_attrs(*item), deser_impl]
215-
},
216188
ast::item_struct(@ast::struct_def { fields, _}, tps) => {
217189
let deser_impl = mk_struct_deser_impl(
218190
cx,
@@ -693,67 +665,48 @@ fn mk_deser_method(
693665
}
694666
}
695667

696-
fn mk_rec_ser_impl(
697-
cx: ext_ctxt,
698-
span: span,
699-
ident: ast::ident,
700-
fields: ~[ast::ty_field],
701-
tps: ~[ast::ty_param]
702-
) -> @ast::item {
703-
let fields = mk_ser_fields(cx, span, mk_rec_fields(fields));
704-
705-
// ast for `__s.emit_rec(|| $(fields))`
706-
let body = cx.expr_call(
707-
span,
708-
cx.expr_field(
709-
span,
710-
cx.expr_var(span, ~"__s"),
711-
cx.ident_of(~"emit_rec")
712-
),
713-
~[cx.lambda_stmts(span, fields)]
714-
);
715-
716-
mk_ser_impl(cx, span, ident, tps, body)
717-
}
718-
719-
fn mk_rec_deser_impl(
668+
fn mk_struct_ser_impl(
720669
cx: ext_ctxt,
721670
span: span,
722671
ident: ast::ident,
723-
fields: ~[ast::ty_field],
672+
fields: ~[@ast::struct_field],
724673
tps: ~[ast::ty_param]
725674
) -> @ast::item {
726-
let fields = mk_deser_fields(cx, span, mk_rec_fields(fields));
727-
728-
// ast for `read_rec(|| $(fields))`
729-
let body = cx.expr_call(
730-
span,
731-
cx.expr_field(
732-
span,
733-
cx.expr_var(span, ~"__d"),
734-
cx.ident_of(~"read_rec")
735-
),
736-
~[
737-
cx.lambda_expr(
738-
cx.expr(
675+
let fields = do mk_struct_fields(fields).mapi |idx, field| {
676+
// ast for `|| self.$(name).encode(__s)`
677+
let expr_lambda = cx.lambda_expr(
678+
cx.expr_call(
679+
span,
680+
cx.expr_field(
739681
span,
740-
ast::expr_rec(fields, None)
741-
)
682+
cx.expr_field(
683+
span,
684+
cx.expr_var(span, ~"self"),
685+
field.ident
686+
),
687+
cx.ident_of(~"encode")
688+
),
689+
~[cx.expr_var(span, ~"__s")]
742690
)
743-
]
744-
);
745-
746-
mk_deser_impl(cx, span, ident, tps, body)
747-
}
691+
);
748692

749-
fn mk_struct_ser_impl(
750-
cx: ext_ctxt,
751-
span: span,
752-
ident: ast::ident,
753-
fields: ~[@ast::struct_field],
754-
tps: ~[ast::ty_param]
755-
) -> @ast::item {
756-
let fields = mk_ser_fields(cx, span, mk_struct_fields(fields));
693+
// ast for `__s.emit_field($(name), $(idx), $(expr_lambda))`
694+
cx.stmt(
695+
cx.expr_call(
696+
span,
697+
cx.expr_field(
698+
span,
699+
cx.expr_var(span, ~"__s"),
700+
cx.ident_of(~"emit_field")
701+
),
702+
~[
703+
cx.lit_str(span, @cx.str_of(field.ident)),
704+
cx.lit_uint(span, idx),
705+
expr_lambda,
706+
]
707+
)
708+
)
709+
};
757710

758711
// ast for `__s.emit_struct($(name), || $(fields))`
759712
let ser_body = cx.expr_call(
@@ -780,7 +733,47 @@ fn mk_struct_deser_impl(
780733
fields: ~[@ast::struct_field],
781734
tps: ~[ast::ty_param]
782735
) -> @ast::item {
783-
let fields = mk_deser_fields(cx, span, mk_struct_fields(fields));
736+
let fields = do mk_struct_fields(fields).mapi |idx, field| {
737+
// ast for `|| std::serialize::decode(__d)`
738+
let expr_lambda = cx.lambda(
739+
cx.expr_blk(
740+
cx.expr_call(
741+
span,
742+
cx.expr_path_global(span, ~[
743+
cx.ident_of(~"std"),
744+
cx.ident_of(~"serialize"),
745+
cx.ident_of(~"Decodable"),
746+
cx.ident_of(~"decode"),
747+
]),
748+
~[cx.expr_var(span, ~"__d")]
749+
)
750+
)
751+
);
752+
753+
// ast for `__d.read_field($(name), $(idx), $(expr_lambda))`
754+
let expr: @ast::expr = cx.expr_call(
755+
span,
756+
cx.expr_field(
757+
span,
758+
cx.expr_var(span, ~"__d"),
759+
cx.ident_of(~"read_field")
760+
),
761+
~[
762+
cx.lit_str(span, @cx.str_of(field.ident)),
763+
cx.lit_uint(span, idx),
764+
expr_lambda,
765+
]
766+
);
767+
768+
ast::spanned {
769+
node: ast::field_ {
770+
mutbl: field.mutbl,
771+
ident: field.ident,
772+
expr: expr,
773+
},
774+
span: span,
775+
}
776+
};
784777

785778
// ast for `read_struct($(name), || $(fields))`
786779
let body = cx.expr_call(
@@ -818,16 +811,6 @@ struct field {
818811
mutbl: ast::mutability,
819812
}
820813

821-
fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] {
822-
do fields.map |field| {
823-
field {
824-
span: field.span,
825-
ident: field.node.ident,
826-
mutbl: field.node.mt.mutbl,
827-
}
828-
}
829-
}
830-
831814
fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] {
832815
do fields.map |field| {
833816
let (ident, mutbl) = match field.node.kind {
@@ -847,96 +830,6 @@ fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] {
847830
}
848831
}
849832

850-
fn mk_ser_fields(
851-
cx: ext_ctxt,
852-
span: span,
853-
fields: ~[field]
854-
) -> ~[@ast::stmt] {
855-
do fields.mapi |idx, field| {
856-
// ast for `|| self.$(name).encode(__s)`
857-
let expr_lambda = cx.lambda_expr(
858-
cx.expr_call(
859-
span,
860-
cx.expr_field(
861-
span,
862-
cx.expr_field(
863-
span,
864-
cx.expr_var(span, ~"self"),
865-
field.ident
866-
),
867-
cx.ident_of(~"encode")
868-
),
869-
~[cx.expr_var(span, ~"__s")]
870-
)
871-
);
872-
873-
// ast for `__s.emit_field($(name), $(idx), $(expr_lambda))`
874-
cx.stmt(
875-
cx.expr_call(
876-
span,
877-
cx.expr_field(
878-
span,
879-
cx.expr_var(span, ~"__s"),
880-
cx.ident_of(~"emit_field")
881-
),
882-
~[
883-
cx.lit_str(span, @cx.str_of(field.ident)),
884-
cx.lit_uint(span, idx),
885-
expr_lambda,
886-
]
887-
)
888-
)
889-
}
890-
}
891-
892-
fn mk_deser_fields(
893-
cx: ext_ctxt,
894-
span: span,
895-
fields: ~[field]
896-
) -> ~[ast::field] {
897-
do fields.mapi |idx, field| {
898-
// ast for `|| std::serialize::decode(__d)`
899-
let expr_lambda = cx.lambda(
900-
cx.expr_blk(
901-
cx.expr_call(
902-
span,
903-
cx.expr_path_global(span, ~[
904-
cx.ident_of(~"std"),
905-
cx.ident_of(~"serialize"),
906-
cx.ident_of(~"Decodable"),
907-
cx.ident_of(~"decode"),
908-
]),
909-
~[cx.expr_var(span, ~"__d")]
910-
)
911-
)
912-
);
913-
914-
// ast for `__d.read_field($(name), $(idx), $(expr_lambda))`
915-
let expr: @ast::expr = cx.expr_call(
916-
span,
917-
cx.expr_field(
918-
span,
919-
cx.expr_var(span, ~"__d"),
920-
cx.ident_of(~"read_field")
921-
),
922-
~[
923-
cx.lit_str(span, @cx.str_of(field.ident)),
924-
cx.lit_uint(span, idx),
925-
expr_lambda,
926-
]
927-
);
928-
929-
ast::spanned {
930-
node: ast::field_ {
931-
mutbl: field.mutbl,
932-
ident: field.ident,
933-
expr: expr,
934-
},
935-
span: span,
936-
}
937-
}
938-
}
939-
940833
fn mk_enum_ser_impl(
941834
cx: ext_ctxt,
942835
span: span,

src/test/run-pass/auto-encode.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,33 @@ impl CLike : cmp::Eq {
123123
pure fn eq(&self, other: &CLike) -> bool {
124124
(*self) as int == *other as int
125125
}
126-
pure fn ne(&self, other: &CLike) -> bool { !(*self).eq(other) }
126+
pure fn ne(&self, other: &CLike) -> bool { !self.eq(other) }
127127
}
128128

129129
#[auto_encode]
130130
#[auto_decode]
131-
type Spanned<T> = {lo: uint, hi: uint, node: T};
131+
struct Spanned<T> {
132+
lo: uint,
133+
hi: uint,
134+
node: T,
135+
}
132136

133137
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
134138
pure fn eq(&self, other: &Spanned<T>) -> bool {
135-
(*self).lo == other.lo &&
136-
(*self).hi == other.hi &&
137-
(*self).node == other.node
139+
self.lo == other.lo &&
140+
self.hi == other.hi &&
141+
self.node == other.node
138142
}
139-
pure fn ne(&self, other: &Spanned<T>) -> bool { !(*self).eq(other) }
143+
pure fn ne(&self, other: &Spanned<T>) -> bool { !self.eq(other) }
140144
}
141145

142146
#[auto_encode]
143147
#[auto_decode]
144-
type SomeRec = {v: ~[uint]};
148+
struct SomeStruct { v: ~[uint] }
145149

146150
#[auto_encode]
147151
#[auto_decode]
148-
enum AnEnum = SomeRec;
152+
enum AnEnum = SomeStruct;
149153

150154
#[auto_encode]
151155
#[auto_decode]
@@ -168,12 +172,12 @@ fn main() {
168172
@Plus(@Val(22u), @Val(5u)))");
169173
test_ebml(a);
170174

171-
let a = &{lo: 0u, hi: 5u, node: 22u};
172-
test_prettyprint(a, &~"{lo: 0u, hi: 5u, node: 22u}");
175+
let a = &Spanned {lo: 0u, hi: 5u, node: 22u};
176+
test_prettyprint(a, &~"Spanned {lo: 0u, hi: 5u, node: 22u}");
173177
test_ebml(a);
174178

175-
let a = &AnEnum({v: ~[1u, 2u, 3u]});
176-
test_prettyprint(a, &~"AnEnum({v: ~[1u, 2u, 3u]})");
179+
let a = &AnEnum(SomeStruct {v: ~[1u, 2u, 3u]});
180+
test_prettyprint(a, &~"AnEnum(SomeStruct {v: ~[1u, 2u, 3u]})");
177181
test_ebml(a);
178182

179183
let a = &Point {x: 3u, y: 5u};

0 commit comments

Comments
 (0)