From 58a8fc6c13329312a73b465e495874f5ac08b7a9 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 13 May 2024 21:56:09 +0530 Subject: [PATCH] cgen: handle auto deref var for index when the array element is an interface or a sumtype (#21491) --- vlib/v/gen/c/array.v | 7 +++++-- .../tests/interface_arr_for_mut_iter_index_test.v | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/interface_arr_for_mut_iter_index_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index cdb3a16016f97c..857b73ec34c9b7 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -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 { diff --git a/vlib/v/tests/interface_arr_for_mut_iter_index_test.v b/vlib/v/tests/interface_arr_for_mut_iter_index_test.v new file mode 100644 index 00000000000000..d8d6d913ce925f --- /dev/null +++ b/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 + } +}