Skip to content

Commit 8319b5a

Browse files
committed
add cap clause to pretty printer, with a test
1 parent bfc9a49 commit 8319b5a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/comp/syntax/print/pprust.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,13 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
840840
}
841841
bclose_(s, expr.span, alt_indent_unit);
842842
}
843-
ast::expr_fn(proto, decl, body, captures) { // NDM captures
844-
head(s, proto_to_str(proto));
843+
ast::expr_fn(proto, decl, body, cap_clause) {
844+
// containing cbox, will be closed by print-block at }
845+
cbox(s, indent_unit);
846+
// head-box, will be closed by print-block at start
847+
ibox(s, 0u);
848+
word(s.s, proto_to_str(proto));
849+
print_cap_clause(s, *cap_clause);
845850
print_fn_args_and_ret(s, decl);
846851
space(s.s);
847852
print_block(s, body);
@@ -1156,6 +1161,33 @@ fn print_fn(s: ps, decl: ast::fn_decl, name: ast::ident,
11561161
print_fn_args_and_ret(s, decl);
11571162
}
11581163

1164+
fn print_cap_clause(s: ps, cap_clause: ast::capture_clause) {
1165+
fn print_cap_item(s: ps, &&cap_item: @ast::capture_item) {
1166+
word(s.s, cap_item.name);
1167+
}
1168+
1169+
let has_copies = vec::is_not_empty(cap_clause.copies);
1170+
let has_moves = vec::is_not_empty(cap_clause.moves);
1171+
if !has_copies && !has_moves { ret; }
1172+
1173+
word(s.s, "[");
1174+
1175+
if has_copies {
1176+
word_nbsp(s, "copy");
1177+
commasep(s, inconsistent, cap_clause.copies, print_cap_item);
1178+
if has_moves {
1179+
word_space(s, ";");
1180+
}
1181+
}
1182+
1183+
if has_moves {
1184+
word_nbsp(s, "move");
1185+
commasep(s, inconsistent, cap_clause.moves, print_cap_item);
1186+
}
1187+
1188+
word(s.s, "]");
1189+
}
1190+
11591191
fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) {
11601192
popen(s);
11611193
fn print_arg(s: ps, x: ast::arg) {

src/test/pretty/cap-clause.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// pp-exact
2+
3+
fn main() {
4+
let x = 1;
5+
let y = 2;
6+
let z = 3;
7+
let l1 = lambda[copy x]() -> int { x + y };
8+
let l2 = lambda[copy x; move y]() -> int { x + y };
9+
let l3 = lambda[move z]() -> int { z };
10+
11+
let x = 1;
12+
let y = 2;
13+
let z = 3;
14+
let s1 = sendfn[copy x]() -> int { x + y };
15+
let s2 = sendfn[copy x; move y]() -> int { x + y };
16+
let s3 = sendfn[move z]() -> int { z };
17+
}

0 commit comments

Comments
 (0)