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

cgen: fix if expr with nested array calls (fix #11953) (#11973)

This commit is contained in:
yuyi 2021-09-26 01:38:33 +08:00 committed by GitHub
parent 13b2aa701c
commit d329e1decd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 11 deletions

View File

@ -157,7 +157,8 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.empty_line = true
noscan := g.check_noscan(ret_info.elem_type)
g.writeln('$ret_typ $tmp = __new_array${noscan}(0, 0, sizeof($ret_elem_type));')
if g.infix_left_var_name.len > 0 {
has_infix_left_var_name := g.infix_left_var_name.len > 0
if has_infix_left_var_name {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
}
@ -215,7 +216,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
if !is_embed_map_filter {
g.stmt_path_pos << g.out.len
}
if g.infix_left_var_name.len > 0 {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
}
@ -348,7 +349,8 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
g.empty_line = true
noscan := g.check_noscan(info.elem_type)
g.writeln('$styp $tmp = __new_array${noscan}(0, 0, sizeof($elem_type_str));')
if g.infix_left_var_name.len > 0 {
has_infix_left_var_name := g.infix_left_var_name.len > 0
if has_infix_left_var_name {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
}
@ -407,7 +409,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
if !is_embed_map_filter {
g.stmt_path_pos << g.out.len
}
if g.infix_left_var_name.len > 0 {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
}
@ -640,7 +642,8 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) {
elem_type_str := g.typ(info.elem_type)
g.empty_line = true
g.writeln('bool $tmp = false;')
if g.infix_left_var_name.len > 0 {
has_infix_left_var_name := g.infix_left_var_name.len > 0
if has_infix_left_var_name {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
}
@ -699,7 +702,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) {
if !is_embed_map_filter {
g.stmt_path_pos << g.out.len
}
if g.infix_left_var_name.len > 0 {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
}
@ -722,7 +725,8 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) {
elem_type_str := g.typ(info.elem_type)
g.empty_line = true
g.writeln('bool $tmp = true;')
if g.infix_left_var_name.len > 0 {
has_infix_left_var_name := g.infix_left_var_name.len > 0
if has_infix_left_var_name {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
}
@ -782,7 +786,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) {
if !is_embed_map_filter {
g.stmt_path_pos << g.out.len
}
if g.infix_left_var_name.len > 0 {
if has_infix_left_var_name {
g.indent--
g.writeln('}')
}

View File

@ -646,7 +646,7 @@ fn (mut g Gen) need_tmp_var_in_array_call(node ast.Expr) bool {
// infix_expr_and_or_op generates code for `&&` and `||`
fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
if node.right is ast.IfExpr {
// b := a && if true { a = false ...} else {...}
// `b := a && if true { a = false ...} else {...}`
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
if g.need_tmp_var_in_if(node.right) {
@ -666,11 +666,15 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
}
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())
// `if a == 0 || arr.any(it.is_letter()) {...}`
tmp := g.new_tmp_var()
cur_line := g.go_before_stmt(0).trim_space()
g.empty_line = true
g.write('bool $tmp = (')
if g.infix_left_var_name.len > 0 {
g.write('bool $tmp = (($g.infix_left_var_name) $node.op.str() ')
} else {
g.write('bool $tmp = (')
}
g.expr(node.left)
g.writeln(');')
g.stmt_path_pos << g.out.len

View File

@ -0,0 +1,15 @@
fn test_if_expr_with_nested_array_call1() {
arr := ['']
if arr.len == 1 || (arr[1] == '' && arr.all(it[0].is_letter())) {
println('yes')
assert true
}
}
fn test_if_expr_with_nested_array_call2() {
arr := ['abc']
if (arr.len == 1 && arr.all(it.len > 0 && it.bytes().any(it == `b`))) || arr[1] == '' {
println('yes')
assert true
}
}