ast: fix generic nested struct init (#12405)

pull/12407/head
yuyi 2021-11-07 23:06:37 +08:00 committed by GitHub
parent 1c12186701
commit 9ec1262734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -1661,16 +1661,14 @@ pub fn (mut t Table) generic_insts_to_concrete() {
generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name)
for i in 0 .. fields.len {
if fields[i].typ.has_flag(.generic) {
sym := t.get_type_symbol(fields[i].typ)
if sym.kind == .struct_ && fields[i].typ.idx() != info.parent_idx {
if fields[i].typ.idx() != info.parent_idx {
fields[i].typ = t.unwrap_generic_type(fields[i].typ,
generic_names, info.concrete_types)
} else {
if t_typ := t.resolve_generic_to_concrete(fields[i].typ,
generic_names, info.concrete_types)
{
fields[i].typ = t_typ
}
}
if t_typ := t.resolve_generic_to_concrete(fields[i].typ,
generic_names, info.concrete_types)
{
fields[i].typ = t_typ
}
}
}

View File

@ -0,0 +1,25 @@
struct Foo<T> {
foo T
}
struct Bar<T> {
mut:
foos []Foo<T>
}
fn (mut b Bar<T>) add(v T) {
b.foos << Foo<T>{
foo: v
}
}
fn test_nested_generics_struct_init() {
mut bar := Bar<string>{}
bar.add('bar')
println(bar)
result := '$bar'
assert result.contains('Bar<string>{')
assert result.contains('foos: [Foo<string>{')
assert result.contains("foo: 'bar'")
}