Skip to content

cmd/compile: spurious stack frame for inlined method of returned value #21879

Closed
@ChrisHines

Description

@ChrisHines

What did you do?

https://play.golang.org/p/ZJ4aswqjbQ

package main

import "runtime/debug"

func main() {
	// correct
	println(trace().stack)

	// also correct
	t := trace()
	println(t.getStack())

	// inlined getStack method appears in stack trace
	// as the caller of func trace()
	println(trace().getStack())
}

func trace() callStack {
	return callStack{
		stack: string(debug.Stack()),
	}
}

type callStack struct {
	stack string
}

func (c callStack) getStack() string {
	return c.stack
}

https://play.golang.org/p/wVO2lrLiAk

package main

import (
	"runtime"
)

func main() {
	// prints "main.main" as expected
	println(caller().frame.Function)

	// prints main.call.name with default compile, but prints "main.main" if
	// compiled with -gcflags="-l"
	println(caller().name())
}

func caller() call {
	var pcs [3]uintptr
	n := runtime.Callers(1, pcs[:])
	frames := runtime.CallersFrames(pcs[:n])
	frame, _ := frames.Next()
	frame, _ = frames.Next()

	return call{frame: frame}
}

type call struct {
	frame runtime.Frame
}

func (c call) name() string {
	return c.frame.Function
}

What did you expect to see?

  • Methods called after a stack trace has been captured should not appear in the stack trace.

What did you see instead?

  • The getStack and name methods logically called after a stack trace had been captured appear in the stack trace as the caller of a function they don't call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions