checker: infer T from parameter of type `[]T` (#6611)

pull/6613/head
Nick Treleaven 2020-10-13 13:15:25 +01:00 committed by GitHub
parent 05ec32c331
commit 78bcda14c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -376,15 +376,26 @@ pub fn (c &Checker) check_sumtype_compatibility(a, b table.Type) bool {
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, arg in f.params {
if arg.type_source_name == gt_name {
typ = call_expr.args[i].typ
for i, param in f.params {
arg := call_expr.args[i]
if param.type_source_name == gt_name {
typ = arg.typ
break
}
arg_sym := c.table.get_type_symbol(arg.typ)
if arg_sym.kind == .array && param.type_source_name == '[]$gt_name' {
info := arg_sym.info as table.Array
typ = info.elem_type
break
}
}
if typ == table.void_type {
c.error('could not infer generic type `$gt_name` in call to `$f.name`', call_expr.pos)
} else {
if c.pref.is_verbose {
s := c.table.type_to_str(typ)
println('inferred `$f.name<$s>`')
}
c.table.register_fn_gen_type(f.name, typ)
call_expr.generic_type = typ
}

View File

@ -0,0 +1,16 @@
fn f_array<T>(a []T) T {
return a[0]
}
fn g_array<T>(mut a []T) {
a[0] = a[1]
}
fn test_array() {
mut a := [7, 8]
r := f_array(a)
assert r == 7
g_array(mut a)
assert a[0] == 8
}