vfmt: fix `type MyFn = fn (int) int mut arr := []MyFn{}`

pull/7218/head
Delyan Angelov 2020-12-09 15:18:38 +02:00
parent 3b6b5b8090
commit 338f3afd31
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 23 additions and 11 deletions

View File

@ -0,0 +1,4 @@
type MyFn = fn (int) int
mut arr := []MyFn{}
arr << MyFn(test)

View File

@ -885,6 +885,8 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin
if !table.is_fmt { if !table.is_fmt {
info := sym.info as FnType info := sym.info as FnType
res = table.fn_signature(info.func, type_only: true) res = table.fn_signature(info.func, type_only: true)
} else {
res = table.shorten_user_defined_typenames(res, import_aliases)
} }
} }
.map { .map {
@ -914,17 +916,7 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin
return 'void' return 'void'
} }
else { else {
// types defined by the user res = table.shorten_user_defined_typenames(res, import_aliases)
// 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]
}
} }
} }
nr_muls := t.nr_muls() 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 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 { pub struct FnSignatureOpts {
skip_receiver bool skip_receiver bool
type_only bool type_only bool