1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: fix map = map2 assignment (#8581)

This commit is contained in:
yuyi 2021-02-06 10:06:34 +08:00 committed by GitHub
parent 57258c2988
commit 1b6efebac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 1 deletions

View File

@ -2713,7 +2713,14 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
&& right_sym.kind == .array && (left is ast.Ident && !left.is_blank_ident())
&& right is ast.Ident {
// Do not allow `a = b`, only `a = b.clone()`
c.error('use `array2 = array1.clone()` instead of `array2 = array1` (or use `unsafe`)',
c.error('use `array2 $assign_stmt.op.str() array1.clone()` instead of `array2 $assign_stmt.op.str() array1` (or use `unsafe`)',
assign_stmt.pos)
}
if left_sym.kind == .map && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign]
&& right_sym.kind == .map && (left is ast.Ident && !left.is_blank_ident())
&& right is ast.Ident {
// Do not allow `a = b`, only `a = b.clone()`
c.error('use `map2 $assign_stmt.op.str() map1.clone()` instead of `map2 $assign_stmt.op.str() map1` (or use `unsafe`)',
assign_stmt.pos)
}
left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()

View File

@ -0,0 +1,27 @@
vlib/v/checker/tests/array_or_map_assign_err.vv:3:5: error: use `array2 := array1.clone()` instead of `array2 := array1` (or use `unsafe`)
1 | fn main() {
2 | a1 := [1, 2, 3]
3 | a2 := a1
| ~~
4 | mut a3 := []int{}
5 | a3 = a1
vlib/v/checker/tests/array_or_map_assign_err.vv:5:5: error: use `array2 = array1.clone()` instead of `array2 = array1` (or use `unsafe`)
3 | a2 := a1
4 | mut a3 := []int{}
5 | a3 = a1
| ^
6 |
7 | m1 := {'one': 1}
vlib/v/checker/tests/array_or_map_assign_err.vv:8:5: error: use `map2 := map1.clone()` instead of `map2 := map1` (or use `unsafe`)
6 |
7 | m1 := {'one': 1}
8 | m2 := m1
| ~~
9 | mut m3 := map[string]int{}
10 | m3 = m1
vlib/v/checker/tests/array_or_map_assign_err.vv:10:5: error: use `map2 = map1.clone()` instead of `map2 = map1` (or use `unsafe`)
8 | m2 := m1
9 | mut m3 := map[string]int{}
10 | m3 = m1
| ^
11 | }

View File

@ -0,0 +1,11 @@
fn main() {
a1 := [1, 2, 3]
a2 := a1
mut a3 := []int{}
a3 = a1
m1 := {'one': 1}
m2 := m1
mut m3 := map[string]int{}
m3 = m1
}