• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// errorcheck -0 -m -l
2
3// Copyright 2015 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.
6
7// Test escape analysis for maps.
8
9package escape
10
11var sink interface{}
12
13func map0() {
14	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
15	// BAD: i should not escape
16	i := 0 // ERROR "moved to heap: i"
17	// BAD: j should not escape
18	j := 0 // ERROR "moved to heap: j"
19	m[&i] = &j
20	_ = m
21}
22
23func map1() *int {
24	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
25	// BAD: i should not escape
26	i := 0 // ERROR "moved to heap: i"
27	j := 0 // ERROR "moved to heap: j"
28	m[&i] = &j
29	return m[&i]
30}
31
32func map2() map[*int]*int {
33	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) escapes to heap"
34	i := 0                   // ERROR "moved to heap: i"
35	j := 0                   // ERROR "moved to heap: j"
36	m[&i] = &j
37	return m
38}
39
40func map3() []*int {
41	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
42	i := 0                   // ERROR "moved to heap: i"
43	// BAD: j should not escape
44	j := 0 // ERROR "moved to heap: j"
45	m[&i] = &j
46	var r []*int
47	for k := range m {
48		r = append(r, k)
49	}
50	return r
51}
52
53func map4() []*int {
54	m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
55	// BAD: i should not escape
56	i := 0 // ERROR "moved to heap: i"
57	j := 0 // ERROR "moved to heap: j"
58	m[&i] = &j
59	var r []*int
60	for k, v := range m {
61		// We want to test exactly "for k, v := range m" rather than "for _, v := range m".
62		// The following if is merely to use (but not leak) k.
63		if k != nil {
64			r = append(r, v)
65		}
66	}
67	return r
68}
69
70func map5(m map[*int]*int) { // ERROR "m does not escape"
71	i := 0 // ERROR "moved to heap: i"
72	j := 0 // ERROR "moved to heap: j"
73	m[&i] = &j
74}
75
76func map6(m map[*int]*int) { // ERROR "m does not escape"
77	if m != nil {
78		m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
79	}
80	i := 0 // ERROR "moved to heap: i"
81	j := 0 // ERROR "moved to heap: j"
82	m[&i] = &j
83}
84
85func map7() {
86	// BAD: i should not escape
87	i := 0 // ERROR "moved to heap: i"
88	// BAD: j should not escape
89	j := 0                     // ERROR "moved to heap: j"
90	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
91	_ = m
92}
93
94func map8() {
95	i := 0                     // ERROR "moved to heap: i"
96	j := 0                     // ERROR "moved to heap: j"
97	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} escapes to heap"
98	sink = m
99}
100
101func map9() *int {
102	// BAD: i should not escape
103	i := 0                     // ERROR "moved to heap: i"
104	j := 0                     // ERROR "moved to heap: j"
105	m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
106	return m[nil]
107}
108