1 // errorcheckwithauto -0 -m -d=inlfuncswithclosures=1
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 func _() int { // ERROR "can inline _"
55 return tmp2(0) // ERROR "inlining call to h"
58 var somethingWrong error
60 // local closures can be inlined
61 func l(x, y int) (int, int, error) { // ERROR "can inline l"
62 e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result"
66 e(somethingWrong) // ERROR "inlining call to l.func1"
69 f(nil) // ERROR "inlining call to l.func1"
74 // any re-assignment prevents closure inlining
76 foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape"
78 foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape"
82 // address taking prevents closure inlining
84 foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape"
90 // make sure assignment inside closure is detected
92 foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape"
93 func(x int) { // ERROR "can inline o.func2"
95 foo = func() int { return 2 } // ERROR "func literal does not escape" "can inline o.func2"
97 }(11) // ERROR "inlining call to o.func2"
101 func p() int { // ERROR "can inline p"
102 return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1"
105 func q(x int) int { // ERROR "can inline q"
106 foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape"
107 return foo() // ERROR "inlining call to q.func1"
111 foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape"
114 bar := func(x int) int { // ERROR "func literal does not escape" "can inline r.func2"
115 return x + func(y int) int { // ERROR "can inline r.func2.1" "can inline r.func3"
117 }(x) // ERROR "inlining call to r.func2.1"
119 return foo(42) + bar(42) // ERROR "inlining call to r.func1" "inlining call to r.func2" "inlining call to r.func3"
122 func s0(x int) int { // ERROR "can inline s0"
123 foo := func() { // ERROR "can inline s0.func1" "func literal does not escape"
126 foo() // ERROR "inlining call to s0.func1"
130 func s1(x int) int { // ERROR "can inline s1"
131 foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape"
135 return foo() // ERROR "inlining call to s1.func1"
138 // can't currently inline functions with a break statement
139 func switchBreak(x, y int) int {
155 func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape"
166 func (T) meth(int, int) {} // ERROR "can inline T.meth"
168 func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k"
170 func _() { // ERROR "can inline _"
171 T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth"
172 // ERRORAUTO "inlining call to T.meth"
175 func small1() { // ERROR "can inline small1"
178 func small2() int { // ERROR "can inline small2"
179 return runtime.GOMAXPROCS(0)
181 func small3(t T) { // ERROR "can inline small3"
184 func small4(t T) { // not inlineable - has 2 calls.
185 t.meth2(runtime.GOMAXPROCS(0), 5)
187 func (T) meth2(int, int) { // not inlineable - has 2 calls.
192 // Issue #29737 - make sure we can do inlining for a chain of recursive functions
193 func ee() { // ERROR "can inline ee"
194 ff(100) // ERROR "inlining call to ff" "inlining call to gg" "inlining call to hh"
197 func ff(x int) { // ERROR "can inline ff"
203 func gg(x int) { // ERROR "can inline gg"
206 func hh(x int) { // ERROR "can inline hh"
207 ff(x - 1) // ERROR "inlining call to ff" // ERROR "inlining call to gg"
210 // Issue #14768 - make sure we can inline for loops.
211 func for1(fn func() bool) { // ERROR "can inline for1" "fn does not escape"
221 // BAD: for2 should be inlineable too.
222 func for2(fn func() bool) { // ERROR "fn does not escape"
233 // Issue #18493 - make sure we can do inlining of functions with a method value
236 func (a T1) meth(val int) int { // ERROR "can inline T1.meth"
240 func getMeth(t1 T1) func(int) int { // ERROR "can inline getMeth"
241 return t1.meth // ERROR "t1.meth escapes to heap"
242 // ERRORAUTO "inlining call to T1.meth"
245 func ii() { // ERROR "can inline ii"
247 f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape"
251 // Issue #42194 - make sure that functions evaluated in
252 // go and defer statements can be inlined.
254 defer gd1(gd2()) // ERROR "inlining call to gd2"
255 defer gd3()() // ERROR "inlining call to gd3"
256 go gd1(gd2()) // ERROR "inlining call to gd2"
257 go gd3()() // ERROR "inlining call to gd3"
260 func gd2() int { // ERROR "can inline gd2"
264 func gd3() func() { // ERROR "can inline gd3"
268 // Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost.
269 func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape"
271 d[0] = float32bits(x[0]) // ERROR "inlining call to float32bits"
272 d[1] = float32bits(x[1]) // ERROR "inlining call to float32bits"
273 d[2] = float32bits(x[2]) // ERROR "inlining call to float32bits"
274 d[3] = float32bits(x[3]) // ERROR "inlining call to float32bits"
275 d[4] = float32bits(x[4]) // ERROR "inlining call to float32bits"
276 d[5] = float32bits(x[5]) // ERROR "inlining call to float32bits"
279 // float32bits is a copy of math.Float32bits to ensure that
280 // these tests pass with `-gcflags=-l`.
281 func float32bits(f float32) uint32 { // ERROR "can inline float32bits"
282 return *(*uint32)(unsafe.Pointer(&f))
285 // Ensure OCONVNOP is zero cost.
286 func Conv(v uint64) uint64 { // ERROR "can inline Conv"
287 return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)"
289 func conv2(v uint64) uint64 { // ERROR "can inline conv2"
290 return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1"
292 func conv1(v uint64) uint64 { // ERROR "can inline conv1"
293 return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))