checker: check map copy error in fn_mut_arg (#9242)
parent
de73ef665c
commit
9fbb139e29
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue