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

checker: fix generic fn infer nested struct (#10262)

This commit is contained in:
yuyi 2021-05-30 19:06:52 +08:00 committed by GitHub
parent c67891d69f
commit 13ddfaa433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -506,11 +506,19 @@ pub fn (mut c Checker) infer_fn_generic_types(f ast.Fn, mut call_expr ast.CallEx
sym := c.table.get_type_symbol(call_expr.receiver_type)
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
&& info.generic_types.len == info.concrete_types.len {
idx := receiver_generic_names.index(gt_name)
typ = info.concrete_types[idx]
if c.table.cur_fn.generic_names.len > 0 { // in generic fn
if gt_name in c.table.cur_fn.generic_names
&& c.table.cur_fn.generic_names.len == c.cur_concrete_types.len {
idx := c.table.cur_fn.generic_names.index(gt_name)
typ = c.cur_concrete_types[idx]
}
} else { // in non-generic fn
receiver_generic_names := info.generic_types.map(c.table.get_type_symbol(it).name)
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

@ -0,0 +1,20 @@
struct Item<T> {
value T
}
fn (i Item<T>) unwrap() T {
return i.value
}
fn process<T>(i Item<T>) {
n := i.unwrap()
println(n)
assert n == 5
}
fn test_generic_fn_infer_nested_struct() {
item := Item<int>{
value: 5
}
process(item)
}