Skip to content

cmd/compile: bad line numbers for deferred function in new SSA back end #14646

@rsc

Description

@rsc
$ n /tmp/x.go
    1   package main
    2   
    3   import "runtime"
    4   
    5   func main() {
    6       var file string
    7       var line int
    8       func() {
    9           defer func() {
   10               _, file, line, _ = runtime.Caller(1)
   11           }()
   12       }()
   13       println(file, line)
   14   }
$ go1.4 run /tmp/x.go
/tmp/x.go 12
$ go1.5 run /tmp/x.go
/tmp/x.go 12
$ go1.6 run /tmp/x.go
/tmp/x.go 12
$ go run /tmp/x.go
/tmp/x.go 8
$ 

The file and line of the caller of a deferred function should appear to be the end of that function or the explicit return statement that triggered the deferred function calls. It has regressed since Go 1.6 was cut to report the beginning of the function. We should fix this and add a test.

It looks like explicit returns are handled correctly and that only the implicit return inserted at the end of the function is generated with incorrect line information:

$ n /tmp/x.go
    1   package main
    2   
    3   import (
    4       "os"
    5       "runtime"
    6   )
    7   
    8   func main() {
    9       var file string
   10       var line int
   11       func() {
   12           defer func() {
   13               _, file, line, _ = runtime.Caller(1)
   14           }()
   15           if len(os.Args) > 1 {
   16               return
   17           }
   18           return
   19       }()
   20       println(file, line)
   21   }
$ go1.6 run /tmp/x.go
/tmp/x.go 18
$ go1.6 run /tmp/x.go -
/tmp/x.go 16
$ go run /tmp/x.go
/tmp/x.go 18
$ go run /tmp/x.go -
/tmp/x.go 16
$ 

/cc @randall77 @dr2chase

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions