From 1d9835f0e42183817df51f046bb312122bb70857 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 2 Aug 2023 15:47:52 +0800 Subject: [PATCH] parser, cgen: fix `for i++; i<10; i++ {` (fix #18445) (#19035) --- vlib/v/gen/c/for.v | 6 ++++++ vlib/v/parser/for.v | 4 ++++ vlib/v/tests/for_c_init_with_var_inc_test.v | 9 +++++++++ 3 files changed, 19 insertions(+) create mode 100644 vlib/v/tests/for_c_init_with_var_inc_test.v diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index 872fea1094..6f6a22f28a 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -17,6 +17,9 @@ fn (mut g Gen) for_c_stmt(node ast.ForCStmt) { g.indent++ if node.has_init { g.stmt(node.init) + if node.init is ast.ExprStmt { + g.write('; ') + } } g.writeln('bool _is_first = true;') g.writeln('while (true) {') @@ -58,6 +61,9 @@ fn (mut g Gen) for_c_stmt(node ast.ForCStmt) { g.write('; ') } else { g.stmt(node.init) + if node.init is ast.ExprStmt { + g.write('; ') + } // Remove excess return and add space if g.out.last_n(1) == '\n' { g.go_back(1) diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index cf4b8edbca..395a17854d 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -31,6 +31,7 @@ fn (mut p Parser) for_stmt() ast.Stmt { p.close_scope() return for_stmt } else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] + || (p.peek_tok.kind in [.inc, .dec] && p.peek_token(2).kind in [.semicolon, .comma]) || p.peek_tok.kind.is_assign() || p.tok.kind == .semicolon || (p.peek_tok.kind == .comma && p.peek_token(2).kind != .key_mut && p.peek_token(3).kind != .key_in) { @@ -49,6 +50,9 @@ fn (mut p Parser) for_stmt() ast.Stmt { if p.peek_tok.kind in [.assign, .decl_assign] || p.peek_tok.kind.is_assign() || is_multi { init = p.assign_stmt() has_init = true + } else if p.peek_tok.kind in [.inc, .dec] { + init = p.stmt(false) + has_init = true } comments << p.eat_comments() // Allow `for ;; i++ {` diff --git a/vlib/v/tests/for_c_init_with_var_inc_test.v b/vlib/v/tests/for_c_init_with_var_inc_test.v new file mode 100644 index 0000000000..00cb3745cc --- /dev/null +++ b/vlib/v/tests/for_c_init_with_var_inc_test.v @@ -0,0 +1,9 @@ +fn test_for_c_init_with_var_inc() { + mut results := []int{} + mut i := 0 + for i++; i < 10; i++ { + println(i) + results << i + } + assert results == [1, 2, 3, 4, 5, 6, 7, 8, 9] +}