3 // Copyright 2009 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.
9 // Test for correct heap-moving of escaped variables.
10 // It is hard to check for the allocations, but it is easy
11 // to check that if you call the function twice at the
12 // same stack level, the pointers returned should be
17 var allptr = make([]*int, 0, 100)
19 func noalias(p, q *int, s string) {
23 allptr = allptr[0:n+2]
27 for i := 0; i < n; i++ {
28 if allptr[i] != nil && *allptr[i] != -(i+1) {
29 println("aliased pointers", -(i+1), *allptr[i], "after", s)
36 func val(p, q *int, v int, s string) {
38 println("wrong value want", v, "got", *p, "after", s)
42 println("wrong value want", v+1, "got", *q, "after", s)
47 func chk(p, q *int, v int, s string) {
52 func chkalias(p, q *int, v int, s string) {
54 println("want aliased pointers but got different after", s)
57 println("wrong value want", v+1, "got", *q, "after", s)
61 func i_escapes(x int) *int {
67 func j_escapes(x int) *int {
73 func k_escapes(x int) *int {
78 func in_escapes(x int) *int {
82 func send(c chan int, x int) {
86 func select_escapes(x int) *int {
96 func select_escapes1(x int, y int) (*int, *int) {
102 for i := 0; i < 2; i++ {
112 func range_escapes(x int) *int {
115 for _, v := range a {
122 func range_escapes2(x, y int) (*int, *int) {
127 for k, v := range a {
134 func for_escapes2(x int, y int) (*int, *int) {
137 for i := x; n < 2; i = y {
144 func out_escapes(i int) (x int, p *int) {
146 p = &x // ERROR "address of out parameter"
150 func out_escapes_2(i int) (x int, p *int) {
152 return x, &x // ERROR "address of out parameter"
155 func defer1(i int) (x int) {
157 go func() { x = i; c <- 1 }()
163 p, q := i_escapes(1), i_escapes(2)
164 chk(p, q, 1, "i_escapes")
166 p, q = j_escapes(3), j_escapes(4)
167 chk(p, q, 3, "j_escapes")
169 p, q = k_escapes(5), k_escapes(6)
170 chk(p, q, 5, "k_escapes")
172 p, q = in_escapes(7), in_escapes(8)
173 chk(p, q, 7, "in_escapes")
175 p, q = select_escapes(9), select_escapes(10)
176 chk(p, q, 9, "select_escapes")
178 p, q = select_escapes1(11, 12)
179 chk(p, q, 11, "select_escapes1")
181 p, q = range_escapes(13), range_escapes(14)
182 chk(p, q, 13, "range_escapes")
184 p, q = range_escapes2(101, 102)
185 chkalias(p, q, 101, "range_escapes2")
187 p, q = for_escapes2(103, 104)
188 chkalias(p, q, 103, "for_escapes2")
190 _, p = out_escapes(15)
191 _, q = out_escapes(16)
192 chk(p, q, 15, "out_escapes")
194 _, p = out_escapes_2(17)
195 _, q = out_escapes_2(18)
196 chk(p, q, 17, "out_escapes_2")
200 println("defer failed", x)
205 panic("BUG: no escape")