checker: fix infer_fn_type for generic methods (#7767)
parent
ac22fe998a
commit
40b8d9ca3d
|
@ -419,7 +419,7 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
|
||||||
gt_name := 'T'
|
gt_name := 'T'
|
||||||
mut typ := table.void_type
|
mut typ := table.void_type
|
||||||
for i, param in f.params {
|
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) {
|
if param.typ.has_flag(.generic) {
|
||||||
typ = arg.typ
|
typ = arg.typ
|
||||||
break
|
break
|
||||||
|
|
|
@ -19,3 +19,19 @@ fn test_generic_method() {
|
||||||
y: 6
|
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'
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue