cgen: fix defer with function variables (fix #12854) (#12866)

pull/12868/head
yuyi 2021-12-17 02:58:54 +08:00 committed by GitHub
parent 5f1eaaf3b1
commit f87f8ec3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 1 deletions

View File

@ -345,7 +345,9 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
}
}
info := var.obj as ast.Var
g.writeln('${g.typ(info.typ)}$deref $var.name;')
if g.table.get_type_symbol(info.typ).kind != .function {
g.writeln('${g.typ(info.typ)}$deref $var.name;')
}
}
}
}

View File

@ -0,0 +1,37 @@
[has_globals]
module main
__global fcall_count = int(0)
fn f1() {
println(1)
fcall_count++
}
fn f2() {
println(2)
fcall_count++
}
fn f3(f fn ()) {
f()
}
fn func_defer() {
mut func := f1
println('Before')
defer {
func()
}
defer {
f3(func)
}
func = f2
println('After')
assert true
}
fn test_defer_with_fn_var() {
func_defer()
assert fcall_count == 2
}