From abcebfed6847d9cca076cdd8ee13d67045111706 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 3 Feb 2025 18:53:54 -0300 Subject: [PATCH] cgen: fix codegen for `for` or-block (fix #23625) (#23644) --- vlib/v/gen/c/cgen.v | 7 +++++++ vlib/v/tests/options/option_for_or_block_test.v | 14 ++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 vlib/v/tests/options/option_for_or_block_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 4b6d3fe68..553278203 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2131,6 +2131,13 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { } } } else { + if i > 0 && stmt is ast.Return { + last_stmt := stmts[i - 1] + if last_stmt is ast.ExprStmt && last_stmt.expr is ast.CallExpr + && !g.out.last_n(2).contains(';') { + g.writeln(';') + } + } g.stmt(stmt) if (g.inside_if_option || g.inside_if_result || g.inside_match_option || g.inside_match_result) && stmt is ast.ExprStmt { diff --git a/vlib/v/tests/options/option_for_or_block_test.v b/vlib/v/tests/options/option_for_or_block_test.v new file mode 100644 index 000000000..cc624bfe9 --- /dev/null +++ b/vlib/v/tests/options/option_for_or_block_test.v @@ -0,0 +1,14 @@ +fn option() ![]int { + return [] +} + +fn func() {} + +fn test_main() { + for _ in option() or { + func() + return + } { + } + assert true +} -- 2.39.5