From fd88bfbac81bc5ce2586143c04fa07a730e95b44 Mon Sep 17 00:00:00 2001 From: Enzo Date: Sun, 4 Oct 2020 06:32:47 +0200 Subject: [PATCH] fmt: fix formatting of anon args (#6545) --- vlib/os/os_nix.c.v | 4 +-- vlib/pg/pg.v | 18 ++++++------- vlib/v/ast/str.v | 13 ++++++---- vlib/v/fmt/tests/fn_with_anon_params_keep.vv | 1 + vlib/v/gen/fn.v | 6 ----- vlib/v/gen/js/js.v | 1 - vlib/v/parser/fn.v | 27 ++++++++++---------- 7 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 vlib/v/fmt/tests/fn_with_anon_params_keep.vv diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 5f38fc934b..0df1e574af 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -28,7 +28,7 @@ mut: fn C.uname(name voidptr) int -fn C.symlink(arg_1, arg_2 charptr) int +fn C.symlink(charptr, charptr) int pub fn uname() Uname { mut u := Uname{} @@ -124,7 +124,7 @@ pub fn mkdir(path string) ?bool { r := unsafe { C.mkdir(charptr(apath.str), 511) } - + if r == -1 { return error(posix_get_error_msg(C.errno)) } diff --git a/vlib/pg/pg.v b/vlib/pg/pg.v index f70b05540e..5131ecfd3d 100644 --- a/vlib/pg/pg.v +++ b/vlib/pg/pg.v @@ -30,23 +30,23 @@ pub: fn C.PQconnectdb(a byteptr) &C.PGconn -fn C.PQerrorMessage(arg_1 voidptr) byteptr +fn C.PQerrorMessage(voidptr) byteptr -fn C.PQgetvalue(arg_1 voidptr, arg_2, arg_3 int) byteptr +fn C.PQgetvalue(voidptr, int, int) byteptr -fn C.PQstatus(arg_1 voidptr) int +fn C.PQstatus(voidptr) int -fn C.PQntuples(arg_1 voidptr) int +fn C.PQntuples(voidptr) int -fn C.PQnfields(arg_1 voidptr) int +fn C.PQnfields(voidptr) int -fn C.PQexec(arg_1 voidptr) voidptr +fn C.PQexec(voidptr) voidptr -fn C.PQexecParams(arg_1 voidptr) voidptr +fn C.PQexecParams(voidptr) voidptr -fn C.PQclear(arg_1 voidptr) voidptr +fn C.PQclear(voidptr) voidptr -fn C.PQfinish(arg_1 voidptr) +fn C.PQfinish(voidptr) // connect makes a new connection to the database server using // the parameters from the `Config` structure, returning diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 91e2735719..0d6131a961 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -64,7 +64,8 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { continue } is_last_arg := i == node.params.len - 1 - should_add_type := is_last_arg || node.params[i + 1].typ != arg.typ || + is_type_only := arg.name == '' + should_add_type := is_last_arg || is_type_only || node.params[i + 1].typ != arg.typ || (node.is_variadic && i == node.params.len - 2) if arg.is_mut { f.write(arg.typ.share().str() + ' ') @@ -79,11 +80,13 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { } s = util.no_cur_mod(s, cur_mod) if should_add_type { - if node.is_variadic && is_last_arg { - f.write(' ...' + s) - } else { - f.write(' ' + s) + if !is_type_only { + f.write(' ') } + if node.is_variadic && is_last_arg { + f.write('...') + } + f.write(s) } if !is_last_arg { f.write(', ') diff --git a/vlib/v/fmt/tests/fn_with_anon_params_keep.vv b/vlib/v/fmt/tests/fn_with_anon_params_keep.vv new file mode 100644 index 0000000000..d5dab9ce08 --- /dev/null +++ b/vlib/v/fmt/tests/fn_with_anon_params_keep.vv @@ -0,0 +1 @@ +fn C.PQgetvalue(voidptr, int, int) byteptr diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index bb5b5a17ef..f443cb453b 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -208,7 +208,6 @@ fn (mut g Gen) write_defer_stmts_when_needed() { fn (mut g Gen) fn_args(args []table.Param, is_variadic bool) ([]string, []string) { mut fargs := []string{} mut fargtypes := []string{} - no_names := args.len > 0 && args[0].name == 'arg_1' for i, arg in args { caname := c_name(arg.name) typ := g.unwrap_generic(arg.typ) @@ -245,11 +244,6 @@ fn (mut g Gen) fn_args(args []table.Param, is_variadic bool) ([]string, []string g.write(')') g.definitions.write(')') } - } else if no_names { - g.write(arg_type_name) - g.definitions.write(arg_type_name) - fargs << '' - fargtypes << arg_type_name } else { mut nr_muls := arg.typ.nr_muls() s := arg_type_name + ' ' + caname diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 3433096889..a15e56e966 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -900,7 +900,6 @@ fn (mut g JsGen) gen_method_decl(it ast.FnDecl) { } fn (mut g JsGen) fn_args(args []table.Param, is_variadic bool) { - // no_names := args.len > 0 && args[0].name == 'arg_1' for i, arg in args { name := g.js_name(arg.name) is_varg := i == args.len - 1 && is_variadic diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 4a971f1f48..0785e976fd 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -253,18 +253,20 @@ fn (mut p Parser) fn_decl() ast.FnDecl { // Args args2, are_args_type_only, is_variadic := p.fn_args() params << args2 - for param in params { - if p.scope.known_var(param.name) { - p.error_with_pos('redefinition of parameter `$param.name`', param.pos) + if !are_args_type_only { + for param in params { + if p.scope.known_var(param.name) { + p.error_with_pos('redefinition of parameter `$param.name`', param.pos) + } + p.scope.register(param.name, ast.Var{ + name: param.name + typ: param.typ + is_mut: param.is_mut + pos: param.pos + is_used: true + is_arg: true + }) } - p.scope.register(param.name, ast.Var{ - name: param.name - typ: param.typ - is_mut: param.is_mut - pos: param.pos - is_used: true - is_arg: true - }) } mut end_pos := p.prev_tok.position() // Return type @@ -430,7 +432,6 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { // p.warn('types only') mut arg_no := 1 for p.tok.kind != .rpar { - arg_name := 'arg_$arg_no' is_shared := p.tok.kind == .key_shared is_atomic := p.tok.kind == .key_atomic is_mut := p.tok.kind == .key_mut || is_shared || is_atomic @@ -480,7 +481,7 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { sym := p.table.get_type_symbol(arg_type) args << table.Param{ pos: pos - name: arg_name + name: '' is_mut: is_mut typ: arg_type type_source_name: sym.source_name