v4 / vlib / v / tests / generics / generic_struct_default_interface_field_test.v
29 lines · 25 sloc · 744 bytes · d480353ecbda7955a5910cbde7daa9146cb06003
Raw
1// A generic struct used as the default value of an interface-typed field inside
2// a generic wrapper must monomorphize with the wrapper's type argument, instead
3// of leaving the type parameter unresolved (`Text_T_T`). The leftover generic
4// variant must also not pollute the interface's auto-generated str().
5// See issue #27550.
6interface Tag {
7 tag() string
8}
9
10struct Text[T] {
11 val T
12}
13
14fn (t Text[T]) tag() string {
15 return 'text'
16}
17
18struct Wrapper[T] {
19 tag Tag = Text[T]{}
20}
21
22fn test_generic_struct_default_interface_field() {
23 w := Wrapper[int]{}
24 assert w.tag.tag() == 'text'
25 // stringifying the wrapper exercises the interface variant auto-str dispatch
26 s := w.str()
27 assert s.contains('Text[int]{')
28 assert s.contains('val: 0')
29}
30