From 8319b5a252c39716f74c96cea22b3024fb707097 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 30 Dec 2011 13:32:42 -0800 Subject: [PATCH] add cap clause to pretty printer, with a test --- src/comp/syntax/print/pprust.rs | 36 +++++++++++++++++++++++++++++++-- src/test/pretty/cap-clause.rs | 17 ++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/test/pretty/cap-clause.rs diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index c8f6b1b0a875..72e0879e4bf3 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -840,8 +840,13 @@ fn print_expr(s: ps, &&expr: @ast::expr) { } bclose_(s, expr.span, alt_indent_unit); } - ast::expr_fn(proto, decl, body, captures) { // NDM captures - head(s, proto_to_str(proto)); + ast::expr_fn(proto, decl, body, cap_clause) { + // containing cbox, will be closed by print-block at } + cbox(s, indent_unit); + // head-box, will be closed by print-block at start + ibox(s, 0u); + word(s.s, proto_to_str(proto)); + print_cap_clause(s, *cap_clause); print_fn_args_and_ret(s, decl); space(s.s); print_block(s, body); @@ -1156,6 +1161,33 @@ fn print_fn(s: ps, decl: ast::fn_decl, name: ast::ident, print_fn_args_and_ret(s, decl); } +fn print_cap_clause(s: ps, cap_clause: ast::capture_clause) { + fn print_cap_item(s: ps, &&cap_item: @ast::capture_item) { + word(s.s, cap_item.name); + } + + let has_copies = vec::is_not_empty(cap_clause.copies); + let has_moves = vec::is_not_empty(cap_clause.moves); + if !has_copies && !has_moves { ret; } + + word(s.s, "["); + + if has_copies { + word_nbsp(s, "copy"); + commasep(s, inconsistent, cap_clause.copies, print_cap_item); + if has_moves { + word_space(s, ";"); + } + } + + if has_moves { + word_nbsp(s, "move"); + commasep(s, inconsistent, cap_clause.moves, print_cap_item); + } + + word(s.s, "]"); +} + fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) { popen(s); fn print_arg(s: ps, x: ast::arg) { diff --git a/src/test/pretty/cap-clause.rs b/src/test/pretty/cap-clause.rs new file mode 100644 index 000000000000..cbe19a4cd1ec --- /dev/null +++ b/src/test/pretty/cap-clause.rs @@ -0,0 +1,17 @@ +// pp-exact + +fn main() { + let x = 1; + let y = 2; + let z = 3; + let l1 = lambda[copy x]() -> int { x + y }; + let l2 = lambda[copy x; move y]() -> int { x + y }; + let l3 = lambda[move z]() -> int { z }; + + let x = 1; + let y = 2; + let z = 3; + let s1 = sendfn[copy x]() -> int { x + y }; + let s2 = sendfn[copy x; move y]() -> int { x + y }; + let s3 = sendfn[move z]() -> int { z }; +}