Russ Cox | cd22afa | 2012-09-23 13:16:14 -0400 | [diff] [blame] | 1 | // errorcheck -0 -m |
Russ Cox | 075eef4 | 2012-02-23 23:09:53 -0500 | [diff] [blame] | 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
Russ Cox | 075eef4 | 2012-02-23 23:09:53 -0500 | [diff] [blame] | 4 | // 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 | |
| 10 | package foo |
| 11 | |
| 12 | var p *int |
| 13 | |
Russ Cox | cae604f | 2012-03-05 13:51:44 -0500 | [diff] [blame] | 14 | func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x" |
| 15 | return &x // ERROR "&x escapes to heap" |
Russ Cox | 075eef4 | 2012-02-23 23:09:53 -0500 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | var f func() |
| 19 | |
| 20 | func 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 Cox | 77ccb16 | 2015-02-24 12:19:01 -0500 | [diff] [blame] | 25 | func() { // ERROR "func literal does not escape" "can inline f1.func1" |
Russ Cox | cae604f | 2012-03-05 13:51:44 -0500 | [diff] [blame] | 26 | p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x" |
Russ Cox | 075eef4 | 2012-02-23 23:09:53 -0500 | [diff] [blame] | 27 | }() |
Russ Cox | cae604f | 2012-03-05 13:51:44 -0500 | [diff] [blame] | 28 | |
Russ Cox | 77ccb16 | 2015-02-24 12:19:01 -0500 | [diff] [blame] | 29 | f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2" |
Russ Cox | cae604f | 2012-03-05 13:51:44 -0500 | [diff] [blame] | 30 | p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x" |
Russ Cox | 075eef4 | 2012-02-23 23:09:53 -0500 | [diff] [blame] | 31 | } |
| 32 | f() |
| 33 | } |
Russ Cox | cae604f | 2012-03-05 13:51:44 -0500 | [diff] [blame] | 34 | |
| 35 | func f2() {} // ERROR "can inline f2" |
| 36 | |
| 37 | // No inline for panic, recover. |
| 38 | func f3() { panic(1) } |
| 39 | func f4() { recover() } |
Russ Cox | 54af752 | 2012-09-24 15:53:12 -0400 | [diff] [blame] | 40 | |
| 41 | func f5() *byte { |
| 42 | type T struct { |
| 43 | x [1]byte |
| 44 | } |
Russ Cox | 77ccb16 | 2015-02-24 12:19:01 -0500 | [diff] [blame] | 45 | t := new(T) // ERROR "new.T. escapes to heap" |
Russ Cox | 54af752 | 2012-09-24 15:53:12 -0400 | [diff] [blame] | 46 | return &t.x[0] // ERROR "&t.x.0. escapes to heap" |
| 47 | } |
| 48 | |
| 49 | func f6() *byte { |
| 50 | type T struct { |
| 51 | x struct { |
| 52 | y byte |
| 53 | } |
| 54 | } |
Russ Cox | 77ccb16 | 2015-02-24 12:19:01 -0500 | [diff] [blame] | 55 | t := new(T) // ERROR "new.T. escapes to heap" |
Russ Cox | 54af752 | 2012-09-24 15:53:12 -0400 | [diff] [blame] | 56 | return &t.x.y // ERROR "&t.x.y escapes to heap" |
| 57 | } |