Debian Codegraph @

internal/refactor/inline/testdata/generic.txtar

raw
1 Inlining a call to a generic function.
2
3 a1: explicit type args, no shadowing
4 a2: the call uses type inference
5 a3: the type argument is shadowed in the callee
6 a4: ditto, with a more complicated arg
7 a5: a free identifier in the callee is captured by a global
8 in the caller's scope (covered elsewhere; verifying for generics)
9 a7: return type of a generic function is generic interface requiring explicit conversion.
10 a8, a9: same as a7, but for parameters.
11 a10: inline declares a variable whose type is a type parameter
12 -- go.mod --
13 module testdata
14 go 1.18
15
16 -- a/a1.go --
17 package a
18
19 func _() {
20 f[int](1) //@ inline(re"f", a1)
21 }
22
23 func f[T any](x T) { print(x) }
24 -- a1 --
25 ...
26 func _() {
27 print(int(1)) //@ inline(re"f", a1)
28 }
29
30 -- a/a1a.go --
31 package a
32
33 func _() {
34 f[([]int)]([]int{1}) //@ inline(re"f", a1a)
35 }
36
37 func f[T any](x T) { print(x) }
38 -- a1a --
39 ...
40 func _() {
41 print(([]int)([]int{1})) //@ inline(re"f", a1a)
42 }
43
44 -- a/a2.go --
45 package a
46
47 func _() {
48 f(1) //@ inline(re"f", re"cannot inline.*type.*inference")
49 }
50
51 -- a/a3.go --
52 package a
53
54 func _() {
55 g[int]() //@ inline(re"g", re"cannot inline:.*shadow")
56 }
57
58 func g[T any]() {
59 type int bool
60 var x T
61 print(x)
62 }
63
64 -- a/a4.go --
65 package a
66
67 func _() {
68 g[map[int]string]() //@ inline(re"g", re"cannot inline:.*shadow")
69 }
70
71 -- a/a5.go --
72 package a
73
74 import "testdata/b"
75
76 type bool int
77
78 func _() {
79 b.H[int]() //@ inline(re"H", re"cannot inline.*shadowed")
80 }
81 -- b/b.go --
82 package b
83
84 func H[T comparable]() {
85 var x map[T]bool
86 print(x)
87 }
88
89 -- a/a6.go --
90 package a
91
92 type G[T any] struct{}
93
94 func (G[T]) f(x T) { print(x) }
95
96 func _() {
97 G[int]{}.f[bool]() //@ inline(re"f", re"generic methods not yet supported")
98 }
99
100 -- a/a7.go --
101 package a
102
103 type Iface[T any] interface{ foo() }
104
105 func newIface[T any]() Iface[T] { return nil }
106
107 func _() {
108 bar := newIface[int]() //@ inline(re"newIface", a7out)
109 _ = bar
110 }
111
112 -- a7out --
113 package a
114
115 type Iface[T any] interface{ foo() }
116
117 func newIface[T any]() Iface[T] { return nil }
118
119 func _() {
120 bar := Iface[int](nil) //@ inline(re"newIface", a7out)
121 _ = bar
122 }
123
124 -- a/a8.go --
125 package a
126
127 func acceptsIface[T any](val Iface[T]) { print(val); append([]T, val) }
128
129 func _() {
130 acceptsIface[int](Iface[int](nil)) //@ inline(re"acceptsIface", a8out)
131 }
132
133 -- a8out --
134 package a
135
136 func acceptsIface[T any](val Iface[T]) { print(val); append([]T, val) }
137
138 func _() {
139 print(Iface[int](nil))
140 append([]int, Iface[int](nil)) //@ inline(re"acceptsIface", a8out)
141 }
142
143 -- a/a9.go --
144 package a
145
146 type ifaceImpl struct{}
147
148 func (ifaceImpl) foo() {}
149
150 func _() {
151 acceptsIface[int](ifaceImpl{}) //@ inline(re"acceptsIface", a9out)
152 }
153
154 -- a9out --
155 package a
156
157 type ifaceImpl struct{}
158
159 func (ifaceImpl) foo() {}
160
161 func _() {
162 print(ifaceImpl{})
163 append([]int, Iface[int](ifaceImpl{})) //@ inline(re"acceptsIface", a9out)
164 }
165 -- a/a10.go --
166 package a
167
168 func f2[T any](x, y T) { panic(x) }
169 func id(x int) int { return x }
170
171 func _() {
172 f2[int](id(1), id(2)) //@ inline(re"f2", a10out)
173 }
174 -- a10out --
175 package a
176
177 func f2[T any](x, y T) { panic(x) }
178 func id(x int) int { return x }
179
180 func _() {
181 var x, _ int = id(1), id(2)
182 panic(x) //@ inline(re"f2", a10out)
183 }