Black Lives Matter. Support the Equal Justice Initiative.

Text file src/go/types/testdata/check/typeinst2.go2

Documentation: go/types/testdata/check

     1  // Copyright 2019 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  package p
     6  
     7  type List[E any] []E
     8  var _ List[List[List[int]]]
     9  var _ List[List[List[int]]] = []List[List[int]]{}
    10  
    11  type (
    12  	T1[P1 any] struct {
    13  		f1 T2[P1, float32]
    14  	}
    15  
    16  	T2[P2, P3 any] struct {
    17  		f2 P2
    18  		f3 P3
    19  	}
    20  )
    21  
    22  func _() {
    23  	var x1 T1[int]
    24  	var x2 T2[int, float32]
    25  
    26  	x1.f1.f2 = 0
    27  	x1.f1 = x2
    28  }
    29  
    30  type T3[P any] T1[T2[P, P]]
    31  
    32  func _() {
    33  	var x1 T3[int]
    34  	var x2 T2[int, int]
    35  	x1.f1.f2 = x2
    36  }
    37  
    38  func f[P any] (x P) List[P] {
    39  	return List[P]{x}
    40  }
    41  
    42  var (
    43  	_ []int = f(0)
    44  	_ []float32 = f[float32](10)
    45  	_ List[complex128] = f(1i)
    46  	_ []List[int] = f(List[int]{})
    47          _ List[List[int]] = []List[int]{}
    48          _ = []List[int]{}
    49  )
    50  
    51  // Parameterized types with methods
    52  
    53  func (l List[E]) Head() (_ E, _ bool) {
    54  	if len(l) > 0 {
    55  		return l[0], true
    56  	}
    57  	return
    58  }
    59  
    60  // A test case for instantiating types with other types (extracted from map.go2)
    61  
    62  type Pair[K any] struct {
    63  	key K
    64  }
    65  
    66  type Receiver[T any] struct {
    67  	values T
    68  }
    69  
    70  type Iterator[K any] struct {
    71  	r Receiver[Pair[K]]
    72  }
    73  
    74  func Values [T any] (r Receiver[T]) T {
    75          return r.values
    76  }
    77  
    78  func (it Iterator[K]) Next() K {
    79          return Values[Pair[K]](it.r).key
    80  }
    81  
    82  // A more complex test case testing type bounds (extracted from linalg.go2 and reduced to essence)
    83  
    84  type NumericAbs[T any] interface {
    85  	Abs() T
    86  }
    87  
    88  func AbsDifference[T NumericAbs[T]](x T)
    89  
    90  type OrderedAbs[T any] T
    91  
    92  func (a OrderedAbs[T]) Abs() OrderedAbs[T]
    93  
    94  func OrderedAbsDifference[T any](x T) {
    95  	AbsDifference(OrderedAbs[T](x))
    96  }
    97  
    98  // same code, reduced to essence
    99  
   100  func g[P interface{ m() P }](x P)
   101  
   102  type T4[P any] P
   103  
   104  func (_ T4[P]) m() T4[P]
   105  
   106  func _[Q any](x Q) {
   107  	g(T4[Q](x))
   108  }
   109  
   110  // Another test case that caused  problems in the past
   111  
   112  type T5[_ interface { a() }, _ interface{}] struct{}
   113  
   114  type A[P any] struct{ x P }
   115  
   116  func (_ A[P]) a() {}
   117  
   118  var _ T5[A[int], int]
   119  
   120  // Invoking methods with parameterized receiver types uses
   121  // type inference to determine the actual type arguments matching
   122  // the receiver type parameters from the actual receiver argument.
   123  // Go does implicit address-taking and dereferenciation depending
   124  // on the actual receiver and the method's receiver type. To make
   125  // type inference work, the type-checker matches "pointer-ness"
   126  // of the actual receiver and the method's receiver type.
   127  // The following code tests this mechanism.
   128  
   129  type R1[A any] struct{}
   130  func (_ R1[A]) vm()
   131  func (_ *R1[A]) pm()
   132  
   133  func _[T any](r R1[T], p *R1[T]) {
   134  	r.vm()
   135  	r.pm()
   136  	p.vm()
   137  	p.pm()
   138  }
   139  
   140  type R2[A, B any] struct{}
   141  func (_ R2[A, B]) vm()
   142  func (_ *R2[A, B]) pm()
   143  
   144  func _[T any](r R2[T, int], p *R2[string, T]) {
   145  	r.vm()
   146  	r.pm()
   147  	p.vm()
   148  	p.pm()
   149  }
   150  
   151  // An interface can (explicitly) declare at most one type list.
   152  type _ interface {
   153  	m0()
   154  	type int, string, bool
   155  	type /* ERROR multiple type lists */ float32, float64
   156  	m1()
   157  	m2()
   158  	type /* ERROR multiple type lists */ complex64, complex128
   159  	type /* ERROR multiple type lists */ rune
   160  }
   161  
   162  // Interface type lists may contain each type at most once.
   163  // (If there are multiple lists, we assume the author intended
   164  // for them to be all in a single list, and we report the error
   165  // as well.)
   166  type _ interface {
   167  	type int, int /* ERROR duplicate type int */
   168  	type /* ERROR multiple type lists */ int /* ERROR duplicate type int */
   169  }
   170  
   171  type _ interface {
   172  	type struct{f int}, struct{g int}, struct /* ERROR duplicate type */ {f int}
   173  }
   174  
   175  // Interface type lists can contain any type, incl. *Named types.
   176  // Verify that we use the underlying type to compute the operational type.
   177  type MyInt int
   178  func add1[T interface{type MyInt}](x T) T {
   179  	return x + 1
   180  }
   181  
   182  type MyString string
   183  func double[T interface{type MyInt, MyString}](x T) T {
   184  	return x + x
   185  }
   186  
   187  // Embedding of interfaces with type lists leads to interfaces
   188  // with type lists that are the intersection of the embedded
   189  // type lists.
   190  
   191  type E0 interface {
   192  	type int, bool, string
   193  }
   194  
   195  type E1 interface {
   196  	type int, float64, string
   197  }
   198  
   199  type E2 interface {
   200  	type float64
   201  }
   202  
   203  type I0 interface {
   204  	E0
   205  }
   206  
   207  func f0[T I0]()
   208  var _ = f0[int]
   209  var _ = f0[bool]
   210  var _ = f0[string]
   211  var _ = f0[float64 /* ERROR does not satisfy I0 */ ]
   212  
   213  type I01 interface {
   214  	E0
   215  	E1
   216  }
   217  
   218  func f01[T I01]()
   219  var _ = f01[int]
   220  var _ = f01[bool /* ERROR does not satisfy I0 */ ]
   221  var _ = f01[string]
   222  var _ = f01[float64 /* ERROR does not satisfy I0 */ ]
   223  
   224  type I012 interface {
   225  	E0
   226  	E1
   227  	E2
   228  }
   229  
   230  func f012[T I012]()
   231  var _ = f012[int /* ERROR does not satisfy I012 */ ]
   232  var _ = f012[bool /* ERROR does not satisfy I012 */ ]
   233  var _ = f012[string /* ERROR does not satisfy I012 */ ]
   234  var _ = f012[float64 /* ERROR does not satisfy I012 */ ]
   235  
   236  type I12 interface {
   237  	E1
   238  	E2
   239  }
   240  
   241  func f12[T I12]()
   242  var _ = f12[int /* ERROR does not satisfy I12 */ ]
   243  var _ = f12[bool /* ERROR does not satisfy I12 */ ]
   244  var _ = f12[string /* ERROR does not satisfy I12 */ ]
   245  var _ = f12[float64]
   246  
   247  type I0_ interface {
   248  	E0
   249  	type int
   250  }
   251  
   252  func f0_[T I0_]()
   253  var _ = f0_[int]
   254  var _ = f0_[bool /* ERROR does not satisfy I0_ */ ]
   255  var _ = f0_[string /* ERROR does not satisfy I0_ */ ]
   256  var _ = f0_[float64 /* ERROR does not satisfy I0_ */ ]
   257  

View as plain text