From 298a2a2d7f0524e867c7e0ca996c9a4a69f133d5 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 17 Feb 2024 06:36:40 -0300 Subject: [PATCH] cgen: fix thread return type generation (fix #20836) (#20850) --- vlib/v/gen/c/spawn_and_go.v | 5 ++--- vlib/v/tests/thread_ptr_ret_test.v | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/thread_ptr_ret_test.v diff --git a/vlib/v/gen/c/spawn_and_go.v b/vlib/v/gen/c/spawn_and_go.v index 1d7cf696d3..fe302e6cdb 100644 --- a/vlib/v/gen/c/spawn_and_go.v +++ b/vlib/v/gen/c/spawn_and_go.v @@ -122,9 +122,8 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) { gohandle_name = '__v_thread' } } else { - opt := if is_opt { '${option_name}_' } else { '' } - res := if is_res { '${result_name}_' } else { '' } - gohandle_name = '__v_thread_${opt}${res}${g.table.sym(g.unwrap_generic(node.call_expr.return_type)).cname}' + ret_styp := g.typ(g.unwrap_generic(node.call_expr.return_type)).replace('*', '_ptr') + gohandle_name = '__v_thread_${ret_styp}' } if is_spawn { if g.pref.os == .windows { diff --git a/vlib/v/tests/thread_ptr_ret_test.v b/vlib/v/tests/thread_ptr_ret_test.v new file mode 100644 index 0000000000..6d5f177c6f --- /dev/null +++ b/vlib/v/tests/thread_ptr_ret_test.v @@ -0,0 +1,21 @@ +fn test_main() { + foo := spawn get_pointer() + ret := foo.wait() + assert *ret == 42 +} + +fn test_opt() { + foo := spawn get_pointer_opt() + ret := foo.wait() + assert *ret? == 42 +} + +fn get_pointer() &int { + val := 42 + return &val +} + +fn get_pointer_opt() ?&int { + val := 42 + return &val +}