Skip to content

Commit

Permalink
cgen: handle auto deref var for index when the array element is an i…
Browse files Browse the repository at this point in the history
…nterface or a sumtype (#21491)
  • Loading branch information
Delta456 committed May 13, 2024
1 parent 331ccac commit 58a8fc6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions vlib/v/gen/c/array.v
Expand Up @@ -1146,10 +1146,13 @@ fn (mut g Gen) gen_array_index(node ast.CallExpr) {
}
g.expr(node.left)
g.write(', ')
if node.args[0].expr.is_auto_deref_var() {

elem_typ := g.table.sym(node.left_type).array_info().elem_type
// auto deref var is redundant for interfaces and sum types.
if node.args[0].expr.is_auto_deref_var()
&& g.table.sym(elem_typ).kind !in [.interface_, .sum_type] {
g.write('*')
}
elem_typ := g.table.sym(node.left_type).array_info().elem_type
if g.table.sym(elem_typ).kind in [.interface_, .sum_type] {
g.expr_with_cast(node.args[0].expr, node.args[0].typ, elem_typ)
} else {
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/tests/interface_arr_for_mut_iter_index_test.v
@@ -0,0 +1,14 @@
interface Ennemi {
}

struct Map {
ennemis []Ennemi
}

fn test_interface_arr_for_mut_iter_index() {
mut maap := Map{}

for mut ennemi in maap.ennemis {
assert maap.ennemis.index(ennemi) == 0
}
}

0 comments on commit 58a8fc6

Please sign in to comment.