From 0c3b69eaef052dc883be1991c0bbac13d432cf96 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 20 Apr 2022 18:08:47 +0800 Subject: [PATCH] checker: fix error for assign array of aliases (#14096) --- vlib/v/checker/assign.v | 11 +++++++++++ vlib/v/tests/assign_array_of_aliases_test.v | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 vlib/v/tests/assign_array_of_aliases_test.v diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 470d81430a..e269edeee7 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -336,6 +336,17 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.error('use `array2 $node.op.str() array1.clone()` instead of `array2 $node.op.str() array1` (or use `unsafe`)', node.pos) } + if left_sym.kind == .array && right_sym.kind == .array { + // `mut arr := [u8(1),2,3]` + // `arr = [byte(4),5,6]` + left_info := left_sym.info as ast.Array + left_elem_type := c.table.unaliased_type(left_info.elem_type) + right_info := right_sym.info as ast.Array + right_elem_type := c.table.unaliased_type(right_info.elem_type) + if left_info.nr_dims == right_info.nr_dims && left_elem_type == right_elem_type { + continue + } + } if left_sym.kind == .array_fixed && !c.inside_unsafe && node.op in [.assign, .decl_assign] && right_sym.kind == .array_fixed && (left is ast.Ident && !left.is_blank_ident()) && right is ast.Ident { diff --git a/vlib/v/tests/assign_array_of_aliases_test.v b/vlib/v/tests/assign_array_of_aliases_test.v new file mode 100644 index 0000000000..3375b6fa59 --- /dev/null +++ b/vlib/v/tests/assign_array_of_aliases_test.v @@ -0,0 +1,10 @@ +fn test_assign_array_of_aliases() { + mut arr := [u8(1), 2, 3] + arr = [byte(4), 5, 6] + + println(arr) + assert arr.len == 3 + assert arr[0] == 4 + assert arr[1] == 5 + assert arr[2] == 6 +}