From 3ac3375b43307abffb00728ed97256ab21d980ff Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 4 Jun 2022 19:51:00 +0300 Subject: [PATCH] cgen: fix `for (int i = 0; i < 10; i++, a++) {` (multiple expressions in the inc part) --- vlib/v/gen/c/for.v | 16 +++++++++++++++- ...or_c_multiple_expressions_in_increment_test.v | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/for_c_multiple_expressions_in_increment_test.v diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index a7e8b35b46..5e88134550 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -70,7 +70,21 @@ fn (mut g Gen) for_c_stmt(node ast.ForCStmt) { } g.write('; ') if node.has_inc { - g.stmt(node.inc) + mut processed := false + if node.inc is ast.ExprStmt { + if node.inc.expr is ast.ConcatExpr { + for inc_expr_idx, inc_expr in node.inc.expr.vals { + g.expr(inc_expr) + if inc_expr_idx < node.inc.expr.vals.len - 1 { + g.write(', ') + } + } + processed = true + } + } + if !processed { + g.stmt(node.inc) + } } g.writeln(') {') g.is_vlines_enabled = true diff --git a/vlib/v/tests/for_c_multiple_expressions_in_increment_test.v b/vlib/v/tests/for_c_multiple_expressions_in_increment_test.v new file mode 100644 index 0000000000..84c4c45df2 --- /dev/null +++ b/vlib/v/tests/for_c_multiple_expressions_in_increment_test.v @@ -0,0 +1,6 @@ +fn test_2_expressions_in_same_for_increment_part() { + mut a := 0 + for i := 0; i < 10; i++, a++ { + assert a == i + } +}