1 // run -gcflags=-d=inlstaticinit=1
2 //go:build !gcflags_noopt
4 // Copyright 2010 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // Test that many initializations can be done at link time and
9 // generate no executable init functions.
10 // Also test that trivial func init are optimized away.
19 // All these initializations should be done at link time.
21 type S struct{ a, b, c int }
22 type SS struct{ aa, bb, cc S }
23 type SA struct{ a, b, c [3]int }
24 type SC struct{ a, b, c []int }
30 slice = []byte{1, 2, 3}
31 sliceInt = []int{1, 2, 3}
32 hello = "hello, world"
33 bytes = []byte("hello, world")
37 nilmap map[string]int = nil
39 nilchan chan int = nil
43 var a = [3]int{1001, 1002, 1003}
44 var s = S{1101, 1102, 1103}
45 var c = []int{1201, 1202, 1203}
47 var aa = [3][3]int{[3]int{2001, 2002, 2003}, [3]int{2004, 2005, 2006}, [3]int{2007, 2008, 2009}}
48 var as = [3]S{S{2101, 2102, 2103}, S{2104, 2105, 2106}, S{2107, 2108, 2109}}
50 var sa = SA{[3]int{3001, 3002, 3003}, [3]int{3004, 3005, 3006}, [3]int{3007, 3008, 3009}}
51 var ss = SS{S{3101, 3102, 3103}, S{3104, 3105, 3106}, S{3107, 3108, 3109}}
53 var ca = [][3]int{[3]int{4001, 4002, 4003}, [3]int{4004, 4005, 4006}, [3]int{4007, 4008, 4009}}
54 var cs = []S{S{4101, 4102, 4103}, S{4104, 4105, 4106}, S{4107, 4108, 4109}}
56 var answers = [...]int{
75 2001, 2201, 2301, 4001, 4201, 4301, 5001, 5201, 5301,
76 2002, 2202, 2302, 4002, 4202, 4302, 5002, 5202, 5302,
77 2003, 2203, 2303, 4003, 4203, 4303, 5003, 5203, 5303,
89 2004, 2204, 2304, 4004, 4204, 4304, 5004, 5204, 5304,
90 2005, 2205, 2305, 4005, 4205, 4305, 5005, 5205, 5305,
91 2006, 2206, 2306, 4006, 4206, 4306, 5006, 5206, 5306,
103 2007, 2207, 2307, 4007, 4207, 4307, 5007, 5207, 5307,
104 2008, 2208, 2308, 4008, 4208, 4308, 5008, 5208, 5308,
105 2009, 2209, 2309, 4009, 4209, 4309, 5009, 5209, 5309,
113 copy_sliceInt = sliceInt
114 // copy_hello = hello // static init of copied strings defeats link -X; see #34675
116 // Could be handled without an initialization function, but
117 // requires special handling for "a = []byte("..."); b = a"
118 // which is not a likely case.
119 // copy_bytes = bytes
120 // https://codereview.appspot.com/171840043 is one approach to
121 // make this special case work.
123 copy_four, copy_five = four, five
125 // copy_y = y // static init of copied strings defeats link -X; see #34675
126 copy_nilslice = nilslice
128 copy_nilfunc = nilfunc
129 copy_nilchan = nilchan
146 var copy_answers = answers
169 var c0 = complex64(0)
170 var c1 = complex64(1)
173 var d0 = complex128(0)
174 var d1 = complex128(1)
177 var s0 = []int{0, 0, 0}
178 var s1 = []int{1, 2, 3}
180 func fi() int { return 1 }
183 var a0 = [10]int{0, 0, 0}
184 var a1 = [10]int{1, 2, 3, 4}
186 type T struct{ X, Y int }
192 var t1 = T{X: 1, Y: 2}
196 var ps0 = &[]int{0, 0, 0}
197 var ps1 = &[]int{1, 2, 3}
200 var pa0 = &[10]int{0, 0, 0}
201 var pa1 = &[10]int{1, 2, 3}
207 var pt1 = &T{X: 1, Y: 2}
210 // The checks similar to
212 // are commented out. The compiler no longer statically initializes them.
213 // See issue 7665 and https://codereview.appspot.com/93200044.
214 // If https://codereview.appspot.com/169040043 is submitted, and this
215 // test is changed to pass -complete to the compiler, then we can
216 // uncomment the copy lines again.
261 // var copy_psx = psx
265 // var copy_pax = pax
269 // var copy_ptx = ptx
276 var _ interface{} = 1
286 var _ Mer = (*T1)(nil)
289 var PtrByte unsafe.Pointer = unsafe.Pointer(&Byte)
291 var LitSXInit = &S{1, 2, 3}
292 var LitSAnyXInit any = &S{4, 5, 6}
294 func FS(x, y, z int) *S { return &S{x, y, z} }
295 func FSA(x, y, z int) any { return &S{x, y, z} }
296 func F3(x int) *S { return &S{x, x, x} }
298 var LitSCallXInit = FS(7, 8, 9)
299 var LitSAnyCallXInit any = FSA(10, 11, 12)
301 var LitSRepeat = F3(1 + 2)
303 func F0() *S { return &S{1, 2, 3} }
305 var LitSNoArgs = F0()
307 var myError = errors.New("mine")
309 func gopherize(s string) string { return "gopher gopher gopher " + s }
311 var animals = gopherize("badger")
313 // These init funcs should optimize away.
328 // Actual test: check for init funcs in runtime data structures.
330 type initTask struct {
336 //go:linkname main_inittask main..inittask
337 var main_inittask initTask
340 if nfns := main_inittask.nfns; nfns != 0 {
342 panic("unexpected init funcs")