Source file test/devirtualization.go

     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  
  1144  func selfAssigns() {
  1145  	{
  1146  		var a A = &Impl{} // ERROR "&Impl{} does not escape$"
  1147  		a = a
  1148  		a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
  1149  	}
  1150  	{
  1151  		var a A = &Impl{} // ERROR "&Impl{} does not escape"
  1152  		var asAny any = a
  1153  		asAny = asAny
  1154  		asAny.(A).A() // ERROR "devirtualizing asAny.\(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  		a = asAny.(A)
  1160  		asAny.(A).A() // ERROR "devirtualizing asAny.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
  1161  		a.(A).A()     // ERROR "devirtualizing a.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
  1162  		b := a
  1163  		b.(A).A() // ERROR "devirtualizing b.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
  1164  	}
  1165  	{
  1166  		var a A = &Impl{} // ERROR "&Impl{} does not escape"
  1167  		var asAny any = a
  1168  		asAny = asAny
  1169  		a = asAny.(A)
  1170  		asAny = a
  1171  		asAny.(A).A() // ERROR "devirtualizing asAny.\(A\).A to \*Impl$" "inlining call to \(\*Impl\).A"
  1172  		asAny.(M).M() // ERROR "devirtualizing asAny.\(M\).M to \*Impl$" "inlining call to \(\*Impl\).M"
  1173  	}
  1174  	{
  1175  		var a A = &Impl{} // ERROR "&Impl{} does not escape"
  1176  		var asAny A = a
  1177  		a = asAny.(A)
  1178  		a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
  1179  	}
  1180  	{
  1181  		var a, b, c A
  1182  		c = &Impl{} // ERROR "&Impl{} does not escape$"
  1183  		a = c
  1184  		c = b
  1185  		b = c
  1186  		a = b
  1187  		b = a
  1188  		c = a
  1189  		a.A() // ERROR "devirtualizing a.A to \*Impl$" "inlining call to \(\*Impl\).A"
  1190  	}
  1191  }
  1192  
  1193  func boolNoDevirt() {
  1194  	{
  1195  		m := make(map[int]*Impl) // ERROR "make\(map\[int\]\*Impl\) does not escape$"
  1196  		var v any = &Impl{}      // ERROR "&Impl{} escapes to heap$"
  1197  		_, v = m[0]              // ERROR ".autotmp_[0-9]+ escapes to heap$"
  1198  		v.(A).A()
  1199  	}
  1200  	{
  1201  		m := make(chan *Impl)
  1202  		var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1203  		select {
  1204  		case _, v = <-m: // ERROR ".autotmp_[0-9]+ escapes to heap$"
  1205  		}
  1206  		v.(A).A()
  1207  	}
  1208  	{
  1209  		m := make(chan *Impl)
  1210  		var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1211  		_, v = <-m          // ERROR ".autotmp_[0-9]+ escapes to heap$"
  1212  		v.(A).A()
  1213  	}
  1214  	{
  1215  		var a any = 4       // ERROR "4 does not escape$"
  1216  		var v any = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1217  		_, v = a.(int)      // ERROR ".autotmp_[0-9]+ escapes to heap$"
  1218  		v.(A).A()
  1219  	}
  1220  }
  1221  
  1222  func addrTaken() {
  1223  	{
  1224  		var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1225  		var ptrA = &a
  1226  		a.A()
  1227  		_ = ptrA
  1228  	}
  1229  	{
  1230  		var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1231  		var ptrA = &a
  1232  		*ptrA = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1233  		a.A()
  1234  	}
  1235  	{
  1236  		var a A = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1237  		var ptrA = &a
  1238  		*ptrA = &Impl2{} // ERROR "&Impl2{} escapes to heap$"
  1239  		a.A()
  1240  	}
  1241  }
  1242  
  1243  func testInvalidAsserts() {
  1244  	any(0).(interface{ A() }).A() // ERROR "any\(0\) escapes to heap$"
  1245  	{
  1246  		var a M = &Impl{} // ERROR "&Impl{} escapes to heap$"
  1247  		a.(C).C()         // this will panic
  1248  		a.(any).(C).C()   // this will panic
  1249  	}
  1250  	{
  1251  		var a C = &CImpl{} // ERROR "&CImpl{} escapes to heap$"
  1252  		a.(M).M()          // this will panic
  1253  		a.(any).(M).M()    // this will panic
  1254  	}
  1255  	{
  1256  		var a C = &CImpl{} // ERROR "&CImpl{} does not escape$"
  1257  
  1258  		// this will panic
  1259  		a.(M).(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
  1260  
  1261  		// this will panic
  1262  		a.(any).(M).(*Impl).M() // ERROR "inlining call to \(\*Impl\).M"
  1263  	}
  1264  }
  1265  
  1266  type namedBool bool
  1267  
  1268  func (namedBool) M() {} // ERROR "can inline namedBool.M$"
  1269  
  1270  //go:noinline
  1271  func namedBoolTest() {
  1272  	m := map[int]int{} // ERROR "map\[int\]int{} does not escape"
  1273  	var ok namedBool
  1274  	_, ok = m[5]
  1275  	var i M = ok // ERROR "ok does not escape"
  1276  	i.M()        // ERROR "devirtualizing i.M to namedBool$" "inlining call to namedBool.M"
  1277  }
  1278  

View as plain text