Source file test/typeparam/builtins.go

     1  // compile
     2  
     3  // Copyright 2020 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 built-in calls on generic types.
     8  
     9  // derived and expanded from cmd/compile/internal/types2/testdata/check/builtins.go2
    10  
    11  package builtins
    12  
    13  // close
    14  
    15  type C0 interface{ int }
    16  type C1 interface{ chan int }
    17  type C2 interface{ chan int | <-chan int }
    18  type C3 interface{ chan int | chan float32 }
    19  type C4 interface{ chan int | chan<- int }
    20  type C5[T any] interface{ ~chan T | chan<- T }
    21  
    22  func f1[T C1](ch T) {
    23  	close(ch)
    24  }
    25  
    26  func f2[T C3](ch T) {
    27  	close(ch)
    28  }
    29  
    30  func f3[T C4](ch T) {
    31  	close(ch)
    32  }
    33  
    34  func f4[T C5[X], X any](ch T) {
    35  	close(ch)
    36  }
    37  
    38  // delete
    39  
    40  type M0 interface{ int }
    41  type M1 interface{ map[string]int }
    42  type M2 interface {
    43  	map[string]int | map[string]float64
    44  }
    45  type M3 interface{ map[string]int | map[rune]int }
    46  type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
    47  
    48  func g1[T M1](m T) {
    49  	delete(m, "foo")
    50  }
    51  
    52  func g2[T M2](m T) {
    53  	delete(m, "foo")
    54  }
    55  
    56  func g3[T M4[rune, V], V any](m T) {
    57  	delete(m, 'k')
    58  }
    59  
    60  // make
    61  
    62  func m1[
    63  	S1 interface{ []int },
    64  	S2 interface{ []int | chan int },
    65  
    66  	M1 interface{ map[string]int },
    67  	M2 interface{ map[string]int | chan int },
    68  
    69  	C1 interface{ chan int },
    70  	C2 interface{ chan int | chan string },
    71  ]() {
    72  	type m1S0 []int
    73  	type m1M0 map[string]int
    74  	type m1C0 chan int
    75  
    76  	_ = make([]int, 10)
    77  	_ = make(m1S0, 10)
    78  	_ = make(S1, 10)
    79  	_ = make(S1, 10, 20)
    80  
    81  	_ = make(map[string]int)
    82  	_ = make(m1M0)
    83  	_ = make(M1)
    84  	_ = make(M1, 10)
    85  
    86  	_ = make(chan int)
    87  	_ = make(m1C0)
    88  	_ = make(C1)
    89  	_ = make(C1, 10)
    90  }
    91  
    92  // len/cap
    93  
    94  type Slice[T any] interface {
    95  	[]T
    96  }
    97  
    98  func c1[T any, S Slice[T]]() {
    99  	x := make(S, 5, 10)
   100  	_ = len(x)
   101  	_ = cap(x)
   102  }
   103  
   104  // append
   105  
   106  func a1[T any, S Slice[T]]() {
   107  	x := make(S, 5)
   108  	y := make(S, 2)
   109  	var z T
   110  	_ = append(x, y...)
   111  	_ = append(x, z)
   112  }
   113  

View as plain text