checker: do not allow arr1=arr2 without cloning

pull/7429/head
Alexander Medvednikov 2020-12-20 15:33:55 +01:00
parent 583c02316a
commit 6bf21c300a
5 changed files with 8 additions and 7 deletions

View File

@ -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 {

View File

@ -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'

View File

@ -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

View File

@ -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 {

View File

@ -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