diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index d51d73698b..4d3a67d9f0 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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;') + } } } } diff --git a/vlib/v/tests/defer_return_test.v b/vlib/v/tests/defer/defer_return_test.v similarity index 100% rename from vlib/v/tests/defer_return_test.v rename to vlib/v/tests/defer/defer_return_test.v diff --git a/vlib/v/tests/defer_test.v b/vlib/v/tests/defer/defer_test.v similarity index 100% rename from vlib/v/tests/defer_test.v rename to vlib/v/tests/defer/defer_test.v diff --git a/vlib/v/tests/defer/defer_with_fn_var_test.v b/vlib/v/tests/defer/defer_with_fn_var_test.v new file mode 100644 index 0000000000..7d57732aae --- /dev/null +++ b/vlib/v/tests/defer/defer_with_fn_var_test.v @@ -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 +}