Skip to content

Commit 86d1cc2

Browse files
authored
cgen: fix fixed array handling with operator overloading call (fix #20467) (#20469)
1 parent 3142114 commit 86d1cc2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

vlib/v/gen/c/assign.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
369369
right_sym := g.table.sym(unwrapped_val_type)
370370
unaliased_right_sym := g.table.final_sym(unwrapped_val_type)
371371
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
372-
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr]
372+
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr, ast.InfixExpr]
373373
|| (val is ast.CastExpr && val.expr !is ast.ArrayInit)
374374
|| (val is ast.PrefixExpr && val.op == .arrow)
375375
|| (val is ast.UnsafeExpr && val.expr is ast.Ident)) && !g.pref.translated

vlib/v/gen/c/infix.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) {
807807
g.op_arg(node.right, method.params[1].typ, right.typ)
808808
}
809809
g.write(')')
810+
811+
if left.typ != 0 && !left.typ.has_flag(.option) && !left.typ.has_flag(.result)
812+
&& g.table.final_sym(left.typ).kind == .array_fixed {
813+
// it's non-option fixed array, requires accessing .ret_arr member to get the array
814+
g.write('.ret_arr')
815+
}
810816
}
811817
}
812818

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Vec3 = [3]int
2+
3+
fn (v Vec3) add(u Vec3) Vec3 {
4+
return Vec3([v[0] + u[0], v[1] + u[1], v[2] + u[2]]!)
5+
}
6+
7+
fn (v Vec3) + (u Vec3) Vec3 {
8+
return Vec3([v[0] + u[0], v[1] + u[1], v[2] + u[2]]!)
9+
}
10+
11+
fn test_main() {
12+
vec := Vec3([1, 2, 3]!)
13+
a := vec.add(vec)
14+
b := vec + vec
15+
assert a == b
16+
}

0 commit comments

Comments
 (0)