From 547169674d0fc798989fa4768d473e63a18f0563 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 13 Jan 2022 00:36:19 +0800 Subject: [PATCH] cgen: fix fn call with mut sumtype argument (#13143) --- vlib/v/gen/c/fn.v | 6 ++++ vlib/v/tests/fn_call_mut_sumtype_args_test.v | 30 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 vlib/v/tests/fn_call_mut_sumtype_args_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 39c791a3a4..1e7d9a364c 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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 { diff --git a/vlib/v/tests/fn_call_mut_sumtype_args_test.v b/vlib/v/tests/fn_call_mut_sumtype_args_test.v new file mode 100644 index 0000000000..91d3c839a7 --- /dev/null +++ b/vlib/v/tests/fn_call_mut_sumtype_args_test.v @@ -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 + } +}