blob: 868c456020733c8ad590abae88889ed764a01e4c [file] [log] [blame]
Dmitry Vyukov130538b2015-02-19 18:14:13 +03001// 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() {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030014 m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030015 // 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 // ERROR "&i escapes to heap" "&j escapes to heap"
20 _ = m
21}
22
23func map1() *int {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030024 m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030025 // 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 // ERROR "&i escapes to heap" "&j escapes to heap"
29 return m[&i] // ERROR "&i does not escape"
30}
31
32func map2() map[*int]*int {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030033 m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) escapes to heap"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030034 i := 0 // ERROR "moved to heap: i"
35 j := 0 // ERROR "moved to heap: j"
36 m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
37 return m
38}
39
40func map3() []*int {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030041 m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030042 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 // ERROR "&i escapes to heap" "&j escapes to heap"
46 var r []*int
47 for k := range m {
48 r = append(r, k)
49 }
50 return r
51}
52
53func map4() []*int {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030054 m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030055 // 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 // ERROR "&i escapes to heap" "&j escapes to heap"
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 // ERROR "&i escapes to heap" "&j escapes to heap"
74}
75
76func map6(m map[*int]*int) { // ERROR "m does not escape"
77 if m != nil {
Dmitry Vyukov8205bfb2015-03-28 17:08:53 +030078 m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030079 }
80 i := 0 // ERROR "moved to heap: i"
81 j := 0 // ERROR "moved to heap: j"
82 m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
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 "&i escapes to heap" "&j escapes to heap" "literal 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 "&i escapes to heap" "&j escapes to heap" "literal escapes to heap"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030098 sink = m // ERROR "m escapes to heap"
Dmitry Vyukov130538b2015-02-19 18:14:13 +030099}
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 "&i escapes to heap" "&j escapes to heap" "literal does not escape"
106 return m[nil]
107}