Skip to content

cmd/compile: backing a local slice with an array in a loop causes the array to escape #24730

@mundaym

Description

@mundaym

What version of Go are you using (go version)?

go version devel +68c7cb25a7 Wed Apr 4 12:18:29 2018 +0100 darwin/amd64

Does this issue reproduce with the latest release?

Yes (go version go1.10.1 darwin/amd64)

What did you do?

Compile the following code with '-m -m' to print escape information:

package main

type T struct {
	x [64]byte
}

func (t *T) f() {
	y := t.x[:]
	for i := range t.x[:] {
		y[i] = 1
	}
}

func (t *T) g() {
	y := t.x[:]
	for i := range t.x[:] {
		y = t.x[:] // <--- causes t.x to escape 
		y[i] = 1
	}
}

func main() { }

What did you expect to see?

t.x not to escape in f() or g() (it is only referenced by local variables).

What did you see instead?

t.x escapes in g() due to the second assignment.

./esc.go:7:10: (*T).f t does not escape
./esc.go:8:10: (*T).f t.x does not escape
./esc.go:17:10: t.x escapes to heap
./esc.go:17:10: 	from y (assigned) at ./esc.go:17:5
./esc.go:14:10: (*T).g t does not escape
./esc.go:15:10: (*T).g t.x does not escape
./esc.go:16:20: (*T).g t.x does not escape

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions