From 4acddd37b04e7b378b11b7a100ba5c51b844a8f5 Mon Sep 17 00:00:00 2001 From: Jengro Date: Sat, 27 Jun 2026 05:53:33 +0800 Subject: [PATCH] markused: fix generic fn instantiation dedup key collision for identical module names (#27399) --- vlib/v/markused/walker.v | 2 +- ...eric_fn_dedup_collision_issue_27398_test.v | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generic_fn_dedup_collision_issue_27398_test.v diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 2b9fce094..76db5e7fc 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -510,7 +510,7 @@ fn (mut w Walker) record_used_fn_generic_types(fkey string, concrete_types []ast fn (w &Walker) fn_generic_types_key(fkey string, concrete_types []ast.Type) string { mut parts := []string{cap: concrete_types.len} for typ in concrete_types { - parts << w.table.type_to_str(typ) + parts << u32(typ).str() } return '${fkey}:${parts.join('|')}' } diff --git a/vlib/v/tests/generic_fn_dedup_collision_issue_27398_test.v b/vlib/v/tests/generic_fn_dedup_collision_issue_27398_test.v new file mode 100644 index 000000000..46fbaf13f --- /dev/null +++ b/vlib/v/tests/generic_fn_dedup_collision_issue_27398_test.v @@ -0,0 +1,70 @@ +import os + +fn write_file(path string, content string) { + os.write_file(path, content) or { panic(err) } +} + +fn test_generic_fn_dedup_collision_issue_27398() { + n_pairs := 24 + tmp := os.join_path(os.vtmp_dir(), 'v_issue_27398_${os.getpid()}') + os.rmdir_all(tmp) or {} + defer { + os.rmdir_all(tmp) or {} + } + + os.mkdir_all(os.join_path(tmp, 'api')) or { panic(err) } + write_file(os.join_path(tmp, 'api', 'common.v'), 'module api + +pub struct MyStruct[T] { +pub: + value T +} + +pub fn make_struct[T](val T) MyStruct[T] { + return MyStruct[T]{value: val} +} +') + + common_mod := 'module common + +import api + +pub struct Type_a { pub: name string } +pub struct Type_b { pub: age int } + +pub fn call_api() { + a := Type_a{name: "hello"} + b := Type_b{age: 42} + _ = api.make_struct(a) + _ = api.make_struct(b) +} +' + + mut main_mod := 'module main\n\n' + for i in 0 .. n_pairs { + for _, s in ['a', 'b'] { + dir := os.join_path(tmp, 'm${i}${s}', 'common') + os.mkdir_all(dir) or { panic(err) } + write_file(os.join_path(dir, 'types.v'), common_mod) + main_mod += 'import m${i}${s}.common as m${i}${s}\n' + } + } + main_mod += '\nfn main() {\n' + for i in 0 .. n_pairs { + for _, s in ['a', 'b'] { + main_mod += '\tm${i}${s}.call_api()\n' + } + } + main_mod += '\tprintln("ok")\n}\n' + write_file(os.join_path(tmp, 'main.v'), main_mod) + + old_wd := os.getwd() + os.chdir(tmp) or { panic(err) } + defer { + os.chdir(old_wd) or { panic(err) } + } + res := os.execute('${os.quoted_path(@VEXE)} -skip-unused run .') + + assert res.exit_code == 0, 'compilation failed:\n${res.output}' + assert res.output.trim_space() == 'ok' +} -- 2.39.5