cgen: fix fn call with mut sumtype argument (#13143)

pull/13151/head
yuyi 2022-01-13 00:36:19 +08:00 committed by GitHub
parent b658b65774
commit 547169674d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -1602,6 +1602,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.write('&/*mut*/')
} else if arg_is_ptr && !expr_is_ptr {
if arg.is_mut {
arg_sym := g.table.sym(arg_typ)
if exp_sym.kind == .array {
if (arg.expr is ast.Ident && (arg.expr as ast.Ident).kind == .variable)
|| arg.expr is ast.SelectorExpr {
@ -1615,6 +1616,11 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.write('}[0]')
}
return
} else if arg_sym.kind == .sum_type && exp_sym.kind == .sum_type
&& arg.expr is ast.Ident {
g.write('&')
g.expr(arg.expr)
return
}
}
if !g.is_json_fn {

View File

@ -0,0 +1,30 @@
type Sum = Struct1 | int
struct Struct1 {
mut:
value int
}
fn update_sum(mut sum Sum) {
match mut sum {
Struct1 {
sum.value = 42
}
else {}
}
}
fn test_fn_call_mut_sumtype_args() {
mut s := Sum(Struct1{
value: 6
})
update_sum(mut s)
if mut s is Struct1 {
println(s.value)
assert s.value == 42
} else {
assert false
}
}