From fd75cce0f3eb71f5edf216042688385fdc300da9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 7 Apr 2020 04:05:59 +0200 Subject: [PATCH] vfmt: minor fixes + tests --- cmd/tools/vtest-fixed.v | 2 +- vlib/v/ast/str.v | 29 ++++++++++++------------ vlib/v/fmt/tests/integer_literal_keep.vv | 5 ---- vlib/v/fmt/tests/struct_keep.vv | 22 ++++++++++++++++++ vlib/v/table/atypes.v | 2 +- 5 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 vlib/v/fmt/tests/struct_keep.vv diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index 336ad1a90b..18e43d104b 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -78,7 +78,7 @@ const ( 'vlib/time/time_test.v', 'vlib/time/misc/misc_test.v', 'vlib/v/doc/doc_test.v', - // 'vlib/v/fmt/fmt_keep_test.v', + 'vlib/v/fmt/fmt_keep_test.v', 'vlib/v/fmt/fmt_test.v', 'vlib/v/gen/cgen_test.v', // 'vlib/v/parser/parser_test.v', diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index dbf5fb3f58..e0cc9322e6 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -2,11 +2,8 @@ // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. module ast -/* -These methods are used only by vfmt, vdoc, and for debugging. -*/ - +// These methods are used only by vfmt, vdoc, and for debugging. import ( v.table strings @@ -21,7 +18,10 @@ pub fn (node &FnDecl) str(t &table.Table) string { if node.is_method { sym := t.get_type_symbol(node.receiver.typ) name := sym.name.after('.') - m := if node.rec_mut { 'mut ' } else { '' } + mut m := if node.rec_mut { 'mut ' } else { '' } + if !node.rec_mut && table.type_is_ptr(node.receiver.typ) { + m = '&' + } receiver = '($node.receiver.name $m$name) ' } name := node.name.after('.') @@ -35,14 +35,13 @@ pub fn (node &FnDecl) str(t &table.Table) string { continue } is_last_arg := i == node.args.len - 1 - should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || - (node.is_variadic && i == node.args.len - 2) + should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i == + node.args.len - 2) f.write(arg.name) if should_add_type { if node.is_variadic && is_last_arg { f.write(' ...' + t.type_to_str(arg.typ)) - } - else { + } else { f.write(' ' + t.type_to_str(arg.typ)) } } @@ -82,7 +81,7 @@ pub fn (x Expr) str() string { res << "'" for i, val in it.vals { res << val - if i>=it.exprs.len { + if i >= it.exprs.len { continue } res << '$' @@ -91,7 +90,7 @@ pub fn (x Expr) str() string { res << it.exprs[i].str() res << it.expr_fmts[i] res << '}' - }else{ + } else { res << it.exprs[i].str() } } @@ -117,20 +116,20 @@ pub fn (node Stmt) str() string { match node { AssignStmt { mut out := '' - for i,ident in it.left { + for i, ident in it.left { var_info := ident.var_info() if var_info.is_mut { out += 'mut ' } out += ident.name - if i < it.left.len-1 { + if i < it.left.len - 1 { out += ',' } } out += ' $it.op.str() ' - for i,val in it.right { + for i, val in it.right { out += val.str() - if i < it.right.len-1 { + if i < it.right.len - 1 { out += ',' } } diff --git a/vlib/v/fmt/tests/integer_literal_keep.vv b/vlib/v/fmt/tests/integer_literal_keep.vv index 81ffaa0d09..4a77e68bdf 100644 --- a/vlib/v/fmt/tests/integer_literal_keep.vv +++ b/vlib/v/fmt/tests/integer_literal_keep.vv @@ -1,8 +1,3 @@ -import os - -fn foo(a []os.File) { -} - struct IfExpr { } diff --git a/vlib/v/fmt/tests/struct_keep.vv b/vlib/v/fmt/tests/struct_keep.vv new file mode 100644 index 0000000000..45409ad3eb --- /dev/null +++ b/vlib/v/fmt/tests/struct_keep.vv @@ -0,0 +1,22 @@ +import os + +fn foo(a []os.File) { +} + +struct User { + age int + name string +} + +fn handle_users(users []User) { + println(users.len) +} + +fn (u &User) foo(u2 &User) { +} + +type Expr = IfExpr | IntegerLiteral + +fn exprs(e []Expr) { + println(e.len) +} diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 60a3f95ebd..2e09c95ec2 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -613,7 +613,7 @@ pub fn (table &Table) type_to_str(t Type) string { if vals.len > 2 { res = vals[vals.len - 2] + '.' + vals[vals.len - 1] } - if sym.kind == .array { + if sym.kind == .array && !res.starts_with('[]') { res = '[]' + res } }