Black Lives Matter. Support the Equal Justice Initiative.

Text file src/go/types/testdata/check/decls3.src

Documentation: go/types/testdata/check

     1  // Copyright 2012 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  // embedded types
     6  
     7  package decls3
     8  
     9  import "unsafe"
    10  import "fmt"
    11  
    12  // fields with the same name at the same level cancel each other out
    13  
    14  func _() {
    15  	type (
    16  		T1 struct { X int }
    17  		T2 struct { X int }
    18  		T3 struct { T1; T2 } // X is embedded twice at the same level via T1->X, T2->X
    19  	)
    20  
    21  	var t T3
    22  	_ = t.X /* ERROR "ambiguous selector t.X" */
    23  }
    24  
    25  func _() {
    26  	type (
    27  		T1 struct { X int }
    28  		T2 struct { T1 }
    29  		T3 struct { T1 }
    30  		T4 struct { T2; T3 } // X is embedded twice at the same level via T2->T1->X, T3->T1->X
    31  	)
    32  
    33  	var t T4
    34  	_ = t.X /* ERROR "ambiguous selector t.X" */
    35  }
    36  
    37  func issue4355() {
    38  	type (
    39  	    T1 struct {X int}
    40  	    T2 struct {T1}
    41  	    T3 struct {T2}
    42  	    T4 struct {T2}
    43  	    T5 struct {T3; T4} // X is embedded twice at the same level via T3->T2->T1->X, T4->T2->T1->X
    44  	)
    45  
    46  	var t T5
    47  	_ = t.X /* ERROR "ambiguous selector t.X" */
    48  }
    49  
    50  func _() {
    51  	type State int
    52  	type A struct{ State }
    53  	type B struct{ fmt.State }
    54  	type T struct{ A; B }
    55  
    56  	var t T
    57  	_ = t.State /* ERROR "ambiguous selector t.State" */
    58  }
    59  
    60  // Embedded fields can be predeclared types.
    61  
    62  func _() {
    63  	type T0 struct{
    64  		int
    65  		float32
    66  		f int
    67  	}
    68  	var x T0
    69  	_ = x.int
    70  	_ = x.float32
    71  	_ = x.f
    72  
    73  	type T1 struct{
    74  		T0
    75  	}
    76  	var y T1
    77  	_ = y.int
    78  	_ = y.float32
    79  	_ = y.f
    80  }
    81  
    82  // Restrictions on embedded field types.
    83  
    84  func _() {
    85  	type I1 interface{}
    86  	type I2 interface{}
    87  	type P1 *int
    88  	type P2 *int
    89  	type UP unsafe.Pointer
    90  
    91  	type T1 struct {
    92  		I1
    93  		* /* ERROR "cannot be a pointer to an interface" */ I2
    94  		* /* ERROR "cannot be a pointer to an interface" */ error
    95  		P1 /* ERROR "cannot be a pointer" */
    96  		* /* ERROR "cannot be a pointer" */ P2
    97  	}
    98  
    99  	// unsafe.Pointers are treated like regular pointers when embedded
   100  	type T2 struct {
   101  		unsafe /* ERROR "cannot be unsafe.Pointer" */ .Pointer
   102  		*/* ERROR "cannot be unsafe.Pointer" */ /* ERROR "Pointer redeclared" */ unsafe.Pointer
   103  		UP /* ERROR "cannot be unsafe.Pointer" */
   104  		* /* ERROR "cannot be unsafe.Pointer" */  /* ERROR "UP redeclared" */ UP
   105  	}
   106  }
   107  
   108  // Named types that are pointers.
   109  
   110  type S struct{ x int }
   111  func (*S) m() {}
   112  type P *S
   113  
   114  func _() {
   115  	var s *S
   116  	_ = s.x
   117  	_ = s.m
   118  
   119  	var p P
   120  	_ = p.x
   121  	_ = p.m /* ERROR "no field or method" */
   122  	_ = P.m /* ERROR "no field or method" */
   123  }
   124  
   125  // Borrowed from the FieldByName test cases in reflect/all_test.go.
   126  
   127  type D1 struct {
   128  	d int
   129  }
   130  type D2 struct {
   131  	d int
   132  }
   133  
   134  type S0 struct {
   135  	A, B, C int
   136  	D1
   137  	D2
   138  }
   139  
   140  type S1 struct {
   141  	B int
   142  	S0
   143  }
   144  
   145  type S2 struct {
   146  	A int
   147  	*S1
   148  }
   149  
   150  type S1x struct {
   151  	S1
   152  }
   153  
   154  type S1y struct {
   155  	S1
   156  }
   157  
   158  type S3 struct {
   159  	S1x
   160  	S2
   161  	D, E int
   162  	*S1y
   163  }
   164  
   165  type S4 struct {
   166  	*S4
   167  	A int
   168  }
   169  
   170  // The X in S6 and S7 annihilate, but they also block the X in S8.S9.
   171  type S5 struct {
   172  	S6
   173  	S7
   174  	S8
   175  }
   176  
   177  type S6 struct {
   178  	X int
   179  }
   180  
   181  type S7 S6
   182  
   183  type S8 struct {
   184  	S9
   185  }
   186  
   187  type S9 struct {
   188  	X int
   189  	Y int
   190  }
   191  
   192  // The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
   193  type S10 struct {
   194  	S11
   195  	S12
   196  	S13
   197  }
   198  
   199  type S11 struct {
   200  	S6
   201  }
   202  
   203  type S12 struct {
   204  	S6
   205  }
   206  
   207  type S13 struct {
   208  	S8
   209  }
   210  
   211  func _() {
   212  	_ = struct{}{}.Foo /* ERROR "no field or method" */
   213  	_ = S0{}.A
   214  	_ = S0{}.D /* ERROR "no field or method" */
   215  	_ = S1{}.A
   216  	_ = S1{}.B
   217  	_ = S1{}.S0
   218  	_ = S1{}.C
   219  	_ = S2{}.A
   220  	_ = S2{}.S1
   221  	_ = S2{}.B
   222  	_ = S2{}.C
   223  	_ = S2{}.D /* ERROR "no field or method" */
   224  	_ = S3{}.S1 /* ERROR "ambiguous selector \(S3 literal\).S1" */
   225  	_ = S3{}.A
   226  	_ = S3{}.B /* ERROR "ambiguous selector" \(S3 literal\).B */
   227  	_ = S3{}.D
   228  	_ = S3{}.E
   229  	_ = S4{}.A
   230  	_ = S4{}.B /* ERROR "no field or method" */
   231  	_ = S5{}.X /* ERROR "ambiguous selector \(S5 literal\).X" */
   232  	_ = S5{}.Y
   233  	_ = S10{}.X /* ERROR "ambiguous selector \(S10 literal\).X" */
   234  	_ = S10{}.Y
   235  }
   236  
   237  // Borrowed from the FieldByName benchmark in reflect/all_test.go.
   238  
   239  type R0 struct {
   240  	*R1
   241  	*R2
   242  	*R3
   243  	*R4
   244  }
   245  
   246  type R1 struct {
   247  	*R5
   248  	*R6
   249  	*R7
   250  	*R8
   251  }
   252  
   253  type R2 R1
   254  type R3 R1
   255  type R4 R1
   256  
   257  type R5 struct {
   258  	*R9
   259  	*R10
   260  	*R11
   261  	*R12
   262  }
   263  
   264  type R6 R5
   265  type R7 R5
   266  type R8 R5
   267  
   268  type R9 struct {
   269  	*R13
   270  	*R14
   271  	*R15
   272  	*R16
   273  }
   274  
   275  type R10 R9
   276  type R11 R9
   277  type R12 R9
   278  
   279  type R13 struct {
   280  	*R17
   281  	*R18
   282  	*R19
   283  	*R20
   284  }
   285  
   286  type R14 R13
   287  type R15 R13
   288  type R16 R13
   289  
   290  type R17 struct {
   291  	*R21
   292  	*R22
   293  	*R23
   294  	*R24
   295  }
   296  
   297  type R18 R17
   298  type R19 R17
   299  type R20 R17
   300  
   301  type R21 struct {
   302  	X int
   303  }
   304  
   305  type R22 R21
   306  type R23 R21
   307  type R24 R21
   308  
   309  var _ = R0{}.X /* ERROR "ambiguous selector \(R0 literal\).X" */

View as plain text