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

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

This commit is contained in:
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 } ast.FnType { arg_sym.info.func.return_type }
else { arg_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' { } else if method_name == 'filter' {
// check fn // check fn
c.check_map_and_filter(false, elem_typ, node) 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
}