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)
}
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.is_lvalue() {
&& right_sym.kind == .map && ((right is ast.Ident && right.is_auto_deref_var())
|| !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() {
// Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
right.position())

View File

@ -26,3 +26,10 @@ vlib/v/checker/tests/array_or_map_assign_err.vv:10:7: error: cannot copy map: ca
| ~~
11 |
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

@ -11,4 +11,14 @@ fn main() {
_ = a2
_ = m2
mut m := {'foo':1}
foo(mut m)
}
fn foo(mut m map[string]int) {
m2 := m
m['foo'] = 100
println(m)
println(m2)
}