From 72b2f22057654ae7a8bd4279aeeb4d16c37a9f37 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 8 May 2023 18:24:28 +0800 Subject: [PATCH] cgen: fix generics array of threads with multiple types (#18137) --- vlib/v/gen/c/fn.v | 23 +++++++++++-------- vlib/v/tests/generics_array_of_threads_test.v | 15 ++++++++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 7f310f63a6..a88a86e428 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2088,16 +2088,13 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { mut use_tmp_fn_var := false tmp_fn := g.new_tmp_var() - for i, concrete_type in expr.concrete_types { - if concrete_type != ast.void_type && concrete_type != 0 { - // Using _T_ to differentiate between get and get_string - // `foo()` => `foo_T_int()` - if i == 0 { - name += '_T' - } - name += '_' + g.typ(concrete_type) - } + if expr.concrete_types.len > 0 { + name = g.generic_fn_name(expr.concrete_types, name) + } else if expr.is_fn_var && expr.fn_var_type.has_flag(.generic) { + fn_var_type := g.unwrap_generic(expr.fn_var_type) + name = g.typ(fn_var_type) } + if expr.is_method { receiver_sym := g.table.sym(g.unwrap_generic(expr.receiver_type)) name = receiver_sym.cname + '_' + name @@ -2143,7 +2140,13 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { wrapper_fn_name := name + '_thread_wrapper' arg_tmp_var := 'arg_' + tmp g.writeln('${wrapper_struct_name} *${arg_tmp_var} = malloc(sizeof(thread_arg_${name}));') - fn_name := if use_tmp_fn_var { tmp_fn } else { name } + fn_name := if use_tmp_fn_var { + tmp_fn + } else if expr.is_fn_var { + expr.name + } else { + name + } if !(expr.is_method && g.table.sym(expr.receiver_type).kind == .interface_) { g.writeln('${arg_tmp_var}->fn = ${fn_name};') } diff --git a/vlib/v/tests/generics_array_of_threads_test.v b/vlib/v/tests/generics_array_of_threads_test.v index e56039ebae..1691c4042d 100644 --- a/vlib/v/tests/generics_array_of_threads_test.v +++ b/vlib/v/tests/generics_array_of_threads_test.v @@ -7,10 +7,17 @@ fn async_map[T](arr []T, func fn (T) T) []T { } fn test_generic_array_of_threads() { - arr := [1, 2, 3, 4] - results := async_map(arr, fn (a int) int { + arr1 := [1, 2, 3, 4] + results1 := async_map(arr1, fn (a int) int { return -a }) - println(results) - assert results == [-1, -2, -3, -4] + println(results1) + assert results1 == [-1, -2, -3, -4] + + arr2 := [1.0, 2.0, 3.0, 4.0] + results2 := async_map(arr2, fn (a f64) f64 { + return -a + }) + println(results2) + assert results2 == [-1.0, -2.0, -3.0, -4.0] }