You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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:
The text was updated successfully, but these errors were encountered: