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

cgen: fix if expr with multiple array call (fix #16583) (#16589)

This commit is contained in:
yuyi 2022-12-05 18:07:39 +08:00 committed by GitHub
parent 59c979c8d2
commit 7e9e2ff459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -948,6 +948,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
}
if s_ends_with_ln {
g.writeln(s)
@ -1023,6 +1024,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
}
if s_ends_with_ln {
g.writeln(s)

View File

@ -337,12 +337,13 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
}
if node.branches.len > 0 {
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
}
g.set_current_pos_as_last_stmt_pos()
if needs_tmp_var {
if g.infix_left_var_name.len > 0 {
g.indent--
g.writeln('}')
g.set_current_pos_as_last_stmt_pos()
}
g.empty_line = false
g.write('${cur_line} ${tmp}')

View File

@ -0,0 +1,29 @@
fn test_if_expr_with_multi_array_call() {
ret := foo()
println(ret)
assert ret == 'all'
}
fn foo() string {
x := [3, 4]
y := [2, 3, 4, 5]
if x.all(it in y) || y.all(it in x) {
return 'all'
} else if x.any(it in y) {
return 'any'
}
return ''
}
fn test_if_expr_with_several_any_calls() {
x := [3, 4]
y := [2, 3, 4, 5]
if x.any(it in y) || y.any(it in x) {
println('then')
assert true
} else if x.any(it in y) {
println('else')
assert false
}
assert true
}