checker: do not allow arr1=arr2 without cloning
parent
583c02316a
commit
6bf21c300a
|
@ -296,7 +296,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main
|
||||||
mut session := new_test_session(vargs)
|
mut session := new_test_session(vargs)
|
||||||
files := os.walk_ext(os.join_path(parent_dir, folder), '.v')
|
files := os.walk_ext(os.join_path(parent_dir, folder), '.v')
|
||||||
mut mains := []string{}
|
mut mains := []string{}
|
||||||
mut skipped := oskipped
|
mut skipped := oskipped.clone()
|
||||||
for f in files {
|
for f in files {
|
||||||
if !f.contains('modules') && !f.contains('preludes') {
|
if !f.contains('modules') && !f.contains('preludes') {
|
||||||
// $if !linux {
|
// $if !linux {
|
||||||
|
|
|
@ -42,7 +42,7 @@ fn main() {
|
||||||
vexe := pref.vexe_path()
|
vexe := pref.vexe_path()
|
||||||
vroot := os.dir(vexe)
|
vroot := os.dir(vexe)
|
||||||
os.chdir(vroot)
|
os.chdir(vroot)
|
||||||
args := os.args
|
args := os.args.clone()
|
||||||
args_string := args[1..].join(' ')
|
args_string := args[1..].join(' ')
|
||||||
cmd_prefix := args_string.all_before('test-fixed')
|
cmd_prefix := args_string.all_before('test-fixed')
|
||||||
title := 'testing all fixed tests'
|
title := 'testing all fixed tests'
|
||||||
|
|
|
@ -359,6 +359,7 @@ fn test_clone() {
|
||||||
assert nums.slice(1, 3).str() == '[2, 3]'
|
assert nums.slice(1, 3).str() == '[2, 3]'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
fn test_copy() {
|
fn test_copy() {
|
||||||
a := [1, 2, 3]
|
a := [1, 2, 3]
|
||||||
b := a
|
b := a
|
||||||
|
@ -366,7 +367,7 @@ fn test_copy() {
|
||||||
assert b[1] == 2
|
assert b[1] == 2
|
||||||
assert b[2] == 3
|
assert b[2] == 3
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
fn test_mutli_array_clone() {
|
fn test_mutli_array_clone() {
|
||||||
// 2d array_int
|
// 2d array_int
|
||||||
mut a2_1 := [[1, 2, 3], [4, 5, 6]]
|
mut a2_1 := [[1, 2, 3], [4, 5, 6]]
|
||||||
|
@ -683,7 +684,7 @@ fn test_array_str() {
|
||||||
numbers := [1, 2, 3]
|
numbers := [1, 2, 3]
|
||||||
assert numbers == [1, 2, 3]
|
assert numbers == [1, 2, 3]
|
||||||
numbers2 := [numbers, [4, 5, 6]] // dup str() bug
|
numbers2 := [numbers, [4, 5, 6]] // dup str() bug
|
||||||
_ = numbers2
|
println(numbers2)
|
||||||
assert true
|
assert true
|
||||||
assert numbers.str() == '[1, 2, 3]'
|
assert numbers.str() == '[1, 2, 3]'
|
||||||
// QTODO
|
// QTODO
|
||||||
|
|
|
@ -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 &&
|
assign_stmt.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident &&
|
||||||
right is ast.Ident {
|
right is ast.Ident {
|
||||||
// Do not allow `a = b`, only `a = b.clone()`
|
// 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()
|
left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
|
||||||
if left_is_ptr {
|
if left_is_ptr {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import os
|
||||||
fn simple() {
|
fn simple() {
|
||||||
nums := [1, 2, 3] // local array must be freed
|
nums := [1, 2, 3] // local array must be freed
|
||||||
println(nums)
|
println(nums)
|
||||||
nums_copy := nums // array assignments call .clone()
|
nums_copy := nums.clone() // array assignments call .clone()
|
||||||
println(nums_copy)
|
println(nums_copy)
|
||||||
name := 'Peter' // string literals mustn't be freed
|
name := 'Peter' // string literals mustn't be freed
|
||||||
str_inter := 'hello, $name' // concatenated strings must be freed
|
str_inter := 'hello, $name' // concatenated strings must be freed
|
||||||
|
@ -214,7 +214,7 @@ fn return_if_expr() string {
|
||||||
|
|
||||||
fn loop_map() {
|
fn loop_map() {
|
||||||
m := {
|
m := {
|
||||||
'UK': 'London'
|
'UK': 'London'
|
||||||
'France': 'Paris'
|
'France': 'Paris'
|
||||||
}
|
}
|
||||||
// keys must be freed
|
// keys must be freed
|
||||||
|
|
Loading…
Reference in New Issue