Skip to content

Commit 0070527

Browse files
committed
Pretty print vectors as ~[] instead of []/~. Closes #2863.
1 parent 14f19bd commit 0070527

File tree

7 files changed

+80
-52
lines changed

7 files changed

+80
-52
lines changed

src/libsyntax/print/pprust.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,20 @@ fn print_type_ex(s: ps, &&ty: @ast::ty, print_colons: bool) {
378378
word(s.s, constrs_str(cs, ty_constr_to_str));
379379
}
380380
ast::ty_vstore(t, v) {
381-
print_type(s, t);
382-
print_vstore(s, v);
381+
// If it is a vector, print it in prefix notation.
382+
// Someday it will all be like this.
383+
let is_fixed = alt v { ast::vstore_fixed(_) { true } _ { false } };
384+
alt t.node {
385+
ast::ty_vec(*) if !is_fixed {
386+
print_vstore(s, v);
387+
print_type(s, t);
388+
}
389+
_ {
390+
print_type(s, t);
391+
word(s.s, "/");
392+
print_vstore(s, v);
393+
}
394+
}
383395
}
384396
ast::ty_mac(_) {
385397
fail "print_type doesn't know how to print a ty_mac";
@@ -826,11 +838,11 @@ fn print_mac(s: ps, m: ast::mac) {
826838

827839
fn print_vstore(s: ps, t: ast::vstore) {
828840
alt t {
829-
ast::vstore_fixed(some(i)) { word(s.s, #fmt("/%u", i)); }
830-
ast::vstore_fixed(none) { word(s.s, "/_"); }
831-
ast::vstore_uniq { word(s.s, "/~"); }
832-
ast::vstore_box { word(s.s, "/@"); }
833-
ast::vstore_slice(r) { word(s.s, "/"); print_region(s, r); }
841+
ast::vstore_fixed(some(i)) { word(s.s, #fmt("%u", i)); }
842+
ast::vstore_fixed(none) { word(s.s, "_"); }
843+
ast::vstore_uniq { word(s.s, "~"); }
844+
ast::vstore_box { word(s.s, "@"); }
845+
ast::vstore_slice(r) { print_region(s, r); }
834846
}
835847
}
836848

@@ -841,8 +853,20 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
841853
s.ann.pre(ann_node);
842854
alt expr.node {
843855
ast::expr_vstore(e, v) {
844-
print_expr(s, e);
845-
print_vstore(s, v);
856+
// If it is a vector, print it in prefix notation.
857+
// Someday it will all be like this.
858+
let is_fixed = alt v { ast::vstore_fixed(_) { true } _ { false } };
859+
alt e.node {
860+
ast::expr_vec(*) if !is_fixed {
861+
print_vstore(s, v);
862+
print_expr(s, e);
863+
}
864+
_ {
865+
print_expr(s, e);
866+
word(s.s, "/");
867+
print_vstore(s, v);
868+
}
869+
}
846870
}
847871
ast::expr_vec(exprs, mutbl) {
848872
ibox(s, indent_unit);
@@ -1121,7 +1145,7 @@ fn print_expr_parens_if_not_bot(s: ps, ex: @ast::expr) {
11211145
ast::expr_assign_op(_, _, _) | ast::expr_swap(_, _) |
11221146
ast::expr_log(_, _, _) | ast::expr_assert(_) |
11231147
ast::expr_call(_, _, true) |
1124-
ast::expr_check(_, _) { true }
1148+
ast::expr_check(_, _) | ast::expr_vstore(_, _) { true }
11251149
_ { false }
11261150
};
11271151
if parens { popen(s); }

src/rustc/util/ppaux.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,12 @@ fn ty_to_str(cx: ctxt, typ: t) -> str {
223223
parameterized(cx, base, substs.self_r, substs.tps)
224224
}
225225
ty_evec(mt, vs) {
226-
#fmt["[%s]/%s", mt_to_str(cx, mt),
227-
vstore_to_str(cx, vs)]
226+
alt vs {
227+
ty::vstore_fixed(_) {
228+
#fmt["[%s]/%s", mt_to_str(cx, mt), vstore_to_str(cx, vs)]
229+
}
230+
_ { #fmt["%s[%s]", vstore_to_str(cx, vs), mt_to_str(cx, mt)] }
231+
}
228232
}
229233
ty_estr(vs) { #fmt["str/%s", vstore_to_str(cx, vs)] }
230234
ty_opaque_box { "@?" }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// error-pattern:expected `str` but found `[int]/~`
1+
// error-pattern:expected `str` but found `~[int]`
22
fn main() { fail ~[0i]; }

src/test/compile-fail/vec-field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:attempted access of field `some_field_name` on type `[int]/~`
1+
// error-pattern:attempted access of field `some_field_name` on type `~[int]`
22
// issue #367
33

44
fn f() {

src/test/pretty/vec-comments.pp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
// pp-exact:vec-comments.pp
44
fn main() {
55
let v1 =
6-
[
7-
// Comment
8-
0,
9-
// Comment
10-
1,
11-
// Comment
12-
2]/~;
6+
~[
7+
// Comment
8+
0,
9+
// Comment
10+
1,
11+
// Comment
12+
2];
1313
let v2 =
14-
[0, // Comment
15-
1, // Comment
16-
2]/~; // Comment
14+
~[0, // Comment
15+
1, // Comment
16+
2]; // Comment
1717
let v3 =
18-
[
19-
/* Comment */
20-
0,
21-
/* Comment */
22-
1,
23-
/* Comment */
24-
2]/~;
18+
~[
19+
/* Comment */
20+
0,
21+
/* Comment */
22+
1,
23+
/* Comment */
24+
2];
2525
let v4 =
26-
[0, /* Comment */
27-
1, /* Comment */
28-
2]/~; /* Comment */
26+
~[0, /* Comment */
27+
1, /* Comment */
28+
2]; /* Comment */
2929
}

src/test/pretty/vec-comments.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44
fn main() {
55
let v1 =
66
~[
7-
// Comment
8-
0,
9-
// Comment
10-
1,
11-
// Comment
12-
2];
7+
// Comment
8+
0,
9+
// Comment
10+
1,
11+
// Comment
12+
2];
1313
let v2 =
1414
~[0, // Comment
15-
1, // Comment
16-
2]; // Comment
15+
1, // Comment
16+
2]; // Comment
1717
let v3 =
1818
~[
19-
/* Comment */
20-
0,
21-
/* Comment */
22-
1,
23-
/* Comment */
24-
2];
19+
/* Comment */
20+
0,
21+
/* Comment */
22+
1,
23+
/* Comment */
24+
2];
2525
let v4 =
2626
~[0, /* Comment */
27-
1, /* Comment */
28-
2]; /* Comment */
27+
1, /* Comment */
28+
2]; /* Comment */
2929
}

src/test/pretty/vec-type.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// pp-exact:vec-type.pp
22

3-
fn f1(x: [int]/~) { }
3+
fn f1(x: ~[int]) { }
44

5-
fn g1() { f1([1, 2, 3]/~); }
5+
fn g1() { f1(~[1, 2, 3]); }

0 commit comments

Comments
 (0)