Skip to content

cmd/compile: different results from runtime.Caller #67668

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

Closed
spirin opened this issue May 27, 2024 · 5 comments
Closed

cmd/compile: different results from runtime.Caller #67668

spirin opened this issue May 27, 2024 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@spirin
Copy link

spirin commented May 27, 2024

Go version

go1.21.3

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/username/Library/Caches/go-build'
GOENV='/Users/username/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/username/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/username/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/username/project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/_t/33qxzy9s2kq868ctfg5yxws00000gn/T/go-build196945374=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Short example for playground:

package main

import (
	"errors"
	"fmt"
	"runtime"
)

func Wrap(err error) error {
	pc, _, _, ok := runtime.Caller(1)
	details := runtime.FuncForPC(pc)
	if ok && details != nil {
		return fmt.Errorf("%s: %w", details.Name(), err)
	}
	return err
}

var errTest = errors.New("test")

type testObj struct{}

func (testObj) Fn1() error {
	return func() error {
		return Wrap(errTest)
	}()
}

func (testObj) Fn2() error {
	return func() error {
		return Wrap(errors.New("test"))
	}()
}

func main() {
	obj := testObj{}
	fmt.Println(obj.Fn1())
	fmt.Println(obj.Fn2())
}

What did you see happen?

different results:

main.main.testObj.Fn1.func1: test
main.testObj.Fn2.func1: test

What did you expect to see?

same results

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 27, 2024
@spirin
Copy link
Author

spirin commented May 27, 2024

go playground https://go.dev/play/p/NtxAspZ03df

@ianlancetaylor ianlancetaylor changed the title runtime: Different results from runtime.Callers cmd/compile: different results from runtime.Caller May 27, 2024
@ianlancetaylor
Copy link
Contributor

CC @golang/compiler

@zigo101
Copy link

zigo101 commented May 28, 2024

Looks relating to inline, with the //go:noinline directive for Fn1, the outputs are the same.

//go:noinline
func (testObj) Fn1() error {
	return func() error {
		return Wrap(errTest)
	}()
}

func (testObj) Fn2() error {
	return func() error {
		return Wrap(errors.New("test"))
	}()
}

@dr2chase
Copy link
Contributor

This looks like a duplicate of #60324.

@mknyszek
Copy link
Contributor

Closing as duplicate of #60324.

@mknyszek mknyszek closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
Development

No branches or pull requests

6 participants