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

checker, cgen: fix closure with fn variables (fix #15286) (#15427)

This commit is contained in:
yuyi 2022-08-15 11:52:08 +08:00 committed by GitHub
parent 90d9b200f9
commit 6fdcc5bcd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 3 deletions

View File

@ -674,7 +674,15 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
node.fn_var_type = typ
}
ast.Var {
typ = if obj.smartcasts.len != 0 { obj.smartcasts.last() } else { obj.typ }
if obj.smartcasts.len != 0 {
typ = obj.smartcasts.last()
} else {
if obj.typ == 0 {
typ = c.expr(obj.expr)
} else {
typ = obj.typ
}
}
node.is_fn_var = true
node.fn_var_type = typ
}

View File

@ -473,8 +473,23 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) {
ctx_struct := closure_ctx(node.decl)
builder.writeln('$ctx_struct {')
for var in node.inherited_vars {
styp := g.typ(var.typ)
builder.writeln('\t$styp $var.name;')
var_sym := g.table.sym(var.typ)
if var_sym.info is ast.FnType {
ret_styp := g.typ(var_sym.info.func.return_type)
builder.write_string('\t$ret_styp (*$var.name) (')
arg_len := var_sym.info.func.params.len
for j, arg in var_sym.info.func.params {
arg_styp := g.typ(arg.typ)
builder.write_string('$arg_styp $arg.name')
if j < arg_len - 1 {
builder.write_string(', ')
}
}
builder.writeln(');')
} else {
styp := g.typ(var.typ)
builder.writeln('\t$styp $var.name;')
}
}
builder.writeln('};\n')
}
@ -1322,6 +1337,9 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
g.write(')')
}
is_fn_var = true
} else if obj.is_inherited {
g.write(c.closure_ctx + '->' + node.name)
is_fn_var = true
}
}
else {}

View File

@ -0,0 +1,2 @@
test1
test2

View File

@ -0,0 +1,19 @@
module main
fn test1() {
println('test1')
}
fn test2() {
println('test2')
}
fn main() {
t1 := test1
t2 := test2
anon := fn [t1, t2] () {
t1()
t2()
}
anon()
}