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

checker: fix generic fn returning result (#16646)

This commit is contained in:
yuyi 2022-12-11 18:34:58 +08:00 committed by GitHub
parent 94098eef79
commit 73e886eafe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -1285,7 +1285,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
} else if typ := c.table.resolve_generic_to_concrete(func.return_type, func.generic_names,
concrete_types)
{
node.return_type = typ
if typ.has_flag(.generic) {
node.return_type = typ
}
return typ
}
}

View File

@ -0,0 +1,21 @@
fn make_2x3[T](mut res [][]T) ! {
mut a := []T{len: 6}
res = reshape(a, [2, 3])!
}
fn reshape[T](y []T, dims []int) ![][]T {
mut res := [][]T{len: dims[0], init: []T{len: 1}}
return res
}
fn test_generic_fn_return_result() {
mut acopy := [][]f32{len: 1, init: []f32{len: 1}}
make_2x3(mut acopy)!
dump(acopy)
assert '${acopy}' == '[[0.0], [0.0]]'
mut bcopy := [][]u8{len: 1, init: []u8{len: 1}}
make_2x3(mut bcopy)!
dump(bcopy)
assert '${bcopy}' == '[[0], [0]]'
}