From da65680acf03ad838821c3289f667ee64c0074bd Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 1 Nov 2021 15:41:04 +0800 Subject: [PATCH] checker: fix comptime if expr of sync.threads (#12356) --- vlib/v/checker/checker.v | 2 +- vlib/v/tests/comptime_if_expr_threads_test.v | 27 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/comptime_if_expr_threads_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3feb9352e6..d2073cdd0f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4500,7 +4500,7 @@ fn (mut c Checker) stmt(node ast.Stmt) { stmt_pos := node.pos eprintln('checking file: ${c.file.path:-30} | stmt pos: ${stmt_pos.str():-45} | stmt') } - // c.expected_type = ast.void_type + c.expected_type = ast.void_type match mut node { ast.EmptyStmt { if c.pref.is_verbose { diff --git a/vlib/v/tests/comptime_if_expr_threads_test.v b/vlib/v/tests/comptime_if_expr_threads_test.v new file mode 100644 index 0000000000..6fa6e13752 --- /dev/null +++ b/vlib/v/tests/comptime_if_expr_threads_test.v @@ -0,0 +1,27 @@ +import math { floor, sqrt } +import arrays { sum } + +fn async(arr_size int, init_val f64) f64 { + mut val_arr := []f64{} + for _ in 1 .. arr_size { + val_arr << floor((sqrt(init_val) / 2) * 3) + } + + return sum(val_arr.map(it / 2)) or { f64(1) } +} + +fn test_comptime_if_expr_of_threads() { + size := 2000_000 + + println('Async') + mut results := []thread f64{cap: 16} + for num in 0 .. 15 { + results << go async(size, num) + } + waited_results := results.wait() + + println(waited_results) + sum_result := sum(waited_results) or { 1 } + println(sum_result) + assert sum_result == 4.7999976e+07 +}