Skip to content

Commit

Permalink
cgen: fix array of fns index call with direct_array_access mode (#19460)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 28, 2023
1 parent 86a2917 commit 4941a47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion vlib/v/gen/c/index.v
Expand Up @@ -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('*')
Expand Down Expand Up @@ -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)
Expand Down
@@ -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
}

0 comments on commit 4941a47

Please sign in to comment.