checker: infer T from parameter of type `[]T` (#6611)
parent
05ec32c331
commit
78bcda14c4
|
@ -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) {
|
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, arg in f.params {
|
for i, param in f.params {
|
||||||
if arg.type_source_name == gt_name {
|
arg := call_expr.args[i]
|
||||||
typ = call_expr.args[i].typ
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if typ == table.void_type {
|
if typ == table.void_type {
|
||||||
c.error('could not infer generic type `$gt_name` in call to `$f.name`', call_expr.pos)
|
c.error('could not infer generic type `$gt_name` in call to `$f.name`', call_expr.pos)
|
||||||
} else {
|
} 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)
|
c.table.register_fn_gen_type(f.name, typ)
|
||||||
call_expr.generic_type = typ
|
call_expr.generic_type = typ
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue