Skip to content

Commit 33ce116

Browse files
committed
Nicer pretty-printing for anon objs. Closes #499.
(Incidentally, what's the right way to test changes to the pretty-printer? There has to be a better way than what I did, which was to log_err the results of expr_to_str as exprs passed through trans and glance at the output.)
1 parent 4319e7a commit 33ce116

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

src/comp/syntax/print/pprust.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,57 @@ fn print_expr(&ps s, &@ast::expr expr) {
986986
print_expr(s, expr);
987987
pclose(s);
988988
}
989-
case (ast::expr_anon_obj(_, _)) {
990-
word(s.s, "anon obj");
991-
// FIXME (issue #499): nicer pretty-printing of anon objs
989+
case (ast::expr_anon_obj(?anon_obj, ?tps)) {
990+
head(s, "obj");
991+
print_type_params(s, tps);
992+
993+
// Fields
994+
popen(s);
995+
fn print_field(&ps s, &ast::anon_obj_field field) {
996+
ibox(s, indent_unit);
997+
print_mutability(s, field.mut);
998+
print_type(s, *field.ty);
999+
space(s.s);
1000+
word(s.s, field.ident);
1001+
word_space(s, "=");
1002+
print_expr(s, field.expr);
1003+
end(s);
1004+
}
1005+
fn get_span(&ast::anon_obj_field f) -> codemap::span {
1006+
ret f.ty.span;
1007+
}
1008+
alt (anon_obj.fields) {
1009+
case (none) { }
1010+
case (some(?fields)) {
1011+
commasep_cmnt_ivec(s, consistent, fields, print_field,
1012+
get_span);
1013+
}
1014+
}
1015+
pclose(s);
1016+
space(s.s);
1017+
bopen(s);
1018+
1019+
// Methods
1020+
for (@ast::method meth in anon_obj.methods) {
1021+
let ast::ty_param[] typarams = ~[];
1022+
hardbreak_if_not_bol(s);
1023+
maybe_print_comment(s, meth.span.lo);
1024+
print_fn(s, meth.node.meth.decl, meth.node.meth.proto,
1025+
meth.node.ident, typarams);
1026+
word(s.s, " ");
1027+
print_block(s, meth.node.meth.body);
1028+
}
1029+
space(s.s);
9921030

1031+
// With object
1032+
alt (anon_obj.with_obj) {
1033+
case (none) { }
1034+
case (some(?e)) {
1035+
word_space(s, "with");
1036+
print_expr(s, e);
1037+
}
1038+
}
1039+
bclose(s, expr.span);
9931040
}
9941041
}
9951042
s.ann.post(ann_node);

0 commit comments

Comments
 (0)