From 5674d46965cb1dd953164ec1c99af583fa567c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Tue, 23 Feb 2021 08:37:29 +0100 Subject: [PATCH] table,cgen: fix bug preventing `t := []thread{}` to compile (#8913) --- vlib/v/gen/c/cgen.v | 18 +++++++++++------- vlib/v/table/table.v | 6 ++++++ vlib/v/table/types.v | 10 +++++++++- vlib/v/tests/go_array_wait_test.v | 13 +++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9289bee7b3..861c8ae1cc 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -620,18 +620,19 @@ fn (mut g Gen) find_or_register_shared(t table.Type, base string) string { } fn (mut g Gen) register_thread_array_wait_call(eltyp string) string { - thread_typ := '__v_thread_$eltyp' - ret_typ := if eltyp == '' { 'void' } else { 'Array_$eltyp' } + is_void := eltyp == 'void' + thread_typ := if is_void { '__v_thread' } else { '__v_thread_$eltyp' } + ret_typ := if is_void { 'void' } else { 'Array_$eltyp' } thread_arr_typ := 'Array_$thread_typ' fn_name := '${thread_arr_typ}_wait' if fn_name !in g.waiter_fns { g.waiter_fns << fn_name - if eltyp == 'void' { + if is_void { g.gowrappers.writeln(' void ${fn_name}($thread_arr_typ a) { for (int i = 0; i < a.len; ++i) { $thread_typ t = (($thread_typ*)a.data)[i]; - __v_thread_${eltyp}_wait(t); + __v_thread_wait(t); } }') } else { @@ -5376,7 +5377,7 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) { } table.Thread { if g.pref.os == .windows { - if name == '__v_thread_void' { + if name == '__v_thread' { g.type_definitions.writeln('typedef HANDLE $name;') } else { // Windows can only return `u32` (no void*) from a thread, so the @@ -5869,8 +5870,11 @@ fn (mut g Gen) go_stmt(node ast.GoStmt, joinable bool) string { if g.pref.os == .windows && node.call_expr.return_type != table.void_type { g.writeln('$arg_tmp_var->ret_ptr = malloc(sizeof($s_ret_typ));') } - gohandle_name := '__v_thread_' + - g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname + gohandle_name := if node.call_expr.return_type == table.void_type { + '__v_thread' + } else { + '__v_thread_' + g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname + } if g.pref.os == .windows { simple_handle := if joinable && node.call_expr.return_type != table.void_type { 'thread_handle_$tmp' diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 6a4253fda6..0ab7bf72d1 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -487,6 +487,9 @@ pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string { [inline] pub fn (t &Table) thread_name(return_type Type) string { + if return_type == void_type { + return 'thread' + } return_type_sym := t.get_type_symbol(return_type) ptr := if return_type.is_ptr() { '&' } else { '' } return 'thread $ptr$return_type_sym.name' @@ -494,6 +497,9 @@ pub fn (t &Table) thread_name(return_type Type) string { [inline] pub fn (t &Table) thread_cname(return_type Type) string { + if return_type == void_type { + return '__v_thread' + } return_type_sym := t.get_type_symbol(return_type) suffix := if return_type.is_ptr() { '_ptr' } else { '' } return '__v_thread_$return_type_sym.cname$suffix' diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index b08cb233e7..ced58ffaca 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -540,7 +540,15 @@ pub fn (mut t Table) register_builtin_type_symbols() { cname: 'int_literal' mod: 'builtin' ) - t.register_type_symbol(kind: .thread, name: 'thread', cname: '__v_thread', mod: 'builtin') + t.register_type_symbol( + kind: .thread + name: 'thread' + cname: '__v_thread' + mod: 'builtin' + info: Thread{ + return_type: table.void_type + } + ) } [inline] diff --git a/vlib/v/tests/go_array_wait_test.v b/vlib/v/tests/go_array_wait_test.v index 5a5efbca15..af630ad3bb 100644 --- a/vlib/v/tests/go_array_wait_test.v +++ b/vlib/v/tests/go_array_wait_test.v @@ -36,3 +36,16 @@ fn test_array_thread_void_wait() { } } +fn test_void_thread_decl() { + shared a := [2 3 9] + mut t1 := thread(0) + mut tarr := []thread{len: 2} + t1 = go g(shared a, 0) + tarr[0] = go g(shared a, 1) + tarr[1] = go g(shared a, 2) + t1.wait() + tarr.wait() + rlock a { + assert a == [6, 12, 90] + } +}