checker: fix generic fn return array of generic struct (#12170)

pull/12179/head
yuyi 2021-10-14 02:21:32 +08:00 committed by GitHub
parent d373eba79b
commit 97e999768a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -685,6 +685,20 @@ fn (mut c Checker) unwrap_generic_type(typ ast.Type, generic_names []string, con
mut c_nrt := ''
ts := c.table.get_type_symbol(typ)
match mut ts.info {
ast.Array {
mut elem_type := ts.info.elem_type
mut elem_sym := c.table.get_type_symbol(elem_type)
mut dims := 1
for mut elem_sym.info is ast.Array {
info := elem_sym.info as ast.Array
elem_type = info.elem_type
elem_sym = c.table.get_type_symbol(elem_type)
dims++
}
unwrap_typ := c.unwrap_generic_type(elem_type, generic_names, concrete_types)
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.Struct, ast.Interface, ast.SumType {
if !ts.info.is_generic {
return typ

View File

@ -0,0 +1,12 @@
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' == '[]'
}