From 765ffa37eb058b5bef18b7ecc227145a5326668b Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Sat, 4 Jul 2020 17:36:33 +0800 Subject: [PATCH] cgen: fix == for mut arraay args (#5648) --- vlib/builtin/array_test.v | 21 +++++++++++++++++++++ vlib/v/gen/cgen.v | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index ffde01cce7..d95b5ea964 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -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] +} diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c4e5be8b40..8b951a44d6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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..]