1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ast: fix fn_type_signature for anon functions with alias arguments (#13024)

This commit is contained in:
yuyi 2022-01-04 22:02:53 +08:00 committed by GitHub
parent 054bb272df
commit b94c5c2a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 3 deletions

View File

@ -200,8 +200,13 @@ pub fn (t &Table) fn_type_signature(f &Fn) string {
// TODO: for now ignore mut/pts in sig for now // TODO: for now ignore mut/pts in sig for now
typ := arg.typ.set_nr_muls(0) typ := arg.typ.set_nr_muls(0)
arg_type_sym := t.sym(typ) arg_type_sym := t.sym(typ)
sig += arg_type_sym.str().to_lower().replace_each(['.', '__', '&', '', '[', 'arr_', 'chan ', if arg_type_sym.kind == .alias {
'chan_', 'map[', 'map_of_', ']', '_to_', '<', '_T_', ',', '_', ' ', '', '>', '']) sig += arg_type_sym.cname
} else {
sig += arg_type_sym.str().to_lower().replace_each(['.', '__', '&', '', '[', 'arr_',
'chan ', 'chan_', 'map[', 'map_of_', ']', '_to_', '<', '_T_', ',', '_', ' ', '',
'>', ''])
}
if i < f.params.len - 1 { if i < f.params.len - 1 {
sig += '_' sig += '_'
} }
@ -209,7 +214,11 @@ pub fn (t &Table) fn_type_signature(f &Fn) string {
if f.return_type != 0 && f.return_type != void_type { if f.return_type != 0 && f.return_type != void_type {
sym := t.sym(f.return_type) sym := t.sym(f.return_type)
opt := if f.return_type.has_flag(.optional) { 'option_' } else { '' } opt := if f.return_type.has_flag(.optional) { 'option_' } else { '' }
sig += '__$opt$sym.kind' if sym.kind == .alias {
sig += '__$opt$sym.cname'
} else {
sig += '__$opt$sym.kind'
}
} }
return sig return sig
} }

View File

@ -0,0 +1,36 @@
type MyString = string
type MyInt = int
struct S1 {
x fn (a int) MyString
}
struct S2 {
y fn (a int) MyInt
}
fn get_string(a int) MyString {
return '$a'
}
fn get_int(a int) MyInt {
return a
}
fn test_anon_fn_with_alias_args() {
s1 := S1{
x: get_string
}
println(s1.x)
ret1 := s1.x(22)
println(ret1)
assert ret1 == '22'
s2 := S2{
y: get_int
}
println(s2.y)
ret2 := s2.y(22)
println(ret2)
assert ret2 == 22
}