1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix error of match expr (fix #16554) (#16555)

This commit is contained in:
yuyi
2022-11-30 18:04:20 +08:00
committed by GitHub
parent 3175bf374d
commit fd04c1a03a
2 changed files with 36 additions and 0 deletions

View File

@ -830,6 +830,26 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
return
}
g.inside_ternary = prev_inside_ternary
} else if node.right is ast.MatchExpr {
// `b := a && match true { true { a = false ...} else {...}}`
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
if g.need_tmp_var_in_match(node.right) {
tmp := g.new_tmp_var()
cur_line := g.go_before_stmt(0).trim_space()
g.empty_line = true
g.write('bool ${tmp} = (')
g.expr(node.left)
g.writeln(');')
g.set_current_pos_as_last_stmt_pos()
g.write('${cur_line} ${tmp} ${node.op.str()} ')
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
}
g.inside_ternary = prev_inside_ternary
} else if g.need_tmp_var_in_array_call(node.right) {
// `if a == 0 || arr.any(it.is_letter()) {...}`
tmp := g.new_tmp_var()

View File

@ -0,0 +1,16 @@
fn test_match_expr_in_infix_expr() {
mut a := true
b := a && match true {
true {
a = false
true
}
false {
false
}
}
println(a)
assert a == false
println(b)
assert b == true
}