Skip to content

Commit

Permalink
cgen: fix codegen for a.index/1, where a is []Fn (#20849)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Feb 16, 2024
1 parent d2af0dc commit 497b613
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions vlib/builtin/array_index_test.v
@@ -0,0 +1,35 @@
fn test_index_of_ints() {
ia := [1, 2, 3]
ii := ia.index(2)
dump(ii)
assert ii == 1
}

fn test_index_of_strings() {
sa := ['a', 'b', 'c']
si := sa.index('b')
dump(si)
assert si == 1
}

fn test_index_of_voidptrs() {
pa := [voidptr(123), voidptr(45), voidptr(99)]
pi := pa.index(voidptr(45))
dump(pi)
assert pi == 1
}

//////////

fn a() {}

fn b() {}

fn c() {}

fn test_index_of_fns() {
fa := [a, b, c]
fi := fa.index(b)
dump(fi)
assert fi == 1
}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/array.v
Expand Up @@ -1061,7 +1061,7 @@ fn (mut g Gen) gen_array_index_methods() {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*pelem, v)) {')
} else if elem_sym.kind == .function && !info.elem_type.is_ptr() {
fn_builder.writeln('\t\tif ( pelem == v) {')
fn_builder.writeln('\t\tif ( *pelem == v) {')
} else if elem_sym.kind == .map && !info.elem_type.is_ptr() {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq((*pelem, v))) {')
Expand Down

0 comments on commit 497b613

Please sign in to comment.