1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix generics array of threads with multiple types (#18137)

This commit is contained in:
yuyi 2023-05-08 18:24:28 +08:00 committed by GitHub
parent 87623b367c
commit 72b2f22057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 14 deletions

View File

@ -2088,16 +2088,13 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
mut use_tmp_fn_var := false mut use_tmp_fn_var := false
tmp_fn := g.new_tmp_var() tmp_fn := g.new_tmp_var()
for i, concrete_type in expr.concrete_types { if expr.concrete_types.len > 0 {
if concrete_type != ast.void_type && concrete_type != 0 { name = g.generic_fn_name(expr.concrete_types, name)
// Using _T_ to differentiate between get<string> and get_string } else if expr.is_fn_var && expr.fn_var_type.has_flag(.generic) {
// `foo<int>()` => `foo_T_int()` fn_var_type := g.unwrap_generic(expr.fn_var_type)
if i == 0 { name = g.typ(fn_var_type)
name += '_T'
}
name += '_' + g.typ(concrete_type)
}
} }
if expr.is_method { if expr.is_method {
receiver_sym := g.table.sym(g.unwrap_generic(expr.receiver_type)) receiver_sym := g.table.sym(g.unwrap_generic(expr.receiver_type))
name = receiver_sym.cname + '_' + name name = receiver_sym.cname + '_' + name
@ -2143,7 +2140,13 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
wrapper_fn_name := name + '_thread_wrapper' wrapper_fn_name := name + '_thread_wrapper'
arg_tmp_var := 'arg_' + tmp arg_tmp_var := 'arg_' + tmp
g.writeln('${wrapper_struct_name} *${arg_tmp_var} = malloc(sizeof(thread_arg_${name}));') 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_) { if !(expr.is_method && g.table.sym(expr.receiver_type).kind == .interface_) {
g.writeln('${arg_tmp_var}->fn = ${fn_name};') g.writeln('${arg_tmp_var}->fn = ${fn_name};')
} }

View File

@ -7,10 +7,17 @@ fn async_map[T](arr []T, func fn (T) T) []T {
} }
fn test_generic_array_of_threads() { fn test_generic_array_of_threads() {
arr := [1, 2, 3, 4] arr1 := [1, 2, 3, 4]
results := async_map(arr, fn (a int) int { results1 := async_map(arr1, fn (a int) int {
return -a return -a
}) })
println(results) println(results1)
assert results == [-1, -2, -3, -4] 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]
} }