ast: fix generic nested struct init (#12405)
parent
1c12186701
commit
9ec1262734
|
@ -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)
|
generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name)
|
||||||
for i in 0 .. fields.len {
|
for i in 0 .. fields.len {
|
||||||
if fields[i].typ.has_flag(.generic) {
|
if fields[i].typ.has_flag(.generic) {
|
||||||
sym := t.get_type_symbol(fields[i].typ)
|
if fields[i].typ.idx() != info.parent_idx {
|
||||||
if sym.kind == .struct_ && fields[i].typ.idx() != info.parent_idx {
|
|
||||||
fields[i].typ = t.unwrap_generic_type(fields[i].typ,
|
fields[i].typ = t.unwrap_generic_type(fields[i].typ,
|
||||||
generic_names, info.concrete_types)
|
generic_names, info.concrete_types)
|
||||||
} else {
|
}
|
||||||
if t_typ := t.resolve_generic_to_concrete(fields[i].typ,
|
if t_typ := t.resolve_generic_to_concrete(fields[i].typ,
|
||||||
generic_names, info.concrete_types)
|
generic_names, info.concrete_types)
|
||||||
{
|
{
|
||||||
fields[i].typ = t_typ
|
fields[i].typ = t_typ
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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'")
|
||||||
|
}
|
Loading…
Reference in New Issue