From 1654d8b8106b58cfe332152652c3b613728aa7a0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 7 Jan 2022 00:33:37 +0800 Subject: [PATCH] checker: fix generics with nested generic fn inst call (#13059) --- vlib/v/checker/fn.v | 9 +++++- ...s_with_nested_generics_fn_inst_call_test.v | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics_with_nested_generics_fn_inst_call_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 3b25239ef8..1c002bd3a2 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -957,7 +957,14 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) node.concrete_list_pos) } if func.generic_names.len > 0 { - return node.return_type + if has_generic { + return node.return_type + } else if typ := c.table.resolve_generic_to_concrete(func.return_type, func.generic_names, + concrete_types) + { + node.return_type = typ + return typ + } } return func.return_type } diff --git a/vlib/v/tests/generics_with_nested_generics_fn_inst_call_test.v b/vlib/v/tests/generics_with_nested_generics_fn_inst_call_test.v new file mode 100644 index 0000000000..16f66e4b75 --- /dev/null +++ b/vlib/v/tests/generics_with_nested_generics_fn_inst_call_test.v @@ -0,0 +1,30 @@ +fn test_generics_with_nested_generic_fn_inst_call() { + value := [byte(105), 116, 32, 119, 111, 114, 107, 115, 33, 33] + decoded := decode_arr(value) + dump(decoded) + assert decoded.len == 4 + assert decoded[0] == 'it works!!' + assert decoded[1] == 'it works!!' + assert decoded[2] == 'it works!!' + assert decoded[3] == 'it works!!' +} + +fn decode_arr(buf []byte) []T { + arr_size := decode(buf[0..4]) + mut ret := []T{cap: int(arr_size)} + + for _ in 0 .. arr_size { + ret << decode(buf[..]) + } + return ret +} + +fn decode(buf []byte) T { + $if T is u32 { + return u32(buf.len) + } $else $if T is string { + return buf.bytestr() + } $else { + panic('"$T.name" is not supported for `decode`') + } +}