From 73e886eafefeaea38bf8960c88b4439673045f42 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 11 Dec 2022 18:34:58 +0800 Subject: [PATCH] checker: fix generic fn returning result (#16646) --- vlib/v/checker/fn.v | 4 +++- vlib/v/tests/generics_fn_return_result_test.v | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics_fn_return_result_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 063b712738..9c6546d4f2 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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 } } diff --git a/vlib/v/tests/generics_fn_return_result_test.v b/vlib/v/tests/generics_fn_return_result_test.v new file mode 100644 index 0000000000..c4b08d4c96 --- /dev/null +++ b/vlib/v/tests/generics_fn_return_result_test.v @@ -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]]' +}