mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix fn call with mut sumtype argument (#13143)
This commit is contained in:
parent
b658b65774
commit
547169674d
@ -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 {
|
||||
|
30
vlib/v/tests/fn_call_mut_sumtype_args_test.v
Normal file
30
vlib/v/tests/fn_call_mut_sumtype_args_test.v
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user