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

cgen: fixevaluation order for match expressions inside boolean ones (fix #16554) (#16616)

This commit is contained in:
yuyi 2022-12-09 17:56:38 +08:00 committed by GitHub
parent eb88f7e255
commit 68fb4e9fe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -86,6 +86,11 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
cur_line = g.go_before_stmt(0).trim_left(' \t')
tmp_var = g.new_tmp_var()
g.writeln('${g.typ(node.return_type)} ${tmp_var} = ${g.type_default(node.return_type)};')
g.empty_line = true
if g.infix_left_var_name.len > 0 {
g.writeln('if (${g.infix_left_var_name}) {')
g.indent++
}
}
if is_expr && !need_tmp_var {
@ -125,6 +130,14 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
}
}
g.set_current_pos_as_last_stmt_pos()
if need_tmp_var {
if g.infix_left_var_name.len > 0 {
g.writeln('')
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
}
}
g.write(cur_line)
if need_tmp_var {
g.write('${tmp_var}')

View File

@ -0,0 +1,33 @@
fn test_match_expr_skip_in_infix_expr_1() {
mut a := false
b := a && match true {
true {
a = true
true
}
false {
false
}
}
println(a)
assert a == false
println(b)
assert b == false
}
fn test_match_expr_skip_in_infix_expr_2() {
mut a := true
b := a || match true {
true {
a = false
true
}
false {
false
}
}
println(a)
assert a == true
println(b)
assert b == true
}