From 1a838b1f436136d4812dcb058b435434165bbb95 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 21 Feb 2021 23:09:42 +0800 Subject: [PATCH] checker: fix error of match in map_init (fix #8579) (#8879) --- vlib/v/checker/checker.v | 1 + vlib/v/tests/match_in_map_init_test.v | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 vlib/v/tests/match_in_map_init_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 68637060bc..85258a7f84 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5417,6 +5417,7 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type { } val := node.vals[i] key_type := c.expr(key) + c.expected_type = val0_type val_type := c.expr(val) if !c.check_types(key_type, key0_type) { msg := c.expected_msg(key_type, key0_type) diff --git a/vlib/v/tests/match_in_map_init_test.v b/vlib/v/tests/match_in_map_init_test.v new file mode 100644 index 0000000000..0ef88623db --- /dev/null +++ b/vlib/v/tests/match_in_map_init_test.v @@ -0,0 +1,24 @@ +fn test_match_in_map_init() { + ret := foo() + println(ret) + assert ret == map{'token': 'a', 'sleep': '30', 'every': '1'} +} + +fn foo() map[string]string { + mut cfg := map[string][]string{} + cfg['token'] = ['a', 'b'] + cfg['sleep'] = ['30', '60'] + cfg['every'] = ['1', '5'] + + return map{ + 'token': cfg['token'][0] + 'sleep': match cfg['sleep'][0].len { + 0 { '60' } + else { cfg['sleep'][0] } + } + 'every': match cfg['every'][0].len { + 0 { '5' } + else { cfg['every'][0] } + } + } +}