checker: fix generic array.map with generic callback fn (#11991)

pull/11998/head
yuyi 2021-09-27 22:50:15 +08:00 committed by GitHub
parent 2ee873d6ca
commit d6a4bce2ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -2683,7 +2683,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
ast.FnType { arg_sym.info.func.return_type }
else { arg_type }
}
node.return_type = c.table.find_or_register_array(ret_type)
node.return_type = c.table.find_or_register_array(c.unwrap_generic(ret_type))
} else if method_name == 'filter' {
// check fn
c.check_map_and_filter(false, elem_typ, node)

View File

@ -0,0 +1,21 @@
fn test_generics_array_map_with_generic_callback() {
mut a := []Sth{}
a = arr_generic<Sth>(buggy_cb)
println(a)
assert a.len == 0
}
fn arr_generic<T>(cb fn (int) T) []T {
return []int{}.map(cb(it))
}
fn buggy_cb(a int) Sth {
return Sth{
a: a
}
}
struct Sth {
a int
b string
}