github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/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  // Should be no init func in the assembly.
    14  // All these initializations should be done at link time.
    15  
    16  type S struct{ a, b, c int }
    17  type SS struct{ aa, bb, cc S }
    18  type SA struct{ a, b, c [3]int }
    19  type SC struct{ a, b, c []int }
    20  
    21  var (
    22  	zero                      = 2
    23  	one                       = 1
    24  	pi                        = 3.14
    25  	slice                     = []byte{1, 2, 3}
    26  	sliceInt                  = []int{1, 2, 3}
    27  	hello                     = "hello, world"
    28  	bytes                     = []byte("hello, world")
    29  	four, five                = 4, 5
    30  	x, y                      = 0.1, "hello"
    31  	nilslice   []byte         = nil
    32  	nilmap     map[string]int = nil
    33  	nilfunc    func()         = nil
    34  	nilchan    chan int       = nil
    35  	nilptr     *byte          = nil
    36  )
    37  
    38  var a = [3]int{1001, 1002, 1003}
    39  var s = S{1101, 1102, 1103}
    40  var c = []int{1201, 1202, 1203}
    41  
    42  var aa = [3][3]int{[3]int{2001, 2002, 2003}, [3]int{2004, 2005, 2006}, [3]int{2007, 2008, 2009}}
    43  var as = [3]S{S{2101, 2102, 2103}, S{2104, 2105, 2106}, S{2107, 2108, 2109}}
    44  var ac = [3][]int{[]int{2201, 2202, 2203}, []int{2204, 2205, 2206}, []int{2207, 2208, 2209}}
    45  
    46  var sa = SA{[3]int{3001, 3002, 3003}, [3]int{3004, 3005, 3006}, [3]int{3007, 3008, 3009}}
    47  var ss = SS{S{3101, 3102, 3103}, S{3104, 3105, 3106}, S{3107, 3108, 3109}}
    48  var sc = SC{[]int{3201, 3202, 3203}, []int{3204, 3205, 3206}, []int{3207, 3208, 3209}}
    49  
    50  var ca = [][3]int{[3]int{4001, 4002, 4003}, [3]int{4004, 4005, 4006}, [3]int{4007, 4008, 4009}}
    51  var cs = []S{S{4101, 4102, 4103}, S{4104, 4105, 4106}, S{4107, 4108, 4109}}
    52  var cc = [][]int{[]int{4201, 4202, 4203}, []int{4204, 4205, 4206}, []int{4207, 4208, 4209}}
    53  
    54  var answers = [...]int{
    55  	// s
    56  	1101, 1102, 1103,
    57  
    58  	// ss
    59  	3101, 3102, 3103,
    60  	3104, 3105, 3106,
    61  	3107, 3108, 3109,
    62  
    63  	// [0]
    64  	1001, 1201, 1301,
    65  	2101, 2102, 2103,
    66  	4101, 4102, 4103,
    67  	5101, 5102, 5103,
    68  	3001, 3004, 3007,
    69  	3201, 3204, 3207,
    70  	3301, 3304, 3307,
    71  
    72  	// [0][j]
    73  	2001, 2201, 2301, 4001, 4201, 4301, 5001, 5201, 5301,
    74  	2002, 2202, 2302, 4002, 4202, 4302, 5002, 5202, 5302,
    75  	2003, 2203, 2303, 4003, 4203, 4303, 5003, 5203, 5303,
    76  
    77  	// [1]
    78  	1002, 1202, 1302,
    79  	2104, 2105, 2106,
    80  	4104, 4105, 4106,
    81  	5104, 5105, 5106,
    82  	3002, 3005, 3008,
    83  	3202, 3205, 3208,
    84  	3302, 3305, 3308,
    85  
    86  	// [1][j]
    87  	2004, 2204, 2304, 4004, 4204, 4304, 5004, 5204, 5304,
    88  	2005, 2205, 2305, 4005, 4205, 4305, 5005, 5205, 5305,
    89  	2006, 2206, 2306, 4006, 4206, 4306, 5006, 5206, 5306,
    90  
    91  	// [2]
    92  	1003, 1203, 1303,
    93  	2107, 2108, 2109,
    94  	4107, 4108, 4109,
    95  	5107, 5108, 5109,
    96  	3003, 3006, 3009,
    97  	3203, 3206, 3209,
    98  	3303, 3306, 3309,
    99  
   100  	// [2][j]
   101  	2007, 2207, 2307, 4007, 4207, 4307, 5007, 5207, 5307,
   102  	2008, 2208, 2308, 4008, 4208, 4308, 5008, 5208, 5308,
   103  	2009, 2209, 2309, 4009, 4209, 4309, 5009, 5209, 5309,
   104  }
   105  
   106  var (
   107  	copy_zero     = zero
   108  	copy_one      = one
   109  	copy_pi       = pi
   110  	copy_slice    = slice
   111  	copy_sliceInt = sliceInt
   112  	copy_hello    = hello
   113  
   114  	// Could be handled without an initialization function, but
   115  	// requires special handling for "a = []byte("..."); b = a"
   116  	// which is not a likely case.
   117  	// copy_bytes = bytes
   118  	// https://codereview.appspot.com/171840043 is one approach to
   119  	// make this special case work.
   120  
   121  	copy_four, copy_five = four, five
   122  	copy_x, copy_y       = x, y
   123  	copy_nilslice        = nilslice
   124  	copy_nilmap          = nilmap
   125  	copy_nilfunc         = nilfunc
   126  	copy_nilchan         = nilchan
   127  	copy_nilptr          = nilptr
   128  )
   129  
   130  var copy_a = a
   131  var copy_s = s
   132  var copy_c = c
   133  
   134  var copy_aa = aa
   135  var copy_as = as
   136  var copy_ac = ac
   137  
   138  var copy_sa = sa
   139  var copy_ss = ss
   140  var copy_sc = sc
   141  
   142  var copy_ca = ca
   143  var copy_cs = cs
   144  var copy_cc = cc
   145  
   146  var copy_answers = answers
   147  
   148  var bx bool
   149  var b0 = false
   150  var b1 = true
   151  
   152  var fx float32
   153  var f0 = float32(0)
   154  var f1 = float32(1)
   155  
   156  var gx float64
   157  var g0 = float64(0)
   158  var g1 = float64(1)
   159  
   160  var ix int
   161  var i0 = 0
   162  var i1 = 1
   163  
   164  var jx uint
   165  var j0 = uint(0)
   166  var j1 = uint(1)
   167  
   168  var cx complex64
   169  var c0 = complex64(0)
   170  var c1 = complex64(1)
   171  
   172  var dx complex128
   173  var d0 = complex128(0)
   174  var d1 = complex128(1)
   175  
   176  var sx []int
   177  var s0 = []int{0, 0, 0}
   178  var s1 = []int{1, 2, 3}
   179  
   180  func fi() int { return 1 }
   181  
   182  var ax [10]int
   183  var a0 = [10]int{0, 0, 0}
   184  var a1 = [10]int{1, 2, 3, 4}
   185  
   186  type T struct{ X, Y int }
   187  
   188  var tx T
   189  var t0 = T{}
   190  var t0a = T{0, 0}
   191  var t0b = T{X: 0}
   192  var t1 = T{X: 1, Y: 2}
   193  var t1a = T{3, 4}
   194  
   195  var psx *[]int
   196  var ps0 = &[]int{0, 0, 0}
   197  var ps1 = &[]int{1, 2, 3}
   198  
   199  var pax *[10]int
   200  var pa0 = &[10]int{0, 0, 0}
   201  var pa1 = &[10]int{1, 2, 3}
   202  
   203  var ptx *T
   204  var pt0 = &T{}
   205  var pt0a = &T{0, 0}
   206  var pt0b = &T{X: 0}
   207  var pt1 = &T{X: 1, Y: 2}
   208  var pt1a = &T{3, 4}
   209  
   210  // The checks similar to
   211  // var copy_bx = bx
   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.
   217  
   218  // var copy_bx = bx
   219  var copy_b0 = b0
   220  var copy_b1 = b1
   221  
   222  // var copy_fx = fx
   223  var copy_f0 = f0
   224  var copy_f1 = f1
   225  
   226  // var copy_gx = gx
   227  var copy_g0 = g0
   228  var copy_g1 = g1
   229  
   230  // var copy_ix = ix
   231  var copy_i0 = i0
   232  var copy_i1 = i1
   233  
   234  // var copy_jx = jx
   235  var copy_j0 = j0
   236  var copy_j1 = j1
   237  
   238  // var copy_cx = cx
   239  var copy_c0 = c0
   240  var copy_c1 = c1
   241  
   242  // var copy_dx = dx
   243  var copy_d0 = d0
   244  var copy_d1 = d1
   245  
   246  // var copy_sx = sx
   247  var copy_s0 = s0
   248  var copy_s1 = s1
   249  
   250  // var copy_ax = ax
   251  var copy_a0 = a0
   252  var copy_a1 = a1
   253  
   254  // var copy_tx = tx
   255  var copy_t0 = t0
   256  var copy_t0a = t0a
   257  var copy_t0b = t0b
   258  var copy_t1 = t1
   259  var copy_t1a = t1a
   260  
   261  // var copy_psx = psx
   262  var copy_ps0 = ps0
   263  var copy_ps1 = ps1
   264  
   265  // var copy_pax = pax
   266  var copy_pa0 = pa0
   267  var copy_pa1 = pa1
   268  
   269  // var copy_ptx = ptx
   270  var copy_pt0 = pt0
   271  var copy_pt0a = pt0a
   272  var copy_pt0b = pt0b
   273  var copy_pt1 = pt1
   274  var copy_pt1a = pt1a
   275  
   276  var _ interface{} = 1
   277  
   278  type T1 int
   279  
   280  func (t *T1) M() {}
   281  
   282  type Mer interface {
   283  	M()
   284  }
   285  
   286  var _ Mer = (*T1)(nil)