Skip to content

Commit 96f1eda

Browse files
committed
Be more careful about pretty-printing literals
Before, literal printing would basically get derailed completely when a literal was encountered that did not end up being printed. This caused the strangeness seen in #1532. Also cleans up pretty-printing of discriminants a little. Closes #1510 Closes #1532
1 parent 2d36a71 commit 96f1eda

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/comp/syntax/print/pprust.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ fn print_item(s: ps, &&item: @ast::item) {
419419
for v: ast::variant in variants {
420420
space_if_not_bol(s);
421421
maybe_print_comment(s, v.span.lo);
422+
ibox(s, indent_unit);
422423
word(s.s, v.node.name);
423424
if vec::len(v.node.args) > 0u {
424425
popen(s);
@@ -429,14 +430,15 @@ fn print_item(s: ps, &&item: @ast::item) {
429430
pclose(s);
430431
}
431432
alt v.node.disr_expr {
432-
some(expr) {
433-
nbsp(s);
434-
word_nbsp(s, "=");
435-
print_expr(s, expr);
433+
some(d) {
434+
space(s.s);
435+
word_space(s, "=");
436+
print_expr(s, d);
436437
}
437438
_ {}
438439
}
439440
word(s.s, ";");
441+
end(s);
440442
maybe_print_trailing_comment(s, v.span, none::<uint>);
441443
}
442444
bclose(s, item.span);
@@ -1414,11 +1416,12 @@ fn in_cbox(s: ps) -> bool {
14141416

14151417
fn print_literal(s: ps, &&lit: @ast::lit) {
14161418
maybe_print_comment(s, lit.span.lo);
1417-
alt next_lit(s) {
1419+
alt next_lit(s, lit.span.lo) {
14181420
some(lt) {
1419-
if lt.pos == lit.span.lo { word(s.s, lt.lit); s.cur_lit += 1u; ret; }
1421+
word(s.s, lt.lit);
1422+
ret;
14201423
}
1421-
_ { }
1424+
_ {}
14221425
}
14231426
alt lit.node {
14241427
ast::lit_str(st) { print_string(s, st); }
@@ -1443,14 +1446,18 @@ fn print_literal(s: ps, &&lit: @ast::lit) {
14431446

14441447
fn lit_to_str(l: @ast::lit) -> str { be to_str(l, print_literal); }
14451448

1446-
fn next_lit(s: ps) -> option::t<lexer::lit> {
1449+
fn next_lit(s: ps, pos: uint) -> option::t<lexer::lit> {
14471450
alt s.literals {
14481451
some(lits) {
1449-
if s.cur_lit < vec::len(lits) {
1450-
ret some(lits[s.cur_lit]);
1451-
} else { ret none::<lexer::lit>; }
1452+
while s.cur_lit < vec::len(lits) {
1453+
let lt = lits[s.cur_lit];
1454+
if lt.pos > pos { ret none; }
1455+
s.cur_lit += 1u;
1456+
if lt.pos == pos { ret some(lt); }
1457+
}
1458+
ret none;
14521459
}
1453-
_ { ret none::<lexer::lit>; }
1460+
_ { ret none; }
14541461
}
14551462
}
14561463

0 commit comments

Comments
 (0)