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:
parent
054bb272df
commit
b94c5c2a9c
@ -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,8 +214,12 @@ 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 { '' }
|
||||||
|
if sym.kind == .alias {
|
||||||
|
sig += '__$opt$sym.cname'
|
||||||
|
} else {
|
||||||
sig += '__$opt$sym.kind'
|
sig += '__$opt$sym.kind'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return sig
|
return sig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
36
vlib/v/tests/anon_fn_with_alias_args_test.v
Normal file
36
vlib/v/tests/anon_fn_with_alias_args_test.v
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user