table: fix generic sumtype instantiations (#12288)

pull/12297/head
yuyi 2021-10-25 20:22:41 +08:00 committed by GitHub
parent 77a1e3dedb
commit ac99007cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -1771,12 +1771,20 @@ pub fn (mut t Table) generic_insts_to_concrete() {
} }
} }
for i in 0 .. variants.len { for i in 0 .. variants.len {
if t_typ := t.resolve_generic_to_concrete(variants[i], generic_names, if variants[i].has_flag(.generic) {
sym := t.get_type_symbol(variants[i])
if sym.kind == .struct_ && variants[i].idx() != info.parent_idx {
variants[i] = t.unwrap_generic_type(variants[i], generic_names,
info.concrete_types) info.concrete_types)
} else {
if t_typ := t.resolve_generic_to_concrete(variants[i],
generic_names, info.concrete_types)
{ {
variants[i] = t_typ variants[i] = t_typ
} }
} }
}
}
typ.info = SumType{ typ.info = SumType{
...parent_info ...parent_info
is_generic: false is_generic: false

View File

@ -0,0 +1,16 @@
struct Foo<T> {
x T
}
struct Bar<T> {
x T
}
type MyType<T> = Bar<T> | Foo<T>
fn test_generic_sumtype_insts() {
f := Foo<string>{'hi'}
t := MyType<string>(f)
println(t.type_name())
assert t.type_name() == 'Foo<string>'
}