Skip to content

runtime: Caller returns wrong line number inside composite literals #11400

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
Sajmani opened this issue Jun 25, 2015 · 3 comments
Closed

runtime: Caller returns wrong line number inside composite literals #11400

Sajmani opened this issue Jun 25, 2015 · 3 comments
Milestone

Comments

@Sajmani
Copy link
Contributor

Sajmani commented Jun 25, 2015

This is a behavior change since Go 1.4.

Calling runtime.Caller inside a composite literal returns the line number at the end of the literal instead of the line where it is called. Here is a patch that adds a test for this to runtime/symtab_test.go, followed by its output:

diff --git a/src/runtime/symtab_test.go b/src/runtime/symtab_test.go
index bd9fe18..d94de04 100644
--- a/src/runtime/symtab_test.go
+++ b/src/runtime/symtab_test.go
@@ -45,3 +45,55 @@ func testCallerBar(t *testing.T) {
                }
        }
 }
+
+func lineNumber() int {
+       _, _, line, _ := runtime.Caller(1)
+       return line // return 0 for error
+}
+
+// Do not add/remove lines in this block without updating the line numbers.
+var firstLine = lineNumber() // 0
+var (                        // 1
+       lineVar1             = lineNumber()               // 2
+       lineVar2a, lineVar2b = lineNumber(), lineNumber() // 3
+)                        // 4
+var compLit = []struct { // 5
+       lineA, lineB int // 6
+}{ // 7
+       { // 8
+               lineNumber(), lineNumber(), // 9
+       }, // 10
+       { // 11
+               lineNumber(), // 12
+               lineNumber(), // 13
+       }, // 14
+       { // 15
+               lineB: lineNumber(), // 16
+               lineA: lineNumber(), // 17
+       }, // 18
+} // 19
+// Modifications below this line are okay.
+
+func TestLineNumber(t *testing.T) {
+       for _, test := range []struct {
+               name string
+               val  int
+               want int
+       }{
+               {"firstLine", firstLine, 0},
+               {"lineVar1", lineVar1, 2},
+               {"lineVar2a", lineVar2a, 3},
+               {"lineVar2b", lineVar2b, 3},
+               {"compLit[0].lineA", compLit[0].lineA, 9},
+               {"compLit[0].lineB", compLit[0].lineB, 9},
+               {"compLit[1].lineA", compLit[1].lineA, 12},
+               {"compLit[1].lineB", compLit[1].lineB, 13},
+               {"compLit[2].lineA", compLit[2].lineA, 17},
+               {"compLit[2].lineB", compLit[2].lineB, 16},
+       } {
+               if got := test.val - firstLine; got != test.want {
+                       t.Errorf("%s on firstLine+%d want firstLine+%d (firstLine=%d, val=%d)",
+                               test.name, got, test.want, firstLine, test.val)
+               }
+       }
+}

$ go test -short runtime
--- FAIL: TestLineNumber (0.00s)
    symtab_test.go:96: compLit[0].lineA on firstLine+19 want firstLine+9 (firstLine=55, val=74)
    symtab_test.go:96: compLit[0].lineB on firstLine+19 want firstLine+9 (firstLine=55, val=74)
    symtab_test.go:96: compLit[1].lineA on firstLine+19 want firstLine+12 (firstLine=55, val=74)
    symtab_test.go:96: compLit[1].lineB on firstLine+19 want firstLine+13 (firstLine=55, val=74)
    symtab_test.go:96: compLit[2].lineA on firstLine+19 want firstLine+17 (firstLine=55, val=74)
    symtab_test.go:96: compLit[2].lineB on firstLine+19 want firstLine+16 (firstLine=55, val=74)
FAIL
FAIL    runtime 11.072s
@josharian
Copy link
Contributor

Related: #8836.

/cc @dr2chase

@dr2chase dr2chase self-assigned this Jun 26, 2015
@dr2chase
Copy link
Contributor

Appears to have broken with this commit
https://go-review.googlesource.com/#/c/9721/

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/11730 mentions this issue.

@mikioh mikioh added this to the Go1.5 milestone Jul 7, 2015
@golang golang locked and limited conversation to collaborators Jul 11, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants