diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 941ee3a23b..bc9c6e6126 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -315,10 +315,15 @@ fn (mut p Parser) fn_decl() ast.FnDecl { // mut generic_names := p.parse_generic_names() // generic names can be infer with receiver's generic names - if is_method && rec.typ.has_flag(.generic) && generic_names.len == 0 { + if is_method && rec.typ.has_flag(.generic) { sym := p.table.get_type_symbol(rec.typ) if sym.info is ast.Struct { - generic_names = sym.info.generic_types.map(p.table.get_type_symbol(it).name) + rec_generic_names := sym.info.generic_types.map(p.table.get_type_symbol(it).name) + for gname in rec_generic_names { + if gname !in generic_names { + generic_names << gname + } + } } } // Args diff --git a/vlib/v/tests/generics_method_with_multi_types_test.v b/vlib/v/tests/generics_method_with_multi_types_test.v index 1884d69b41..1ca3cbbeb8 100644 --- a/vlib/v/tests/generics_method_with_multi_types_test.v +++ b/vlib/v/tests/generics_method_with_multi_types_test.v @@ -10,7 +10,27 @@ struct App { f M } -fn (mut self App) next(input T) f64 { +fn (mut self App) next1(input T) f64 { + $if M is Something { + return 0 + } $else { + panic('${typeof(M.typ).name} is not supported') + return 1 + } + return 1 +} + +fn (mut self App) next2(input T) f64 { + $if M is Something { + return 0 + } $else { + panic('${typeof(M.typ).name} is not supported') + return 1 + } + return 1 +} + +fn (mut self App) next3(input T) f64 { $if M is Something { return 0 } $else { @@ -26,5 +46,7 @@ fn test_generic_method_with_multi_types() { i: 10 } } - assert app.next(1) == 0 + assert app.next1(1) == 0 + assert app.next2(1) == 0 + assert app.next3(1) == 0 }