Skip to content

Commit f34d19a

Browse files
committed
Make pp more conservative about inserting trailing comments mid-list.
1 parent 69325f8 commit f34d19a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/comp/pretty/pprust.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op,
204204
i += 1u;
205205
if (i < len) {
206206
word(s.s, ",");
207-
maybe_print_trailing_comment(s, get_span(elt));
207+
maybe_print_trailing_comment(s, get_span(elt),
208+
some(get_span(elts.(i)).hi));
208209
space(s.s);
209210
}
210211
}
@@ -404,7 +405,7 @@ fn print_item(&ps s, &@ast::item item) {
404405
pclose(s);
405406
}
406407
word(s.s, ";");
407-
maybe_print_trailing_comment(s, v.span);
408+
maybe_print_trailing_comment(s, v.span, none[uint]);
408409
}
409410
bclose(s, item.span);
410411
}
@@ -469,13 +470,12 @@ fn print_stmt(&ps s, &ast::stmt st) {
469470
}
470471
}
471472
if (front::parser::stmt_ends_with_semi(st)) {word(s.s, ";");}
472-
maybe_print_trailing_comment(s, st.span);
473+
maybe_print_trailing_comment(s, st.span, none[uint]);
473474
}
474475

475476
fn print_block(&ps s, ast::block blk) {
476477
maybe_print_comment(s, blk.span.lo);
477478
bopen(s);
478-
auto first = true;
479479
for (@ast::stmt st in blk.node.stmts) {
480480
print_stmt(s, *st)
481481

@@ -484,7 +484,8 @@ fn print_block(&ps s, ast::block blk) {
484484
case (some(?expr)) {
485485
space(s.s);
486486
print_expr(s, expr);
487-
maybe_print_trailing_comment(s, expr.span);
487+
maybe_print_trailing_comment(s, expr.span,
488+
some(blk.span.hi));
488489
}
489490
case (_) {}
490491
}
@@ -1264,7 +1265,8 @@ fn maybe_print_comment(&ps s, uint pos) {
12641265
}
12651266
}
12661267

1267-
fn maybe_print_trailing_comment(&ps s, common::span span) {
1268+
fn maybe_print_trailing_comment(&ps s, common::span span,
1269+
option::t[uint] next_pos) {
12681270
auto cm;
12691271
alt (s.cm) {
12701272
case (some(?ccm)) {
@@ -1278,9 +1280,14 @@ fn maybe_print_trailing_comment(&ps s, common::span span) {
12781280

12791281
auto span_line = codemap::lookup_pos(cm, span.hi);
12801282
auto comment_line = codemap::lookup_pos(cm, cmnt.pos);
1283+
auto next = cmnt.pos + 1u;
1284+
alt (next_pos) {
1285+
case (none) { }
1286+
case (some(?p)) { next = p; }
1287+
}
12811288
if (span.hi < cmnt.pos &&
1289+
cmnt.pos < next &&
12821290
span_line.line == comment_line.line) {
1283-
word(s.s, " ");
12841291
print_comment(s, cmnt);
12851292
s.cur_cmnt += 1u;
12861293
}
@@ -1327,6 +1334,7 @@ fn print_comment(&ps s, lexer::cmnt cmnt) {
13271334
}
13281335

13291336
case (lexer::trailing) {
1337+
word(s.s, " ");
13301338
if (vec::len(cmnt.lines) == 1u) {
13311339
word(s.s, cmnt.lines.(0));
13321340
hardbreak(s.s);

0 commit comments

Comments
 (0)