github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/types/testdata/check/typeparams.go (about)

     1  // Copyright 2018 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  // import "io" // for type assertion tests
     8  
     9  var _ any // ok to use any anywhere
    10  func _[_ any, _ interface{any}](any) {
    11          var _ any
    12  }
    13  
    14  func identity[T any](x T) T { return x }
    15  
    16  func _[_ any](x int) int { panic(0) }
    17  func _[T any](T /* ERROR "redeclared" */ T)() {}
    18  func _[T, T /* ERROR "redeclared" */ any]() {}
    19  
    20  // Constraints (incl. any) may be parenthesized.
    21  func _[_ (any)]() {}
    22  func _[_ (interface{})]() {}
    23  
    24  func reverse[T any](list []T) []T {
    25          rlist := make([]T, len(list))
    26          i := len(list)
    27          for _, x := range list {
    28                  i--
    29                  rlist[i] = x
    30          }
    31          return rlist
    32  }
    33  
    34  var _ = reverse /* ERROR "cannot use generic function reverse" */
    35  var _ = reverse[int, float32 /* ERROR "got 2 type arguments" */ ] ([]int{1, 2, 3})
    36  var _ = reverse[int]([ /* ERROR "cannot use" */ ]float32{1, 2, 3})
    37  var f = reverse[chan int]
    38  var _ = f(0 /* ERRORx `cannot use 0 .* as \[\]chan int` */ )
    39  
    40  func swap[A, B any](a A, b B) (B, A) { return b, a }
    41  
    42  var _ = swap /* ERROR "multiple-value" */ [int, float32](1, 2)
    43  var f32, i = swap[int, float32](swap[float32, int](1, 2))
    44  var _ float32 = f32
    45  var _ int = i
    46  
    47  func swapswap[A, B any](a A, b B) (A, B) {
    48          return swap[B, A](b, a)
    49  }
    50  
    51  type F[A, B any] func(A, B) (B, A)
    52  
    53  func min[T interface{ ~int }](x, y T) T {
    54          if x < y {
    55                  return x
    56          }
    57          return y
    58  }
    59  
    60  func _[T interface{~int | ~float32}](x, y T) bool { return x < y }
    61  func _[T any](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
    62  func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
    63  
    64  func _[T C1[T]](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
    65  func _[T C2[T]](x, y T) bool { return x < y }
    66  
    67  type C1[T any] interface{}
    68  type C2[T any] interface{ ~int | ~float32 }
    69  
    70  func new[T any]() *T {
    71          var x T
    72          return &x
    73  }
    74  
    75  var _ = new /* ERROR "cannot use generic function new" */
    76  var _ *int = new[int]()
    77  
    78  func _[T any](map[T /* ERROR "invalid map key type T (missing comparable constraint)" */]int) {} // w/o constraint we don't know if T is comparable
    79  
    80  func f1[T1 any](struct{T1 /* ERRORx `cannot be a .* type parameter` */ }) int { panic(0) }
    81  var _ = f1[int](struct{T1}{})
    82  type T1 = int
    83  
    84  func f2[t1 any](struct{t1 /* ERRORx `cannot be a .* type parameter` */ ; x float32}) int { panic(0) }
    85  var _ = f2[t1](struct{t1; x float32}{})
    86  type t1 = int
    87  
    88  
    89  func f3[A, B, C any](A, struct{x B}, func(A, struct{x B}, *C)) int { panic(0) }
    90  
    91  var _ = f3[int, rune, bool](1, struct{x rune}{}, nil)
    92  
    93  // indexing
    94  
    95  func _[T any] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
    96  func _[T interface{ ~int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
    97  func _[T interface{ ~string }] (x T, i int) { _ = x[i] }
    98  func _[T interface{ ~[]int }] (x T, i int) { _ = x[i] }
    99  func _[T interface{ ~[10]int | ~*[20]int | ~map[int]int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
   100  func _[T interface{ ~string | ~[]byte }] (x T, i int) { _ = x[i] }
   101  func _[T interface{ ~[]int | ~[1]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
   102  func _[T interface{ ~string | ~[]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
   103  
   104  // indexing with various combinations of map types in type sets (see issue #42616)
   105  func _[T interface{ ~[]E | ~map[int]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
   106  func _[T interface{ ~[]E }, E any](x T, i int) { _ = &x[i] }
   107  func _[T interface{ ~map[int]E }, E any](x T, i int) { _, _ = x[i] } // comma-ok permitted
   108  func _[T interface{ ~map[int]E }, E any](x T, i int) { _ = &x /* ERROR "cannot take address" */ [i] }
   109  func _[T interface{ ~map[int]E | ~map[uint]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // different map element types
   110  func _[T interface{ ~[]E | ~map[string]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
   111  
   112  // indexing with various combinations of array and other types in type sets
   113  func _[T interface{ [10]int }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
   114  func _[T interface{ [10]byte | string }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
   115  func _[T interface{ [10]int | *[20]int | []int }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
   116  
   117  // indexing with strings and non-variable arrays (assignment not permitted)
   118  func _[T string](x T) { _ = x[0]; x /* ERROR "cannot assign" */ [0] = 0 }
   119  func _[T []byte | string](x T) { x /* ERROR "cannot assign" */ [0] = 0 }
   120  func _[T [10]byte]() { f := func() (x T) { return }; f /* ERROR "cannot assign" */ ()[0] = 0 }
   121  func _[T [10]byte]() { f := func() (x *T) { return }; f /* ERROR "cannot index" */ ()[0] = 0 }
   122  func _[T [10]byte]() { f := func() (x *T) { return }; (*f())[0] = 0 }
   123  func _[T *[10]byte]() { f := func() (x T) { return }; f()[0] = 0 }
   124  
   125  // slicing
   126  
   127  func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j] }
   128  func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j:k] }
   129  func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j] }
   130  func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j:k] }
   131  func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x[i:j] }
   132  func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x[i:j:k /* ERROR "3-index slice of string" */ ] }
   133  
   134  type myByte1 []byte
   135  type myByte2 []byte
   136  func _[T interface{ []byte | myByte1 | myByte2 }] (x T, i, j, k int) { var _ T = x[i:j:k] }
   137  func _[T interface{ []byte | myByte1 | []int }] (x T, i, j, k int) { var _ T = x /* ERROR "no core type" */ [i:j:k] }
   138  
   139  func _[T interface{ []byte | myByte1 | myByte2 | string }] (x T, i, j, k int) { var _ T = x[i:j] }
   140  func _[T interface{ []byte | myByte1 | myByte2 | string }] (x T, i, j, k int) { var _ T = x[i:j:k /* ERROR "3-index slice of string" */ ] }
   141  func _[T interface{ []byte | myByte1 | []int | string }] (x T, i, j, k int) { var _ T = x /* ERROR "no core type" */ [i:j] }
   142  
   143  // len/cap built-ins
   144  
   145  func _[T any](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
   146  func _[T interface{ ~int }](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
   147  func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
   148  func _[T interface{ ~string }](x T) { _ = len(x) }
   149  func _[T interface{ ~[10]int }](x T) { _ = len(x) }
   150  func _[T interface{ ~[]byte }](x T) { _ = len(x) }
   151  func _[T interface{ ~map[int]int }](x T) { _ = len(x) }
   152  func _[T interface{ ~chan int }](x T) { _ = len(x) }
   153  func _[T interface{ ~string | ~[]byte | ~chan int }](x T) { _ = len(x) }
   154  
   155  func _[T any](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
   156  func _[T interface{ ~int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
   157  func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
   158  func _[T interface{ ~string }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
   159  func _[T interface{ ~[10]int }](x T) { _ = cap(x) }
   160  func _[T interface{ ~[]byte }](x T) { _ = cap(x) }
   161  func _[T interface{ ~map[int]int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
   162  func _[T interface{ ~chan int }](x T) { _ = cap(x) }
   163  func _[T interface{ ~[]byte | ~chan int }](x T) { _ = cap(x) }
   164  
   165  // range iteration
   166  
   167  func _[T interface{}](x T) {
   168          for range x /* ERROR "cannot range" */ {}
   169  }
   170  
   171  type myString string
   172  
   173  func _[
   174          B1 interface{ string },
   175          B2 interface{ string | myString },
   176  
   177          C1 interface{ chan int },
   178          C2 interface{ chan int | <-chan int },
   179          C3 interface{ chan<- int },
   180  
   181          S1 interface{ []int },
   182          S2 interface{ []int | [10]int },
   183  
   184          A1 interface{ [10]int },
   185          A2 interface{ [10]int | []int },
   186  
   187          P1 interface{ *[10]int },
   188          P2 interface{ *[10]int | *[]int },
   189  
   190          M1 interface{ map[string]int },
   191          M2 interface{ map[string]int | map[string]string },
   192  ]() {
   193          var b0 string
   194          for range b0 {}
   195          for _ = range b0 {}
   196          for _, _ = range b0 {}
   197  
   198          var b1 B1
   199          for range b1 {}
   200          for _ = range b1 {}
   201          for _, _ = range b1 {}
   202  
   203          var b2 B2
   204          for range b2 {}
   205  
   206          var c0 chan int
   207          for range c0 {}
   208          for _ = range c0 {}
   209          for _, _ /* ERROR "permits only one iteration variable" */ = range c0 {}
   210  
   211          var c1 C1
   212          for range c1 {}
   213          for _ = range c1 {}
   214          for _, _ /* ERROR "permits only one iteration variable" */ = range c1 {}
   215  
   216          var c2 C2
   217          for range c2 {}
   218  
   219          var c3 C3
   220          for range c3 /* ERROR "receive from send-only channel" */ {}
   221  
   222          var s0 []int
   223          for range s0 {}
   224          for _ = range s0 {}
   225          for _, _ = range s0 {}
   226  
   227          var s1 S1
   228          for range s1 {}
   229          for _ = range s1 {}
   230          for _, _ = range s1 {}
   231  
   232          var s2 S2
   233          for range s2 /* ERRORx `cannot range over s2.*no core type` */ {}
   234  
   235          var a0 []int
   236          for range a0 {}
   237          for _ = range a0 {}
   238          for _, _ = range a0 {}
   239  
   240          var a1 A1
   241          for range a1 {}
   242          for _ = range a1 {}
   243          for _, _ = range a1 {}
   244  
   245          var a2 A2
   246          for range a2 /* ERRORx `cannot range over a2.*no core type` */ {}
   247  
   248          var p0 *[10]int
   249          for range p0 {}
   250          for _ = range p0 {}
   251          for _, _ = range p0 {}
   252  
   253          var p1 P1
   254          for range p1 {}
   255          for _ = range p1 {}
   256          for _, _ = range p1 {}
   257  
   258          var p2 P2
   259          for range p2 /* ERRORx `cannot range over p2.*no core type` */ {}
   260  
   261          var m0 map[string]int
   262          for range m0 {}
   263          for _ = range m0 {}
   264          for _, _ = range m0 {}
   265  
   266          var m1 M1
   267          for range m1 {}
   268          for _ = range m1 {}
   269          for _, _ = range m1 {}
   270  
   271          var m2 M2
   272          for range m2 /* ERRORx `cannot range over m2.*no core type` */ {}
   273  }
   274  
   275  // type inference checks
   276  
   277  var _ = new /* ERROR "cannot infer T" */ ()
   278  
   279  func f4[A, B, C any](A, B) C { panic(0) }
   280  
   281  var _ = f4 /* ERROR "cannot infer C" */ (1, 2)
   282  var _ = f4[int, float32, complex128](1, 2)
   283  
   284  func f5[A, B, C any](A, []*B, struct{f []C}) int { panic(0) }
   285  
   286  var _ = f5[int, float32, complex128](0, nil, struct{f []complex128}{})
   287  var _ = f5 /* ERROR "cannot infer" */ (0, nil, struct{f []complex128}{})
   288  var _ = f5(0, []*float32{new[float32]()}, struct{f []complex128}{})
   289  
   290  func f6[A any](A, []A) int { panic(0) }
   291  
   292  var _ = f6(0, nil)
   293  
   294  func f6nil[A any](A) int { panic(0) }
   295  
   296  var _ = f6nil /* ERROR "cannot infer" */ (nil)
   297  
   298  // type inference with variadic functions
   299  
   300  func f7[T any](...T) T { panic(0) }
   301  
   302  var _ int = f7 /* ERROR "cannot infer T" */ ()
   303  var _ int = f7(1)
   304  var _ int = f7(1, 2)
   305  var _ int = f7([]int{}...)
   306  var _ int = f7 /* ERROR "cannot use" */ ([]float64{}...)
   307  var _ float64 = f7([]float64{}...)
   308  var _ = f7[float64](1, 2.3)
   309  var _ = f7(float64(1), 2.3)
   310  var _ = f7(1, 2.3)
   311  var _ = f7(1.2, 3)
   312  
   313  func f8[A, B any](A, B, ...B) int { panic(0) }
   314  
   315  var _ = f8(1) /* ERROR "not enough arguments" */
   316  var _ = f8(1, 2.3)
   317  var _ = f8(1, 2.3, 3.4, 4.5)
   318  var _ = f8(1, 2.3, 3.4, 4)
   319  var _ = f8[int, float64](1, 2.3, 3.4, 4)
   320  
   321  var _ = f8[int, float64](0, 0, nil...) // test case for #18268
   322  
   323  // init functions cannot have type parameters
   324  
   325  func init() {}
   326  func init[_ /* ERROR "func init must have no type parameters" */ any]() {}
   327  func init[P /* ERROR "func init must have no type parameters" */ any]() {}
   328  
   329  type T struct {}
   330  
   331  func (T) m1() {}
   332  func (T) m2[ /* ERROR "method must have no type parameters" */ _ any]() {}
   333  func (T) m3[ /* ERROR "method must have no type parameters" */ P any]() {}
   334  
   335  // type inference across parameterized types
   336  
   337  type S1[P any] struct { f P }
   338  
   339  func f9[P any](x S1[P]) {}
   340  
   341  func _() {
   342          f9[int](S1[int]{42})
   343  	f9(S1[int]{42})
   344  }
   345  
   346  type S2[A, B, C any] struct{}
   347  
   348  func f10[X, Y, Z any](a S2[X, int, Z], b S2[X, Y, bool]) {}
   349  
   350  func _[P any]() {
   351          f10[int, float32, string](S2[int, int, string]{}, S2[int, float32, bool]{})
   352          f10(S2[int, int, string]{}, S2[int, float32, bool]{})
   353          f10(S2[P, int, P]{}, S2[P, float32, bool]{})
   354  }
   355  
   356  // corner case for type inference
   357  // (was bug: after instantiating f11, the type-checker didn't mark f11 as non-generic)
   358  
   359  func f11[T any]() {}
   360  
   361  func _() {
   362  	f11[int]()
   363  }
   364  
   365  // the previous example was extracted from
   366  
   367  // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
   368  // func f12[T interface{m() T}]() {}
   369  // 
   370  // type A[T any] T
   371  // 
   372  // func (a A[T]) m() A[T]
   373  // 
   374  // func _[T any]() {
   375  // 	f12[A[T]]()
   376  // }
   377  
   378  // method expressions
   379  
   380  func (_ S1[P]) m()
   381  
   382  func _() {
   383  	m := S1[int].m
   384  	m(struct { f int }{42})
   385  }
   386  
   387  func _[T any] (x T) {
   388          m := S1[T].m
   389          m(S1[T]{x})
   390  }
   391  
   392  type I1[A any] interface {
   393          m1(A)
   394  }
   395  
   396  var _ I1[int] = r1[int]{}
   397  
   398  type r1[T any] struct{}
   399  
   400  func (_ r1[T]) m1(T)
   401  
   402  type I2[A, B any] interface {
   403          m1(A)
   404          m2(A) B
   405  }
   406  
   407  var _ I2[int, float32] = R2[int, float32]{}
   408  
   409  type R2[P, Q any] struct{}
   410  
   411  func (_ R2[X, Y]) m1(X)
   412  func (_ R2[X, Y]) m2(X) Y
   413  
   414  // type assertions and type switches over generic types
   415  // NOTE: These are currently disabled because it's unclear what the correct
   416  // approach is, and one can always work around by assigning the variable to
   417  // an interface first.
   418  
   419  // // ReadByte1 corresponds to the ReadByte example in the draft design.
   420  // func ReadByte1[T io.Reader](r T) (byte, error) {
   421  // 	if br, ok := r.(io.ByteReader); ok {
   422  // 		return br.ReadByte()
   423  // 	}
   424  // 	var b [1]byte
   425  // 	_, err := r.Read(b[:])
   426  // 	return b[0], err
   427  // }
   428  //
   429  // // ReadBytes2 is like ReadByte1 but uses a type switch instead.
   430  // func ReadByte2[T io.Reader](r T) (byte, error) {
   431  //         switch br := r.(type) {
   432  //         case io.ByteReader:
   433  //                 return br.ReadByte()
   434  //         }
   435  // 	var b [1]byte
   436  // 	_, err := r.Read(b[:])
   437  // 	return b[0], err
   438  // }
   439  //
   440  // // type assertions and type switches over generic types are strict
   441  // type I3 interface {
   442  //         m(int)
   443  // }
   444  //
   445  // type I4 interface {
   446  //         m() int // different signature from I3.m
   447  // }
   448  //
   449  // func _[T I3](x I3, p T) {
   450  //         // type assertions and type switches over interfaces are not strict
   451  //         _ = x.(I4)
   452  //         switch x.(type) {
   453  //         case I4:
   454  //         }
   455  //
   456  //         // type assertions and type switches over generic types are strict
   457  //         _ = p /* ERROR "cannot have dynamic type I4" */.(I4)
   458  //         switch p.(type) {
   459  //         case I4 /* ERROR "cannot have dynamic type I4" */ :
   460  //         }
   461  // }
   462  
   463  // type assertions and type switches over generic types lead to errors for now
   464  
   465  func _[T any](x T) {
   466  	_ = x /* ERROR "cannot use type assertion" */ .(int)
   467  	switch x /* ERROR "cannot use type switch" */ .(type) {
   468  	}
   469  
   470  	// work-around
   471  	var t interface{} = x
   472  	_ = t.(int)
   473  	switch t.(type) {
   474  	}
   475  }
   476  
   477  func _[T interface{~int}](x T) {
   478  	_ = x /* ERROR "cannot use type assertion" */ .(int)
   479  	switch x /* ERROR "cannot use type switch" */ .(type) {
   480  	}
   481  
   482  	// work-around
   483  	var t interface{} = x
   484  	_ = t.(int)
   485  	switch t.(type) {
   486  	}
   487  }
   488  
   489  // error messages related to type bounds mention those bounds
   490  type C[P any] interface{}
   491  
   492  func _[P C[P]] (x P) {
   493  	x.m /* ERROR "x.m undefined" */ ()
   494  }
   495  
   496  type I interface {}
   497  
   498  func _[P I] (x P) {
   499  	x.m /* ERROR "type P has no field or method m" */ ()
   500  }
   501  
   502  func _[P interface{}] (x P) {
   503  	x.m /* ERROR "type P has no field or method m" */ ()
   504  }
   505  
   506  func _[P any] (x P) {
   507  	x.m /* ERROR "type P has no field or method m" */ ()
   508  }