Skip to content

Commit

Permalink
cgen: fix fixed array handling with operator overloading call (fix #2…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 10, 2024
1 parent 3142114 commit 86d1cc2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/assign.v
Expand Up @@ -369,7 +369,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
right_sym := g.table.sym(unwrapped_val_type)
unaliased_right_sym := g.table.final_sym(unwrapped_val_type)
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr]
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr, ast.InfixExpr]
|| (val is ast.CastExpr && val.expr !is ast.ArrayInit)
|| (val is ast.PrefixExpr && val.op == .arrow)
|| (val is ast.UnsafeExpr && val.expr is ast.Ident)) && !g.pref.translated
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/gen/c/infix.v
Expand Up @@ -807,6 +807,12 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) {
g.op_arg(node.right, method.params[1].typ, right.typ)
}
g.write(')')

if left.typ != 0 && !left.typ.has_flag(.option) && !left.typ.has_flag(.result)
&& g.table.final_sym(left.typ).kind == .array_fixed {
// it's non-option fixed array, requires accessing .ret_arr member to get the array
g.write('.ret_arr')
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions vlib/v/tests/fixed_array_op_overload_test.v
@@ -0,0 +1,16 @@
type Vec3 = [3]int

fn (v Vec3) add(u Vec3) Vec3 {
return Vec3([v[0] + u[0], v[1] + u[1], v[2] + u[2]]!)
}

fn (v Vec3) + (u Vec3) Vec3 {
return Vec3([v[0] + u[0], v[1] + u[1], v[2] + u[2]]!)
}

fn test_main() {
vec := Vec3([1, 2, 3]!)
a := vec.add(vec)
b := vec + vec
assert a == b
}

0 comments on commit 86d1cc2

Please sign in to comment.