diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 9fdf71dc88..b5debbfda5 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -60,9 +60,10 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) { } mut type_name := g.typ(it.return_type) if g.cur_generic_type != 0 { - // foo() => foo_int(), foo_string() etc + // foo() => foo_T_int(), foo_T_string() etc gen_name := g.typ(g.cur_generic_type) - name += '_' + gen_name + // Using _T_ to differentiate between get and get_string + name += '_T_' + gen_name } // if g.pref.show_cc && it.is_builtin { // println(name) @@ -534,8 +535,9 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { name = c_name(name) } if node.generic_type != table.void_type && node.generic_type != 0 { - // `foo()` => `foo_int()` - name += '_' + g.typ(node.generic_type) + // Using _T_ to differentiate between get and get_string + // `foo()` => `foo_T_int()` + name += '_T_' + g.typ(node.generic_type) } // TODO2 // cgen shouldn't modify ast nodes, this should be moved diff --git a/vlib/v/tests/generic_functions_with_normal_function_test.v b/vlib/v/tests/generic_functions_with_normal_function_test.v new file mode 100644 index 0000000000..45ab2c42b2 --- /dev/null +++ b/vlib/v/tests/generic_functions_with_normal_function_test.v @@ -0,0 +1,12 @@ +fn get(typ T) T { + return typ +} + +fn get_string(typ string) string { + return 'boom' +} + +fn test_generic_with_same_type() { + assert get_string('') == 'boom' + assert get('hello') == 'hello' +}