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 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@morlay
Copy link

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 Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 10, 2022
@seankhliao
Copy link
Member

cc @golang/runtime

@randall77
Copy link
Contributor

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

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/416455 mentions this issue: cmd/compile,runtime: use better line numbers and names for wrappers

@mdempsky
Copy link
Member

FWIW, at tip, the test program https://go.dev/play/p/DNXU3SaUlSK?v=gotip now prints:

main.ContainsStringItem
main.Contains[...]

This is because of the switch to GOEXPERIMENT=unified.

@mdempsky mdempsky reopened this Sep 27, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Sep 27, 2022
@mdempsky
Copy link
Member

(Misclicked.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants