github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/testdata/decls4.src (about)

     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 /* ERROR "invalid receiver" */) 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  // cycles
    73  type (
    74  	C2 /* ERROR illegal cycle */ = C2
    75  	C3 /* ERROR illegal cycle */ = C4
    76  	C4 = C3
    77  	C5 struct {
    78  		f *C6
    79  	}
    80  	C6 = C5
    81  	C7 /* ERROR illegal cycle */  struct {
    82  		f C8
    83  	}
    84  	C8 = C7
    85  )
    86  
    87  // embedded fields
    88  var (
    89  	s0 struct { T0 }
    90  	s1 struct { A0 } = s0 /* ERROR cannot use */ // embedded field names are different
    91  )
    92  
    93  // embedding and lookup of fields and methods
    94  func _(s struct{A0}) { s.A0 = x0 }
    95  
    96  type eX struct{xf int}
    97  
    98  func (eX) xm()
    99  
   100  type eY = struct{eX} // field/method set of eY includes xf, xm
   101  
   102  type eZ = *struct{eX} // field/method set of eZ includes xf, xm
   103  
   104  type eA struct {
   105  	eX // eX contributes xf, xm to eA
   106  }
   107  
   108  type eA2 struct {
   109  	*eX // *eX contributes xf, xm to eA
   110  }
   111  
   112  type eB struct {
   113  	eY // eY contributes xf, xm to eB
   114  }
   115  
   116  type eB2 struct {
   117  	*eY // *eY contributes xf, xm to eB
   118  }
   119  
   120  type eC struct {
   121  	eZ // eZ contributes xf, xm to eC
   122  }
   123  
   124  var (
   125  	_ = eA{}.xf
   126  	_ = eA{}.xm
   127  	_ = eA2{}.xf
   128  	_ = eA2{}.xm
   129  	_ = eB{}.xf
   130  	_ = eB{}.xm
   131  	_ = eB2{}.xf
   132  	_ = eB2{}.xm
   133  	_ = eC{}.xf
   134  	_ = eC{}.xm
   135  )
   136  
   137  // ambiguous selectors due to embedding via type aliases
   138  type eD struct {
   139  	eY
   140  	eZ
   141  }
   142  
   143  var (
   144  	_ = eD /* ERROR ambiguous selector */ {}.xf
   145  	_ = eD /* ERROR ambiguous selector */ {}.xm
   146  )
   147  
   148  var (
   149  	_ interface{ xm() } = eD /* ERROR missing method xm */ {}
   150  )