cgen: fix match expr when evaluating to sumtype (#12237)

pull/12241/head
05st 2021-10-20 03:52:11 -05:00 committed by GitHub
parent ef0eaeee08
commit 3e52d54586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -4264,6 +4264,9 @@ fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 {
cond_sym := g.table.get_final_type_symbol(node.cond_type)
sym := g.table.get_type_symbol(node.return_type)
if g.table.type_kind(node.return_type) == .sum_type {
return true
}
if sym.kind == .multi_return {
return false
}
@ -4298,6 +4301,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
}
need_tmp_var := g.need_tmp_var_in_match(node)
is_expr := (node.is_expr && node.return_type != ast.void_type) || g.inside_ternary > 0
mut cond_var := ''
mut tmp_var := ''
mut cur_line := ''
@ -4496,7 +4500,11 @@ fn (mut g Gen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var stri
}
g.indent++
g.writeln('{')
if is_expr && tmp_var.len > 0 && g.table.get_type_symbol(node.return_type).kind == .sum_type {
g.expected_cast_type = node.return_type
}
g.stmts_with_tmp_var(branch.stmts, tmp_var)
g.expected_cast_type = 0
g.writeln('} break;')
g.indent--
}
@ -4644,7 +4652,11 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
g.writeln(') {')
}
}
if is_expr && tmp_var.len > 0 && g.table.get_type_symbol(node.return_type).kind == .sum_type {
g.expected_cast_type = node.return_type
}
g.stmts_with_tmp_var(branch.stmts, tmp_var)
g.expected_cast_type = 0
if g.inside_ternary == 0 && node.branches.len >= 1 {
g.write('}')
}

View File

@ -0,0 +1,13 @@
type Ty = bool | int
fn test(a bool) Ty {
return match a {
true { 123 }
else { false }
}
}
fn test_match_expr_sumtype_eval() {
assert test(true) == Ty(123)
assert test(false) == Ty(false)
}