diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index b91dfa1e26..296eec5ff5 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1647,8 +1647,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as } return } else if arg_sym.kind == .sum_type && exp_sym.kind == .sum_type - && arg.expr is ast.Ident { - g.write('&') + && (arg.expr is ast.Ident || arg.expr is ast.SelectorExpr) { + g.write('&/*sum*/') g.expr(arg.expr) return } diff --git a/vlib/v/tests/fn_call_mut_sumtype_args_test.v b/vlib/v/tests/fn_call_mut_sumtype_args_test.v index 91d3c839a7..5710639aca 100644 --- a/vlib/v/tests/fn_call_mut_sumtype_args_test.v +++ b/vlib/v/tests/fn_call_mut_sumtype_args_test.v @@ -1,11 +1,18 @@ -type Sum = Struct1 | int +type Sum1 = Struct1 | int struct Struct1 { mut: value int } -fn update_sum(mut sum Sum) { +type Sum2 = Struct1 | Struct2 | int + +struct Struct2 { +mut: + sum Sum2 +} + +fn update_sum_1(mut sum Sum1) { match mut sum { Struct1 { sum.value = 42 @@ -14,12 +21,24 @@ fn update_sum(mut sum Sum) { } } +fn update_sum_2(mut sum Sum2) { + match mut sum { + Struct1 { + sum.value = 42 + } + Struct2 { + update_sum_2(mut sum.sum) + } + else {} + } +} + fn test_fn_call_mut_sumtype_args() { - mut s := Sum(Struct1{ + mut s := Sum1(Struct1{ value: 6 }) - update_sum(mut s) + update_sum_1(mut s) if mut s is Struct1 { println(s.value) @@ -28,3 +47,24 @@ fn test_fn_call_mut_sumtype_args() { assert false } } + +fn test_fn_call_mut_sumtype_args_field() { + mut s := Sum2(Struct2{ + sum: Sum2(Struct1{ + value: 6 + }) + }) + + update_sum_2(mut s) + + if mut s is Struct2 { + if mut s.sum is Struct1 { + println(s.sum.value) + assert s.sum.value == 42 + } else { + assert false + } + } else { + assert false + } +}