From d3ccdfd75c38685f8b7669d8caa499ea67e2100f Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 23 Dec 2021 17:26:15 +0800 Subject: [PATCH] cgen: fix channel of sumtype (#12938) --- vlib/v/gen/c/infix_expr.v | 6 +++++- vlib/v/tests/channels_test.v | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/infix_expr.v b/vlib/v/gen/c/infix_expr.v index 4461439de5..c006803146 100644 --- a/vlib/v/gen/c/infix_expr.v +++ b/vlib/v/gen/c/infix_expr.v @@ -70,7 +70,11 @@ fn (mut g Gen) infix_expr_arrow_op(node ast.InfixExpr) { } g.expr(node.left) g.write(', ') - g.expr(node.right) + if g.table.sym(elem_type).kind in [.sum_type, .interface_] { + g.expr_with_cast(node.right, node.right_type, elem_type) + } else { + g.expr(node.right) + } g.write(')') if gen_or { g.or_block(tmp_opt, node.or_block, ast.void_type) diff --git a/vlib/v/tests/channels_test.v b/vlib/v/tests/channels_test.v index c05e658302..5f84c71599 100644 --- a/vlib/v/tests/channels_test.v +++ b/vlib/v/tests/channels_test.v @@ -27,3 +27,21 @@ fn test_printing_of_channels() { fch.close() assert fch.str() == 'chan f64{cap: 100, closed: 1}' } + +struct Aa {} + +struct Ab {} + +type As = Aa | Ab + +fn func(ch chan As) { + ch <- Aa{} +} + +fn test_chan_of_sumtype() { + a := chan As{} + go func(a) + ret := <-a + println(ret) + assert '$ret' == 'As(Aa{})' +}