Skip to content

Commit feec92c

Browse files
randall77heschi
authored andcommitted
[release-branch.go1.17] cmd/compile: drop column info when line number saturates
When line number saturates, we can end up getting non-monotonic position info, because the start of the next line after line=lineMax,col=2 is line=lineMax,col=1. Instead, if line==lineMax, make the column always 0 (no column info). If the line number is wrong, having column info probably isn't that helpful. Fixes #52095 Change-Id: If3d90472691b1f6163654f3505e2cb98467f2383 Reviewed-on: https://go-review.googlesource.com/c/go/+/385795 Trust: Keith Randall <[email protected]> Reviewed-by: Than McIntosh <[email protected]> (cherry picked from commit 1de2344) Reviewed-on: https://go-review.googlesource.com/c/go/+/401315 Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 04781d1 commit feec92c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/cmd/internal/src/pos.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,12 @@ func makeBogusLico() lico {
389389
}
390390

391391
func makeLico(line, col uint) lico {
392-
if line > lineMax {
392+
if line >= lineMax {
393393
// cannot represent line, use max. line so we have some information
394394
line = lineMax
395+
// Drop column information if line number saturates.
396+
// Ensures line+col is monotonic. See issue 51193.
397+
col = 0
395398
}
396399
if col > colMax {
397400
// cannot represent column, use max. column so we have some information

src/cmd/internal/src/pos_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func TestLico(t *testing.T) {
140140
{makeLico(1, 0), ":1", 1, 0},
141141
{makeLico(1, 1), ":1:1", 1, 1},
142142
{makeLico(2, 3), ":2:3", 2, 3},
143-
{makeLico(lineMax, 1), fmt.Sprintf(":%d:1", lineMax), lineMax, 1},
144-
{makeLico(lineMax+1, 1), fmt.Sprintf(":%d:1", lineMax), lineMax, 1}, // line too large, stick with max. line
143+
{makeLico(lineMax, 1), fmt.Sprintf(":%d", lineMax), lineMax, 1},
144+
{makeLico(lineMax+1, 1), fmt.Sprintf(":%d", lineMax), lineMax, 1}, // line too large, stick with max. line
145145
{makeLico(1, colMax), ":1", 1, colMax},
146146
{makeLico(1, colMax+1), ":1", 1, 0}, // column too large
147147
{makeLico(lineMax+1, colMax+1), fmt.Sprintf(":%d", lineMax), lineMax, 0},
@@ -170,8 +170,8 @@ func TestIsStmt(t *testing.T) {
170170
{makeLico(1, 1), ":1:1" + def, 1, 1},
171171
{makeLico(1, 1).withIsStmt(), ":1:1" + is, 1, 1},
172172
{makeLico(1, 1).withNotStmt(), ":1:1" + not, 1, 1},
173-
{makeLico(lineMax, 1), fmt.Sprintf(":%d:1", lineMax) + def, lineMax, 1},
174-
{makeLico(lineMax+1, 1), fmt.Sprintf(":%d:1", lineMax) + def, lineMax, 1}, // line too large, stick with max. line
173+
{makeLico(lineMax, 1), fmt.Sprintf(":%d", lineMax) + def, lineMax, 1},
174+
{makeLico(lineMax+1, 1), fmt.Sprintf(":%d", lineMax) + def, lineMax, 1}, // line too large, stick with max. line
175175
{makeLico(1, colMax), ":1" + def, 1, colMax},
176176
{makeLico(1, colMax+1), ":1" + def, 1, 0}, // column too large
177177
{makeLico(lineMax+1, colMax+1), fmt.Sprintf(":%d", lineMax) + def, lineMax, 0},
@@ -214,9 +214,9 @@ func TestLogue(t *testing.T) {
214214
{makeLico(1, 1).withXlogue(PosPrologueEnd), ":1:1" + defs + pro, 1, 1},
215215
{makeLico(1, 1).withXlogue(PosEpilogueBegin), ":1:1" + defs + epi, 1, 1},
216216

217-
{makeLico(lineMax, 1).withXlogue(PosDefaultLogue), fmt.Sprintf(":%d:1", lineMax) + defs + defp, lineMax, 1},
218-
{makeLico(lineMax, 1).withXlogue(PosPrologueEnd), fmt.Sprintf(":%d:1", lineMax) + defs + pro, lineMax, 1},
219-
{makeLico(lineMax, 1).withXlogue(PosEpilogueBegin), fmt.Sprintf(":%d:1", lineMax) + defs + epi, lineMax, 1},
217+
{makeLico(lineMax, 1).withXlogue(PosDefaultLogue), fmt.Sprintf(":%d", lineMax) + defs + defp, lineMax, 1},
218+
{makeLico(lineMax, 1).withXlogue(PosPrologueEnd), fmt.Sprintf(":%d", lineMax) + defs + pro, lineMax, 1},
219+
{makeLico(lineMax, 1).withXlogue(PosEpilogueBegin), fmt.Sprintf(":%d", lineMax) + defs + epi, lineMax, 1},
220220
} {
221221
x := test.x
222222
if got := formatstr("", x.Line(), x.Col(), true) + fmt.Sprintf(":%d:%d", x.IsStmt(), x.Xlogue()); got != test.string {

0 commit comments

Comments
 (0)