checker: fix generics fn inferred structure type (fix #10093) (#10096)

pull/10107/head
yuyi 2021-05-15 17:58:33 +08:00 committed by GitHub
parent 1305ca662f
commit 170282b2af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -557,6 +557,13 @@ pub fn (mut c Checker) infer_fn_generic_types(f ast.Fn, mut call_expr ast.CallEx
}
} else if param.typ.has_flag(.variadic) {
to_set = c.table.mktyp(arg.typ)
} else if arg_sym.kind == .struct_ && param.typ.has_flag(.generic) {
info := arg_sym.info as ast.Struct
generic_names := info.generic_types.map(c.table.get_type_symbol(it).name)
if gt_name in generic_names {
idx := generic_names.index(gt_name)
typ = info.concrete_types[idx]
}
}
}

View File

@ -0,0 +1,19 @@
struct Node<T> {
data T
}
fn foo<T>(n Node<T>) string {
return '$n'
}
fn test_generics_fn_infer_struct() {
ret1 := foo(Node<int>{})
println(ret1)
assert ret1.contains('Node<int>{')
assert ret1.contains('data: 0')
ret2 := foo(Node<byte>{})
println(ret2)
assert ret2.contains('Node<byte>{')
assert ret2.contains('data: 0')
}