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

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

View as plain text