cgen: fix fn mut val of interface type (#10568)

pull/10581/head
yuyi 2021-06-26 23:13:48 +08:00 committed by GitHub
parent 878efcdade
commit 51075ffa15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -487,6 +487,9 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
if needs_clone {
g.write('string_clone(')
}
if right.unaliased_sym.kind == .interface_ && node.right.is_auto_deref_var() {
g.write('*')
}
g.expr_with_cast(node.right, node.right_type, array_info.elem_type)
if needs_clone {
g.write(')')

View File

@ -43,3 +43,27 @@ fn test_fn_mut_args_of_array_last() {
m.ar << 99
assert pass_array_mut(mut m.ar) == 99
}
interface ChildInterface {
data int
}
struct Child {
data int
}
struct Parent {
mut:
children []ChildInterface
}
fn (mut p Parent) add(mut x ChildInterface) {
p.children << x
}
fn test_fn_mut_args_of_interface() {
mut x := Parent{}
x.add(mut Child{ data: 123 })
println(x.children[0].data)
assert x.children[0].data == 123
}