cgen: fix match with multi sumtype exprs (#10377)

pull/10406/head
yuyi 2021-06-07 19:16:30 +08:00 committed by GitHub
parent 3582118b7c
commit cd464010d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -4128,7 +4128,6 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
if is_expr && tmp_var.len == 0 {
g.write(' : ')
} else {
g.writeln('')
g.write_v_source_line_info(branch.pos)
g.write('else ')
}
@ -4169,7 +4168,8 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
g.stmts_with_tmp_var(branch.stmts, tmp_var)
g.expected_cast_type = 0
if g.inside_ternary == 0 {
g.write('}')
g.writeln('}')
g.stmt_path_pos << g.out.len
}
sumtype_index++
if branch.exprs.len == 0 || sumtype_index == branch.exprs.len {

View File

@ -0,0 +1,37 @@
struct Empty {}
type SumType = Empty | int
fn isok(a SumType, b SumType) bool {
return !(match a {
int {
match b {
int { a == b }
Empty { false }
}
}
Empty {
false
}
} || match a {
int {
match b {
int { a + 10 == b - 10 }
Empty { false }
}
}
Empty {
false
}
})
}
fn test_match_with_multi_sumtype_exprs() {
a := SumType(1)
b := SumType(Empty{})
res := isok(a, b)
dump(res)
assert res
}