checker: check map copy error in fn_mut_arg (#9242)

pull/9249/head^2
yuyi 2021-03-11 22:19:36 +08:00 committed by GitHub
parent de73ef665c
commit 9fbb139e29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View File

@ -2747,8 +2747,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
assign_stmt.pos) assign_stmt.pos)
} }
if left_sym.kind == .map && assign_stmt.op in [.assign, .decl_assign] if left_sym.kind == .map && assign_stmt.op in [.assign, .decl_assign]
&& right_sym.kind == .map && !right_type.is_ptr() && !left.is_blank_ident() && right_sym.kind == .map && ((right is ast.Ident && right.is_auto_deref_var())
&& right.is_lvalue() { || !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() {
// Do not allow `a = b` // Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)', c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
right.position()) right.position())

View File

@ -10,10 +10,10 @@ vlib/v/checker/tests/array_or_map_assign_err.vv:5:5: error: use `array2 = array1
4 | mut a3 := []int{} 4 | mut a3 := []int{}
5 | a3 = a1 5 | a3 = a1
| ^ | ^
6 | 6 |
7 | m1 := {'one': 1} 7 | m1 := {'one': 1}
vlib/v/checker/tests/array_or_map_assign_err.vv:8:8: error: cannot copy map: call `move` or `clone` method (or use a reference) vlib/v/checker/tests/array_or_map_assign_err.vv:8:8: error: cannot copy map: call `move` or `clone` method (or use a reference)
6 | 6 |
7 | m1 := {'one': 1} 7 | m1 := {'one': 1}
8 | m2 := m1 8 | m2 := m1
| ~~ | ~~
@ -24,5 +24,12 @@ vlib/v/checker/tests/array_or_map_assign_err.vv:10:7: error: cannot copy map: ca
9 | mut m3 := map[string]int{} 9 | mut m3 := map[string]int{}
10 | m3 = m1 10 | m3 = m1
| ~~ | ~~
11 | 11 |
12 | _ = a2 12 | _ = a2
vlib/v/checker/tests/array_or_map_assign_err.vv:20:8: error: cannot copy map: call `move` or `clone` method (or use a reference)
18 |
19 | fn foo(mut m map[string]int) {
20 | m2 := m
| ^
21 | m['foo'] = 100
22 | println(m)

View File

@ -8,7 +8,17 @@ fn main() {
m2 := m1 m2 := m1
mut m3 := map[string]int{} mut m3 := map[string]int{}
m3 = m1 m3 = m1
_ = a2 _ = a2
_ = m2 _ = m2
mut m := {'foo':1}
foo(mut m)
}
fn foo(mut m map[string]int) {
m2 := m
m['foo'] = 100
println(m)
println(m2)
} }