github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/go/types/testdata/decls0.src (about)

     1  // Copyright 2011 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  // type declarations
     6  
     7  package decls0
     8  
     9  import "unsafe"
    10  
    11  const pi = 3.1415
    12  
    13  type (
    14  	N undeclared /* ERROR "undeclared" */
    15  	B bool
    16  	I int32
    17  	A [10]P
    18  	T struct {
    19  		x, y P
    20  	}
    21  	P *T
    22  	R (*R)
    23  	F func(A) I
    24  	Y interface {
    25  		f(A) I
    26  	}
    27  	S [](((P)))
    28  	M map[I]F
    29  	C chan<- I
    30  
    31  	// blank types must be typechecked
    32  	_ pi /* ERROR "not a type" */
    33  	_ struct{}
    34  	_ struct{ pi /* ERROR "not a type" */ }
    35  )
    36  
    37  
    38  // declarations of init
    39  const _, init /* ERROR "cannot declare init" */ , _ = 0, 1, 2
    40  type init /* ERROR "cannot declare init" */ struct{}
    41  var _, init /* ERROR "cannot declare init" */ int
    42  
    43  func init() {}
    44  func init /* ERROR "missing function body" */ ()
    45  
    46  func _() { const init = 0 }
    47  func _() { type init int }
    48  func _() { var init int; _ = init }
    49  
    50  // invalid array types
    51  type (
    52  	iA0 [... /* ERROR "invalid use of '...'" */ ]byte
    53  	iA1 [1 /* ERROR "invalid array length" */ <<100]int
    54  	iA2 [- /* ERROR "invalid array length" */ 1]complex128
    55  	iA3 ["foo" /* ERROR "must be integer" */ ]string
    56  	iA4 [float64 /* ERROR "must be integer" */ (0)]int
    57  )
    58  
    59  
    60  type (
    61  	p1 pi /* ERROR "no field or method foo" */ .foo
    62  	p2 unsafe.Pointer
    63  )
    64  
    65  
    66  type (
    67  	Pi pi /* ERROR "not a type" */
    68  
    69  	a /* ERROR "illegal cycle" */ a
    70  	a /* ERROR "redeclared" */ int
    71  
    72  	// where the cycle error appears depends on the
    73  	// order in which declarations are processed
    74  	// (which depends on the order in which a map
    75  	// is iterated through)
    76  	b /* ERROR "illegal cycle" */ c
    77  	c d
    78  	d e
    79  	e b
    80  
    81  	t *t
    82  
    83  	U V
    84  	V *W
    85  	W U
    86  
    87  	P1 *S2
    88  	P2 P1
    89  
    90  	S0 struct {
    91  	}
    92  	S1 struct {
    93  		a, b, c int
    94  		u, v, a /* ERROR "redeclared" */ float32
    95  	}
    96  	S2 struct {
    97  		S0 // anonymous field
    98  		S0 /* ERROR "redeclared" */ int
    99  	}
   100  	S3 struct {
   101  		x S2
   102  	}
   103  	S4/* ERROR "illegal cycle" */ struct {
   104  		S4
   105  	}
   106  	S5 /* ERROR "illegal cycle" */ struct {
   107  		S6
   108  	}
   109  	S6 struct {
   110  		field S7
   111  	}
   112  	S7 struct {
   113  		S5
   114  	}
   115  
   116  	L1 []L1
   117  	L2 []int
   118  
   119  	A1 [10.0]int
   120  	A2 /* ERROR "illegal cycle" */ [10]A2
   121  	A3 /* ERROR "illegal cycle" */ [10]struct {
   122  		x A4
   123  	}
   124  	A4 [10]A3
   125  
   126  	F1 func()
   127  	F2 func(x, y, z float32)
   128  	F3 func(x, y, x /* ERROR "redeclared" */ float32)
   129  	F4 func() (x, y, x /* ERROR "redeclared" */ float32)
   130  	F5 func(x int) (x /* ERROR "redeclared" */ float32)
   131  	F6 func(x ...int)
   132  
   133  	I1 interface{}
   134  	I2 interface {
   135  		m1()
   136  	}
   137  	I3 interface {
   138  		m1()
   139  		m1 /* ERROR "redeclared" */ ()
   140  	}
   141  	I4 interface {
   142  		m1(x, y, x /* ERROR "redeclared" */ float32)
   143  		m2() (x, y, x /* ERROR "redeclared" */ float32)
   144  		m3(x int) (x /* ERROR "redeclared" */ float32)
   145  	}
   146  	I5 interface {
   147  		m1(I5)
   148  	}
   149  	I6 interface {
   150  		S0 /* ERROR "not an interface" */
   151  	}
   152  	I7 interface {
   153  		I1
   154  		I1
   155  	}
   156  	I8 /* ERROR "illegal cycle" */ interface {
   157  		I8
   158  	}
   159  	I9 interface {
   160  		I10
   161  	}
   162  	I10 interface {
   163  		I11
   164  	}
   165  	I11 /* ERROR "illegal cycle" */ interface {
   166  		I9
   167  	}
   168  
   169  	C1 chan int
   170  	C2 <-chan int
   171  	C3 chan<- C3
   172  	C4 chan C5
   173  	C5 chan C6
   174  	C6 chan C4
   175  
   176  	M1 map[Last]string
   177  	M2 map[string]M2
   178  
   179  	Last int
   180  )
   181  
   182  // cycles in function/method declarations
   183  // (test cases for issue 5217 and variants)
   184  func f1(x f1 /* ERROR "not a type" */ ) {}
   185  func f2(x *f2 /* ERROR "not a type" */ ) {}
   186  func f3() (x f3 /* ERROR "not a type" */ ) { return }
   187  func f4() (x *f4 /* ERROR "not a type" */ ) { return }
   188  
   189  func (S0) m1(x S0 /* ERROR "field or method" */ .m1) {}
   190  func (S0) m2(x *S0 /* ERROR "field or method" */ .m2) {}
   191  func (S0) m3() (x S0 /* ERROR "field or method" */ .m3) { return }
   192  func (S0) m4() (x *S0 /* ERROR "field or method" */ .m4) { return }
   193  
   194  // interfaces may not have any blank methods
   195  type BlankI interface {
   196  	_ /* ERROR "invalid method name" */ ()
   197  	_ /* ERROR "invalid method name" */ (float32) int
   198  	m()
   199  }
   200  
   201  // non-interface types may have multiple blank methods
   202  type BlankT struct{}
   203  
   204  func (BlankT) _() {}
   205  func (BlankT) _(int) {}
   206  func (BlankT) _() int { return 0 }
   207  func (BlankT) _(int) int { return 0}