1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix generic fn infering map argument (#18341)

This commit is contained in:
yuyi 2023-06-05 00:10:41 +08:00 committed by GitHub
parent 8d2a0ffe37
commit 0e106c9062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -344,7 +344,8 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
mut key0_type := ast.void_type
mut val0_type := ast.void_type
use_expected_type := c.expected_type != ast.void_type && !c.inside_const
&& c.table.sym(c.expected_type).kind == .map
&& c.table.sym(c.expected_type).kind == .map && !(c.inside_fn_arg
&& c.expected_type.has_flag(.generic))
if use_expected_type {
sym := c.table.sym(c.expected_type)
info := sym.map_info()

View File

@ -0,0 +1,17 @@
fn f[T](src map[string]T) T {
return src['a']
}
fn test_generic_fn_infer_map_arg() {
r1 := f({
'a': 1
})
println(r1)
assert r1 == 1
r2 := f({
'a': 'hello'
})
println(r2)
assert r2 == 'hello'
}