go: cmd/compile: misleading panic on deferred func

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

$ go version
go version devel +75f4aa86ba Thu Sep 27 22:02:08 2018 +0000 linux/amd64

What did you do?

https://play.golang.org/p/3aP7yqgWXdf

What did you expect to see?

The panic stack trace should point to where a nil pointer was dereferenced.

What did you see instead?

The panic stack trace pointed at an innocent line.

This is pretty similar to #14646 and #16011. The panic points at a line that is clearly quite capable of yielding that panic, so it led me on a wild goose chase. It took using gdb (gasp!) to figure out that the defer was involved. Consider this an argument for revisiting the “working as intended” judgment on #16011; it was a bug in my code, but the stack trace was very confusing.

If there was a way to indicate that the deferred function was being executed I would have spotted the issue earlier. The regular stack trace does not indicate that. gdb, however, shows runtime.jmpdefer as the immediate stack frame, which is how I figured it out.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

The current behavior is that we return the line number of the return statement that triggered the defer, or the line number of the closing brace of the function if the defer was triggered by the implicit return at the end of the function.

So the line number seems right here, at least with respect to how we currently define “right”.