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

cgen: fix if expr with index expr (#15666)

This commit is contained in:
yuyi
2022-09-05 23:55:53 +08:00
committed by GitHub
parent d649f5aff4
commit 6fd22531a9
2 changed files with 17 additions and 0 deletions

View File

@@ -60,6 +60,14 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
}
}
}
if expr is ast.IndexExpr {
if expr.or_expr.kind != .absent {
return true
}
if g.need_tmp_var_in_expr(expr.index) {
return true
}
}
return false
}

View File

@@ -0,0 +1,9 @@
import rand
fn test_if_expr_with_index_expr() {
a := [1, 2, 3]
b := if true { a[rand.intn(a.len) or { 0 }] } else { 0 }
println(b)
assert true
}