Source file test/typeparam/typelist.go

     1  // compile
     2  
     3  // Copyright 2021 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // This file tests type lists & constraints with core types.
     8  
     9  // Note: This test has been adjusted to use the new
    10  //       type set notation rather than type lists.
    11  
    12  package p
    13  
    14  // Assignability of an unnamed pointer type to a type parameter that
    15  // has a matching underlying type.
    16  func _[T interface{}, PT interface{ ~*T }](x T) PT {
    17  	return &x
    18  }
    19  
    20  // Indexing of generic types containing type parameters in their type list:
    21  func at[T interface{ ~[]E }, E any](x T, i int) E {
    22  	return x[i]
    23  }
    24  
    25  // A generic type inside a function acts like a named type. Its underlying
    26  // type is itself, its "operational type" is defined by the type list in
    27  // the tybe bound, if any.
    28  func _[T interface{ ~int }](x T) {
    29  	type myint int
    30  	var _ int = int(x)
    31  	var _ T = 42
    32  	var _ T = T(myint(42))
    33  }
    34  
    35  // Indexing a generic type which has a an array as core type.
    36  func _[T interface{ ~[10]int }](x T) {
    37  	_ = x[9] // ok
    38  }
    39  
    40  // Dereference of a generic type which has a pointer as core type.
    41  func _[T interface{ ~*int }](p T) int {
    42  	return *p
    43  }
    44  
    45  // Channel send and receive on a generic type which has a channel as core type.
    46  func _[T interface{ ~chan int }](ch T) int {
    47  	// This would deadlock if executed (but ok for a compile test)
    48  	ch <- 0
    49  	return <-ch
    50  }
    51  
    52  // Calling of a generic type which has a function as core type.
    53  func _[T interface{ ~func() }](f T) {
    54  	f()
    55  	go f()
    56  }
    57  
    58  // Same, but function has a parameter and return value.
    59  func _[T interface{ ~func(string) int }](f T) int {
    60  	return f("hello")
    61  }
    62  
    63  // Map access of a generic type which has a map as core type.
    64  func _[V any, T interface{ ~map[string]V }](p T) V {
    65  	return p["test"]
    66  }
    67  
    68  // Testing partial and full type inference, including the case where the types can
    69  // be inferred without needing the types of the function arguments.
    70  
    71  // Cannot embed stand-alone type parameters. Disabled for now.
    72  /*
    73  func f0[A any, B interface{type C}, C interface{type D}, D interface{type A}](A, B, C, D)
    74  func f0x() {
    75          f := f0[string]
    76          f("a", "b", "c", "d")
    77          f0("a", "b", "c", "d")
    78  }
    79  
    80  func f1[A any, B interface{type A}](A, B)
    81  func f1x() {
    82          f := f1[int]
    83          f(int(0), int(0))
    84          f1(int(0), int(0))
    85          f(0, 0)
    86          f1(0, 0)
    87  }
    88  */
    89  
    90  func f2[A any, B interface{ []A }](_ A, _ B) {}
    91  func f2x() {
    92  	f := f2[byte]
    93  	f(byte(0), []byte{})
    94  	f2(byte(0), []byte{})
    95  	f(0, []byte{})
    96  	// f2(0, []byte{}) - this one doesn't work
    97  }
    98  
    99  // Cannot embed stand-alone type parameters. Disabled for now.
   100  /*
   101  func f3[A any, B interface{type C}, C interface{type *A}](a A, _ B, c C)
   102  func f3x() {
   103  	f := f3[int]
   104  	var x int
   105  	f(x, &x, &x)
   106  	f3(x, &x, &x)
   107  }
   108  */
   109  
   110  func f4[A any, B interface{ []C }, C interface{ *A }](_ A, _ B, c C) {}
   111  func f4x() {
   112  	f := f4[int]
   113  	var x int
   114  	f(x, []*int{}, &x)
   115  	f4(x, []*int{}, &x)
   116  }
   117  
   118  func f5[A interface {
   119  	struct {
   120  		b B
   121  		c C
   122  	}
   123  }, B any, C interface{ *B }](x B) A {
   124  	panic(0)
   125  }
   126  func f5x() {
   127  	x := f5(1.2)
   128  	var _ float64 = x.b
   129  	var _ float64 = *x.c
   130  }
   131  
   132  func f6[A any, B interface{ ~struct{ f []A } }](B) A { panic(0) }
   133  func f6x() {
   134  	x := f6(struct{ f []string }{})
   135  	var _ string = x
   136  }
   137  

View as plain text