Source file test/escape4.go

     1  // errorcheck -0 -m
     2  
     3  //go:build !goexperiment.newinliner
     4  
     5  // Copyright 2010 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  // Test, using compiler diagnostic flags, that the escape analysis is working.
    10  // Compiles but does not run.  Inlining is enabled.
    11  
    12  package foo
    13  
    14  var p *int
    15  
    16  func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x"
    17  	return &x
    18  }
    19  
    20  var f func()
    21  
    22  func f1() {
    23  	p = alloc(2) // ERROR "inlining call to alloc" "moved to heap: x"
    24  
    25  	// Escape analysis used to miss inlined code in closures.
    26  
    27  	func() { // ERROR "can inline f1.func1"
    28  		p = alloc(3) // ERROR "inlining call to alloc"
    29  	}() // ERROR "inlining call to f1.func1" "inlining call to alloc" "moved to heap: x"
    30  
    31  	f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2"
    32  		p = alloc(3) // ERROR "inlining call to alloc" "moved to heap: x"
    33  	}
    34  	f()
    35  }
    36  
    37  func f2() {} // ERROR "can inline f2"
    38  
    39  // No inline for recover; panic now allowed to inline.
    40  func f3() { panic(1) } // ERROR "can inline f3" "1 escapes to heap"
    41  func f4() { recover() }
    42  
    43  func f5() *byte { // ERROR "can inline f5"
    44  	type T struct {
    45  		x [1]byte
    46  	}
    47  	t := new(T) // ERROR "new.T. escapes to heap"
    48  	return &t.x[0]
    49  }
    50  
    51  func f6() *byte { // ERROR "can inline f6"
    52  	type T struct {
    53  		x struct {
    54  			y byte
    55  		}
    56  	}
    57  	t := new(T) // ERROR "new.T. escapes to heap"
    58  	return &t.x.y
    59  }
    60  

View as plain text