github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/structs.go (about)

     1  package main
     2  
     3  // TODO: add .ll test files to check the output
     4  
     5  type s0 struct {
     6  }
     7  
     8  type s1 struct {
     9  	a byte
    10  }
    11  
    12  type s2 struct {
    13  	a byte
    14  	b byte
    15  }
    16  
    17  type s3 struct {
    18  	a byte
    19  	b byte
    20  	c byte
    21  }
    22  
    23  // should not be expanded
    24  type s4 struct {
    25  	a byte
    26  	b byte
    27  	c byte
    28  	d byte
    29  }
    30  
    31  // same struct, different type
    32  type s4b struct {
    33  	a byte
    34  	b byte
    35  	c byte
    36  	d byte
    37  }
    38  
    39  type s5 struct {
    40  	a struct {
    41  		aa byte
    42  		ab byte
    43  	}
    44  	b byte
    45  }
    46  
    47  type s6 struct {
    48  	a string
    49  	b byte
    50  }
    51  
    52  type s7 struct {
    53  	a interface{}
    54  	b byte
    55  }
    56  
    57  // should not be expanded
    58  type s8 struct {
    59  	a []byte // 3 elements
    60  	b byte   // 1 element
    61  }
    62  
    63  // linked list (recursive type)
    64  type s9 struct {
    65  	n    int
    66  	next *s9
    67  	s    []*s9
    68  }
    69  
    70  func test0(s s0) {
    71  	println("test0")
    72  }
    73  
    74  func test1(s s1) {
    75  	println("test1", s.a)
    76  }
    77  
    78  func test2(s s2) {
    79  	println("test2", s.a, s.b)
    80  }
    81  
    82  func test3(s s3) {
    83  	println("test3", s.a, s.b, s.c)
    84  }
    85  
    86  func test4(s s4) {
    87  	println("test4", s.a, s.b, s.c, s.d)
    88  	test4b(s4b(s))
    89  	test4bp((*s4b)(&s))
    90  }
    91  
    92  func test4b(s s4b) {
    93  	println("test4b", s.a, s.b, s.c, s.d)
    94  }
    95  
    96  func test4bp(s *s4b) {
    97  	println("test4bp", s.a, s.b, s.c, s.d)
    98  }
    99  
   100  func test5(s s5) {
   101  	println("test5", s.a.aa, s.a.ab, s.b)
   102  }
   103  
   104  func test6(s s6) {
   105  	println("test6", s.a, len(s.a), s.b)
   106  }
   107  
   108  func test7(s s7) {
   109  	println("test7", s.a, s.b)
   110  }
   111  
   112  func test8(s s8) {
   113  	println("test8", len(s.a), cap(s.a), s.a[0], s.a[1], s.b)
   114  }
   115  
   116  func test9(s s9) {
   117  	println("test9", s.next.next)
   118  }
   119  
   120  func main() {
   121  	test0(s0{})
   122  	test1(s1{1})
   123  	test2(s2{1, 2})
   124  	test3(s3{1, 2, 3})
   125  	test4(s4{1, 2, 3, 4})
   126  	test5(s5{a: struct {
   127  		aa byte
   128  		ab byte
   129  	}{1, 2}, b: 3})
   130  	test6(s6{"foo", 5})
   131  	test7(s7{a: nil, b: 8})
   132  	test8(s8{[]byte{12, 13, 14}[:2], 6})
   133  	test9(s9{next: &s9{}})
   134  }