From 4941a470186276da35ebfba913033c7f58d9ab62 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Sep 2023 19:32:43 +0800 Subject: [PATCH] cgen: fix array of fns index call with direct_array_access mode (#19460) --- vlib/v/gen/c/index.v | 9 ++++++++- ...index_call_with_direct_array_access_test.v | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/array_of_fns_index_call_with_direct_array_access_test.v diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index fec906b7a96c28..573391902dd58c 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -264,7 +264,11 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { if elem_sym.info is ast.FnType { g.write('((') g.write_fn_ptr_decl(&elem_sym.info, '') - g.write(')(*(${elem_type_str}*)array_get(') + if is_direct_array_access { + g.write(')((${elem_type_str}*)') + } else { + g.write(')(*(${elem_type_str}*)array_get(') + } } if left_is_ptr && !node.left_type.has_flag(.shared_f) { g.write('*') @@ -296,6 +300,9 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { g.write('data)[') g.expr(node.index) g.write(']') + if g.is_fn_index_call { + g.write(')') + } } else { g.write(', ') g.expr(node.index) diff --git a/vlib/v/tests/array_of_fns_index_call_with_direct_array_access_test.v b/vlib/v/tests/array_of_fns_index_call_with_direct_array_access_test.v new file mode 100644 index 00000000000000..99fcf57d054d12 --- /dev/null +++ b/vlib/v/tests/array_of_fns_index_call_with_direct_array_access_test.v @@ -0,0 +1,19 @@ +struct Bar { + f []fn (int) int +} + +fn func(n int) int { + return n +} + +[direct_array_access] +fn (b Bar) foo() int { + return b.f[0](22) +} + +fn test_array_of_fns_index_call_with_direct_array_acess() { + bar := Bar{[func]} + ret := bar.foo() + println(ret) + assert ret == 22 +}