cgen: fix infix ops, for cross assignments of types with overloaded operators (#12192)

pull/12198/head
yuyi 2021-10-15 16:32:58 +08:00 committed by GitHub
parent 814b4ebb4c
commit 27cd21e459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -3269,9 +3269,22 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
}
}
ast.InfixExpr {
g.gen_cross_tmp_variable(left, val.left)
g.write(val.op.str())
g.gen_cross_tmp_variable(left, val.right)
sym := g.table.get_type_symbol(val.left_type)
if _ := g.table.type_find_method(sym, val.op.str()) {
left_styp := g.typ(val.left_type.set_nr_muls(0))
g.write(left_styp)
g.write('_')
g.write(util.replace_op(val.op.str()))
g.write('(')
g.gen_cross_tmp_variable(left, val.left)
g.write(', ')
g.gen_cross_tmp_variable(left, val.right)
g.write(')')
} else {
g.gen_cross_tmp_variable(left, val.left)
g.write(val.op.str())
g.gen_cross_tmp_variable(left, val.right)
}
}
ast.PrefixExpr {
g.write(val.op.str())

View File

@ -1,3 +1,5 @@
import math.big
// Test cross assign of array elements
fn test_cross_assign_of_array() {
mut a := [0, 1]
@ -157,3 +159,12 @@ fn test_cross_assign_of_complex_types() {
assert x.a == 2
assert x.b == -1
}
fn test_cross_assign_of_big_int() {
mut a := big.zero_int
mut b := big.one_int
a, b = a + b, a
println(a)
assert a == big.one_int
}