parser: add error for `for i := 0; i++; i < ..` (#5850)
parent
82e2b1ec33
commit
fa03f390b3
|
@ -35,12 +35,15 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
if p.peek_tok.kind in [.assign, .decl_assign] {
|
if p.peek_tok.kind in [.assign, .decl_assign] {
|
||||||
init = p.assign_stmt()
|
init = p.assign_stmt()
|
||||||
has_init = true
|
has_init = true
|
||||||
} else if p.tok.kind != .semicolon {
|
|
||||||
}
|
}
|
||||||
// allow `for ;; i++ {`
|
// Allow `for ;; i++ {`
|
||||||
// Allow `for i = 0; i < ...`
|
// Allow `for i = 0; i < ...`
|
||||||
p.check(.semicolon)
|
p.check(.semicolon)
|
||||||
if p.tok.kind != .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)
|
cond = p.expr(0)
|
||||||
has_cond = true
|
has_cond = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
for i := 100; i--; i > 0 {
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
for i := 0; i++; i < 100 {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue