From 5c08dc1c0c60a06bbdeeafccecc2ad274c17ae94 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 24 Dec 2024 20:58:30 -0300 Subject: [PATCH 1/2] fix --- vlib/v/gen/c/spawn_and_go.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/spawn_and_go.v b/vlib/v/gen/c/spawn_and_go.v index 833b7891bdebb3..26baa76e10ace7 100644 --- a/vlib/v/gen/c/spawn_and_go.v +++ b/vlib/v/gen/c/spawn_and_go.v @@ -26,7 +26,7 @@ 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 + mut name := '${expr.name}_${expr.pos.pos}' mut use_tmp_fn_var := false tmp_fn := g.new_tmp_var() From 2c5130f1281ee72f5ab3ce3e7dce87273e7a48a1 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 24 Dec 2024 21:08:49 -0300 Subject: [PATCH 2/2] fix --- vlib/v/gen/c/spawn_and_go.v | 7 ++++++- vlib/v/tests/chan_same_fn_name_test.v | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/chan_same_fn_name_test.v 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' +}