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:
@@ -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
|
||||
}
|
||||
|
||||
|
9
vlib/v/tests/if_expr_with_index_expr_test.v
Normal file
9
vlib/v/tests/if_expr_with_index_expr_test.v
Normal 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
|
||||
}
|
Reference in New Issue
Block a user