Matthew Dempsky | 501b786 | 2019-07-25 12:54:03 -0700 | [diff] [blame] | 1 | // errorcheck -0 -m -l |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2015 The Go Authors. All rights reserved. |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [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 escape analysis for closure arguments. |
| 8 | |
| 9 | package escape |
| 10 | |
| 11 | var sink interface{} |
| 12 | |
| 13 | func ClosureCallArgs0() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 14 | x := 0 |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 15 | func(p *int) { // ERROR "p does not escape" "func literal does not escape" |
| 16 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 17 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | func ClosureCallArgs1() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 21 | x := 0 |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 22 | for { |
| 23 | func(p *int) { // ERROR "p does not escape" "func literal does not escape" |
| 24 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 25 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | func ClosureCallArgs2() { |
| 30 | for { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 31 | x := 0 |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 32 | func(p *int) { // ERROR "p does not escape" "func literal does not escape" |
| 33 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 34 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | func ClosureCallArgs3() { |
| 39 | x := 0 // ERROR "moved to heap: x" |
| 40 | func(p *int) { // ERROR "leaking param: p" "func literal does not escape" |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 41 | sink = p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 42 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func ClosureCallArgs4() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 46 | x := 0 |
Matthew Dempsky | e99e9a6 | 2021-05-26 13:54:31 -0700 | [diff] [blame] | 47 | _ = func(p *int) *int { // ERROR "leaking param: p to result ~r0" "func literal does not escape" |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 48 | return p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 49 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func ClosureCallArgs5() { |
Matthew Dempsky | c0417df | 2020-09-22 02:12:03 -0700 | [diff] [blame] | 53 | x := 0 // ERROR "moved to heap: x" |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 54 | // TODO(mdempsky): We get "leaking param: p" here because the new escape analysis pass |
| 55 | // can tell that p flows directly to sink, but it's a little weird. Re-evaluate. |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 56 | sink = func(p *int) *int { // ERROR "leaking param: p" "func literal does not escape" |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 57 | return p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 58 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func ClosureCallArgs6() { |
| 62 | x := 0 // ERROR "moved to heap: x" |
| 63 | func(p *int) { // ERROR "moved to heap: p" "func literal does not escape" |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 64 | sink = &p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 65 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func ClosureCallArgs7() { |
| 69 | var pp *int |
| 70 | for { |
| 71 | x := 0 // ERROR "moved to heap: x" |
| 72 | func(p *int) { // ERROR "leaking param: p" "func literal does not escape" |
| 73 | pp = p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 74 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 75 | } |
| 76 | _ = pp |
| 77 | } |
| 78 | |
| 79 | func ClosureCallArgs8() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 80 | x := 0 |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 81 | defer func(p *int) { // ERROR "p does not escape" "func literal does not escape" |
| 82 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 83 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func ClosureCallArgs9() { |
| 87 | // BAD: x should not leak |
| 88 | x := 0 // ERROR "moved to heap: x" |
| 89 | for { |
| 90 | defer func(p *int) { // ERROR "func literal escapes to heap" "p does not escape" |
| 91 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 92 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | func ClosureCallArgs10() { |
| 97 | for { |
| 98 | x := 0 // ERROR "moved to heap: x" |
| 99 | defer func(p *int) { // ERROR "func literal escapes to heap" "p does not escape" |
| 100 | *p = 1 |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 101 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | func ClosureCallArgs11() { |
| 106 | x := 0 // ERROR "moved to heap: x" |
| 107 | defer func(p *int) { // ERROR "leaking param: p" "func literal does not escape" |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 108 | sink = p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 109 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func ClosureCallArgs12() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 113 | x := 0 |
Matthew Dempsky | e99e9a6 | 2021-05-26 13:54:31 -0700 | [diff] [blame] | 114 | defer func(p *int) *int { // ERROR "leaking param: p to result ~r0" "func literal does not escape" |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 115 | return p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 116 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func ClosureCallArgs13() { |
| 120 | x := 0 // ERROR "moved to heap: x" |
| 121 | defer func(p *int) { // ERROR "moved to heap: p" "func literal does not escape" |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 122 | sink = &p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 123 | }(&x) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func ClosureCallArgs14() { |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 127 | x := 0 |
| 128 | p := &x |
Matthew Dempsky | e99e9a6 | 2021-05-26 13:54:31 -0700 | [diff] [blame] | 129 | _ = func(p **int) *int { // ERROR "leaking param: p to result ~r0 level=1" "func literal does not escape" |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 130 | return *p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 131 | }(&p) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | func ClosureCallArgs15() { |
Matthew Dempsky | c0417df | 2020-09-22 02:12:03 -0700 | [diff] [blame] | 135 | x := 0 // ERROR "moved to heap: x" |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 136 | p := &x |
Matthew Dempsky | 9f89edc | 2019-08-30 10:56:30 -0700 | [diff] [blame] | 137 | sink = func(p **int) *int { // ERROR "leaking param content: p" "func literal does not escape" |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 138 | return *p |
Matthew Dempsky | abefcac | 2019-04-01 11:58:33 -0700 | [diff] [blame] | 139 | }(&p) |
Dmitry Vyukov | 8a25457 | 2015-02-19 15:57:03 +0300 | [diff] [blame] | 140 | } |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 141 | |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 142 | func ClosureLeak1(s string) string { // ERROR "s does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 143 | t := s + "YYYY" // ERROR "escapes to heap" |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 144 | return ClosureLeak1a(t) // ERROR "... argument does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // See #14409 -- returning part of captured var leaks it. |
Matthew Dempsky | e99e9a6 | 2021-05-26 13:54:31 -0700 | [diff] [blame] | 148 | func ClosureLeak1a(a ...string) string { // ERROR "leaking param: a to result ~r0 level=1$" |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 149 | return func() string { // ERROR "func literal does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 150 | return a[0] |
| 151 | }() |
| 152 | } |
| 153 | |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 154 | func ClosureLeak2(s string) string { // ERROR "s does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 155 | t := s + "YYYY" // ERROR "escapes to heap" |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 156 | c := ClosureLeak2a(t) // ERROR "... argument does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 157 | return c |
| 158 | } |
Matthew Dempsky | a983163 | 2019-04-02 14:44:13 -0700 | [diff] [blame] | 159 | func ClosureLeak2a(a ...string) string { // ERROR "leaking param content: a" |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 160 | return ClosureLeak2b(func() string { // ERROR "func literal does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 161 | return a[0] |
| 162 | }) |
| 163 | } |
Matthew Dempsky | 606019c | 2019-09-12 10:18:03 -0700 | [diff] [blame] | 164 | func ClosureLeak2b(f func() string) string { // ERROR "f does not escape" |
David Chase | d8c815d | 2016-03-01 16:53:37 -0500 | [diff] [blame] | 165 | return f() |
| 166 | } |
Matthew Dempsky | c0417df | 2020-09-22 02:12:03 -0700 | [diff] [blame] | 167 | |
| 168 | func ClosureIndirect() { |
| 169 | f := func(p *int) {} // ERROR "p does not escape" "func literal does not escape" |
| 170 | f(new(int)) // ERROR "new\(int\) does not escape" |
| 171 | |
| 172 | g := f |
| 173 | g(new(int)) // ERROR "new\(int\) does not escape" |
| 174 | |
| 175 | h := nopFunc |
| 176 | h(new(int)) // ERROR "new\(int\) does not escape" |
| 177 | } |
| 178 | |
| 179 | func nopFunc(p *int) {} // ERROR "p does not escape" |