@@ -237,10 +237,16 @@ fn bopen(s: ps) {
237237}
238238
239239fn bclose_(s: ps, span: codemap::span, indented: uint) {
240+ bclose_maybe_open(s, span, indented, true);
241+ }
242+ fn bclose_maybe_open (s: ps, span: codemap::span, indented: uint,
243+ close_box: bool) {
240244 maybe_print_comment(s, span.hi);
241245 break_offset_if_not_bol(s, 1u, -(indented as int));
242246 word(s.s, ~" } ") ;
243- end ( s) ; // close the outer-box
247+ if close_box {
248+ end ( s) ; // close the outer-box
249+ }
244250}
245251fn bclose ( s : ps , span : codemap:: span ) { bclose_ ( s, span, indent_unit) ; }
246252
@@ -827,20 +833,27 @@ fn print_block(s: ps, blk: ast::blk) {
827833 print_possibly_embedded_block(s, blk, block_normal, indent_unit);
828834}
829835
836+ fn print_block_unclosed(s: ps, blk: ast::blk) {
837+ print_possibly_embedded_block_(s, blk, block_normal, indent_unit, ~[],
838+ false);
839+ }
840+
830841fn print_block_with_attrs(s: ps, blk: ast::blk, attrs: ~[ast::attribute]) {
831- print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs);
842+ print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs,
843+ true);
832844}
833845
834846enum embed_type { block_block_fn, block_normal, }
835847
836848fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
837849 indented: uint) {
838850 print_possibly_embedded_block_(
839- s, blk, embedded, indented, ~[]);
851+ s, blk, embedded, indented, ~[], true );
840852}
841853
842854fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
843- indented: uint, attrs: ~[ast::attribute]) {
855+ indented: uint, attrs: ~[ast::attribute],
856+ close_box: bool) {
844857 match blk.node.rules {
845858 ast::unchecked_blk => word(s.s, ~" unchecked") ,
846859 ast:: unsafe_blk => word( s. s , ~"unsafe ") ,
@@ -868,7 +881,7 @@ fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
868881 }
869882 _ => ( )
870883 }
871- bclose_ ( s, blk. span , indented) ;
884+ bclose_maybe_open ( s, blk. span , indented, close_box ) ;
872885 s. ann . post ( ann_node) ;
873886}
874887
@@ -1060,9 +1073,9 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
10601073 let blk = if has_block {
10611074 let blk_arg = vec:: pop ( base_args) ;
10621075 match blk_arg. node {
1063- ast:: expr_loop_body( _) => word_nbsp ( s, ~"for "),
1064- ast::expr_do_body(_) => word_nbsp (s, ~" do") ,
1065- _ => ( )
1076+ ast:: expr_loop_body( _) => { head ( s, ~"for "); }
1077+ ast::expr_do_body(_) => { head (s, ~" do") ; }
1078+ _ => { }
10661079 }
10671080 some( blk_arg)
10681081 } else { none } ;
@@ -1074,7 +1087,19 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
10741087 }
10751088 if has_block {
10761089 nbsp ( s) ;
1077- print_expr ( s, option:: get ( blk) ) ;
1090+ match blk. get ( ) . node {
1091+ // need to handle closures specifically
1092+ ast:: expr_do_body( e) | ast:: expr_loop_body( e) => {
1093+ end ( s) ; // we close our head box; closure
1094+ // will create it's own.
1095+ print_expr ( s, e) ;
1096+ end ( s) ; // close outer box, as closures don't
1097+ }
1098+ _ => {
1099+ // not sure if this can happen.
1100+ print_expr ( s, blk. get ( ) ) ;
1101+ }
1102+ }
10781103 }
10791104 }
10801105 ast:: expr_binary ( op, lhs, rhs) => {
@@ -1174,12 +1199,31 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
11741199 print_block(s, body);
11751200 }
11761201 ast::expr_fn_block(decl, body, cap_clause) => {
1202+ // in do/for blocks we don't want to show an empty
1203+ // argument list, but at this point we don't know which
1204+ // we are inside.
1205+ //
1206+ // if !decl.inputs.is_empty() {
11771207 print_fn_block_args(s, decl, *cap_clause);
1178- // The parser always adds an extra implicit block around lambdas
1208+ space(s.s);
1209+ // }
11791210 assert body.node.stmts.is_empty();
11801211 assert body.node.expr.is_some();
1181- space(s.s);
1182- print_expr(s, body.node.expr.get());
1212+ // we extract the block, so as not to create another set of boxes
1213+ match body.node.expr.get().node {
1214+ ast::expr_block(blk) => {
1215+ print_block_unclosed(s, blk);
1216+ }
1217+ _ => {
1218+ // this is a bare expression
1219+ print_expr(s, body.node.expr.get());
1220+ end(s); // need to close a box
1221+ }
1222+ }
1223+ // a box will be closed by print_expr, but we didn't want an overall
1224+ // wrapper so we closed the corresponding opening. so create an
1225+ // empty box to satisfy the close.
1226+ ibox(s, 0);
11831227 }
11841228 ast::expr_loop_body(body) => {
11851229 print_expr(s, body);
0 commit comments