Skip to content

Commit

Permalink
checker: disallow using a preexisting const name in a for loop, as ei…
Browse files Browse the repository at this point in the history
…ther a key or value ident (#22108)
  • Loading branch information
Delta456 authored Aug 23, 2024
1 parent 19c5670 commit 4b799fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
8 changes: 4 additions & 4 deletions vlib/math/poly.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn poly_n_eval(c []f64, n int, x f64) f64 {
}
len := int(min(c.len, n))
mut ans := c[len - 1]
for e in c[..len - 1] {
ans = e + x * ans
for e_ in c[..len - 1] {
ans = e_ + x * ans
}
return ans
}
Expand All @@ -20,8 +20,8 @@ fn poly_n_1_eval(c []f64, n int, x f64) f64 {
}
len := int(min(c.len, n)) - 1
mut ans := c[len - 1]
for e in c[..len - 1] {
ans = e + x * ans
for e_ in c[..len - 1] {
ans = e_ + x * ans
}
return ans
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ pub:
is_range bool
pos token.Pos
kv_pos token.Pos
vv_pos token.Pos
comments []Comment
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
// and the array cannot be indexed inside the loop
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/checker/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
c.error('invalid use of reserved type `${node.val_var}` as value name', node.pos)
}
}
if _ := c.file.global_scope.find_const('${c.mod}.${node.key_var}') {
c.error('duplicate of a const name `${c.mod}.${node.key_var}`', node.kv_pos)
}

if _ := c.file.global_scope.find_const('${c.mod}.${node.val_var}') {
c.error('duplicate of a const name `${c.mod}.${node.val_var}`', node.vv_pos)
}

if node.is_range {
typ_idx := typ.idx()
high_type := c.expr(mut node.high)
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.vv:5:6: error: duplicate of a const name `item`
3 |
4 | fn main() {
5 | for item, i in ['abc', 'def'] {
| ~~~~
6 | println(item)
7 | println(i)
vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.vv:5:12: error: duplicate of a const name `i`
3 |
4 | fn main() {
5 | for item, i in ['abc', 'def'] {
| ^
6 | println(item)
7 | println(i)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const item = 24
const i = 23

fn main() {
for item, i in ['abc', 'def'] {
println(item)
println(i)
}
}
1 change: 1 addition & 0 deletions vlib/v/parser/for.v
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
is_range: is_range
pos: pos
kv_pos: key_var_pos
vv_pos: val_var_pos
comments: comments
val_is_mut: val_is_mut
scope: p.scope
Expand Down

0 comments on commit 4b799fd

Please sign in to comment.