mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix copying of a fixed array parameter to a function (fix #16640)
This commit is contained in:
parent
30b39bebde
commit
4b2699fddd
@ -443,10 +443,17 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
|
||||
// TODO Instead of the translated check, check if it's a pointer already
|
||||
// and don't generate memcpy &
|
||||
typ_str := g.typ(val_type).trim('*')
|
||||
ref_str := if val_type.is_ptr() { '' } else { '&' }
|
||||
g.write('memcpy((${typ_str}*)')
|
||||
final_typ_str := if is_fixed_array_var { '' } else { '(${typ_str}*)' }
|
||||
final_ref_str := if is_fixed_array_var {
|
||||
''
|
||||
} else if val_type.is_ptr() {
|
||||
'(byte*)'
|
||||
} else {
|
||||
'(byte*)&'
|
||||
}
|
||||
g.write('memcpy(${final_typ_str}')
|
||||
g.expr(left)
|
||||
g.write(', (byte*)${ref_str}')
|
||||
g.write(', ${final_ref_str}')
|
||||
g.expr(val)
|
||||
g.write(', sizeof(${typ_str}))')
|
||||
} else if is_decl {
|
||||
|
@ -119,3 +119,22 @@ fn test_for_in_fixed_array() {
|
||||
arr := [1, 2, 3]!
|
||||
calc_size(arr)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
fn print_fixed_arr(a [2]int) {
|
||||
println('a: ${a}')
|
||||
assert a == [1, 2]!
|
||||
tmp := a
|
||||
println('tmp: ${tmp}')
|
||||
assert tmp == [1, 2]!
|
||||
}
|
||||
|
||||
fn test_assignment_of_a_fixed_array_passed_as_parameter() {
|
||||
arr := [1, 2]!
|
||||
assert arr == [1, 2]!
|
||||
tmp := arr
|
||||
assert tmp == [1, 2]!
|
||||
println(tmp) // [1, 2]
|
||||
print_fixed_arr(arr)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user