Skip to content

Commit

Permalink
checker: fix generic method calls with multi generic types (fix #20330)…
Browse files Browse the repository at this point in the history
… (#20360)
  • Loading branch information
shove70 committed Jan 3, 2024
1 parent 688f89e commit 00a2356
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -2026,6 +2026,12 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
if rec_is_generic && node.concrete_types.len == 0
&& method.generic_names.len == rec_sym.info.generic_types.len {
node.concrete_types = rec_sym.info.generic_types
} else if rec_is_generic && node.concrete_types.len > 0
&& method.generic_names.len > node.concrete_types.len
&& rec_sym.info.generic_types.len + node.concrete_types.len == method.generic_names.len {
t_concrete_types := node.concrete_types.clone()
node.concrete_types = rec_sym.info.generic_types
node.concrete_types << t_concrete_types
} else if !rec_is_generic && rec_sym.info.concrete_types.len > 0
&& node.concrete_types.len > 0
&& rec_sym.info.concrete_types.len + node.concrete_types.len == method.generic_names.len {
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/generics_method_with_multi_types_test.v
Expand Up @@ -50,3 +50,20 @@ fn test_generic_method_with_multi_types() {
assert app.next2(1) == 0
assert app.next3(1) == 0
}

// for issues 20330
// phenomenon: Calling from one generic method to another loses the generic information of the receiver
struct Foo[T] {}

fn (f Foo[T]) method[U]() {
f.another_method[U]()
}

fn (f Foo[T]) another_method[U]() {}

fn test_passes_multiple_types() {
f := Foo[string]{}
f.method[f32]()
f.method[u32]()
assert true
}

0 comments on commit 00a2356

Please sign in to comment.