...

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

Documentation: internal/types/testdata/check

     1  // Copyright 2016 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 aliases
     6  
     7  package decls4
     8  
     9  type (
    10  	T0 [10]int
    11  	T1 []byte
    12  	T2 struct {
    13  		x int
    14  	}
    15  	T3 interface{
    16  		m() T2
    17  	}
    18  	T4 func(int, T0) chan T2
    19  )
    20  
    21  type (
    22  	Ai = int
    23  	A0 = T0
    24  	A1 = T1
    25  	A2 = T2
    26  	A3 = T3
    27  	A4 = T4
    28  
    29  	A10 = [10]int
    30  	A11 = []byte
    31  	A12 = struct {
    32  		x int
    33  	}
    34  	A13 = interface{
    35  		m() A2
    36  	}
    37  	A14 = func(int, A0) chan A2
    38  )
    39  
    40  // check assignment compatibility due to equality of types
    41  var (
    42  	xi_ int
    43  	ai Ai = xi_
    44  
    45  	x0 T0
    46  	a0 A0 = x0
    47  
    48  	x1 T1
    49  	a1 A1 = x1
    50  
    51  	x2 T2
    52  	a2 A2 = x2
    53  
    54  	x3 T3
    55  	a3 A3 = x3
    56  
    57  	x4 T4
    58  	a4 A4 = x4
    59  )
    60  
    61  // alias receiver types
    62  func (Ai /* ERRORx "cannot define new methods on non-local type (int|Ai)" */) m1() {}
    63  func (T0) m1() {}
    64  func (A0) m1 /* ERROR "already declared" */ () {}
    65  func (A0) m2 () {}
    66  func (A3 /* ERROR "invalid receiver" */ ) m1 () {}
    67  func (A10 /* ERROR "invalid receiver" */ ) m1() {}
    68  
    69  // x0 has methods m1, m2 declared via receiver type names T0 and A0
    70  var _ interface{ m1(); m2() } = x0
    71  
    72  // alias receiver types (test case for issue #23042)
    73  type T struct{}
    74  
    75  var (
    76  	_ = T.m
    77  	_ = T{}.m
    78  	_ interface{m()} = T{}
    79  )
    80  
    81  var (
    82  	_ = T.n
    83  	_ = T{}.n
    84  	_ interface{m(); n()} = T{}
    85  )
    86  
    87  type U = T
    88  func (U) m() {}
    89  
    90  // alias receiver types (long type declaration chains)
    91  type (
    92  	V0 = V1
    93  	V1 = (V2)
    94  	V2 = ((V3))
    95  	V3 = T
    96  )
    97  
    98  func (V0) m /* ERROR "already declared" */ () {}
    99  func (V1) n() {}
   100  
   101  // alias receiver types (invalid due to cycles)
   102  type (
   103  	W0 /* ERROR "invalid recursive type" */ = W1
   104  	W1 = (W2)
   105  	W2 = ((W0))
   106  )
   107  
   108  func (W0) m() {} // no error expected (due to above cycle error)
   109  func (W1) n() {}
   110  
   111  // alias receiver types (invalid due to builtin underlying type)
   112  type (
   113  	B0 = B1
   114  	B1 = B2
   115  	B2 = int
   116  )
   117  
   118  func (B0 /* ERRORx "cannot define new methods on non-local type (int|B)" */ ) m() {}
   119  func (B1 /* ERRORx "cannot define new methods on non-local type (int|B)" */ ) n() {}
   120  
   121  // cycles
   122  type (
   123  	C2 /* ERROR "invalid recursive type" */ = C2
   124  	C3 /* ERROR "invalid recursive type" */ = C4
   125  	C4 = C3
   126  	C5 struct {
   127  		f *C6
   128  	}
   129  	C6 = C5
   130  	C7 /* ERROR "invalid recursive type" */  struct {
   131  		f C8
   132  	}
   133  	C8 = C7
   134  )
   135  
   136  // embedded fields
   137  var (
   138  	s0 struct { T0 }
   139  	s1 struct { A0 } = s0 /* ERROR "cannot use" */ // embedded field names are different
   140  )
   141  
   142  // embedding and lookup of fields and methods
   143  func _(s struct{A0}) { s.A0 = x0 }
   144  
   145  type eX struct{xf int}
   146  
   147  func (eX) xm()
   148  
   149  type eY = struct{eX} // field/method set of eY includes xf, xm
   150  
   151  type eZ = *struct{eX} // field/method set of eZ includes xf, xm
   152  
   153  type eA struct {
   154  	eX // eX contributes xf, xm to eA
   155  }
   156  
   157  type eA2 struct {
   158  	*eX // *eX contributes xf, xm to eA
   159  }
   160  
   161  type eB struct {
   162  	eY // eY contributes xf, xm to eB
   163  }
   164  
   165  type eB2 struct {
   166  	*eY // *eY contributes xf, xm to eB
   167  }
   168  
   169  type eC struct {
   170  	eZ // eZ contributes xf, xm to eC
   171  }
   172  
   173  var (
   174  	_ = eA{}.xf
   175  	_ = eA{}.xm
   176  	_ = eA2{}.xf
   177  	_ = eA2{}.xm
   178  	_ = eB{}.xf
   179  	_ = eB{}.xm
   180  	_ = eB2{}.xf
   181  	_ = eB2{}.xm
   182  	_ = eC{}.xf
   183  	_ = eC{}.xm
   184  )
   185  
   186  // ambiguous selectors due to embedding via type aliases
   187  type eD struct {
   188  	eY
   189  	eZ
   190  }
   191  
   192  var (
   193  	_ = eD{}.xf /* ERROR "ambiguous selector eD{}.xf" */
   194  	_ = eD{}.xm /* ERROR "ambiguous selector eD{}.xm" */
   195  )
   196  
   197  var (
   198  	_ interface{ xm() } = eD /* ERROR "ambiguous selector eD.xm" */ {}
   199  )

View as plain text