mirror of
https://github.com/golang/go
synced 2024-11-07 15:06:16 -07:00
103f37497f
People are using this to get the name of the function from a function type: runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() Unfortunately, this technique falls down when the first instruction of the function is from an inlined callee. Then the expression above gets you the name of the inlined function instead of the function itself. To fix this, ensure that the first instruction is never from an inlinee. Normally functions have prologs so those are already fine. In just the cases where a function is a leaf with no local variables, and an instruction from an inlinee appears first in the prog list, add a nop at the start of the function to hold a non-inlined position. Consider the nop a "mini-prolog" for leaf functions. Fixes #58300 Change-Id: Ie37092f4ac3167fe8e5ef4a2207b14abc1786897 Reviewed-on: https://go-review.googlesource.com/c/go/+/465076 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: David Chase <drchase@google.com>
30 lines
450 B
Go
30 lines
450 B
Go
// run
|
|
|
|
// Copyright 2023 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"runtime"
|
|
)
|
|
|
|
func f(n int) int {
|
|
return n % 2
|
|
}
|
|
|
|
func g(n int) int {
|
|
return f(n)
|
|
}
|
|
|
|
func name(fn any) (res string) {
|
|
return runtime.FuncForPC(uintptr(reflect.ValueOf(fn).Pointer())).Name()
|
|
}
|
|
|
|
func main() {
|
|
println(name(f))
|
|
println(name(g))
|
|
}
|