mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix fn_var_signature() when param type is function (#15436)
This commit is contained in:
parent
dc37386bcc
commit
b08f500c60
@ -155,12 +155,8 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
|
||||
}
|
||||
// if it's a decl assign (`:=`) or a blank assignment `_ =`/`_ :=` then generate `void (*ident) (args) =`
|
||||
if (is_decl || blank_assign) && left is ast.Ident {
|
||||
ret_styp := g.typ(val.decl.return_type)
|
||||
g.write('$ret_styp (*$ident.name) (')
|
||||
def_pos := g.definitions.len
|
||||
g.fn_decl_params(val.decl.params, unsafe { nil }, false)
|
||||
g.definitions.go_back(g.definitions.len - def_pos)
|
||||
g.write(') = ')
|
||||
sig := g.fn_var_signature(val.decl.return_type, val.decl.params, ident.name)
|
||||
g.write(sig + ' = ')
|
||||
} else {
|
||||
g.is_assign_lhs = true
|
||||
g.assign_op = node.op
|
||||
|
@ -343,8 +343,8 @@ fn (mut g Gen) gen_map_equality_fn(left_type ast.Type) string {
|
||||
fn_builder.writeln('\t\tif (!map_exists(&b, k)) return false;')
|
||||
kind := g.table.type_kind(value.typ)
|
||||
if kind == .function {
|
||||
func := value.sym.info as ast.FnType
|
||||
sig := g.fn_var_signature(func, 'v')
|
||||
info := value.sym.info as ast.FnType
|
||||
sig := g.fn_var_signature(info.func.return_type, info.func.params, 'v')
|
||||
fn_builder.writeln('\t\t$sig = *(voidptr*)map_get(&a, k, &(voidptr[]){ 0 });')
|
||||
} else {
|
||||
fn_builder.writeln('\t\t$ptr_value_styp v = *($ptr_value_styp*)map_get(&a, k, &($ptr_value_styp[]){ 0 });')
|
||||
|
@ -475,7 +475,8 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) {
|
||||
for var in node.inherited_vars {
|
||||
var_sym := g.table.sym(var.typ)
|
||||
if var_sym.info is ast.FnType {
|
||||
sig := g.fn_var_signature(var_sym.info, var.name)
|
||||
sig := g.fn_var_signature(var_sym.info.func.return_type, var_sym.info.func.params,
|
||||
var.name)
|
||||
builder.writeln('\t' + sig + ';')
|
||||
} else {
|
||||
styp := g.typ(var.typ)
|
||||
|
@ -63,13 +63,20 @@ fn (mut g Gen) unwrap(typ ast.Type) Type {
|
||||
}
|
||||
|
||||
// generate function variable definition, e.g. `void (*var_name) (int, string)`
|
||||
fn (mut g Gen) fn_var_signature(info ast.FnType, var_name string) string {
|
||||
ret_styp := g.typ(info.func.return_type)
|
||||
fn (mut g Gen) fn_var_signature(return_type ast.Type, params []ast.Param, var_name string) string {
|
||||
ret_styp := g.typ(return_type)
|
||||
mut sig := '$ret_styp (*$var_name) ('
|
||||
for j, arg in info.func.params {
|
||||
for j, arg in params {
|
||||
arg_sym := g.table.sym(arg.typ)
|
||||
if arg_sym.info is ast.FnType {
|
||||
arg_sig := g.fn_var_signature(arg_sym.info.func.return_type, arg_sym.info.func.params,
|
||||
'')
|
||||
sig += arg_sig
|
||||
} else {
|
||||
arg_styp := g.typ(arg.typ)
|
||||
sig += '$arg_styp $arg.name'
|
||||
if j < info.func.params.len - 1 {
|
||||
sig += '$arg_styp'
|
||||
}
|
||||
if j < params.len - 1 {
|
||||
sig += ', '
|
||||
}
|
||||
}
|
||||
|
13
vlib/v/tests/anon_fn_decl_with_anon_fn_params_test.v
Normal file
13
vlib/v/tests/anon_fn_decl_with_anon_fn_params_test.v
Normal file
@ -0,0 +1,13 @@
|
||||
fn test_anon_fn_decl_with_anon_fn_params() {
|
||||
a := fn (p1 fn () int, p2 string) string {
|
||||
n := p1()
|
||||
return '$n' + p2
|
||||
}
|
||||
ret := a(aaa, 'hello')
|
||||
println(ret)
|
||||
assert ret == '22hello'
|
||||
}
|
||||
|
||||
fn aaa() int {
|
||||
return 22
|
||||
}
|
Loading…
Reference in New Issue
Block a user