1 // $G $D/$F.go && $L $F.$A && ./$A.out
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 // check 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 {
145 p, q := i_escapes(1), i_escapes(2);
146 chk(p, q, 1, "i_escapes");
148 p, q = j_escapes(3), j_escapes(4);
149 chk(p, q, 3, "j_escapes");
151 p, q = k_escapes(5), k_escapes(6);
152 chk(p, q, 5, "k_escapes");
154 p, q = in_escapes(7), in_escapes(8);
155 chk(p, q, 7, "in_escapes");
157 p, q = select_escapes(9), select_escapes(10);
158 chk(p, q, 9, "select_escapes");
160 p, q = select_escapes1(11, 12);
161 chk(p, q, 11, "select_escapes1");
163 p, q = range_escapes(13), range_escapes(14);
164 chk(p, q, 13, "range_escapes");
166 p, q = range_escapes2(101, 102);
167 chkalias(p, q, 101, "range_escapes2");
169 p, q = for_escapes2(103, 104);
170 chkalias(p, q, 103, "for_escapes2");
173 panic("BUG: no escape");