diff --git a/vlib/v/gen/c/spawn_and_go.v b/vlib/v/gen/c/spawn_and_go.v index 26baa76e10ace7..f16285eaba1135 100644 --- a/vlib/v/gen/c/spawn_and_go.v +++ b/vlib/v/gen/c/spawn_and_go.v @@ -26,10 +26,15 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) { mut handle := '' tmp := g.new_tmp_var() mut expr := node.call_expr - mut name := '${expr.name}_${expr.pos.pos}' + mut name := expr.name mut use_tmp_fn_var := false tmp_fn := g.new_tmp_var() + if expr.is_fn_var { + // generate a name different for same var fn name declared in another scope + name = '${name}_${node.pos.pos}' + } + 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) { diff --git a/vlib/v/tests/chan_same_fn_name_test.v b/vlib/v/tests/chan_same_fn_name_test.v new file mode 100644 index 00000000000000..fee61dd9152d81 --- /dev/null +++ b/vlib/v/tests/chan_same_fn_name_test.v @@ -0,0 +1,24 @@ +fn a() chan string { + ch_out := chan string{} + f := fn (a chan string) { + a <- 'foo' + } + spawn f(ch_out) + return ch_out +} + +fn b(ch_in chan string) string { + f := fn (a chan string, b chan string) { + val := <-a + {} + b <- val + } + ch_out := chan string{} + spawn f(ch_in, ch_out) + return <-ch_out +} + +fn test_main() { + ch0 := a() + assert b(ch0) == 'foo' +}