From 1a2884c5af4b2d8fee8dac2e4222b4823d1e930d Mon Sep 17 00:00:00 2001 From: Swastik Date: Sun, 17 Nov 2024 15:04:23 +0530 Subject: [PATCH] checker: error if cannot generic infer type from empty array ([]) --- vlib/v/checker/check_types.v | 4 ++++ .../tests/generic_fn_array_param_not_infer_type_err.out | 5 +++++ .../tests/generic_fn_array_param_not_infer_type_err.vv | 3 +++ 3 files changed, 12 insertions(+) create mode 100644 vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.out create mode 100644 vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.vv diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 11b7e14c48649b..15b7b31e1a93c5 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -1044,6 +1044,10 @@ fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) { && param_elem_sym.name !in c.table.cur_fn.generic_names { arg_elem_typ, param_elem_typ = arg_elem_sym.info.elem_type, param_elem_sym.info.elem_type arg_elem_sym, param_elem_sym = c.table.sym(arg_elem_typ), c.table.sym(param_elem_typ) + } else if arg.expr is ast.ArrayInit && arg.expr.exprs.len == 0 { + c.error('could not infer generic type `${gt_name}` in call to `${func.name}`', + node.pos) + return } else { if param_elem_sym.name == gt_name { typ = arg_elem_typ diff --git a/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.out b/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.out new file mode 100644 index 00000000000000..e1607f489f8c13 --- /dev/null +++ b/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.vv:3:8: error: could not infer generic type `T` in call to `arrays.sum` + 1 | import arrays + 2 | + 3 | arrays.sum([]) or { panic(err) } + | ~~~~~~~ diff --git a/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.vv b/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.vv new file mode 100644 index 00000000000000..d566df370c3c5c --- /dev/null +++ b/vlib/v/checker/tests/generic_fn_array_param_not_infer_type_err.vv @@ -0,0 +1,3 @@ +import arrays + +arrays.sum([]) or { panic(err) }