cgen: fix match_in_if_expression (#8407)

pull/8421/head
yuyi 2021-01-29 21:51:17 +08:00 committed by GitHub
parent c4758c21c6
commit 56c4a36cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 9 deletions

View File

@ -3444,21 +3444,30 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
return
}
is_expr := (node.is_expr && node.return_type != table.void_type) || g.inside_ternary > 0
mut cond_var := ''
if is_expr {
g.inside_ternary++
// g.write('/* EM ret type=${g.typ(node.return_type)} expected_type=${g.typ(node.expected_type)} */')
}
cur_line := if is_expr {
g.empty_line = true
g.go_before_stmt(0)
if node.cond is ast.Ident || node.cond is ast.SelectExpr || node.cond is ast.IndexExpr {
pos := g.out.len
g.expr(node.cond)
cond_var = g.out.after(pos)
g.out.go_back(cond_var.len)
} else {
''
cur_line := if is_expr {
g.empty_line = true
g.go_before_stmt(0)
} else {
''
}
cond_var = g.new_tmp_var()
g.write('${g.typ(node.cond_type)} $cond_var = ')
g.expr(node.cond)
g.writeln('; ')
g.write(cur_line)
}
cond_var := g.new_tmp_var()
g.write('${g.typ(node.cond_type)} $cond_var = ')
g.expr(node.cond)
g.writeln('; ')
g.write(cur_line)
if is_expr {
// brackets needed otherwise '?' will apply to everything on the left
g.write('(')

View File

@ -0,0 +1,13 @@
fn test_match_in_if_expression() {
num := 3
str := 'a'
res := if num in [1, 3] {
match str {
'a' { 'A' }
else { 'ODD' }
}
} else {
'NONE'
}
assert res == 'A'
}