parser: add error for `for i := 0; i++; i < ..` (#5850)

pull/5852/head^2
Lukas Neubert 2020-07-17 09:31:58 +02:00 committed by GitHub
parent 82e2b1ec33
commit fa03f390b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 2 deletions

View File

@ -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
}

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
fn main() {
for i := 100; i--; i > 0 {
}
}

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
fn main() {
for i := 0; i++; i < 100 {
}
}