Skip to content

Commit 607fbc5

Browse files
committed
chore: sync with x/tools
1 parent 22944a2 commit 607fbc5

File tree

13 files changed

+65
-54
lines changed

13 files changed

+65
-54
lines changed

internal/x/tools/analysisflags/readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ This is just a copy of the code without any changes.
55

66
## History
77

8-
- sync with https://github.com/golang/tools/blob/v0.28.0
8+
- https://github.com/golangci/golangci-lint/pull/XXXX
9+
- sync with https://github.com/golang/tools/blob/v0.37.0/go/analysis/internal/analysisflags
10+
- https://github.com/golangci/golangci-lint/pull/5576
11+
- sync with https://github.com/golang/tools/blob/v0.28.0/go/analysis/internal/analysisflags

internal/x/tools/analysisinternal/analysis.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,30 @@ package analysisinternal
88

99
import (
1010
"fmt"
11-
"os"
11+
"slices"
1212

1313
"golang.org/x/tools/go/analysis"
1414
)
1515

16-
// MakeReadFile returns a simple implementation of the Pass.ReadFile function.
17-
func MakeReadFile(pass *analysis.Pass) func(filename string) ([]byte, error) {
16+
// A ReadFileFunc is a function that returns the
17+
// contents of a file, such as [os.ReadFile].
18+
type ReadFileFunc = func(filename string) ([]byte, error)
19+
20+
// CheckedReadFile returns a wrapper around a Pass.ReadFile
21+
// function that performs the appropriate checks.
22+
func CheckedReadFile(pass *analysis.Pass, readFile ReadFileFunc) ReadFileFunc {
1823
return func(filename string) ([]byte, error) {
1924
if err := CheckReadable(pass, filename); err != nil {
2025
return nil, err
2126
}
22-
return os.ReadFile(filename)
27+
return readFile(filename)
2328
}
2429
}
2530

2631
// CheckReadable enforces the access policy defined by the ReadFile field of [analysis.Pass].
2732
func CheckReadable(pass *analysis.Pass, filename string) error {
28-
if slicesContains(pass.OtherFiles, filename) ||
29-
slicesContains(pass.IgnoredFiles, filename) {
33+
if slices.Contains(pass.OtherFiles, filename) ||
34+
slices.Contains(pass.IgnoredFiles, filename) {
3035
return nil
3136
}
3237
for _, f := range pass.Files {
@@ -36,13 +41,3 @@ func CheckReadable(pass *analysis.Pass, filename string) error {
3641
}
3742
return fmt.Errorf("Pass.ReadFile: %s is not among OtherFiles, IgnoredFiles, or names of Files", filename)
3843
}
39-
40-
// TODO(adonovan): use go1.21 slices.Contains.
41-
func slicesContains[S ~[]E, E comparable](slice S, x E) bool {
42-
for _, elem := range slice {
43-
if elem == x {
44-
return true
45-
}
46-
}
47-
return false
48-
}

internal/x/tools/analysisinternal/readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ This is just a copy of the code without any changes.
55

66
## History
77

8-
- sync with https://github.com/golang/tools/blob/v0.28.0
8+
- https://github.com/golangci/golangci-lint/pull/XXXX
9+
- sync with https://github.com/golang/tools/blob/v0.37.0/internal/analysisinternal/
10+
- https://github.com/golangci/golangci-lint/pull/5576
11+
- sync with https://github.com/golang/tools/blob/v0.28.0/internal/analysisinternal/

internal/x/tools/diff/diff.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package diff
77

88
import (
99
"fmt"
10+
"slices"
1011
"sort"
1112
"strings"
1213
)
@@ -64,7 +65,7 @@ func ApplyBytes(src []byte, edits []Edit) ([]byte, error) {
6465
// It may return a different slice.
6566
func validate(src string, edits []Edit) ([]Edit, int, error) {
6667
if !sort.IsSorted(editsSort(edits)) {
67-
edits = append([]Edit(nil), edits...)
68+
edits = slices.Clone(edits)
6869
SortEdits(edits)
6970
}
7071

internal/x/tools/diff/lcs/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (l lcs) fix() lcs {
5151
// from the set of diagonals in l, find a maximal non-conflicting set
5252
// this problem may be NP-complete, but we use a greedy heuristic,
5353
// which is quadratic, but with a better data structure, could be D log D.
54-
// indepedent is not enough: {0,3,1} and {3,0,2} can't both occur in an lcs
54+
// independent is not enough: {0,3,1} and {3,0,2} can't both occur in an lcs
5555
// which has to have monotone x and y
5656
if len(l) == 0 {
5757
return nil

internal/x/tools/diff/lcs/common_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ package lcs
66

77
import (
88
"log"
9-
"math/rand"
9+
"math/rand/v2"
10+
"slices"
1011
"strings"
1112
"testing"
1213
)
@@ -72,10 +73,8 @@ func check(t *testing.T, str string, lcs lcs, want []string) {
7273
got.WriteString(str[dd.X : dd.X+dd.Len])
7374
}
7475
ans := got.String()
75-
for _, w := range want {
76-
if ans == w {
77-
return
78-
}
76+
if slices.Contains(want, ans) {
77+
return
7978
}
8079
t.Fatalf("str=%q lcs=%v want=%q got=%q", str, lcs, want, ans)
8180
}
@@ -106,11 +105,11 @@ func lcslen(l lcs) int {
106105
}
107106

108107
// return a random string of length n made of characters from s
109-
func randstr(s string, n int) string {
108+
func randstr(rng *rand.Rand, s string, n int) string {
110109
src := []rune(s)
111110
x := make([]rune, n)
112-
for i := 0; i < n; i++ {
113-
x[i] = src[rand.Intn(len(src))]
111+
for i := range n {
112+
x[i] = src[rng.Int64N(int64(len(src)))]
114113
}
115114
return string(x)
116115
}

internal/x/tools/diff/lcs/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ computed labels. That is the worst case. Had the code noticed (x,y)=(u,v)=(3,3)
139139
from the edgegraph. The implementation looks for a number of special cases to try to avoid computing an extra forward path.
140140
141141
If the two-sided algorithm has stop early (because D has become too large) it will have found a forward LCS and a
142-
backwards LCS. Ideally these go with disjoint prefixes and suffixes of A and B, but disjointness may fail and the two
142+
backwards LCS. Ideally these go with disjoint prefixes and suffixes of A and B, but disjointedness may fail and the two
143143
computed LCS may conflict. (An easy example is where A is a suffix of B, and shares a short prefix. The backwards LCS
144144
is all of A, and the forward LCS is a prefix of A.) The algorithm combines the two
145145
to form a best-effort LCS. In the worst case the forward partial LCS may have to

internal/x/tools/diff/lcs/old.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func forward(e *editGraph) lcs {
105105
return ans
106106
}
107107
// from D to D+1
108-
for D := 0; D < e.limit; D++ {
108+
for D := range e.limit {
109109
e.setForward(D+1, -(D + 1), e.getForward(D, -D))
110110
if ok, ans := e.fdone(D+1, -(D + 1)); ok {
111111
return ans
@@ -199,13 +199,14 @@ func (e *editGraph) bdone(D, k int) (bool, lcs) {
199199
}
200200

201201
// run the backward algorithm, until success or up to the limit on D.
202+
// (used only by tests)
202203
func backward(e *editGraph) lcs {
203204
e.setBackward(0, 0, e.ux)
204205
if ok, ans := e.bdone(0, 0); ok {
205206
return ans
206207
}
207208
// from D to D+1
208-
for D := 0; D < e.limit; D++ {
209+
for D := range e.limit {
209210
e.setBackward(D+1, -(D + 1), e.getBackward(D, -D)-1)
210211
if ok, ans := e.bdone(D+1, -(D + 1)); ok {
211212
return ans
@@ -299,7 +300,7 @@ func twosided(e *editGraph) lcs {
299300
e.setBackward(0, 0, e.ux)
300301

301302
// from D to D+1
302-
for D := 0; D < e.limit; D++ {
303+
for D := range e.limit {
303304
// just finished a backwards pass, so check
304305
if got, ok := e.twoDone(D, D); ok {
305306
return e.twolcs(D, D, got)
@@ -376,10 +377,7 @@ func (e *editGraph) twoDone(df, db int) (int, bool) {
376377
if (df+db+e.delta)%2 != 0 {
377378
return 0, false // diagonals cannot overlap
378379
}
379-
kmin := -db + e.delta
380-
if -df > kmin {
381-
kmin = -df
382-
}
380+
kmin := max(-df, -db+e.delta)
383381
kmax := db + e.delta
384382
if df < kmax {
385383
kmax = df

internal/x/tools/diff/lcs/old_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package lcs
77
import (
88
"fmt"
99
"log"
10-
"math/rand"
10+
"math/rand/v2"
1111
"os"
1212
"strings"
1313
"testing"
@@ -106,15 +106,16 @@ func TestRegressionOld003(t *testing.T) {
106106
}
107107

108108
func TestRandOld(t *testing.T) {
109-
rand.Seed(1)
110-
for i := 0; i < 1000; i++ {
109+
rng := rng(t)
110+
111+
for i := range 1000 {
111112
// TODO(adonovan): use ASCII and bytesSeqs here? The use of
112113
// non-ASCII isn't relevant to the property exercised by the test.
113-
a := []rune(randstr("abω", 16))
114-
b := []rune(randstr("abωc", 16))
114+
a := []rune(randstr(rng, "abω", 16))
115+
b := []rune(randstr(rng, "abωc", 16))
115116
seq := runesSeqs{a, b}
116117

117-
const lim = 24 // large enough to get true lcs
118+
const lim = 0 // make sure we get the lcs (24 was too small)
118119
_, forw := compute(seq, forward, lim)
119120
_, back := compute(seq, backward, lim)
120121
_, two := compute(seq, twosided, lim)
@@ -158,7 +159,7 @@ func TestDiffAPI(t *testing.T) {
158159
}
159160

160161
func BenchmarkTwoOld(b *testing.B) {
161-
tests := genBench("abc", 96)
162+
tests := genBench(rng(b), "abc", 96)
162163
for i := 0; i < b.N; i++ {
163164
for _, tt := range tests {
164165
_, two := compute(stringSeqs{tt.before, tt.after}, twosided, 100)
@@ -170,7 +171,7 @@ func BenchmarkTwoOld(b *testing.B) {
170171
}
171172

172173
func BenchmarkForwOld(b *testing.B) {
173-
tests := genBench("abc", 96)
174+
tests := genBench(rng(b), "abc", 96)
174175
for i := 0; i < b.N; i++ {
175176
for _, tt := range tests {
176177
_, two := compute(stringSeqs{tt.before, tt.after}, forward, 100)
@@ -181,14 +182,21 @@ func BenchmarkForwOld(b *testing.B) {
181182
}
182183
}
183184

184-
func genBench(set string, n int) []struct{ before, after string } {
185+
// rng returns a randomly initialized PRNG whose seeds are logged so
186+
// that occasional test failures can be deterministically replayed.
187+
func rng(tb testing.TB) *rand.Rand {
188+
seed1, seed2 := rand.Uint64(), rand.Uint64()
189+
tb.Logf("PRNG seeds: %d, %d", seed1, seed2)
190+
return rand.New(rand.NewPCG(seed1, seed2))
191+
}
192+
193+
func genBench(rng *rand.Rand, set string, n int) []struct{ before, after string } {
185194
// before and after for benchmarks. 24 strings of length n with
186195
// before and after differing at least once, and about 5%
187-
rand.Seed(3)
188196
var ans []struct{ before, after string }
189-
for i := 0; i < 24; i++ {
197+
for range 24 {
190198
// maybe b should have an approximately known number of diffs
191-
a := randstr(set, n)
199+
a := randstr(rng, set, n)
192200
cnt := 0
193201
bb := make([]rune, 0, n)
194202
for _, r := range a {

internal/x/tools/diff/ndiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func diffRunes(before, after []rune) []Edit {
7272
func runes(bytes []byte) []rune {
7373
n := utf8.RuneCount(bytes)
7474
runes := make([]rune, n)
75-
for i := 0; i < n; i++ {
75+
for i := range n {
7676
r, sz := utf8.DecodeRune(bytes)
7777
bytes = bytes[sz:]
7878
runes[i] = r

0 commit comments

Comments
 (0)