fmt: keep names of parameters in anonymus function (#10173)

pull/10179/head
Lukas Neubert 2021-05-23 04:54:39 +02:00 committed by GitHub
parent efa07cbcbf
commit 79b97fa361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 21 deletions

View File

@ -31,7 +31,6 @@ const (
// TODOs and unfixed vfmt bugs
'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */,
'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */,
'vlib/v/tests/fn_high_test.v', /* param name removed */
'vlib/v/tests/interop_test.v', /* bad comment formatting */
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */

View File

@ -184,8 +184,12 @@ pub fn (t &Table) fn_type_source_signature(f &Fn) string {
if arg.is_mut {
sig += 'mut '
}
// NB: arg name is only added for fmt, else it would causes errors with generics
if t.is_fmt && arg.name.len > 0 {
sig += '$arg.name '
}
arg_type_sym := t.get_type_symbol(arg.typ)
sig += '$arg_type_sym.name'
sig += arg_type_sym.name
if i < f.params.len - 1 {
sig += ', '
}
@ -854,7 +858,6 @@ pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, ha
util.no_dots(f.name.clone())
}
anon := f.name.len == 0 || is_anon
// existing
existing_idx := t.type_idxs[name]
if existing_idx > 0 && t.type_symbols[existing_idx].kind != .placeholder {
return existing_idx

View File

@ -909,7 +909,10 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]
} else {
if res.starts_with('fn (') {
// fn foo ()
res = t.fn_signature_using_aliases(info.func, import_aliases, type_only: true)
has_names := info.func.params.any(it.name.len > 0)
res = t.fn_signature_using_aliases(info.func, import_aliases,
type_only: !has_names
)
} else {
// FnFoo
res = t.shorten_user_defined_typenames(res, import_aliases)

View File

@ -1,11 +1,20 @@
struct Row {
id int
import v.ast
struct Data {
a fn (stmt ast.Stmt, vp voidptr) bool
}
pub fn (a []Row) reduce(iter fn (int, int) int, accum_start int) int {
pub fn (a []Data) reduce(iter fn (int, int) int, accum_start int) int {
iter(accum_start)
}
pub fn test_anon_fn_void(func fn ()) int {
return 0
}
fn C.HasAnonFnWithNamedParams(cb fn (c C.bar, d voidptr))
// NB: the signature of both anonymus functions should only differs in the param name
fn anon_fn_param_has_no_name(f fn (int) string) {}
fn anon_fn_with_named_param(func fn (a int) string) {}

View File

@ -1,6 +0,0 @@
import v.ast
struct Data {
a fn (string, voidptr) bool
b fn (ast.Stmt, voidptr) bool
}

View File

@ -1,6 +0,0 @@
import v.ast
struct Data {
a fn (s string, f voidptr) bool
b fn (stmt ast.Stmt, f voidptr) bool
}

View File

@ -12,7 +12,7 @@ fn high_fn_no_ret(f fn (int)) {
f(111)
}
fn high_fn_array(f fn(a []int) []int) {
fn high_fn_array(f fn (a []int) []int) {
}
fn high_fn_multi_return(a int, b fn (c []int, d []string) ([]int, []string)) {
@ -61,7 +61,7 @@ fn test_high_fn_ret_anons() {
assert top_lvl_sqr(param) == param * param
}
fn high_fn_applier(arg int, func fn(a int)string) string {
fn high_fn_applier(arg int, func fn (a int) string) string {
return func(arg)
}