From c9e8dd56c2e5192fe030ca8534733887604a0999 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 10 Jul 2023 15:40:48 +0800 Subject: [PATCH] cgen: fix cross assign with aliased array (#18830) --- vlib/v/gen/c/assign.v | 2 +- vlib/v/tests/cross_assign_aliased_array_test.v | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/cross_assign_aliased_array_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 2f6226b982..995f96039d 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -889,7 +889,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) { } } ast.IndexExpr { - sym := g.table.sym(left.left_type) + sym := g.table.sym(g.table.unaliased_type(left.left_type)) if sym.kind == .array { info := sym.info as ast.Array elem_typ := g.table.sym(info.elem_type) diff --git a/vlib/v/tests/cross_assign_aliased_array_test.v b/vlib/v/tests/cross_assign_aliased_array_test.v new file mode 100644 index 0000000000..29558946c3 --- /dev/null +++ b/vlib/v/tests/cross_assign_aliased_array_test.v @@ -0,0 +1,13 @@ +pub type IntSlice = []int + +pub fn (mut x IntSlice) swap(i int, j int) { + x[i], x[j] = x[j], x[i] +} + +fn test_cross_assign_aliased_array() { + mut x := IntSlice([11, 22]) + println(x) + x.swap(0, 1) + println(x) + assert x == IntSlice([22, 11]) +}