diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7647532880..0235f40dc7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -888,13 +888,6 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { c.error('unknown function: $fn_name', call_expr.pos) return table.void_type } - if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type { - // Handle `foo() T` => `foo() int` => return int - sym := c.table.get_type_symbol(f.return_type) - if sym.name == 'T' { - return call_expr.generic_type - } - } if !found_in_args && call_expr.mod in ['builtin', 'main'] { scope := c.file.scope.innermost(call_expr.pos.pos) if _ := scope.find_var(fn_name) { @@ -988,6 +981,13 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { call_expr.pos) } } + if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type { + // Handle `foo() T` => `foo() int` => return int + sym := c.table.get_type_symbol(f.return_type) + if sym.name == 'T' { + return call_expr.generic_type + } + } return f.return_type } diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 564cb4a8d5..46d6948ca2 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -572,6 +572,9 @@ pub fn (table &Table) qualify_module(mod, file_path string) string { pub fn (table &Table) register_fn_gen_type(fn_name string, typ Type) { mut a := table.fn_gen_types[fn_name] + if typ in a { + return + } a << typ table.fn_gen_types[fn_name] = a } diff --git a/vlib/v/tests/generic_test.v b/vlib/v/tests/generic_test.v index d41aeff96c..cdbe1c5d86 100644 --- a/vlib/v/tests/generic_test.v +++ b/vlib/v/tests/generic_test.v @@ -7,7 +7,9 @@ fn simple(p T) T { fn test_generic_fn() { assert simple(1) == 1 + assert simple(1 + 0) == 1 assert simple('g') == 'g' + assert simple('g') + 'h' == 'gh' } /*