...

Source file src/internal/types/testdata/check/init0.go

Documentation: internal/types/testdata/check

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // initialization cycles
     6  
     7  package init0
     8  
     9  // initialization cycles (we don't know the types)
    10  const (
    11  	s0 /* ERROR "initialization cycle: s0 refers to itself" */ = s0
    12  
    13  	x0 /* ERROR "initialization cycle for x0" */ = y0
    14  	y0 = x0
    15  
    16  	a0 = b0
    17  	b0 /* ERROR "initialization cycle for b0" */ = c0
    18  	c0 = d0
    19  	d0 = b0
    20  )
    21  
    22  var (
    23  	s1 /* ERROR "initialization cycle: s1 refers to itself" */ = s1
    24  
    25  	x1 /* ERROR "initialization cycle for x1" */ = y1
    26  	y1 = x1
    27  
    28  	a1 = b1
    29  	b1 /* ERROR "initialization cycle for b1" */ = c1
    30  	c1 = d1
    31  	d1 = b1
    32  )
    33  
    34  // initialization cycles (we know the types)
    35  const (
    36  	s2 /* ERROR "initialization cycle: s2 refers to itself" */ int = s2
    37  
    38  	x2 /* ERROR "initialization cycle for x2" */ int = y2
    39  	y2 = x2
    40  
    41  	a2 = b2
    42  	b2 /* ERROR "initialization cycle for b2" */ int = c2
    43  	c2 = d2
    44  	d2 = b2
    45  )
    46  
    47  var (
    48  	s3 /* ERROR "initialization cycle: s3 refers to itself" */ int = s3
    49  
    50  	x3 /* ERROR "initialization cycle for x3" */ int = y3
    51  	y3 = x3
    52  
    53  	a3 = b3
    54  	b3 /* ERROR "initialization cycle for b3" */ int = c3
    55  	c3 = d3
    56  	d3 = b3
    57  )
    58  
    59  // cycles via struct fields
    60  
    61  type S1 struct {
    62  	f int
    63  }
    64  const cx3 S1 /* ERROR "invalid constant type" */ = S1{cx3.f}
    65  var vx3 /* ERROR "initialization cycle: vx3 refers to itself" */ S1 = S1{vx3.f}
    66  
    67  // cycles via functions
    68  
    69  var x4 = x5
    70  var x5 /* ERROR "initialization cycle for x5" */ = f1()
    71  func f1() int { return x5*10 }
    72  
    73  var x6, x7 /* ERROR "initialization cycle" */ = f2()
    74  var x8 = x7
    75  func f2() (int, int) { return f3() + f3(), 0 }
    76  func f3() int { return x8 }
    77  
    78  // cycles via function literals
    79  
    80  var x9 /* ERROR "initialization cycle: x9 refers to itself" */ = func() int { return x9 }()
    81  
    82  var x10 /* ERROR "initialization cycle for x10" */ = f4()
    83  
    84  func f4() int {
    85  	_ = func() {
    86  		_ = x10
    87  	}
    88  	return 0
    89  }
    90  
    91  // cycles via method expressions
    92  
    93  type T1 struct{}
    94  
    95  func (T1) m() bool { _ = x11; return false }
    96  
    97  var x11 /* ERROR "initialization cycle for x11" */ = T1.m(T1{})
    98  
    99  // cycles via method values
   100  
   101  type T2 struct{}
   102  
   103  func (T2) m() bool { _ = x12; return false }
   104  
   105  var t1 T2
   106  var x12 /* ERROR "initialization cycle for x12" */ = t1.m
   107  

View as plain text