From 6fd22531a90a87f7d93eb96107566979b8591544 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 5 Sep 2022 23:55:53 +0800 Subject: [PATCH] cgen: fix if expr with index expr (#15666) --- vlib/v/gen/c/if.v | 8 ++++++++ vlib/v/tests/if_expr_with_index_expr_test.v | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 vlib/v/tests/if_expr_with_index_expr_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 96ffcd9a4..dba188516 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 000000000..2d1f45263 --- /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 +} -- 2.30.2