cgen: fix channel of sumtype (#12938)

pull/12945/head
yuyi 2021-12-23 17:26:15 +08:00 committed by GitHub
parent b4723c18fc
commit d3ccdfd75c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -70,7 +70,11 @@ fn (mut g Gen) infix_expr_arrow_op(node ast.InfixExpr) {
}
g.expr(node.left)
g.write(', ')
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)

View File

@ -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{})'
}