cgen: fix appending an array to a shared array (#13265)

pull/13274/head
crthpl 2022-01-25 02:05:43 -08:00 committed by GitHub
parent 5f38ba896e
commit 009a65b1fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -585,15 +585,25 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
// push an array => PUSH_MANY, but not if pushing an array to 2d array (`[][]int << []int`)
g.write('_PUSH_MANY${noscan}(')
mut expected_push_many_atype := left.typ
is_shared := expected_push_many_atype.has_flag(.shared_f)
if !expected_push_many_atype.is_ptr() {
// fn f(mut a []int) { a << [1,2,3] } -> type of `a` is `array_int*` -> no need for &
g.write('&')
} else {
expected_push_many_atype = expected_push_many_atype.deref()
}
if is_shared {
g.write('&')
}
if is_shared {
expected_push_many_atype = expected_push_many_atype.clear_flag(.shared_f)
}
g.expr(node.left)
if node.left_type.has_flag(.shared_f) {
g.write('->val')
}
g.write(', (')
g.expr_with_cast(node.right, node.right_type, left.unaliased)
g.expr_with_cast(node.right, node.right_type, left.unaliased.clear_flag(.shared_f))
styp := g.typ(expected_push_many_atype)
g.write('), $tmp_var, $styp)')
} else {

View File

@ -0,0 +1,6 @@
fn test_shared_array_append_many() {
shared a := []int{}
lock a {
a << [1, 2, 3]
}
}