3 // Copyright 2015 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.
7 // Test, using compiler diagnostic flags, that inlining is working.
8 // Compiles but does not run.
17 func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
18 return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
21 func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
22 return unsafe.Pointer(uintptr(p) + x)
25 func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
26 return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
34 func h(x int) int { // ERROR "can inline h"
38 func i(x int) int { // ERROR "can inline i"
43 func j(x int) int { // ERROR "can inline j"
52 var somethingWrong error = errors.New("something went wrong")
54 // local closures can be inlined
55 func l(x, y int) (int, int, error) {
56 e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result"
60 e(somethingWrong) // ERROR "inlining call to l.func1"
65 // any re-assignment prevents closure inlining
67 foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape"
69 foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape"
73 // address taking prevents closure inlining
75 foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape"
76 bar := &foo // ERROR "&foo does not escape"
81 // make sure assignment inside closure is detected
83 foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape"
84 func(x int) { // ERROR "func literal does not escape"
86 foo = func() int { return 2 } // ERROR "can inline o.func2" "func literal escapes"
93 return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1"
97 foo := func() int { return x * 2 } // ERROR "can inline q.func1" "q func literal does not escape"
98 return foo() // ERROR "inlining call to q.func1"
102 foo := func(x int) int { // ERROR "can inline r.func1" "r func literal does not escape"
105 bar := func(x int) int { // ERROR "r func literal does not escape"
106 return x + func(y int) int { // ERROR "can inline r.func2.1"
108 }(x) // ERROR "inlining call to r.func2.1"
110 return foo(42) + bar(42) // ERROR "inlining call to r.func1"
114 foo := func() { // ERROR "can inline s0.func1" "s0 func literal does not escape"
117 foo() // ERROR "inlining call to s0.func1" "&x does not escape"
122 foo := func() int { // ERROR "can inline s1.func1" "s1 func literal does not escape"
126 return foo() // ERROR "inlining call to s1.func1" "&x does not escape"
129 // can't currently inline functions with a break statement
130 func switchBreak(x, y int) int {
146 // can't currently inline functions with a type switch
147 func switchType(x interface{}) int { // ERROR "switchType x does not escape"
158 func (T) meth(int, int) {} // ERROR "can inline T.meth"
160 func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k"
162 func _() { // ERROR "can inline _"
163 T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth"