From 9704a014064de4130a8aa0d1506d6a66b95d1427 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:46:50 +0200 Subject: [PATCH] parser: fix case of falsely registering imports as used, remove unused imports (#21156) --- .../modules/sim/anim/worker.v | 1 - examples/sokol/fonts.v | 1 - examples/sokol/freetype_raven.v | 1 - vlib/gg/draw.c.v | 3 +- vlib/gg/gg.c.v | 1 - vlib/math/big/division_array_ops.v | 1 - vlib/v/builder/cbuilder/parallel_cc.v | 1 - vlib/v/builder/compile.v | 1 - vlib/v/builder/msvc_windows.v | 1 - vlib/v/checker/assign.v | 1 - vlib/v/checker/fn.v | 1 - vlib/v/checker/if.v | 1 - vlib/v/checker/infix.v | 1 - vlib/v/checker/return.v | 1 - .../v/checker/tests/mul_op_wrong_type_err.out | 168 +++++++++--------- vlib/v/checker/tests/mul_op_wrong_type_err.vv | 1 - vlib/v/checker/tests/unused_import_err.out | 12 ++ vlib/v/checker/tests/unused_import_err.vv | 2 + vlib/v/gen/js/comptime.v | 1 - vlib/v/parser/comptime.v | 1 - vlib/v/parser/parser.v | 2 +- vlib/x/sessions/tests/session_app_test.v | 1 - 22 files changed, 100 insertions(+), 104 deletions(-) create mode 100644 vlib/v/checker/tests/unused_import_err.out create mode 100644 vlib/v/checker/tests/unused_import_err.vv diff --git a/examples/pendulum-simulation/modules/sim/anim/worker.v b/examples/pendulum-simulation/modules/sim/anim/worker.v index a3b149e024c212..ff83bf3bdb50f3 100644 --- a/examples/pendulum-simulation/modules/sim/anim/worker.v +++ b/examples/pendulum-simulation/modules/sim/anim/worker.v @@ -1,7 +1,6 @@ module anim import benchmark -import sim import sim.img fn pixels_worker(mut app App) { diff --git a/examples/sokol/fonts.v b/examples/sokol/fonts.v index bf46b452ef64fd..4f6ebc3b79d8bc 100644 --- a/examples/sokol/fonts.v +++ b/examples/sokol/fonts.v @@ -1,4 +1,3 @@ -import sokol import sokol.sapp import sokol.gfx import sokol.sgl diff --git a/examples/sokol/freetype_raven.v b/examples/sokol/freetype_raven.v index 1f2a7ecba69263..ae212347f6b4d4 100644 --- a/examples/sokol/freetype_raven.v +++ b/examples/sokol/freetype_raven.v @@ -1,4 +1,3 @@ -import sokol import sokol.sapp import sokol.gfx import sokol.sgl diff --git a/vlib/gg/draw.c.v b/vlib/gg/draw.c.v index b9cf0eb87abdc0..64ae5073b6aea4 100644 --- a/vlib/gg/draw.c.v +++ b/vlib/gg/draw.c.v @@ -3,9 +3,8 @@ module gg import gx -import sokol -import sokol.sgl import math +import sokol.sgl // draw_pixel draws one pixel on the screen. // diff --git a/vlib/gg/gg.c.v b/vlib/gg/gg.c.v index 5ca1d3d5a3b72b..62661c2b5dbd35 100644 --- a/vlib/gg/gg.c.v +++ b/vlib/gg/gg.c.v @@ -6,7 +6,6 @@ module gg import os import os.font import gx -import sokol import sokol.sapp import sokol.sgl import sokol.gfx diff --git a/vlib/math/big/division_array_ops.v b/vlib/math/big/division_array_ops.v index b9a4eff3c71090..bf4a2a50f64471 100644 --- a/vlib/math/big/division_array_ops.v +++ b/vlib/math/big/division_array_ops.v @@ -1,6 +1,5 @@ module big -import math import math.bits // suppose operand_a bigger than operand_b and both not null. diff --git a/vlib/v/builder/cbuilder/parallel_cc.v b/vlib/v/builder/cbuilder/parallel_cc.v index 2c63e06a3755de..94a42e40b127ee 100644 --- a/vlib/v/builder/cbuilder/parallel_cc.v +++ b/vlib/v/builder/cbuilder/parallel_cc.v @@ -2,7 +2,6 @@ module cbuilder import os import time -import sync import v.util import v.builder import sync.pool diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 6170f64468622e..e7c07be9c3d2cb 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -6,7 +6,6 @@ module builder import os import v.pref import v.util -import v.checker pub type FnBackend = fn (mut b Builder) diff --git a/vlib/v/builder/msvc_windows.v b/vlib/v/builder/msvc_windows.v index 9d122fda2b1057..71153ea233eef3 100644 --- a/vlib/v/builder/msvc_windows.v +++ b/vlib/v/builder/msvc_windows.v @@ -2,7 +2,6 @@ module builder import os import time -import v.pref import v.util import v.cflag diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 82aacda52d7f49..9de799aca5f228 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -3,7 +3,6 @@ module checker import v.ast -import v.pref // TODO: 600 line function fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 0e893cc4b6a56f..1677877f58ad38 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1,7 +1,6 @@ module checker import v.ast -import v.pref import v.util import v.token diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index b8e395d1d07d03..bf716f0bfac61d 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -3,7 +3,6 @@ module checker import v.ast -import v.pref import v.token fn (mut c Checker) check_compatible_types(left_type ast.Type, right ast.TypeNode) ComptimeBranchSkipState { diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index b17891d132eeb1..defcf4c484fc07 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -1,7 +1,6 @@ module checker import v.ast -import v.pref import v.token fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index 131ca273ee2396..a4e5fb44534045 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -3,7 +3,6 @@ module checker import v.ast -import v.pref // error_type_name returns a proper type name reference for error messages // ? => Option type diff --git a/vlib/v/checker/tests/mul_op_wrong_type_err.out b/vlib/v/checker/tests/mul_op_wrong_type_err.out index 10ca288dd89679..3badc0ea93ef98 100644 --- a/vlib/v/checker/tests/mul_op_wrong_type_err.out +++ b/vlib/v/checker/tests/mul_op_wrong_type_err.out @@ -1,98 +1,98 @@ -vlib/v/checker/tests/mul_op_wrong_type_err.vv:5:13: error: mismatched types `Aaa` and `int literal` - 3 | struct Aaa{} - 4 | fn main() { - 5 | println(Aaa{} * 10) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:4:13: error: mismatched types `Aaa` and `int literal` + 2 | struct Aaa{} + 3 | fn main() { + 4 | println(Aaa{} * 10) | ~~~~~~~~~~ - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:5:13: error: infix expr: cannot use `int literal` (right expression) as `Aaa` - 3 | struct Aaa{} - 4 | fn main() { - 5 | println(Aaa{} * 10) + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:4:13: error: infix expr: cannot use `int literal` (right expression) as `Aaa` + 2 | struct Aaa{} + 3 | fn main() { + 4 | println(Aaa{} * 10) | ~~~~~~~~~~ - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:6:13: error: mismatched types `int literal` and `Aaa` - 4 | fn main() { - 5 | println(Aaa{} * 10) - 6 | println(10 * Aaa{}) + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:5:13: error: mismatched types `int literal` and `Aaa` + 3 | fn main() { + 4 | println(Aaa{} * 10) + 5 | println(10 * Aaa{}) | ~~~~~~~~~~ - 7 | println([1,2,3] * 10) - 8 | println(10 * [1,2,3]) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:6:13: error: infix expr: cannot use `Aaa` (right expression) as `int literal` - 4 | fn main() { - 5 | println(Aaa{} * 10) - 6 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) + 7 | println(10 * [1,2,3]) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:5:13: error: infix expr: cannot use `Aaa` (right expression) as `int literal` + 3 | fn main() { + 4 | println(Aaa{} * 10) + 5 | println(10 * Aaa{}) | ~~~~~~~~~~ - 7 | println([1,2,3] * 10) - 8 | println(10 * [1,2,3]) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:7:13: error: mismatched types `[]int` and `int literal` - 5 | println(Aaa{} * 10) - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) + 6 | println([1,2,3] * 10) + 7 | println(10 * [1,2,3]) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:6:13: error: mismatched types `[]int` and `int literal` + 4 | println(Aaa{} * 10) + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) | ~~~~~~~~~~~~ - 8 | println(10 * [1,2,3]) - 9 | a := map[string]int -vlib/v/checker/tests/mul_op_wrong_type_err.vv:7:13: error: infix expr: cannot use `int literal` (right expression) as `[]int` - 5 | println(Aaa{} * 10) - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) + 7 | println(10 * [1,2,3]) + 8 | a := map[string]int +vlib/v/checker/tests/mul_op_wrong_type_err.vv:6:13: error: infix expr: cannot use `int literal` (right expression) as `[]int` + 4 | println(Aaa{} * 10) + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) | ~~~~~~~~~~~~ - 8 | println(10 * [1,2,3]) - 9 | a := map[string]int -vlib/v/checker/tests/mul_op_wrong_type_err.vv:8:13: error: mismatched types `int literal` and `[]int` - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) - 8 | println(10 * [1,2,3]) + 7 | println(10 * [1,2,3]) + 8 | a := map[string]int +vlib/v/checker/tests/mul_op_wrong_type_err.vv:7:13: error: mismatched types `int literal` and `[]int` + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) + 7 | println(10 * [1,2,3]) | ~~~~~~~~~~~~ - 9 | a := map[string]int - 10 | println(a * 10) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:8:13: error: infix expr: cannot use `[]int` (right expression) as `int literal` - 6 | println(10 * Aaa{}) - 7 | println([1,2,3] * 10) - 8 | println(10 * [1,2,3]) + 8 | a := map[string]int + 9 | println(a * 10) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:7:13: error: infix expr: cannot use `[]int` (right expression) as `int literal` + 5 | println(10 * Aaa{}) + 6 | println([1,2,3] * 10) + 7 | println(10 * [1,2,3]) | ~~~~~~~~~~~~ - 9 | a := map[string]int - 10 | println(a * 10) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:10:13: error: mismatched types `map[string]int` and `int literal` - 8 | println(10 * [1,2,3]) - 9 | a := map[string]int - 10 | println(a * 10) + 8 | a := map[string]int + 9 | println(a * 10) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:9:13: error: mismatched types `map[string]int` and `int literal` + 7 | println(10 * [1,2,3]) + 8 | a := map[string]int + 9 | println(a * 10) | ~~~~~~ - 11 | println(10 * a) - 12 | c1 := cmplx.complex(1,-2) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:10:13: error: infix expr: cannot use `int literal` (right expression) as `map[string]int` - 8 | println(10 * [1,2,3]) - 9 | a := map[string]int - 10 | println(a * 10) + 10 | println(10 * a) + 11 | c1 := cmplx.complex(1,-2) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:9:13: error: infix expr: cannot use `int literal` (right expression) as `map[string]int` + 7 | println(10 * [1,2,3]) + 8 | a := map[string]int + 9 | println(a * 10) | ~~~~~~ - 11 | println(10 * a) - 12 | c1 := cmplx.complex(1,-2) -vlib/v/checker/tests/mul_op_wrong_type_err.vv:11:13: error: mismatched types `int literal` and `map[string]int` - 9 | a := map[string]int - 10 | println(a * 10) - 11 | println(10 * a) + 10 | println(10 * a) + 11 | c1 := cmplx.complex(1,-2) +vlib/v/checker/tests/mul_op_wrong_type_err.vv:10:13: error: mismatched types `int literal` and `map[string]int` + 8 | a := map[string]int + 9 | println(a * 10) + 10 | println(10 * a) | ~~~~~~ - 12 | c1 := cmplx.complex(1,-2) - 13 | c2 := c1 * 2.0 -vlib/v/checker/tests/mul_op_wrong_type_err.vv:11:13: error: infix expr: cannot use `map[string]int` (right expression) as `int literal` - 9 | a := map[string]int - 10 | println(a * 10) - 11 | println(10 * a) + 11 | c1 := cmplx.complex(1,-2) + 12 | c2 := c1 * 2.0 +vlib/v/checker/tests/mul_op_wrong_type_err.vv:10:13: error: infix expr: cannot use `map[string]int` (right expression) as `int literal` + 8 | a := map[string]int + 9 | println(a * 10) + 10 | println(10 * a) | ~~~~~~ - 12 | c1 := cmplx.complex(1,-2) - 13 | c2 := c1 * 2.0 -vlib/v/checker/tests/mul_op_wrong_type_err.vv:13:8: error: infix expr: cannot use `float literal` (right expression) as `math.complex.Complex` - 11 | println(10 * a) - 12 | c1 := cmplx.complex(1,-2) - 13 | c2 := c1 * 2.0 + 11 | c1 := cmplx.complex(1,-2) + 12 | c2 := c1 * 2.0 +vlib/v/checker/tests/mul_op_wrong_type_err.vv:12:8: error: infix expr: cannot use `float literal` (right expression) as `math.complex.Complex` + 10 | println(10 * a) + 11 | c1 := cmplx.complex(1,-2) + 12 | c2 := c1 * 2.0 | ~~~~~~~~ - 14 | println(c2) - 15 | c3 := 2.0 * c1 -vlib/v/checker/tests/mul_op_wrong_type_err.vv:15:8: error: infix expr: cannot use `math.complex.Complex` (right expression) as `float literal` - 13 | c2 := c1 * 2.0 - 14 | println(c2) - 15 | c3 := 2.0 * c1 + 13 | println(c2) + 14 | c3 := 2.0 * c1 +vlib/v/checker/tests/mul_op_wrong_type_err.vv:14:8: error: infix expr: cannot use `math.complex.Complex` (right expression) as `float literal` + 12 | c2 := c1 * 2.0 + 13 | println(c2) + 14 | c3 := 2.0 * c1 | ~~~~~~~~ - 16 | println(c3) - 17 | } + 15 | println(c3) + 16 | } diff --git a/vlib/v/checker/tests/mul_op_wrong_type_err.vv b/vlib/v/checker/tests/mul_op_wrong_type_err.vv index a1e3394ce2e7a0..85e7d941265da3 100644 --- a/vlib/v/checker/tests/mul_op_wrong_type_err.vv +++ b/vlib/v/checker/tests/mul_op_wrong_type_err.vv @@ -1,4 +1,3 @@ -import math import math.complex as cmplx struct Aaa{} fn main() { diff --git a/vlib/v/checker/tests/unused_import_err.out b/vlib/v/checker/tests/unused_import_err.out new file mode 100644 index 00000000000000..99d4792eba35fe --- /dev/null +++ b/vlib/v/checker/tests/unused_import_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/unused_import_err.vv:1:8: warning: module 'te (term)' is imported but never used + 1 | import term as te + | ~~~~ + 2 | import te.ui +vlib/v/checker/tests/unused_import_err.vv:2:8: warning: module 'ui (te.ui)' is imported but never used + 1 | import term as te + 2 | import te.ui + | ~~~~~ +vlib/v/checker/tests/unused_import_err.vv:2:1: builder error: cannot import module "te.ui" (not found) + 1 | import term as te + 2 | import te.ui + | ~~~~~~~~~~~~ diff --git a/vlib/v/checker/tests/unused_import_err.vv b/vlib/v/checker/tests/unused_import_err.vv new file mode 100644 index 00000000000000..e2d8c18e265d85 --- /dev/null +++ b/vlib/v/checker/tests/unused_import_err.vv @@ -0,0 +1,2 @@ +import term as te +import te.ui diff --git a/vlib/v/gen/js/comptime.v b/vlib/v/gen/js/comptime.v index 0bf772a2f46650..012bfba034f853 100644 --- a/vlib/v/gen/js/comptime.v +++ b/vlib/v/gen/js/comptime.v @@ -1,7 +1,6 @@ module js import v.ast -import v.pref fn (mut g JsGen) comptime_if(node ast.IfExpr) { if !node.is_expr && !node.has_else && node.branches.len == 1 { diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index b2347633561353..bfb00b64e79711 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -5,7 +5,6 @@ module parser import os import v.ast -import v.pref import v.token const supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error', diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 67b2547fa8d7a0..466088376d0d8b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -699,7 +699,7 @@ fn (mut p Parser) check_js_name() string { fn (mut p Parser) check_name() string { pos := p.tok.pos() name := p.tok.lit - if p.peek_tok.kind == .dot && name in p.imports { + if p.tok.kind != .name && p.peek_tok.kind == .dot && name in p.imports { p.register_used_import(name) } match p.tok.kind { diff --git a/vlib/x/sessions/tests/session_app_test.v b/vlib/x/sessions/tests/session_app_test.v index 8854c273982ef9..4cec03946917d1 100644 --- a/vlib/x/sessions/tests/session_app_test.v +++ b/vlib/x/sessions/tests/session_app_test.v @@ -1,5 +1,4 @@ import net.http -import os import time import x.sessions import x.vweb