checker: fix generic fn return types with generic struct (#12186)

pull/12198/head
yuyi 2021-10-15 16:50:10 +08:00 committed by GitHub
parent 27cd21e459
commit c108e01917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 12 deletions

View File

@ -699,6 +699,24 @@ fn (mut c Checker) unwrap_generic_type(typ ast.Type, generic_names []string, con
idx := c.table.find_or_register_array_with_dims(unwrap_typ, dims)
return ast.new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
ast.ArrayFixed {
unwrap_typ := c.unwrap_generic_type(ts.info.elem_type, generic_names, concrete_types)
idx := c.table.find_or_register_array_fixed(unwrap_typ, ts.info.size, ast.None{})
return ast.new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
ast.Chan {
unwrap_typ := c.unwrap_generic_type(ts.info.elem_type, generic_names, concrete_types)
idx := c.table.find_or_register_chan(unwrap_typ, unwrap_typ.nr_muls() > 0)
return ast.new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
ast.Map {
unwrap_key_type := c.unwrap_generic_type(ts.info.key_type, generic_names,
concrete_types)
unwrap_value_type := c.unwrap_generic_type(ts.info.value_type, generic_names,
concrete_types)
idx := c.table.find_or_register_map(unwrap_key_type, unwrap_value_type)
return ast.new_type(idx).derive_add_muls(typ).clear_flag(.generic)
}
ast.Struct, ast.Interface, ast.SumType {
if !ts.info.is_generic {
return typ

View File

@ -1,12 +0,0 @@
pub struct Abcd<T> {
}
pub fn iterators<T>() []&Abcd<T> {
return []&Abcd<T>{}
}
fn test_generic_fn_return_array_of_generic_struct() {
a := iterators<f64>()
println(a)
assert '$a' == '[]'
}

View File

@ -0,0 +1,32 @@
struct Abcd<T> {
}
fn iterators_array<T>() []&Abcd<T> {
return []&Abcd<T>{}
}
fn test_generic_fn_return_array_of_generic_struct() {
a := iterators_array<f64>()
println(a)
assert '$a' == '[]'
}
fn iterators_chan<T>() chan Abcd<T> {
return chan Abcd<T>{}
}
fn test_generic_fn_return_chan_of_generic_struct() {
a := iterators_chan<f64>()
println(a)
assert typeof(a).name == 'chan Abcd<f64>'
}
fn iterators_map<T>() map[string]&Abcd<T> {
return map[string]&Abcd<T>{}
}
fn test_generic_fn_return_map_of_generic_struct() {
a := iterators_map<f64>()
println(a)
assert '$a' == '{}'
}