Skip to content

cmd/compile: generic function argument causes escape to heap #48849

@bboreham

Description

@bboreham

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

$ go version
go version devel go1.18-7c572a29eb Sat Oct 2 22:06:39 2021 +0100 darwin/arm64

Does this issue reproduce with the latest release?

Yes, needs generics.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/bryan/Library/Caches/go-build"
GOENV="/Users/bryan/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/bryan/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/bryan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/bryan/src/github.com/golang/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/bryan/src/github.com/golang/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="devel go1.18-7c572a29eb Sat Oct 2 22:06:39 2021 +0100"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/bryan/src/github.com/golang/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bp/y4j6bp157f52wv55knd_j1fc0000gn/T/go-build3341590964=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

This program: https://play.golang.org/p/speaQxyFO4a

package main

type fooer interface {
	foo()
}

type bar struct{}

func (b *bar) foo() {
}

func f[T fooer](t T) {
	t.foo()
}

func main() {
	var b bar
	f(&b)
}

Compiled with -gcflags "-m -m -l" to show escape analysis.

What did you expect to see?

What I get if t is declared as the concrete type *bar:

/tmp/main.go:12:8: t does not escape

What did you see instead?

/tmp/main.go:12:17: parameter t leaks to {heap} with derefs=0:
/tmp/main.go:12:17:   flow: {heap} = t:
/tmp/main.go:12:17:     from unsafe.Pointer(t) (interface-converted) at /tmp/main.go:13:3
/tmp/main.go:12:17:     from (<node EFACE>).foo() (call parameter) at /tmp/main.go:13:7

Metadata

Metadata

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.genericsIssue is related to generics

Type

No type

Projects

Status

Todo

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions