Skip to content

Commit 09fff08

Browse files
authored
checker: fix missing check on range expr when high var is same iteration value var (#23130)
1 parent fdfb389 commit 09fff08

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

vlib/v/checker/for.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
6868
} else if high_type.has_option_or_result() {
6969
c.error('the `high` value in a `for x in low..high {` loop, cannot be Result or Option',
7070
node.high.pos())
71+
} else if node.cond is ast.Ident && node.val_var == node.cond.name {
72+
if node.is_range {
73+
c.error('in a `for x in <range>` loop, the key or value iteration variable `${node.val_var}` can not be the same as the low variable',
74+
node.cond.pos())
75+
} else {
76+
c.error('in a `for x in array` loop, the key or value iteration variable `${node.val_var}` can not be the same as the low variable',
77+
node.cond.pos())
78+
}
79+
} else if node.high is ast.Ident && node.val_var == node.high.name {
80+
c.error('in a `for x in <range>` loop, the key or value iteration variable `${node.val_var}` can not be the same as the high variable',
81+
node.high.pos())
7182
}
83+
7284
if high_type in [ast.int_type, ast.int_literal_type] {
7385
node.val_type = typ
7486
} else {
@@ -77,6 +89,10 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
7789
node.high_type = high_type
7890
node.scope.update_var_type(node.val_var, node.val_type)
7991
} else {
92+
if node.cond is ast.Ident && node.cond.name in [node.key_var, node.val_var] {
93+
c.error('in a `for x in array` loop, the key or value iteration variable `${node.val_var}` can not be the same as the low variable',
94+
node.cond.pos())
95+
}
8096
mut is_comptime := false
8197
if (node.cond is ast.Ident && c.comptime.is_comptime_var(node.cond))
8298
|| node.cond is ast.ComptimeSelector {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
vlib/v/checker/for_in_same_var_err.vv:1:10: error: in a `for x in <range>` loop, the key or value iteration variable `x` can not be the same as the low variable
2+
1 | for x in x .. 10 {
3+
| ^
4+
2 | dump(x)
5+
3 | }
6+
vlib/v/checker/for_in_same_var_err.vv:5:15: error: in a `for x in <range>` loop, the key or value iteration variable `y` can not be the same as the high variable
7+
3 | }
8+
4 |
9+
5 | for y in 0 .. y {
10+
| ^
11+
6 | dump(y)
12+
7 | }
13+
vlib/v/checker/for_in_same_var_err.vv:9:10: error: in a `for x in <range>` loop, the key or value iteration variable `z` can not be the same as the low variable
14+
7 | }
15+
8 |
16+
9 | for z in z .. z {
17+
| ^
18+
10 | dump(z)
19+
11 | }
20+
vlib/v/checker/for_in_same_var_err.vv:13:10: error: in a `for x in array` loop, the key or value iteration variable `w` can not be the same as the low variable
21+
11 | }
22+
12 |
23+
13 | for w in w {
24+
| ^
25+
14 | dump(w)
26+
15 | }
27+
vlib/v/checker/for_in_same_var_err.vv:14:7: error: dump expression can not be void
28+
12 |
29+
13 | for w in w {
30+
14 | dump(w)
31+
| ^
32+
15 | }
33+
16 |
34+
vlib/v/checker/for_in_same_var_err.vv:17:13: error: in a `for x in array` loop, the key or value iteration variable `_` can not be the same as the low variable
35+
15 | }
36+
16 |
37+
17 | for k, _ in k {
38+
| ^
39+
18 | dump(k)
40+
19 | }
41+
vlib/v/checker/for_in_same_var_err.vv:17:13: error: for in: cannot index `int`
42+
15 | }
43+
16 |
44+
17 | for k, _ in k {
45+
| ^
46+
18 | dump(k)
47+
19 | }

vlib/v/checker/for_in_same_var_err.vv

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
for x in x .. 10 {
2+
dump(x)
3+
}
4+
5+
for y in 0 .. y {
6+
dump(y)
7+
}
8+
9+
for z in z .. z {
10+
dump(z)
11+
}
12+
13+
for w in w {
14+
dump(w)
15+
}
16+
17+
for k, _ in k {
18+
dump(k)
19+
}

vlib/v/parser/for.v

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ fn (mut p Parser) for_stmt() ast.Stmt {
146146
}
147147
comments << p.eat_comments()
148148
p.check(.key_in)
149-
if p.tok.kind == .name && p.tok.lit in [key_var_name, val_var_name] {
150-
return p.error('in a `for x in array` loop, the key or value iteration variable `${p.tok.lit}` can not be the same as the array variable')
151-
}
152149
comments << p.eat_comments()
153150
// arr_expr
154151
p.inside_for_expr = true

0 commit comments

Comments
 (0)