Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix thread struct name for same fn var name on different scope #23261

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
Loading