Debian Codegraph @

test/devirtualization.go

raw
1 // errorcheck -0 -m
2
3 // Copyright 2025 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 package escape
8
9 type M interface{ M() }
10
11 type A interface{ A() }
12
13 type C interface{ C() }
14
15 type Impl struct{}
16
17 func (*Impl) M() {} // ERROR "can inline \(\*Impl\).M$"
18
19 func (*Impl) A() {} // ERROR "can inline \(\*Impl\).A$"
20
21 type Impl2 struct{}
22
23 func (*Impl2) M() {} // ERROR "can inline \(\*Impl2\).M$"
24
25 func (*Impl2) A() {} // ERROR "can inline \(\*Impl2\).A$"
26
27 type CImpl struct{}
28
29 func (CImpl) C() {} // ERROR "can inline CImpl.C$"
30
31 func typeAsserts() {
32 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
33
34 a.(M).M() // ERROR "devirtualizing a.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
35 a.(A).A() // ERROR "devirtualizing a.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
36 a.(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
37 a.(*Impl).A() // ERROR "inlining call to \(\*Impl\).A"
38
39 v := a.(M)
40 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
41 v.(A).A() // ERROR "devirtualizing v.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
42 v.(*Impl).A() // ERROR "inlining call to \(\*Impl\).A"
43 v.(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
44
45 v2 := a.(A)
46 v2.A() // ERROR "devirtualizing v2.A to \*Impl$" "inlining call to \(\*Impl\).A"
47 v2.(M).M() // ERROR "devirtualizing v2.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
48 v2.(*Impl).A() // ERROR "inlining call to \(\*Impl\).A"
49 v2.(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
50
51 a.(M).(A).A() // ERROR "devirtualizing a.\(M\).\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
52 a.(A).(M).M() // ERROR "devirtualizing a.\(A\).\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
53
54 a.(M).(A).(*Impl).A() // ERROR "inlining call to \(\*Impl\).A"
55 a.(A).(M).(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
56
57 any(a).(M).M() // ERROR "devirtualizing any\(a\).\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
58 any(a).(A).A() // ERROR "devirtualizing any\(a\).\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
59 any(a).(M).(any).(A).A() // ERROR "devirtualizing any\(a\).\(M\).\(any\).\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
60
61 c := any(a)
62 c.(A).A() // ERROR "devirtualizing c.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
63 c.(M).M() // ERROR "devirtualizing c.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
64
65 M(a).M() // ERROR "devirtualizing M\(a\).M to \*Impl$" "inlining call to \(\*Impl\).M"
66 M(M(a)).M() // ERROR "devirtualizing M\(M\(a\)\).M to \*Impl$" "inlining call to \(\*Impl\).M"
67
68 a2 := a.(A)
69 A(a2).A() // ERROR "devirtualizing A\(a2\).A to \*Impl$" "inlining call to \(\*Impl\).A"
70 A(A(a2)).A() // ERROR "devirtualizing A\(A\(a2\)\).A to \*Impl$" "inlining call to \(\*Impl\).A"
71
72 {
73 var a C = &CImpl{} // ERROR "&CImpl{} does not escape$"
74 a.(any).(C).C() // ERROR "devirtualizing a.\(any\).\(C\).C to \*CImpl$" "inlining call to CImpl.C"
75 a.(any).(*CImpl).C() // ERROR "inlining call to CImpl.C"
76 }
77 }
78
79 func typeAssertsWithOkReturn() {
80 {
81 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
82 if v, ok := a.(M); ok {
83 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
84 }
85 }
86 {
87 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
88 if v, ok := a.(A); ok {
89 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
90 }
91 }
92 {
93 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
94 v, ok := a.(M)
95 if ok {
96 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
97 }
98 }
99 {
100 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
101 v, ok := a.(A)
102 if ok {
103 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
104 }
105 }
106 {
107 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
108 v, ok := a.(*Impl)
109 if ok {
110 v.A() // ERROR "inlining call to \(\*Impl\).A"
111 v.M() // ERROR "inlining call to \(\*Impl\).M"
112 }
113 }
114 {
115 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
116 v, _ := a.(M)
117 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
118 }
119 {
120 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
121 v, _ := a.(A)
122 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
123 }
124 {
125 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
126 v, _ := a.(*Impl)
127 v.A() // ERROR "inlining call to \(\*Impl\).A"
128 v.M() // ERROR "inlining call to \(\*Impl\).M"
129 }
130 {
131 a := newM() // ERROR "&Impl{} does not escape$" "inlining call to newM"
132 callA(a) // ERROR "devirtualizing m.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A" "inlining call to callA"
133 callIfA(a) // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A" "inlining call to callIfA"
134 }
135 {
136 _, a := newM2ret() // ERROR "&Impl{} does not escape$" "inlining call to newM2ret"
137 callA(a) // ERROR "devirtualizing m.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A" "inlining call to callA"
138 callIfA(a) // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A" "inlining call to callIfA"
139 }
140 {
141 var a M = &Impl{} // ERROR "&Impl{} does not escape$"
142 // Note the !ok condition, devirtualizing here is fine.
143 if v, ok := a.(M); !ok {
144 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
145 }
146 }
147 {
148 var a A = newImplNoInline()
149 if v, ok := a.(M); ok {
150 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
151 }
152 }
153 {
154 var impl2InA A = &Impl2{} // ERROR "&Impl2{} does not escape$"
155 var a A
156 a, _ = impl2InA.(*Impl)
157 // a now contains the zero value of *Impl
158 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
159 }
160 {
161 a := newANoInline()
162 a.A()
163 }
164 {
165 _, a := newANoInlineRet2()
166 a.A()
167 }
168 }
169
170 func newM() M { // ERROR "can inline newM$"
171 return &Impl{} // ERROR "&Impl{} escapes to heap$"
172 }
173
174 func newM2ret() (int, M) { // ERROR "can inline newM2ret$"
175 return -1, &Impl{} // ERROR "&Impl{} escapes to heap$"
176 }
177
178 func callA(m M) { // ERROR "can inline callA$" "leaking param: m$"
179 m.(A).A()
180 }
181
182 func callIfA(m M) { // ERROR "can inline callIfA$" "leaking param: m$"
183 if v, ok := m.(A); ok {
184 v.A()
185 }
186 }
187
188 //go:noinline
189 func newImplNoInline() *Impl {
190 return &Impl{} // ERROR "&Impl{} escapes to heap$"
191 }
192
193 //go:noinline
194 func newImpl2ret2() (string, *Impl2) {
195 return "str", &Impl2{} // ERROR "&Impl2{} escapes to heap$"
196 }
197
198 //go:noinline
199 func newImpl2() *Impl2 {
200 return &Impl2{} // ERROR "&Impl2{} escapes to heap$"
201 }
202
203 //go:noinline
204 func newANoInline() A {
205 return &Impl{} // ERROR "&Impl{} escapes to heap$"
206 }
207
208 //go:noinline
209 func newANoInlineRet2() (string, A) {
210 return "", &Impl{} // ERROR "&Impl{} escapes to heap$"
211 }
212
213 func testTypeSwitch() {
214 {
215 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
216 switch v := v.(type) {
217 case A:
218 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
219 case M:
220 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
221 }
222 }
223 {
224 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
225 switch v := v.(type) {
226 case A:
227 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
228 case M:
229 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
230 v = &Impl{} // ERROR "&Impl{} does not escape$"
231 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
232 }
233 v.(M).M() // ERROR "devirtualizing v.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
234 }
235 {
236 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
237 switch v1 := v.(type) {
238 case A:
239 v1.A()
240 case M:
241 v1.M()
242 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
243 }
244 }
245 {
246 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
247 switch v := v.(type) {
248 case A:
249 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
250 case M:
251 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
252 case C:
253 v.C()
254 }
255 }
256 {
257 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
258 switch v := v.(type) {
259 case M:
260 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
261 default:
262 panic("does not implement M") // ERROR ".does not implement M. escapes to heap$"
263 }
264 }
265 }
266
267 func differentTypeAssign() {
268 {
269 var a A
270 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
271 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
272 a.A()
273 }
274 {
275 a := A(&Impl{}) // ERROR "&Impl{} escapes to heap$"
276 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
277 a.A()
278 }
279 {
280 a := A(&Impl{}) // ERROR "&Impl{} escapes to heap$"
281 a.A()
282 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
283 }
284 {
285 a := A(&Impl{}) // ERROR "&Impl{} escapes to heap$"
286 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
287 var asAny any = a
288 asAny.(A).A()
289 }
290 {
291 a := A(&Impl{}) // ERROR "&Impl{} escapes to heap$"
292 var asAny any = a
293 asAny = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
294 asAny.(A).A()
295 }
296 {
297 a := A(&Impl{}) // ERROR "&Impl{} escapes to heap$"
298 var asAny any = a
299 asAny.(A).A()
300 asAny = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
301 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
302 }
303 {
304 var a A
305 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
306 a = newImpl2()
307 a.A()
308 }
309 {
310 var a A
311 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
312 _, a = newImpl2ret2()
313 a.A()
314 }
315 }
316
317 func assignWithTypeAssert() {
318 {
319 var i1 A = &Impl{} // ERROR "&Impl{} does not escape$"
320 var i2 A = &Impl2{} // ERROR "&Impl2{} does not escape$"
321 i1 = i2.(*Impl) // this will panic
322 i1.A() // ERROR "devirtualizing i1.A to \*Impl$" "inlining call to \(\*Impl\).A"
323 i2.A() // ERROR "devirtualizing i2.A to \*Impl2$" "inlining call to \(\*Impl2\).A"
324 }
325 {
326 var i1 A = &Impl{} // ERROR "&Impl{} does not escape$"
327 var i2 A = &Impl2{} // ERROR "&Impl2{} does not escape$"
328 i1, _ = i2.(*Impl) // i1 is going to be nil
329 i1.A() // ERROR "devirtualizing i1.A to \*Impl$" "inlining call to \(\*Impl\).A"
330 i2.A() // ERROR "devirtualizing i2.A to \*Impl2$" "inlining call to \(\*Impl2\).A"
331 }
332 }
333
334 func nilIface() {
335 {
336 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
337 v = nil
338 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
339 }
340 {
341 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
342 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
343 v = nil
344 }
345 {
346 var nilIface A
347 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
348 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
349 v = nilIface
350 }
351 {
352 var nilIface A
353 var v A = &Impl{} // ERROR "&Impl{} does not escape$"
354 v = nilIface
355 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
356 }
357 {
358 var v A
359 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
360 v = &Impl{} // ERROR "&Impl{} does not escape$"
361 }
362 {
363 var v A
364 var v2 A = v
365 v2.A() // ERROR "devirtualizing v2.A to \*Impl$" "inlining call to \(\*Impl\).A"
366 v2 = &Impl{} // ERROR "&Impl{} does not escape$"
367 }
368 {
369 var v A
370 v.A()
371 }
372 {
373 var v A
374 var v2 A = v
375 v2.A()
376 }
377 {
378 var v A
379 var v2 A
380 v2 = v
381 v2.A()
382 }
383 }
384
385 func longDevirtTest() {
386 var a interface {
387 M
388 A
389 } = &Impl{} // ERROR "&Impl{} does not escape$"
390
391 {
392 var b A = a
393 b.A() // ERROR "devirtualizing b.A to \*Impl$" "inlining call to \(\*Impl\).A"
394 b.(M).M() // ERROR "devirtualizing b.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
395 }
396 {
397 var b M = a
398 b.M() // ERROR "devirtualizing b.M to \*Impl$" "inlining call to \(\*Impl\).M"
399 b.(A).A() // ERROR "devirtualizing b.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
400 }
401 {
402 var b A = a.(M).(A)
403 b.A() // ERROR "devirtualizing b.A to \*Impl$" "inlining call to \(\*Impl\).A"
404 b.(M).M() // ERROR "devirtualizing b.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
405 }
406 {
407 var b M = a.(A).(M)
408 b.M() // ERROR "devirtualizing b.M to \*Impl$" "inlining call to \(\*Impl\).M"
409 b.(A).A() // ERROR "devirtualizing b.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
410 }
411
412 if v, ok := a.(A); ok {
413 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
414 }
415
416 if v, ok := a.(M); ok {
417 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
418 }
419
420 {
421 var c A = a
422
423 if v, ok := c.(A); ok {
424 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
425 }
426
427 c = &Impl{} // ERROR "&Impl{} does not escape$"
428
429 if v, ok := c.(M); ok {
430 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
431 }
432
433 if v, ok := c.(interface {
434 A
435 M
436 }); ok {
437 v.M() // ERROR "devirtualizing v.M to \*Impl$" "inlining call to \(\*Impl\).M"
438 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
439 }
440 }
441 }
442
443 func deferDevirt() {
444 var a A
445 defer func() { // ERROR "can inline deferDevirt.func1$" "func literal does not escape$"
446 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
447 }()
448 a = &Impl{} // ERROR "&Impl{} does not escape$"
449 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
450 }
451
452 func deferNoDevirt() {
453 var a A
454 defer func() { // ERROR "can inline deferNoDevirt.func1$" "func literal does not escape$"
455 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
456 }()
457 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
458 a.A()
459 }
460
461 //go:noinline
462 func closureDevirt() {
463 var a A
464 func() { // ERROR "func literal does not escape$"
465 // defer so that it does not lnline.
466 defer func() {}() // ERROR "can inline closureDevirt.func1.1$" "func literal does not escape$"
467 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
468 }()
469 a = &Impl{} // ERROR "&Impl{} does not escape$"
470 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
471 }
472
473 //go:noinline
474 func closureNoDevirt() {
475 var a A
476 func() { // ERROR "func literal does not escape$"
477 // defer so that it does not lnline.
478 defer func() {}() // ERROR "can inline closureNoDevirt.func1.1$" "func literal does not escape$"
479 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
480 }()
481 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
482 a.A()
483 }
484
485 var global = "1"
486
487 func closureDevirt2() {
488 var a A
489 a = &Impl{} // ERROR "&Impl{} does not escape$"
490 c := func() { // ERROR "can inline closureDevirt2.func1$" "func literal does not escape$"
491 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
492 }
493 if global == "1" {
494 c = func() { // ERROR "can inline closureDevirt2.func2$" "func literal does not escape$"
495 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
496 }
497 }
498 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
499 c()
500 }
501
502 func closureNoDevirt2() {
503 var a A
504 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
505 c := func() { // ERROR "can inline closureNoDevirt2.func1$" "func literal does not escape$"
506 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
507 }
508 if global == "1" {
509 c = func() { // ERROR "can inline closureNoDevirt2.func2$" "func literal does not escape$"
510 a = &Impl{} // ERROR "&Impl{} escapes to heap$"
511 }
512 }
513 a.A()
514 c()
515 }
516
517 //go:noinline
518 func closureDevirt3() {
519 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
520 func() { // ERROR "func literal does not escape$"
521 // defer so that it does not lnline.
522 defer func() {}() // ERROR "can inline closureDevirt3.func1.1$" "func literal does not escape$"
523 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
524 }()
525 func() { // ERROR "can inline closureDevirt3.func2$"
526 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
527 }() // ERROR "inlining call to closureDevirt3.func2" "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
528 }
529
530 //go:noinline
531 func closureNoDevirt3() {
532 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
533 func() { // ERROR "func literal does not escape$"
534 // defer so that it does not lnline.
535 defer func() {}() // ERROR "can inline closureNoDevirt3.func1.1$" "func literal does not escape$"
536 a.A()
537 }()
538 func() { // ERROR "can inline closureNoDevirt3.func2$"
539 a.A()
540 }() // ERROR "inlining call to closureNoDevirt3.func2"
541 a = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
542 }
543
544 //go:noinline
545 func varDeclaredInClosureReferencesOuter() {
546 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
547 func() { // ERROR "func literal does not escape$"
548 // defer for noinline
549 defer func() {}() // ERROR "can inline varDeclaredInClosureReferencesOuter.func1.1$" "func literal does not escape$"
550 var v A = a
551 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
552 }()
553 func() { // ERROR "func literal does not escape$"
554 // defer for noinline
555 defer func() {}() // ERROR "can inline varDeclaredInClosureReferencesOuter.func2.1$" "func literal does not escape$"
556 var v A = a
557 v = &Impl{} // ERROR "&Impl{} does not escape$"
558 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
559 }()
560
561 var b A = &Impl{} // ERROR "&Impl{} escapes to heap$"
562 func() { // ERROR "func literal does not escape$"
563 // defer for noinline
564 defer func() {}() // ERROR "can inline varDeclaredInClosureReferencesOuter.func3.1$" "func literal does not escape$"
565 var v A = b
566 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
567 v.A()
568 }()
569 func() { // ERROR "func literal does not escape$"
570 // defer for noinline
571 defer func() {}() // ERROR "can inline varDeclaredInClosureReferencesOuter.func4.1$" "func literal does not escape$"
572 var v A = b
573 v.A()
574 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
575 }()
576 }
577
578 //go:noinline
579 func testNamedReturn0() (v A) {
580 v = &Impl{} // ERROR "&Impl{} escapes to heap$"
581 v.A()
582 return
583 }
584
585 //go:noinline
586 func testNamedReturn1() (v A) {
587 v = &Impl{} // ERROR "&Impl{} escapes to heap$"
588 v.A()
589 return &Impl{} // ERROR "&Impl{} escapes to heap$"
590 }
591
592 func testNamedReturns3() (v A) {
593 v = &Impl{} // ERROR "&Impl{} escapes to heap$"
594 defer func() { // ERROR "can inline testNamedReturns3.func1$" "func literal does not escape$"
595 v.A()
596 }()
597 v.A()
598 return &Impl2{} // ERROR "&Impl2{} escapes to heap$"
599 }
600
601 var (
602 globalImpl = &Impl{}
603 globalImpl2 = &Impl2{}
604 globalA A = &Impl{}
605 globalM M = &Impl{}
606 )
607
608 func globals() {
609 {
610 globalA.A()
611 globalA.(M).M()
612 globalM.M()
613 globalM.(A).A()
614
615 a := globalA
616 a.A()
617 a.(M).M()
618
619 m := globalM
620 m.M()
621 m.(A).A()
622 }
623 {
624 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
625 a = globalImpl
626 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
627 }
628 {
629 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
630 a = A(globalImpl)
631 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
632 }
633 {
634 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
635 a = M(globalImpl).(A)
636 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
637 }
638 {
639 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
640 a = globalA.(*Impl)
641 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
642 a = globalM.(*Impl)
643 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
644 }
645 {
646 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
647 a = globalImpl2
648 a.A()
649 }
650 {
651 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
652 a = globalA
653 a.A()
654 }
655 {
656 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
657 a = globalM.(A)
658 a.A()
659 }
660 }
661
662 func mapsDevirt() {
663 {
664 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
665 var v A = m[0]
666 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
667 v.(M).M() // ERROR "devirtualizing v.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
668 }
669 {
670 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
671 var v A
672 var ok bool
673 if v, ok = m[0]; ok {
674 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
675 }
676 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
677 }
678 {
679 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
680 var v A
681 v, _ = m[0]
682 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
683 }
684 }
685
686 func mapsNoDevirt() {
687 {
688 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
689 var v A = m[0]
690 v.A()
691 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
692 v.(M).M()
693 }
694 {
695 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
696 var v A
697 var ok bool
698 if v, ok = m[0]; ok {
699 v.A()
700 }
701 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
702 v.A()
703 }
704 {
705 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
706 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
707 v, _ = m[0]
708 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
709 v.A()
710 }
711
712 {
713 m := make(map[int]A) // ERROR "make\(map\[int\]A\) does not escape$"
714 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
715 v = m[0]
716 v.A()
717 }
718 {
719 m := make(map[int]A) // ERROR "make\(map\[int\]A\) does not escape$"
720 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
721 var ok bool
722 if v, ok = m[0]; ok {
723 v.A()
724 }
725 v.A()
726 }
727 {
728 m := make(map[int]A) // ERROR "make\(map\[int\]A\) does not escape$"
729 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
730 v, _ = m[0]
731 v.A()
732 }
733 }
734
735 func chanDevirt() {
736 {
737 m := make(chan *Impl)
738 var v A = <-m
739 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
740 }
741 {
742 m := make(chan *Impl)
743 var v A
744 v = <-m
745 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
746 }
747 {
748 m := make(chan *Impl)
749 var v A
750 v, _ = <-m
751 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
752 }
753 {
754 m := make(chan *Impl)
755 var v A
756 var ok bool
757 if v, ok = <-m; ok {
758 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
759 }
760 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
761 }
762 {
763 m := make(chan *Impl)
764 var v A
765 var ok bool
766 if v, ok = <-m; ok {
767 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
768 }
769 select {
770 case <-m:
771 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
772 case v = <-m:
773 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
774 case v, ok = <-m:
775 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
776 }
777 }
778 }
779
780 func chanNoDevirt() {
781 {
782 m := make(chan *Impl)
783 var v A = <-m
784 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
785 v.A()
786 }
787 {
788 m := make(chan *Impl)
789 var v A
790 v = <-m
791 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
792 v.A()
793 }
794 {
795 m := make(chan *Impl)
796 var v A
797 v, _ = <-m
798 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
799 v.A()
800 }
801 {
802 m := make(chan *Impl)
803 var v A
804 var ok bool
805 if v, ok = <-m; ok {
806 v.A()
807 }
808 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
809 v.A()
810 }
811 {
812 m := make(chan *Impl)
813 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
814 var ok bool
815 if v, ok = <-m; ok {
816 v.A()
817 }
818 }
819 {
820 m := make(chan *Impl)
821 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
822 select {
823 case v = <-m:
824 v.A()
825 }
826 v.A()
827 }
828 {
829 m := make(chan *Impl)
830 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
831 select {
832 case v, _ = <-m:
833 v.A()
834 }
835 v.A()
836 }
837
838 {
839 m := make(chan A)
840 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
841 v = <-m
842 v.A()
843 }
844 {
845 m := make(chan A)
846 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
847 v, _ = <-m
848 v.A()
849 }
850 {
851 m := make(chan A)
852 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
853 var ok bool
854 if v, ok = <-m; ok {
855 v.A()
856 }
857 }
858 {
859 m := make(chan A)
860 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
861 select {
862 case v = <-m:
863 v.A()
864 }
865 v.A()
866 }
867 {
868 m := make(chan A)
869 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
870 select {
871 case v, _ = <-m:
872 v.A()
873 }
874 v.A()
875 }
876 }
877
878 func rangeDevirt() {
879 {
880 var v A
881 m := make(map[*Impl]struct{}) // ERROR "make\(map\[\*Impl\]struct {}\) does not escape$"
882 v = &Impl{} // ERROR "&Impl{} does not escape$"
883 for v = range m {
884 }
885 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
886 }
887 {
888 var v A
889 m := make(map[*Impl]*Impl) // ERROR "make\(map\[\*Impl\]\*Impl\) does not escape$"
890 v = &Impl{} // ERROR "&Impl{} does not escape$"
891 for v = range m {
892 }
893 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
894 }
895 {
896 var v A
897 m := make(map[*Impl]*Impl) // ERROR "make\(map\[\*Impl\]\*Impl\) does not escape$"
898 v = &Impl{} // ERROR "&Impl{} does not escape$"
899 for _, v = range m {
900 }
901 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
902 }
903 {
904 var v A
905 m := make(chan *Impl)
906 v = &Impl{} // ERROR "&Impl{} does not escape$"
907 for v = range m {
908 }
909 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
910 }
911 {
912 var v A
913 m := []*Impl{} // ERROR "\[\]\*Impl{} does not escape$"
914 v = &Impl{} // ERROR "&Impl{} does not escape$"
915 for _, v = range m {
916 }
917 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
918 }
919 {
920 var v A
921 v = &Impl{} // ERROR "&Impl{} does not escape$"
922 impl := &Impl{} // ERROR "&Impl{} does not escape$"
923 i := 0
924 for v = impl; i < 10; i++ {
925 }
926 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
927 }
928 {
929 var v A
930 v = &Impl{} // ERROR "&Impl{} does not escape$"
931 impl := &Impl{} // ERROR "&Impl{} does not escape$"
932 i := 0
933 for v = impl; i < 10; i++ {
934 }
935 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
936 }
937 {
938 var v A
939 m := [1]*Impl{&Impl{}} // ERROR "&Impl{} does not escape$"
940 v = &Impl{} // ERROR "&Impl{} does not escape$"
941 for _, v = range m {
942 }
943 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
944 }
945 {
946 var v A
947 m := [1]*Impl{&Impl{}} // ERROR "&Impl{} does not escape$"
948 v = &Impl{} // ERROR "&Impl{} does not escape$"
949 for _, v = range &m {
950 }
951 v.A() // ERROR "devirtualizing v.A to \*Impl$" "inlining call to \(\*Impl\).A"
952 }
953 }
954
955 func rangeNoDevirt() {
956 {
957 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
958 m := make(map[*Impl]struct{}) // ERROR "make\(map\[\*Impl\]struct {}\) does not escape$"
959 for v = range m {
960 }
961 v.A()
962 }
963 {
964 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
965 m := make(map[*Impl]*Impl) // ERROR "make\(map\[\*Impl\]\*Impl\) does not escape$"
966 for v = range m {
967 }
968 v.A()
969 }
970 {
971 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
972 m := make(map[*Impl]*Impl) // ERROR "make\(map\[\*Impl\]\*Impl\) does not escape$"
973 for _, v = range m {
974 }
975 v.A()
976 }
977 {
978 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
979 m := make(chan *Impl)
980 for v = range m {
981 }
982 v.A()
983 }
984 {
985 var v A = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
986 m := []*Impl{} // ERROR "\[\]\*Impl{} does not escape$"
987 for _, v = range m {
988 }
989 v.A()
990 }
991 {
992 var v A
993 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
994 impl := &Impl{} // ERROR "&Impl{} escapes to heap$"
995 i := 0
996 for v = impl; i < 10; i++ {
997 }
998 v.A()
999 }
1000 {
1001 var v A
1002 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1003 impl := &Impl{} // ERROR "&Impl{} escapes to heap$"
1004 i := 0
1005 for v = impl; i < 10; i++ {
1006 }
1007 v.A()
1008 }
1009 {
1010 var v A
1011 m := [1]*Impl{&Impl{}} // ERROR "&Impl{} escapes to heap$"
1012 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1013 for _, v = range m {
1014 }
1015 v.A()
1016 }
1017 {
1018 var v A
1019 m := [1]*Impl{&Impl{}} // ERROR "&Impl{} escapes to heap$"
1020 v = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1021 for _, v = range &m {
1022 }
1023 v.A()
1024 }
1025
1026 {
1027 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1028 m := make(map[A]struct{}) // ERROR "make\(map\[A\]struct {}\) does not escape$"
1029 for v = range m {
1030 }
1031 v.A()
1032 }
1033 {
1034 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1035 m := make(map[A]A) // ERROR "make\(map\[A\]A\) does not escape$"
1036 for v = range m {
1037 }
1038 v.A()
1039 }
1040 {
1041 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1042 m := make(map[A]A) // ERROR "make\(map\[A\]A\) does not escape$"
1043 for _, v = range m {
1044 }
1045 v.A()
1046 }
1047 {
1048 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1049 m := make(chan A)
1050 for v = range m {
1051 }
1052 v.A()
1053 }
1054 {
1055 var v A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1056 m := []A{} // ERROR "\[\]A{} does not escape$"
1057 for _, v = range m {
1058 }
1059 v.A()
1060 }
1061
1062 {
1063 var v A
1064 m := [1]A{&Impl{}} // ERROR "&Impl{} escapes to heap$"
1065 v = &Impl{} // ERROR "&Impl{} escapes to heap$"
1066 for _, v = range m {
1067 }
1068 v.A()
1069 }
1070 {
1071 var v A
1072 m := [1]A{&Impl{}} // ERROR "&Impl{} escapes to heap$"
1073 v = &Impl{} // ERROR "&Impl{} escapes to heap$"
1074 for _, v = range &m {
1075 }
1076 v.A()
1077 }
1078 }
1079
1080 var globalInt = 1
1081
1082 func testIfInit() {
1083 {
1084 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
1085 var i = &Impl{} // ERROR "&Impl{} does not escape$"
1086 if a = i; globalInt == 1 {
1087 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1088 }
1089 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1090 a.(M).M() // ERROR "devirtualizing a.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
1091 }
1092 {
1093 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1094 var i2 = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1095 if a = i2; globalInt == 1 {
1096 a.A()
1097 }
1098 a.A()
1099 }
1100 }
1101
1102 func testSwitchInit() {
1103 {
1104 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
1105 var i = &Impl{} // ERROR "&Impl{} does not escape$"
1106 switch a = i; globalInt {
1107 case 12:
1108 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1109 }
1110 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1111 a.(M).M() // ERROR "devirtualizing a.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
1112 }
1113 {
1114 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1115 var i2 = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1116 switch a = i2; globalInt {
1117 case 12:
1118 a.A()
1119 }
1120 a.A()
1121 }
1122 }
1123
1124 type implWrapper Impl
1125
1126 func (implWrapper) A() {} // ERROR "can inline implWrapper.A$"
1127
1128 //go:noinline
1129 func devirtWrapperType() {
1130 {
1131 i := &Impl{} // ERROR "&Impl{} does not escape$"
1132 // This is an OCONVNOP, so we have to be careful, not to devirtualize it to Impl.A.
1133 var a A = (*implWrapper)(i)
1134 a.A() // ERROR "devirtualizing a.A to \*implWrapper$" "inlining call to implWrapper.A"
1135 }
1136 {
1137 i := Impl{}
1138 // This is an OCONVNOP, so we have to be careful, not to devirtualize it to Impl.A.
1139 var a A = (implWrapper)(i) // ERROR "implWrapper\(i\) does not escape$"
1140 a.A() // ERROR "devirtualizing a.A to implWrapper$" "inlining call to implWrapper.A"
1141 }
1142 {
1143 type anyWrapper any
1144 var foo any = &Impl{} // ERROR "&Impl\{\} does not escape"
1145 var bar anyWrapper = foo
1146 bar.(M).M() // ERROR "devirtualizing bar\.\(M\).M to \*Impl" "inlining call to \(\*Impl\)\.M"
1147 }
1148 }
1149
1150 func selfAssigns() {
1151 {
1152 var a A = &Impl{} // ERROR "&Impl{} does not escape$"
1153 a = a
1154 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1155 }
1156 {
1157 var a A = &Impl{} // ERROR "&Impl{} does not escape"
1158 var asAny any = a
1159 asAny = asAny
1160 asAny.(A).A() // ERROR "devirtualizing asAny.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
1161 }
1162 {
1163 var a A = &Impl{} // ERROR "&Impl{} does not escape"
1164 var asAny any = a
1165 a = asAny.(A)
1166 asAny.(A).A() // ERROR "devirtualizing asAny.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
1167 a.(A).A() // ERROR "devirtualizing a.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
1168 b := a
1169 b.(A).A() // ERROR "devirtualizing b.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
1170 }
1171 {
1172 var a A = &Impl{} // ERROR "&Impl{} does not escape"
1173 var asAny any = a
1174 asAny = asAny
1175 a = asAny.(A)
1176 asAny = a
1177 asAny.(A).A() // ERROR "devirtualizing asAny.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
1178 asAny.(M).M() // ERROR "devirtualizing asAny.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
1179 }
1180 {
1181 var a A = &Impl{} // ERROR "&Impl{} does not escape"
1182 var asAny A = a
1183 a = asAny.(A)
1184 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1185 }
1186 {
1187 var a, b, c A
1188 c = &Impl{} // ERROR "&Impl{} does not escape$"
1189 a = c
1190 c = b
1191 b = c
1192 a = b
1193 b = a
1194 c = a
1195 a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
1196 }
1197 }
1198
1199 func boolNoDevirt() {
1200 {
1201 m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
1202 var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
1203 _, v = m[0] // ERROR ".autotmp_[0-9]+ escapes to heap$"
1204 v.(A).A()
1205 }
1206 {
1207 m := make(chan *Impl)
1208 var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
1209 select {
1210 case _, v = <-m: // ERROR ".autotmp_[0-9]+ escapes to heap$"
1211 }
1212 v.(A).A()
1213 }
1214 {
1215 m := make(chan *Impl)
1216 var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
1217 _, v = <-m // ERROR ".autotmp_[0-9]+ escapes to heap$"
1218 v.(A).A()
1219 }
1220 {
1221 var a any = 4 // ERROR "4 does not escape$"
1222 var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
1223 _, v = a.(int) // ERROR ".autotmp_[0-9]+ escapes to heap$"
1224 v.(A).A()
1225 }
1226 }
1227
1228 func addrTaken() {
1229 {
1230 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1231 var ptrA = &a
1232 a.A()
1233 _ = ptrA
1234 }
1235 {
1236 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1237 var ptrA = &a
1238 *ptrA = &Impl{} // ERROR "&Impl{} escapes to heap$"
1239 a.A()
1240 }
1241 {
1242 var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
1243 var ptrA = &a
1244 *ptrA = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
1245 a.A()
1246 }
1247 }
1248
1249 func testInvalidAsserts() {
1250 any(0).(interface{ A() }).A() // ERROR "any\(0\) escapes to heap$"
1251 {
1252 var a M = &Impl{} // ERROR "&Impl{} escapes to heap$"
1253 a.(C).C() // this will panic
1254 a.(any).(C).C() // this will panic
1255 }
1256 {
1257 var a C = &CImpl{} // ERROR "&CImpl{} escapes to heap$"
1258 a.(M).M() // this will panic
1259 a.(any).(M).M() // this will panic
1260 }
1261 {
1262 var a C = &CImpl{} // ERROR "&CImpl{} does not escape$"
1263
1264 // this will panic
1265 a.(M).(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
1266
1267 // this will panic
1268 a.(any).(M).(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
1269 }
1270 }
1271
1272 type namedBool bool
1273
1274 func (namedBool) M() {} // ERROR "can inline namedBool.M$"
1275
1276 //go:noinline
1277 func namedBoolTest() {
1278 m := map[int]int{} // ERROR "map\[int\]int{} does not escape"
1279 var ok namedBool
1280 _, ok = m[5]
1281 var i M = ok // ERROR "ok does not escape"
1282 i.M() // ERROR "devirtualizing i.M to namedBool$" "inlining call to namedBool.M"
1283 }
1284
1285 //go:noinline
1286 func identicalType() {
1287 var i C = struct {
1288 CImpl
1289 }{} // ERROR "struct \{ CImpl \}\{\} escapes to heap"
1290
1291 i = struct {
1292 CImpl
1293 }{} // ERROR "struct \{ CImpl \}\{\} escapes to heap"
1294
1295 i.C()
1296 }
1297
1298 type GenericAImpl[T any] struct{ _ T }
1299
1300 //go:noinline
1301 func (GenericAImpl[T]) A() {}
1302
1303 //go:noinline
1304 func generics() {
1305 {
1306 var i A = GenericAImpl[int]{} // ERROR "GenericAImpl\[int\]\{\} does not escape"
1307 i.A() // ERROR "devirtualizing i.A to GenericAImpl\[int\]"
1308 }
1309 {
1310 var i A = GenericAImpl[int]{} // ERROR "GenericAImpl\[int\]\{\} does not escape"
1311 i = GenericAImpl[int]{} // ERROR "GenericAImpl\[int\]\{\} does not escape"
1312 i.A() // ERROR "devirtualizing i.A to GenericAImpl\[int\]"
1313 }
1314 {
1315 var i A = GenericAImpl[int]{} // ERROR "GenericAImpl\[int\]\{\} escapes to heap"
1316 i = GenericAImpl[byte]{} // ERROR "GenericAImpl\[uint8\]\{\} escapes to heap"
1317 i.A()
1318 }
1319 }