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.
18 func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
19 return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
22 func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
23 return unsafe.Pointer(uintptr(p) + x)
26 func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
27 return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
35 func h(x int) int { // ERROR "can inline h"
39 func i(x int) int { // ERROR "can inline i"
44 func j(x int) int { // ERROR "can inline j"
53 var somethingWrong error = errors.New("something went wrong")
55 // local closures can be inlined
56 func l(x, y int) (int, int, error) {
57 e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result"
61 e(somethingWrong) // ERROR "inlining call to l.func1"
66 // any re-assignment prevents closure inlining
68 foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape"
70 foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape"
74 // address taking prevents closure inlining
76 foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape"
82 // make sure assignment inside closure is detected
84 foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape"
85 func(x int) { // ERROR "func literal does not escape"
87 foo = func() int { return 2 } // ERROR "can inline o.func2" "func literal escapes"
94 return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1"
98 foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape"
99 return foo() // ERROR "inlining call to q.func1"
103 foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape"
106 bar := func(x int) int { // ERROR "func literal does not escape"
107 return x + func(y int) int { // ERROR "can inline r.func2.1"
109 }(x) // ERROR "inlining call to r.func2.1"
111 return foo(42) + bar(42) // ERROR "inlining call to r.func1"
115 foo := func() { // ERROR "can inline s0.func1" "func literal does not escape"
118 foo() // ERROR "inlining call to s0.func1"
123 foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape"
127 return foo() // ERROR "inlining call to s1.func1"
130 // can't currently inline functions with a break statement
131 func switchBreak(x, y int) int {
147 // can't currently inline functions with a type switch
148 func switchType(x interface{}) int { // ERROR "x does not escape"
159 func (T) meth(int, int) {} // ERROR "can inline T.meth"
161 func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k"
163 func _() { // ERROR "can inline _"
164 T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth"
167 func small1() { // ERROR "can inline small1"
170 func small2() int { // ERROR "can inline small2"
171 return runtime.GOMAXPROCS(0)
173 func small3(t T) { // ERROR "can inline small3"
176 func small4(t T) { // not inlineable - has 2 calls.
177 t.meth2(runtime.GOMAXPROCS(0), 5)
179 func (T) meth2(int, int) { // not inlineable - has 2 calls.