diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index b27bba3583..f49d5cf495 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1771,10 +1771,18 @@ pub fn (mut t Table) generic_insts_to_concrete() { } } for i in 0 .. variants.len { - if t_typ := t.resolve_generic_to_concrete(variants[i], generic_names, - info.concrete_types) - { - variants[i] = t_typ + 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) + } else { + if t_typ := t.resolve_generic_to_concrete(variants[i], + generic_names, info.concrete_types) + { + variants[i] = t_typ + } + } } } typ.info = SumType{ diff --git a/vlib/v/tests/generic_sumtype_insts_test.v b/vlib/v/tests/generic_sumtype_insts_test.v new file mode 100644 index 0000000000..49e890ed55 --- /dev/null +++ b/vlib/v/tests/generic_sumtype_insts_test.v @@ -0,0 +1,16 @@ +struct Foo { + x T +} + +struct Bar { + x T +} + +type MyType = Bar | Foo + +fn test_generic_sumtype_insts() { + f := Foo{'hi'} + t := MyType(f) + println(t.type_name()) + assert t.type_name() == 'Foo' +}