checker: fix infer_fn_type for generic methods (#7767)

pull/7789/head
Ned Palacios 2021-01-02 00:33:23 +08:00 committed by GitHub
parent ac22fe998a
commit 40b8d9ca3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

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

View File

@ -19,3 +19,19 @@ fn test_generic_method() {
y: 6
}
}
struct Person {
mut:
name string
}
fn (mut p Person) show<T>(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'
}