1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix inferring method receiver types (#10166)

This commit is contained in:
yuyi 2021-05-22 16:04:19 +08:00 committed by GitHub
parent 104d9a7c8a
commit 565dbc4051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -503,7 +503,8 @@ pub fn (mut c Checker) infer_fn_generic_types(f ast.Fn, mut call_expr ast.CallEx
if sym.kind == .struct_ {
info := sym.info as ast.Struct
receiver_generic_names := info.generic_types.map(c.table.get_type_symbol(it).name)
if gt_name in receiver_generic_names {
if gt_name in receiver_generic_names
&& info.generic_types.len == info.concrete_types.len {
idx := receiver_generic_names.index(gt_name)
typ = info.concrete_types[idx]
}

View File

@ -40,14 +40,30 @@ fn holder_call_21<T>(func T, a int) int {
return h.call(a)
}
fn holder_call_12<T>(func T, a int) int {
return FnHolder1{func}.call(a)
}
fn holder_call_22<T>(func T, a int) int {
return FnHolder2{func}.call(a)
}
fn test_generic_struct_with_anon_fn_parameter() {
mut ret := holder_call_1(neg, 1)
assert ret == -1
ret = holder_call_11(neg, 2)
assert ret == -2
ret = holder_call_12(neg, 3)
assert ret == -3
ret = FnHolder1<fn (int) int>{neg}.call(4)
assert ret == -4
ret = holder_call_2(neg, 3)
assert ret == -3
ret = holder_call_21(neg, 4)
assert ret == -4
ret = holder_call_22(neg, 5)
assert ret == -5
ret = FnHolder2<fn (int) int>{neg}.call(6)
assert ret == -6
}