From 338f3afd3173b6330b788575248e6daa2debc25e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 9 Dec 2020 15:18:38 +0200 Subject: [PATCH] vfmt: fix `type MyFn = fn (int) int mut arr := []MyFn{}` --- vlib/v/fmt/tests/fntype_alias_array_keep.vv | 4 +++ vlib/v/table/types.v | 30 +++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 vlib/v/fmt/tests/fntype_alias_array_keep.vv diff --git a/vlib/v/fmt/tests/fntype_alias_array_keep.vv b/vlib/v/fmt/tests/fntype_alias_array_keep.vv new file mode 100644 index 0000000000..080fe9acaf --- /dev/null +++ b/vlib/v/fmt/tests/fntype_alias_array_keep.vv @@ -0,0 +1,4 @@ +type MyFn = fn (int) int + +mut arr := []MyFn{} +arr << MyFn(test) diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index c6e0263872..6e3116c223 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -885,6 +885,8 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin if !table.is_fmt { info := sym.info as FnType res = table.fn_signature(info.func, type_only: true) + } else { + res = table.shorten_user_defined_typenames(res, import_aliases) } } .map { @@ -914,17 +916,7 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin return 'void' } else { - // types defined by the user - // mod.submod.submod2.Type => submod2.Type - parts := res.split('.') - res = if parts.len > 1 { parts[parts.len - 2..].join('.') } else { parts[0] } - // cur_mod.Type => Type - if res.starts_with(table.cmod_prefix) { - res = res.replace_once(table.cmod_prefix, '') - } - if res in import_aliases { - res = import_aliases[res] - } + res = table.shorten_user_defined_typenames(res, import_aliases) } } nr_muls := t.nr_muls() @@ -937,6 +929,22 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin return res } +fn (t Table) shorten_user_defined_typenames(originalname string, import_aliases map[string]string) string { + mut res := originalname + // types defined by the user + // mod.submod.submod2.Type => submod2.Type + parts := res.split('.') + res = if parts.len > 1 { parts[parts.len - 2..].join('.') } else { parts[0] } + // cur_mod.Type => Type + if res.starts_with(t.cmod_prefix) { + res = res.replace_once(t.cmod_prefix, '') + } + if res in import_aliases { + res = import_aliases[res] + } + return res +} + pub struct FnSignatureOpts { skip_receiver bool type_only bool