cgen: fix error for fn with fixed array argument (fix #13976) (#13982)

pull/13987/head
yuyi 2022-04-09 20:57:27 +08:00 committed by GitHub
parent bf385d2ac9
commit 704e3c6e72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 4 deletions

View File

@ -238,11 +238,12 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) {
} else {
g.out.go_back_to(pos)
is_var_mut := !is_decl && left.is_auto_deref_var()
addr := if is_var_mut { '' } else { '&' }
addr_left := if is_var_mut { '' } else { '&' }
g.writeln('')
g.write('memcpy($addr')
g.write('memcpy($addr_left')
g.expr(left)
g.writeln(', &$v_var, sizeof($arr_typ));')
addr_val := if is_fixed_array_var { '' } else { '&' }
g.writeln(', $addr_val$v_var, sizeof($arr_typ));')
}
g.is_assign_lhs = false
} else {

View File

@ -626,7 +626,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
if elem_sym.kind == .function {
g.write(', _MOV((voidptr[]){ ')
} else if elem_is_array_var {
g.write(', &')
addr := if elem_sym.kind == .array_fixed { '' } else { '&' }
g.write(', $addr')
} else {
g.write(', _MOV(($elem_type_str[]){ ')
}

View File

@ -0,0 +1,44 @@
struct Test1 {
mut:
value [4]int
}
fn (mut t Test1) set(new_value [4]int) {
t.value = new_value
}
fn test_fn_with_fixed_array_argument_1() {
mut t := Test1{}
println(t)
assert '$t.value' == '[0, 0, 0, 0]'
t.set([1, 2, 3, 4]!)
println(t)
assert '$t.value' == '[1, 2, 3, 4]'
}
struct Test2 {
mut:
fixed_value [2][4]int
dynamic_value [][4]int
}
fn (mut t Test2) set(index int, new_value [4]int) {
t.fixed_value[index] = new_value
t.dynamic_value << new_value
}
fn test_fn_with_fixed_array_argument_2() {
mut t := Test2{}
println(t)
assert '$t.fixed_value' == '[[0, 0, 0, 0], [0, 0, 0, 0]]'
assert '$t.dynamic_value' == '[]'
t.set(0, [1, 2, 3, 4]!)
println(t)
assert '$t.fixed_value' == '[[1, 2, 3, 4], [0, 0, 0, 0]]'
assert '$t.dynamic_value' == '[[1, 2, 3, 4]]'
}