From 4473a0dd6bb9b08a81581dfea0b93f679fe63c60 Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:18 +0530 Subject: [PATCH 1/4] checker: warn on unused imported functions used via `import math { sin, cos }` --- vlib/v/checker/checker.v | 3 +++ .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ++++++++++ .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c3e11bab6e6f32..387d169c2dfc6f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2707,6 +2707,9 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } + if func.usages != 1 { + c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) + } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out new file mode 100644 index 00000000000000..d0bb2c363db71a --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out @@ -0,0 +1,10 @@ +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv new file mode 100644 index 00000000000000..e5a7d32933c895 --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv @@ -0,0 +1,3 @@ +import math { sin, cos } + +fn main() {} From e6c6c24e9e7284caaf0dcd649a6c27dcef014a39 Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:42 +0530 Subject: [PATCH 2/4] Revert "checker: warn on unused imported functions used via `import math { sin, cos }`" This reverts commit ddf0468f1c4f9af54ada162712c212214e58ca56. --- vlib/v/checker/checker.v | 3 --- .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ---------- .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 --- 3 files changed, 16 deletions(-) delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 387d169c2dfc6f..c3e11bab6e6f32 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2707,9 +2707,6 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } - if func.usages != 1 { - c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) - } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out deleted file mode 100644 index d0bb2c363db71a..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out +++ /dev/null @@ -1,10 +0,0 @@ -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv deleted file mode 100644 index e5a7d32933c895..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -import math { sin, cos } - -fn main() {} From 1a2884c5af4b2d8fee8dac2e4222b4823d1e930d Mon Sep 17 00:00:00 2001 From: Swastik Date: Sun, 17 Nov 2024 15:04:23 +0530 Subject: [PATCH 3/4] 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) } From a679a3686684efa96369d40543748dce9b05bf6d Mon Sep 17 00:00:00 2001 From: Swastik Date: Sun, 17 Nov 2024 15:56:57 +0530 Subject: [PATCH 4/4] fix test --- vlib/v/checker/check_types.v | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 15b7b31e1a93c5..0bc09d26a9eee3 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -1044,10 +1044,6 @@ 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 @@ -1055,6 +1051,11 @@ fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) { typ = typ.set_nr_muls(0) } } + if arg_elem_sym.kind == .any { + c.error('could not infer generic type `${gt_name}` in call to `${func.name}`', + node.pos) + return + } break } }