cgen: fix == for mut arraay args (#5648)

pull/5647/head
Ruofan XU 2020-07-04 17:36:33 +08:00 committed by GitHub
parent da98c3c135
commit 765ffa37eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -877,3 +877,24 @@ fn test_plus_assign_string() {
a[0] += 'abc'
assert a == ['abc']
}
fn mut_arr_with_eq_in_fn(mut a []int) {
if a == [1,2,3,4] {
a[0] = 0
}
if [0,2,3,4] == a {
a[1] = 0
}
if !(a != [0,0,3,4]) {
a[2] = 0
}
if !([0,0,0,4] != a) {
a[3] = 0
}
}
fn test_mut_arr_with_eq_in_fn() {
mut a := [1,2,3,4]
mut_arr_with_eq_in_fn(mut a)
assert a == [0,0,0,0]
}

View File

@ -1879,8 +1879,14 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else if node.op == .ne {
g.write('!${ptr_typ}_arr_eq(')
}
if node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
g.write(', ')
if node.right_type.is_ptr() {
g.write('*')
}
g.expr(node.right)
g.write(')')
} else if node.op in [.key_in, .not_in] {
@ -2814,7 +2820,7 @@ fn (mut g Gen) assoc(node ast.Assoc) {
fn (mut g Gen) gen_array_equality_fn(left table.Type) string {
left_sym := g.table.get_type_symbol(left)
typ_name := g.typ(left)
ptr_typ := typ_name[typ_name.index_after('_', 0) + 1..]
ptr_typ := typ_name[typ_name.index_after('_', 0) + 1..].trim('*')
elem_sym := g.table.get_type_symbol(left_sym.array_info().elem_type)
elem_typ := g.typ(left_sym.array_info().elem_type)
ptr_elem_typ := elem_typ[elem_typ.index_after('_', 0) + 1..]