From 11f734296fb6b9b5da4d812ad936ae2aa6cce6fc Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Jan 2023 06:27:17 -0300 Subject: [PATCH] checker: fix generic array clone (#17153) --- vlib/v/checker/fn.v | 6 +++++- vlib/v/tests/generic_array_clone_test.v | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generic_array_clone_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index dec7467e66..5d026e14c8 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2232,7 +2232,11 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if method_name == 'slice' && !c.is_builtin_mod { c.error('.slice() is a private method, use `x[start..end]` instead', node.pos) } - array_info := left_sym.info as ast.Array + array_info := if left_sym.info is ast.Array { + left_sym.info as ast.Array + } else { + c.table.sym(c.unwrap_generic(left_type)).info as ast.Array + } elem_typ = array_info.elem_type if method_name in ['filter', 'map', 'any', 'all'] { // position of `it` doesn't matter diff --git a/vlib/v/tests/generic_array_clone_test.v b/vlib/v/tests/generic_array_clone_test.v new file mode 100644 index 0000000000..13eb10ae72 --- /dev/null +++ b/vlib/v/tests/generic_array_clone_test.v @@ -0,0 +1,8 @@ +fn test_main() { + encode([]int{len: 5, init: 5}) +} + +fn encode[U](val U) { + new_val := val.clone() + assert new_val == [5, 5, 5, 5, 5] +}