From 22c0cdc192f86ab309c7c6a347b7249413638ca3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 6 Jun 2023 22:18:14 +0800 Subject: [PATCH] checker: fix infering generic array type in nested call (fix #18317) (#18350) --- vlib/v/checker/check_types.v | 9 +++++++-- vlib/v/tests/generics_fn_return_result_test.v | 2 +- ...fer_generic_array_type_in_nested_call_test.v | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/infer_generic_array_type_in_nested_call_test.v diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 8587566fb9..b43a0d0238 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -934,9 +934,14 @@ fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) { typ = typ.set_nr_muls(0) } } else if param.typ.has_flag(.generic) { - arg_sym := c.table.final_sym(arg.typ) + arg_typ := if c.table.sym(arg.typ).kind == .any { + c.unwrap_generic(arg.typ) + } else { + arg.typ + } + arg_sym := c.table.final_sym(arg_typ) if param.typ.has_flag(.variadic) { - typ = ast.mktyp(arg.typ) + typ = ast.mktyp(arg_typ) } else if arg_sym.info is ast.Array && param_sym.info is ast.Array { mut arg_elem_typ, mut param_elem_typ := arg_sym.info.elem_type, param_sym.info.elem_type mut arg_elem_sym, mut param_elem_sym := c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ) diff --git a/vlib/v/tests/generics_fn_return_result_test.v b/vlib/v/tests/generics_fn_return_result_test.v index c4b08d4c96..f1cc93e74b 100644 --- a/vlib/v/tests/generics_fn_return_result_test.v +++ b/vlib/v/tests/generics_fn_return_result_test.v @@ -1,6 +1,6 @@ fn make_2x3[T](mut res [][]T) ! { mut a := []T{len: 6} - res = reshape(a, [2, 3])! + res = reshape[T](a, [2, 3])! } fn reshape[T](y []T, dims []int) ![][]T { diff --git a/vlib/v/tests/infer_generic_array_type_in_nested_call_test.v b/vlib/v/tests/infer_generic_array_type_in_nested_call_test.v new file mode 100644 index 0000000000..a85f8799ab --- /dev/null +++ b/vlib/v/tests/infer_generic_array_type_in_nested_call_test.v @@ -0,0 +1,17 @@ +fn unmarshal_any[T]() T { + mut typ := T{} + $if T is $array { + unmarshal_array(mut typ) + } + return typ +} + +fn unmarshal_array[T](mut typ []T) { + typ << 42 +} + +fn test_infer_generic_array_type_in_nested_call() { + ret := unmarshal_any[[]int]() + println(ret) + assert ret == [42] +}