diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index a1e77a8715..69cea9e2cc 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -35,12 +35,15 @@ fn (mut p Parser) for_stmt() ast.Stmt { if p.peek_tok.kind in [.assign, .decl_assign] { init = p.assign_stmt() has_init = true - } else if p.tok.kind != .semicolon { } - // allow `for ;; i++ {` + // Allow `for ;; i++ {` // Allow `for i = 0; i < ...` p.check(.semicolon) if p.tok.kind != .semicolon { + // Disallow `for i := 0; i++; i < ...` + if p.tok.kind == .name && p.peek_tok.kind in [.inc, .dec] { + p.error('cannot use $p.tok.lit$p.peek_tok.kind as value') + } cond = p.expr(0) has_cond = true } diff --git a/vlib/v/parser/tests/dec_use_as_value.out b/vlib/v/parser/tests/dec_use_as_value.out new file mode 100644 index 0000000000..6c6a0ed17f --- /dev/null +++ b/vlib/v/parser/tests/dec_use_as_value.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/dec_use_as_value.v:2:16: error: cannot use i-- as value + 1 | fn main() { + 2 | for i := 100; i--; i > 0 { + | ^ + 3 | } + 4 | } diff --git a/vlib/v/parser/tests/dec_use_as_value.vv b/vlib/v/parser/tests/dec_use_as_value.vv new file mode 100644 index 0000000000..47740705d7 --- /dev/null +++ b/vlib/v/parser/tests/dec_use_as_value.vv @@ -0,0 +1,4 @@ +fn main() { + for i := 100; i--; i > 0 { + } +} diff --git a/vlib/v/parser/tests/inc_use_as_value.out b/vlib/v/parser/tests/inc_use_as_value.out new file mode 100644 index 0000000000..2727bd54dc --- /dev/null +++ b/vlib/v/parser/tests/inc_use_as_value.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/inc_use_as_value.v:2:14: error: cannot use i++ as value + 1 | fn main() { + 2 | for i := 0; i++; i < 100 { + | ^ + 3 | } + 4 | } diff --git a/vlib/v/parser/tests/inc_use_as_value.vv b/vlib/v/parser/tests/inc_use_as_value.vv new file mode 100644 index 0000000000..885f6b7cf7 --- /dev/null +++ b/vlib/v/parser/tests/inc_use_as_value.vv @@ -0,0 +1,4 @@ +fn main() { + for i := 0; i++; i < 100 { + } +}