You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
funcnoescape_ptr() {
i:=new(int) //Does not escape*i=5
}
funcnoescape_ptr_chain() {
i:=new(int) //Does not escapej:=&ik:=&j***k=5
}
funcnoescape_slice() {
i:=new(int) //Does not escapes:= []*int{i, i, i}
*s[0] =5
}
funcescapes_map() {
i:=new(int) //Escapes :(m:=map[int]*int{0: i} //but fortunately, this doesn't*m[0] =5
}
funcmain() {
}
What did you expect to see?
./main.go:4:10: new(int) does not escape
./main.go:8:10: new(int) does not escape
./main.go:14:10: new(int) does not escape
./main.go:15:13: []*int{...} does not escape
+./main.go:19:10: new(int) does not escape
./main.go:20:19: map[int]*int{...} does not escape
What did you see instead?
./main.go:4:10: new(int) does not escape
./main.go:8:10: new(int) does not escape
./main.go:14:10: new(int) does not escape
./main.go:15:13: []*int{...} does not escape
-./main.go:19:10: new(int) escapes to heap
./main.go:20:19: map[int]*int{...} does not escape
The text was updated successfully, but these errors were encountered:
This is unfortunately working as intended.
Maps are fundamentally a heap-based data structure. Due to the possibility of map growth (which requires heap allocation), all the keys and values must escape even if the map doesn't.
Conceivably we could detect non-escaping maps with known bounded numbers of inserts that would guarantee no growth, but that's probably a pretty small set.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Compiled this program with
go build -gcflags="-m"
What did you expect to see?
./main.go:4:10: new(int) does not escape ./main.go:8:10: new(int) does not escape ./main.go:14:10: new(int) does not escape ./main.go:15:13: []*int{...} does not escape +./main.go:19:10: new(int) does not escape ./main.go:20:19: map[int]*int{...} does not escape
What did you see instead?
./main.go:4:10: new(int) does not escape ./main.go:8:10: new(int) does not escape ./main.go:14:10: new(int) does not escape ./main.go:15:13: []*int{...} does not escape -./main.go:19:10: new(int) escapes to heap ./main.go:20:19: map[int]*int{...} does not escape
The text was updated successfully, but these errors were encountered: