From ea6d2d53db9cf0b2141bb626f3bd02dced4a562f Mon Sep 17 00:00:00 2001 From: Wertzui123 <46199283+Wertzui123@users.noreply.github.com> Date: Wed, 27 Oct 2021 13:55:36 +0200 Subject: [PATCH] parser: disallow `for mut in range` (fix #12234) (#12277) --- vlib/builtin/js/array.js.v | 2 +- vlib/v/parser/for.v | 3 +++ vlib/v/parser/tests/for.out | 4 ---- vlib/v/parser/tests/for_index_in_range.out | 4 ++++ vlib/v/parser/tests/{for.vv => for_index_in_range.vv} | 0 vlib/v/parser/tests/for_mut_in_range.out | 4 ++++ vlib/v/parser/tests/for_mut_in_range.vv | 2 ++ 7 files changed, 14 insertions(+), 5 deletions(-) delete mode 100644 vlib/v/parser/tests/for.out create mode 100644 vlib/v/parser/tests/for_index_in_range.out rename vlib/v/parser/tests/{for.vv => for_index_in_range.vv} (100%) create mode 100644 vlib/v/parser/tests/for_mut_in_range.out create mode 100644 vlib/v/parser/tests/for_mut_in_range.vv diff --git a/vlib/builtin/js/array.js.v b/vlib/builtin/js/array.js.v index 5e16df28f0..d20f4084ed 100644 --- a/vlib/builtin/js/array.js.v +++ b/vlib/builtin/js/array.js.v @@ -15,7 +15,7 @@ struct array_buffer { fn (mut a array_buffer) make_copy() { if a.index_start != 0 || a.has_slice { 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)) mut x := i diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index 36605c3ca0..ee00941bef 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -153,6 +153,9 @@ fn (mut p Parser) for_stmt() ast.Stmt { return p.error_with_pos('cannot declare index variable with range `for`', key_var_pos) } + if val_is_mut { + return p.error_with_pos('variable in range `for` cannot be mut', mut_pos) + } } else { // this type will be set in checker p.scope.register(ast.Var{ diff --git a/vlib/v/parser/tests/for.out b/vlib/v/parser/tests/for.out deleted file mode 100644 index acc089bb6a..0000000000 --- a/vlib/v/parser/tests/for.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/parser/tests/for_index_in_range.out b/vlib/v/parser/tests/for_index_in_range.out new file mode 100644 index 0000000000..dab1a6e940 --- /dev/null +++ b/vlib/v/parser/tests/for_index_in_range.out @@ -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 | } diff --git a/vlib/v/parser/tests/for.vv b/vlib/v/parser/tests/for_index_in_range.vv similarity index 100% rename from vlib/v/parser/tests/for.vv rename to vlib/v/parser/tests/for_index_in_range.vv diff --git a/vlib/v/parser/tests/for_mut_in_range.out b/vlib/v/parser/tests/for_mut_in_range.out new file mode 100644 index 0000000000..6426bd4e86 --- /dev/null +++ b/vlib/v/parser/tests/for_mut_in_range.out @@ -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 | } diff --git a/vlib/v/parser/tests/for_mut_in_range.vv b/vlib/v/parser/tests/for_mut_in_range.vv new file mode 100644 index 0000000000..7c0521e024 --- /dev/null +++ b/vlib/v/parser/tests/for_mut_in_range.vv @@ -0,0 +1,2 @@ +for mut i in 0..5 { +}