Source file test/sinit.go

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

View as plain text