blob: 73dda77e0ecb54d83fcd3a1b28a75f7042954a2f [file] [log] [blame]
Dan Willemsenbc60c3c2021-12-15 01:09:00 -08001// 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
11package builtins
12
13// close
14
15type C0 interface{ int }
16type C1 interface{ chan int }
17type C2 interface{ chan int | <-chan int }
18type C3 interface{ chan int | chan float32 }
19type C4 interface{ chan int | chan<- int }
20type C5[T any] interface{ ~chan T | chan<- T }
21
22func f1[T C1](ch T) {
23 close(ch)
24}
25
26func f2[T C3](ch T) {
27 close(ch)
28}
29
30func f3[T C4](ch T) {
31 close(ch)
32}
33
34func f4[T C5[X], X any](ch T) {
35 close(ch)
36}
37
38// delete
39
40type M0 interface{ int }
41type M1 interface{ map[string]int }
42type M2 interface {
43 map[string]int | map[string]float64
44}
45type M3 interface{ map[string]int | map[rune]int }
46type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
47
48func g1[T M1](m T) {
49 delete(m, "foo")
50}
51
52func g2[T M2](m T) {
53 delete(m, "foo")
54}
55
56func g3[T M4[rune, V], V any](m T) {
57 delete(m, 'k')
58}
59
60// make
61
62func 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.
88type m1S0 []int
89type m1M0 map[string]int
90type m1C0 chan int
91
92// len/cap
93
94type Slice[T any] interface {
95 []T
96}
97
98func c1[T any, S Slice[T]]() {
99 x := make(S, 5, 10)
100 _ = len(x)
101 _ = cap(x)
102}
103
104// append
105
106func 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}