Skip to content

Commit ea6d2d5

Browse files
authored
parser: disallow for mut in range (fix #12234) (#12277)
1 parent 6eaacd3 commit ea6d2d5

File tree

7 files changed

+14
-5
lines changed

7 files changed

+14
-5
lines changed

vlib/builtin/js/array.js.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct array_buffer {
1515
fn (mut a array_buffer) make_copy() {
1616
if a.index_start != 0 || a.has_slice {
1717
mut new_arr := JS.makeEmtpyJSArray()
18-
for mut i in 0 .. a.len {
18+
for i in 0 .. a.len {
1919
#new_arr.push(a.val.get(i))
2020

2121
mut x := i

vlib/v/parser/for.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ fn (mut p Parser) for_stmt() ast.Stmt {
153153
return p.error_with_pos('cannot declare index variable with range `for`',
154154
key_var_pos)
155155
}
156+
if val_is_mut {
157+
return p.error_with_pos('variable in range `for` cannot be mut', mut_pos)
158+
}
156159
} else {
157160
// this type will be set in checker
158161
p.scope.register(ast.Var{

vlib/v/parser/tests/for.out

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vlib/v/parser/tests/for_index_in_range.vv:1:5: error: cannot declare index variable with range `for`
2+
1 | for i, k in 0..5 {
3+
| ^
4+
2 | }
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vlib/v/parser/tests/for_mut_in_range.vv:1:5: error: variable in range `for` cannot be mut
2+
1 | for mut i in 0..5 {
3+
| ~~~
4+
2 | }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
for mut i in 0..5 {
2+
}

0 commit comments

Comments
 (0)