diff --git a/doc/docs.md b/doc/docs.md index c33533e151..e29f469912 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -2228,8 +2228,6 @@ a property of the individual channel object. Channels can be passed to coroutine variables: ```v -import sync - fn f(ch chan int) { // ... } @@ -2331,8 +2329,6 @@ if select { For special purposes there are some builtin properties and methods: ```v nofmt -import sync - struct Abc{x int} a := 2.13 diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index c549317319..3d6a9762c3 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -1,5 +1,3 @@ -import sync - fn test_pointer() { mut arr := []&int{} a := 1 diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f1e073b59d..2ece699281 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -461,6 +461,7 @@ pub mut: scope &Scope stmts []Stmt // all the statements in the source file imports []Import // all the imports + auto_imports []string // imports that were implicitely added imported_symbols map[string]string // used for `import {symbol}`, it maps symbol => module.symbol errors []errors.Error // all the checker errors in the file warnings []errors.Warning // all the checker warings in the file diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 617f843db4..b4822bae46 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -38,7 +38,7 @@ pub mut: file ast.File did_imports bool is_assign bool - auto_imports []string // automatically inserted imports that the user forgot to specify + auto_imports []string // automatically inserted imports that the user does not need to specify import_pos int // position of the imports in the resulting string for later autoimports insertion used_imports []string // to remove unused imports is_debug bool @@ -83,6 +83,7 @@ pub fn (mut f Fmt) process_file_imports(file &ast.File) { f.mod2alias[sym.name] = sym.name } } + f.auto_imports = file.auto_imports } pub fn (mut f Fmt) write(s string) { @@ -201,8 +202,8 @@ pub fn (mut f Fmt) imports(imports []ast.Import) { if f.did_imports || imports.len == 0 { return } - // f.import_pos = f.out.len f.did_imports = true + mut num_imports := 0 /* if imports.len == 1 { imp_stmt_str := f.imp_stmt_str(imports[0]) @@ -215,12 +216,16 @@ pub fn (mut f Fmt) imports(imports []ast.Import) { // TODO bring back once only unused imports are removed // continue } - // f.out_imports.write('\t') - // f.out_imports.writeln(f.imp_stmt_str(imp)) + if imp.mod in f.auto_imports && imp.mod !in f.used_imports { + continue + } f.out_imports.write('import ') f.out_imports.writeln(f.imp_stmt_str(imp)) + num_imports++ + } + if num_imports > 0 { + f.out_imports.writeln('') } - f.out_imports.writeln('') // f.out_imports.writeln(')\n') // } } @@ -1678,12 +1683,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { // a `node.left` expression. Import `time` automatically. // TODO fetch all available modules if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64'] { - if node.left.name !in f.auto_imports { - f.auto_imports << node.left.name - f.file.imports << ast.Import{ - mod: node.left.name - alias: node.left.name - } + f.file.imports << ast.Import{ + mod: node.left.name + alias: node.left.name } // for imp in f.file.imports { // println(imp.mod) diff --git a/vlib/v/fmt/tests/chan_init_keep.vv b/vlib/v/fmt/tests/chan_init_keep.vv index 4acca35a9d..e8d00e6d7f 100644 --- a/vlib/v/fmt/tests/chan_init_keep.vv +++ b/vlib/v/fmt/tests/chan_init_keep.vv @@ -1,5 +1,3 @@ -import sync - struct FSMEvent { x int } diff --git a/vlib/v/fmt/tests/shared_expected.vv b/vlib/v/fmt/tests/shared_expected.vv index e973eee05b..9b9ddc25fe 100644 --- a/vlib/v/fmt/tests/shared_expected.vv +++ b/vlib/v/fmt/tests/shared_expected.vv @@ -1,4 +1,3 @@ -import sync import time struct St { diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index 0c6d26d44c..e3739a7135 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -42,6 +42,9 @@ fn (mut p Parser) register_auto_import(alias string) { } p.ast_imports << node } + if alias !in p.auto_imports { + p.auto_imports << alias + } p.register_used_import(alias) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 35e318b52c..4dbac3ae0c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -53,6 +53,7 @@ mut: imports map[string]string // alias => mod_name ast_imports []ast.Import // mod_names used_imports []string // alias + auto_imports []string // imports, the user does not need to specify imported_symbols map[string]string is_amp bool // for generating the right code for `&Foo{}` returns bool @@ -232,6 +233,7 @@ pub fn (mut p Parser) parse() ast.File { mod: module_decl imports: p.ast_imports imported_symbols: p.imported_symbols + auto_imports: p.auto_imports stmts: stmts scope: p.scope global_scope: p.global_scope diff --git a/vlib/x/websocket/io.v b/vlib/x/websocket/io.v index 4ab1005aaa..f8b2da589c 100644 --- a/vlib/x/websocket/io.v +++ b/vlib/x/websocket/io.v @@ -2,7 +2,6 @@ module websocket import net import time -import sync // socket_read reads from socket into the provided buffer fn (mut ws Client) socket_read(mut buffer []byte) ?int { diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index 558afc444c..927255899f 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -7,7 +7,6 @@ import x.openssl import net.urllib import time import log -import sync import rand const ( diff --git a/vlib/x/websocket/websocket_server.v b/vlib/x/websocket/websocket_server.v index 67bd314f31..398c2d82e8 100644 --- a/vlib/x/websocket/websocket_server.v +++ b/vlib/x/websocket/websocket_server.v @@ -3,7 +3,6 @@ module websocket import net import x.openssl import log -import sync import time import rand