diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c8264ddb1c..9982187881 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,10 +19,10 @@ jobs: run: ./v test v - name: Test v->js run: ./v -o hi.js examples/hello_v_js.v && node hi.js - - name: Test vid - run: | - git clone --depth 1 https://github.com/vlang/vid.git - cd vid && ../v -debug -o vid . + # - name: Test vid + # run: | + # git clone --depth 1 https://github.com/vlang/vid.git + # cd vid && ../v -debug -o vid . build-ubuntu: runs-on: ubuntu-18.04 diff --git a/.travis.yml b/.travis.yml index 82e8caad54..4a9bc7a223 100644 --- a/.travis.yml +++ b/.travis.yml @@ -82,8 +82,8 @@ script: node hi.js fi - | - if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then - # Build Vid - git clone https://github.com/vlang/vid - cd vid && ../v -debug -o vid . - fi + # if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then + # # Build Vid + # git clone https://github.com/vlang/vid + # cd vid && ../v -debug -o vid . + # fi diff --git a/compiler/cgen.v b/compiler/cgen.v index dbaf707588..74fc3aaf03 100644 --- a/compiler/cgen.v +++ b/compiler/cgen.v @@ -5,7 +5,6 @@ module main import os -import strings struct CGen { out os.File diff --git a/compiler/fn.v b/compiler/fn.v index 957c232add..1050b2c9b9 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -730,6 +730,7 @@ fn (p mut Parser) fn_args(f mut Fn) { if types_only { for p.tok != .rpar { typ := p.get_type() + p.check_and_register_used_imported_type(typ) v := Var { typ: typ is_arg: true @@ -761,6 +762,7 @@ fn (p mut Parser) fn_args(f mut Fn) { p.next() } mut typ := p.get_type() + p.check_and_register_used_imported_type(typ) if is_mut && is_primitive_type(typ) { p.error('mutable arguments are only allowed for arrays, maps, and structs.' + '\nreturn values instead: `foo(n mut int)` => `foo(n int) int`') diff --git a/compiler/main.v b/compiler/main.v index 9ed4947721..f1bf471c1d 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -653,11 +653,13 @@ fn (v mut V) add_v_files_to_compile() { } } // Add remaining user files + mut i := 0 mut j := 0 mut len := -1 - for i, fit in v.table.file_imports { + for _, fit in v.table.file_imports { // Don't add a duplicate; builtin files are always there if fit.file_path in v.files || fit.module_name == 'builtin' { + i++ continue } if len == -1 { @@ -671,6 +673,7 @@ fn (v mut V) add_v_files_to_compile() { //println(fit) //println('fit $fit.file_path') v.files << fit.file_path + i++ } } diff --git a/compiler/modules.v b/compiler/modules.v index b148ff7a86..62374f9aef 100644 --- a/compiler/modules.v +++ b/compiler/modules.v @@ -7,8 +7,8 @@ module main import os // add a module and its deps (module speficic dag method) -pub fn(graph mut DepGraph) from_import_tables(import_tables []FileImportTable) { - for fit in import_tables { +pub fn(graph mut DepGraph) from_import_tables(import_tables map[string]FileImportTable) { + for _, fit in import_tables { mut deps := []string for _, m in fit.imports { deps << m diff --git a/compiler/parser.v b/compiler/parser.v index 7bd49ff8d2..32c4410322 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -6,7 +6,6 @@ module main import ( os - rand strings ) @@ -29,7 +28,7 @@ mut: lit string cgen &CGen table &Table - import_table &FileImportTable // Holds imports for just the file being parsed + import_table FileImportTable // Holds imports for just the file being parsed pass Pass os OS mod string @@ -92,7 +91,7 @@ fn (v mut V) new_parser(path string) Parser { break } } - + mut p := Parser { v: v file_path: path @@ -101,7 +100,7 @@ fn (v mut V) new_parser(path string) Parser { file_pcguard: path_pcguard scanner: new_scanner(path) table: v.table - import_table: new_file_import_table(path) + import_table: v.table.get_file_import_table(path) cur_fn: EmptyFn cgen: v.cgen is_script: (v.pref.is_script && path == v.dir) @@ -186,7 +185,7 @@ fn (p mut Parser) parse(pass Pass) { p.error('module `builtin` cannot be imported') } // save file import table - p.table.file_imports << *p.import_table + p.table.file_imports[p.file_path] = p.import_table return } // Go through every top level token or throw a compilation error if a non-top level token is met @@ -277,6 +276,9 @@ fn (p mut Parser) parse(pass Pass) { if p.is_script && !p.pref.is_test { p.set_current_fn( MainFn ) p.check_unused_variables() + if !p.first_pass() && !p.pref.is_repl { + p.check_unused_imports() + } } if false && !p.first_pass() && p.fileis('main.v') { out := os.create('/var/tmp/fmt.v') or { @@ -635,6 +637,7 @@ fn (p mut Parser) struct_decl() { access_mod := if is_pub{AccessMod.public} else { AccessMod.private} p.fgen(' ') field_type := p.get_type() + p.check_and_register_used_imported_type(field_type) is_atomic := p.tok == .key_atomic if is_atomic { p.next() @@ -1555,6 +1558,7 @@ fn (p mut Parser) name_expr() string { mut mod := name // must be aliased module if name != p.mod && p.import_table.known_alias(name) { + p.import_table.register_used_import(name) // we replaced "." with "_dot_" in p.mod for C variable names, do same here. mod = p.import_table.resolve_alias(name).replace('.', '_dot_') } @@ -2388,6 +2392,7 @@ fn (p mut Parser) factor() string { if !('json' in p.table.imports) { p.error('undefined: `json`, use `import json`') } + p.import_table.register_used_import('json') return p.js_decode() } //if p.fileis('orm_test') { @@ -3801,3 +3806,29 @@ fn (p mut Parser) defer_st() { p.cgen.resetln('') } +fn (p mut Parser) check_and_register_used_imported_type(typ_name string) { + us_idx := typ_name.index('__') + if us_idx != -1 { + arg_mod := typ_name.left(us_idx) + if p.import_table.known_alias(arg_mod) { + p.import_table.register_used_import(arg_mod) + } + } +} + +fn (p mut Parser) check_unused_imports() { + mut output := '' + for alias, mod in p.import_table.imports { + if !p.import_table.is_used_import(alias) { + mod_alias := if alias == mod { alias } else { '$alias ($mod)' } + output += '\n * $mod_alias' + } + } + if output == '' { return } + output = '$p.file_path: the following imports were never used:$output' + if p.pref.is_prod { + cerror(output) + } else { + println('warning: $output') + } +} diff --git a/compiler/table.v b/compiler/table.v index 9dfb63f89f..f3b83da741 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -16,7 +16,7 @@ mut: obf_ids map[string]int // obf_ids['myfunction'] == 23 modules []string // List of all modules registered by the application imports []string // List of all imports - file_imports []FileImportTable // List of imports for file + file_imports map[string]FileImportTable // List of imports for file cflags []CFlag // ['-framework Cocoa', '-lglfw3'] fn_cnt int //atomic obfuscate bool @@ -31,9 +31,10 @@ mut: // Holds import information scoped to the parsed file struct FileImportTable { mut: - module_name string - file_path string - imports map[string]string + module_name string + file_path string + imports map[string]string // alias => module + used_imports []string // alias } enum AccessMod { @@ -829,8 +830,22 @@ fn (table &Table) qualify_module(mod string, file_path string) string { return mod } -fn new_file_import_table(file_path string) &FileImportTable { - return &FileImportTable{ +fn (table &Table) get_file_import_table(file_path string) FileImportTable { + // if file_path.clone() in table.file_imports { + // return table.file_imports[file_path.clone()] + // } + // just get imports. memory error when recycling import table + mut imports := map[string]string + if file_path in table.file_imports { + imports = table.file_imports[file_path].imports + } + mut fit := new_file_import_table(file_path.clone()) + fit.imports = imports + return fit +} + +fn new_file_import_table(file_path string) FileImportTable { + return FileImportTable{ file_path: file_path imports: map[string]string } @@ -845,7 +860,9 @@ fn (fit mut FileImportTable) register_import(mod string) { } fn (fit mut FileImportTable) register_alias(alias string, mod string) { - if alias in fit.imports { + // NOTE: come back here + // if alias in fit.imports && fit.imports[alias] == mod {} + if alias in fit.imports && fit.imports[alias] != mod { cerror('cannot import $mod as $alias: import name $alias already in use in "${fit.file_path}".') } if mod.contains('.internal.') { @@ -880,6 +897,16 @@ fn (fit &FileImportTable) resolve_alias(alias string) string { return fit.imports[alias] } +fn (fit mut FileImportTable) register_used_import(alias string) { + if !(alias in fit.used_imports) { + fit.used_imports << alias + } +} + +fn (fit &FileImportTable) is_used_import(alias string) bool { + return alias in fit.used_imports +} + fn (t &Type) contains_field_type(typ string) bool { if !t.name[0].is_capital() { return false diff --git a/examples/hot_reload/graph.v b/examples/hot_reload/graph.v index 79b55192f1..b9b6f3a65d 100644 --- a/examples/hot_reload/graph.v +++ b/examples/hot_reload/graph.v @@ -1,12 +1,11 @@ module main import gx -import gl import gg -import time -import glfw -import math - +import time +import glfw +// import math + const ( Size = 700 Scale = 50.0 diff --git a/examples/hot_reload/message.v b/examples/hot_reload/message.v index 3b35f5cf01..9822d91bcc 100644 --- a/examples/hot_reload/message.v +++ b/examples/hot_reload/message.v @@ -3,7 +3,6 @@ module main import time -import os [live] fn print_message() { diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index ce99fc7534..be16c25825 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -7,7 +7,6 @@ module main import rand import time import gx -import gl import gg import glfw import math diff --git a/vc b/vc deleted file mode 160000 index c2c2641919..0000000000 --- a/vc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c2c26419198a271ca00cb8d77119d17c5622569e diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index c8a9e2e38b..0d0f376ca4 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -1,5 +1,3 @@ -import os - const ( q = [1, 2, 3] A = 8 diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 99adb3aa11..937b031ac1 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -1,4 +1,4 @@ -import time +// import time struct User { name string diff --git a/vlib/crypto/aes/aes.v b/vlib/crypto/aes/aes.v index e027fdc082..b6c21dd758 100644 --- a/vlib/crypto/aes/aes.v +++ b/vlib/crypto/aes/aes.v @@ -8,7 +8,6 @@ module aes import ( - crypto.cipher crypto.internal.subtle ) diff --git a/vlib/crypto/aes/aes_cbc.v b/vlib/crypto/aes/aes_cbc.v index 56bd9ae5a6..7fd8068b23 100644 --- a/vlib/crypto/aes/aes_cbc.v +++ b/vlib/crypto/aes/aes_cbc.v @@ -31,7 +31,7 @@ fn _new_cbc(b AesCipher, iv []byte) AesCbc { b: b, block_size: b.block_size(), iv: iv.clone(), - tmp: [byte(0); b.block_size()], + tmp: [byte(0)].repeat(b.block_size()), } } diff --git a/vlib/crypto/aes/cypher_generic.v b/vlib/crypto/aes/cypher_generic.v index c9eb191c07..b8eb5379d8 100644 --- a/vlib/crypto/aes/cypher_generic.v +++ b/vlib/crypto/aes/cypher_generic.v @@ -5,8 +5,8 @@ module aes import ( - crypto.cipher - crypto.internal.subtle + // crypto.cipher + // crypto.internal.subtle ) // new_cipher_generic creates and returns a new cipher.Block diff --git a/vlib/freetype/freetype.v b/vlib/freetype/freetype.v index e5b474135d..6534b58f60 100644 --- a/vlib/freetype/freetype.v +++ b/vlib/freetype/freetype.v @@ -8,7 +8,6 @@ import ( os gx gg - stbi glm gl ) diff --git a/vlib/gl/1shader.v b/vlib/gl/1shader.v index fdc4cc3278..1ccf739163 100644 --- a/vlib/gl/1shader.v +++ b/vlib/gl/1shader.v @@ -4,7 +4,7 @@ module gl -import os +// import os import gx import glm diff --git a/vlib/gl/gl.v b/vlib/gl/gl.v index 25167cc8fe..e2324ead15 100644 --- a/vlib/gl/gl.v +++ b/vlib/gl/gl.v @@ -8,6 +8,9 @@ module gl #include "glad.h" #flag @VROOT/thirdparty/glad/glad.o +// joe-c: fix & remove +enum TmpGlImportHack{} + pub fn init_glad() { ok := C.gladLoadGL() if isnil(ok) { diff --git a/vlib/glfw/glfw.v b/vlib/glfw/glfw.v index 38e9d72139..abd95ce547 100644 --- a/vlib/glfw/glfw.v +++ b/vlib/glfw/glfw.v @@ -4,6 +4,8 @@ module glfw +// note: we might need special case for this +// see TmpGlImportHack below (joe-c) import gl #flag -I @VROOT/thirdparty/glfw @@ -41,6 +43,11 @@ const ( KeyDown = 264 ) +// joe-c: fix & remove +struct TmpGlImportHack { + hack gl.TmpGlImportHack +} + struct WinCfg { width int height int diff --git a/vlib/glm/glm_test.v b/vlib/glm/glm_test.v index 05caa649e9..d73aea9a00 100644 --- a/vlib/glm/glm_test.v +++ b/vlib/glm/glm_test.v @@ -2,8 +2,8 @@ // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. -import os -import gl +// might need special case for this +// import gl import glm fn cmp(a, b f32) bool { diff --git a/vlib/http/download_nix.v b/vlib/http/download_nix.v index 11390b632f..07b0a2b67a 100644 --- a/vlib/http/download_nix.v +++ b/vlib/http/download_nix.v @@ -4,8 +4,6 @@ module http -import os - type downloadfn fn (written int) type download_finished_fn fn () diff --git a/vlib/http/http_test.v b/vlib/http/http_test.v index 81abfda343..2b72075889 100644 --- a/vlib/http/http_test.v +++ b/vlib/http/http_test.v @@ -1,5 +1,5 @@ -import net.urllib -import http +// import net.urllib +// import http fn test_escape_unescape() { /* diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index de2406942c..852fbed143 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -4,7 +4,8 @@ module stbi -import gl +// note we might need special case for this +// import gl #flag -I @VROOT/thirdparty/stb_image diff --git a/vlib/sync/sync_win.v b/vlib/sync/sync_win.v index ebe7c3a6e5..447d541ee9 100644 --- a/vlib/sync/sync_win.v +++ b/vlib/sync/sync_win.v @@ -3,7 +3,6 @@ // that can be found in the LICENSE file. module sync -import os // Mutex HANDLE type MHANDLE voidptr diff --git a/vlib/term/colors.v b/vlib/term/colors.v index b382ffd277..541d0e164c 100644 --- a/vlib/term/colors.v +++ b/vlib/term/colors.v @@ -4,8 +4,6 @@ module term -import os - fn _format(msg, open, close string) string { return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm' } diff --git a/vlib/term/colors_win.v b/vlib/term/colors_win.v index 85dcfd44e1..efbc5313ec 100644 --- a/vlib/term/colors_win.v +++ b/vlib/term/colors_win.v @@ -4,8 +4,6 @@ module term -import os - pub fn format(msg, open, close string) string { return _format(msg, open, close) } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 887a452a00..c0e2b766aa 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -2,7 +2,6 @@ module vweb import ( os - strings net http net.urllib