Skip to content

Commit 2b457c2

Browse files
committed
Pretty print explicit self types. Work on #2585.
1 parent a65366d commit 2b457c2

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

src/libsyntax/print/pprust.rs

+63-17
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ fn print_struct(s: ps, struct_def: @ast::struct_def, tps: ~[ast::ty_param],
617617
cbox(s, indent_unit);
618618
ibox(s, 4);
619619
word(s.s, ~"new(");
620-
print_fn_args(s, ctor.node.dec, ~[]);
620+
print_fn_args(s, ctor.node.dec, ~[], none);
621621
word(s.s, ~")");
622622
space(s.s);
623623
print_block(s, ctor.node.body);
@@ -1193,7 +1193,7 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
11931193
// head-box, will be closed by print-block at start
11941194
ibox(s, 0u);
11951195
word(s.s, fn_header_info_to_str(none, decl.purity, some(proto)));
1196-
print_fn_args_and_ret(s, decl, *cap_clause);
1196+
print_fn_args_and_ret(s, decl, *cap_clause, none);
11971197
space(s.s);
11981198
print_block(s, body);
11991199
}
@@ -1522,33 +1522,64 @@ fn print_pat(s: ps, &&pat: @ast::pat) {
15221522
s.ann.post(ann_node);
15231523
}
15241524
1525+
fn print_self_ty(s: ps, self_ty: ast::self_ty_) {
1526+
match self_ty {
1527+
ast::sty_static | ast::sty_by_ref => {}
1528+
ast::sty_value => { word(s.s, ~"self"); }
1529+
ast::sty_region(m) => {
1530+
word(s.s, ~"&"); print_mutability(s, m); word(s.s, ~"self");
1531+
}
1532+
ast::sty_box(m) => {
1533+
word(s.s, ~"@"); print_mutability(s, m); word(s.s, ~"self");
1534+
}
1535+
ast::sty_uniq(m) => {
1536+
word(s.s, ~"~"); print_mutability(s, m); word(s.s, ~"self");
1537+
}
1538+
}
1539+
}
1540+
15251541
fn print_fn(s: ps, decl: ast::fn_decl, name: ast::ident,
15261542
typarams: ~[ast::ty_param],
15271543
opt_self_ty: option<ast::self_ty_>) {
15281544
head(s, fn_header_info_to_str(opt_self_ty, decl.purity, none));
15291545
word(s.s, *name);
15301546
print_type_params(s, typarams);
1531-
print_fn_args_and_ret(s, decl, ~[]);
1547+
print_fn_args_and_ret(s, decl, ~[], opt_self_ty);
15321548
}
15331549

15341550
fn print_fn_args(s: ps, decl: ast::fn_decl,
1535-
cap_items: ~[ast::capture_item]) {
1536-
commasep(s, inconsistent, decl.inputs, print_arg);
1537-
if cap_items.is_not_empty() {
1538-
let mut first = decl.inputs.is_empty();
1539-
for cap_items.each |cap_item| {
1540-
if first { first = false; } else { word_space(s, ~","); }
1541-
if cap_item.is_move { word_nbsp(s, ~"move") }
1542-
else { word_nbsp(s, ~"copy") }
1543-
word(s.s, *cap_item.name);
1544-
}
1551+
cap_items: ~[ast::capture_item],
1552+
opt_self_ty: option<ast::self_ty_>) {
1553+
// It is unfortunate to duplicate the commasep logic, but we
1554+
// we want the self type, the args, and the capture clauses all
1555+
// in the same box.
1556+
box(s, 0u, inconsistent);
1557+
let mut first = true;
1558+
for opt_self_ty.each |self_ty| {
1559+
first = false;
1560+
print_self_ty(s, self_ty);
1561+
}
1562+
1563+
for decl.inputs.each |arg| {
1564+
if first { first = false; } else { word_space(s, ~","); }
1565+
print_arg(s, arg);
15451566
}
1567+
1568+
for cap_items.each |cap_item| {
1569+
if first { first = false; } else { word_space(s, ~","); }
1570+
if cap_item.is_move { word_nbsp(s, ~"move") }
1571+
else { word_nbsp(s, ~"copy") }
1572+
word(s.s, *cap_item.name);
1573+
}
1574+
1575+
end(s);
15461576
}
15471577

15481578
fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl,
1549-
cap_items: ~[ast::capture_item]) {
1579+
cap_items: ~[ast::capture_item],
1580+
opt_self_ty: option<ast::self_ty_>) {
15501581
popen(s);
1551-
print_fn_args(s, decl, cap_items);
1582+
print_fn_args(s, decl, cap_items, opt_self_ty);
15521583
pclose(s);
15531584

15541585
maybe_print_comment(s, decl.output.span.lo);
@@ -1562,7 +1593,7 @@ fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl,
15621593
fn print_fn_block_args(s: ps, decl: ast::fn_decl,
15631594
cap_items: ~[ast::capture_item]) {
15641595
word(s.s, ~"|");
1565-
print_fn_args(s, decl, cap_items);
1596+
print_fn_args(s, decl, cap_items, none);
15661597
word(s.s, ~"|");
15671598
if decl.output.node != ast::ty_infer {
15681599
space_if_not_bol(s);
@@ -1741,9 +1772,24 @@ fn print_ty_fn(s: ps, opt_proto: option<ast::proto>,
17411772
match id { some(id) => { word(s.s, ~" "); word(s.s, *id); } _ => () }
17421773
match tps { some(tps) => print_type_params(s, tps), _ => () }
17431774
zerobreak(s.s);
1775+
17441776
popen(s);
1745-
commasep(s, inconsistent, decl.inputs, print_arg);
1777+
// It is unfortunate to duplicate the commasep logic, but we
1778+
// we want the self type, the args, and the capture clauses all
1779+
// in the same box.
1780+
box(s, 0u, inconsistent);
1781+
let mut first = true;
1782+
for opt_self_ty.each |self_ty| {
1783+
first = false;
1784+
print_self_ty(s, self_ty);
1785+
}
1786+
for decl.inputs.each |arg| {
1787+
if first { first = false; } else { word_space(s, ~","); }
1788+
print_arg(s, arg);
1789+
}
1790+
end(s);
17461791
pclose(s);
1792+
17471793
maybe_print_comment(s, decl.output.span.lo);
17481794
if decl.output.node != ast::ty_nil {
17491795
space_if_not_bol(s);

0 commit comments

Comments
 (0)