Skip to content

Commit

Permalink
cgen: fix thread struct name for same fn var name on different scope (f…
Browse files Browse the repository at this point in the history
…ix #21746) (#23261)
  • Loading branch information
felipensp authored Dec 25, 2024
1 parent 4cd3009 commit f27181e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/spawn_and_go.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
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) {
Expand Down
24 changes: 24 additions & 0 deletions vlib/v/tests/chan_same_fn_name_test.v
Original file line number Diff line number Diff line change
@@ -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'
}

0 comments on commit f27181e

Please sign in to comment.