]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/src: fix Pos.String() for positions after line directives
authorRobert Griesemer <gri@golang.org>
Tue, 7 Mar 2017 22:53:56 +0000 (14:53 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 7 Mar 2017 23:06:58 +0000 (23:06 +0000)
The old code simply printed the position of the line directive in
square brackets for a position modified by a line directive. Now
we print the corresponding actual source file position instead.

Fixes #19392.

Change-Id: I933f3e435d03a6ee8269df36ae35f9202b7b2e76
Reviewed-on: https://go-review.googlesource.com/37932
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/internal/src/pos.go
src/cmd/internal/src/pos_test.go

index 198fdf72922a448bb06ae911c036bd7f65b4ebea..04e2068d7c3ab2d4e6d0ebad930e38bc9e7c5883 100644 (file)
@@ -84,15 +84,14 @@ func (p Pos) String() string {
                return "<unknown line number>"
        }
 
-       b := p.base
-
-       if b == b.Pos().base {
+       s := posString(p.Filename(), p.Line(), p.Col())
+       if b := p.base; b == b.Pos().base {
                // base is file base (incl. nil)
-               return posString(b.Filename(), p.Line(), p.Col())
+               return s
        }
 
        // base is relative
-       return posString(b.Filename(), p.RelLine(), p.Col()) + "[" + b.Pos().String() + "]"
+       return posString(p.RelFilename(), p.RelLine(), p.Col()) + "[" + s + "]"
 }
 
 // Don't print column numbers because existing tests may not work anymore.
index 3c11840f998b85eb7460320ad82ce24a098c9e9e..3dc5d37b15fbedb53faa1f55dff11af3701454c7 100644 (file)
@@ -18,6 +18,13 @@ func TestPos(t *testing.T) {
        f3 := NewLinePragmaBase(MakePos(f1, 10, 1), "f3", 100)
        f4 := NewLinePragmaBase(MakePos(f3, 10, 1), "f4", 100)
 
+       // line directives from issue #19392
+       fp := NewFileBase("p.go", "p.go")
+       fc := NewLinePragmaBase(MakePos(fp, 3, 0), "c.go", 10)
+       ft := NewLinePragmaBase(MakePos(fp, 6, 0), "t.go", 20)
+       fv := NewLinePragmaBase(MakePos(fp, 9, 0), "v.go", 30)
+       ff := NewLinePragmaBase(MakePos(fp, 12, 0), "f.go", 40)
+
        for _, test := range []struct {
                pos    Pos
                string string
@@ -34,9 +41,15 @@ func TestPos(t *testing.T) {
                {MakePos(nil, 2, 3), ":2:3", "", 2, 3, "", 2},
                {MakePos(f0, 2, 3), ":2:3", "", 2, 3, "", 2},
                {MakePos(f1, 1, 1), "f1:1:1", "f1", 1, 1, "f1", 1},
-               {MakePos(f2, 7, 10), "f2:16:10[<unknown line number>]", "", 7, 10, "f2", 16},
-               {MakePos(f3, 12, 7), "f3:101:7[f1:10:1]", "f1", 12, 7, "f3", 101},
-               {MakePos(f4, 25, 1), "f4:114:1[f3:99:1[f1:10:1]]", "f3", 25, 1, "f4", 114}, // doesn't occur in Go code
+               {MakePos(f2, 7, 10), "f2:16:10[:7:10]", "", 7, 10, "f2", 16},
+               {MakePos(f3, 12, 7), "f3:101:7[f1:12:7]", "f1", 12, 7, "f3", 101},
+               {MakePos(f4, 25, 1), "f4:114:1[f3:25:1]", "f3", 25, 1, "f4", 114},
+
+               // positions from issue #19392
+               {MakePos(fc, 4, 0), "c.go:10:0[p.go:4:0]", "p.go", 4, 0, "c.go", 10},
+               {MakePos(ft, 7, 0), "t.go:20:0[p.go:7:0]", "p.go", 7, 0, "t.go", 20},
+               {MakePos(fv, 10, 0), "v.go:30:0[p.go:10:0]", "p.go", 10, 0, "v.go", 30},
+               {MakePos(ff, 13, 0), "f.go:40:0[p.go:13:0]", "p.go", 13, 0, "f.go", 40},
        } {
                pos := test.pos
                if got := pos.String(); got != test.string {