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

cgen: fix if_expr with array methods cond (#10786)

This commit is contained in:
yuyi 2021-07-14 00:51:49 +08:00 committed by GitHub
parent 78a41969f6
commit ed78e638b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -4522,7 +4522,10 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
if needs_tmp_var {
g.stmts_with_tmp_var(branch.stmts, tmp)
} else {
// restore if_expr stmt header pos
stmt_pos := g.nth_stmt_pos(0)
g.stmts(branch.stmts)
g.stmt_path_pos << stmt_pos
}
}
g.writeln('}')

View File

@ -199,3 +199,17 @@ fn min<T>(a T, b T) T {
fn test_if_expr_with_fn_generic() {
assert min(42, 13) == 13
}
fn test_if_expr_with_complex_array_methods() {
mut ret := []string{}
entries := ['a', 'b', 'c']
if false {
ret = entries.map(it.capitalize())
} else if entries.any(it == 'a') {
ret = entries.map(it)
}
println(ret)
assert ret == ['a', 'b', 'c']
}