Dan Willemsen | bc60c3c | 2021-12-15 01:09:00 -0800 | [diff] [blame] | 1 | // compile -G=3 |
| 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 | _ = make([]int, 10) |
| 73 | _ = make(m1S0, 10) |
| 74 | _ = make(S1, 10) |
| 75 | _ = make(S1, 10, 20) |
| 76 | |
| 77 | _ = make(map[string]int) |
| 78 | _ = make(m1M0) |
| 79 | _ = make(M1) |
| 80 | _ = make(M1, 10) |
| 81 | |
| 82 | _ = make(chan int) |
| 83 | _ = make(m1C0) |
| 84 | _ = make(C1) |
| 85 | _ = make(C1, 10) |
| 86 | } |
| 87 | // TODO: put these type declarations back inside m1 when issue 47631 is fixed. |
| 88 | type m1S0 []int |
| 89 | type m1M0 map[string]int |
| 90 | type m1C0 chan int |
| 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 | } |