v.checker: fix panic for `a, b, c = fn_returning_several_maps()`

pull/11977/head
Delyan Angelov 2021-10-02 18:40:35 +03:00
parent 0916806350
commit 02e4aa0f0e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 43 additions and 4 deletions

View File

@ -4019,10 +4019,17 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
// `map = {}`
if left_type != 0 {
sym := c.table.get_type_symbol(left_type)
if sym.kind == .map && node.right[i] is ast.StructInit {
c.warn('assigning a struct literal to a map is deprecated - use `map{}` instead',
node.right[i].position())
node.right[i] = ast.MapInit{}
if sym.kind == .map {
if node.right.len <= i {
// `map_1, map_2, map_3 = f()`, where f returns (map[int]int, map[int]int, map[int]int)
// i.e. 3 left parts of the assignment, but just 1 right part
} else {
if node.right[i] is ast.StructInit {
c.warn('assigning a struct literal to a map is deprecated - use `map{}` instead',
node.right[i].position())
node.right[i] = ast.MapInit{}
}
}
}
}
}

View File

@ -0,0 +1,32 @@
fn many_maps() (map[int]int, map[int]int, map[int]int) {
return {
1: 2
}, {
3: 4
}, {
5: 1000
}
}
fn test_fn_returning_many_maps_at_the_same_time() {
mut a, mut b, mut c := {
0: 0
}, {
0: 0
}, {
0: 0
}
a, b, c = many_maps()
dump(a)
dump(b)
dump(c)
assert a == {
1: 2
}
assert b == {
3: 4
}
assert c == {
5: 1000
}
}