diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 96fb88a432..118919f04d 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -557,6 +557,13 @@ pub fn (mut c Checker) infer_fn_generic_types(f ast.Fn, mut call_expr ast.CallEx } } else if param.typ.has_flag(.variadic) { to_set = c.table.mktyp(arg.typ) + } else if arg_sym.kind == .struct_ && param.typ.has_flag(.generic) { + info := arg_sym.info as ast.Struct + generic_names := info.generic_types.map(c.table.get_type_symbol(it).name) + if gt_name in generic_names { + idx := generic_names.index(gt_name) + typ = info.concrete_types[idx] + } } } diff --git a/vlib/v/tests/generic_fn_infer_struct_test.v b/vlib/v/tests/generic_fn_infer_struct_test.v new file mode 100644 index 0000000000..3985e1ea67 --- /dev/null +++ b/vlib/v/tests/generic_fn_infer_struct_test.v @@ -0,0 +1,19 @@ +struct Node { + data T +} + +fn foo(n Node) string { + return '$n' +} + +fn test_generics_fn_infer_struct() { + ret1 := foo(Node{}) + println(ret1) + assert ret1.contains('Node{') + assert ret1.contains('data: 0') + + ret2 := foo(Node{}) + println(ret2) + assert ret2.contains('Node{') + assert ret2.contains('data: 0') +}