From d5bb269cbe2ff8bc69bd3b8e3bfd4d5e4253a941 Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:18 +0530 Subject: [PATCH 1/3] 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 1877ab8b6d7547..dd884fc0f22da6 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2567,6 +2567,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 757b75b8c2f7713b3dd8b1094666cbec69c18f2f Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:42 +0530 Subject: [PATCH 2/3] 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 dd884fc0f22da6..1877ab8b6d7547 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2567,9 +2567,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 fc8792319e0cbb02c4f1dcbb60030bd1dbdba947 Mon Sep 17 00:00:00 2001 From: Swastik Date: Mon, 20 May 2024 00:17:52 +0530 Subject: [PATCH 3/3] checker: allow alias enum flagged to have bit operations --- vlib/v/checker/infix.v | 10 +++++---- vlib/v/tests/enum_flag_alias_op_test.v | 29 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/enum_flag_alias_op_test.v diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 560f14bfa98fed..078b63c0592a95 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -264,14 +264,16 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { left_sym = c.table.sym(unwrapped_left_type) unwrapped_right_type := c.unwrap_generic(right_type) right_sym = c.table.sym(unwrapped_right_type) - if mut right_sym.info is ast.Alias && right_sym.info.language != .c + if mut right_sym.info is ast.Alias && (right_sym.info.language != .c && c.mod == c.table.type_to_str(unwrapped_right_type).split('.')[0] - && c.table.sym(right_sym.info.parent_type).is_primitive() { + && (c.table.sym(right_sym.info.parent_type).is_primitive() + || c.table.sym(right_sym.info.parent_type).kind == .enum_)) { right_sym = c.table.sym(right_sym.info.parent_type) } - if mut left_sym.info is ast.Alias && left_sym.info.language != .c + if mut left_sym.info is ast.Alias && (left_sym.info.language != .c && c.mod == c.table.type_to_str(unwrapped_left_type).split('.')[0] - && c.table.sym(left_sym.info.parent_type).is_primitive() { + && (c.table.sym(left_sym.info.parent_type).is_primitive() + || c.table.sym(left_sym.info.parent_type).kind == .enum_)) { left_sym = c.table.sym(left_sym.info.parent_type) } diff --git a/vlib/v/tests/enum_flag_alias_op_test.v b/vlib/v/tests/enum_flag_alias_op_test.v new file mode 100644 index 00000000000000..bea8eb37d5ab23 --- /dev/null +++ b/vlib/v/tests/enum_flag_alias_op_test.v @@ -0,0 +1,29 @@ +fn test_enum_flag_alias_op() { + enum_container := EnumContainer{ + et: .a | .b // works + } + + alias_container := AliasContainer{ + at: .a | .b // fails + } + + assert enum_container.et == .a | .b + assert alias_container.at == .a | .b +} + +@[flag] +enum EnumType { + a + b + c +} + +struct EnumContainer { + et EnumType +} + +type AliasType = EnumType + +struct AliasContainer { + at AliasType +}