diff --git a/vlib/math/poly.v b/vlib/math/poly.v index 72c1d440b147ed..ee7d329525ebe5 100644 --- a/vlib/math/poly.v +++ b/vlib/math/poly.v @@ -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 } @@ -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 } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c8e6d043eeb2c9..bd533c335760cd 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index aafb828ddec3a9..3a867f8359a3fc 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -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) diff --git a/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.out b/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.out new file mode 100644 index 00000000000000..16174c46a71d7e --- /dev/null +++ b/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.out @@ -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) diff --git a/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.vv b/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.vv new file mode 100644 index 00000000000000..334f61a7a29114 --- /dev/null +++ b/vlib/v/checker/tests/const_name_for_loop_duplicate_name_err.vv @@ -0,0 +1,9 @@ +const item = 24 +const i = 23 + +fn main() { + for item, i in ['abc', 'def'] { + println(item) + println(i) + } +} diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index 0e33178eff9703..264345567a3fbe 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -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