diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 185b17b80c..d06ae08cb0 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -116,11 +116,11 @@ mut: defer_stmts []ast.DeferStmt defer_ifdef string defer_profile_code string - str_types []StrType // types that need automatic str() generation - generated_str_fns []StrType // types that already have a str() function - threaded_fns []string // for generating unique wrapper types and fns for `go xxx()` - waiter_fns []string // functions that wait for `go xxx()` to finish - auto_fn_definitions []string // auto generated functions defination list + str_types []StrType // types that need automatic str() generation + generated_str_fns []StrType // types that already have a str() function + threaded_fns shared []string // for generating unique wrapper types and fns for `go xxx()` + waiter_fns []string // functions that wait for `go xxx()` to finish + auto_fn_definitions []string // auto generated functions defination list sumtype_casting_fns []SumtypeCastingFn anon_fn_definitions []string // anon generated functions defination list sumtype_definitions map[int]bool // `_TypeA_to_sumtype_TypeB()` fns that have been generated @@ -298,6 +298,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string { inner_loop: &ast.EmptyStmt{} field_data_type: ast.Type(global_g.table.find_type_idx('FieldData')) array_sort_fn: global_g.array_sort_fn + threaded_fns: global_g.threaded_fns done_optionals: global_g.done_optionals is_autofree: global_g.pref.autofree } @@ -370,7 +371,6 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string { global_g.nr_closures += g.nr_closures global_g.has_main = global_g.has_main || g.has_main - global_g.threaded_fns << g.threaded_fns global_g.waiter_fns << g.waiter_fns global_g.auto_fn_definitions << g.auto_fn_definitions global_g.anon_fn_definitions << g.anon_fn_definitions @@ -6668,7 +6668,14 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { } } // Register the wrapper type and function - if name !in g.threaded_fns { + mut should_register := false + lock g.threaded_fns { + if name !in g.threaded_fns { + g.threaded_fns << name + should_register = true + } + } + if should_register { g.type_definitions.writeln('\ntypedef struct $wrapper_struct_name {') if expr.is_method { styp := g.typ(expr.receiver_type) @@ -6762,7 +6769,6 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { g.gowrappers.writeln('\treturn 0;') } g.gowrappers.writeln('}') - g.threaded_fns << name } if node.is_expr { g.empty_line = false diff --git a/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f1.v b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f1.v new file mode 100644 index 0000000000..71fa28cdb0 --- /dev/null +++ b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f1.v @@ -0,0 +1,6 @@ +module main + +fn f1() { + x := Abc{111} + go f(x) +} diff --git a/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f2.v b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f2.v new file mode 100644 index 0000000000..2facc29762 --- /dev/null +++ b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/f2.v @@ -0,0 +1,6 @@ +module main + +fn f2() { + x := Abc{222} + go f(x) +} diff --git a/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/main.v b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/main.v new file mode 100644 index 0000000000..cc92bba179 --- /dev/null +++ b/vlib/v/tests/run_project_folders/go_fns_in_different_files_over_the_same_struct/main.v @@ -0,0 +1,18 @@ +module main + +import time + +struct Abc { + x int +} + +fn f(x Abc) { + println(x) +} + +fn main() { + f1() + f2() + time.sleep(1 * time.second) + println('OK') +} diff --git a/vlib/v/tests/run_project_folders_test.v b/vlib/v/tests/run_project_folders_test.v index 866def9889..f7c2690f2f 100644 --- a/vlib/v/tests/run_project_folders_test.v +++ b/vlib/v/tests/run_project_folders_test.v @@ -24,13 +24,13 @@ fn test_v_profile_works() ? { for folder_path in folder_paths { local_path := folder_path.replace(vroot + os.path_separator, '').replace('\\', '/') - println('..... $local_path/') + println('..... v run $local_path/') res := os.execute('"$vexe" run $folder_path') // eprintln('res: $res') assert res.exit_code == 0 assert res.output.len > 0 assert res.output.contains('OK') term.clear_previous_line() - println('${term.bold('OK')} $local_path/') + println('${term.bold('OK')} v run $local_path/') } }