checker: fix generics with assign nested generic fn call (#12366)

pull/12358/head weekly.2021.44
yuyi 2021-11-02 16:12:26 +08:00 committed by GitHub
parent 0952af606c
commit 639cbfa0d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 26 deletions

View File

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

View File

@ -0,0 +1,37 @@
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_assign_nested_generic_method_call() {
s1 := SSS<int>{100}
assert s1.outer() == '100'
s2 := SSS<string>{'hello'}
assert s2.outer() == 'hello'
}
fn fn_inner<T>(t T) T {
return t
}
fn fn_outer<T>(t T) string {
ret := fn_inner<T>(t)
println(ret)
return '$ret'
}
fn test_generics_with_assign_nested_generic_fn_call() {
assert fn_outer(100) == '100'
assert fn_outer('hello') == 'hello'
}

View File

@ -1,24 +0,0 @@
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'
}