checker: fix error for assign array of aliases (#14096)

master
yuyi 2022-04-20 18:08:47 +08:00 committed by GitHub
parent 364656b312
commit 0c3b69eaef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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 {

View File

@ -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
}