Skip to content

Commit 27cd21e

Browse files
authored
cgen: fix infix ops, for cross assignments of types with overloaded operators (#12192)
1 parent 814b4eb commit 27cd21e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,9 +3269,22 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
32693269
}
32703270
}
32713271
ast.InfixExpr {
3272-
g.gen_cross_tmp_variable(left, val.left)
3273-
g.write(val.op.str())
3274-
g.gen_cross_tmp_variable(left, val.right)
3272+
sym := g.table.get_type_symbol(val.left_type)
3273+
if _ := g.table.type_find_method(sym, val.op.str()) {
3274+
left_styp := g.typ(val.left_type.set_nr_muls(0))
3275+
g.write(left_styp)
3276+
g.write('_')
3277+
g.write(util.replace_op(val.op.str()))
3278+
g.write('(')
3279+
g.gen_cross_tmp_variable(left, val.left)
3280+
g.write(', ')
3281+
g.gen_cross_tmp_variable(left, val.right)
3282+
g.write(')')
3283+
} else {
3284+
g.gen_cross_tmp_variable(left, val.left)
3285+
g.write(val.op.str())
3286+
g.gen_cross_tmp_variable(left, val.right)
3287+
}
32753288
}
32763289
ast.PrefixExpr {
32773290
g.write(val.op.str())

vlib/v/tests/cross_assign_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import math.big
2+
13
// Test cross assign of array elements
24
fn test_cross_assign_of_array() {
35
mut a := [0, 1]
@@ -157,3 +159,12 @@ fn test_cross_assign_of_complex_types() {
157159
assert x.a == 2
158160
assert x.b == -1
159161
}
162+
163+
fn test_cross_assign_of_big_int() {
164+
mut a := big.zero_int
165+
mut b := big.one_int
166+
167+
a, b = a + b, a
168+
println(a)
169+
assert a == big.one_int
170+
}

0 commit comments

Comments
 (0)