checker: fix nested generic method call (#12353)

pull/12364/head
yuyi 2021-11-01 20:41:54 +08:00 committed by GitHub
parent 0ab133a563
commit dcf230ca24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -2227,13 +2227,15 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
concrete_types = node.concrete_types
}
// resolve return generics struct to concrete type
if method.generic_names.len > 0 && method.return_type.has_flag(.generic) {
if method.generic_names.len > 0 && method.return_type.has_flag(.generic)
&& c.table.cur_fn.generic_names.len == 0 {
node.return_type = c.table.unwrap_generic_type(method.return_type, method.generic_names,
concrete_types)
} else {
node.return_type = method.return_type
}
if node.concrete_types.len > 0 && method.return_type != 0 {
if node.concrete_types.len > 0 && method.return_type != 0
&& c.table.cur_fn.generic_names.len == 0 {
if typ := c.table.resolve_generic_to_concrete(method.return_type, method.generic_names,
concrete_types)
{

View File

@ -0,0 +1,24 @@
struct SSS<T> {
mut:
x T
}
fn (s SSS<T>) inner() T {
return s.x
}
fn (s SSS<T>) outer() string {
ret := s.inner<T>()
println(ret)
return '$ret'
}
fn test_generics_with_nested_generic_method_call_assign() {
s1 := SSS<int>{100}
s1.outer()
assert s1.outer() == '100'
s2 := SSS<string>{'hello'}
s2.outer()
assert s2.outer() == 'hello'
}