parser: disallow `for mut in range` (fix #12234) (#12277)

pull/12320/head
Wertzui123 2021-10-27 13:55:36 +02:00 committed by GitHub
parent 6eaacd3391
commit ea6d2d53db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 5 deletions

View File

@ -15,7 +15,7 @@ struct array_buffer {
fn (mut a array_buffer) make_copy() { fn (mut a array_buffer) make_copy() {
if a.index_start != 0 || a.has_slice { if a.index_start != 0 || a.has_slice {
mut new_arr := JS.makeEmtpyJSArray() mut new_arr := JS.makeEmtpyJSArray()
for mut i in 0 .. a.len { for i in 0 .. a.len {
#new_arr.push(a.val.get(i)) #new_arr.push(a.val.get(i))
mut x := i mut x := i

View File

@ -153,6 +153,9 @@ fn (mut p Parser) for_stmt() ast.Stmt {
return p.error_with_pos('cannot declare index variable with range `for`', return p.error_with_pos('cannot declare index variable with range `for`',
key_var_pos) key_var_pos)
} }
if val_is_mut {
return p.error_with_pos('variable in range `for` cannot be mut', mut_pos)
}
} else { } else {
// this type will be set in checker // this type will be set in checker
p.scope.register(ast.Var{ p.scope.register(ast.Var{

View File

@ -1,4 +0,0 @@
vlib/v/parser/tests/for.vv:1:5: error: cannot declare index variable with range `for`
1 | for i, k in 0..5 {
| ^
2 | }

View File

@ -0,0 +1,4 @@
vlib/v/parser/tests/for_index_in_range.vv:1:5: error: cannot declare index variable with range `for`
1 | for i, k in 0..5 {
| ^
2 | }

View File

@ -0,0 +1,4 @@
vlib/v/parser/tests/for_mut_in_range.vv:1:5: error: variable in range `for` cannot be mut
1 | for mut i in 0..5 {
| ~~~
2 | }

View File

@ -0,0 +1,2 @@
for mut i in 0..5 {
}