github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/test/sinit.go (about)

     1  // skip
     2  
     3  // Copyright 2010 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test that many initializations can be done at link time and
     8  // generate no executable init functions.
     9  // This test is run by sinit_run.go.
    10  
    11  package p
    12  
    13  import "unsafe"
    14  
    15  // Should be no init func in the assembly.
    16  // All these initializations should be done at link time.
    17  
    18  type S struct{ a, b, c int }
    19  type SS struct{ aa, bb, cc S }
    20  type SA struct{ a, b, c [3]int }
    21  type SC struct{ a, b, c []int }
    22  
    23  var (
    24  	zero                      = 2
    25  	one                       = 1
    26  	pi                        = 3.14
    27  	slice                     = []byte{1, 2, 3}
    28  	sliceInt                  = []int{1, 2, 3}
    29  	hello                     = "hello, world"
    30  	bytes                     = []byte("hello, world")
    31  	four, five                = 4, 5
    32  	x, y                      = 0.1, "hello"
    33  	nilslice   []byte         = nil
    34  	nilmap     map[string]int = nil
    35  	nilfunc    func()         = nil
    36  	nilchan    chan int       = nil
    37  	nilptr     *byte          = nil
    38  )
    39  
    40  var a = [3]int{1001, 1002, 1003}
    41  var s = S{1101, 1102, 1103}
    42  var c = []int{1201, 1202, 1203}
    43  
    44  var aa = [3][3]int{[3]int{2001, 2002, 2003}, [3]int{2004, 2005, 2006}, [3]int{2007, 2008, 2009}}
    45  var as = [3]S{S{2101, 2102, 2103}, S{2104, 2105, 2106}, S{2107, 2108, 2109}}
    46  var ac = [3][]int{[]int{2201, 2202, 2203}, []int{2204, 2205, 2206}, []int{2207, 2208, 2209}}
    47  
    48  var sa = SA{[3]int{3001, 3002, 3003}, [3]int{3004, 3005, 3006}, [3]int{3007, 3008, 3009}}
    49  var ss = SS{S{3101, 3102, 3103}, S{3104, 3105, 3106}, S{3107, 3108, 3109}}
    50  var sc = SC{[]int{3201, 3202, 3203}, []int{3204, 3205, 3206}, []int{3207, 3208, 3209}}
    51  
    52  var ca = [][3]int{[3]int{4001, 4002, 4003}, [3]int{4004, 4005, 4006}, [3]int{4007, 4008, 4009}}
    53  var cs = []S{S{4101, 4102, 4103}, S{4104, 4105, 4106}, S{4107, 4108, 4109}}
    54  var cc = [][]int{[]int{4201, 4202, 4203}, []int{4204, 4205, 4206}, []int{4207, 4208, 4209}}
    55  
    56  var answers = [...]int{
    57  	// s
    58  	1101, 1102, 1103,
    59  
    60  	// ss
    61  	3101, 3102, 3103,
    62  	3104, 3105, 3106,
    63  	3107, 3108, 3109,
    64  
    65  	// [0]
    66  	1001, 1201, 1301,
    67  	2101, 2102, 2103,
    68  	4101, 4102, 4103,
    69  	5101, 5102, 5103,
    70  	3001, 3004, 3007,
    71  	3201, 3204, 3207,
    72  	3301, 3304, 3307,
    73  
    74  	// [0][j]
    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,
    78  
    79  	// [1]
    80  	1002, 1202, 1302,
    81  	2104, 2105, 2106,
    82  	4104, 4105, 4106,
    83  	5104, 5105, 5106,
    84  	3002, 3005, 3008,
    85  	3202, 3205, 3208,
    86  	3302, 3305, 3308,
    87  
    88  	// [1][j]
    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,
    92  
    93  	// [2]
    94  	1003, 1203, 1303,
    95  	2107, 2108, 2109,
    96  	4107, 4108, 4109,
    97  	5107, 5108, 5109,
    98  	3003, 3006, 3009,
    99  	3203, 3206, 3209,
   100  	3303, 3306, 3309,
   101  
   102  	// [2][j]
   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,
   106  }
   107  
   108  var (
   109  	copy_zero     = zero
   110  	copy_one      = one
   111  	copy_pi       = pi
   112  	copy_slice    = slice
   113  	copy_sliceInt = sliceInt
   114  	copy_hello    = hello
   115  
   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.
   122  
   123  	copy_four, copy_five = four, five
   124  	copy_x, copy_y       = x, y
   125  	copy_nilslice        = nilslice
   126  	copy_nilmap          = nilmap
   127  	copy_nilfunc         = nilfunc
   128  	copy_nilchan         = nilchan
   129  	copy_nilptr          = nilptr
   130  )
   131  
   132  var copy_a = a
   133  var copy_s = s
   134  var copy_c = c
   135  
   136  var copy_aa = aa
   137  var copy_as = as
   138  var copy_ac = ac
   139  
   140  var copy_sa = sa
   141  var copy_ss = ss
   142  var copy_sc = sc
   143  
   144  var copy_ca = ca
   145  var copy_cs = cs
   146  var copy_cc = cc
   147  
   148  var copy_answers = answers
   149  
   150  var bx bool
   151  var b0 = false
   152  var b1 = true
   153  
   154  var fx float32
   155  var f0 = float32(0)
   156  var f1 = float32(1)
   157  
   158  var gx float64
   159  var g0 = float64(0)
   160  var g1 = float64(1)
   161  
   162  var ix int
   163  var i0 = 0
   164  var i1 = 1
   165  
   166  var jx uint
   167  var j0 = uint(0)
   168  var j1 = uint(1)
   169  
   170  var cx complex64
   171  var c0 = complex64(0)
   172  var c1 = complex64(1)
   173  
   174  var dx complex128
   175  var d0 = complex128(0)
   176  var d1 = complex128(1)
   177  
   178  var sx []int
   179  var s0 = []int{0, 0, 0}
   180  var s1 = []int{1, 2, 3}
   181  
   182  func fi() int { return 1 }
   183  
   184  var ax [10]int
   185  var a0 = [10]int{0, 0, 0}
   186  var a1 = [10]int{1, 2, 3, 4}
   187  
   188  type T struct{ X, Y int }
   189  
   190  var tx T
   191  var t0 = T{}
   192  var t0a = T{0, 0}
   193  var t0b = T{X: 0}
   194  var t1 = T{X: 1, Y: 2}
   195  var t1a = T{3, 4}
   196  
   197  var psx *[]int
   198  var ps0 = &[]int{0, 0, 0}
   199  var ps1 = &[]int{1, 2, 3}
   200  
   201  var pax *[10]int
   202  var pa0 = &[10]int{0, 0, 0}
   203  var pa1 = &[10]int{1, 2, 3}
   204  
   205  var ptx *T
   206  var pt0 = &T{}
   207  var pt0a = &T{0, 0}
   208  var pt0b = &T{X: 0}
   209  var pt1 = &T{X: 1, Y: 2}
   210  var pt1a = &T{3, 4}
   211  
   212  // The checks similar to
   213  // var copy_bx = bx
   214  // are commented out.  The  compiler no longer statically initializes them.
   215  // See issue 7665 and https://codereview.appspot.com/93200044.
   216  // If https://codereview.appspot.com/169040043 is submitted, and this
   217  // test is changed to pass -complete to the compiler, then we can
   218  // uncomment the copy lines again.
   219  
   220  // var copy_bx = bx
   221  var copy_b0 = b0
   222  var copy_b1 = b1
   223  
   224  // var copy_fx = fx
   225  var copy_f0 = f0
   226  var copy_f1 = f1
   227  
   228  // var copy_gx = gx
   229  var copy_g0 = g0
   230  var copy_g1 = g1
   231  
   232  // var copy_ix = ix
   233  var copy_i0 = i0
   234  var copy_i1 = i1
   235  
   236  // var copy_jx = jx
   237  var copy_j0 = j0
   238  var copy_j1 = j1
   239  
   240  // var copy_cx = cx
   241  var copy_c0 = c0
   242  var copy_c1 = c1
   243  
   244  // var copy_dx = dx
   245  var copy_d0 = d0
   246  var copy_d1 = d1
   247  
   248  // var copy_sx = sx
   249  var copy_s0 = s0
   250  var copy_s1 = s1
   251  
   252  // var copy_ax = ax
   253  var copy_a0 = a0
   254  var copy_a1 = a1
   255  
   256  // var copy_tx = tx
   257  var copy_t0 = t0
   258  var copy_t0a = t0a
   259  var copy_t0b = t0b
   260  var copy_t1 = t1
   261  var copy_t1a = t1a
   262  
   263  // var copy_psx = psx
   264  var copy_ps0 = ps0
   265  var copy_ps1 = ps1
   266  
   267  // var copy_pax = pax
   268  var copy_pa0 = pa0
   269  var copy_pa1 = pa1
   270  
   271  // var copy_ptx = ptx
   272  var copy_pt0 = pt0
   273  var copy_pt0a = pt0a
   274  var copy_pt0b = pt0b
   275  var copy_pt1 = pt1
   276  var copy_pt1a = pt1a
   277  
   278  var _ interface{} = 1
   279  
   280  type T1 int
   281  
   282  func (t *T1) M() {}
   283  
   284  type Mer interface {
   285  	M()
   286  }
   287  
   288  var _ Mer = (*T1)(nil)
   289  
   290  var Byte byte
   291  var PtrByte unsafe.Pointer = unsafe.Pointer(&Byte)