Skip to content

Commit ac99007

Browse files
authored
table: fix generic sumtype instantiations (#12288)
1 parent 77a1e3d commit ac99007

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

vlib/v/ast/table.v

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,10 +1771,18 @@ pub fn (mut t Table) generic_insts_to_concrete() {
17711771
}
17721772
}
17731773
for i in 0 .. variants.len {
1774-
if t_typ := t.resolve_generic_to_concrete(variants[i], generic_names,
1775-
info.concrete_types)
1776-
{
1777-
variants[i] = t_typ
1774+
if variants[i].has_flag(.generic) {
1775+
sym := t.get_type_symbol(variants[i])
1776+
if sym.kind == .struct_ && variants[i].idx() != info.parent_idx {
1777+
variants[i] = t.unwrap_generic_type(variants[i], generic_names,
1778+
info.concrete_types)
1779+
} else {
1780+
if t_typ := t.resolve_generic_to_concrete(variants[i],
1781+
generic_names, info.concrete_types)
1782+
{
1783+
variants[i] = t_typ
1784+
}
1785+
}
17781786
}
17791787
}
17801788
typ.info = SumType{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Foo<T> {
2+
x T
3+
}
4+
5+
struct Bar<T> {
6+
x T
7+
}
8+
9+
type MyType<T> = Bar<T> | Foo<T>
10+
11+
fn test_generic_sumtype_insts() {
12+
f := Foo<string>{'hi'}
13+
t := MyType<string>(f)
14+
println(t.type_name())
15+
assert t.type_name() == 'Foo<string>'
16+
}

0 commit comments

Comments
 (0)