From 02e4aa0f0e675af27c66c15da30925fd5026f863 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 2 Oct 2021 18:40:35 +0300 Subject: [PATCH] v.checker: fix panic for `a, b, c = fn_returning_several_maps()` --- vlib/v/checker/checker.v | 15 ++++++++---- vlib/v/tests/multiret_with_maps_test.v | 32 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/multiret_with_maps_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 56986f4137..ce84dbf390 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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{} + } + } } } } diff --git a/vlib/v/tests/multiret_with_maps_test.v b/vlib/v/tests/multiret_with_maps_test.v new file mode 100644 index 0000000000..c6f8001987 --- /dev/null +++ b/vlib/v/tests/multiret_with_maps_test.v @@ -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 + } +}