v2 / vlib / v / tests / comptime / comptime_indirection_check_test.v
34 lines · 28 sloc · 686 bytes · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1struct Encoder {}
2
3struct Writer {}
4
5struct StructTypePointer[T] {
6mut:
7 val &T
8}
9
10pub fn (e &Encoder) encode_value[T](val T, mut wr Writer) ! {
11 assert e.encode_struct[T](val, 1, mut wr)! == 'a'
12}
13
14fn (e &Encoder) encode_struct[U](val U, level int, mut wr Writer) !string {
15 $for field in U.fields {
16 if val.$(field.name) != unsafe { nil } {
17 $if field.indirections > 0 {
18 assert field.indirections == 1
19 return 'a'
20 } $else {
21 return 'b'
22 }
23 }
24 }
25 return 'z'
26}
27
28fn test_check() {
29 e := Encoder{}
30 mut sb := Writer{}
31
32 mut string_initialized_with_reference := 'ads'
33 e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut sb) or {}
34}
35