aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-12-08 11:26:11 -0800
committerGopher Robot <gobot@golang.org>2023-01-17 19:53:54 +0000
commit3af3810a3eeef49890b3cffb8d3f2a491f1b1b35 (patch)
treead1fe514fd7c49368270e0d788295c6cc1491c71 /src/cmd/compile
parent390eee7ab18a091662fd7580b08b4aa3515b5951 (diff)
downloadgo-3af3810a3eeef49890b3cffb8d3f2a491f1b1b35.tar.xz
go/types, types2: avoid sorting all errors when matching errors
Sorting is only needed if there are multiple matching errors on the same line. Instead, in that rare case, select the error that is closest. Follow-up on CL 456137. Change-Id: Ia2056b21c629f3a42495e32de89607fbefb82fa7 Reviewed-on: https://go-review.googlesource.com/c/go/+/456335 Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/cmd/compile')
-rw-r--r--src/cmd/compile/internal/types2/check_test.go39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/cmd/compile/internal/types2/check_test.go b/src/cmd/compile/internal/types2/check_test.go
index 0f97fe9680..611466ba01 100644
--- a/src/cmd/compile/internal/types2/check_test.go
+++ b/src/cmd/compile/internal/types2/check_test.go
@@ -31,7 +31,6 @@ import (
"os"
"path/filepath"
"regexp"
- "sort"
"strings"
"testing"
@@ -68,8 +67,8 @@ func unpackError(err error) (syntax.Pos, string) {
}
}
-// delta returns the absolute difference between x and y.
-func delta(x, y uint) uint {
+// absDiff returns the absolute difference between x and y.
+func absDiff(x, y uint) uint {
if x < y {
return y - x
}
@@ -165,13 +164,6 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
return
}
- // sort errlist in source order
- sort.Slice(errlist, func(i, j int) bool {
- pi, _ := unpackError(errlist[i])
- pj, _ := unpackError(errlist[j])
- return pi.Cmp(pj) < 0
- })
-
// collect expected errors
errmap := make(map[string]map[uint][]syntax.Error)
for _, filename := range filenames {
@@ -187,6 +179,7 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
}
// match against found errors
+ var indices []int // list indices of matching errors, reused for each error
for _, err := range errlist {
gotPos, gotMsg := unpackError(err)
@@ -199,8 +192,8 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
errList = filemap[line]
}
- // one of errors in errList should match the current error
- index := -1 // errList index of matching message, if any
+ // At least one of the errors in errList should match the current error.
+ indices = indices[:0]
for i, want := range errList {
pattern := strings.TrimSpace(want.Msg[len(" ERROR "):])
if n := len(pattern); n >= 2 && pattern[0] == '"' && pattern[n-1] == '"' {
@@ -212,19 +205,27 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
continue
}
if rx.MatchString(gotMsg) {
- index = i
- break
+ indices = append(indices, i)
}
}
- if index < 0 {
+ if len(indices) == 0 {
t.Errorf("%s: no error expected: %q", gotPos, gotMsg)
continue
}
+ // len(indices) > 0
+
+ // If there are multiple matching errors, select the one with the closest column position.
+ index := -1 // index of matching error
+ var delta uint
+ for _, i := range indices {
+ if d := absDiff(gotPos.Col(), errList[i].Pos.Col()); index < 0 || d < delta {
+ index, delta = i, d
+ }
+ }
- // column position must be within expected colDelta
- want := errList[index]
- if delta(gotPos.Col(), want.Pos.Col()) > colDelta {
- t.Errorf("%s: got col = %d; want %d", gotPos, gotPos.Col(), want.Pos.Col())
+ // The closest column position must be within expected colDelta.
+ if delta > colDelta {
+ t.Errorf("%s: got col = %d; want %d", gotPos, gotPos.Col(), errList[index].Pos.Col())
}
// eliminate from errList