Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: FuncForPC().Name() returns autogenerated inline func name instead of func name when Func with generics #52809

Closed
morlay opened this issue May 10, 2022 · 2 comments
Labels
NeedsInvestigation

Comments

@morlay
Copy link

@morlay morlay commented May 10, 2022

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

$ go version
go version go1.18.1 darwin/arm64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/morlay/Library/Caches/go-build"
GOENV="DEV"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS="-gcflags=-G=3"
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/morlay/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/morlay"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/opt/homebrew/Cellar/go/1.18.1/libexec"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.18.1/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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/r0/jnrxjtyd3t91_tm25_lks6j40000gp/T/go-build2559481692=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://go.dev/play/p/DNXU3SaUlSK

What did you expect to see?

main.Contains[string]

At least should be, as call stacks did

main.Contains[...]

What did you see instead?

main.main.func1
@seankhliao seankhliao added the NeedsInvestigation label May 10, 2022
@seankhliao
Copy link
Member

@seankhliao seankhliao commented May 10, 2022

cc @golang/runtime

@randall77
Copy link
Contributor

@randall77 randall77 commented May 10, 2022

There is only so much that can FuncForPC can do when the compiler must introduce wrappers. In this case, the wrapper main.main.func1 is to introduce the dictionary argument before calling Contains.

Similar situations occur without generics. For example:

package main

import (
	"reflect"
	"runtime"
)

type T struct {
	a int
}

func (t T) M() int {
	return t.a
}

func main() {
	var t T
	ptr := reflect.ValueOf(t.M).Pointer()
	println(runtime.FuncForPC(ptr).Name())
}

prints main.T.M-fm, not main.T.M. This "-fm" thingy is a wrapper which makes a function from a method (aka a method value).

FuncForPC is a very low-level API and exposes a lot of the internal workings of the compiler/runtime. Generally its use is discouraged in favor of using runtime.Callers. runtime.Callers/runtime.CallersFrames does work correctly, as it is in a position to correctly compensate for wrappers/inlining/etc.

If you're trying to introspect a function closure without calling it, then unfortunately runtime.Callers isn't an option. But I don't think that introspection is possible to do in a way that fixes this bug, unfortunately.

package main

import (
	"fmt"
	"runtime"
)

func Contains[E any](list []E, e E) {
	var pcs [10]uintptr
	n := runtime.Callers(1, pcs[:])
	frames := runtime.CallersFrames(pcs[:n])
	for {
		frame, more := frames.Next()
		fmt.Printf("%s\n", frame.Function)
		if !more {
			break
		}
	}
}

func main() {
	Contains[string](nil, "")
}

reports

main.Contains[...]
main.main
runtime.main
runtime.goexit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

3 participants