From 40b8d9ca3dbddf04ea9b9d73bd75d3aa149201ba Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Sat, 2 Jan 2021 00:33:23 +0800 Subject: [PATCH] checker: fix infer_fn_type for generic methods (#7767) --- vlib/v/checker/check_types.v | 2 +- vlib/v/tests/generics_method_test.v | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index bab606b2ac..3bca0fb59a 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -419,7 +419,7 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) { gt_name := 'T' mut typ := table.void_type for i, param in f.params { - arg := call_expr.args[i] + arg := if i != 0 && call_expr.is_method { call_expr.args[i - 1] } else { call_expr.args[i] } if param.typ.has_flag(.generic) { typ = arg.typ break diff --git a/vlib/v/tests/generics_method_test.v b/vlib/v/tests/generics_method_test.v index ef54376d20..d5b2f58293 100644 --- a/vlib/v/tests/generics_method_test.v +++ b/vlib/v/tests/generics_method_test.v @@ -19,3 +19,19 @@ fn test_generic_method() { y: 6 } } + +struct Person { +mut: + name string +} + +fn (mut p Person) show(name string, data T) string { + p.name = name + return 'name: $p.name, data: $data' +} + +fn test_generic_method_with_fixed_arg_type() { + mut person := Person{} + res := person.show('bob', 10) + assert res == 'name: bob, data: 10' +}