Skip to content

Commit

Permalink
checker: fix generic fn infering map argument (#18341)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jun 4, 2023
1 parent 8d2a0ff commit 0e106c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/checker/containers.v
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/generic_fn_infer_map_argument_test.v
@@ -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'
}

0 comments on commit 0e106c9

Please sign in to comment.