checker: fix error of match in map_init (fix #8579) (#8879)

pull/8882/head
yuyi 2021-02-21 23:09:42 +08:00 committed by GitHub
parent ac4791045f
commit 1a838b1f43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -5417,6 +5417,7 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type {
} }
val := node.vals[i] val := node.vals[i]
key_type := c.expr(key) key_type := c.expr(key)
c.expected_type = val0_type
val_type := c.expr(val) val_type := c.expr(val)
if !c.check_types(key_type, key0_type) { if !c.check_types(key_type, key0_type) {
msg := c.expected_msg(key_type, key0_type) msg := c.expected_msg(key_type, key0_type)

View File

@ -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] }
}
}
}