From 752d7bad4fc6cb4c70edbe0b735ca89d7da16732 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 7 Mar 2017 14:53:56 -0800 Subject: [PATCH] cmd/internal/src: fix Pos.String() for positions after line directives 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 Reviewed-by: Matthew Dempsky TryBot-Result: Gobot Gobot --- src/cmd/internal/src/pos.go | 9 ++++----- src/cmd/internal/src/pos_test.go | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/cmd/internal/src/pos.go b/src/cmd/internal/src/pos.go index 198fdf7292..04e2068d7c 100644 --- a/src/cmd/internal/src/pos.go +++ b/src/cmd/internal/src/pos.go @@ -84,15 +84,14 @@ func (p Pos) String() string { return "" } - 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. diff --git a/src/cmd/internal/src/pos_test.go b/src/cmd/internal/src/pos_test.go index 3c11840f99..3dc5d37b15 100644 --- a/src/cmd/internal/src/pos_test.go +++ b/src/cmd/internal/src/pos_test.go @@ -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[]", "", 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 { -- 2.48.1