checker: fix generics return recursive generic struct (fix #10028) (#10031)

pull/10038/head^2
yuyi 2021-05-08 19:33:09 +08:00 committed by GitHub
parent b5afa049e9
commit ed7c8d5e19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -1639,9 +1639,9 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e
mut fields := rts.info.fields.clone() mut fields := rts.info.fields.clone()
if rts.info.generic_types.len == concrete_types.len { if rts.info.generic_types.len == concrete_types.len {
generic_names := rts.info.generic_types.map(c.table.get_type_symbol(it).name) generic_names := rts.info.generic_types.map(c.table.get_type_symbol(it).name)
for i, _ in fields { for i in 0 .. fields.len {
if t_typ := c.table.resolve_generic_to_concrete(fields[i].typ, if t_typ := c.table.resolve_generic_to_concrete(fields[i].typ,
generic_names, concrete_types, false) generic_names, concrete_types, true)
{ {
fields[i].typ = t_typ fields[i].typ = t_typ
} }

View File

@ -0,0 +1,18 @@
struct Node<T> {
mut:
val T
next &Node<T>
}
fn make_node<T>(val []T) Node<T> {
return Node{
val: val[0]
next: 0
}
}
fn test_generics_return_recursive_generics_struct() {
n := make_node([1, 2, 3])
println(n.val)
assert n.val == 1
}