From cbcba2e4cfe443fb8557854f935c9dc786f6f409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Mon, 31 Aug 2020 10:44:39 +0200 Subject: [PATCH] parser: enable module auto import (of `sync`) (#6271) --- vlib/sync/bench/channel_bench_v.v | 25 ++++++++----------- vlib/sync/channel_1_test.v | 14 ++++------- vlib/sync/channel_2_test.v | 14 ++++------- vlib/sync/channel_3_test.v | 38 ++++++++++++----------------- vlib/sync/channel_4_test.v | 38 ++++++++++++----------------- vlib/v/builder/compile.v | 2 +- vlib/v/parser/lock.v | 2 +- vlib/v/parser/module.v | 16 ++++++++++++ vlib/v/parser/parse_type.v | 6 +++-- vlib/v/parser/pratt.v | 18 ++++++++++---- vlib/v/tests/autolock_array1_test.v | 1 - vlib/v/tests/shared_array_test.v | 1 - vlib/v/tests/shared_lock_2_test.v | 1 - vlib/v/tests/shared_lock_3_test.v | 1 - vlib/v/tests/shared_lock_4_test.v | 1 - vlib/v/tests/shared_lock_test.v | 1 - vlib/v/util/util.v | 2 +- 17 files changed, 88 insertions(+), 93 deletions(-) diff --git a/vlib/sync/bench/channel_bench_v.v b/vlib/sync/bench/channel_bench_v.v index f390bfdb8..d22bcdc02 100644 --- a/vlib/sync/bench/channel_bench_v.v +++ b/vlib/sync/bench/channel_bench_v.v @@ -6,24 +6,21 @@ // The receive threads add all received numbers and send them to the // main thread where the total sum is compare to the expected value. -import sync import time import os -fn do_rec(mut ch sync.Channel, mut resch sync.Channel, n int) { +fn do_rec(ch chan int, resch chan i64, n int) { mut sum := i64(0) for _ in 0 .. n { - mut a := 0 - ch.pop(&a) - sum += a + sum += <-ch } println(sum) - resch.push(&sum) + resch <- sum } -fn do_send(mut ch sync.Channel, start, end int) { +fn do_send(ch chan int, start, end int) { for i in start .. end { - ch.push(&i) + ch <- i } } @@ -37,12 +34,12 @@ fn main() { buflen := os.args[3].int() nobj := os.args[4].int() stopwatch := time.new_stopwatch({}) - mut ch := sync.new_channel(buflen) - mut resch := sync.new_channel(0) + ch := chan int{cap: buflen} + resch := chan i64{} mut no := nobj for i in 0 .. nrec { n := no / (nrec - i) - go do_rec(mut ch, mut resch, n) + go do_rec(ch, resch, n) no -= n } assert no == 0 @@ -51,14 +48,12 @@ fn main() { n := no / (nsend - i) end := no no -= n - go do_send(mut ch, no, end) + go do_send(ch, no, end) } assert no == 0 mut sum := i64(0) for _ in 0 .. nrec { - mut r := i64(0) - resch.pop(&r) - sum += r + sum += <-resch } elapsed := stopwatch.elapsed() rate := f64(nobj)/elapsed*time.microsecond diff --git a/vlib/sync/channel_1_test.v b/vlib/sync/channel_1_test.v index 8fe7c9b5d..5d8c96861 100644 --- a/vlib/sync/channel_1_test.v +++ b/vlib/sync/channel_1_test.v @@ -1,23 +1,19 @@ -import sync - const ( num_iterations = 10000 ) -fn do_send(mut ch sync.Channel) { +fn do_send(ch chan int) { for i in 0 .. num_iterations { - ch.push(&i) + ch <- i } } fn test_channel_buffered() { - mut ch := sync.new_channel(1000) - go do_send(mut ch) + ch := chan int{cap: 1000} + go do_send(ch) mut sum := i64(0) for _ in 0 .. num_iterations { - a := 0 - ch.pop(&a) - sum += a + sum += <-ch } assert sum == u64(num_iterations)*(num_iterations-1)/2 } diff --git a/vlib/sync/channel_2_test.v b/vlib/sync/channel_2_test.v index be52470f8..acf624505 100644 --- a/vlib/sync/channel_2_test.v +++ b/vlib/sync/channel_2_test.v @@ -1,23 +1,19 @@ -import sync - const ( num_iterations = 10000 ) -fn do_send(mut ch sync.Channel) { +fn do_send(ch chan int) { for i in 0 .. num_iterations { - ch.push(&i) + ch <- i } } fn test_channel_unbuffered() { - mut ch := sync.new_channel(0) - go do_send(mut ch) + ch := chan int{} + go do_send(ch) mut sum := i64(0) for _ in 0 .. num_iterations { - a := 0 - ch.pop(&a) - sum += a + sum += <-ch } assert sum == u64(num_iterations)*(num_iterations-1)/2 } diff --git a/vlib/sync/channel_3_test.v b/vlib/sync/channel_3_test.v index 40781261f..d07276b0d 100644 --- a/vlib/sync/channel_3_test.v +++ b/vlib/sync/channel_3_test.v @@ -1,38 +1,32 @@ -import sync - -fn do_rec(mut ch sync.Channel, mut resch sync.Channel) { +fn do_rec(ch chan int, resch chan i64) { mut sum := i64(0) for _ in 0 .. 2000 { - mut a := 0 - ch.pop(&a) - sum += a + sum += <-ch } println(sum) - resch.push(&sum) + resch <- sum } -fn do_send(mut ch sync.Channel) { +fn do_send(ch chan int) { for i in 0 .. 2000 { - ch.push(&i) + ch <- i } } fn test_channel_multi_unbuffered() { - mut ch := sync.new_channel(0) - mut resch := sync.new_channel(0) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_send(mut ch) - go do_send(mut ch) - go do_send(mut ch) - go do_send(mut ch) + ch := chan int{} + resch := chan i64{} + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_send(ch) + go do_send(ch) + go do_send(ch) + go do_send(ch) mut sum := i64(0) for _ in 0 .. 4 { - mut r := i64(0) - resch.pop(&r) - sum += r + sum += <-resch } assert sum == i64(4) * 2000 * (2000 - 1) / 2 } diff --git a/vlib/sync/channel_4_test.v b/vlib/sync/channel_4_test.v index 2348bcff7..379266870 100644 --- a/vlib/sync/channel_4_test.v +++ b/vlib/sync/channel_4_test.v @@ -1,38 +1,32 @@ -import sync - -fn do_rec(mut ch sync.Channel, mut resch sync.Channel) { +fn do_rec(ch chan int, resch chan i64) { mut sum := i64(0) for _ in 0 .. 2000 { - mut a := 0 - ch.pop(&a) - sum += a + sum += <-ch } println(sum) - resch.push(&sum) + resch <- sum } -fn do_send(mut ch sync.Channel) { +fn do_send(ch chan int) { for i in 0 .. 2000 { - ch.push(&i) + ch <- i } } fn test_channel_multi_buffered() { - mut ch := sync.new_channel(100) - mut resch := sync.new_channel(0) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_rec(mut ch, mut resch) - go do_send(mut ch) - go do_send(mut ch) - go do_send(mut ch) - go do_send(mut ch) + ch := chan int{cap: 100} + resch := chan i64{} + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_rec(ch, resch) + go do_send(ch) + go do_send(ch) + go do_send(ch) + go do_send(ch) mut sum := i64(0) for _ in 0 .. 4 { - mut r := i64(0) - resch.pop(&r) - sum += r + sum += <-resch } assert sum == i64(4) * 2000 * (2000 - 1) / 2 } diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 7eb91a963..d04c885c9 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -197,7 +197,7 @@ Did you forget to add vlib to the path? (Use @vlib for default vlib)') } pub fn (v &Builder) get_user_files() []string { - if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash', 'vlib/time'] { + if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash'] { // This means we are building a builtin module with `v build-module vlib/strings` etc // get_builtin_files() has already added the files in this module, // do nothing here to avoid duplicate definition errors. diff --git a/vlib/v/parser/lock.v b/vlib/v/parser/lock.v index 1873e1e31..31a38b406 100644 --- a/vlib/v/parser/lock.v +++ b/vlib/v/parser/lock.v @@ -5,7 +5,7 @@ import v.table fn (mut p Parser) lock_expr() ast.LockExpr { // TODO Handle aliasing sync - p.register_used_import('sync') + p.register_auto_import('sync') pos := p.tok.position() is_rlock := p.tok.kind == .key_rlock p.next() diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index 88b2dbc52..19857c7b4 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -3,6 +3,8 @@ // that can be found in the LICENSE file. module parser +import v.ast + // return true if file being parsed imports `mod` pub fn (p &Parser) known_import(mod string) bool { return mod in p.imports @@ -29,6 +31,20 @@ fn (mut p Parser) register_used_import(alias string) { } } +fn (mut p Parser) register_auto_import(alias string) { + if alias !in p.imports { + p.imports[alias] = alias + p.table.imports << alias + node := ast.Import{ + pos: p.tok.position() + mod: alias + alias: alias + } + p.ast_imports << node + } + p.register_used_import(alias) +} + fn (mut p Parser) check_unused_imports() { if p.pref.is_repl || p.pref.is_fmt { // The REPL should be much more liberal, and should not warn about diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index af4e85ad6..fe37005f9 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -52,10 +52,12 @@ pub fn (mut p Parser) parse_map_type() table.Type { } pub fn (mut p Parser) parse_chan_type() table.Type { - p.next() - if p.tok.kind != .name && p.tok.kind != .key_mut && p.tok.kind != .amp { + if p.peek_tok.kind != .name && p.peek_tok.kind != .key_mut && p.peek_tok.kind != .amp { + p.next() return table.chan_type } + p.register_auto_import('sync') + p.next() elem_type := p.parse_type() idx := p.table.find_or_register_chan(elem_type) return table.new_type(idx) diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index b5be2a959..ec5876cb4 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -282,6 +282,9 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr { op := p.tok.kind + if op == .arrow { + p.register_auto_import('sync') + } // mut typ := p. // println('infix op=$op.str()') precedence := p.tok.precedence() @@ -310,6 +313,9 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { if op == .amp { p.is_amp = true } + if op == .arrow { + p.register_auto_import('sync') + } // if op == .mul && !p.inside_unsafe { // p.warn('unsafe') // } @@ -322,7 +328,8 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { mut or_stmts := []ast.Stmt{} mut or_kind := ast.OrKind.absent // allow `x := <-ch or {...}` to handle closed channel - if op == .arrow && p.tok.kind == .key_orelse { + if op == .arrow { + if p.tok.kind == .key_orelse { p.next() p.open_scope() p.scope.register('errcode', ast.Var{ @@ -340,10 +347,11 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { or_kind = .block or_stmts = p.parse_block_no_scope(false) p.close_scope() - } - if p.tok.kind == .question { - p.next() - or_kind = .propagate + } + if p.tok.kind == .question { + p.next() + or_kind = .propagate + } } return ast.PrefixExpr{ op: op diff --git a/vlib/v/tests/autolock_array1_test.v b/vlib/v/tests/autolock_array1_test.v index 5ad8e7a8e..5d8637a34 100644 --- a/vlib/v/tests/autolock_array1_test.v +++ b/vlib/v/tests/autolock_array1_test.v @@ -1,4 +1,3 @@ -import sync import time const ( diff --git a/vlib/v/tests/shared_array_test.v b/vlib/v/tests/shared_array_test.v index 966ee2653..6f0454be5 100644 --- a/vlib/v/tests/shared_array_test.v +++ b/vlib/v/tests/shared_array_test.v @@ -1,4 +1,3 @@ -import sync import time fn incr(shared foo []int, index int) { diff --git a/vlib/v/tests/shared_lock_2_test.v b/vlib/v/tests/shared_lock_2_test.v index 5944fd589..9785cb315 100644 --- a/vlib/v/tests/shared_lock_2_test.v +++ b/vlib/v/tests/shared_lock_2_test.v @@ -1,4 +1,3 @@ -import sync import time struct St { diff --git a/vlib/v/tests/shared_lock_3_test.v b/vlib/v/tests/shared_lock_3_test.v index ad6a0682f..c04539026 100644 --- a/vlib/v/tests/shared_lock_3_test.v +++ b/vlib/v/tests/shared_lock_3_test.v @@ -1,4 +1,3 @@ -import sync import time struct St { diff --git a/vlib/v/tests/shared_lock_4_test.v b/vlib/v/tests/shared_lock_4_test.v index e973eee05..9b9ddc25f 100644 --- a/vlib/v/tests/shared_lock_4_test.v +++ b/vlib/v/tests/shared_lock_4_test.v @@ -1,4 +1,3 @@ -import sync import time struct St { diff --git a/vlib/v/tests/shared_lock_test.v b/vlib/v/tests/shared_lock_test.v index 29feedf95..7f3b4ec2a 100644 --- a/vlib/v/tests/shared_lock_test.v +++ b/vlib/v/tests/shared_lock_test.v @@ -1,4 +1,3 @@ -import sync import time struct St { diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 5735e7bbf..ee46da5cb 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -13,7 +13,7 @@ pub const ( // math.bits is needed by strconv.ftoa pub const ( - builtin_module_parts = ['math.bits', 'strconv', 'strconv.ftoa', 'hash', 'strings', 'time', 'builtin'] + builtin_module_parts = ['math.bits', 'strconv', 'strconv.ftoa', 'hash', 'strings', 'builtin'] ) pub const ( -- 2.30.2