summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/issue12006.go
blob: e878bc48e241e11befd77c30a2790c07a9d2058a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// errorcheck -0 -m -l

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test escape analysis through ... parameters.

package foo

func FooN(vals ...*int) (s int) { // ERROR "vals does not escape"
	for _, v := range vals {
		s += *v
	}
	return s
}

// Append forces heap allocation and copies entries in vals to heap, therefore they escape to heap.
func FooNx(x *int, vals ...*int) (s int) { // ERROR "leaking param: x" "leaking param content: vals"
	vals = append(vals, x)
	return FooN(vals...)
}

var sink []*int

func FooNy(x *int, vals ...*int) (s int) { // ERROR "leaking param: x" "leaking param: vals"
	vals = append(vals, x)
	sink = vals
	return FooN(vals...)
}

func FooNz(vals ...*int) (s int) { // ERROR "leaking param: vals"
	sink = vals
	return FooN(vals...)
}

func TFooN() {
	for i := 0; i < 1000; i++ {
		var i, j int
		FooN(&i, &j) // ERROR "... argument does not escape"
	}
}

func TFooNx() {
	for i := 0; i < 1000; i++ {
		var i, j, k int   // ERROR "moved to heap: i" "moved to heap: j" "moved to heap: k"
		FooNx(&k, &i, &j) // ERROR "... argument does not escape"
	}
}

func TFooNy() {
	for i := 0; i < 1000; i++ {
		var i, j, k int   // ERROR "moved to heap: i" "moved to heap: j" "moved to heap: k"
		FooNy(&k, &i, &j) // ERROR "... argument escapes to heap"
	}
}

func TFooNz() {
	for i := 0; i < 1000; i++ {
		var i, j int  // ERROR "moved to heap: i" "moved to heap: j"
		FooNz(&i, &j) // ERROR "... argument escapes to heap"
	}
}

var isink *int32

func FooI(args ...interface{}) { // ERROR "leaking param content: args"
	for i := 0; i < len(args); i++ {
		switch x := args[i].(type) {
		case nil:
			println("is nil")
		case int32:
			println("is int32")
		case *int32:
			println("is *int32")
			isink = x
		case string:
			println("is string")
		}
	}
}

func TFooI() {
	a := int32(1) // ERROR "moved to heap: a"
	b := "cat"
	c := &a
	FooI(a, b, c) // ERROR "a escapes to heap" "b escapes to heap" "... argument does not escape"
}

func FooJ(args ...interface{}) *int32 { // ERROR "leaking param: args to result ~r0 level=1"
	for i := 0; i < len(args); i++ {
		switch x := args[i].(type) {
		case nil:
			println("is nil")
		case int32:
			println("is int32")
		case *int32:
			println("is *int32")
			return x
		case string:
			println("is string")
		}
	}
	return nil
}

func TFooJ1() {
	a := int32(1)
	b := "cat"
	c := &a
	FooJ(a, b, c) // ERROR "a does not escape" "b does not escape" "... argument does not escape"
}

func TFooJ2() {
	a := int32(1) // ERROR "moved to heap: a"
	b := "cat"
	c := &a
	isink = FooJ(a, b, c) // ERROR "a escapes to heap" "b escapes to heap" "... argument does not escape"
}

type fakeSlice struct {
	l int
	a *[4]interface{}
}

func FooK(args fakeSlice) *int32 { // ERROR "leaking param: args to result ~r0 level=1"
	for i := 0; i < args.l; i++ {
		switch x := (*args.a)[i].(type) {
		case nil:
			println("is nil")
		case int32:
			println("is int32")
		case *int32:
			println("is *int32")
			return x
		case string:
			println("is string")
		}
	}
	return nil
}

func TFooK2() {
	a := int32(1) // ERROR "moved to heap: a"
	b := "cat"
	c := &a
	fs := fakeSlice{3, &[4]interface{}{a, b, c, nil}} // ERROR "a escapes to heap" "b escapes to heap" "&\[4\]interface {}{...} does not escape"
	isink = FooK(fs)
}

func FooL(args []interface{}) *int32 { // ERROR "leaking param: args to result ~r0 level=1"
	for i := 0; i < len(args); i++ {
		switch x := args[i].(type) {
		case nil:
			println("is nil")
		case int32:
			println("is int32")
		case *int32:
			println("is *int32")
			return x
		case string:
			println("is string")
		}
	}
	return nil
}

func TFooL2() {
	a := int32(1) // ERROR "moved to heap: a"
	b := "cat"
	c := &a
	s := []interface{}{a, b, c} // ERROR "a escapes to heap" "b escapes to heap" "\[\]interface {}{...} does not escape"
	isink = FooL(s)
}