From 6bf21c300a14ba3d630afceb3cb035f33fd6e57e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 20 Dec 2020 15:33:55 +0100 Subject: [PATCH] checker: do not allow arr1=arr2 without cloning --- cmd/tools/modules/testing/common.v | 2 +- cmd/tools/vtest-fixed.v | 2 +- vlib/builtin/array_test.v | 5 +++-- vlib/v/checker/checker.v | 2 +- vlib/v/tests/valgrind/1.strings_and_arrays.v | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 464f8d3727..e35c208a96 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -296,7 +296,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main mut session := new_test_session(vargs) files := os.walk_ext(os.join_path(parent_dir, folder), '.v') mut mains := []string{} - mut skipped := oskipped + mut skipped := oskipped.clone() for f in files { if !f.contains('modules') && !f.contains('preludes') { // $if !linux { diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index 8b7a70163f..19eb2aedf5 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -42,7 +42,7 @@ fn main() { vexe := pref.vexe_path() vroot := os.dir(vexe) os.chdir(vroot) - args := os.args + args := os.args.clone() args_string := args[1..].join(' ') cmd_prefix := args_string.all_before('test-fixed') title := 'testing all fixed tests' diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 1b768fd2a9..514a8f7987 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -359,6 +359,7 @@ fn test_clone() { assert nums.slice(1, 3).str() == '[2, 3]' } +/* fn test_copy() { a := [1, 2, 3] b := a @@ -366,7 +367,7 @@ fn test_copy() { assert b[1] == 2 assert b[2] == 3 } - +*/ fn test_mutli_array_clone() { // 2d array_int mut a2_1 := [[1, 2, 3], [4, 5, 6]] @@ -683,7 +684,7 @@ fn test_array_str() { numbers := [1, 2, 3] assert numbers == [1, 2, 3] numbers2 := [numbers, [4, 5, 6]] // dup str() bug - _ = numbers2 + println(numbers2) assert true assert numbers.str() == '[1, 2, 3]' // QTODO diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index d9812378fd..3a1c7ab5ed 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2213,7 +2213,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { assign_stmt.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident && right is ast.Ident { // Do not allow `a = b`, only `a = b.clone()` - c.warn('use `array2 = array1.clone()` instead of `array2 = array1`', assign_stmt.pos) + c.error('use `array2 = array1.clone()` instead of `array2 = array1`', assign_stmt.pos) } left_is_ptr := left_type.is_ptr() || left_sym.is_pointer() if left_is_ptr { diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 1a8942fcc9..24522ed8a5 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -4,7 +4,7 @@ import os fn simple() { nums := [1, 2, 3] // local array must be freed println(nums) - nums_copy := nums // array assignments call .clone() + nums_copy := nums.clone() // array assignments call .clone() println(nums_copy) name := 'Peter' // string literals mustn't be freed str_inter := 'hello, $name' // concatenated strings must be freed @@ -214,7 +214,7 @@ fn return_if_expr() string { fn loop_map() { m := { - 'UK': 'London' + 'UK': 'London' 'France': 'Paris' } // keys must be freed