summaryrefslogtreecommitdiffstats
path: root/test/escape_map.go
blob: 23abaa1e0cc5e91fa1a1ba7c3f66cc31b812f5a5 (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
// 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 for maps.

package escape

var sink interface{}

func map0() {
	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
	// BAD: i should not escape
	i := 0 // ERROR "moved to heap: i"
	// BAD: j should not escape
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
	_ = m
}

func map1() *int {
	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
	// BAD: i should not escape
	i := 0 // ERROR "moved to heap: i"
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
	return m[&i]
}

func map2() map[*int]*int {
	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) escapes to heap"
	i := 0                   // ERROR "moved to heap: i"
	j := 0                   // ERROR "moved to heap: j"
	m[&i] = &j
	return m
}

func map3() []*int {
	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
	i := 0                   // ERROR "moved to heap: i"
	// BAD: j should not escape
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
	var r []*int
	for k := range m {
		r = append(r, k)
	}
	return r
}

func map4() []*int {
	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
	// BAD: i should not escape
	i := 0 // ERROR "moved to heap: i"
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
	var r []*int
	for k, v := range m {
		// We want to test exactly "for k, v := range m" rather than "for _, v := range m".
		// The following if is merely to use (but not leak) k.
		if k != nil {
			r = append(r, v)
		}
	}
	return r
}

func map5(m map[*int]*int) { // ERROR "m does not escape"
	i := 0 // ERROR "moved to heap: i"
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
}

func map6(m map[*int]*int) { // ERROR "m does not escape"
	if m != nil {
		m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
	}
	i := 0 // ERROR "moved to heap: i"
	j := 0 // ERROR "moved to heap: j"
	m[&i] = &j
}

func map7() {
	// BAD: i should not escape
	i := 0 // ERROR "moved to heap: i"
	// BAD: j should not escape
	j := 0                     // ERROR "moved to heap: j"
	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
	_ = m
}

func map8() {
	i := 0                     // ERROR "moved to heap: i"
	j := 0                     // ERROR "moved to heap: j"
	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} escapes to heap"
	sink = m
}

func map9() *int {
	// BAD: i should not escape
	i := 0                     // ERROR "moved to heap: i"
	j := 0                     // ERROR "moved to heap: j"
	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
	return m[nil]
}