From b76b1ee76328ce62a6a7e149f89b83ba786ad603 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 22 Apr 2025 02:38:00 -0300 Subject: [PATCH] cgen: fix codegen for index expr on for loop with branchstmt (fix #22760) (#24289) --- vlib/v/gen/c/cgen.v | 5 +++-- vlib/v/tests/index_on_loop_test.v | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/index_on_loop_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9c36fb4ba..5e80f29cd 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2200,8 +2200,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { } } g.stmt(stmt) - if (g.inside_if_option || g.inside_if_result || g.inside_match_option - || g.inside_match_result) && stmt is ast.ExprStmt { + if stmt is ast.ExprStmt && (g.inside_if_option || g.inside_if_result + || g.inside_match_option || g.inside_match_result + || (stmt.expr is ast.IndexExpr && stmt.expr.or_expr.kind != .absent)) { g.writeln(';') } } diff --git a/vlib/v/tests/index_on_loop_test.v b/vlib/v/tests/index_on_loop_test.v new file mode 100644 index 000000000..8273fcce1 --- /dev/null +++ b/vlib/v/tests/index_on_loop_test.v @@ -0,0 +1,7 @@ +fn test_main() { + arr := ['hello', 'world'] + for { + arr[69] or { break } + } + assert true +} -- 2.39.5