From f87f8ec3f7cfc7334e5c5b3d063bbfb9512ec5e1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 17 Dec 2021 02:58:54 +0800 Subject: [PATCH] cgen: fix defer with function variables (fix #12854) (#12866) --- vlib/v/gen/c/fn.v | 4 ++- vlib/v/tests/{ => defer}/defer_return_test.v | 0 vlib/v/tests/{ => defer}/defer_test.v | 0 vlib/v/tests/defer/defer_with_fn_var_test.v | 37 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) rename vlib/v/tests/{ => defer}/defer_return_test.v (100%) rename vlib/v/tests/{ => defer}/defer_test.v (100%) create mode 100644 vlib/v/tests/defer/defer_with_fn_var_test.v 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 +}