diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 96ffcd9a48..dba1885169 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -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 } diff --git a/vlib/v/tests/if_expr_with_index_expr_test.v b/vlib/v/tests/if_expr_with_index_expr_test.v new file mode 100644 index 0000000000..2d1f452635 --- /dev/null +++ b/vlib/v/tests/if_expr_with_index_expr_test.v @@ -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 +}