Skip to content

Commit

Permalink
Pretty print vectors as ~[] instead of []/~. Closes #2863.
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Jul 10, 2012
1 parent 14f19bd commit 0070527
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 52 deletions.
44 changes: 34 additions & 10 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,20 @@ fn print_type_ex(s: ps, &&ty: @ast::ty, print_colons: bool) {
word(s.s, constrs_str(cs, ty_constr_to_str));
}
ast::ty_vstore(t, v) {
print_type(s, t);
print_vstore(s, v);
// If it is a vector, print it in prefix notation.
// Someday it will all be like this.
let is_fixed = alt v { ast::vstore_fixed(_) { true } _ { false } };
alt t.node {
ast::ty_vec(*) if !is_fixed {
print_vstore(s, v);
print_type(s, t);
}
_ {
print_type(s, t);
word(s.s, "/");
print_vstore(s, v);
}
}
}
ast::ty_mac(_) {
fail "print_type doesn't know how to print a ty_mac";
Expand Down Expand Up @@ -826,11 +838,11 @@ fn print_mac(s: ps, m: ast::mac) {

fn print_vstore(s: ps, t: ast::vstore) {
alt t {
ast::vstore_fixed(some(i)) { word(s.s, #fmt("/%u", i)); }
ast::vstore_fixed(none) { word(s.s, "/_"); }
ast::vstore_uniq { word(s.s, "/~"); }
ast::vstore_box { word(s.s, "/@"); }
ast::vstore_slice(r) { word(s.s, "/"); print_region(s, r); }
ast::vstore_fixed(some(i)) { word(s.s, #fmt("%u", i)); }
ast::vstore_fixed(none) { word(s.s, "_"); }
ast::vstore_uniq { word(s.s, "~"); }
ast::vstore_box { word(s.s, "@"); }
ast::vstore_slice(r) { print_region(s, r); }
}
}

Expand All @@ -841,8 +853,20 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
s.ann.pre(ann_node);
alt expr.node {
ast::expr_vstore(e, v) {
print_expr(s, e);
print_vstore(s, v);
// If it is a vector, print it in prefix notation.
// Someday it will all be like this.
let is_fixed = alt v { ast::vstore_fixed(_) { true } _ { false } };
alt e.node {
ast::expr_vec(*) if !is_fixed {
print_vstore(s, v);
print_expr(s, e);
}
_ {
print_expr(s, e);
word(s.s, "/");
print_vstore(s, v);
}
}
}
ast::expr_vec(exprs, mutbl) {
ibox(s, indent_unit);
Expand Down Expand Up @@ -1121,7 +1145,7 @@ fn print_expr_parens_if_not_bot(s: ps, ex: @ast::expr) {
ast::expr_assign_op(_, _, _) | ast::expr_swap(_, _) |
ast::expr_log(_, _, _) | ast::expr_assert(_) |
ast::expr_call(_, _, true) |
ast::expr_check(_, _) { true }
ast::expr_check(_, _) | ast::expr_vstore(_, _) { true }
_ { false }
};
if parens { popen(s); }
Expand Down
8 changes: 6 additions & 2 deletions src/rustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ fn ty_to_str(cx: ctxt, typ: t) -> str {
parameterized(cx, base, substs.self_r, substs.tps)
}
ty_evec(mt, vs) {
#fmt["[%s]/%s", mt_to_str(cx, mt),
vstore_to_str(cx, vs)]
alt vs {
ty::vstore_fixed(_) {
#fmt["[%s]/%s", mt_to_str(cx, mt), vstore_to_str(cx, vs)]
}
_ { #fmt["%s[%s]", vstore_to_str(cx, vs), mt_to_str(cx, mt)] }
}
}
ty_estr(vs) { #fmt["str/%s", vstore_to_str(cx, vs)] }
ty_opaque_box { "@?" }
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/fail-type-err.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// error-pattern:expected `str` but found `[int]/~`
// error-pattern:expected `str` but found `~[int]`
fn main() { fail ~[0i]; }
2 changes: 1 addition & 1 deletion src/test/compile-fail/vec-field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// error-pattern:attempted access of field `some_field_name` on type `[int]/~`
// error-pattern:attempted access of field `some_field_name` on type `~[int]`
// issue #367

fn f() {
Expand Down
40 changes: 20 additions & 20 deletions src/test/pretty/vec-comments.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
// pp-exact:vec-comments.pp
fn main() {
let v1 =
[
// Comment
0,
// Comment
1,
// Comment
2]/~;
~[
// Comment
0,
// Comment
1,
// Comment
2];
let v2 =
[0, // Comment
1, // Comment
2]/~; // Comment
~[0, // Comment
1, // Comment
2]; // Comment
let v3 =
[
/* Comment */
0,
/* Comment */
1,
/* Comment */
2]/~;
~[
/* Comment */
0,
/* Comment */
1,
/* Comment */
2];
let v4 =
[0, /* Comment */
1, /* Comment */
2]/~; /* Comment */
~[0, /* Comment */
1, /* Comment */
2]; /* Comment */
}
32 changes: 16 additions & 16 deletions src/test/pretty/vec-comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
fn main() {
let v1 =
~[
// Comment
0,
// Comment
1,
// Comment
2];
// Comment
0,
// Comment
1,
// Comment
2];
let v2 =
~[0, // Comment
1, // Comment
2]; // Comment
1, // Comment
2]; // Comment
let v3 =
~[
/* Comment */
0,
/* Comment */
1,
/* Comment */
2];
/* Comment */
0,
/* Comment */
1,
/* Comment */
2];
let v4 =
~[0, /* Comment */
1, /* Comment */
2]; /* Comment */
1, /* Comment */
2]; /* Comment */
}
4 changes: 2 additions & 2 deletions src/test/pretty/vec-type.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// pp-exact:vec-type.pp

fn f1(x: [int]/~) { }
fn f1(x: ~[int]) { }

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

0 comments on commit 0070527

Please sign in to comment.