blob: 69a54ac721b08c03121de26690ddb6567ec4eb56 [file] [log] [blame]
Russ Coxcd22afa2012-09-23 13:16:14 -04001// errorcheck -0 -m
Russ Cox075eef42012-02-23 23:09:53 -05002
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2010 The Go Authors. All rights reserved.
Russ Cox075eef42012-02-23 23:09:53 -05004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Test, using compiler diagnostic flags, that the escape analysis is working.
8// Compiles but does not run. Inlining is enabled.
9
10package foo
11
12var p *int
13
Russ Coxcae604f2012-03-05 13:51:44 -050014func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x"
15 return &x // ERROR "&x escapes to heap"
Russ Cox075eef42012-02-23 23:09:53 -050016}
17
18var f func()
19
20func f1() {
21 p = alloc(2) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
22
23 // Escape analysis used to miss inlined code in closures.
24
Russ Cox77ccb162015-02-24 12:19:01 -050025 func() { // ERROR "func literal does not escape" "can inline f1.func1"
Russ Coxcae604f2012-03-05 13:51:44 -050026 p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
Russ Cox075eef42012-02-23 23:09:53 -050027 }()
Russ Coxcae604f2012-03-05 13:51:44 -050028
Russ Cox77ccb162015-02-24 12:19:01 -050029 f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2"
Russ Coxcae604f2012-03-05 13:51:44 -050030 p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
Russ Cox075eef42012-02-23 23:09:53 -050031 }
32 f()
33}
Russ Coxcae604f2012-03-05 13:51:44 -050034
35func f2() {} // ERROR "can inline f2"
36
37// No inline for panic, recover.
38func f3() { panic(1) }
39func f4() { recover() }
Russ Cox54af7522012-09-24 15:53:12 -040040
41func f5() *byte {
42 type T struct {
43 x [1]byte
44 }
Russ Cox77ccb162015-02-24 12:19:01 -050045 t := new(T) // ERROR "new.T. escapes to heap"
Russ Cox54af7522012-09-24 15:53:12 -040046 return &t.x[0] // ERROR "&t.x.0. escapes to heap"
47}
48
49func f6() *byte {
50 type T struct {
51 x struct {
52 y byte
53 }
54 }
Russ Cox77ccb162015-02-24 12:19:01 -050055 t := new(T) // ERROR "new.T. escapes to heap"
Russ Cox54af7522012-09-24 15:53:12 -040056 return &t.x.y // ERROR "&t.x.y escapes to heap"
57}