From ee69ea24143ab20dc65638b78f194180a132d290 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 23 Dec 2024 15:15:52 +0000 Subject: [PATCH 001/309] go/analysis/analysistest: avoid nil panic from an invalid token.Pos See failures in CL 638395 Change-Id: I03ea2ab77fd71707eb9fd6c2dd53ab553f5d8563 GitHub-Last-Rev: 39e217ee73c6c3503b133f741b6d6332cc0fb972 GitHub-Pull-Request: golang/tools#549 Reviewed-on: https://go-review.googlesource.com/c/tools/+/638415 Auto-Submit: Robert Griesemer Commit-Queue: Alan Donovan Reviewed-by: Robert Griesemer Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Cherry Mui --- go/analysis/analysistest/analysistest.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 3cc2beca737..7a27e006033 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -191,11 +191,20 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns act.Analyzer.Name, start, end) continue } - file, endfile := act.Package.Fset.File(start), act.Package.Fset.File(end) - if file == nil || endfile == nil || file != endfile { + file := act.Package.Fset.File(start) + if file == nil { + t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed start position %v", act.Analyzer.Name, start) + continue + } + endFile := act.Package.Fset.File(end) + if endFile == nil { + t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed end position %v", act.Analyzer.Name, end) + continue + } + if file != endFile { t.Errorf( "diagnostic for analysis %v contains Suggested Fix with malformed spanning files %v and %v", - act.Analyzer.Name, file.Name(), endfile.Name()) + act.Analyzer.Name, file.Name(), endFile.Name()) continue } if _, ok := fileContents[file]; !ok { From a2408f8cd32f9ec9a896262fa6ce199b0ee64ade Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 7 Jan 2025 10:51:55 -0500 Subject: [PATCH 002/309] internal/astutil/cursor: Cursor.Children: document invariants Fixes golang/go#71074 Change-Id: I640748cde4272f12696e3125f82b3a84afe9376f Reviewed-on: https://go-review.googlesource.com/c/tools/+/641075 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- internal/astutil/cursor/cursor.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 9f0b906f1c2..945170be25c 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -197,6 +197,8 @@ func (c Cursor) Parent() Cursor { // the last node in the list, or is not part of a list. // // NextSibling must not be called on the Root node. +// +// See note at [Cursor.Children]. func (c Cursor) NextSibling() (Cursor, bool) { if c.index < 0 { panic("Cursor.NextSibling called on Root node") @@ -218,6 +220,8 @@ func (c Cursor) NextSibling() (Cursor, bool) { // the first node in the list, or is not part of a list. // // It must not be called on the Root node. +// +// See note at [Cursor.Children]. func (c Cursor) PrevSibling() (Cursor, bool) { if c.index < 0 { panic("Cursor.PrevSibling called on Root node") @@ -266,6 +270,26 @@ func (c Cursor) LastChild() (Cursor, bool) { // Children returns an iterator over the direct children of the // current node, if any. +// +// When using Children, NextChild, and PrevChild, bear in mind that a +// Node's children may come from different fields, some of which may +// be lists of nodes without a distinguished intervening container +// such as [ast.BlockStmt]. +// +// For example, [ast.CaseClause] has a field List of expressions and a +// field Body of statements, so the children of a CaseClause are a mix +// of expressions and statements. Other nodes that have "uncontained" +// list fields include: +// +// - [ast.ValueSpec] (Names, Values) +// - [ast.CompositeLit] (Type, Elts) +// - [ast.IndexListExpr] (X, Indices) +// - [ast.CallExpr] (Fun, Args) +// - [ast.AssignStmt] (Lhs, Rhs) +// +// So, do not assume that the previous sibling of an ast.Stmt is also +// an ast.Stmt unless you have established that, say, its parent is a +// BlockStmt. func (c Cursor) Children() iter.Seq[Cursor] { return func(yield func(Cursor) bool) { c, ok := c.FirstChild() From a339e37cca94adf4fec5665dc7f3172f9ea5263b Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 3 Jan 2025 12:22:23 -0500 Subject: [PATCH 003/309] gopls/internal/util/persistent: {Map,Set}: use iter.Seq2 This CL updates the tree data structures to use go1.23-style iterators. package persistent func (*Map[K, V]) Keys() iter.Seq[K] func (*Map[K, V]) All() iter.Seq2[K, V] func (*Set[K]) All() iter.Seq[K] Change-Id: I4b8917fa35c38e055e42e10cefea7997fe7b35f3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640035 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/cache/filemap.go | 17 +++++---- gopls/internal/cache/filemap_test.go | 8 ++-- gopls/internal/cache/load.go | 4 +- gopls/internal/cache/snapshot.go | 30 +++++++-------- gopls/internal/cache/view.go | 2 +- gopls/internal/util/persistent/map.go | 43 +++++++++++----------- gopls/internal/util/persistent/map_test.go | 4 +- gopls/internal/util/persistent/set.go | 20 ++++++---- gopls/internal/util/persistent/set_test.go | 4 +- 9 files changed, 69 insertions(+), 63 deletions(-) diff --git a/gopls/internal/cache/filemap.go b/gopls/internal/cache/filemap.go index c826141ed98..1f1fd947d71 100644 --- a/gopls/internal/cache/filemap.go +++ b/gopls/internal/cache/filemap.go @@ -5,6 +5,7 @@ package cache import ( + "iter" "path/filepath" "golang.org/x/tools/gopls/internal/file" @@ -77,9 +78,9 @@ func (m *fileMap) get(key protocol.DocumentURI) (file.Handle, bool) { return m.files.Get(key) } -// foreach calls f for each (uri, fh) in the map. -func (m *fileMap) foreach(f func(uri protocol.DocumentURI, fh file.Handle)) { - m.files.Range(f) +// all returns the sequence of (uri, fh) entries in the map. +func (m *fileMap) all() iter.Seq2[protocol.DocumentURI, file.Handle] { + return m.files.All() } // set stores the given file handle for key, updating overlays and directories @@ -130,9 +131,9 @@ func (m *fileMap) delete(key protocol.DocumentURI) { // getOverlays returns a new unordered array of overlay files. func (m *fileMap) getOverlays() []*overlay { var overlays []*overlay - m.overlays.Range(func(_ protocol.DocumentURI, o *overlay) { + for _, o := range m.overlays.All() { overlays = append(overlays, o) - }) + } return overlays } @@ -143,9 +144,9 @@ func (m *fileMap) getOverlays() []*overlay { func (m *fileMap) getDirs() *persistent.Set[string] { if m.dirs == nil { m.dirs = new(persistent.Set[string]) - m.files.Range(func(u protocol.DocumentURI, _ file.Handle) { - m.addDirs(u) - }) + for uri := range m.files.All() { + m.addDirs(uri) + } } return m.dirs } diff --git a/gopls/internal/cache/filemap_test.go b/gopls/internal/cache/filemap_test.go index 13f2c1a9ccd..24b3a19d108 100644 --- a/gopls/internal/cache/filemap_test.go +++ b/gopls/internal/cache/filemap_test.go @@ -83,9 +83,9 @@ func TestFileMap(t *testing.T) { } var gotFiles []string - m.foreach(func(uri protocol.DocumentURI, _ file.Handle) { + for uri := range m.all() { gotFiles = append(gotFiles, normalize(uri.Path())) - }) + } sort.Strings(gotFiles) if diff := cmp.Diff(test.wantFiles, gotFiles); diff != "" { t.Errorf("Files mismatch (-want +got):\n%s", diff) @@ -100,9 +100,9 @@ func TestFileMap(t *testing.T) { } var gotDirs []string - m.getDirs().Range(func(dir string) { + for dir := range m.getDirs().All() { gotDirs = append(gotDirs, normalize(dir)) - }) + } sort.Strings(gotDirs) if diff := cmp.Diff(test.wantDirs, gotDirs); diff != "" { t.Errorf("Dirs mismatch (-want +got):\n%s", diff) diff --git a/gopls/internal/cache/load.go b/gopls/internal/cache/load.go index 873cef56a2b..140cbc45490 100644 --- a/gopls/internal/cache/load.go +++ b/gopls/internal/cache/load.go @@ -262,11 +262,11 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork AllowNetwork, scopes . s.mu.Lock() // Assert the invariant s.packages.Get(id).m == s.meta.metadata[id]. - s.packages.Range(func(id PackageID, ph *packageHandle) { + for id, ph := range s.packages.All() { if s.meta.Packages[id] != ph.mp { panic("inconsistent metadata") } - }) + } // Compute the minimal metadata updates (for Clone) // required to preserve the above invariant. diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index de4a52ff6cb..ffca1dc00ec 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -344,11 +344,11 @@ func (s *Snapshot) Templates() map[protocol.DocumentURI]file.Handle { defer s.mu.Unlock() tmpls := map[protocol.DocumentURI]file.Handle{} - s.files.foreach(func(k protocol.DocumentURI, fh file.Handle) { + for k, fh := range s.files.all() { if s.FileKind(fh) == file.Tmpl { tmpls[k] = fh } - }) + } return tmpls } @@ -864,13 +864,13 @@ func (s *Snapshot) addKnownSubdirs(patterns map[protocol.RelativePattern]unit, w s.mu.Lock() defer s.mu.Unlock() - s.files.getDirs().Range(func(dir string) { + for dir := range s.files.getDirs().All() { for _, wsDir := range wsDirs { if pathutil.InDir(wsDir, dir) { patterns[protocol.RelativePattern{Pattern: filepath.ToSlash(dir)}] = unit{} } } - }) + } } // watchSubdirs reports whether gopls should request separate file watchers for @@ -912,11 +912,11 @@ func (s *Snapshot) filesInDir(uri protocol.DocumentURI) []protocol.DocumentURI { return nil } var files []protocol.DocumentURI - s.files.foreach(func(uri protocol.DocumentURI, _ file.Handle) { + for uri := range s.files.all() { if pathutil.InDir(dir, uri.Path()) { files = append(files, uri) } - }) + } return files } @@ -1029,13 +1029,11 @@ func (s *Snapshot) clearShouldLoad(scopes ...loadScope) { case packageLoadScope: scopePath := PackagePath(scope) var toDelete []PackageID - s.shouldLoad.Range(func(id PackageID, pkgPaths []PackagePath) { - for _, pkgPath := range pkgPaths { - if pkgPath == scopePath { - toDelete = append(toDelete, id) - } + for id, pkgPaths := range s.shouldLoad.All() { + if slices.Contains(pkgPaths, scopePath) { + toDelete = append(toDelete, id) } - }) + } for _, id := range toDelete { s.shouldLoad.Delete(id) } @@ -1183,7 +1181,7 @@ func (s *Snapshot) reloadWorkspace(ctx context.Context) { var scopes []loadScope var seen map[PackagePath]bool s.mu.Lock() - s.shouldLoad.Range(func(_ PackageID, pkgPaths []PackagePath) { + for _, pkgPaths := range s.shouldLoad.All() { for _, pkgPath := range pkgPaths { if seen == nil { seen = make(map[PackagePath]bool) @@ -1194,7 +1192,7 @@ func (s *Snapshot) reloadWorkspace(ctx context.Context) { seen[pkgPath] = true scopes = append(scopes, packageLoadScope(pkgPath)) } - }) + } s.mu.Unlock() if len(scopes) == 0 { @@ -1886,13 +1884,13 @@ func deleteMostRelevantModFile(m *persistent.Map[protocol.DocumentURI, *memoize. var mostRelevant protocol.DocumentURI changedFile := changed.Path() - m.Range(func(modURI protocol.DocumentURI, _ *memoize.Promise) { + for modURI := range m.All() { if len(modURI) > len(mostRelevant) { if pathutil.InDir(modURI.DirPath(), changedFile) { mostRelevant = modURI } } - }) + } if mostRelevant != "" { m.Delete(mostRelevant) } diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index 5fb03cb1152..33c77760e7f 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -1171,7 +1171,7 @@ func (s *Snapshot) Vulnerabilities(modfiles ...protocol.DocumentURI) map[protoco defer s.mu.Unlock() if len(modfiles) == 0 { // empty means all modfiles - modfiles = s.vulns.Keys() + modfiles = slices.Collect(s.vulns.Keys()) } for _, modfile := range modfiles { vuln, _ := s.vulns.Get(modfile) diff --git a/gopls/internal/util/persistent/map.go b/gopls/internal/util/persistent/map.go index 5cb556a482b..193f98791d8 100644 --- a/gopls/internal/util/persistent/map.go +++ b/gopls/internal/util/persistent/map.go @@ -9,6 +9,7 @@ package persistent import ( "fmt" + "iter" "math/rand" "strings" "sync/atomic" @@ -57,10 +58,10 @@ func (m *Map[K, V]) String() string { var buf strings.Builder buf.WriteByte('{') var sep string - m.Range(func(k K, v V) { + for k, v := range m.All() { fmt.Fprintf(&buf, "%s%v: %v", sep, k, v) sep = ", " - }) + } buf.WriteByte('}') return buf.String() } @@ -149,29 +150,29 @@ func (pm *Map[K, V]) Clear() { pm.root = nil } -// Keys returns all keys present in the map. -func (pm *Map[K, V]) Keys() []K { - var keys []K - pm.root.forEach(func(k, _ any) { - keys = append(keys, k.(K)) - }) - return keys +// Keys returns the ascending sequence of keys present in the map. +func (pm *Map[K, V]) Keys() iter.Seq[K] { + return func(yield func(K) bool) { + pm.root.forEach(func(k, _ any) bool { + return yield(k.(K)) + }) + } } -// Range calls f sequentially in ascending key order for all entries in the map. -func (pm *Map[K, V]) Range(f func(key K, value V)) { - pm.root.forEach(func(k, v any) { - f(k.(K), v.(V)) - }) +// All returns the sequence of map entries in ascending key order. +func (pm *Map[K, V]) All() iter.Seq2[K, V] { + return func(yield func(K, V) bool) { + pm.root.forEach(func(k, v any) bool { + return yield(k.(K), v.(V)) + }) + } } -func (node *mapNode) forEach(f func(key, value any)) { - if node == nil { - return - } - node.left.forEach(f) - f(node.key, node.value.value) - node.right.forEach(f) +func (node *mapNode) forEach(yield func(key, value any) bool) bool { + return node == nil || + node.left.forEach(yield) && + yield(node.key, node.value.value) && + node.right.forEach(yield) } // Get returns the map value associated with the specified key. diff --git a/gopls/internal/util/persistent/map_test.go b/gopls/internal/util/persistent/map_test.go index effa1c1da85..88dced2a85f 100644 --- a/gopls/internal/util/persistent/map_test.go +++ b/gopls/internal/util/persistent/map_test.go @@ -240,12 +240,12 @@ func (vm *validatedMap) validate(t *testing.T) { } actualMap := make(map[int]int, len(vm.expected)) - vm.impl.Range(func(key, value int) { + for key, value := range vm.impl.All() { if other, ok := actualMap[key]; ok { t.Fatalf("key is present twice, key: %d, first value: %d, second value: %d", key, value, other) } actualMap[key] = value - }) + } assertSameMap(t, actualMap, vm.expected) } diff --git a/gopls/internal/util/persistent/set.go b/gopls/internal/util/persistent/set.go index 2d5f4edac96..e47d046fb48 100644 --- a/gopls/internal/util/persistent/set.go +++ b/gopls/internal/util/persistent/set.go @@ -4,7 +4,11 @@ package persistent -import "golang.org/x/tools/gopls/internal/util/constraints" +import ( + "iter" + + "golang.org/x/tools/gopls/internal/util/constraints" +) // Set is a collection of elements of type K. // @@ -43,12 +47,14 @@ func (s *Set[K]) Contains(key K) bool { return ok } -// Range calls f sequentially in ascending key order for all entries in the set. -func (s *Set[K]) Range(f func(key K)) { - if s.impl != nil { - s.impl.Range(func(key K, _ struct{}) { - f(key) - }) +// All returns the sequence of set elements in ascending order. +func (s *Set[K]) All() iter.Seq[K] { + return func(yield func(K) bool) { + if s.impl != nil { + s.impl.root.forEach(func(k, _ any) bool { + return yield(k.(K)) + }) + } } } diff --git a/gopls/internal/util/persistent/set_test.go b/gopls/internal/util/persistent/set_test.go index 31911b451b3..192b1c74121 100644 --- a/gopls/internal/util/persistent/set_test.go +++ b/gopls/internal/util/persistent/set_test.go @@ -111,11 +111,11 @@ func diff[K constraints.Ordered](got *persistent.Set[K], want []K) string { wantSet[w] = struct{}{} } var diff []string - got.Range(func(key K) { + for key := range got.All() { if _, ok := wantSet[key]; !ok { diff = append(diff, fmt.Sprintf("+%v", key)) } - }) + } for key := range wantSet { if !got.Contains(key) { diff = append(diff, fmt.Sprintf("-%v", key)) From 8179c75b4c86b1b844643a584ece1f4ea970c1bf Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 2 Jan 2025 14:59:30 -0500 Subject: [PATCH 004/309] internal/analysisinternal: factor useful helper functions This CL refines, generalizes, and promotes a number of helper functions that have proven useful in go/analysis/passes/... and gopls' modernizer analyzer: package analysisinternal func Format func Imports func Is{Method,Type,Function}Named Details: - all the Is*Named functions accept a list of candidates, and a types.Object (that may be nil). This makes it much more convenient to use on the result of typeutil.Callee. Also, modernizations in passing: - info.PkgNameOf - interface{} -> any - slices.Contains - fmt.Appendf - etc Change-Id: Ib441af3eef9c5030f0ee2b38df69b4a25015ed97 Reviewed-on: https://go-review.googlesource.com/c/tools/+/639157 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/passes/assign/assign.go | 7 +- go/analysis/passes/atomic/atomic.go | 11 +-- go/analysis/passes/atomicalign/atomicalign.go | 12 +-- go/analysis/passes/bools/bools.go | 9 +- go/analysis/passes/cgocall/cgocall.go | 6 +- go/analysis/passes/copylock/copylock.go | 14 +-- .../passes/deepequalerrors/deepequalerrors.go | 10 +-- go/analysis/passes/defers/defers.go | 7 +- go/analysis/passes/errorsas/errorsas.go | 10 +-- go/analysis/passes/httpmux/httpmux.go | 10 ++- .../passes/httpresponse/httpresponse.go | 12 +-- .../passes/internal/analysisutil/util.go | 62 ------------- go/analysis/passes/loopclosure/loopclosure.go | 3 +- go/analysis/passes/lostcancel/lostcancel.go | 3 +- go/analysis/passes/printf/printf.go | 17 ++-- .../reflectvaluecompare.go | 8 +- go/analysis/passes/shift/shift.go | 4 +- go/analysis/passes/sigchanyzer/sigchanyzer.go | 9 +- go/analysis/passes/slog/slog.go | 7 +- go/analysis/passes/sortslice/analyzer.go | 13 +-- .../testinggoroutine/testinggoroutine.go | 3 +- go/analysis/passes/testinggoroutine/util.go | 2 + go/analysis/passes/tests/tests.go | 3 +- go/analysis/passes/timeformat/timeformat.go | 24 ++--- go/analysis/passes/unmarshal/unmarshal.go | 2 +- go/analysis/passes/unsafeptr/unsafeptr.go | 5 +- go/analysis/passes/waitgroup/waitgroup.go | 22 +---- gopls/internal/analysis/modernize/bloop.go | 39 ++------ .../internal/analysis/modernize/fmtappendf.go | 26 +++--- gopls/internal/analysis/modernize/maps.go | 10 +-- gopls/internal/analysis/modernize/minmax.go | 11 +-- .../internal/analysis/modernize/modernize.go | 27 ------ gopls/internal/analysis/modernize/slices.go | 14 +-- .../internal/analysis/modernize/sortslice.go | 30 ++----- internal/analysisinternal/analysis.go | 89 ++++++++++++++++--- 35 files changed, 239 insertions(+), 302 deletions(-) diff --git a/go/analysis/passes/assign/assign.go b/go/analysis/passes/assign/assign.go index 0d95fefcb5a..ff94c271c45 100644 --- a/go/analysis/passes/assign/assign.go +++ b/go/analysis/passes/assign/assign.go @@ -19,6 +19,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -32,7 +33,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ @@ -57,8 +58,8 @@ func run(pass *analysis.Pass) (interface{}, error) { if reflect.TypeOf(lhs) != reflect.TypeOf(rhs) { continue // short-circuit the heavy-weight gofmt check } - le := analysisutil.Format(pass.Fset, lhs) - re := analysisutil.Format(pass.Fset, rhs) + le := analysisinternal.Format(pass.Fset, lhs) + re := analysisinternal.Format(pass.Fset, rhs) if le == re { pass.Report(analysis.Diagnostic{ Pos: stmt.Pos(), Message: fmt.Sprintf("self-assignment of %s to %s", re, le), diff --git a/go/analysis/passes/atomic/atomic.go b/go/analysis/passes/atomic/atomic.go index 931f9ca7540..82d5439ce57 100644 --- a/go/analysis/passes/atomic/atomic.go +++ b/go/analysis/passes/atomic/atomic.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -28,8 +29,8 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "sync/atomic") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "sync/atomic") { return nil, nil // doesn't directly import sync/atomic } @@ -52,8 +53,8 @@ func run(pass *analysis.Pass) (interface{}, error) { if !ok { continue } - fn := typeutil.StaticCallee(pass.TypesInfo, call) - if analysisutil.IsFunctionNamed(fn, "sync/atomic", "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintptr") { + obj := typeutil.Callee(pass.TypesInfo, call) + if analysisinternal.IsFunctionNamed(obj, "sync/atomic", "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintptr") { checkAtomicAddAssignment(pass, n.Lhs[i], call) } } @@ -71,7 +72,7 @@ func checkAtomicAddAssignment(pass *analysis.Pass, left ast.Expr, call *ast.Call arg := call.Args[0] broken := false - gofmt := func(e ast.Expr) string { return analysisutil.Format(pass.Fset, e) } + gofmt := func(e ast.Expr) string { return analysisinternal.Format(pass.Fset, e) } if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND { broken = gofmt(left) == gofmt(uarg.X) diff --git a/go/analysis/passes/atomicalign/atomicalign.go b/go/analysis/passes/atomicalign/atomicalign.go index aff6d25b3e1..2508b41f661 100644 --- a/go/analysis/passes/atomicalign/atomicalign.go +++ b/go/analysis/passes/atomicalign/atomicalign.go @@ -16,9 +16,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) const Doc = "check for non-64-bits-aligned arguments to sync/atomic functions" @@ -31,11 +31,11 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { if 8*pass.TypesSizes.Sizeof(types.Typ[types.Uintptr]) == 64 { return nil, nil // 64-bit platform } - if !analysisutil.Imports(pass.Pkg, "sync/atomic") { + if !analysisinternal.Imports(pass.Pkg, "sync/atomic") { return nil, nil // doesn't directly import sync/atomic } @@ -53,10 +53,10 @@ func run(pass *analysis.Pass) (interface{}, error) { inspect.Preorder(nodeFilter, func(node ast.Node) { call := node.(*ast.CallExpr) - fn := typeutil.StaticCallee(pass.TypesInfo, call) - if analysisutil.IsFunctionNamed(fn, "sync/atomic", funcNames...) { + obj := typeutil.Callee(pass.TypesInfo, call) + if analysisinternal.IsFunctionNamed(obj, "sync/atomic", funcNames...) { // For all the listed functions, the expression to check is always the first function argument. - check64BitAlignment(pass, fn.Name(), call.Args[0]) + check64BitAlignment(pass, obj.Name(), call.Args[0]) } }) diff --git a/go/analysis/passes/bools/bools.go b/go/analysis/passes/bools/bools.go index 8cec6e8224a..e1cf9f9b7ad 100644 --- a/go/analysis/passes/bools/bools.go +++ b/go/analysis/passes/bools/bools.go @@ -15,6 +15,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" ) const Doc = "check for common mistakes involving boolean operators" @@ -27,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ @@ -103,7 +104,7 @@ func (op boolOp) commutativeSets(info *types.Info, e *ast.BinaryExpr, seen map[* func (op boolOp) checkRedundant(pass *analysis.Pass, exprs []ast.Expr) { seen := make(map[string]bool) for _, e := range exprs { - efmt := analysisutil.Format(pass.Fset, e) + efmt := analysisinternal.Format(pass.Fset, e) if seen[efmt] { pass.ReportRangef(e, "redundant %s: %s %s %s", op.name, efmt, op.tok, efmt) } else { @@ -149,8 +150,8 @@ func (op boolOp) checkSuspect(pass *analysis.Pass, exprs []ast.Expr) { } // e is of the form 'x != c' or 'x == c'. - xfmt := analysisutil.Format(pass.Fset, x) - efmt := analysisutil.Format(pass.Fset, e) + xfmt := analysisinternal.Format(pass.Fset, x) + efmt := analysisinternal.Format(pass.Fset, e) if prev, found := seen[xfmt]; found { // checkRedundant handles the case in which efmt == prev. if efmt != prev { diff --git a/go/analysis/passes/cgocall/cgocall.go b/go/analysis/passes/cgocall/cgocall.go index 613583a1a64..4f3bb035d65 100644 --- a/go/analysis/passes/cgocall/cgocall.go +++ b/go/analysis/passes/cgocall/cgocall.go @@ -18,7 +18,7 @@ import ( "strconv" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" + "golang.org/x/tools/internal/analysisinternal" ) const debug = false @@ -40,8 +40,8 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "runtime/cgo") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "runtime/cgo") { return nil, nil // doesn't use cgo } diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go index 03496cb3037..a9f02ac62e6 100644 --- a/go/analysis/passes/copylock/copylock.go +++ b/go/analysis/passes/copylock/copylock.go @@ -15,8 +15,8 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/versions" ) @@ -86,7 +86,7 @@ func checkCopyLocksAssign(pass *analysis.Pass, assign *ast.AssignStmt, goversion lhs := assign.Lhs for i, x := range assign.Rhs { if path := lockPathRhs(pass, x); path != nil { - pass.ReportRangef(x, "assignment copies lock value to %v: %v", analysisutil.Format(pass.Fset, assign.Lhs[i]), path) + pass.ReportRangef(x, "assignment copies lock value to %v: %v", analysisinternal.Format(pass.Fset, assign.Lhs[i]), path) lhs = nil // An lhs has been reported. We prefer the assignment warning and do not report twice. } } @@ -100,7 +100,7 @@ func checkCopyLocksAssign(pass *analysis.Pass, assign *ast.AssignStmt, goversion if id, ok := l.(*ast.Ident); ok && id.Name != "_" { if obj := pass.TypesInfo.Defs[id]; obj != nil && obj.Type() != nil { if path := lockPath(pass.Pkg, obj.Type(), nil); path != nil { - pass.ReportRangef(l, "for loop iteration copies lock value to %v: %v", analysisutil.Format(pass.Fset, l), path) + pass.ReportRangef(l, "for loop iteration copies lock value to %v: %v", analysisinternal.Format(pass.Fset, l), path) } } } @@ -132,7 +132,7 @@ func checkCopyLocksCompositeLit(pass *analysis.Pass, cl *ast.CompositeLit) { x = node.Value } if path := lockPathRhs(pass, x); path != nil { - pass.ReportRangef(x, "literal copies lock value from %v: %v", analysisutil.Format(pass.Fset, x), path) + pass.ReportRangef(x, "literal copies lock value from %v: %v", analysisinternal.Format(pass.Fset, x), path) } } } @@ -163,7 +163,7 @@ func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) { } for _, x := range ce.Args { if path := lockPathRhs(pass, x); path != nil { - pass.ReportRangef(x, "call of %s copies lock value: %v", analysisutil.Format(pass.Fset, ce.Fun), path) + pass.ReportRangef(x, "call of %s copies lock value: %v", analysisinternal.Format(pass.Fset, ce.Fun), path) } } } @@ -230,7 +230,7 @@ func checkCopyLocksRangeVar(pass *analysis.Pass, rtok token.Token, e ast.Expr) { return } if path := lockPath(pass.Pkg, typ, nil); path != nil { - pass.Reportf(e.Pos(), "range var %s copies lock: %v", analysisutil.Format(pass.Fset, e), path) + pass.Reportf(e.Pos(), "range var %s copies lock: %v", analysisinternal.Format(pass.Fset, e), path) } } @@ -350,7 +350,7 @@ func lockPath(tpkg *types.Package, typ types.Type, seen map[types.Type]bool) typ // In go1.10, sync.noCopy did not implement Locker. // (The Unlock method was added only in CL 121876.) // TODO(adonovan): remove workaround when we drop go1.10. - if analysisutil.IsNamedType(typ, "sync", "noCopy") { + if analysisinternal.IsTypeNamed(typ, "sync", "noCopy") { return []string{typ.String()} } diff --git a/go/analysis/passes/deepequalerrors/deepequalerrors.go b/go/analysis/passes/deepequalerrors/deepequalerrors.go index 70b5e39ecf8..d15e3bc59ba 100644 --- a/go/analysis/passes/deepequalerrors/deepequalerrors.go +++ b/go/analysis/passes/deepequalerrors/deepequalerrors.go @@ -12,9 +12,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) const Doc = `check for calls of reflect.DeepEqual on error values @@ -34,8 +34,8 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "reflect") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "reflect") { return nil, nil // doesn't directly import reflect } @@ -46,8 +46,8 @@ func run(pass *analysis.Pass) (interface{}, error) { } inspect.Preorder(nodeFilter, func(n ast.Node) { call := n.(*ast.CallExpr) - fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func) - if analysisutil.IsFunctionNamed(fn, "reflect", "DeepEqual") && hasError(pass, call.Args[0]) && hasError(pass, call.Args[1]) { + obj := typeutil.Callee(pass.TypesInfo, call) + if analysisinternal.IsFunctionNamed(obj, "reflect", "DeepEqual") && hasError(pass, call.Args[0]) && hasError(pass, call.Args[1]) { pass.ReportRangef(call, "avoid using reflect.DeepEqual with errors") } }) diff --git a/go/analysis/passes/defers/defers.go b/go/analysis/passes/defers/defers.go index 5e8e80a6a77..e11957f2d09 100644 --- a/go/analysis/passes/defers/defers.go +++ b/go/analysis/passes/defers/defers.go @@ -13,6 +13,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -27,15 +28,15 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "time") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "time") { return nil, nil } checkDeferCall := func(node ast.Node) bool { switch v := node.(type) { case *ast.CallExpr: - if analysisutil.IsFunctionNamed(typeutil.StaticCallee(pass.TypesInfo, v), "time", "Since") { + if analysisinternal.IsFunctionNamed(typeutil.Callee(pass.TypesInfo, v), "time", "Since") { pass.Reportf(v.Pos(), "call to time.Since is not deferred") } case *ast.FuncLit: diff --git a/go/analysis/passes/errorsas/errorsas.go b/go/analysis/passes/errorsas/errorsas.go index 7f62ad4c825..b8d29d019db 100644 --- a/go/analysis/passes/errorsas/errorsas.go +++ b/go/analysis/passes/errorsas/errorsas.go @@ -13,9 +13,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) const Doc = `report passing non-pointer or non-error values to errors.As @@ -31,7 +31,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { switch pass.Pkg.Path() { case "errors", "errors_test": // These packages know how to use their own APIs. @@ -39,7 +39,7 @@ func run(pass *analysis.Pass) (interface{}, error) { return nil, nil } - if !analysisutil.Imports(pass.Pkg, "errors") { + if !analysisinternal.Imports(pass.Pkg, "errors") { return nil, nil // doesn't directly import errors } @@ -50,8 +50,8 @@ func run(pass *analysis.Pass) (interface{}, error) { } inspect.Preorder(nodeFilter, func(n ast.Node) { call := n.(*ast.CallExpr) - fn := typeutil.StaticCallee(pass.TypesInfo, call) - if !analysisutil.IsFunctionNamed(fn, "errors", "As") { + obj := typeutil.Callee(pass.TypesInfo, call) + if !analysisinternal.IsFunctionNamed(obj, "errors", "As") { return } if len(call.Args) < 2 { diff --git a/go/analysis/passes/httpmux/httpmux.go b/go/analysis/passes/httpmux/httpmux.go index 78748c5c12e..58d3ed5daca 100644 --- a/go/analysis/passes/httpmux/httpmux.go +++ b/go/analysis/passes/httpmux/httpmux.go @@ -14,9 +14,9 @@ import ( "golang.org/x/mod/semver" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typesinternal" ) @@ -45,7 +45,7 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } } - if !analysisutil.Imports(pass.Pkg, "net/http") { + if !analysisinternal.Imports(pass.Pkg, "net/http") { return nil, nil } // Look for calls to ServeMux.Handle or ServeMux.HandleFunc. @@ -78,7 +78,7 @@ func isServeMuxRegisterCall(pass *analysis.Pass, call *ast.CallExpr) bool { if fn == nil { return false } - if analysisutil.IsFunctionNamed(fn, "net/http", "Handle", "HandleFunc") { + if analysisinternal.IsFunctionNamed(fn, "net/http", "Handle", "HandleFunc") { return true } if !isMethodNamed(fn, "net/http", "Handle", "HandleFunc") { @@ -86,11 +86,13 @@ func isServeMuxRegisterCall(pass *analysis.Pass, call *ast.CallExpr) bool { } recv := fn.Type().(*types.Signature).Recv() // isMethodNamed() -> non-nil isPtr, named := typesinternal.ReceiverNamed(recv) - return isPtr && analysisutil.IsNamedType(named, "net/http", "ServeMux") + return isPtr && analysisinternal.IsTypeNamed(named, "net/http", "ServeMux") } // isMethodNamed reports when a function f is a method, // in a package with the path pkgPath and the name of f is in names. +// +// (Unlike [analysisinternal.IsMethodNamed], it ignores the receiver type name.) func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool { if f == nil { return false diff --git a/go/analysis/passes/httpresponse/httpresponse.go b/go/analysis/passes/httpresponse/httpresponse.go index 91ebe29de11..e9acd96547e 100644 --- a/go/analysis/passes/httpresponse/httpresponse.go +++ b/go/analysis/passes/httpresponse/httpresponse.go @@ -12,8 +12,8 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typesinternal" ) @@ -41,12 +41,12 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) // Fast path: if the package doesn't import net/http, // skip the traversal. - if !analysisutil.Imports(pass.Pkg, "net/http") { + if !analysisinternal.Imports(pass.Pkg, "net/http") { return nil, nil } @@ -118,7 +118,7 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool { return false // the function called does not return two values. } isPtr, named := typesinternal.ReceiverNamed(res.At(0)) - if !isPtr || named == nil || !analysisutil.IsNamedType(named, "net/http", "Response") { + if !isPtr || named == nil || !analysisinternal.IsTypeNamed(named, "net/http", "Response") { return false // the first return type is not *http.Response. } @@ -133,11 +133,11 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool { return ok && id.Name == "http" // function in net/http package. } - if analysisutil.IsNamedType(typ, "net/http", "Client") { + if analysisinternal.IsTypeNamed(typ, "net/http", "Client") { return true // method on http.Client. } ptr, ok := types.Unalias(typ).(*types.Pointer) - return ok && analysisutil.IsNamedType(ptr.Elem(), "net/http", "Client") // method on *http.Client. + return ok && analysisinternal.IsTypeNamed(ptr.Elem(), "net/http", "Client") // method on *http.Client. } // restOfBlock, given a traversal stack, finds the innermost containing diff --git a/go/analysis/passes/internal/analysisutil/util.go b/go/analysis/passes/internal/analysisutil/util.go index a4fa8d31c4e..d3df898d301 100644 --- a/go/analysis/passes/internal/analysisutil/util.go +++ b/go/analysis/passes/internal/analysisutil/util.go @@ -7,9 +7,7 @@ package analysisutil import ( - "bytes" "go/ast" - "go/printer" "go/token" "go/types" "os" @@ -18,13 +16,6 @@ import ( "golang.org/x/tools/internal/analysisinternal" ) -// Format returns a string representation of the expression. -func Format(fset *token.FileSet, x ast.Expr) string { - var b bytes.Buffer - printer.Fprint(&b, fset, x) - return b.String() -} - // HasSideEffects reports whether evaluation of e has side effects. func HasSideEffects(info *types.Info, e ast.Expr) bool { safe := true @@ -105,57 +96,4 @@ func LineStart(f *token.File, line int) token.Pos { } } -// Imports returns true if path is imported by pkg. -func Imports(pkg *types.Package, path string) bool { - for _, imp := range pkg.Imports() { - if imp.Path() == path { - return true - } - } - return false -} - -// IsNamedType reports whether t is the named type with the given package path -// and one of the given names. -// This function avoids allocating the concatenation of "pkg.Name", -// which is important for the performance of syntax matching. -func IsNamedType(t types.Type, pkgPath string, names ...string) bool { - n, ok := types.Unalias(t).(*types.Named) - if !ok { - return false - } - obj := n.Obj() - if obj == nil || obj.Pkg() == nil || obj.Pkg().Path() != pkgPath { - return false - } - name := obj.Name() - for _, n := range names { - if name == n { - return true - } - } - return false -} - -// IsFunctionNamed reports whether f is a top-level function defined in the -// given package and has one of the given names. -// It returns false if f is nil or a method. -func IsFunctionNamed(f *types.Func, pkgPath string, names ...string) bool { - if f == nil { - return false - } - if f.Pkg() == nil || f.Pkg().Path() != pkgPath { - return false - } - if f.Type().(*types.Signature).Recv() != nil { - return false - } - for _, n := range names { - if f.Name() == n { - return true - } - } - return false -} - var MustExtractDoc = analysisinternal.MustExtractDoc diff --git a/go/analysis/passes/loopclosure/loopclosure.go b/go/analysis/passes/loopclosure/loopclosure.go index fe05eda44e4..d3181242153 100644 --- a/go/analysis/passes/loopclosure/loopclosure.go +++ b/go/analysis/passes/loopclosure/loopclosure.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typesinternal" "golang.org/x/tools/internal/versions" ) @@ -368,5 +369,5 @@ func isMethodCall(info *types.Info, expr ast.Expr, pkgPath, typeName, method str // Check that the receiver is a . or // *.. _, named := typesinternal.ReceiverNamed(recv) - return analysisutil.IsNamedType(named, pkgPath, typeName) + return analysisinternal.IsTypeNamed(named, pkgPath, typeName) } diff --git a/go/analysis/passes/lostcancel/lostcancel.go b/go/analysis/passes/lostcancel/lostcancel.go index 26fdc1206f8..f8a661aa5db 100644 --- a/go/analysis/passes/lostcancel/lostcancel.go +++ b/go/analysis/passes/lostcancel/lostcancel.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/cfg" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -48,7 +49,7 @@ var contextPackage = "context" // checkLostCancel analyzes a single named or literal function. func run(pass *analysis.Pass) (interface{}, error) { // Fast path: bypass check if file doesn't use context.WithCancel. - if !analysisutil.Imports(pass.Pkg, contextPackage) { + if !analysisinternal.Imports(pass.Pkg, contextPackage) { return nil, nil } diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go index 171ad201372..95c4bbaa98a 100644 --- a/go/analysis/passes/printf/printf.go +++ b/go/analysis/passes/printf/printf.go @@ -24,6 +24,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typeparams" ) @@ -480,7 +481,7 @@ func isFormatter(typ types.Type) bool { sig := fn.Type().(*types.Signature) return sig.Params().Len() == 2 && sig.Results().Len() == 0 && - analysisutil.IsNamedType(sig.Params().At(0).Type(), "fmt", "State") && + analysisinternal.IsTypeNamed(sig.Params().At(0).Type(), "fmt", "State") && types.Identical(sig.Params().At(1).Type(), types.Typ[types.Rune]) } @@ -848,7 +849,7 @@ func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, state *formatState) (o if reason != "" { details = " (" + reason + ")" } - pass.ReportRangef(call, "%s format %s uses non-int %s%s as argument of *", state.name, state.format, analysisutil.Format(pass.Fset, arg), details) + pass.ReportRangef(call, "%s format %s uses non-int %s%s as argument of *", state.name, state.format, analysisinternal.Format(pass.Fset, arg), details) return false } } @@ -862,7 +863,7 @@ func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, state *formatState) (o } arg := call.Args[argNum] if isFunctionValue(pass, arg) && state.verb != 'p' && state.verb != 'T' { - pass.ReportRangef(call, "%s format %s arg %s is a func value, not called", state.name, state.format, analysisutil.Format(pass.Fset, arg)) + pass.ReportRangef(call, "%s format %s arg %s is a func value, not called", state.name, state.format, analysisinternal.Format(pass.Fset, arg)) return false } if reason, ok := matchArgType(pass, v.typ, arg); !ok { @@ -874,12 +875,12 @@ func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, state *formatState) (o if reason != "" { details = " (" + reason + ")" } - pass.ReportRangef(call, "%s format %s has arg %s of wrong type %s%s", state.name, state.format, analysisutil.Format(pass.Fset, arg), typeString, details) + pass.ReportRangef(call, "%s format %s has arg %s of wrong type %s%s", state.name, state.format, analysisinternal.Format(pass.Fset, arg), typeString, details) return false } if v.typ&argString != 0 && v.verb != 'T' && !bytes.Contains(state.flags, []byte{'#'}) { if methodName, ok := recursiveStringer(pass, arg); ok { - pass.ReportRangef(call, "%s format %s with arg %s causes recursive %s method call", state.name, state.format, analysisutil.Format(pass.Fset, arg), methodName) + pass.ReportRangef(call, "%s format %s with arg %s causes recursive %s method call", state.name, state.format, analysisinternal.Format(pass.Fset, arg), methodName) return false } } @@ -1032,7 +1033,7 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) { if sel, ok := call.Args[0].(*ast.SelectorExpr); ok { if x, ok := sel.X.(*ast.Ident); ok { if x.Name == "os" && strings.HasPrefix(sel.Sel.Name, "Std") { - pass.ReportRangef(call, "%s does not take io.Writer but has first arg %s", fn.FullName(), analysisutil.Format(pass.Fset, call.Args[0])) + pass.ReportRangef(call, "%s does not take io.Writer but has first arg %s", fn.FullName(), analysisinternal.Format(pass.Fset, call.Args[0])) } } } @@ -1061,10 +1062,10 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) { } for _, arg := range args { if isFunctionValue(pass, arg) { - pass.ReportRangef(call, "%s arg %s is a func value, not called", fn.FullName(), analysisutil.Format(pass.Fset, arg)) + pass.ReportRangef(call, "%s arg %s is a func value, not called", fn.FullName(), analysisinternal.Format(pass.Fset, arg)) } if methodName, ok := recursiveStringer(pass, arg); ok { - pass.ReportRangef(call, "%s arg %s causes recursive call to %s method", fn.FullName(), analysisutil.Format(pass.Fset, arg), methodName) + pass.ReportRangef(call, "%s arg %s causes recursive call to %s method", fn.FullName(), analysisinternal.Format(pass.Fset, arg), methodName) } } } diff --git a/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go b/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go index 6789d73579a..72435b2fc7a 100644 --- a/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go +++ b/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go @@ -8,13 +8,13 @@ import ( _ "embed" "go/ast" "go/token" - "go/types" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -49,8 +49,8 @@ func run(pass *analysis.Pass) (interface{}, error) { } } case *ast.CallExpr: - fn, _ := typeutil.Callee(pass.TypesInfo, n).(*types.Func) - if analysisutil.IsFunctionNamed(fn, "reflect", "DeepEqual") && (isReflectValue(pass, n.Args[0]) || isReflectValue(pass, n.Args[1])) { + obj := typeutil.Callee(pass.TypesInfo, n) + if analysisinternal.IsFunctionNamed(obj, "reflect", "DeepEqual") && (isReflectValue(pass, n.Args[0]) || isReflectValue(pass, n.Args[1])) { pass.ReportRangef(n, "avoid using reflect.DeepEqual with reflect.Value") } } @@ -65,7 +65,7 @@ func isReflectValue(pass *analysis.Pass, e ast.Expr) bool { return false } // See if the type is reflect.Value - if !analysisutil.IsNamedType(tv.Type, "reflect", "Value") { + if !analysisinternal.IsTypeNamed(tv.Type, "reflect", "Value") { return false } if _, ok := e.(*ast.CompositeLit); ok { diff --git a/go/analysis/passes/shift/shift.go b/go/analysis/passes/shift/shift.go index 759ed0043ff..46b5f6d68c6 100644 --- a/go/analysis/passes/shift/shift.go +++ b/go/analysis/passes/shift/shift.go @@ -19,8 +19,8 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typeparams" ) @@ -123,7 +123,7 @@ func checkLongShift(pass *analysis.Pass, node ast.Node, x, y ast.Expr) { } } if amt >= minSize { - ident := analysisutil.Format(pass.Fset, x) + ident := analysisinternal.Format(pass.Fset, x) qualifier := "" if len(sizes) > 1 { qualifier = "may be " diff --git a/go/analysis/passes/sigchanyzer/sigchanyzer.go b/go/analysis/passes/sigchanyzer/sigchanyzer.go index 5f121f720d8..78a2fa5ea3b 100644 --- a/go/analysis/passes/sigchanyzer/sigchanyzer.go +++ b/go/analysis/passes/sigchanyzer/sigchanyzer.go @@ -8,6 +8,8 @@ package sigchanyzer import ( "bytes" + "slices" + _ "embed" "go/ast" "go/format" @@ -18,6 +20,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -32,8 +35,8 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "os/signal") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "os/signal") { return nil, nil // doesn't directly import signal } @@ -69,7 +72,7 @@ func run(pass *analysis.Pass) (interface{}, error) { // mutating the AST. See https://golang.org/issue/46129. chanDeclCopy := &ast.CallExpr{} *chanDeclCopy = *chanDecl - chanDeclCopy.Args = append([]ast.Expr(nil), chanDecl.Args...) + chanDeclCopy.Args = slices.Clone(chanDecl.Args) chanDeclCopy.Args = append(chanDeclCopy.Args, &ast.BasicLit{ Kind: token.INT, Value: "1", diff --git a/go/analysis/passes/slog/slog.go b/go/analysis/passes/slog/slog.go index 0129102a336..c1ac960435d 100644 --- a/go/analysis/passes/slog/slog.go +++ b/go/analysis/passes/slog/slog.go @@ -20,6 +20,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/typesinternal" ) @@ -114,10 +115,10 @@ func run(pass *analysis.Pass) (any, error) { default: if unknownArg == nil { pass.ReportRangef(arg, "%s arg %q should be a string or a slog.Attr (possible missing key or value)", - shortName(fn), analysisutil.Format(pass.Fset, arg)) + shortName(fn), analysisinternal.Format(pass.Fset, arg)) } else { pass.ReportRangef(arg, "%s arg %q should probably be a string or a slog.Attr (previous arg %q cannot be a key)", - shortName(fn), analysisutil.Format(pass.Fset, arg), analysisutil.Format(pass.Fset, unknownArg)) + shortName(fn), analysisinternal.Format(pass.Fset, arg), analysisinternal.Format(pass.Fset, unknownArg)) } // Stop here so we report at most one missing key per call. return @@ -157,7 +158,7 @@ func run(pass *analysis.Pass) (any, error) { } func isAttr(t types.Type) bool { - return analysisutil.IsNamedType(t, "log/slog", "Attr") + return analysisinternal.IsTypeNamed(t, "log/slog", "Attr") } // shortName returns a name for the function that is shorter than FullName. diff --git a/go/analysis/passes/sortslice/analyzer.go b/go/analysis/passes/sortslice/analyzer.go index 6c151a02c16..9fe0d209289 100644 --- a/go/analysis/passes/sortslice/analyzer.go +++ b/go/analysis/passes/sortslice/analyzer.go @@ -15,9 +15,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) const Doc = `check the argument type of sort.Slice @@ -33,8 +33,8 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { - if !analysisutil.Imports(pass.Pkg, "sort") { +func run(pass *analysis.Pass) (any, error) { + if !analysisinternal.Imports(pass.Pkg, "sort") { return nil, nil // doesn't directly import sort } @@ -46,10 +46,11 @@ func run(pass *analysis.Pass) (interface{}, error) { inspect.Preorder(nodeFilter, func(n ast.Node) { call := n.(*ast.CallExpr) - fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func) - if !analysisutil.IsFunctionNamed(fn, "sort", "Slice", "SliceStable", "SliceIsSorted") { + obj := typeutil.Callee(pass.TypesInfo, call) + if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable", "SliceIsSorted") { return } + callee := obj.(*types.Func) arg := call.Args[0] typ := pass.TypesInfo.Types[arg].Type @@ -126,7 +127,7 @@ func run(pass *analysis.Pass) (interface{}, error) { pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), - Message: fmt.Sprintf("%s's argument must be a slice; is called with %s", fn.FullName(), typ.String()), + Message: fmt.Sprintf("%s's argument must be a slice; is called with %s", callee.FullName(), typ.String()), SuggestedFixes: fixes, }) }) diff --git a/go/analysis/passes/testinggoroutine/testinggoroutine.go b/go/analysis/passes/testinggoroutine/testinggoroutine.go index effcdc5700b..fef5a6014c4 100644 --- a/go/analysis/passes/testinggoroutine/testinggoroutine.go +++ b/go/analysis/passes/testinggoroutine/testinggoroutine.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -38,7 +39,7 @@ var Analyzer = &analysis.Analyzer{ func run(pass *analysis.Pass) (interface{}, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - if !analysisutil.Imports(pass.Pkg, "testing") { + if !analysisinternal.Imports(pass.Pkg, "testing") { return nil, nil } diff --git a/go/analysis/passes/testinggoroutine/util.go b/go/analysis/passes/testinggoroutine/util.go index 8c7a51ca525..027c99e6b0f 100644 --- a/go/analysis/passes/testinggoroutine/util.go +++ b/go/analysis/passes/testinggoroutine/util.go @@ -36,6 +36,8 @@ func localFunctionDecls(info *types.Info, files []*ast.File) func(*types.Func) * // isMethodNamed returns true if f is a method defined // in package with the path pkgPath with a name in names. +// +// (Unlike [analysisinternal.IsMethodNamed], it ignores the receiver type name.) func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool { if f == nil { return false diff --git a/go/analysis/passes/tests/tests.go b/go/analysis/passes/tests/tests.go index 36f2c43eb64..285b34218c3 100644 --- a/go/analysis/passes/tests/tests.go +++ b/go/analysis/passes/tests/tests.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -257,7 +258,7 @@ func isTestingType(typ types.Type, testingType string) bool { if !ok { return false } - return analysisutil.IsNamedType(ptr.Elem(), "testing", testingType) + return analysisinternal.IsTypeNamed(ptr.Elem(), "testing", testingType) } // Validate that fuzz target function's arguments are of accepted types. diff --git a/go/analysis/passes/timeformat/timeformat.go b/go/analysis/passes/timeformat/timeformat.go index 4a6c6b8bc6c..4fdbb2b5415 100644 --- a/go/analysis/passes/timeformat/timeformat.go +++ b/go/analysis/passes/timeformat/timeformat.go @@ -19,6 +19,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) const badFormat = "2006-02-01" @@ -35,7 +36,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // Note: (time.Time).Format is a method and can be a typeutil.Callee // without directly importing "time". So we cannot just skip this package // when !analysisutil.Imports(pass.Pkg, "time"). @@ -48,11 +49,9 @@ func run(pass *analysis.Pass) (interface{}, error) { } inspect.Preorder(nodeFilter, func(n ast.Node) { call := n.(*ast.CallExpr) - fn, ok := typeutil.Callee(pass.TypesInfo, call).(*types.Func) - if !ok { - return - } - if !isTimeDotFormat(fn) && !isTimeDotParse(fn) { + obj := typeutil.Callee(pass.TypesInfo, call) + if !analysisinternal.IsMethodNamed(obj, "time", "Time", "Format") && + !analysisinternal.IsFunctionNamed(obj, "time", "Parse") { return } if len(call.Args) > 0 { @@ -87,19 +86,6 @@ func run(pass *analysis.Pass) (interface{}, error) { return nil, nil } -func isTimeDotFormat(f *types.Func) bool { - if f.Name() != "Format" || f.Pkg() == nil || f.Pkg().Path() != "time" { - return false - } - // Verify that the receiver is time.Time. - recv := f.Type().(*types.Signature).Recv() - return recv != nil && analysisutil.IsNamedType(recv.Type(), "time", "Time") -} - -func isTimeDotParse(f *types.Func) bool { - return analysisutil.IsFunctionNamed(f, "time", "Parse") -} - // badFormatAt return the start of a bad format in e or -1 if no bad format is found. func badFormatAt(info *types.Info, e ast.Expr) int { tv, ok := info.Types[e] diff --git a/go/analysis/passes/unmarshal/unmarshal.go b/go/analysis/passes/unmarshal/unmarshal.go index a7889fa4590..26e894bd400 100644 --- a/go/analysis/passes/unmarshal/unmarshal.go +++ b/go/analysis/passes/unmarshal/unmarshal.go @@ -28,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { switch pass.Pkg.Path() { case "encoding/gob", "encoding/json", "encoding/xml", "encoding/asn1": // These packages know how to use their own APIs. diff --git a/go/analysis/passes/unsafeptr/unsafeptr.go b/go/analysis/passes/unsafeptr/unsafeptr.go index 272ae7fe045..fb5b944faad 100644 --- a/go/analysis/passes/unsafeptr/unsafeptr.go +++ b/go/analysis/passes/unsafeptr/unsafeptr.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -104,7 +105,7 @@ func isSafeUintptr(info *types.Info, x ast.Expr) bool { } switch sel.Sel.Name { case "Pointer", "UnsafeAddr": - if analysisutil.IsNamedType(info.Types[sel.X].Type, "reflect", "Value") { + if analysisinternal.IsTypeNamed(info.Types[sel.X].Type, "reflect", "Value") { return true } } @@ -152,5 +153,5 @@ func hasBasicType(info *types.Info, x ast.Expr, kind types.BasicKind) bool { // isReflectHeader reports whether t is reflect.SliceHeader or reflect.StringHeader. func isReflectHeader(t types.Type) bool { - return analysisutil.IsNamedType(t, "reflect", "SliceHeader", "StringHeader") + return analysisinternal.IsTypeNamed(t, "reflect", "SliceHeader", "StringHeader") } diff --git a/go/analysis/passes/waitgroup/waitgroup.go b/go/analysis/passes/waitgroup/waitgroup.go index cbb0bfc9e6b..14c6986eaba 100644 --- a/go/analysis/passes/waitgroup/waitgroup.go +++ b/go/analysis/passes/waitgroup/waitgroup.go @@ -9,7 +9,6 @@ package waitgroup import ( _ "embed" "go/ast" - "go/types" "reflect" "golang.org/x/tools/go/analysis" @@ -17,7 +16,7 @@ import ( "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/internal/typesinternal" + "golang.org/x/tools/internal/analysisinternal" ) //go:embed doc.go @@ -32,7 +31,7 @@ var Analyzer = &analysis.Analyzer{ } func run(pass *analysis.Pass) (any, error) { - if !analysisutil.Imports(pass.Pkg, "sync") { + if !analysisinternal.Imports(pass.Pkg, "sync") { return nil, nil // doesn't directly import sync } @@ -44,8 +43,8 @@ func run(pass *analysis.Pass) (any, error) { inspect.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) (proceed bool) { if push { call := n.(*ast.CallExpr) - if fn, ok := typeutil.Callee(pass.TypesInfo, call).(*types.Func); ok && - isMethodNamed(fn, "sync", "WaitGroup", "Add") && + obj := typeutil.Callee(pass.TypesInfo, call) + if analysisinternal.IsMethodNamed(obj, "sync", "WaitGroup", "Add") && hasSuffix(stack, wantSuffix) && backindex(stack, 1) == backindex(stack, 2).(*ast.BlockStmt).List[0] { // ExprStmt must be Block's first stmt @@ -86,19 +85,6 @@ func hasSuffix(stack, suffix []ast.Node) bool { return true } -// isMethodNamed reports whether f is a method with the specified -// package, receiver type, and method names. -func isMethodNamed(fn *types.Func, pkg, recv, name string) bool { - if fn.Pkg() != nil && fn.Pkg().Path() == pkg && fn.Name() == name { - if r := fn.Type().(*types.Signature).Recv(); r != nil { - if _, gotRecv := typesinternal.ReceiverNamed(r); gotRecv != nil { - return gotRecv.Obj().Name() == recv - } - } - } - return false -} - // backindex is like [slices.Index] but from the back of the slice. func backindex[T any](slice []T, i int) T { return slice[len(slice)-1-i] diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 18be946281e..582e19eed7e 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) @@ -27,7 +28,7 @@ import ( // for i := 0; i < b.N; i++ {} => for b.Loop() {} // for range b.N {} func bloop(pass *analysis.Pass) { - if !_imports(pass.Pkg, "testing") { + if !analysisinternal.Imports(pass.Pkg, "testing") { return } @@ -52,12 +53,8 @@ func bloop(pass *analysis.Pass) { return false // not preceding: stop } if call, ok := stmt.X.(*ast.CallExpr); ok { - fn := typeutil.StaticCallee(info, call) - if fn != nil && - (isMethod(fn, "testing", "B", "StopTimer") || - isMethod(fn, "testing", "B", "StartTimer") || - isMethod(fn, "testing", "B", "ResetTimer")) { - + obj := typeutil.Callee(info, call) + if analysisinternal.IsMethodNamed(obj, "testing", "B", "StopTimer", "StartTimer", "ResetTimer") { // Delete call statement. // TODO(adonovan): delete following newline, or // up to start of next stmt? (May delete a comment.) @@ -75,7 +72,7 @@ func bloop(pass *analysis.Pass) { return append(edits, analysis.TextEdit{ Pos: start, End: end, - NewText: fmt.Appendf(nil, "%s.Loop()", formatNode(pass.Fset, b)), + NewText: fmt.Appendf(nil, "%s.Loop()", analysisinternal.Format(pass.Fset, b)), }) } @@ -93,7 +90,7 @@ func bloop(pass *analysis.Pass) { if cmp, ok := n.Cond.(*ast.BinaryExpr); ok && cmp.Op == token.LSS { if sel, ok := cmp.Y.(*ast.SelectorExpr); ok && sel.Sel.Name == "N" && - isPtrToNamed(info.TypeOf(sel.X), "testing", "B") { + isTestingB(info.TypeOf(sel.X)) { delStart, delEnd := n.Cond.Pos(), n.Cond.End() @@ -136,7 +133,7 @@ func bloop(pass *analysis.Pass) { n.Key == nil && n.Value == nil && sel.Sel.Name == "N" && - isPtrToNamed(info.TypeOf(sel.X), "testing", "B") { + isTestingB(info.TypeOf(sel.X)) { pass.Report(analysis.Diagnostic{ // Highlight "range b.N". @@ -155,15 +152,8 @@ func bloop(pass *analysis.Pass) { } } -// isPtrToNamed reports whether t is type "*pkgpath.Name". -func isPtrToNamed(t types.Type, pkgpath, name string) bool { - if ptr, ok := t.(*types.Pointer); ok { - named, ok := ptr.Elem().(*types.Named) - return ok && - named.Obj().Name() == name && - named.Obj().Pkg().Path() == pkgpath - } - return false +func isTestingB(t types.Type) bool { + return analysisinternal.IsTypeNamed(typesinternal.Unpointer(t), "testing", "B") } // uses reports whether the subtree cur contains a use of obj. @@ -176,17 +166,6 @@ func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { return false } -// isMethod reports whether fn is pkgpath.(T).Name. -func isMethod(fn *types.Func, pkgpath, T, name string) bool { - if recv := fn.Signature().Recv(); recv != nil { - _, recvName := typesinternal.ReceiverNamed(recv) - return recvName != nil && - isPackageLevel(recvName.Obj(), pkgpath, T) && - fn.Name() == name - } - return false -} - // enclosingFunc returns the cursor for the innermost Func{Decl,Lit} // that encloses (or is) c, if any. // diff --git a/gopls/internal/analysis/modernize/fmtappendf.go b/gopls/internal/analysis/modernize/fmtappendf.go index dd1013e511a..8575827aa3e 100644 --- a/gopls/internal/analysis/modernize/fmtappendf.go +++ b/gopls/internal/analysis/modernize/fmtappendf.go @@ -7,10 +7,13 @@ package modernize import ( "go/ast" "go/types" + "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" ) // The fmtappend function replaces []byte(fmt.Sprintf(...)) by @@ -25,17 +28,20 @@ func fmtappendf(pass *analysis.Pass) { if tv.IsType() && types.Identical(tv.Type, byteSliceType) { call, ok := conv.Args[0].(*ast.CallExpr) if ok { - var appendText = "" - var id *ast.Ident - if id = isQualifiedIdent(info, call.Fun, "fmt", "Sprintf"); id != nil { - appendText = "Appendf" - } else if id = isQualifiedIdent(info, call.Fun, "fmt", "Sprint"); id != nil { - appendText = "Append" - } else if id = isQualifiedIdent(info, call.Fun, "fmt", "Sprintln"); id != nil { - appendText = "Appendln" - } else { + obj := typeutil.Callee(info, call) + if !analysisinternal.IsFunctionNamed(obj, "fmt", "Sprintf", "Sprintln", "Sprint") { continue } + + // Find "Sprint" identifier. + var id *ast.Ident + switch e := ast.Unparen(call.Fun).(type) { + case *ast.SelectorExpr: + id = e.Sel // "fmt.Sprint" + case *ast.Ident: + id = e // "Sprint" after `import . "fmt"` + } + pass.Report(analysis.Diagnostic{ Pos: conv.Pos(), End: conv.End(), @@ -57,7 +63,7 @@ func fmtappendf(pass *analysis.Pass) { { Pos: id.Pos(), End: id.End(), - NewText: []byte(appendText), // replace Sprint with Append + NewText: []byte(strings.Replace(obj.Name(), "Sprint", "Append", 1)), }, { Pos: call.Lparen + 1, diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 071d074533a..6e8eaf8a1e8 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -141,7 +141,7 @@ func mapsloop(pass *analysis.Pass) { newText = fmt.Appendf(nil, "%s.%s(%s)", mapsName, funcName, - formatNode(pass.Fset, x)) + analysisinternal.Format(pass.Fset, x)) } else { // Replace loop with call statement. start, end = rng.Pos(), rng.End() @@ -149,8 +149,8 @@ func mapsloop(pass *analysis.Pass) { newText = fmt.Appendf(nil, "%s.%s(%s, %s)", mapsName, funcName, - formatNode(pass.Fset, m), - formatNode(pass.Fset, x)) + analysisinternal.Format(pass.Fset, m), + analysisinternal.Format(pass.Fset, x)) } pass.Report(analysis.Diagnostic{ Pos: assign.Lhs[0].Pos(), @@ -197,8 +197,8 @@ func mapsloop(pass *analysis.Pass) { // iter.Seq[K, V] and returns K and V if so. func assignableToIterSeq2(t types.Type) (k, v types.Type, ok bool) { // The only named type assignable to iter.Seq2 is iter.Seq2. - if named, isNamed := t.(*types.Named); isNamed { - if !isPackageLevel(named.Obj(), "iter", "Seq2") { + if is[*types.Named](t) { + if !analysisinternal.IsTypeNamed(t, "iter", "Seq2") { return } t = t.Underlying() diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index 06330657876..e496f0dab0d 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -13,6 +13,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" ) @@ -85,10 +86,10 @@ func minmax(pass *analysis.Pass) { Pos: ifStmt.Pos(), End: ifStmt.End(), NewText: fmt.Appendf(nil, "%s = %s(%s, %s)", - formatNode(pass.Fset, lhs), + analysisinternal.Format(pass.Fset, lhs), sym, - formatNode(pass.Fset, a), - formatNode(pass.Fset, b)), + analysisinternal.Format(pass.Fset, a), + analysisinternal.Format(pass.Fset, b)), }}, }}, }) @@ -135,8 +136,8 @@ func minmax(pass *analysis.Pass) { End: ifStmt.End(), NewText: fmt.Appendf(nil, "%s(%s, %s)", sym, - formatNode(pass.Fset, a), - formatNode(pass.Fset, b)), + analysisinternal.Format(pass.Fset, a), + analysisinternal.Format(pass.Fset, b)), }}, }}, }) diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index a117afa994c..b925e013f78 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -5,7 +5,6 @@ package modernize import ( - "bytes" _ "embed" "go/ast" "go/format" @@ -79,29 +78,12 @@ func run(pass *analysis.Pass) (any, error) { // -- helpers -- -// TODO(adonovan): factor with analysisutil.Imports. -func _imports(pkg *types.Package, path string) bool { - for _, imp := range pkg.Imports() { - if imp.Path() == path { - return true - } - } - return false -} - // equalSyntax reports whether x and y are syntactically equal (ignoring comments). func equalSyntax(x, y ast.Expr) bool { sameName := func(x, y *ast.Ident) bool { return x.Name == y.Name } return astutil.Equal(x, y, sameName) } -// formatNode formats n. -func formatNode(fset *token.FileSet, n ast.Node) []byte { - var buf bytes.Buffer - format.Node(&buf, fset, n) // ignore errors - return buf.Bytes() -} - // formatExprs formats a comma-separated list of expressions. func formatExprs(fset *token.FileSet, exprs []ast.Expr) string { var buf strings.Builder @@ -120,15 +102,6 @@ func isZeroLiteral(e ast.Expr) bool { return ok && lit.Kind == token.INT && lit.Value == "0" } -// isPackageLevel reports whether obj is the package-level symbol pkg.Name. -func isPackageLevel(obj types.Object, pkgpath, name string) bool { - pkg := obj.Pkg() - return pkg != nil && - obj.Parent() == pkg.Scope() && - obj.Pkg().Path() == pkgpath && - obj.Name() == name -} - // filesUsing returns a cursor for each *ast.File in the inspector // that uses at least the specified version of Go (e.g. "go1.24"). func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) iter.Seq[cursor.Cursor] { diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index 695ade3f652..13892989977 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -15,6 +15,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" ) @@ -69,8 +70,8 @@ func appendclipped(pass *analysis.Pass) { // Special case for common but redundant clone of os.Environ(). // append(zerocap, os.Environ()...) -> os.Environ() if scall, ok := s.(*ast.CallExpr); ok { - if id := isQualifiedIdent(info, scall.Fun, "os", "Environ"); id != nil { - + obj := typeutil.Callee(info, scall) + if analysisinternal.IsFunctionNamed(obj, "os", "Environ") { pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), @@ -81,7 +82,7 @@ func appendclipped(pass *analysis.Pass) { TextEdits: []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), - NewText: formatNode(pass.Fset, s), + NewText: []byte(analysisinternal.Format(pass.Fset, s)), }}, }}, }) @@ -101,7 +102,7 @@ func appendclipped(pass *analysis.Pass) { TextEdits: append(importEdits, []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), - NewText: []byte(fmt.Sprintf("%s.Clone(%s)", slicesName, formatNode(pass.Fset, s))), + NewText: fmt.Appendf(nil, "%s.Clone(%s)", slicesName, analysisinternal.Format(pass.Fset, s)), }}...), }}, }) @@ -125,7 +126,7 @@ func appendclipped(pass *analysis.Pass) { TextEdits: append(importEdits, []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), - NewText: []byte(fmt.Sprintf("%s.Concat(%s)", slicesName, formatExprs(pass.Fset, sliceArgs))), + NewText: fmt.Appendf(nil, "%s.Concat(%s)", slicesName, formatExprs(pass.Fset, sliceArgs)), }}...), }}, }) @@ -200,7 +201,8 @@ func isClippedSlice(info *types.Info, e ast.Expr) (clipped, empty bool) { } // slices.Clip(x)? - if id := isQualifiedIdent(info, e.Fun, "slices", "Clip"); id != nil { + obj := typeutil.Callee(info, e) + if analysisinternal.IsFunctionNamed(obj, "slices", "Clip") { return true, false } diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 98e501875d2..7f590eefc32 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -9,10 +9,12 @@ import ( "go/ast" "go/token" "go/types" + "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" ) @@ -34,7 +36,7 @@ import ( // - sort.Sort(x) where x has a named slice type whose Less method is the natural order. // -> sort.Slice(x) func sortslice(pass *analysis.Pass) { - if !_imports(pass.Pkg, "sort") { + if !analysisinternal.Imports(pass.Pkg, "sort") { return } @@ -42,13 +44,11 @@ func sortslice(pass *analysis.Pass) { check := func(file *ast.File, call *ast.CallExpr) { // call to sort.Slice{,Stable}? - var stable string - if isQualifiedIdent(info, call.Fun, "sort", "Slice") != nil { - } else if isQualifiedIdent(info, call.Fun, "sort", "SliceStable") != nil { - stable = "Stable" - } else { + obj := typeutil.Callee(info, call) + if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable") { return } + stable := cond(strings.HasSuffix(obj.Name(), "Stable"), "Stable", "") if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 { sig := info.Types[lit.Type].Type.(*types.Signature) @@ -111,21 +111,3 @@ func sortslice(pass *analysis.Pass) { } } } - -// isQualifiedIdent reports whether e is a reference to pkg.Name. If so, it returns the identifier. -func isQualifiedIdent(info *types.Info, e ast.Expr, pkgpath, name string) *ast.Ident { - var id *ast.Ident - switch e := e.(type) { - case *ast.Ident: - id = e // e.g. dot import - case *ast.SelectorExpr: - id = e.Sel - default: - return nil - } - obj, ok := info.Uses[id] - if ok && isPackageLevel(obj, pkgpath, name) { - return id - } - return nil -} diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 58615232ff9..10fb580ceac 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -10,13 +10,17 @@ import ( "bytes" "fmt" "go/ast" + "go/printer" "go/scanner" "go/token" "go/types" "os" pathpkg "path" + "slices" + "strings" "golang.org/x/tools/go/analysis" + "golang.org/x/tools/internal/typesinternal" ) func TypeErrorEndPos(fset *token.FileSet, src []byte, start token.Pos) token.Pos { @@ -225,8 +229,8 @@ func AddImport(info *types.Info, file *ast.File, pos token.Pos, pkgpath, preferr // Is there an existing import of this package? // If so, are we in its scope? (not shadowed) for _, spec := range file.Imports { - pkgname, ok := importedPkgName(info, spec) - if ok && pkgname.Imported().Path() == pkgpath { + pkgname := info.PkgNameOf(spec) + if pkgname != nil && pkgname.Imported().Path() == pkgpath { if _, obj := scope.LookupParent(pkgname.Name(), pos); obj == pkgname { return pkgname.Name(), nil } @@ -273,15 +277,76 @@ func AddImport(info *types.Info, file *ast.File, pos token.Pos, pkgpath, preferr }} } -// importedPkgName returns the PkgName object declared by an ImportSpec. -// TODO(adonovan): use go1.22's Info.PkgNameOf. -func importedPkgName(info *types.Info, imp *ast.ImportSpec) (*types.PkgName, bool) { - var obj types.Object - if imp.Name != nil { - obj = info.Defs[imp.Name] - } else { - obj = info.Implicits[imp] +// Format returns a string representation of the expression e. +func Format(fset *token.FileSet, e ast.Expr) string { + var buf strings.Builder + printer.Fprint(&buf, fset, e) // ignore errors + return buf.String() +} + +// Imports returns true if path is imported by pkg. +func Imports(pkg *types.Package, path string) bool { + for _, imp := range pkg.Imports() { + if imp.Path() == path { + return true + } + } + return false +} + +// IsTypeNamed reports whether t is (or is an alias for) a +// package-level defined type with the given package path and one of +// the given names. It returns false if t is nil. +// +// This function avoids allocating the concatenation of "pkg.Name", +// which is important for the performance of syntax matching. +func IsTypeNamed(t types.Type, pkgPath string, names ...string) bool { + if named, ok := types.Unalias(t).(*types.Named); ok { + tname := named.Obj() + return tname != nil && + isPackageLevel(tname) && + tname.Pkg().Path() == pkgPath && + slices.Contains(names, tname.Name()) } - pkgname, ok := obj.(*types.PkgName) - return pkgname, ok + return false +} + +// IsFunctionNamed reports whether obj is a package-level function +// defined in the given package and has one of the given names. +// It returns false if obj is nil. +// +// This function avoids allocating the concatenation of "pkg.Name", +// which is important for the performance of syntax matching. +func IsFunctionNamed(obj types.Object, pkgPath string, names ...string) bool { + f, ok := obj.(*types.Func) + return ok && + isPackageLevel(obj) && + f.Pkg().Path() == pkgPath && + f.Type().(*types.Signature).Recv() == nil && + slices.Contains(names, f.Name()) +} + +// IsMethodNamed reports whether obj is a method defined on a +// package-level type with the given package and type name, and has +// one of the given names. It returns false if obj is nil. +// +// This function avoids allocating the concatenation of "pkg.TypeName.Name", +// which is important for the performance of syntax matching. +func IsMethodNamed(obj types.Object, pkgPath string, typeName string, names ...string) bool { + if fn, ok := obj.(*types.Func); ok { + if recv := fn.Type().(*types.Signature).Recv(); recv != nil { + _, T := typesinternal.ReceiverNamed(recv) + return IsTypeNamed(T, pkgPath, typeName) && + slices.Contains(names, fn.Name()) + } + } + return false +} + +// isPackageLevel reports whether obj is a package-level symbol. +// +// TODO(adonovan): publish in typesinternal and factor with +// gopls/internal/golang/rename_check.go, refactor/rename/util.go. +func isPackageLevel(obj types.Object) bool { + return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope() } From 7c7f3536f7765cdc58dc2a1fab59f1ad7e009f79 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 7 Jan 2024 16:28:53 -0500 Subject: [PATCH 005/309] gopls/internal/analysis/hostport: report net.Dial("%s:%d") addresses This change defines an analyzer that reports calls to net.Dial, net.DialTimeout, or net.Dialer.Dial with an address produced by a direct call to fmt.Sprintf, or via an intermediate local variable declared using the form: addr := fmt.Sprintf("%s:%d", host, port) ... net.Dial("tcp", addr) In other words, it uses the more precise approach suggested in dominikh/go-tools#358, not the blunter instrument of golang/go#28308. Formatting addresses this way doesn't work with IPv6. The diagnostic carries a fix to use net.JoinHostPort instead. The analyzer turns up a fairly small number of diagnostics across the corpus; however it is precise and cheap to run (since it requires a direct import of net). + test, relnote, doc We plan to add this to cmd/vet after go1.24 is released. Updates golang/go#28308 Updates dominikh/go-tools#358 Change-Id: I72e27253b75ed4702762a65c1b069e7920103bb7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/554495 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/analyzers.md | 25 +++ gopls/doc/release/v0.18.0.md | 9 + gopls/internal/analysis/hostport/hostport.go | 192 ++++++++++++++++++ .../analysis/hostport/hostport_test.go | 17 ++ gopls/internal/analysis/hostport/main.go | 14 ++ .../analysis/hostport/testdata/src/a/a.go | 40 ++++ .../hostport/testdata/src/a/a.go.golden | 40 ++++ gopls/internal/doc/api.json | 11 + gopls/internal/settings/analysis.go | 2 + 9 files changed, 350 insertions(+) create mode 100644 gopls/internal/analysis/hostport/hostport.go create mode 100644 gopls/internal/analysis/hostport/hostport_test.go create mode 100644 gopls/internal/analysis/hostport/main.go create mode 100644 gopls/internal/analysis/hostport/testdata/src/a/a.go create mode 100644 gopls/internal/analysis/hostport/testdata/src/a/a.go.golden diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 2905a0e5336..acc95d29dc4 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -290,6 +290,31 @@ Default: on. Package documentation: [framepointer](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer) + +## `hostport`: check format of addresses passed to net.Dial + + +This analyzer flags code that produce network address strings using +fmt.Sprintf, as in this example: + + addr := fmt.Sprintf("%s:%d", host, 12345) // "will not work with IPv6" + ... + conn, err := net.Dial("tcp", addr) // "when passed to dial here" + +The analyzer suggests a fix to use the correct approach, a call to +net.JoinHostPort: + + addr := net.JoinHostPort(host, "12345") + ... + conn, err := net.Dial("tcp", addr) + +A similar diagnostic and fix are produced for a format string of "%s:%s". + + +Default: on. + +Package documentation: [hostport](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/hostport) + ## `httpresponse`: check for mistakes using HTTP responses diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 9f7ddd0909b..769ca69f2ea 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -40,6 +40,15 @@ functions and methods are candidates. (For a more precise analysis that may report unused exported functions too, use the `golang.org/x/tools/cmd/deadcode` command.) +## New `hostport` analyzer + +With the growing use of IPv6, forming a "host:port" string using +`fmt.Sprintf("%s:%d")` is no longer appropriate because host names may +contain colons. Gopls now reports places where a string constructed in +this fashion (or with `%s` for the port) is passed to `net.Dial` or a +related function, and offers a fix to use `net.JoinHostPort` +instead. + ## "Implementations" supports generics At long last, the "Go to Implementations" feature now fully supports diff --git a/gopls/internal/analysis/hostport/hostport.go b/gopls/internal/analysis/hostport/hostport.go new file mode 100644 index 00000000000..bf3b761b840 --- /dev/null +++ b/gopls/internal/analysis/hostport/hostport.go @@ -0,0 +1,192 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package hostport defines an analyzer for calls to net.Dial with +// addresses of the form "%s:%d" or "%s:%s", which work only with IPv4. +package hostport + +import ( + "fmt" + "go/ast" + "go/constant" + "go/types" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" +) + +const Doc = `check format of addresses passed to net.Dial + +This analyzer flags code that produce network address strings using +fmt.Sprintf, as in this example: + + addr := fmt.Sprintf("%s:%d", host, 12345) // "will not work with IPv6" + ... + conn, err := net.Dial("tcp", addr) // "when passed to dial here" + +The analyzer suggests a fix to use the correct approach, a call to +net.JoinHostPort: + + addr := net.JoinHostPort(host, "12345") + ... + conn, err := net.Dial("tcp", addr) + +A similar diagnostic and fix are produced for a format string of "%s:%s". +` + +var Analyzer = &analysis.Analyzer{ + Name: "hostport", + Doc: Doc, + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/hostport", + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Run: run, +} + +func run(pass *analysis.Pass) (any, error) { + // Fast path: if the package doesn't import net and fmt, skip + // the traversal. + if !analysisinternal.Imports(pass.Pkg, "net") || + !analysisinternal.Imports(pass.Pkg, "fmt") { + return nil, nil + } + + info := pass.TypesInfo + + // checkAddr reports a diagnostic (and returns true) if e + // is a call of the form fmt.Sprintf("%d:%d", ...). + // The diagnostic includes a fix. + // + // dialCall is non-nil if the Dial call is non-local + // but within the same file. + checkAddr := func(e ast.Expr, dialCall *ast.CallExpr) { + if call, ok := e.(*ast.CallExpr); ok { + obj := typeutil.Callee(info, call) + if analysisinternal.IsFunctionNamed(obj, "fmt", "Sprintf") { + // Examine format string. + formatArg := call.Args[0] + if tv := info.Types[formatArg]; tv.Value != nil { + numericPort := false + format := constant.StringVal(tv.Value) + switch format { + case "%s:%d": + // Have: fmt.Sprintf("%s:%d", host, port) + numericPort = true + + case "%s:%s": + // Have: fmt.Sprintf("%s:%s", host, portStr) + // Keep port string as is. + + default: + return + } + + // Use granular edits to preserve original formatting. + edits := []analysis.TextEdit{ + { + // Replace fmt.Sprintf with net.JoinHostPort. + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: []byte("net.JoinHostPort"), + }, + { + // Delete format string. + Pos: formatArg.Pos(), + End: call.Args[1].Pos(), + }, + } + + // Turn numeric port into a string. + if numericPort { + // port => fmt.Sprintf("%d", port) + // 123 => "123" + port := call.Args[2] + newPort := fmt.Sprintf(`fmt.Sprintf("%%d", %s)`, port) + if port := info.Types[port].Value; port != nil { + if i, ok := constant.Int64Val(port); ok { + newPort = fmt.Sprintf(`"%d"`, i) // numeric constant + } + } + + edits = append(edits, analysis.TextEdit{ + Pos: port.Pos(), + End: port.End(), + NewText: []byte(newPort), + }) + } + + // Refer to Dial call, if not adjacent. + suffix := "" + if dialCall != nil { + suffix = fmt.Sprintf(" (passed to net.Dial at L%d)", + safetoken.StartPosition(pass.Fset, dialCall.Pos()).Line) + } + + pass.Report(analysis.Diagnostic{ + // Highlight the format string. + Pos: formatArg.Pos(), + End: formatArg.End(), + Message: fmt.Sprintf("address format %q does not work with IPv6%s", format, suffix), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Replace fmt.Sprintf with net.JoinHostPort", + TextEdits: edits, + }}, + }) + } + } + } + } + + // Check address argument of each call to net.Dial et al. + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curCall := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil)) { + call := curCall.Node().(*ast.CallExpr) + + obj := typeutil.Callee(info, call) + if analysisinternal.IsFunctionNamed(obj, "net", "Dial", "DialTimeout") || + analysisinternal.IsMethodNamed(obj, "net", "Dialer", "Dial") { + + switch address := call.Args[1].(type) { + case *ast.CallExpr: + // net.Dial("tcp", fmt.Sprintf("%s:%d", ...)) + checkAddr(address, nil) + + case *ast.Ident: + // addr := fmt.Sprintf("%s:%d", ...) + // ... + // net.Dial("tcp", addr) + + // Search for decl of addrVar within common ancestor of addrVar and Dial call. + if addrVar, ok := info.Uses[address].(*types.Var); ok { + pos := addrVar.Pos() + // TODO(adonovan): use Cursor.Ancestors iterator when available. + for _, curAncestor := range curCall.Stack(nil) { + if curIdent, ok := curAncestor.FindPos(pos, pos); ok { + // curIdent is the declaring ast.Ident of addr. + switch parent := curIdent.Parent().Node().(type) { + case *ast.AssignStmt: + if len(parent.Rhs) == 1 { + // Have: addr := fmt.Sprintf("%s:%d", ...) + checkAddr(parent.Rhs[0], call) + } + + case *ast.ValueSpec: + if len(parent.Values) == 1 { + // Have: var addr = fmt.Sprintf("%s:%d", ...) + checkAddr(parent.Values[0], call) + } + } + break + } + } + } + } + } + } + return nil, nil +} diff --git a/gopls/internal/analysis/hostport/hostport_test.go b/gopls/internal/analysis/hostport/hostport_test.go new file mode 100644 index 00000000000..4e57a43e8d4 --- /dev/null +++ b/gopls/internal/analysis/hostport/hostport_test.go @@ -0,0 +1,17 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package hostport_test + +import ( + "testing" + + "golang.org/x/tools/go/analysis/analysistest" + "golang.org/x/tools/gopls/internal/analysis/hostport" +) + +func Test(t *testing.T) { + testdata := analysistest.TestData() + analysistest.RunWithSuggestedFixes(t, testdata, hostport.Analyzer, "a") +} diff --git a/gopls/internal/analysis/hostport/main.go b/gopls/internal/analysis/hostport/main.go new file mode 100644 index 00000000000..99f7a09ec39 --- /dev/null +++ b/gopls/internal/analysis/hostport/main.go @@ -0,0 +1,14 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build ignore + +package main + +import ( + "golang.org/x/tools/go/analysis/singlechecker" + "golang.org/x/tools/gopls/internal/analysis/hostport" +) + +func main() { singlechecker.Main(hostport.Analyzer) } diff --git a/gopls/internal/analysis/hostport/testdata/src/a/a.go b/gopls/internal/analysis/hostport/testdata/src/a/a.go new file mode 100644 index 00000000000..7d80f80f734 --- /dev/null +++ b/gopls/internal/analysis/hostport/testdata/src/a/a.go @@ -0,0 +1,40 @@ +package a + +import ( + "fmt" + "net" +) + +func direct(host string, port int, portStr string) { + // Dial, directly called with result of Sprintf. + net.Dial("tcp", fmt.Sprintf("%s:%d", host, port)) // want `address format "%s:%d" does not work with IPv6` + + net.Dial("tcp", fmt.Sprintf("%s:%s", host, portStr)) // want `address format "%s:%s" does not work with IPv6` +} + +// port is a constant: +var addr4 = fmt.Sprintf("%s:%d", "localhost", 123) // want `address format "%s:%d" does not work with IPv6 \(passed to net.Dial at L39\)` + +func indirect(host string, port int) { + // Dial, addr is immediately preceding. + { + addr1 := fmt.Sprintf("%s:%d", host, port) // want `address format "%s:%d" does not work with IPv6.*at L22` + net.Dial("tcp", addr1) + } + + // DialTimeout, addr is in ancestor block. + addr2 := fmt.Sprintf("%s:%d", host, port) // want `address format "%s:%d" does not work with IPv6.*at L28` + { + net.DialTimeout("tcp", addr2, 0) + } + + // Dialer.Dial, addr is declared with var. + var dialer net.Dialer + { + var addr3 = fmt.Sprintf("%s:%d", host, port) // want `address format "%s:%d" does not work with IPv6.*at L35` + dialer.Dial("tcp", addr3) + } + + // Dialer.Dial again, addr is declared at package level. + dialer.Dial("tcp", addr4) +} diff --git a/gopls/internal/analysis/hostport/testdata/src/a/a.go.golden b/gopls/internal/analysis/hostport/testdata/src/a/a.go.golden new file mode 100644 index 00000000000..b219224e0aa --- /dev/null +++ b/gopls/internal/analysis/hostport/testdata/src/a/a.go.golden @@ -0,0 +1,40 @@ +package a + +import ( + "fmt" + "net" +) + +func direct(host string, port int, portStr string) { + // Dial, directly called with result of Sprintf. + net.Dial("tcp", net.JoinHostPort(host, fmt.Sprintf("%d", port))) // want `address format "%s:%d" does not work with IPv6` + + net.Dial("tcp", net.JoinHostPort(host, portStr)) // want `address format "%s:%s" does not work with IPv6` +} + +// port is a constant: +var addr4 = net.JoinHostPort("localhost", "123") // want `address format "%s:%d" does not work with IPv6 \(passed to net.Dial at L39\)` + +func indirect(host string, port int) { + // Dial, addr is immediately preceding. + { + addr1 := net.JoinHostPort(host, fmt.Sprintf("%d", port)) // want `address format "%s:%d" does not work with IPv6.*at L22` + net.Dial("tcp", addr1) + } + + // DialTimeout, addr is in ancestor block. + addr2 := net.JoinHostPort(host, fmt.Sprintf("%d", port)) // want `address format "%s:%d" does not work with IPv6.*at L28` + { + net.DialTimeout("tcp", addr2, 0) + } + + // Dialer.Dial, addr is declared with var. + var dialer net.Dialer + { + var addr3 = net.JoinHostPort(host, fmt.Sprintf("%d", port)) // want `address format "%s:%d" does not work with IPv6.*at L35` + dialer.Dial("tcp", addr3) + } + + // Dialer.Dial again, addr is declared at package level. + dialer.Dial("tcp", addr4) +} diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 982ec34909b..b6fcc8f5b19 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -440,6 +440,11 @@ "Doc": "report assembly that clobbers the frame pointer before saving it", "Default": "true" }, + { + "Name": "\"hostport\"", + "Doc": "check format of addresses passed to net.Dial\n\nThis analyzer flags code that produce network address strings using\nfmt.Sprintf, as in this example:\n\n addr := fmt.Sprintf(\"%s:%d\", host, 12345) // \"will not work with IPv6\"\n ...\n conn, err := net.Dial(\"tcp\", addr) // \"when passed to dial here\"\n\nThe analyzer suggests a fix to use the correct approach, a call to\nnet.JoinHostPort:\n\n addr := net.JoinHostPort(host, \"12345\")\n ...\n conn, err := net.Dial(\"tcp\", addr)\n\nA similar diagnostic and fix are produced for a format string of \"%s:%s\".\n", + "Default": "true" + }, { "Name": "\"httpresponse\"", "Doc": "check for mistakes using HTTP responses\n\nA common mistake when using the net/http package is to defer a function\ncall to close the http.Response Body before checking the error that\ndetermines whether the response is valid:\n\n\tresp, err := http.Head(url)\n\tdefer resp.Body.Close()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// (defer statement belongs here)\n\nThis checker helps uncover latent nil dereference bugs by reporting a\ndiagnostic for such mistakes.", @@ -1060,6 +1065,12 @@ "URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer", "Default": true }, + { + "Name": "hostport", + "Doc": "check format of addresses passed to net.Dial\n\nThis analyzer flags code that produce network address strings using\nfmt.Sprintf, as in this example:\n\n addr := fmt.Sprintf(\"%s:%d\", host, 12345) // \"will not work with IPv6\"\n ...\n conn, err := net.Dial(\"tcp\", addr) // \"when passed to dial here\"\n\nThe analyzer suggests a fix to use the correct approach, a call to\nnet.JoinHostPort:\n\n addr := net.JoinHostPort(host, \"12345\")\n ...\n conn, err := net.Dial(\"tcp\", addr)\n\nA similar diagnostic and fix are produced for a format string of \"%s:%s\".\n", + "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/hostport", + "Default": true + }, { "Name": "httpresponse", "Doc": "check for mistakes using HTTP responses\n\nA common mistake when using the net/http package is to defer a function\ncall to close the http.Response Body before checking the error that\ndetermines whether the response is valid:\n\n\tresp, err := http.Head(url)\n\tdefer resp.Body.Close()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// (defer statement belongs here)\n\nThis checker helps uncover latent nil dereference bugs by reporting a\ndiagnostic for such mistakes.", diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 7e13c801a85..9663c2289d6 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -49,6 +49,7 @@ import ( "golang.org/x/tools/gopls/internal/analysis/deprecated" "golang.org/x/tools/gopls/internal/analysis/embeddirective" "golang.org/x/tools/gopls/internal/analysis/fillreturns" + "golang.org/x/tools/gopls/internal/analysis/hostport" "golang.org/x/tools/gopls/internal/analysis/infertypeargs" "golang.org/x/tools/gopls/internal/analysis/modernize" "golang.org/x/tools/gopls/internal/analysis/nonewvars" @@ -158,6 +159,7 @@ func init() { {analyzer: sortslice.Analyzer, enabled: true}, {analyzer: embeddirective.Analyzer, enabled: true}, {analyzer: waitgroup.Analyzer, enabled: true}, // to appear in cmd/vet@go1.25 + {analyzer: hostport.Analyzer, enabled: true}, // to appear in cmd/vet@go1.25 {analyzer: modernize.Analyzer, enabled: true, severity: protocol.SeverityInformation}, // disabled due to high false positives From 2ad5c902e0ffc3c06c19d1ead005489493bf5c4c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 3 Jan 2025 16:56:34 -0500 Subject: [PATCH 006/309] gopls/internal/settings: set severity=Info for modernizers "Simplifiers and modernizers" make suggestions about perfectly correct code, so we downgrade their severity accordingly. Also, flip sense of enabled field to reduce boilerplate. Change-Id: If46040777f1840a505a814277e1e2b8f3340fccc Reviewed-on: https://go-review.googlesource.com/c/tools/+/640039 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/settings/analysis.go | 139 ++++++++++++++----------- gopls/internal/settings/staticcheck.go | 6 +- 2 files changed, 80 insertions(+), 65 deletions(-) diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 9663c2289d6..9204e54458b 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -69,7 +69,7 @@ import ( // Analyzers are immutable, since they are shared across multiple LSP sessions. type Analyzer struct { analyzer *analysis.Analyzer - enabled bool + nonDefault bool actionKinds []protocol.CodeActionKind severity protocol.DiagnosticSeverity tags []protocol.DiagnosticTag @@ -80,7 +80,7 @@ func (a *Analyzer) Analyzer() *analysis.Analyzer { return a.analyzer } // EnabledByDefault reports whether the analyzer is enabled by default for all sessions. // This value can be configured per-analysis in user settings. -func (a *Analyzer) EnabledByDefault() bool { return a.enabled } +func (a *Analyzer) EnabledByDefault() bool { return !a.nonDefault } // ActionKinds is the set of kinds of code action this analyzer produces. // @@ -109,86 +109,101 @@ func (a *Analyzer) String() string { return a.analyzer.String() } var DefaultAnalyzers = make(map[string]*Analyzer) // initialized below func init() { - // The traditional vet suite: analyzers := []*Analyzer{ // The traditional vet suite: - {analyzer: appends.Analyzer, enabled: true}, - {analyzer: asmdecl.Analyzer, enabled: true}, - {analyzer: assign.Analyzer, enabled: true}, - {analyzer: atomic.Analyzer, enabled: true}, - {analyzer: bools.Analyzer, enabled: true}, - {analyzer: buildtag.Analyzer, enabled: true}, - {analyzer: cgocall.Analyzer, enabled: true}, - {analyzer: composite.Analyzer, enabled: true}, - {analyzer: copylock.Analyzer, enabled: true}, - {analyzer: defers.Analyzer, enabled: true}, - {analyzer: deprecated.Analyzer, enabled: true, severity: protocol.SeverityHint, tags: []protocol.DiagnosticTag{protocol.Deprecated}}, - {analyzer: directive.Analyzer, enabled: true}, - {analyzer: errorsas.Analyzer, enabled: true}, - {analyzer: framepointer.Analyzer, enabled: true}, - {analyzer: httpresponse.Analyzer, enabled: true}, - {analyzer: ifaceassert.Analyzer, enabled: true}, - {analyzer: loopclosure.Analyzer, enabled: true}, - {analyzer: lostcancel.Analyzer, enabled: true}, - {analyzer: nilfunc.Analyzer, enabled: true}, - {analyzer: printf.Analyzer, enabled: true}, - {analyzer: shift.Analyzer, enabled: true}, - {analyzer: sigchanyzer.Analyzer, enabled: true}, - {analyzer: slog.Analyzer, enabled: true}, - {analyzer: stdmethods.Analyzer, enabled: true}, - {analyzer: stdversion.Analyzer, enabled: true}, - {analyzer: stringintconv.Analyzer, enabled: true}, - {analyzer: structtag.Analyzer, enabled: true}, - {analyzer: testinggoroutine.Analyzer, enabled: true}, - {analyzer: tests.Analyzer, enabled: true}, - {analyzer: timeformat.Analyzer, enabled: true}, - {analyzer: unmarshal.Analyzer, enabled: true}, - {analyzer: unreachable.Analyzer, enabled: true}, - {analyzer: unsafeptr.Analyzer, enabled: true}, - {analyzer: unusedresult.Analyzer, enabled: true}, + {analyzer: appends.Analyzer}, + {analyzer: asmdecl.Analyzer}, + {analyzer: assign.Analyzer}, + {analyzer: atomic.Analyzer}, + {analyzer: bools.Analyzer}, + {analyzer: buildtag.Analyzer}, + {analyzer: cgocall.Analyzer}, + {analyzer: composite.Analyzer}, + {analyzer: copylock.Analyzer}, + {analyzer: defers.Analyzer}, + {analyzer: deprecated.Analyzer, severity: protocol.SeverityHint, tags: []protocol.DiagnosticTag{protocol.Deprecated}}, + {analyzer: directive.Analyzer}, + {analyzer: errorsas.Analyzer}, + {analyzer: framepointer.Analyzer}, + {analyzer: httpresponse.Analyzer}, + {analyzer: ifaceassert.Analyzer}, + {analyzer: loopclosure.Analyzer}, + {analyzer: lostcancel.Analyzer}, + {analyzer: nilfunc.Analyzer}, + {analyzer: printf.Analyzer}, + {analyzer: shift.Analyzer}, + {analyzer: sigchanyzer.Analyzer}, + {analyzer: slog.Analyzer}, + {analyzer: stdmethods.Analyzer}, + {analyzer: stdversion.Analyzer}, + {analyzer: stringintconv.Analyzer}, + {analyzer: structtag.Analyzer}, + {analyzer: testinggoroutine.Analyzer}, + {analyzer: tests.Analyzer}, + {analyzer: timeformat.Analyzer}, + {analyzer: unmarshal.Analyzer}, + {analyzer: unreachable.Analyzer}, + {analyzer: unsafeptr.Analyzer}, + {analyzer: unusedresult.Analyzer}, // not suitable for vet: // - some (nilness, yield) use go/ssa; see #59714. // - others don't meet the "frequency" criterion; // see GOROOT/src/cmd/vet/README. - // - some (modernize) report diagnostics on perfectly valid code (hence severity=info) - {analyzer: atomicalign.Analyzer, enabled: true}, - {analyzer: deepequalerrors.Analyzer, enabled: true}, - {analyzer: nilness.Analyzer, enabled: true}, // uses go/ssa - {analyzer: yield.Analyzer, enabled: true}, // uses go/ssa - {analyzer: sortslice.Analyzer, enabled: true}, - {analyzer: embeddirective.Analyzer, enabled: true}, - {analyzer: waitgroup.Analyzer, enabled: true}, // to appear in cmd/vet@go1.25 - {analyzer: hostport.Analyzer, enabled: true}, // to appear in cmd/vet@go1.25 - {analyzer: modernize.Analyzer, enabled: true, severity: protocol.SeverityInformation}, + {analyzer: atomicalign.Analyzer}, + {analyzer: deepequalerrors.Analyzer}, + {analyzer: nilness.Analyzer}, // uses go/ssa + {analyzer: yield.Analyzer}, // uses go/ssa + {analyzer: sortslice.Analyzer}, + {analyzer: embeddirective.Analyzer}, + {analyzer: waitgroup.Analyzer}, // to appear in cmd/vet@go1.25 + {analyzer: hostport.Analyzer}, // to appear in cmd/vet@go1.25 // disabled due to high false positives - {analyzer: shadow.Analyzer, enabled: false}, // very noisy + {analyzer: shadow.Analyzer, nonDefault: true}, // very noisy // fieldalignment is not even off-by-default; see #67762. - // "simplifiers": analyzers that offer mere style fixes - // gofmt -s suite: - {analyzer: simplifycompositelit.Analyzer, enabled: true, actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}}, - {analyzer: simplifyrange.Analyzer, enabled: true, actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}}, - {analyzer: simplifyslice.Analyzer, enabled: true, actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}}, - // other simplifiers: - {analyzer: infertypeargs.Analyzer, enabled: true, severity: protocol.SeverityHint}, - {analyzer: unusedparams.Analyzer, enabled: true}, - {analyzer: unusedfunc.Analyzer, enabled: true}, - {analyzer: unusedwrite.Analyzer, enabled: true}, // uses go/ssa + // simplifiers and modernizers + // + // These analyzers offer mere style fixes on correct code, + // thus they will never appear in cmd/vet and + // their severity level is "information". + // + // gofmt -s suite + { + analyzer: simplifycompositelit.Analyzer, + actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}, + severity: protocol.SeverityInformation, + }, + { + analyzer: simplifyrange.Analyzer, + actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}, + severity: protocol.SeverityInformation, + }, + { + analyzer: simplifyslice.Analyzer, + actionKinds: []protocol.CodeActionKind{protocol.SourceFixAll, protocol.QuickFix}, + severity: protocol.SeverityInformation, + }, + // other simplifiers + {analyzer: infertypeargs.Analyzer, severity: protocol.SeverityInformation}, + {analyzer: unusedparams.Analyzer, severity: protocol.SeverityInformation}, + {analyzer: unusedfunc.Analyzer, severity: protocol.SeverityInformation}, + {analyzer: unusedwrite.Analyzer, severity: protocol.SeverityInformation}, // uses go/ssa + {analyzer: modernize.Analyzer, severity: protocol.SeverityInformation}, // type-error analyzers // These analyzers enrich go/types errors with suggested fixes. - {analyzer: fillreturns.Analyzer, enabled: true}, - {analyzer: nonewvars.Analyzer, enabled: true}, - {analyzer: noresultvalues.Analyzer, enabled: true}, + {analyzer: fillreturns.Analyzer}, + {analyzer: nonewvars.Analyzer}, + {analyzer: noresultvalues.Analyzer}, // TODO(rfindley): why isn't the 'unusedvariable' analyzer enabled, if it // is only enhancing type errors with suggested fixes? // // In particular, enabling this analyzer could cause unused variables to be // greyed out, (due to the 'deletions only' fix). That seems like a nice UI // feature. - {analyzer: unusedvariable.Analyzer, enabled: false}, + {analyzer: unusedvariable.Analyzer, nonDefault: true}, } for _, analyzer := range analyzers { DefaultAnalyzers[analyzer.analyzer.Name] = analyzer diff --git a/gopls/internal/settings/staticcheck.go b/gopls/internal/settings/staticcheck.go index fca3e55f17e..6e06e0b44ea 100644 --- a/gopls/internal/settings/staticcheck.go +++ b/gopls/internal/settings/staticcheck.go @@ -43,9 +43,9 @@ func init() { } StaticcheckAnalyzers[a.Analyzer.Name] = &Analyzer{ - analyzer: a.Analyzer, - enabled: !a.Doc.NonDefault, - severity: mapSeverity(a.Doc.Severity), + analyzer: a.Analyzer, + nonDefault: a.Doc.NonDefault, + severity: mapSeverity(a.Doc.Severity), } } } From ac8980cd5c168ef25a9a52d0ed0a566a047d75de Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Tue, 7 Jan 2025 10:05:57 -0500 Subject: [PATCH 007/309] gopls/internal/protocol: modernize to use any Replace 'interface{}' by 'any' almost everywhere. And there's one slices.Clone() too. Change-Id: I156e3e026cd7f46aff35f5dfe2adee204a40c03a Reviewed-on: https://go-review.googlesource.com/c/tools/+/641055 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/protocol/command/command_gen.go | 2 +- gopls/internal/protocol/command/gen/gen.go | 2 +- gopls/internal/protocol/command/util.go | 6 +- gopls/internal/protocol/generate/generate.go | 2 +- gopls/internal/protocol/generate/main_test.go | 4 +- gopls/internal/protocol/generate/output.go | 9 +- gopls/internal/protocol/generate/tables.go | 32 +-- gopls/internal/protocol/mapper_test.go | 6 +- gopls/internal/protocol/protocol.go | 28 +-- gopls/internal/protocol/tsclient.go | 6 +- gopls/internal/protocol/tsprotocol.go | 190 +++++++++--------- gopls/internal/protocol/tsserver.go | 18 +- 12 files changed, 153 insertions(+), 152 deletions(-) diff --git a/gopls/internal/protocol/command/command_gen.go b/gopls/internal/protocol/command/command_gen.go index 28a7f44e88f..a5527064ef9 100644 --- a/gopls/internal/protocol/command/command_gen.go +++ b/gopls/internal/protocol/command/command_gen.go @@ -113,7 +113,7 @@ var Commands = []Command{ WorkspaceStats, } -func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Interface) (interface{}, error) { +func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Interface) (any, error) { switch Command(params.Command) { case AddDependency: var a0 DependencyArgs diff --git a/gopls/internal/protocol/command/gen/gen.go b/gopls/internal/protocol/command/gen/gen.go index 98155282499..d4935020b38 100644 --- a/gopls/internal/protocol/command/gen/gen.go +++ b/gopls/internal/protocol/command/gen/gen.go @@ -53,7 +53,7 @@ var Commands = []Command { {{- end}} } -func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Interface) (interface{}, error) { +func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Interface) (any, error) { switch Command(params.Command) { {{- range .Commands}} case {{.MethodName}}: diff --git a/gopls/internal/protocol/command/util.go b/gopls/internal/protocol/command/util.go index d07cd863f1c..3753b1e8eb1 100644 --- a/gopls/internal/protocol/command/util.go +++ b/gopls/internal/protocol/command/util.go @@ -21,7 +21,7 @@ func (c Command) String() string { return string(c) } // Example usage: // // jsonArgs, err := MarshalArgs(1, "hello", true, StructuredArg{42, 12.6}) -func MarshalArgs(args ...interface{}) ([]json.RawMessage, error) { +func MarshalArgs(args ...any) ([]json.RawMessage, error) { var out []json.RawMessage for _, arg := range args { argJSON, err := json.Marshal(arg) @@ -34,7 +34,7 @@ func MarshalArgs(args ...interface{}) ([]json.RawMessage, error) { } // MustMarshalArgs is like MarshalArgs, but panics on error. -func MustMarshalArgs(args ...interface{}) []json.RawMessage { +func MustMarshalArgs(args ...any) []json.RawMessage { msg, err := MarshalArgs(args...) if err != nil { panic(err) @@ -54,7 +54,7 @@ func MustMarshalArgs(args ...interface{}) []json.RawMessage { // structured StructuredArg // ) // err := UnmarshalArgs(args, &num, &str, &bul, &structured) -func UnmarshalArgs(jsonArgs []json.RawMessage, args ...interface{}) error { +func UnmarshalArgs(jsonArgs []json.RawMessage, args ...any) error { if len(args) != len(jsonArgs) { return fmt.Errorf("DecodeArgs: expected %d input arguments, got %d JSON arguments", len(args), len(jsonArgs)) } diff --git a/gopls/internal/protocol/generate/generate.go b/gopls/internal/protocol/generate/generate.go index 7418918f51f..2bb14790940 100644 --- a/gopls/internal/protocol/generate/generate.go +++ b/gopls/internal/protocol/generate/generate.go @@ -64,7 +64,7 @@ func propStar(name string, t NameType, gotype string) (string, string) { star = "" // passed by reference, so no need for * } else { switch gotype { - case "bool", "uint32", "int32", "string", "interface{}": + case "bool", "uint32", "int32", "string", "interface{}", "any": star = "" // gopls compatibility if t.Optional } } diff --git a/gopls/internal/protocol/generate/main_test.go b/gopls/internal/protocol/generate/main_test.go index 73c22048a80..cc616b66195 100644 --- a/gopls/internal/protocol/generate/main_test.go +++ b/gopls/internal/protocol/generate/main_test.go @@ -40,7 +40,7 @@ func TestParseContents(t *testing.T) { if err != nil { t.Fatal(err) } - var our interface{} + var our any if err := json.Unmarshal(out, &our); err != nil { t.Fatal(err) } @@ -50,7 +50,7 @@ func TestParseContents(t *testing.T) { if err != nil { t.Fatalf("could not read metaModel.json: %v", err) } - var raw interface{} + var raw any if err := json.Unmarshal(buf, &raw); err != nil { t.Fatal(err) } diff --git a/gopls/internal/protocol/generate/output.go b/gopls/internal/protocol/generate/output.go index c981bf9c383..ba9d0cb909f 100644 --- a/gopls/internal/protocol/generate/output.go +++ b/gopls/internal/protocol/generate/output.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "log" + "slices" "sort" "strings" ) @@ -219,8 +220,8 @@ func genStructs(model *Model) { fmt.Fprintf(out, "//\n") out.WriteString(lspLink(model, camelCase(s.Name))) fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(s.Line)) - // for gpls compatibilitye, embed most extensions, but expand the rest some day - props := append([]NameType{}, s.Properties...) + // for gopls compatibility, embed most extensions, but expand the rest some day + props := slices.Clone(s.Properties) if s.Name == "SymbolInformation" { // but expand this one for _, ex := range s.Extends { fmt.Fprintf(out, "\t// extends %s\n", ex.Name) @@ -242,7 +243,7 @@ func genStructs(model *Model) { // base types // (For URI and DocumentURI, see ../uri.go.) - types["LSPAny"] = "type LSPAny = interface{}\n" + types["LSPAny"] = "type LSPAny = any\n" // A special case, the only previously existing Or type types["DocumentDiagnosticReport"] = "type DocumentDiagnosticReport = Or_DocumentDiagnosticReport // (alias) \n" @@ -318,7 +319,7 @@ func genGenTypes() { sort.Strings(names) fmt.Fprintf(out, "// created for Or %v\n", names) fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1)) - fmt.Fprintf(out, "\tValue interface{} `json:\"value\"`\n") + fmt.Fprintf(out, "\tValue any `json:\"value\"`\n") case "and": fmt.Fprintf(out, "// created for And\n") fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1)) diff --git a/gopls/internal/protocol/generate/tables.go b/gopls/internal/protocol/generate/tables.go index c80337f187b..c0841a2334b 100644 --- a/gopls/internal/protocol/generate/tables.go +++ b/gopls/internal/protocol/generate/tables.go @@ -57,32 +57,32 @@ var usedGoplsStar = make(map[prop]bool) // For gopls compatibility, use a different, typically more restrictive, type for some fields. var renameProp = map[prop]string{ - {"CancelParams", "id"}: "interface{}", + {"CancelParams", "id"}: "any", {"Command", "arguments"}: "[]json.RawMessage", {"CodeAction", "data"}: "json.RawMessage", // delay unmarshalling commands - {"Diagnostic", "code"}: "interface{}", + {"Diagnostic", "code"}: "any", {"Diagnostic", "data"}: "json.RawMessage", // delay unmarshalling quickfixes - {"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]interface{}", + {"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]any", {"ExecuteCommandParams", "arguments"}: "[]json.RawMessage", {"FoldingRange", "kind"}: "string", {"Hover", "contents"}: "MarkupContent", {"InlayHint", "label"}: "[]InlayHintLabelPart", - {"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]interface{}", - {"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]interface{}", + {"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any", + {"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any", // PJW: this one is tricky. - {"ServerCapabilities", "codeActionProvider"}: "interface{}", + {"ServerCapabilities", "codeActionProvider"}: "any", - {"ServerCapabilities", "inlayHintProvider"}: "interface{}", + {"ServerCapabilities", "inlayHintProvider"}: "any", // slightly tricky - {"ServerCapabilities", "renameProvider"}: "interface{}", + {"ServerCapabilities", "renameProvider"}: "any", // slightly tricky - {"ServerCapabilities", "semanticTokensProvider"}: "interface{}", + {"ServerCapabilities", "semanticTokensProvider"}: "any", // slightly tricky - {"ServerCapabilities", "textDocumentSync"}: "interface{}", + {"ServerCapabilities", "textDocumentSync"}: "any", {"TextDocumentSyncOptions", "save"}: "SaveOptions", {"WorkspaceEdit", "documentChanges"}: "[]DocumentChange", } @@ -122,7 +122,7 @@ var goplsType = map[string]string{ "ConfigurationParams": "ParamConfiguration", "DocumentUri": "DocumentURI", "InitializeParams": "ParamInitialize", - "LSPAny": "interface{}", + "LSPAny": "any", "Lit_SemanticTokensOptions_range_Item1": "PRangeESemanticTokensOptions", @@ -130,18 +130,18 @@ var goplsType = map[string]string{ "Or_DidChangeConfigurationRegistrationOptions_section": "OrPSection_workspace_didChangeConfiguration", "Or_InlayHintLabelPart_tooltip": "OrPTooltipPLabel", "Or_InlayHint_tooltip": "OrPTooltip_textDocument_inlayHint", - "Or_LSPAny": "interface{}", + "Or_LSPAny": "any", "Or_ParameterInformation_documentation": "string", "Or_ParameterInformation_label": "string", "Or_PrepareRenameResult": "PrepareRenamePlaceholder", - "Or_ProgressToken": "interface{}", + "Or_ProgressToken": "any", "Or_Result_textDocument_completion": "CompletionList", "Or_Result_textDocument_declaration": "Or_textDocument_declaration", "Or_Result_textDocument_definition": "[]Location", - "Or_Result_textDocument_documentSymbol": "[]interface{}", + "Or_Result_textDocument_documentSymbol": "[]any", "Or_Result_textDocument_implementation": "[]Location", - "Or_Result_textDocument_semanticTokens_full_delta": "interface{}", + "Or_Result_textDocument_semanticTokens_full_delta": "any", "Or_Result_textDocument_typeDefinition": "[]Location", "Or_Result_workspace_symbol": "[]SymbolInformation", "Or_TextDocumentContentChangeEvent": "TextDocumentContentChangePartial", @@ -152,7 +152,7 @@ var goplsType = map[string]string{ "Tuple_ParameterInformation_label_Item1": "UIntCommaUInt", "WorkspaceFoldersServerCapabilities": "WorkspaceFolders5Gn", - "[]LSPAny": "[]interface{}", + "[]LSPAny": "[]any", "[]Or_Result_textDocument_codeAction_Item0_Elem": "[]CodeAction", "[]PreviousResultId": "[]PreviousResultID", diff --git a/gopls/internal/protocol/mapper_test.go b/gopls/internal/protocol/mapper_test.go index 8ba611a99f9..4326cc7be74 100644 --- a/gopls/internal/protocol/mapper_test.go +++ b/gopls/internal/protocol/mapper_test.go @@ -318,9 +318,9 @@ func getPrePost(content []byte, offset int) (string, string) { // -- these are the historical lsppos tests -- type testCase struct { - content string // input text - substrOrOffset interface{} // explicit integer offset, or a substring - wantLine, wantChar int // expected LSP position information + content string // input text + substrOrOffset any // explicit integer offset, or a substring + wantLine, wantChar int // expected LSP position information } // offset returns the test case byte offset diff --git a/gopls/internal/protocol/protocol.go b/gopls/internal/protocol/protocol.go index 7cc5589aa0b..f98d6371273 100644 --- a/gopls/internal/protocol/protocol.go +++ b/gopls/internal/protocol/protocol.go @@ -33,8 +33,8 @@ type ClientCloser interface { type connSender interface { io.Closer - Notify(ctx context.Context, method string, params interface{}) error - Call(ctx context.Context, method string, params, result interface{}) error + Notify(ctx context.Context, method string, params any) error + Call(ctx context.Context, method string, params, result any) error } type clientDispatcher struct { @@ -59,11 +59,11 @@ func (c clientConn) Close() error { return c.conn.Close() } -func (c clientConn) Notify(ctx context.Context, method string, params interface{}) error { +func (c clientConn) Notify(ctx context.Context, method string, params any) error { return c.conn.Notify(ctx, method, params) } -func (c clientConn) Call(ctx context.Context, method string, params interface{}, result interface{}) error { +func (c clientConn) Call(ctx context.Context, method string, params any, result any) error { id, err := c.conn.Call(ctx, method, params, result) if ctx.Err() != nil { cancelCall(ctx, c, id) @@ -83,11 +83,11 @@ func (c clientConnV2) Close() error { return c.conn.Close() } -func (c clientConnV2) Notify(ctx context.Context, method string, params interface{}) error { +func (c clientConnV2) Notify(ctx context.Context, method string, params any) error { return c.conn.Notify(ctx, method, params) } -func (c clientConnV2) Call(ctx context.Context, method string, params interface{}, result interface{}) error { +func (c clientConnV2) Call(ctx context.Context, method string, params any, result any) error { call := c.conn.Call(ctx, method, params) err := call.Await(ctx, result) if ctx.Err() != nil { @@ -126,16 +126,16 @@ func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler { } func ClientHandlerV2(client Client) jsonrpc2_v2.Handler { - return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if ctx.Err() != nil { return nil, RequestCancelledErrorV2 } req1 := req2to1(req) var ( - result interface{} + result any resErr error ) - replier := func(_ context.Context, res interface{}, err error) error { + replier := func(_ context.Context, res any, err error) error { if err != nil { resErr = err return nil @@ -166,16 +166,16 @@ func ServerHandler(server Server, handler jsonrpc2.Handler) jsonrpc2.Handler { } func ServerHandlerV2(server Server) jsonrpc2_v2.Handler { - return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if ctx.Err() != nil { return nil, RequestCancelledErrorV2 } req1 := req2to1(req) var ( - result interface{} + result any resErr error ) - replier := func(_ context.Context, res interface{}, err error) error { + replier := func(_ context.Context, res any, err error) error { if err != nil { resErr = err return nil @@ -232,7 +232,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler { // be careful about racing between the two paths. // TODO(iancottrell): Add a test that watches the stream and verifies the response // for the cancelled request flows. - replyWithDetachedContext := func(ctx context.Context, resp interface{}, err error) error { + replyWithDetachedContext := func(ctx context.Context, resp any, err error) error { // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest if ctx.Err() != nil && err == nil { err = RequestCancelledError @@ -257,7 +257,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler { } } -func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params interface{}, result interface{}) error { +func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params any, result any) error { id, err := conn.Call(ctx, method, params, result) if ctx.Err() != nil { cancelCall(ctx, clientConn{conn}, id) diff --git a/gopls/internal/protocol/tsclient.go b/gopls/internal/protocol/tsclient.go index 8fd322d424a..51eef36b4bf 100644 --- a/gopls/internal/protocol/tsclient.go +++ b/gopls/internal/protocol/tsclient.go @@ -26,7 +26,7 @@ type Client interface { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#client_unregisterCapability UnregisterCapability(context.Context, *UnregistrationParams) error // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#telemetry_event - Event(context.Context, *interface{}) error + Event(context.Context, *any) error // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_publishDiagnostics PublishDiagnostics(context.Context, *PublishDiagnosticsParams) error // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#window_logMessage @@ -97,7 +97,7 @@ func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, return true, reply(ctx, nil, err) case "telemetry/event": - var params interface{} + var params any if err := UnmarshalJSON(r.Params(), ¶ms); err != nil { return true, sendParseError(ctx, reply, err) } @@ -236,7 +236,7 @@ func (s *clientDispatcher) RegisterCapability(ctx context.Context, params *Regis func (s *clientDispatcher) UnregisterCapability(ctx context.Context, params *UnregistrationParams) error { return s.sender.Call(ctx, "client/unregisterCapability", params, nil) } -func (s *clientDispatcher) Event(ctx context.Context, params *interface{}) error { +func (s *clientDispatcher) Event(ctx context.Context, params *any) error { return s.sender.Notify(ctx, "telemetry/event", params) } func (s *clientDispatcher) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) error { diff --git a/gopls/internal/protocol/tsprotocol.go b/gopls/internal/protocol/tsprotocol.go index 198aeae7d01..444e51e0717 100644 --- a/gopls/internal/protocol/tsprotocol.go +++ b/gopls/internal/protocol/tsprotocol.go @@ -135,7 +135,7 @@ type CallHierarchyItem struct { SelectionRange Range `json:"selectionRange"` // A data entry field that is preserved between a call hierarchy prepare and // incoming calls or outgoing calls requests. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // Call hierarchy options used during static registration. @@ -196,7 +196,7 @@ type CallHierarchyRegistrationOptions struct { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#cancelParams type CancelParams struct { // The request id to cancel. - ID interface{} `json:"id"` + ID any `json:"id"` } // Additional information that describes document changes. @@ -249,7 +249,7 @@ type ClientCapabilities struct { // @since 3.16.0 General *GeneralClientCapabilities `json:"general,omitempty"` // Experimental client capabilities. - Experimental interface{} `json:"experimental,omitempty"` + Experimental any `json:"experimental,omitempty"` } // @since 3.18.0 @@ -758,7 +758,7 @@ type CodeLens struct { Command *Command `json:"command,omitempty"` // A data entry field that is preserved on a code lens item between // a {@link CodeLensRequest} and a {@link CodeLensResolveRequest} - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // The client capabilities of a {@link CodeLensRequest}. @@ -1047,7 +1047,7 @@ type CompletionItem struct { Command *Command `json:"command,omitempty"` // A data entry field that is preserved on a completion item between a // {@link CompletionRequest} and a {@link CompletionResolveRequest}. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // In many cases the items of an actual completion result share the same @@ -1085,7 +1085,7 @@ type CompletionItemDefaults struct { // A default data value. // // @since 3.17.0 - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // The kind of a completion entry. @@ -1413,7 +1413,7 @@ type Diagnostic struct { // always provide a severity value. Severity DiagnosticSeverity `json:"severity,omitempty"` // The diagnostic's code, which usually appear in the user interface. - Code interface{} `json:"code,omitempty"` + Code any `json:"code,omitempty"` // An optional property to describe the error code. // Requires the code field (above) to be present/not null. // @@ -1563,7 +1563,7 @@ type DidChangeConfigurationClientCapabilities struct { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#didChangeConfigurationParams type DidChangeConfigurationParams struct { // The actual changed settings - Settings interface{} `json:"settings"` + Settings any `json:"settings"` } // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#didChangeConfigurationRegistrationOptions @@ -1789,7 +1789,7 @@ type DocumentDiagnosticReportKind string // // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#documentDiagnosticReportPartialResult type DocumentDiagnosticReportPartialResult struct { - RelatedDocuments map[DocumentURI]interface{} `json:"relatedDocuments"` + RelatedDocuments map[DocumentURI]any `json:"relatedDocuments"` } // A document filter describes a top level text document or @@ -1899,7 +1899,7 @@ type DocumentLink struct { Tooltip string `json:"tooltip,omitempty"` // A data entry field that is preserved on a document link between a // DocumentLinkRequest and a DocumentLinkResolveRequest. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // The client capabilities of a {@link DocumentLinkRequest}. @@ -2702,7 +2702,7 @@ type InlayHint struct { PaddingRight bool `json:"paddingRight,omitempty"` // A data entry field that is preserved on an inlay hint between // a `textDocument/inlayHint` and a `inlayHint/resolve` request. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // Inlay hint client capabilities. @@ -3053,13 +3053,13 @@ type InsertTextFormat uint32 // // @since 3.16.0 type InsertTextMode uint32 -type LSPAny = interface{} +type LSPAny = any // LSP arrays. // @since 3.17.0 // // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#lSPArray -type LSPArray = []interface{} // (alias) +type LSPArray = []any // (alias) type LSPErrorCodes int32 // LSP object definition. @@ -3623,337 +3623,337 @@ type OptionalVersionedTextDocumentIdentifier struct { // created for Or [Location LocationUriOnly] type OrPLocation_workspace_symbol struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [[]string string] type OrPSection_workspace_didChangeConfiguration struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkupContent string] type OrPTooltipPLabel struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkupContent string] type OrPTooltip_textDocument_inlayHint struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [int32 string] type Or_CancelParams_id struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [ClientSemanticTokensRequestFullDelta bool] type Or_ClientSemanticTokensRequestOptions_full struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [Lit_ClientSemanticTokensRequestOptions_range_Item1 bool] type Or_ClientSemanticTokensRequestOptions_range struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [EditRangeWithInsertReplace Range] type Or_CompletionItemDefaults_editRange struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkupContent string] type Or_CompletionItem_documentation struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InsertReplaceEdit TextEdit] type Or_CompletionItem_textEdit struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [Location []Location] type Or_Definition struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [int32 string] type Or_Diagnostic_code struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [RelatedFullDocumentDiagnosticReport RelatedUnchangedDocumentDiagnosticReport] type Or_DocumentDiagnosticReport struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [FullDocumentDiagnosticReport UnchangedDocumentDiagnosticReport] type Or_DocumentDiagnosticReportPartialResult_relatedDocuments_Value struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookCellTextDocumentFilter TextDocumentFilter] type Or_DocumentFilter struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [Pattern RelativePattern] type Or_GlobPattern struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkedString MarkupContent []MarkedString] type Or_Hover_contents struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [[]InlayHintLabelPart string] type Or_InlayHint_label struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [StringValue string] type Or_InlineCompletionItem_insertText struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InlineValueEvaluatableExpression InlineValueText InlineValueVariableLookup] type Or_InlineValue struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkedStringWithLanguage string] type Or_MarkedString struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentFilter string] type Or_NotebookCellTextDocumentFilter_notebook struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentFilterNotebookType NotebookDocumentFilterPattern NotebookDocumentFilterScheme] type Or_NotebookDocumentFilter struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentFilter string] type Or_NotebookDocumentFilterWithCells_notebook struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentFilter string] type Or_NotebookDocumentFilterWithNotebook_notebook struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentFilterWithCells NotebookDocumentFilterWithNotebook] type Or_NotebookDocumentSyncOptions_notebookSelector_Elem struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [FullDocumentDiagnosticReport UnchangedDocumentDiagnosticReport] type Or_RelatedFullDocumentDiagnosticReport_relatedDocuments_Value struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [FullDocumentDiagnosticReport UnchangedDocumentDiagnosticReport] type Or_RelatedUnchangedDocumentDiagnosticReport_relatedDocuments_Value struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [CodeAction Command] type Or_Result_textDocument_codeAction_Item0_Elem struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InlineCompletionList []InlineCompletionItem] type Or_Result_textDocument_inlineCompletion struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [SemanticTokensFullDelta bool] type Or_SemanticTokensOptions_full struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [PRangeESemanticTokensOptions bool] type Or_SemanticTokensOptions_range struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [CallHierarchyOptions CallHierarchyRegistrationOptions bool] type Or_ServerCapabilities_callHierarchyProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [CodeActionOptions bool] type Or_ServerCapabilities_codeActionProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DocumentColorOptions DocumentColorRegistrationOptions bool] type Or_ServerCapabilities_colorProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DeclarationOptions DeclarationRegistrationOptions bool] type Or_ServerCapabilities_declarationProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DefinitionOptions bool] type Or_ServerCapabilities_definitionProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DiagnosticOptions DiagnosticRegistrationOptions] type Or_ServerCapabilities_diagnosticProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DocumentFormattingOptions bool] type Or_ServerCapabilities_documentFormattingProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DocumentHighlightOptions bool] type Or_ServerCapabilities_documentHighlightProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DocumentRangeFormattingOptions bool] type Or_ServerCapabilities_documentRangeFormattingProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [DocumentSymbolOptions bool] type Or_ServerCapabilities_documentSymbolProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [FoldingRangeOptions FoldingRangeRegistrationOptions bool] type Or_ServerCapabilities_foldingRangeProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [HoverOptions bool] type Or_ServerCapabilities_hoverProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [ImplementationOptions ImplementationRegistrationOptions bool] type Or_ServerCapabilities_implementationProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InlayHintOptions InlayHintRegistrationOptions bool] type Or_ServerCapabilities_inlayHintProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InlineCompletionOptions bool] type Or_ServerCapabilities_inlineCompletionProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [InlineValueOptions InlineValueRegistrationOptions bool] type Or_ServerCapabilities_inlineValueProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [LinkedEditingRangeOptions LinkedEditingRangeRegistrationOptions bool] type Or_ServerCapabilities_linkedEditingRangeProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MonikerOptions MonikerRegistrationOptions bool] type Or_ServerCapabilities_monikerProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [NotebookDocumentSyncOptions NotebookDocumentSyncRegistrationOptions] type Or_ServerCapabilities_notebookDocumentSync struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [ReferenceOptions bool] type Or_ServerCapabilities_referencesProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [RenameOptions bool] type Or_ServerCapabilities_renameProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [SelectionRangeOptions SelectionRangeRegistrationOptions bool] type Or_ServerCapabilities_selectionRangeProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [SemanticTokensOptions SemanticTokensRegistrationOptions] type Or_ServerCapabilities_semanticTokensProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [TextDocumentSyncKind TextDocumentSyncOptions] type Or_ServerCapabilities_textDocumentSync struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [TypeDefinitionOptions TypeDefinitionRegistrationOptions bool] type Or_ServerCapabilities_typeDefinitionProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [TypeHierarchyOptions TypeHierarchyRegistrationOptions bool] type Or_ServerCapabilities_typeHierarchyProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [WorkspaceSymbolOptions bool] type Or_ServerCapabilities_workspaceSymbolProvider struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [MarkupContent string] type Or_SignatureInformation_documentation struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [AnnotatedTextEdit SnippetTextEdit TextEdit] type Or_TextDocumentEdit_edits_Elem struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [TextDocumentFilterLanguage TextDocumentFilterPattern TextDocumentFilterScheme] type Or_TextDocumentFilter struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [SaveOptions bool] type Or_TextDocumentSyncOptions_save struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [WorkspaceFullDocumentDiagnosticReport WorkspaceUnchangedDocumentDiagnosticReport] type Or_WorkspaceDocumentDiagnosticReport struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [CreateFile DeleteFile RenameFile TextDocumentEdit] type Or_WorkspaceEdit_documentChanges_Elem struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [TextDocumentContentOptions TextDocumentContentRegistrationOptions] type Or_WorkspaceOptions_textDocumentContent struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Or [Declaration []DeclarationLink] type Or_textDocument_declaration struct { - Value interface{} `json:"value"` + Value any `json:"value"` } // created for Literal (Lit_SemanticTokensOptions_range_Item1) @@ -4122,11 +4122,11 @@ type ProgressParams struct { // The progress token provided by the client or server. Token ProgressToken `json:"token"` // The progress data. - Value interface{} `json:"value"` + Value any `json:"value"` } // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#progressToken -type ProgressToken = interface{} // (alias) +type ProgressToken = any // (alias) // The publish diagnostic client capabilities. // // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#publishDiagnosticsClientCapabilities @@ -4227,7 +4227,7 @@ type Registration struct { // The method / capability to register for. Method string `json:"method"` // Options necessary for the registration. - RegisterOptions interface{} `json:"registerOptions,omitempty"` + RegisterOptions any `json:"registerOptions,omitempty"` } // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#registrationParams @@ -4262,7 +4262,7 @@ type RelatedFullDocumentDiagnosticReport struct { // a.cpp and result in errors in a header file b.hpp. // // @since 3.17.0 - RelatedDocuments map[DocumentURI]interface{} `json:"relatedDocuments,omitempty"` + RelatedDocuments map[DocumentURI]any `json:"relatedDocuments,omitempty"` FullDocumentDiagnosticReport } @@ -4279,7 +4279,7 @@ type RelatedUnchangedDocumentDiagnosticReport struct { // a.cpp and result in errors in a header file b.hpp. // // @since 3.17.0 - RelatedDocuments map[DocumentURI]interface{} `json:"relatedDocuments,omitempty"` + RelatedDocuments map[DocumentURI]any `json:"relatedDocuments,omitempty"` UnchangedDocumentDiagnosticReport } @@ -4691,7 +4691,7 @@ type ServerCapabilities struct { // Defines how text documents are synced. Is either a detailed structure // defining each notification or for backwards compatibility the // TextDocumentSyncKind number. - TextDocumentSync interface{} `json:"textDocumentSync,omitempty"` + TextDocumentSync any `json:"textDocumentSync,omitempty"` // Defines how notebook documents are synced. // // @since 3.17.0 @@ -4719,7 +4719,7 @@ type ServerCapabilities struct { // The server provides code actions. CodeActionOptions may only be // specified if the client states that it supports // `codeActionLiteralSupport` in its initial `initialize` request. - CodeActionProvider interface{} `json:"codeActionProvider,omitempty"` + CodeActionProvider any `json:"codeActionProvider,omitempty"` // The server provides code lens. CodeLensProvider *CodeLensOptions `json:"codeLensProvider,omitempty"` // The server provides document link support. @@ -4737,7 +4737,7 @@ type ServerCapabilities struct { // The server provides rename support. RenameOptions may only be // specified if the client states that it supports // `prepareSupport` in its initial `initialize` request. - RenameProvider interface{} `json:"renameProvider,omitempty"` + RenameProvider any `json:"renameProvider,omitempty"` // The server provides folding provider support. FoldingRangeProvider *Or_ServerCapabilities_foldingRangeProvider `json:"foldingRangeProvider,omitempty"` // The server provides selection range support. @@ -4755,7 +4755,7 @@ type ServerCapabilities struct { // The server provides semantic tokens support. // // @since 3.16.0 - SemanticTokensProvider interface{} `json:"semanticTokensProvider,omitempty"` + SemanticTokensProvider any `json:"semanticTokensProvider,omitempty"` // The server provides moniker support. // // @since 3.16.0 @@ -4771,7 +4771,7 @@ type ServerCapabilities struct { // The server provides inlay hints. // // @since 3.17.0 - InlayHintProvider interface{} `json:"inlayHintProvider,omitempty"` + InlayHintProvider any `json:"inlayHintProvider,omitempty"` // The server has support for pull model diagnostics. // // @since 3.17.0 @@ -4784,7 +4784,7 @@ type ServerCapabilities struct { // Workspace specific server capabilities. Workspace *WorkspaceOptions `json:"workspace,omitempty"` // Experimental server capabilities. - Experimental interface{} `json:"experimental,omitempty"` + Experimental any `json:"experimental,omitempty"` } // @since 3.18.0 @@ -5590,7 +5590,7 @@ type TypeHierarchyItem struct { // supertypes or subtypes requests. It could also be used to identify the // type hierarchy in the server, helping improve the performance on // resolving supertypes and subtypes. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` } // Type hierarchy options used during static registration. @@ -6132,7 +6132,7 @@ type WorkspaceSymbol struct { Location OrPLocation_workspace_symbol `json:"location"` // A data entry field that is preserved on a workspace symbol between a // workspace symbol request and a workspace symbol resolve request. - Data interface{} `json:"data,omitempty"` + Data any `json:"data,omitempty"` BaseSymbolInformation } @@ -6244,7 +6244,7 @@ type XInitializeParams struct { // The capabilities provided by the client (editor or tool) Capabilities ClientCapabilities `json:"capabilities"` // User provided initialization options. - InitializationOptions interface{} `json:"initializationOptions,omitempty"` + InitializationOptions any `json:"initializationOptions,omitempty"` // The initial trace setting. If omitted trace is disabled ('off'). Trace *TraceValue `json:"trace,omitempty"` WorkDoneProgressParams @@ -6287,7 +6287,7 @@ type _InitializeParams struct { // The capabilities provided by the client (editor or tool) Capabilities ClientCapabilities `json:"capabilities"` // User provided initialization options. - InitializationOptions interface{} `json:"initializationOptions,omitempty"` + InitializationOptions any `json:"initializationOptions,omitempty"` // The initial trace setting. If omitted trace is disabled ('off'). Trace *TraceValue `json:"trace,omitempty"` WorkDoneProgressParams diff --git a/gopls/internal/protocol/tsserver.go b/gopls/internal/protocol/tsserver.go index 51ddad9ec1f..d09f118c171 100644 --- a/gopls/internal/protocol/tsserver.go +++ b/gopls/internal/protocol/tsserver.go @@ -80,7 +80,7 @@ type Server interface { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_documentLink DocumentLink(context.Context, *DocumentLinkParams) ([]DocumentLink, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_documentSymbol - DocumentSymbol(context.Context, *DocumentSymbolParams) ([]interface{}, error) + DocumentSymbol(context.Context, *DocumentSymbolParams) ([]any, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_foldingRange FoldingRange(context.Context, *FoldingRangeParams) ([]FoldingRange, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_formatting @@ -120,7 +120,7 @@ type Server interface { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_semanticTokens_full SemanticTokensFull(context.Context, *SemanticTokensParams) (*SemanticTokens, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_semanticTokens_full_delta - SemanticTokensFullDelta(context.Context, *SemanticTokensDeltaParams) (interface{}, error) + SemanticTokensFullDelta(context.Context, *SemanticTokensDeltaParams) (any, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_semanticTokens_range SemanticTokensRange(context.Context, *SemanticTokensRangeParams) (*SemanticTokens, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_signatureHelp @@ -152,7 +152,7 @@ type Server interface { // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workspace_didRenameFiles DidRenameFiles(context.Context, *RenameFilesParams) error // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workspace_executeCommand - ExecuteCommand(context.Context, *ExecuteCommandParams) (interface{}, error) + ExecuteCommand(context.Context, *ExecuteCommandParams) (any, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workspace_symbol Symbol(context.Context, *WorkspaceSymbolParams) ([]SymbolInformation, error) // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workspace_textDocumentContent @@ -1083,8 +1083,8 @@ func (s *serverDispatcher) DocumentLink(ctx context.Context, params *DocumentLin } return result, nil } -func (s *serverDispatcher) DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) ([]interface{}, error) { - var result []interface{} +func (s *serverDispatcher) DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) ([]any, error) { + var result []any if err := s.sender.Call(ctx, "textDocument/documentSymbol", params, &result); err != nil { return nil, err } @@ -1223,8 +1223,8 @@ func (s *serverDispatcher) SemanticTokensFull(ctx context.Context, params *Seman } return result, nil } -func (s *serverDispatcher) SemanticTokensFullDelta(ctx context.Context, params *SemanticTokensDeltaParams) (interface{}, error) { - var result interface{} +func (s *serverDispatcher) SemanticTokensFullDelta(ctx context.Context, params *SemanticTokensDeltaParams) (any, error) { + var result any if err := s.sender.Call(ctx, "textDocument/semanticTokens/full/delta", params, &result); err != nil { return nil, err } @@ -1303,8 +1303,8 @@ func (s *serverDispatcher) DidDeleteFiles(ctx context.Context, params *DeleteFil func (s *serverDispatcher) DidRenameFiles(ctx context.Context, params *RenameFilesParams) error { return s.sender.Notify(ctx, "workspace/didRenameFiles", params) } -func (s *serverDispatcher) ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (interface{}, error) { - var result interface{} +func (s *serverDispatcher) ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (any, error) { + var result any if err := s.sender.Call(ctx, "workspace/executeCommand", params, &result); err != nil { return nil, err } From 155dc6e01004d076e033aefd5c8ec33fa1877e19 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 3 Jan 2025 17:07:11 -0500 Subject: [PATCH 008/309] gopls/internal/settings: document why unusedvariable is off Updates golang/go#48975 Updates golang/go#54373 Change-Id: I6537ee08ae87e75ce47fdac63f6286048ec666e9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640040 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/settings/analysis.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 9204e54458b..a0265a9beba 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -197,13 +197,7 @@ func init() { {analyzer: fillreturns.Analyzer}, {analyzer: nonewvars.Analyzer}, {analyzer: noresultvalues.Analyzer}, - // TODO(rfindley): why isn't the 'unusedvariable' analyzer enabled, if it - // is only enhancing type errors with suggested fixes? - // - // In particular, enabling this analyzer could cause unused variables to be - // greyed out, (due to the 'deletions only' fix). That seems like a nice UI - // feature. - {analyzer: unusedvariable.Analyzer, nonDefault: true}, + {analyzer: unusedvariable.Analyzer, nonDefault: true}, // not fully baked; see #54373 } for _, analyzer := range analyzers { DefaultAnalyzers[analyzer.analyzer.Name] = analyzer From fc2161a739ce1ecc8cef96de21c936a77c190d96 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 3 Jan 2025 15:35:46 -0500 Subject: [PATCH 009/309] internal/analysis/modernize: minmax: don't reduce to y:=min(x, y) Fixes golang/go#71111 Change-Id: Ie396f1a6c6b357fbe4c53e2f4e480990397f4d07 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640038 Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/minmax.go | 39 ++++++++++++------- .../modernize/testdata/src/minmax/minmax.go | 10 +++++ .../testdata/src/minmax/minmax.go.golden | 11 +++++- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index e496f0dab0d..d17ad684d66 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -98,19 +98,23 @@ func minmax(pass *analysis.Pass) { } else if prev, ok := curIfStmt.PrevSibling(); ok && is[*ast.AssignStmt](prev.Node()) { fassign := prev.Node().(*ast.AssignStmt) - // Have: lhs2 = rhs2; if a < b { lhs = rhs } + // Have: lhs0 = rhs0; if a < b { lhs = rhs } + // // For pattern 2, check that - // - lhs = lhs2 - // - {rhs,rhs2} = {a,b}, but allow lhs2 to - // stand for rhs2. - // TODO(adonovan): accept "var lhs2 = rhs2" form too. - lhs2 := fassign.Lhs[0] - rhs2 := fassign.Rhs[0] - - if equalSyntax(lhs, lhs2) { - if equalSyntax(rhs, a) && (equalSyntax(rhs2, b) || equalSyntax(lhs2, b)) { + // - lhs = lhs0 + // - {a,b} = {rhs,rhs0} or {rhs,lhs0} + // The replacement must use rhs0 not lhs0 though. + // For example, we accept this variant: + // lhs = x; if lhs < y { lhs = y } => lhs = min(x, y), not min(lhs, y) + // + // TODO(adonovan): accept "var lhs0 = rhs0" form too. + lhs0 := fassign.Lhs[0] + rhs0 := fassign.Rhs[0] + + if equalSyntax(lhs, lhs0) { + if equalSyntax(rhs, a) && (equalSyntax(rhs0, b) || equalSyntax(lhs0, b)) { sign = +sign - } else if (equalSyntax(rhs2, a) || equalSyntax(lhs2, a)) && equalSyntax(rhs, b) { + } else if (equalSyntax(rhs0, a) || equalSyntax(lhs0, a)) && equalSyntax(rhs, b) { sign = -sign } else { return @@ -121,6 +125,15 @@ func minmax(pass *analysis.Pass) { return // min/max function is shadowed } + // Permit lhs0 to stand for rhs0 in the matching, + // but don't actually reduce to lhs0 = min(lhs0, rhs) + // since the "=" could be a ":=". Use min(rhs0, rhs). + if equalSyntax(lhs0, a) { + a = rhs0 + } else if equalSyntax(lhs0, b) { + b = rhs0 + } + // pattern 2 pass.Report(analysis.Diagnostic{ // Highlight the condition a < b. @@ -131,8 +144,8 @@ func minmax(pass *analysis.Pass) { SuggestedFixes: []analysis.SuggestedFix{{ Message: fmt.Sprintf("Replace if/else with %s", sym), TextEdits: []analysis.TextEdit{{ - // Replace rhs2 and IfStmt with min(a, b) - Pos: rhs2.Pos(), + // Replace rhs0 and IfStmt with min(a, b) + Pos: rhs0.Pos(), End: ifStmt.End(), NewText: fmt.Appendf(nil, "%s(%s, %s)", sym, diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index 393b3729e07..22747ed5547 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -71,3 +71,13 @@ func nopeIfStmtHasInitStmt() { } print(x) } + +// Regression test for a bug: fix was "y := max(x, y)". +func oops() { + x := 1 + y := 2 + if x > y { // want "if statement can be modernized using max" + y = x + } + print(y) +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index aacf84dd1c4..c045fa35a85 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -11,12 +11,12 @@ func ifmax(a, b int) { } func ifminvariant(a, b int) { - x := min(x, b) + x := min(a, b) print(x) } func ifmaxvariant(a, b int) { - x := min(a, x) + x := min(a, b) print(x) } @@ -51,3 +51,10 @@ func nopeIfStmtHasInitStmt() { } print(x) } + +// Regression test for a bug: fix was "y := max(x, y)". +func oops() { + x := 1 + y := max(x, 2) + print(y) +} From 16f297998268bc70724fe77c4f78f26c4c08bf6b Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Mon, 6 Jan 2025 16:47:45 -0500 Subject: [PATCH 010/309] gopls/internal/analysis: unusedvariable The unused variable quickfix removes more than it should when there are comments following the unused variable statement. This CL accounts for comments when determing the end position of the text edit in the quickfix. Fixes golang/go#54373 Change-Id: I9c920983c0c70b26b23dbb457cffeaa39a8ba721 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640579 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Reviewed-by: Alan Donovan --- gopls/doc/analyzers.md | 2 +- gopls/doc/release/v0.18.0.md | 4 ++ .../unusedvariable/testdata/src/assign/a.go | 11 ++++++ .../testdata/src/assign/a.go.golden | 8 ++++ .../analysis/unusedvariable/unusedvariable.go | 38 ++++++++++++++++--- gopls/internal/doc/api.json | 4 +- gopls/internal/settings/analysis.go | 2 +- 7 files changed, 59 insertions(+), 10 deletions(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index acc95d29dc4..4521b04f841 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -1011,7 +1011,7 @@ Package documentation: [unusedresult](https://pkg.go.dev/golang.org/x/tools/go/a -Default: off. Enable by setting `"analyses": {"unusedvariable": true}`. +Default: on. Package documentation: [unusedvariable](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedvariable) diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 769ca69f2ea..b785b2fae9c 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -49,6 +49,10 @@ this fashion (or with `%s` for the port) is passed to `net.Dial` or a related function, and offers a fix to use `net.JoinHostPort` instead. +## `unusedvariable` analyzer now on by default + +This analyzer suggests deleting the unused variable declaration. + ## "Implementations" supports generics At long last, the "Go to Implementations" feature now fully supports diff --git a/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go b/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go index 8421824b2d3..f53fd8cc091 100644 --- a/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go +++ b/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go @@ -65,6 +65,17 @@ func commentAbove() { v := "s" // want `declared (and|but) not used` } +func commentBelow() { + v := "s" // want `declared (and|but) not used` + // v is a variable +} + +func commentSpaceBelow() { + v := "s" // want `declared (and|but) not used` + + // v is a variable +} + func fBool() bool { return true } diff --git a/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go.golden b/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go.golden index 8f8d6128ea8..075d7c28b42 100644 --- a/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go.golden +++ b/gopls/internal/analysis/unusedvariable/testdata/src/assign/a.go.golden @@ -50,6 +50,14 @@ func commentAbove() { // v is a variable } +func commentBelow() { + // v is a variable +} + +func commentSpaceBelow() { + // v is a variable +} + func fBool() bool { return true } diff --git a/gopls/internal/analysis/unusedvariable/unusedvariable.go b/gopls/internal/analysis/unusedvariable/unusedvariable.go index 5e4dd52be7e..15bcd43d873 100644 --- a/gopls/internal/analysis/unusedvariable/unusedvariable.go +++ b/gopls/internal/analysis/unusedvariable/unusedvariable.go @@ -17,6 +17,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/util/safetoken" ) const Doc = `check for unused variables and suggest fixes` @@ -37,7 +38,7 @@ var unusedVariableRegexp = []*regexp.Regexp{ regexp.MustCompile("^declared and not used: (.*)$"), // Go 1.23+ } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { for _, typeErr := range pass.TypeErrors { for _, re := range unusedVariableRegexp { match := re.FindStringSubmatch(typeErr.Msg) @@ -113,7 +114,7 @@ func runForError(pass *analysis.Pass, err types.Error, name string) error { continue } - fixes := removeVariableFromAssignment(path, stmt, ident) + fixes := removeVariableFromAssignment(pass.Fset, path, stmt, ident) // fixes may be nil if len(fixes) > 0 { diag.SuggestedFixes = fixes @@ -164,7 +165,7 @@ func removeVariableFromSpec(pass *analysis.Pass, path []ast.Node, stmt *ast.Valu // Find parent DeclStmt and delete it for _, node := range path { if declStmt, ok := node.(*ast.DeclStmt); ok { - edits := deleteStmtFromBlock(path, declStmt) + edits := deleteStmtFromBlock(pass.Fset, path, declStmt) if len(edits) == 0 { return nil // can this happen? } @@ -198,7 +199,7 @@ func removeVariableFromSpec(pass *analysis.Pass, path []ast.Node, stmt *ast.Valu } } -func removeVariableFromAssignment(path []ast.Node, stmt *ast.AssignStmt, ident *ast.Ident) []analysis.SuggestedFix { +func removeVariableFromAssignment(fset *token.FileSet, path []ast.Node, stmt *ast.AssignStmt, ident *ast.Ident) []analysis.SuggestedFix { // The only variable in the assignment is unused if len(stmt.Lhs) == 1 { // If LHS has only one expression to be valid it has to have 1 expression @@ -221,7 +222,7 @@ func removeVariableFromAssignment(path []ast.Node, stmt *ast.AssignStmt, ident * } // RHS does not have any side effects, delete the whole statement - edits := deleteStmtFromBlock(path, stmt) + edits := deleteStmtFromBlock(fset, path, stmt) if len(edits) == 0 { return nil // can this happen? } @@ -252,7 +253,7 @@ func suggestedFixMessage(name string) string { return fmt.Sprintf("Remove variable %s", name) } -func deleteStmtFromBlock(path []ast.Node, stmt ast.Stmt) []analysis.TextEdit { +func deleteStmtFromBlock(fset *token.FileSet, path []ast.Node, stmt ast.Stmt) []analysis.TextEdit { // Find innermost enclosing BlockStmt. var block *ast.BlockStmt for i := range path { @@ -282,6 +283,31 @@ func deleteStmtFromBlock(path []ast.Node, stmt ast.Stmt) []analysis.TextEdit { end = block.List[nodeIndex+1].Pos() } + // Account for comments within the block containing the statement + // TODO(adonovan): when golang/go#20744 is addressed, query the AST + // directly for comments between stmt.End() and end. For now we + // must scan the entire file's comments (though we could binary search). + astFile := path[len(path)-1].(*ast.File) + currFile := fset.File(end) + stmtEndLine := safetoken.Line(currFile, stmt.End()) +outer: + for _, cg := range astFile.Comments { + for _, co := range cg.List { + if stmt.End() <= co.Pos() && co.Pos() <= end { + coLine := safetoken.Line(currFile, co.Pos()) + // If a comment exists within the current block, after the unused variable statement, + // and before the next statement, we shouldn't delete it. + if coLine > stmtEndLine { + end = co.Pos() + break outer + } + if co.Pos() > end { + break outer + } + } + } + } + return []analysis.TextEdit{ { Pos: stmt.Pos(), diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b6fcc8f5b19..9fa3443cc5f 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -608,7 +608,7 @@ { "Name": "\"unusedvariable\"", "Doc": "check for unused variables and suggest fixes", - "Default": "false" + "Default": "true" }, { "Name": "\"unusedwrite\"", @@ -1267,7 +1267,7 @@ "Name": "unusedvariable", "Doc": "check for unused variables and suggest fixes", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedvariable", - "Default": false + "Default": true }, { "Name": "unusedwrite", diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index a0265a9beba..0bd9fa8136b 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -197,7 +197,7 @@ func init() { {analyzer: fillreturns.Analyzer}, {analyzer: nonewvars.Analyzer}, {analyzer: noresultvalues.Analyzer}, - {analyzer: unusedvariable.Analyzer, nonDefault: true}, // not fully baked; see #54373 + {analyzer: unusedvariable.Analyzer}, } for _, analyzer := range analyzers { DefaultAnalyzers[analyzer.analyzer.Name] = analyzer From b4e093ecab06b92e0b6b18bd0bace46e474ec13b Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Fri, 3 Jan 2025 18:12:14 -0500 Subject: [PATCH 011/309] go/packages: run TestIssue70394 with Go 1.23 It's expected to pass now that the CL 631855 cherry-pick is submitted. Also apply gofmt after CL 637961 while here. For golang/go#70394. Change-Id: I436b24f86669af7c9a2ff06703732018ab2a9ffc Reviewed-on: https://go-review.googlesource.com/c/tools/+/640017 LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: Tim King Auto-Submit: Dmitri Shuralyov --- go/packages/packages_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index fc420321c31..06fa488d1ed 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -3157,8 +3157,7 @@ func TestIssue69606b(t *testing.T) { // in another package (m/b) where the types for m/b are coming from the compiler, // e.g. `go list -compiled=true ... m/b`. func TestIssue70394(t *testing.T) { - // TODO(taking): backport https://go.dev/cl/604099 so that this works on 23. - testenv.NeedsGo1Point(t, 24) + testenv.NeedsGo1Point(t, 23) testenv.NeedsTool(t, "go") // requires go list. testenv.NeedsGoBuild(t) // requires the compiler for export data. @@ -3339,7 +3338,7 @@ func main() { pkgs, err := packages.Load(&packages.Config{ Mode: packages.NeedName | packages.NeedTarget, - Env: append(os.Environ(), "GOPATH=" + gopath, "GO111MODULE=off"), + Env: append(os.Environ(), "GOPATH="+gopath, "GO111MODULE=off"), }, filepath.Join(gopath, "src", "...")) if err != nil { t.Fatal(err) From f7fb515e7122f841fb5181e7424643ff6062bf96 Mon Sep 17 00:00:00 2001 From: Tim King Date: Tue, 7 Jan 2025 16:26:21 -0800 Subject: [PATCH 012/309] internal/analysisinternal: check for interface recievers typesinternal.ReceiverNamed(R) may return nil on a interface method receiver R. Document this, and update analysisinternal.IsMethodNamed to allow for this this. Fixes golang/go#70399 Change-Id: I4f0542f2f9495cc373ad2d772736152b29dcc254 Cq-Include-Trybots: luci.golang.try:x_tools-gotip-linux-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/tools/+/641177 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/analysisinternal/analysis.go | 3 ++- internal/typesinternal/recv.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 10fb580ceac..7f0b2a7bd52 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -336,7 +336,8 @@ func IsMethodNamed(obj types.Object, pkgPath string, typeName string, names ...s if fn, ok := obj.(*types.Func); ok { if recv := fn.Type().(*types.Signature).Recv(); recv != nil { _, T := typesinternal.ReceiverNamed(recv) - return IsTypeNamed(T, pkgPath, typeName) && + return T != nil && + IsTypeNamed(T, pkgPath, typeName) && slices.Contains(names, fn.Name()) } } diff --git a/internal/typesinternal/recv.go b/internal/typesinternal/recv.go index e54accc69a0..8352ea76173 100644 --- a/internal/typesinternal/recv.go +++ b/internal/typesinternal/recv.go @@ -12,7 +12,8 @@ import ( // type of recv, which may be of the form N or *N, or aliases thereof. // It also reports whether a Pointer was present. // -// The named result may be nil in ill-typed code. +// The named result may be nil if recv is from a method on an +// anonymous interface or struct types or in ill-typed code. func ReceiverNamed(recv *types.Var) (isPtr bool, named *types.Named) { t := recv.Type() if ptr, ok := types.Unalias(t).(*types.Pointer); ok { From 6016188d8e58282ea0a17064039059f275da5d01 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Wed, 8 Jan 2025 10:04:48 -0500 Subject: [PATCH 013/309] cmd: apply modernizers Apply modernizers to all the files in the x/tools/cmd tree. Also suppress most warnings. Change-Id: I6cd80200f423e79971cedbc64cd731300e65c834 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641355 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- cmd/benchcmp/benchcmp.go | 2 +- cmd/bundle/main_test.go | 4 ++-- cmd/godex/godex.go | 2 +- cmd/godex/print.go | 2 +- cmd/godex/writetype.go | 2 +- cmd/godoc/godoc_test.go | 10 +++++----- cmd/gotype/gotype.go | 2 +- cmd/goyacc/yacc.go | 7 ++++--- cmd/signature-fuzzer/fuzz-driver/driver.go | 2 +- cmd/signature-fuzzer/fuzz-runner/rnr_test.go | 4 ++-- cmd/signature-fuzzer/fuzz-runner/runner.go | 6 +++--- .../internal/fuzz-generator/generator.go | 11 ++++------- .../internal/fuzz-generator/stringparm.go | 5 +---- cmd/splitdwarf/internal/macho/file.go | 13 +++++++------ cmd/splitdwarf/internal/macho/file_test.go | 18 +++++++++--------- cmd/splitdwarf/splitdwarf.go | 6 +++--- cmd/ssadump/main.go | 2 +- cmd/stringer/multifile_test.go | 4 ++-- cmd/stringer/stringer.go | 6 +++--- 19 files changed, 52 insertions(+), 56 deletions(-) diff --git a/cmd/benchcmp/benchcmp.go b/cmd/benchcmp/benchcmp.go index ed53d717c9f..d078d3d4d9c 100644 --- a/cmd/benchcmp/benchcmp.go +++ b/cmd/benchcmp/benchcmp.go @@ -133,7 +133,7 @@ func main() { } } -func fatal(msg interface{}) { +func fatal(msg any) { fmt.Fprintln(os.Stderr, msg) os.Exit(1) } diff --git a/cmd/bundle/main_test.go b/cmd/bundle/main_test.go index 0dee2afb0b2..42dac86a2b8 100644 --- a/cmd/bundle/main_test.go +++ b/cmd/bundle/main_test.go @@ -27,7 +27,7 @@ func testBundle(t *testing.T, x packagestest.Exporter) { e := packagestest.Export(t, x, []packagestest.Module{ { Name: "initial", - Files: map[string]interface{}{ + Files: map[string]any{ "a.go": load("testdata/src/initial/a.go"), "b.go": load("testdata/src/initial/b.go"), "c.go": load("testdata/src/initial/c.go"), @@ -35,7 +35,7 @@ func testBundle(t *testing.T, x packagestest.Exporter) { }, { Name: "domain.name/importdecl", - Files: map[string]interface{}{ + Files: map[string]any{ "p.go": load("testdata/src/domain.name/importdecl/p.go"), }, }, diff --git a/cmd/godex/godex.go b/cmd/godex/godex.go index e91dbfcea5f..619976d4a37 100644 --- a/cmd/godex/godex.go +++ b/cmd/godex/godex.go @@ -84,7 +84,7 @@ func main() { } } -func logf(format string, args ...interface{}) { +func logf(format string, args ...any) { if *verbose { fmt.Fprintf(os.Stderr, format, args...) } diff --git a/cmd/godex/print.go b/cmd/godex/print.go index 57383e0e7ec..120c2e04d6b 100644 --- a/cmd/godex/print.go +++ b/cmd/godex/print.go @@ -48,7 +48,7 @@ func (p *printer) print(s string) { } } -func (p *printer) printf(format string, args ...interface{}) { +func (p *printer) printf(format string, args ...any) { p.print(fmt.Sprintf(format, args...)) } diff --git a/cmd/godex/writetype.go b/cmd/godex/writetype.go index bfe36977892..866f718f05f 100644 --- a/cmd/godex/writetype.go +++ b/cmd/godex/writetype.go @@ -111,7 +111,7 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited // n := t.NumMethods() if n == 0 { - p.print("interface{}") + p.print("any") return } diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index 94159445a54..66b93f10630 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -71,14 +71,14 @@ func serverAddress(t *testing.T) string { return ln.Addr().String() } -func waitForServerReady(t *testing.T, ctx context.Context, cmd *exec.Cmd, addr string) { +func waitForServerReady(t *testing.T, ctx context.Context, addr string) { waitForServer(t, ctx, fmt.Sprintf("http://%v/", addr), "Go Documentation Server", false) } -func waitForSearchReady(t *testing.T, ctx context.Context, cmd *exec.Cmd, addr string) { +func waitForSearchReady(t *testing.T, ctx context.Context, _ *exec.Cmd, addr string) { waitForServer(t, ctx, fmt.Sprintf("http://%v/search?q=FALLTHROUGH", addr), "The list of tokens.", @@ -208,7 +208,7 @@ func testWeb(t *testing.T, x packagestest.Exporter, bin string, withIndex bool) e := packagestest.Export(t, x, []packagestest.Module{ { Name: "godoc.test/repo1", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `// Package a is a package in godoc.test/repo1. package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"`, "b/b.go": `package b; const Name = "repo1b"`, @@ -216,7 +216,7 @@ package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"`, }, { Name: "godoc.test/repo2", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const Name = "repo2a"`, "b/b.go": `package b; const Name = "repo2b"`, }, @@ -261,7 +261,7 @@ package a; import _ "godoc.test/repo2/a"; const Name = "repo1a"`, if withIndex { waitForSearchReady(t, ctx, cmd, addr) } else { - waitForServerReady(t, ctx, cmd, addr) + waitForServerReady(t, ctx, addr) waitUntilScanComplete(t, ctx, addr) } diff --git a/cmd/gotype/gotype.go b/cmd/gotype/gotype.go index 4a731f26233..591f163f561 100644 --- a/cmd/gotype/gotype.go +++ b/cmd/gotype/gotype.go @@ -185,7 +185,7 @@ func report(err error) { } // parse may be called concurrently -func parse(filename string, src interface{}) (*ast.File, error) { +func parse(filename string, src any) (*ast.File, error) { if *verbose { fmt.Println(filename) } diff --git a/cmd/goyacc/yacc.go b/cmd/goyacc/yacc.go index bc6395480e8..965a76f14dc 100644 --- a/cmd/goyacc/yacc.go +++ b/cmd/goyacc/yacc.go @@ -52,6 +52,7 @@ import ( "go/format" "math" "os" + "slices" "strconv" "strings" "unicode" @@ -2323,7 +2324,7 @@ func wrstate(i int) { var pp, qq int if len(errors) > 0 { - actions := append([]int(nil), temp1...) + actions := slices.Clone(temp1) defaultAction := ERRCODE if lastred != 0 { defaultAction = -lastred @@ -3176,7 +3177,7 @@ func create(s string) *bufio.Writer { } // write out error comment -func lerrorf(lineno int, s string, v ...interface{}) { +func lerrorf(lineno int, s string, v ...any) { nerrors++ fmt.Fprintf(stderr, s, v...) fmt.Fprintf(stderr, ": %v:%v\n", infile, lineno) @@ -3186,7 +3187,7 @@ func lerrorf(lineno int, s string, v ...interface{}) { } } -func errorf(s string, v ...interface{}) { +func errorf(s string, v ...any) { lerrorf(lineno, s, v...) } diff --git a/cmd/signature-fuzzer/fuzz-driver/driver.go b/cmd/signature-fuzzer/fuzz-driver/driver.go index f61ca4b4b52..bd5e5550d42 100644 --- a/cmd/signature-fuzzer/fuzz-driver/driver.go +++ b/cmd/signature-fuzzer/fuzz-driver/driver.go @@ -59,7 +59,7 @@ var selbadfcnflag = flag.Int("badfcnidx", 0, "[Testing only] select index of bad var goimpflag = flag.Bool("goimports", false, "Run 'goimports' on generated code.") var randctlflag = flag.Int("randctl", generator.RandCtlChecks|generator.RandCtlPanic, "Wraprand control flag") -func verb(vlevel int, s string, a ...interface{}) { +func verb(vlevel int, s string, a ...any) { if *verbflag >= vlevel { fmt.Printf(s, a...) fmt.Printf("\n") diff --git a/cmd/signature-fuzzer/fuzz-runner/rnr_test.go b/cmd/signature-fuzzer/fuzz-runner/rnr_test.go index 2bab5b41add..77891c13946 100644 --- a/cmd/signature-fuzzer/fuzz-runner/rnr_test.go +++ b/cmd/signature-fuzzer/fuzz-runner/rnr_test.go @@ -16,7 +16,7 @@ import ( "golang.org/x/tools/internal/testenv" ) -func canRace(t *testing.T) bool { +func canRace() bool { _, err := exec.Command("go", "run", "-race", "./testdata/himom.go").CombinedOutput() return err == nil } @@ -70,7 +70,7 @@ func testRace(t *testing.T, binaryPath string) { // For this test to work, the current test platform has to support the // race detector. Check to see if that is the case by running a very // simple Go program through it. - if !canRace(t) { + if !canRace() { t.Skip("current platform does not appear to support the race detector") } diff --git a/cmd/signature-fuzzer/fuzz-runner/runner.go b/cmd/signature-fuzzer/fuzz-runner/runner.go index 27ab975f0c8..a1c4a11e90a 100644 --- a/cmd/signature-fuzzer/fuzz-runner/runner.go +++ b/cmd/signature-fuzzer/fuzz-runner/runner.go @@ -43,19 +43,19 @@ var forcetmpcleanflag = flag.Bool("forcetmpclean", false, "[Testing only] force var cleancacheflag = flag.Bool("cleancache", true, "[Testing only] don't clean the go cache") var raceflag = flag.Bool("race", false, "[Testing only] build generated code with -race") -func verb(vlevel int, s string, a ...interface{}) { +func verb(vlevel int, s string, a ...any) { if *verbflag >= vlevel { fmt.Printf(s, a...) fmt.Printf("\n") } } -func warn(s string, a ...interface{}) { +func warn(s string, a ...any) { fmt.Fprintf(os.Stderr, s, a...) fmt.Fprintf(os.Stderr, "\n") } -func fatal(s string, a ...interface{}) { +func fatal(s string, a ...any) { fmt.Fprintf(os.Stderr, s, a...) fmt.Fprintf(os.Stderr, "\n") os.Exit(1) diff --git a/cmd/signature-fuzzer/internal/fuzz-generator/generator.go b/cmd/signature-fuzzer/internal/fuzz-generator/generator.go index ba5f0552516..6c8002f9f0c 100644 --- a/cmd/signature-fuzzer/internal/fuzz-generator/generator.go +++ b/cmd/signature-fuzzer/internal/fuzz-generator/generator.go @@ -445,7 +445,7 @@ func writeCom(b *bytes.Buffer, i int) { var Verbctl int = 0 -func verb(vlevel int, s string, a ...interface{}) { +func verb(vlevel int, s string, a ...any) { if Verbctl >= vlevel { fmt.Printf(s, a...) fmt.Printf("\n") @@ -856,10 +856,7 @@ func (s *genstate) GenFunc(fidx int, pidx int) *funcdef { f.returns = append(f.returns, r) } spw := uint(s.wr.Intn(11)) - rstack := 1 << spw - if rstack < 4 { - rstack = 4 - } + rstack := max(1< ns { - en = ns - } + en := min(st+nel, ns) return "\"" + string(letters[st:en]) + "\"", value + 1 } diff --git a/cmd/splitdwarf/internal/macho/file.go b/cmd/splitdwarf/internal/macho/file.go index ceaaa028e16..dbfa2c0ac4a 100644 --- a/cmd/splitdwarf/internal/macho/file.go +++ b/cmd/splitdwarf/internal/macho/file.go @@ -15,6 +15,7 @@ import ( "fmt" "io" "os" + "slices" "strings" "unsafe" ) @@ -314,7 +315,7 @@ type FormatError struct { msg string } -func formatError(off int64, format string, data ...interface{}) *FormatError { +func formatError(off int64, format string, data ...any) *FormatError { return &FormatError{off, fmt.Sprintf(format, data...)} } @@ -518,7 +519,7 @@ func (b LoadBytes) String() string { } func (b LoadBytes) Raw() []byte { return b } -func (b LoadBytes) Copy() LoadBytes { return LoadBytes(append([]byte{}, b...)) } +func (b LoadBytes) Copy() LoadBytes { return LoadBytes(slices.Clone(b)) } func (b LoadBytes) LoadSize(t *FileTOC) uint32 { return uint32(len(b)) } func (lc LoadCmd) Put(b []byte, o binary.ByteOrder) int { @@ -648,7 +649,7 @@ func (s *Symtab) Put(b []byte, o binary.ByteOrder) int { func (s *Symtab) String() string { return fmt.Sprintf("Symtab %#v", s.SymtabCmd) } func (s *Symtab) Copy() *Symtab { - return &Symtab{SymtabCmd: s.SymtabCmd, Syms: append([]Symbol{}, s.Syms...)} + return &Symtab{SymtabCmd: s.SymtabCmd, Syms: slices.Clone(s.Syms)} } func (s *Symtab) LoadSize(t *FileTOC) uint32 { return uint32(unsafe.Sizeof(SymtabCmd{})) @@ -719,7 +720,7 @@ type Dysymtab struct { func (s *Dysymtab) String() string { return fmt.Sprintf("Dysymtab %#v", s.DysymtabCmd) } func (s *Dysymtab) Copy() *Dysymtab { - return &Dysymtab{DysymtabCmd: s.DysymtabCmd, IndirectSyms: append([]uint32{}, s.IndirectSyms...)} + return &Dysymtab{DysymtabCmd: s.DysymtabCmd, IndirectSyms: slices.Clone(s.IndirectSyms)} } func (s *Dysymtab) LoadSize(t *FileTOC) uint32 { return uint32(unsafe.Sizeof(DysymtabCmd{})) @@ -898,7 +899,7 @@ func NewFile(r io.ReaderAt) (*File, error) { if _, err := r.ReadAt(symdat, int64(hdr.Symoff)); err != nil { return nil, err } - st, err := f.parseSymtab(symdat, strtab, cmddat, &hdr, offset) + st, err := f.parseSymtab(symdat, strtab, &hdr, offset) st.SymtabCmd = hdr if err != nil { return nil, err @@ -1060,7 +1061,7 @@ func NewFile(r io.ReaderAt) (*File, error) { return f, nil } -func (f *File) parseSymtab(symdat, strtab, cmddat []byte, hdr *SymtabCmd, offset int64) (*Symtab, error) { +func (f *File) parseSymtab(symdat, strtab []byte, hdr *SymtabCmd, offset int64) (*Symtab, error) { bo := f.ByteOrder symtab := make([]Symbol, hdr.Nsyms) b := bytes.NewReader(symdat) diff --git a/cmd/splitdwarf/internal/macho/file_test.go b/cmd/splitdwarf/internal/macho/file_test.go index eacd238a16c..c28f3a294bf 100644 --- a/cmd/splitdwarf/internal/macho/file_test.go +++ b/cmd/splitdwarf/internal/macho/file_test.go @@ -13,7 +13,7 @@ import ( type fileTest struct { file string hdr FileHeader - loads []interface{} + loads []any sections []*SectionHeader relocations map[string][]Reloc } @@ -22,7 +22,7 @@ var fileTests = []fileTest{ { "testdata/gcc-386-darwin-exec", FileHeader{0xfeedface, Cpu386, 0x3, 0x2, 0xc, 0x3c0, 0x85}, - []interface{}{ + []any{ &SegmentHeader{LcSegment, 0x38, "__PAGEZERO", 0x0, 0x1000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0}, &SegmentHeader{LcSegment, 0xc0, "__TEXT", 0x1000, 0x1000, 0x0, 0x1000, 0x7, 0x5, 0x2, 0x0, 0}, &SegmentHeader{LcSegment, 0xc0, "__DATA", 0x2000, 0x1000, 0x1000, 0x1000, 0x7, 0x3, 0x2, 0x0, 2}, @@ -48,7 +48,7 @@ var fileTests = []fileTest{ { "testdata/gcc-amd64-darwin-exec", FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0x2, 0xb, 0x568, 0x85}, - []interface{}{ + []any{ &SegmentHeader{LcSegment64, 0x48, "__PAGEZERO", 0x0, 0x100000000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0}, &SegmentHeader{LcSegment64, 0x1d8, "__TEXT", 0x100000000, 0x1000, 0x0, 0x1000, 0x7, 0x5, 0x5, 0x0, 0}, &SegmentHeader{LcSegment64, 0x138, "__DATA", 0x100001000, 0x1000, 0x1000, 0x1000, 0x7, 0x3, 0x3, 0x0, 5}, @@ -76,7 +76,7 @@ var fileTests = []fileTest{ { "testdata/gcc-amd64-darwin-exec-debug", FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0xa, 0x4, 0x5a0, 0}, - []interface{}{ + []any{ nil, // LC_UUID &SegmentHeader{LcSegment64, 0x1d8, "__TEXT", 0x100000000, 0x1000, 0x0, 0x0, 0x7, 0x5, 0x5, 0x0, 0}, &SegmentHeader{LcSegment64, 0x138, "__DATA", 0x100001000, 0x1000, 0x0, 0x0, 0x7, 0x3, 0x3, 0x0, 5}, @@ -104,7 +104,7 @@ var fileTests = []fileTest{ { "testdata/clang-386-darwin-exec-with-rpath", FileHeader{0xfeedface, Cpu386, 0x3, 0x2, 0x10, 0x42c, 0x1200085}, - []interface{}{ + []any{ nil, // LC_SEGMENT nil, // LC_SEGMENT nil, // LC_SEGMENT @@ -128,7 +128,7 @@ var fileTests = []fileTest{ { "testdata/clang-amd64-darwin-exec-with-rpath", FileHeader{0xfeedfacf, CpuAmd64, 0x80000003, 0x2, 0x10, 0x4c8, 0x200085}, - []interface{}{ + []any{ nil, // LC_SEGMENT nil, // LC_SEGMENT nil, // LC_SEGMENT @@ -155,7 +155,7 @@ var fileTests = []fileTest{ nil, nil, map[string][]Reloc{ - "__text": []Reloc{ + "__text": { { Addr: 0x1d, Type: uint8(GENERIC_RELOC_VANILLA), @@ -190,7 +190,7 @@ var fileTests = []fileTest{ nil, nil, map[string][]Reloc{ - "__text": []Reloc{ + "__text": { { Addr: 0x19, Type: uint8(X86_64_RELOC_BRANCH), @@ -208,7 +208,7 @@ var fileTests = []fileTest{ Value: 2, }, }, - "__compact_unwind": []Reloc{ + "__compact_unwind": { { Addr: 0x0, Type: uint8(X86_64_RELOC_UNSIGNED), diff --git a/cmd/splitdwarf/splitdwarf.go b/cmd/splitdwarf/splitdwarf.go index e2a7790106f..90ff10b6a05 100644 --- a/cmd/splitdwarf/splitdwarf.go +++ b/cmd/splitdwarf/splitdwarf.go @@ -35,11 +35,11 @@ const ( pageAlign = 12 // 4096 = 1 << 12 ) -func note(format string, why ...interface{}) { +func note(format string, why ...any) { fmt.Fprintf(os.Stderr, format+"\n", why...) } -func fail(format string, why ...interface{}) { +func fail(format string, why ...any) { note(format, why...) os.Exit(1) } @@ -191,7 +191,7 @@ for input_exe need to allow writing. exeNeedsUuid := uuid == nil if exeNeedsUuid { - uuid = &macho.Uuid{macho.UuidCmd{LoadCmd: macho.LcUuid}} + uuid = &macho.Uuid{UuidCmd: macho.UuidCmd{LoadCmd: macho.LcUuid}} uuid.Len = uuid.LoadSize(newtoc) copy(uuid.Id[0:], contentuuid(&exeMacho.FileTOC)[0:16]) uuid.Id[6] = uuid.Id[6]&^0xf0 | 0x40 // version 4 (pseudo-random); see section 4.1.3 diff --git a/cmd/ssadump/main.go b/cmd/ssadump/main.go index f04c1c04633..7eda7b5e2ec 100644 --- a/cmd/ssadump/main.go +++ b/cmd/ssadump/main.go @@ -188,7 +188,7 @@ func doMain() error { // e.g. --flag=one --flag=two would produce []string{"one", "two"}. type stringListValue []string -func (ss *stringListValue) Get() interface{} { return []string(*ss) } +func (ss *stringListValue) Get() any { return []string(*ss) } func (ss *stringListValue) String() string { return fmt.Sprintf("%q", *ss) } diff --git a/cmd/stringer/multifile_test.go b/cmd/stringer/multifile_test.go index 32914c5e825..152e1cd7cc1 100644 --- a/cmd/stringer/multifile_test.go +++ b/cmd/stringer/multifile_test.go @@ -29,7 +29,7 @@ import ( // Several tests expect the type Foo generated in some package. func expectFooString(pkg string) []byte { - return []byte(fmt.Sprintf(` + return fmt.Appendf(nil, ` // Header comment ignored. package %s @@ -54,7 +54,7 @@ func (i Foo) String() string { return "Foo(" + strconv.FormatInt(int64(i), 10) + ")" } return _Foo_name[_Foo_index[i]:_Foo_index[i+1]] -}`, pkg)) +}`, pkg) } func TestMultifileStringer(t *testing.T) { diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go index 09be11ca58e..038e8e831b6 100644 --- a/cmd/stringer/stringer.go +++ b/cmd/stringer/stringer.go @@ -244,10 +244,10 @@ type Generator struct { buf bytes.Buffer // Accumulated output. pkg *Package // Package we are scanning. - logf func(format string, args ...interface{}) // test logging hook; nil when not testing + logf func(format string, args ...any) // test logging hook; nil when not testing } -func (g *Generator) Printf(format string, args ...interface{}) { +func (g *Generator) Printf(format string, args ...any) { fmt.Fprintf(&g.buf, format, args...) } @@ -279,7 +279,7 @@ type Package struct { func loadPackages( patterns, tags []string, trimPrefix string, lineComment bool, - logf func(format string, args ...interface{}), + logf func(format string, args ...any), ) []*Package { cfg := &packages.Config{ Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedFiles, From 248b94e2e3d947010840f7698d42a77e85e487c0 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 8 Jan 2025 12:23:03 -0500 Subject: [PATCH 014/309] go/ast/inspector: treat empty type filter like nil The documentation for type filtering speaks only of the length of the slice, but the logic checks for nil. Fix the logic to match the documentation. (This is an observable change but there is never any reason to pass []ast.Node{}... as a type filter.) Change-Id: Ifa8f72c0c2ff5a4c0fc2ee39e65641503a93c471 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641436 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- go/ast/inspector/typeof.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ast/inspector/typeof.go b/go/ast/inspector/typeof.go index 40b1bfd7e62..97784484578 100644 --- a/go/ast/inspector/typeof.go +++ b/go/ast/inspector/typeof.go @@ -219,7 +219,7 @@ func typeOf(n ast.Node) uint64 { //go:linkname maskOf func maskOf(nodes []ast.Node) uint64 { - if nodes == nil { + if len(nodes) == 0 { return math.MaxUint64 // match all node types } var mask uint64 From 60643c02c5bc7d7ebcebc9cf596df0c530e318d1 Mon Sep 17 00:00:00 2001 From: Tim King Date: Tue, 7 Jan 2025 15:49:45 -0800 Subject: [PATCH 015/309] go/types/typeutil: clarify what inGenericSig applies to Change-Id: Ib0737d7a99db3038a109aa66ac9b3caa3fff3ec9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641455 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Commit-Queue: Tim King --- go/types/typeutil/map.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/go/types/typeutil/map.go b/go/types/typeutil/map.go index 93b3090c687..43261147c05 100644 --- a/go/types/typeutil/map.go +++ b/go/types/typeutil/map.go @@ -257,10 +257,13 @@ func (h hasher) hash(t types.Type) uint32 { } tparams := t.TypeParams() - for i := range tparams.Len() { - h.inGenericSig = true - tparam := tparams.At(i) - hash += 7 * h.hash(tparam.Constraint()) + if n := tparams.Len(); n > 0 { + h.inGenericSig = true // affects constraints, params, and results + + for i := range n { + tparam := tparams.At(i) + hash += 7 * h.hash(tparam.Constraint()) + } } return hash + 3*h.hashTuple(t.Params()) + 5*h.hashTuple(t.Results()) From ae303ab94b5bbc25572282595b16de7ae9566e0b Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 8 Jan 2025 21:05:56 +0000 Subject: [PATCH 016/309] gopls/internal/analysis/modernize: replace WithCancel with t.Cancel This CL adds a modernizer to replace calls to context.WithCancel with calls to t.Cancel, where t is the *testing.T (or B, or F) for the relevant surrounding test function. Example: func TestFoo(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel ... } => func TestFoo(t *testing.T) { ctx := t.Context() } Also, factor out an analysisinternal.IsPointerNamed helper to assist with identifying pointers to named types. This slightly alters the behavior of the bloop pass, as it was previously tolerant implicitly referenced testing.B variables, but that seems unimportant. Updates golang/go#70815 Change-Id: Id10b5feb85a43e71d5ad740198d27135e8a3e6cf Reviewed-on: https://go-review.googlesource.com/c/tools/+/641440 Auto-Submit: Robert Findley Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/analyzers.md | 2 + gopls/internal/analysis/modernize/bloop.go | 9 +- gopls/internal/analysis/modernize/doc.go | 2 + .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + .../src/testingcontext/testingcontext.go | 1 + .../src/testingcontext/testingcontext_test.go | 78 ++++++ .../testingcontext_test.go.golden | 71 +++++ .../analysis/modernize/testingcontext.go | 257 ++++++++++++++++++ gopls/internal/doc/api.json | 4 +- internal/analysisinternal/analysis.go | 11 + internal/astutil/cursor/cursor.go | 8 +- 12 files changed, 432 insertions(+), 13 deletions(-) create mode 100644 gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go.golden create mode 100644 gopls/internal/analysis/modernize/testingcontext.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 4521b04f841..c7f03b55019 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -480,6 +480,8 @@ existing code by using more modern features of Go, such as: from the maps package, added in go1.21; - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19; + - replacing uses of context.WithCancel in tests with t.Context, added in + go1.24; Default: on. diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 582e19eed7e..9c88eb7257d 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -16,7 +16,6 @@ import ( "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" - "golang.org/x/tools/internal/typesinternal" ) // bloop updates benchmarks that use "for range b.N", replacing it @@ -90,7 +89,7 @@ func bloop(pass *analysis.Pass) { if cmp, ok := n.Cond.(*ast.BinaryExpr); ok && cmp.Op == token.LSS { if sel, ok := cmp.Y.(*ast.SelectorExpr); ok && sel.Sel.Name == "N" && - isTestingB(info.TypeOf(sel.X)) { + analysisinternal.IsPointerToNamed(info.TypeOf(sel.X), "testing", "B") { delStart, delEnd := n.Cond.Pos(), n.Cond.End() @@ -133,7 +132,7 @@ func bloop(pass *analysis.Pass) { n.Key == nil && n.Value == nil && sel.Sel.Name == "N" && - isTestingB(info.TypeOf(sel.X)) { + analysisinternal.IsPointerToNamed(info.TypeOf(sel.X), "testing", "B") { pass.Report(analysis.Diagnostic{ // Highlight "range b.N". @@ -152,10 +151,6 @@ func bloop(pass *analysis.Pass) { } } -func isTestingB(t types.Type) bool { - return analysisinternal.IsTypeNamed(typesinternal.Unpointer(t), "testing", "B") -} - // uses reports whether the subtree cur contains a use of obj. func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { for curId := range cur.Preorder((*ast.Ident)(nil)) { diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 379e29b9b0b..35514357d0f 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -23,4 +23,6 @@ // from the maps package, added in go1.21; // - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), // added in go1.19; +// - replacing uses of context.WithCancel in tests with t.Context, added in +// go1.24; package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index b925e013f78..373461825d0 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -63,6 +63,7 @@ func run(pass *analysis.Pass) (any, error) { mapsloop(pass) minmax(pass) sortslice(pass) + testingContext(pass) // TODO(adonovan): // - more modernizers here; see #70815. diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 218c2238762..bf3114e2382 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -20,5 +20,6 @@ func Test(t *testing.T) { "mapsloop", "minmax", "sortslice", + "testingcontext", ) } diff --git a/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext.go b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext.go new file mode 100644 index 00000000000..8f29e6f6098 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext.go @@ -0,0 +1 @@ +package testingcontext diff --git a/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go new file mode 100644 index 00000000000..e4f2b6257ab --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go @@ -0,0 +1,78 @@ +package testingcontext + +import ( + "context" + + "testing" +) + +func Test(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t.Context" + defer cancel() + _ = ctx + + func() { + ctx, cancel := context.WithCancel(context.Background()) // Nope. scope of defer is not the testing func. + defer cancel() + _ = ctx + }() + + { + ctx, cancel := context.WithCancel(context.TODO()) // want "context.WithCancel can be modernized using t.Context" + defer cancel() + _ = ctx + var t int // not in scope of the call to WithCancel + _ = t + } + + { + ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) // Nope. ctx is redeclared. + defer cancel() + _ = ctx + } + + { + var t int + ctx, cancel := context.WithCancel(context.Background()) // Nope. t is shadowed. + defer cancel() + _ = ctx + _ = t + } + + t.Run("subtest", func(t2 *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t2.Context" + defer cancel() + _ = ctx + }) +} + +func TestAlt(t2 *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t2.Context" + defer cancel() + _ = ctx +} + +func Testnot(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) // Nope. Not a test func. + defer cancel() + _ = ctx +} + +func Benchmark(b *testing.B) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using b.Context" + defer cancel() + _ = ctx + + b.Run("subtest", func(b2 *testing.B) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using b2.Context" + defer cancel() + _ = ctx + }) +} + +func Fuzz(f *testing.F) { + ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using f.Context" + defer cancel() + _ = ctx +} diff --git a/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go.golden b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go.golden new file mode 100644 index 00000000000..c1d6bf0fce4 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/testingcontext/testingcontext_test.go.golden @@ -0,0 +1,71 @@ +package testingcontext + +import ( + "context" + + "testing" +) + +func Test(t *testing.T) { + ctx := t.Context() + _ = ctx + + func() { + ctx, cancel := context.WithCancel(context.Background()) // Nope. scope of defer is not the testing func. + defer cancel() + _ = ctx + }() + + { + ctx := t.Context() + _ = ctx + var t int // not in scope of the call to WithCancel + _ = t + } + + { + ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) // Nope. ctx is redeclared. + defer cancel() + _ = ctx + } + + { + var t int + ctx, cancel := context.WithCancel(context.Background()) // Nope. t is shadowed. + defer cancel() + _ = ctx + _ = t + } + + t.Run("subtest", func(t2 *testing.T) { + ctx := t2.Context() + _ = ctx + }) +} + +func TestAlt(t2 *testing.T) { + ctx := t2.Context() + _ = ctx +} + +func Testnot(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) // Nope. Not a test func. + defer cancel() + _ = ctx +} + +func Benchmark(b *testing.B) { + ctx := b.Context() + _ = ctx + + b.Run("subtest", func(b2 *testing.B) { + ctx := b2.Context() + _ = ctx + }) +} + +func Fuzz(f *testing.F) { + ctx := f.Context() + _ = ctx +} diff --git a/gopls/internal/analysis/modernize/testingcontext.go b/gopls/internal/analysis/modernize/testingcontext.go new file mode 100644 index 00000000000..daedb2e8f85 --- /dev/null +++ b/gopls/internal/analysis/modernize/testingcontext.go @@ -0,0 +1,257 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "fmt" + "go/ast" + "go/token" + "go/types" + "slices" + "strings" + "unicode" + "unicode/utf8" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" +) + +// The testingContext pass replaces calls to context.WithCancel from within +// tests to a use of testing.{T,B,F}.Context(), added in Go 1.24. +// +// Specifically, the testingContext pass suggests to replace: +// +// ctx, cancel := context.WithCancel(context.Background()) // or context.TODO +// defer cancel() +// +// with: +// +// ctx := t.Context() +// +// provided: +// +// - ctx and cancel are declared by the assignment +// - the deferred call is the only use of cancel +// - the call is within a test or subtest function +// - the relevant testing.{T,B,F} is named and not shadowed at the call +func testingContext(pass *analysis.Pass) { + if !analysisinternal.Imports(pass.Pkg, "testing") { + return + } + + info := pass.TypesInfo + + // checkCall finds eligible calls to context.WithCancel to replace. + checkCall := func(cur cursor.Cursor) { + call := cur.Node().(*ast.CallExpr) + obj := typeutil.Callee(info, call) + if !analysisinternal.IsFunctionNamed(obj, "context", "WithCancel") { + return + } + + // Have: context.WithCancel(arg) + + arg, ok := call.Args[0].(*ast.CallExpr) + if !ok { + return + } + if obj := typeutil.Callee(info, arg); !analysisinternal.IsFunctionNamed(obj, "context", "Background", "TODO") { + return + } + + // Have: context.WithCancel(context.{Background,TODO}()) + + parent := cur.Parent() + assign, ok := parent.Node().(*ast.AssignStmt) + if !ok || assign.Tok != token.DEFINE { + return + } + + // Have: a, b := context.WithCancel(context.{Background,TODO}()) + + // Check that both a and b are declared, not redeclarations. + var lhs []types.Object + for _, expr := range assign.Lhs { + id, ok := expr.(*ast.Ident) + if !ok { + return + } + obj, ok := info.Defs[id] + if !ok { + return + } + lhs = append(lhs, obj) + } + + next, ok := parent.NextSibling() + if !ok { + return + } + defr, ok := next.Node().(*ast.DeferStmt) + if !ok { + return + } + if soleUse(info, lhs[1]) != defr.Call.Fun { + return + } + + // Have: + // a, b := context.WithCancel(context.{Background,TODO}()) + // defer b() + + // Check that we are in a test func. + var testObj types.Object // relevant testing.{T,B,F}, or nil + + // TODO(rfindley): use cur.Ancestors when it is available. + stack := cur.Stack(nil) + slices.Reverse(stack) + findTestObj: + for _, ancestor := range stack { + switch n := ancestor.Node().(type) { + case *ast.FuncLit: + if call, ok := ancestor.Parent().Node().(*ast.CallExpr); ok && len(call.Args) == 2 && call.Args[1] == n { + obj := typeutil.Callee(info, call) + if (analysisinternal.IsMethodNamed(obj, "testing", "T", "Run") || + analysisinternal.IsMethodNamed(obj, "testing", "B", "Run")) && + len(n.Type.Params.List[0].Names) == 1 { + + testObj = info.Defs[n.Type.Params.List[0].Names[0]] + } + } + break findTestObj + case *ast.FuncDecl: + testObj = isTestFn(info, n) + break findTestObj + } + } + + if testObj != nil { + // Have a test function. Check that we can resolve the relevant + // testing.{T,B,F} at the current position. + if _, obj := lhs[0].Parent().LookupParent(testObj.Name(), lhs[0].Pos()); obj == testObj { + pass.Report(analysis.Diagnostic{ + Pos: call.Fun.Pos(), + End: call.Fun.End(), + Category: "testingcontext", + Message: fmt.Sprintf("context.WithCancel can be modernized using %s.Context", testObj.Name()), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Replace context.WithCancel with %s.Context", testObj.Name()), + TextEdits: []analysis.TextEdit{{ + Pos: assign.Pos(), + End: defr.End(), + NewText: fmt.Appendf(nil, "%s := %s.Context()", lhs[0].Name(), testObj.Name()), + }}, + }}, + }) + } + } + } + + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.24") { + for cur := range curFile.Preorder((*ast.CallExpr)(nil)) { + checkCall(cur) + } + } +} + +// soleUse returns the ident that refers to obj, if there is exactly one. +// +// TODO(rfindley): consider factoring to share with gopls/internal/refactor/inline. +func soleUse(info *types.Info, obj types.Object) (sole *ast.Ident) { + // This is not efficient, but it is called infrequently. + for id, obj2 := range info.Uses { + if obj2 == obj { + if sole != nil { + return nil // not unique + } + sole = id + } + } + return sole +} + +// isTestFn checks whether fn is a test function (TestX, BenchmarkX, FuzzX), +// returning the corresponding types.Object of the *testing.{T,B,F} argument. +// Returns nil if fn is a test function, but the testing.{T,B,F} argument is +// unnamed (or _). +// +// TODO(rfindley): consider handling the case of an unnamed argument, by adding +// an edit to give the argument a name. +// +// Adapted from go/analysis/passes/tests. +// TODO(rfindley): consider refactoring to share logic. +func isTestFn(info *types.Info, fn *ast.FuncDecl) types.Object { + // Want functions with 0 results and 1 parameter. + if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 || + fn.Type.Params == nil || + len(fn.Type.Params.List) != 1 || + len(fn.Type.Params.List[0].Names) != 1 { + + return nil + } + + prefix := testKind(fn.Name.Name) + if prefix == "" { + return nil + } + + if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 { + return nil // test functions must not be generic + } + + obj := info.Defs[fn.Type.Params.List[0].Names[0]] + if obj == nil { + return nil // e.g. _ *testing.T + } + + var name string + switch prefix { + case "Test": + name = "T" + case "Benchmark": + name = "B" + case "Fuzz": + name = "F" + } + + if !analysisinternal.IsPointerToNamed(obj.Type(), "testing", name) { + return nil + } + return obj +} + +// testKind returns "Test", "Benchmark", or "Fuzz" if name is a valid resp. +// test, benchmark, or fuzz function name. Otherwise, isTestName returns "". +// +// Adapted from go/analysis/passes/tests.isTestName. +func testKind(name string) string { + var prefix string + switch { + case strings.HasPrefix(name, "Test"): + prefix = "Test" + case strings.HasPrefix(name, "Benchmark"): + prefix = "Benchmark" + case strings.HasPrefix(name, "Fuzz"): + prefix = "Fuzz" + } + if prefix == "" { + return "" + } + suffix := name[len(prefix):] + if len(suffix) == 0 { + // "Test" is ok. + return prefix + } + r, _ := utf8.DecodeRuneInString(suffix) + if unicode.IsLower(r) { + return "" + } + return prefix +} diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 9fa3443cc5f..b5673b6232f 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -472,7 +472,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;", "Default": "true" }, { @@ -1103,7 +1103,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 7f0b2a7bd52..39583a401b0 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -311,6 +311,17 @@ func IsTypeNamed(t types.Type, pkgPath string, names ...string) bool { return false } +// IsPointerToNamed reports whether t is (or is an alias for) a pointer to a +// package-level defined type with the given package path and one of the given +// names. It returns false if t is not a pointer type. +func IsPointerToNamed(t types.Type, pkgPath string, names ...string) bool { + r := typesinternal.Unpointer(t) + if r == t { + return false + } + return IsTypeNamed(r, pkgPath, names...) +} + // IsFunctionNamed reports whether obj is a package-level function // defined in the given package and has one of the given names. // It returns false if obj is nil. diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 945170be25c..8d53f8eeb77 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -191,10 +191,10 @@ func (c Cursor) Parent() Cursor { return Cursor{c.in, c.events()[c.index].parent} } -// NextSibling returns the cursor for the next sibling node in the -// same list (for example, of files, decls, specs, statements, fields, -// or expressions) as the current node. It returns zero if the node is -// the last node in the list, or is not part of a list. +// NextSibling returns the cursor for the next sibling node in the same list +// (for example, of files, decls, specs, statements, fields, or expressions) as +// the current node. It returns (zero, false) if the node is the last node in +// the list, or is not part of a list. // // NextSibling must not be called on the Root node. // From df3de6aedd7fc85318a826d38a6c348fb54c62cd Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Sat, 4 Jan 2025 17:31:01 -0500 Subject: [PATCH 017/309] gopls: prepare for mod cache index This CL adds the infrastructure for using the module cache index when satisfying missing imports. There is no change in behavior as it invokes the existing imports.Source. There is a new option importsSource whose value can be "goimports" to keep the old behavior, "gopls" to use (in a future CL) the module cache index, and "off" (for use by cider) to avoid looking in the file system at all. The index is kept in memory. Periodically the code checks to see if it needs to be updated. Change-Id: I61e0b5e224a6c26d75932417b26ecb9f432b460f Reviewed-on: https://go-review.googlesource.com/c/tools/+/640495 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/imports.go | 41 ++++++++++++++++++++++++++--- gopls/internal/cache/session.go | 15 +++++------ gopls/internal/cache/source.go | 35 ++++++++++++++++++++++++ gopls/internal/cache/view.go | 9 ++++--- gopls/internal/golang/format.go | 17 +++++++++--- gopls/internal/settings/default.go | 1 + gopls/internal/settings/settings.go | 23 +++++++++++++--- internal/imports/fix.go | 2 +- 8 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 gopls/internal/cache/source.go diff --git a/gopls/internal/cache/imports.go b/gopls/internal/cache/imports.go index c467a851f8f..aa274221669 100644 --- a/gopls/internal/cache/imports.go +++ b/gopls/internal/cache/imports.go @@ -15,6 +15,7 @@ import ( "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/event/keys" "golang.org/x/tools/internal/imports" + "golang.org/x/tools/internal/modindex" ) // refreshTimer implements delayed asynchronous refreshing of state. @@ -59,11 +60,8 @@ func (t *refreshTimer) schedule() { if t.timer == nil { // Don't refresh more than twice per minute. - delay := 30 * time.Second // Don't spend more than ~2% of the time refreshing. - if adaptive := 50 * t.duration; adaptive > delay { - delay = adaptive - } + delay := max(30*time.Second, 50*t.duration) t.timer = time.AfterFunc(delay, func() { start := time.Now() t.mu.Lock() @@ -149,6 +147,41 @@ func newImportsState(backgroundCtx context.Context, modCache *sharedModCache, en return s } +// modcacheState holds a modindex.Index and controls its updates +type modcacheState struct { + dir string // GOMODCACHE + refreshTimer *refreshTimer + mu sync.Mutex + index *modindex.Index +} + +// newModcacheState constructs a new modcacheState for goimports. +// The returned state is automatically updated until [modcacheState.stopTimer] is called. +func newModcacheState(dir string) *modcacheState { + s := &modcacheState{ + dir: dir, + } + s.index, _ = modindex.ReadIndex(dir) + s.refreshTimer = newRefreshTimer(s.refreshIndex) + go s.refreshIndex() + return s +} + +func (s *modcacheState) refreshIndex() { + ok, err := modindex.Update(s.dir) + if err != nil || !ok { + return + } + // read the new index + s.mu.Lock() + defer s.mu.Unlock() + s.index, _ = modindex.ReadIndex(s.dir) +} + +func (s *modcacheState) stopTimer() { + s.refreshTimer.stop() +} + // stopTimer stops scheduled refreshes of this imports state. func (s *importsState) stopTimer() { s.refreshTimer.stop() diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index a6f4118e23e..99f7ecae957 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -8,10 +8,10 @@ import ( "context" "errors" "fmt" + "maps" "os" "path/filepath" "slices" - "sort" "strconv" "strings" "sync" @@ -218,7 +218,7 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, * ModCache: s.cache.modCache.dirCache(def.folder.Env.GOMODCACHE), } if def.folder.Options.VerboseOutput { - pe.Logf = func(format string, args ...interface{}) { + pe.Logf = func(format string, args ...any) { event.Log(ctx, fmt.Sprintf(format, args...)) } } @@ -237,6 +237,9 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, * viewDefinition: def, importsState: newImportsState(backgroundCtx, s.cache.modCache, pe), } + if def.folder.Options.ImportsSource != "off" { + v.modcacheState = newModcacheState(def.folder.Env.GOMODCACHE) + } s.snapshotWG.Add(1) v.snapshot = &Snapshot{ @@ -833,9 +836,7 @@ func (s *Session) DidModifyFiles(ctx context.Context, modifications []file.Modif openFiles = append(openFiles, o.URI()) } // Sort for determinism. - sort.Slice(openFiles, func(i, j int) bool { - return openFiles[i] < openFiles[j] - }) + slices.Sort(openFiles) // TODO(rfindley): can we avoid running the go command (go env) // synchronously to change processing? Can we assume that the env did not @@ -1124,9 +1125,7 @@ func (s *Session) FileWatchingGlobPatterns(ctx context.Context) map[protocol.Rel if err != nil { continue // view is shut down; continue with others } - for k, v := range snapshot.fileWatchingGlobPatterns() { - patterns[k] = v - } + maps.Copy(patterns, snapshot.fileWatchingGlobPatterns()) release() } return patterns diff --git a/gopls/internal/cache/source.go b/gopls/internal/cache/source.go new file mode 100644 index 00000000000..b5e1e74b160 --- /dev/null +++ b/gopls/internal/cache/source.go @@ -0,0 +1,35 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "context" + + "golang.org/x/tools/internal/imports" +) + +// interim code for using the module cache index in imports +// This code just forwards everything to an imports.ProcessEnvSource + +// goplsSource is an imports.Source that provides import information using +// gopls and the module cache index. +// TODO(pjw): implement. Right now, this just forwards to the imports.ProcessEnvSource. +type goplsSource struct { + envSource *imports.ProcessEnvSource +} + +func (s *Snapshot) NewGoplsSource(is *imports.ProcessEnvSource) *goplsSource { + return &goplsSource{ + envSource: is, + } +} + +func (s *goplsSource) LoadPackageNames(ctx context.Context, srcDir string, paths []imports.ImportPath) (map[imports.ImportPath]imports.PackageName, error) { + return s.envSource.LoadPackageNames(ctx, srcDir, paths) +} + +func (s *goplsSource) ResolveReferences(ctx context.Context, filename string, missing imports.References) ([]*imports.Result, error) { + return s.envSource.ResolveReferences(ctx, filename, missing) +} diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index 33c77760e7f..a6cdae4d2e8 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "log" + "maps" "os" "os/exec" "path" @@ -106,8 +107,12 @@ type View struct { // background contexts created for this view. baseCtx context.Context + // importsState is for the old imports code importsState *importsState + // maintain the current module cache index + modcacheState *modcacheState + // pkgIndex is an index of package IDs, for efficient storage of typerefs. pkgIndex *typerefs.PackageIndex @@ -1143,9 +1148,7 @@ func (s *Snapshot) ModuleUpgrades(modfile protocol.DocumentURI) map[string]strin defer s.mu.Unlock() upgrades := map[string]string{} orig, _ := s.moduleUpgrades.Get(modfile) - for mod, ver := range orig { - upgrades[mod] = ver - } + maps.Copy(upgrades, orig) return upgrades } diff --git a/gopls/internal/golang/format.go b/gopls/internal/golang/format.go index fa255e6b1c6..de4ec3a642c 100644 --- a/gopls/internal/golang/format.go +++ b/gopls/internal/golang/format.go @@ -21,6 +21,7 @@ import ( "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/event" @@ -120,7 +121,7 @@ func allImportsFixes(ctx context.Context, snapshot *cache.Snapshot, pgf *parsego defer done() if err := snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, opts *imports.Options) error { - allFixEdits, editsPerFix, err = computeImportEdits(ctx, pgf, snapshot.View().Folder().Env.GOROOT, opts) + allFixEdits, editsPerFix, err = computeImportEdits(ctx, pgf, snapshot, opts) return err }); err != nil { return nil, nil, fmt.Errorf("allImportsFixes: %v", err) @@ -130,12 +131,22 @@ func allImportsFixes(ctx context.Context, snapshot *cache.Snapshot, pgf *parsego // computeImportEdits computes a set of edits that perform one or all of the // necessary import fixes. -func computeImportEdits(ctx context.Context, pgf *parsego.File, goroot string, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*importFix, err error) { +func computeImportEdits(ctx context.Context, pgf *parsego.File, snapshot *cache.Snapshot, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*importFix, err error) { + goroot := snapshot.View().Folder().Env.GOROOT filename := pgf.URI.Path() // Build up basic information about the original file. isource, err := imports.NewProcessEnvSource(options.Env, filename, pgf.File.Name.Name) - allFixes, err := imports.FixImports(ctx, filename, pgf.Src, goroot, options.Env.Logf, isource) + var source imports.Source + switch snapshot.Options().ImportsSource { + case settings.ImportsSourceGopls: + source = snapshot.NewGoplsSource(isource) + case settings.ImportsSourceOff: // for cider, which has no file system + source = nil + case settings.ImportsSourceGoimports: + source = isource + } + allFixes, err := imports.FixImports(ctx, filename, pgf.Src, goroot, options.Env.Logf, source) if err != nil { return nil, nil, err } diff --git a/gopls/internal/settings/default.go b/gopls/internal/settings/default.go index f9b947b31a8..50f6f2ba3ea 100644 --- a/gopls/internal/settings/default.go +++ b/gopls/internal/settings/default.go @@ -39,6 +39,7 @@ func DefaultOptions(overrides ...func(*Options)) *Options { DynamicWatchedFilesSupported: true, LineFoldingOnly: false, HierarchicalDocumentSymbolSupport: true, + ImportsSource: ImportsSourceGopls, }, ServerOptions: ServerOptions{ SupportedCodeActions: map[file.Kind]map[protocol.CodeActionKind]bool{ diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 785ebd8b582..cd0c77e3c82 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -53,6 +53,7 @@ type ClientOptions struct { PreferredContentFormat protocol.MarkupKind LineFoldingOnly bool HierarchicalDocumentSymbolSupport bool + ImportsSource ImportsSourceEnum `status:"experimental"` SemanticTypes []string SemanticMods []string RelatedInformationSupported bool @@ -697,6 +698,19 @@ func (s ImportShortcut) ShowDefinition() bool { return s == BothShortcuts || s == DefinitionShortcut } +// ImportsSourceEnum has legal values: +// +// - `off` to disable searching the file system for imports +// - `gopls` to use the metadata graph and module cache index +// - `goimports` for the old behavior, to be deprecated +type ImportsSourceEnum string + +const ( + ImportsSourceOff ImportsSourceEnum = "off" + ImportsSourceGopls = "gopls" + ImportsSourceGoimports = "goimports" +) + type Matcher string const ( @@ -949,6 +963,11 @@ func (o *Options) setOne(name string, value any) error { return setBool(&o.CompleteUnimported, value) case "completionBudget": return setDuration(&o.CompletionBudget, value) + case "importsSource": + return setEnum(&o.ImportsSource, value, + ImportsSourceOff, + ImportsSourceGopls, + ImportsSourceGoimports) case "matcher": return setEnum(&o.Matcher, value, Fuzzy, @@ -1033,9 +1052,7 @@ func (o *Options) setOne(name string, value any) error { o.Codelenses = make(map[CodeLensSource]bool) } o.Codelenses = maps.Clone(o.Codelenses) - for source, enabled := range lensOverrides { - o.Codelenses[source] = enabled - } + maps.Copy(o.Codelenses, lensOverrides) if name == "codelens" { return deprecatedError("codelenses") diff --git a/internal/imports/fix.go b/internal/imports/fix.go index 5ae576977a2..b1fac90fff9 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -927,7 +927,7 @@ type ProcessEnv struct { WorkingDir string // If Logf is non-nil, debug logging is enabled through this function. - Logf func(format string, args ...interface{}) + Logf func(format string, args ...any) // If set, ModCache holds a shared cache of directory info to use across // multiple ProcessEnvs. From 6efe0f4b404b25e02999c3e34db08771f855fc28 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 23 Dec 2024 17:19:46 -0500 Subject: [PATCH 018/309] internal/astutil/cursor: Cursor.Ancestors iterator This CL adds an iterator for the strict ancestors (transitive parents) of the current node. It accepts a type filter. Also, use it in two places. This may slow down Cursor.Stack slightly, but see comments in benchmark for justification. The basic Cursor traversal is still competitive so long as Stack or Ancestors are called sparingly. BenchmarkInspectCalls/Preorder-8 12069 106995 ns/op BenchmarkInspectCalls/WithStack-8 7447 153992 ns/op BenchmarkInspectCalls/CursorStack-8 2545 460433 ns/op (was 178907: 2.6x increase) BenchmarkInspectCalls/Cursor-8 12225 99472 ns/op BenchmarkInspectCalls/CursorAncestors-8 3249 350503 ns/op (=3x WithStack) Change-Id: I79941423888028a622b20b7ab63b37f8435dce33 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641435 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/hostport/hostport.go | 3 +- gopls/internal/analysis/modernize/bloop.go | 26 ++++--------- internal/astutil/cursor/cursor.go | 38 +++++++++++++------ internal/astutil/cursor/cursor_test.go | 39 ++++++++++++++++++++ 4 files changed, 74 insertions(+), 32 deletions(-) diff --git a/gopls/internal/analysis/hostport/hostport.go b/gopls/internal/analysis/hostport/hostport.go index bf3b761b840..a7030ae116f 100644 --- a/gopls/internal/analysis/hostport/hostport.go +++ b/gopls/internal/analysis/hostport/hostport.go @@ -164,8 +164,7 @@ func run(pass *analysis.Pass) (any, error) { // Search for decl of addrVar within common ancestor of addrVar and Dial call. if addrVar, ok := info.Uses[address].(*types.Var); ok { pos := addrVar.Pos() - // TODO(adonovan): use Cursor.Ancestors iterator when available. - for _, curAncestor := range curCall.Stack(nil) { + for curAncestor := range curCall.Ancestors() { if curIdent, ok := curAncestor.FindPos(pos, pos); ok { // curIdent is the declaring ast.Ident of addr. switch parent := curIdent.Parent().Node().(type) { diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 9c88eb7257d..2f004c7ffb2 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -36,12 +36,12 @@ func bloop(pass *analysis.Pass) { // edits computes the text edits for a matched for/range loop // at the specified cursor. b is the *testing.B value, and // (start, end) is the portion using b.N to delete. - edits := func(cur cursor.Cursor, b ast.Expr, start, end token.Pos) (edits []analysis.TextEdit) { + edits := func(curLoop cursor.Cursor, b ast.Expr, start, end token.Pos) (edits []analysis.TextEdit) { + curFn, _ := enclosingFunc(curLoop) // Within the same function, delete all calls to // b.{Start,Stop,Timer} that precede the loop. filter := []ast.Node{(*ast.ExprStmt)(nil), (*ast.FuncLit)(nil)} - fn, _ := enclosingFunc(cur) - fn.Inspect(filter, func(cur cursor.Cursor, push bool) (descend bool) { + curFn.Inspect(filter, func(cur cursor.Cursor, push bool) (descend bool) { if push { node := cur.Node() if is[*ast.FuncLit](node) { @@ -162,22 +162,10 @@ func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { } // enclosingFunc returns the cursor for the innermost Func{Decl,Lit} -// that encloses (or is) c, if any. -// -// TODO(adonovan): consider adding: -// -// func (Cursor) AnyEnclosing(filter ...ast.Node) (Cursor bool) -// func (Cursor) Enclosing[N ast.Node]() (Cursor, bool) -// -// See comments at [cursor.Cursor.Stack]. +// that encloses c, if any. func enclosingFunc(c cursor.Cursor) (cursor.Cursor, bool) { - for { - switch c.Node().(type) { - case *ast.FuncLit, *ast.FuncDecl: - return c, true - case nil: - return cursor.Cursor{}, false - } - c = c.Parent() + for curAncestor := range c.Ancestors((*ast.FuncLit)(nil), (*ast.FuncDecl)(nil)) { + return curAncestor, true } + return cursor.Cursor{}, false } diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 8d53f8eeb77..89dd641c420 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -157,13 +157,6 @@ func (c Cursor) Inspect(types []ast.Node, f func(c Cursor, push bool) (descend b // must be empty. // // Stack must not be called on the Root node. -// -// TODO(adonovan): perhaps this should be replaced by: -// -// func (Cursor) Ancestors(filter []ast.Node) iter.Seq[Cursor] -// -// returning a filtering iterator up the parent chain. -// This finesses the question of allocation entirely. func (c Cursor) Stack(stack []Cursor) []Cursor { if len(stack) > 0 { panic("stack is non-empty") @@ -172,14 +165,37 @@ func (c Cursor) Stack(stack []Cursor) []Cursor { panic("Cursor.Stack called on Root node") } - events := c.events() - for i := c.index; i >= 0; i = events[i].parent { - stack = append(stack, Cursor{c.in, i}) - } + stack = append(stack, c) + stack = slices.AppendSeq(stack, c.Ancestors()) slices.Reverse(stack) return stack } +// Ancestors returns an iterator over the ancestors of the current +// node, starting with [Cursor.Parent]. +// +// Ancestors must not be called on the Root node (whose [Cursor.Node] returns nil). +// +// The types argument, if non-empty, enables type-based filtering of +// events: the sequence includes only ancestors whose type matches an +// element of the types slice. +func (c Cursor) Ancestors(types ...ast.Node) iter.Seq[Cursor] { + if c.index < 0 { + panic("Cursor.Ancestors called on Root node") + } + + mask := maskOf(types) + + return func(yield func(Cursor) bool) { + events := c.events() + for i := events[c.index].parent; i >= 0; i = events[i].parent { + if events[i].typ&mask != 0 && !yield(Cursor{c.in, i}) { + break + } + } + } +} + // Parent returns the parent of the current node. // // Parent must not be called on the Root node (whose [Cursor.Node] returns nil). diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index e578fa300a6..06cd358c22e 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -15,6 +15,7 @@ import ( "iter" "log" "path/filepath" + "reflect" "slices" "strings" "testing" @@ -152,6 +153,13 @@ func g() { if got, want := fmt.Sprint(stack), "[*ast.File *ast.FuncDecl *ast.BlockStmt *ast.ExprStmt *ast.CallExpr]"; got != want { t.Errorf("curCall.Stack() = %q, want %q", got, want) } + + // Ancestors = Reverse(Stack[:last]). + stack = stack[:len(stack)-1] + slices.Reverse(stack) + if got, want := slices.Collect(curCall.Ancestors()), stack; !reflect.DeepEqual(got, want) { + t.Errorf("Ancestors = %v, Reverse(Stack - last element) = %v", got, want) + } } // nested Inspect traversal @@ -381,6 +389,15 @@ func BenchmarkInspectCalls(b *testing.B) { // And if the calls to Stack are very selective, // or are replaced by 2 calls to Parent, it runs // 27% faster than WithStack. + // + // But the purpose of inspect.WithStack is not to obtain the + // stack on every node, but to perform a traversal in which it + // one as the _option_ to access the stack if it should be + // needed, but the need is rare and usually only for a small + // portion. Arguably, because Cursor traversals always + // provide, at no extra cost, the option to access the + // complete stack, the right comparison is the plain Cursor + // benchmark below. b.Run("CursorStack", func(b *testing.B) { var ncalls int for range b.N { @@ -392,6 +409,28 @@ func BenchmarkInspectCalls(b *testing.B) { } } }) + + b.Run("Cursor", func(b *testing.B) { + var ncalls int + for range b.N { + for cur := range cursor.Root(inspect).Preorder(callExprs...) { + _ = cur.Node().(*ast.CallExpr) + ncalls++ + } + } + }) + + b.Run("CursorAncestors", func(b *testing.B) { + var ncalls int + for range b.N { + for cur := range cursor.Root(inspect).Preorder(callExprs...) { + _ = cur.Node().(*ast.CallExpr) + for range cur.Ancestors() { + } + ncalls++ + } + } + }) } // This benchmark compares methods for finding a known node in a tree. From 1b796a93b9c5e1f507618de1d18d5908df5e3702 Mon Sep 17 00:00:00 2001 From: Tim King Date: Thu, 9 Jan 2025 11:34:37 -0800 Subject: [PATCH 019/309] go/ssa: removing termList type This is cleanup to remove the internal termList type. Some additional minor refactoring so underIs takes a Type instead of a list of terms. Change-Id: I934b16bbf94f1d62762a5622a85ceea22f1c2108 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641835 Auto-Submit: Tim King LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- go/ssa/builder.go | 2 +- go/ssa/const.go | 4 ++-- go/ssa/coretype.go | 59 +++++++++++++++++++++------------------------- go/ssa/sanity.go | 4 ++-- go/ssa/util.go | 13 +++++----- 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/go/ssa/builder.go b/go/ssa/builder.go index b109fbf3cd3..d2407e62fbd 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -856,7 +856,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value { if recv, ok := types.Unalias(sel.recv).(*types.TypeParam); ok { // Emit a nil check if any possible instantiation of the // type parameter is an interface type. - if typeSetOf(recv).Len() > 0 { + if len(typeSetOf(recv)) > 0 { // recv has a concrete term its typeset. // So it cannot be instantiated as an interface. // diff --git a/go/ssa/const.go b/go/ssa/const.go index 764b73529e3..91ed6f28647 100644 --- a/go/ssa/const.go +++ b/go/ssa/const.go @@ -45,7 +45,7 @@ func soleTypeKind(typ types.Type) types.BasicInfo { // Candidates (perhaps all) are eliminated during the type-set // iteration, which executes at least once. state := types.IsBoolean | types.IsInteger | types.IsString - underIs(typeSetOf(typ), func(ut types.Type) bool { + underIs(typ, func(ut types.Type) bool { var c types.BasicInfo if t, ok := ut.(*types.Basic); ok { c = t.Info() @@ -126,7 +126,7 @@ func (c *Const) IsNil() bool { // nillable reports whether *new(T) == nil is legal for type T. func nillable(t types.Type) bool { if typeparams.IsTypeParam(t) { - return underIs(typeSetOf(t), func(u types.Type) bool { + return underIs(t, func(u types.Type) bool { // empty type set (u==nil) => any underlying types => not nillable return u != nil && nillable(u) }) diff --git a/go/ssa/coretype.go b/go/ssa/coretype.go index d937134227d..082f8998b45 100644 --- a/go/ssa/coretype.go +++ b/go/ssa/coretype.go @@ -22,30 +22,24 @@ func isBytestring(T types.Type) bool { return false } - tset := typeSetOf(U) - if tset.Len() != 2 { - return false - } hasBytes, hasString := false, false - underIs(tset, func(t types.Type) bool { + ok := underIs(U, func(t types.Type) bool { switch { case isString(t): hasString = true + return true case isByteSlice(t): hasBytes = true + return true + default: + return false } - return hasBytes || hasString }) - return hasBytes && hasString + return ok && hasBytes && hasString } -// termList is a list of types. -type termList []*types.Term // type terms of the type set -func (s termList) Len() int { return len(s) } -func (s termList) At(i int) types.Type { return s[i].Type() } - -// typeSetOf returns the type set of typ. Returns an empty typeset on an error. -func typeSetOf(typ types.Type) termList { +// typeSetOf returns the type set of typ as a normalized term set. Returns an empty set on an error. +func typeSetOf(typ types.Type) []*types.Term { // This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on. var terms []*types.Term var err error @@ -65,20 +59,21 @@ func typeSetOf(typ types.Type) termList { } if err != nil { - return termList(nil) + return nil } - return termList(terms) + return terms } -// underIs calls f with the underlying types of the specific type terms -// of s and reports whether all calls to f returned true. If there are -// no specific terms, underIs returns the result of f(nil). -func underIs(s termList, f func(types.Type) bool) bool { - if s.Len() == 0 { +// underIs calls f with the underlying types of the type terms +// of the type set of typ and reports whether all calls to f returned true. +// If there are no specific terms, underIs returns the result of f(nil). +func underIs(typ types.Type, f func(types.Type) bool) bool { + s := typeSetOf(typ) + if len(s) == 0 { return f(nil) } - for i := 0; i < s.Len(); i++ { - u := s.At(i).Underlying() + for _, t := range s { + u := t.Type().Underlying() if !f(u) { return false } @@ -87,7 +82,7 @@ func underIs(s termList, f func(types.Type) bool) bool { } // indexType returns the element type and index mode of a IndexExpr over a type. -// It returns (nil, invalid) if the type is not indexable; this should never occur in a well-typed program. +// It returns an invalid mode if the type is not indexable; this should never occur in a well-typed program. func indexType(typ types.Type) (types.Type, indexMode) { switch U := typ.Underlying().(type) { case *types.Array: @@ -104,22 +99,22 @@ func indexType(typ types.Type) (types.Type, indexMode) { return tByte, ixValue // must be a string case *types.Interface: tset := typeSetOf(U) - if tset.Len() == 0 { + if len(tset) == 0 { return nil, ixInvalid // no underlying terms or error is empty. } - - elem, mode := indexType(tset.At(0)) - for i := 1; i < tset.Len() && mode != ixInvalid; i++ { - e, m := indexType(tset.At(i)) + elem, mode := indexType(tset[0].Type()) + for _, t := range tset[1:] { + e, m := indexType(t.Type()) if !types.Identical(elem, e) { // if type checked, just a sanity check return nil, ixInvalid } // Update the mode to the most constrained address type. mode = mode.meet(m) + if mode == ixInvalid { + return nil, ixInvalid // fast exit + } } - if mode != ixInvalid { - return elem, mode - } + return elem, mode } return nil, ixInvalid } diff --git a/go/ssa/sanity.go b/go/ssa/sanity.go index ef2928e3b74..e35e4d79357 100644 --- a/go/ssa/sanity.go +++ b/go/ssa/sanity.go @@ -142,8 +142,8 @@ func (s *sanity) checkInstr(idx int, instr Instruction) { case *ChangeType: case *SliceToArrayPointer: case *Convert: - if from := instr.X.Type(); !isBasicConvTypes(typeSetOf(from)) { - if to := instr.Type(); !isBasicConvTypes(typeSetOf(to)) { + if from := instr.X.Type(); !isBasicConvTypes(from) { + if to := instr.Type(); !isBasicConvTypes(to) { s.errorf("convert %s -> %s: at least one type must be basic (or all basic, []byte, or []rune)", from, to) } } diff --git a/go/ssa/util.go b/go/ssa/util.go index aa070eacdcb..4a056cbe0bd 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -85,21 +85,22 @@ func isRuneSlice(t types.Type) bool { return false } -// isBasicConvTypes returns true when a type set can be -// one side of a Convert operation. This is when: +// isBasicConvTypes returns true when the type set of a type +// can be one side of a Convert operation. This is when: // - All are basic, []byte, or []rune. // - At least 1 is basic. // - At most 1 is []byte or []rune. -func isBasicConvTypes(tset termList) bool { - basics := 0 - all := underIs(tset, func(t types.Type) bool { +func isBasicConvTypes(typ types.Type) bool { + basics, cnt := 0, 0 + ok := underIs(typ, func(t types.Type) bool { + cnt++ if isBasic(t) { basics++ return true } return isByteSlice(t) || isRuneSlice(t) }) - return all && basics >= 1 && tset.Len()-basics <= 1 + return ok && basics >= 1 && cnt-basics <= 1 } // isPointer reports whether t's underlying type is a pointer. From c1a7fcfc101d2fdf16ba2d1175509e2671cb2bcc Mon Sep 17 00:00:00 2001 From: xzb <2598514867@qq.com> Date: Tue, 7 Jan 2025 20:30:46 +0000 Subject: [PATCH 020/309] go/analysis/passes/printf: extract operation parsing logic into tools/internal/fmtstr This CL made a refactor that extract operation parsing logic into tools/internal/fmtstr package, so they can be used uniformly by printf-analyzer for type checking, DocumentHighlight, and future possibility for SemanticHighlight and rich Hover information with little effort. Previously, the code responsible for parsing and validating printf-style format strings was spread inline and blend together in printf.go, in order to use it for documenthighligt/hover, this CL extends formatState by encoding posRange and metainfo for every sub-item of a operation (flag, width, precision, verb), and only expose those field, by that way callers can compute necessary information from them for various usages. Updates golang/go#70050 Change-Id: I8337ad0bdf9f5c1aa301d2e9155b2d4938cf4a7c GitHub-Last-Rev: 8e576345020d79d5c5534f8e66c3eee5f7bb2edd GitHub-Pull-Request: golang/tools#547 Reviewed-on: https://go-review.googlesource.com/c/tools/+/632598 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Reviewed-by: Robert Findley Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan --- go/analysis/passes/printf/printf.go | 333 +++++----------- go/analysis/passes/printf/testdata/src/a/a.go | 4 +- internal/fmtstr/main.go | 94 +++++ internal/fmtstr/parse.go | 370 ++++++++++++++++++ 4 files changed, 562 insertions(+), 239 deletions(-) create mode 100644 internal/fmtstr/main.go create mode 100644 internal/fmtstr/parse.go diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go index 95c4bbaa98a..b95e2fd6f1a 100644 --- a/go/analysis/passes/printf/printf.go +++ b/go/analysis/passes/printf/printf.go @@ -5,7 +5,6 @@ package printf import ( - "bytes" _ "embed" "fmt" "go/ast" @@ -15,9 +14,7 @@ import ( "reflect" "regexp" "sort" - "strconv" "strings" - "unicode/utf8" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -25,6 +22,7 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/fmtstr" "golang.org/x/tools/internal/typeparams" ) @@ -421,9 +419,9 @@ func checkCall(pass *analysis.Pass) { fn, kind := printfNameAndKind(pass, call) switch kind { case KindPrintf, KindErrorf: - checkPrintf(pass, kind, call, fn) + checkPrintf(pass, kind, call, fn.FullName()) case KindPrint: - checkPrint(pass, call, fn) + checkPrint(pass, call, fn.FullName()) } }) } @@ -485,26 +483,8 @@ func isFormatter(typ types.Type) bool { types.Identical(sig.Params().At(1).Type(), types.Typ[types.Rune]) } -// formatState holds the parsed representation of a printf directive such as "%3.*[4]d". -// It is constructed by parsePrintfVerb. -type formatState struct { - verb rune // the format verb: 'd' for "%d" - format string // the full format directive from % through verb, "%.3d". - name string // Printf, Sprintf etc. - flags []byte // the list of # + etc. - argNums []int // the successive argument numbers that are consumed, adjusted to refer to actual arg in call - firstArg int // Index of first argument after the format in the Printf call. - // Used only during parse. - pass *analysis.Pass - call *ast.CallExpr - argNum int // Which argument we're expecting to format now. - hasIndex bool // Whether the argument is indexed. - indexPending bool // Whether we have an indexed argument that has not resolved. - nbytes int // number of bytes of the format string consumed. -} - // checkPrintf checks a call to a formatted print routine such as Printf. -func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, fn *types.Func) { +func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, name string) { idx := formatStringIndex(pass, call) if idx < 0 || idx >= len(call.Args) { return @@ -523,7 +503,7 @@ func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, fn *types.F Pos: formatArg.Pos(), End: formatArg.End(), Message: fmt.Sprintf("non-constant format string in call to %s", - fn.FullName()), + name), SuggestedFixes: []analysis.SuggestedFix{{ Message: `Insert "%s" format string`, TextEdits: []analysis.TextEdit{{ @@ -540,49 +520,46 @@ func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, fn *types.F firstArg := idx + 1 // Arguments are immediately after format string. if !strings.Contains(format, "%") { if len(call.Args) > firstArg { - pass.Reportf(call.Lparen, "%s call has arguments but no formatting directives", fn.FullName()) + pass.Reportf(call.Lparen, "%s call has arguments but no formatting directives", name) } return } - // Hard part: check formats against args. - argNum := firstArg - maxArgNum := firstArg + + // Pass the string constant value so + // fmt.Sprintf("%"+("s"), "hi", 3) can be reported as + // "fmt.Sprintf call needs 1 arg but has 2 args". + operations, err := fmtstr.Parse(format, idx) + if err != nil { + // All error messages are in predicate form ("call has a problem") + // so that they may be affixed into a subject ("log.Printf "). + pass.ReportRangef(call.Args[idx], "%s %s", name, err) + return + } + + // index of the highest used index. + maxArgIndex := firstArg - 1 anyIndex := false - for i, w := 0, 0; i < len(format); i += w { - w = 1 - if format[i] != '%' { - continue - } - state := parsePrintfVerb(pass, call, fn.FullName(), format[i:], firstArg, argNum) - if state == nil { - return + // Check formats against args. + for _, operation := range operations { + if operation.Prec.Index != -1 || + operation.Width.Index != -1 || + operation.Verb.Index != -1 { + anyIndex = true } - w = len(state.format) - if !okPrintfArg(pass, call, state) { // One error per format is enough. + if !okPrintfArg(pass, call, &maxArgIndex, firstArg, name, operation) { + // One error per format is enough. return } - if state.hasIndex { - anyIndex = true - } - if state.verb == 'w' { + if operation.Verb.Verb == 'w' { switch kind { case KindNone, KindPrint, KindPrintf: - pass.Reportf(call.Pos(), "%s does not support error-wrapping directive %%w", state.name) + pass.Reportf(call.Pos(), "%s does not support error-wrapping directive %%w", name) return } } - if len(state.argNums) > 0 { - // Continue with the next sequential argument. - argNum = state.argNums[len(state.argNums)-1] + 1 - } - for _, n := range state.argNums { - if n >= maxArgNum { - maxArgNum = n + 1 - } - } } // Dotdotdot is hard. - if call.Ellipsis.IsValid() && maxArgNum >= len(call.Args)-1 { + if call.Ellipsis.IsValid() && maxArgIndex >= len(call.Args)-2 { return } // If any formats are indexed, extra arguments are ignored. @@ -590,147 +567,13 @@ func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, fn *types.F return } // There should be no leftover arguments. - if maxArgNum != len(call.Args) { - expect := maxArgNum - firstArg + if maxArgIndex+1 < len(call.Args) { + expect := maxArgIndex + 1 - firstArg numArgs := len(call.Args) - firstArg - pass.ReportRangef(call, "%s call needs %v but has %v", fn.FullName(), count(expect, "arg"), count(numArgs, "arg")) - } -} - -// parseFlags accepts any printf flags. -func (s *formatState) parseFlags() { - for s.nbytes < len(s.format) { - switch c := s.format[s.nbytes]; c { - case '#', '0', '+', '-', ' ': - s.flags = append(s.flags, c) - s.nbytes++ - default: - return - } + pass.ReportRangef(call, "%s call needs %v but has %v", name, count(expect, "arg"), count(numArgs, "arg")) } } -// scanNum advances through a decimal number if present. -func (s *formatState) scanNum() { - for ; s.nbytes < len(s.format); s.nbytes++ { - c := s.format[s.nbytes] - if c < '0' || '9' < c { - return - } - } -} - -// parseIndex scans an index expression. It returns false if there is a syntax error. -func (s *formatState) parseIndex() bool { - if s.nbytes == len(s.format) || s.format[s.nbytes] != '[' { - return true - } - // Argument index present. - s.nbytes++ // skip '[' - start := s.nbytes - s.scanNum() - ok := true - if s.nbytes == len(s.format) || s.nbytes == start || s.format[s.nbytes] != ']' { - ok = false // syntax error is either missing "]" or invalid index. - s.nbytes = strings.Index(s.format[start:], "]") - if s.nbytes < 0 { - s.pass.ReportRangef(s.call, "%s format %s is missing closing ]", s.name, s.format) - return false - } - s.nbytes = s.nbytes + start - } - arg32, err := strconv.ParseInt(s.format[start:s.nbytes], 10, 32) - if err != nil || !ok || arg32 <= 0 || arg32 > int64(len(s.call.Args)-s.firstArg) { - s.pass.ReportRangef(s.call, "%s format has invalid argument index [%s]", s.name, s.format[start:s.nbytes]) - return false - } - s.nbytes++ // skip ']' - arg := int(arg32) - arg += s.firstArg - 1 // We want to zero-index the actual arguments. - s.argNum = arg - s.hasIndex = true - s.indexPending = true - return true -} - -// parseNum scans a width or precision (or *). It returns false if there's a bad index expression. -func (s *formatState) parseNum() bool { - if s.nbytes < len(s.format) && s.format[s.nbytes] == '*' { - if s.indexPending { // Absorb it. - s.indexPending = false - } - s.nbytes++ - s.argNums = append(s.argNums, s.argNum) - s.argNum++ - } else { - s.scanNum() - } - return true -} - -// parsePrecision scans for a precision. It returns false if there's a bad index expression. -func (s *formatState) parsePrecision() bool { - // If there's a period, there may be a precision. - if s.nbytes < len(s.format) && s.format[s.nbytes] == '.' { - s.flags = append(s.flags, '.') // Treat precision as a flag. - s.nbytes++ - if !s.parseIndex() { - return false - } - if !s.parseNum() { - return false - } - } - return true -} - -// parsePrintfVerb looks the formatting directive that begins the format string -// and returns a formatState that encodes what the directive wants, without looking -// at the actual arguments present in the call. The result is nil if there is an error. -func parsePrintfVerb(pass *analysis.Pass, call *ast.CallExpr, name, format string, firstArg, argNum int) *formatState { - state := &formatState{ - format: format, - name: name, - flags: make([]byte, 0, 5), - argNum: argNum, - argNums: make([]int, 0, 1), - nbytes: 1, // There's guaranteed to be a percent sign. - firstArg: firstArg, - pass: pass, - call: call, - } - // There may be flags. - state.parseFlags() - // There may be an index. - if !state.parseIndex() { - return nil - } - // There may be a width. - if !state.parseNum() { - return nil - } - // There may be a precision. - if !state.parsePrecision() { - return nil - } - // Now a verb, possibly prefixed by an index (which we may already have). - if !state.indexPending && !state.parseIndex() { - return nil - } - if state.nbytes == len(state.format) { - pass.ReportRangef(call.Fun, "%s format %s is missing verb at end of string", name, state.format) - return nil - } - verb, w := utf8.DecodeRuneInString(state.format[state.nbytes:]) - state.verb = verb - state.nbytes += w - if verb != '%' { - state.argNums = append(state.argNums, state.argNum) - } - state.format = state.format[:state.nbytes] - return state -} - // printfArgType encodes the types of expressions a printf verb accepts. It is a bitmask. type printfArgType int @@ -791,79 +634,96 @@ var printVerbs = []printVerb{ {'X', sharpNumFlag, argRune | argInt | argString | argPointer | argFloat | argComplex}, } -// okPrintfArg compares the formatState to the arguments actually present, -// reporting any discrepancies it can discern. If the final argument is ellipsissed, -// there's little it can do for that. -func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, state *formatState) (ok bool) { +// okPrintfArg compares the operation to the arguments actually present, +// reporting any discrepancies it can discern, maxArgIndex was the index of the highest used index. +// If the final argument is ellipsissed, there's little it can do for that. +func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, maxArgIndex *int, firstArg int, name string, operation *fmtstr.Operation) (ok bool) { + verb := operation.Verb.Verb var v printVerb found := false // Linear scan is fast enough for a small list. for _, v = range printVerbs { - if v.verb == state.verb { + if v.verb == verb { found = true break } } - // Could current arg implement fmt.Formatter? + // Could verb's arg implement fmt.Formatter? // Skip check for the %w verb, which requires an error. formatter := false - if v.typ != argError && state.argNum < len(call.Args) { - if tv, ok := pass.TypesInfo.Types[call.Args[state.argNum]]; ok { + if v.typ != argError && operation.Verb.ArgIndex < len(call.Args) { + if tv, ok := pass.TypesInfo.Types[call.Args[operation.Verb.ArgIndex]]; ok { formatter = isFormatter(tv.Type) } } if !formatter { if !found { - pass.ReportRangef(call, "%s format %s has unknown verb %c", state.name, state.format, state.verb) + pass.ReportRangef(call, "%s format %s has unknown verb %c", name, operation.Text, verb) return false } - for _, flag := range state.flags { + for _, flag := range operation.Flags { // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11. // See issues 23598 and 23605. if flag == '0' { continue } if !strings.ContainsRune(v.flags, rune(flag)) { - pass.ReportRangef(call, "%s format %s has unrecognized flag %c", state.name, state.format, flag) + pass.ReportRangef(call, "%s format %s has unrecognized flag %c", name, operation.Text, flag) return false } } } - // Verb is good. If len(state.argNums)>trueArgs, we have something like %.*s and all - // but the final arg must be an integer. - trueArgs := 1 - if state.verb == '%' { - trueArgs = 0 + + var argIndexes []int + // First check for *. + if operation.Width.Dynamic != -1 { + argIndexes = append(argIndexes, operation.Width.Dynamic) + } + if operation.Prec.Dynamic != -1 { + argIndexes = append(argIndexes, operation.Prec.Dynamic) } - nargs := len(state.argNums) - for i := 0; i < nargs-trueArgs; i++ { - argNum := state.argNums[i] - if !argCanBeChecked(pass, call, i, state) { + // If len(argIndexes)>0, we have something like %.*s and all + // indexes in argIndexes must be an integer. + for _, argIndex := range argIndexes { + if !argCanBeChecked(pass, call, argIndex, firstArg, operation, name) { return } - arg := call.Args[argNum] + arg := call.Args[argIndex] if reason, ok := matchArgType(pass, argInt, arg); !ok { details := "" if reason != "" { details = " (" + reason + ")" } - pass.ReportRangef(call, "%s format %s uses non-int %s%s as argument of *", state.name, state.format, analysisinternal.Format(pass.Fset, arg), details) + pass.ReportRangef(call, "%s format %s uses non-int %s%s as argument of *", name, operation.Text, analysisinternal.Format(pass.Fset, arg), details) return false } } - if state.verb == '%' || formatter { + // Collect to update maxArgNum in one loop. + if operation.Verb.ArgIndex != -1 && verb != '%' { + argIndexes = append(argIndexes, operation.Verb.ArgIndex) + } + for _, index := range argIndexes { + *maxArgIndex = max(*maxArgIndex, index) + } + + // Special case for '%', go will print "fmt.Printf("%10.2%%dhello", 4)" + // as "%4hello", discard any runes between the two '%'s, and treat the verb '%' + // as an ordinary rune, so early return to skip the type check. + if verb == '%' || formatter { return true } - argNum := state.argNums[len(state.argNums)-1] - if !argCanBeChecked(pass, call, len(state.argNums)-1, state) { + + // Now check verb's type. + verbArgIndex := operation.Verb.ArgIndex + if !argCanBeChecked(pass, call, verbArgIndex, firstArg, operation, name) { return false } - arg := call.Args[argNum] - if isFunctionValue(pass, arg) && state.verb != 'p' && state.verb != 'T' { - pass.ReportRangef(call, "%s format %s arg %s is a func value, not called", state.name, state.format, analysisinternal.Format(pass.Fset, arg)) + arg := call.Args[verbArgIndex] + if isFunctionValue(pass, arg) && verb != 'p' && verb != 'T' { + pass.ReportRangef(call, "%s format %s arg %s is a func value, not called", name, operation.Text, analysisinternal.Format(pass.Fset, arg)) return false } if reason, ok := matchArgType(pass, v.typ, arg); !ok { @@ -875,12 +735,12 @@ func okPrintfArg(pass *analysis.Pass, call *ast.CallExpr, state *formatState) (o if reason != "" { details = " (" + reason + ")" } - pass.ReportRangef(call, "%s format %s has arg %s of wrong type %s%s", state.name, state.format, analysisinternal.Format(pass.Fset, arg), typeString, details) + pass.ReportRangef(call, "%s format %s has arg %s of wrong type %s%s", name, operation.Text, analysisinternal.Format(pass.Fset, arg), typeString, details) return false } - if v.typ&argString != 0 && v.verb != 'T' && !bytes.Contains(state.flags, []byte{'#'}) { + if v.typ&argString != 0 && v.verb != 'T' && !strings.Contains(operation.Flags, "#") { if methodName, ok := recursiveStringer(pass, arg); ok { - pass.ReportRangef(call, "%s format %s with arg %s causes recursive %s method call", state.name, state.format, analysisinternal.Format(pass.Fset, arg), methodName) + pass.ReportRangef(call, "%s format %s with arg %s causes recursive %s method call", name, operation.Text, analysisinternal.Format(pass.Fset, arg), methodName) return false } } @@ -964,25 +824,24 @@ func isFunctionValue(pass *analysis.Pass, e ast.Expr) bool { // argCanBeChecked reports whether the specified argument is statically present; // it may be beyond the list of arguments or in a terminal slice... argument, which // means we can't see it. -func argCanBeChecked(pass *analysis.Pass, call *ast.CallExpr, formatArg int, state *formatState) bool { - argNum := state.argNums[formatArg] - if argNum <= 0 { +func argCanBeChecked(pass *analysis.Pass, call *ast.CallExpr, argIndex, firstArg int, operation *fmtstr.Operation, name string) bool { + if argIndex <= 0 { // Shouldn't happen, so catch it with prejudice. - panic("negative arg num") + panic("negative argIndex") } - if argNum < len(call.Args)-1 { + if argIndex < len(call.Args)-1 { return true // Always OK. } if call.Ellipsis.IsValid() { return false // We just can't tell; there could be many more arguments. } - if argNum < len(call.Args) { + if argIndex < len(call.Args) { return true } // There are bad indexes in the format or there are fewer arguments than the format needs. // This is the argument number relative to the format: Printf("%s", "hi") will give 1 for the "hi". - arg := argNum - state.firstArg + 1 // People think of arguments as 1-indexed. - pass.ReportRangef(call, "%s format %s reads arg #%d, but call has %v", state.name, state.format, arg, count(len(call.Args)-state.firstArg, "arg")) + arg := argIndex - firstArg + 1 // People think of arguments as 1-indexed. + pass.ReportRangef(call, "%s format %s reads arg #%d, but call has %v", name, operation.Text, arg, count(len(call.Args)-firstArg, "arg")) return false } @@ -999,7 +858,7 @@ const ( ) // checkPrint checks a call to an unformatted print routine such as Println. -func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) { +func checkPrint(pass *analysis.Pass, call *ast.CallExpr, name string) { firstArg := 0 typ := pass.TypesInfo.Types[call.Fun].Type if typ == nil { @@ -1033,7 +892,7 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) { if sel, ok := call.Args[0].(*ast.SelectorExpr); ok { if x, ok := sel.X.(*ast.Ident); ok { if x.Name == "os" && strings.HasPrefix(sel.Sel.Name, "Std") { - pass.ReportRangef(call, "%s does not take io.Writer but has first arg %s", fn.FullName(), analysisinternal.Format(pass.Fset, call.Args[0])) + pass.ReportRangef(call, "%s does not take io.Writer but has first arg %s", name, analysisinternal.Format(pass.Fset, call.Args[0])) } } } @@ -1047,25 +906,25 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) { if strings.Contains(s, "%") { m := printFormatRE.FindStringSubmatch(s) if m != nil { - pass.ReportRangef(call, "%s call has possible Printf formatting directive %s", fn.FullName(), m[0]) + pass.ReportRangef(call, "%s call has possible Printf formatting directive %s", name, m[0]) } } } - if strings.HasSuffix(fn.Name(), "ln") { + if strings.HasSuffix(name, "ln") { // The last item, if a string, should not have a newline. arg = args[len(args)-1] if s, ok := stringConstantExpr(pass, arg); ok { if strings.HasSuffix(s, "\n") { - pass.ReportRangef(call, "%s arg list ends with redundant newline", fn.FullName()) + pass.ReportRangef(call, "%s arg list ends with redundant newline", name) } } } for _, arg := range args { if isFunctionValue(pass, arg) { - pass.ReportRangef(call, "%s arg %s is a func value, not called", fn.FullName(), analysisinternal.Format(pass.Fset, arg)) + pass.ReportRangef(call, "%s arg %s is a func value, not called", name, analysisinternal.Format(pass.Fset, arg)) } if methodName, ok := recursiveStringer(pass, arg); ok { - pass.ReportRangef(call, "%s arg %s causes recursive call to %s method", fn.FullName(), analysisinternal.Format(pass.Fset, arg), methodName) + pass.ReportRangef(call, "%s arg %s causes recursive call to %s method", name, analysisinternal.Format(pass.Fset, arg), methodName) } } } diff --git a/go/analysis/passes/printf/testdata/src/a/a.go b/go/analysis/passes/printf/testdata/src/a/a.go index 18b9e3be2b9..02ce425f8a3 100644 --- a/go/analysis/passes/printf/testdata/src/a/a.go +++ b/go/analysis/passes/printf/testdata/src/a/a.go @@ -212,8 +212,8 @@ func PrintfTests() { // Bad argument reorderings. Printf("%[xd", 3) // want `a.Printf format %\[xd is missing closing \]` Printf("%[x]d x", 3) // want `a.Printf format has invalid argument index \[x\]` - Printf("%[3]*s x", "hi", 2) // want `a.Printf format has invalid argument index \[3\]` - _ = fmt.Sprintf("%[3]d x", 2) // want `fmt.Sprintf format has invalid argument index \[3\]` + Printf("%[3]*s x", "hi", 2) // want `a.Printf format %\[3]\*s reads arg #3, but call has 2 args` + _ = fmt.Sprintf("%[3]d x", 2) // want `fmt.Sprintf format %\[3]d reads arg #3, but call has 1 arg` Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // want `a.Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*` Printf("%[0]s x", "arg1") // want `a.Printf format has invalid argument index \[0\]` Printf("%[0]d x", 1) // want `a.Printf format has invalid argument index \[0\]` diff --git a/internal/fmtstr/main.go b/internal/fmtstr/main.go new file mode 100644 index 00000000000..7fcbfdbbf2c --- /dev/null +++ b/internal/fmtstr/main.go @@ -0,0 +1,94 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build ignore + +// The fmtstr command parses the format strings of calls to selected +// printf-like functions in the specified source file, and prints the +// formatting operations and their operands. +// +// It is intended only for debugging and is not a supported interface. +package main + +import ( + "flag" + "fmt" + "go/ast" + "go/parser" + "go/printer" + "go/token" + "log" + "strconv" + "strings" + + "golang.org/x/tools/internal/fmtstr" +) + +func main() { + log.SetPrefix("fmtstr: ") + log.SetFlags(0) + flag.Parse() + + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, flag.Args()[0], nil, 0) + if err != nil { + log.Fatal(err) + } + + functions := map[string]int{ + "fmt.Errorf": 0, + "fmt.Fprintf": 1, + "fmt.Printf": 0, + "fmt.Sprintf": 0, + "log.Printf": 0, + } + + ast.Inspect(f, func(n ast.Node) bool { + if call, ok := n.(*ast.CallExpr); ok && !call.Ellipsis.IsValid() { + if sel, ok := call.Fun.(*ast.SelectorExpr); ok && is[*ast.Ident](sel.X) { + name := sel.X.(*ast.Ident).Name + "." + sel.Sel.Name // e.g. "fmt.Printf" + if fmtstrIndex, ok := functions[name]; ok && + len(call.Args) > fmtstrIndex { + // Is it a string literal? + if fmtstrArg, ok := call.Args[fmtstrIndex].(*ast.BasicLit); ok && + fmtstrArg.Kind == token.STRING { + // Have fmt.Printf("format", ...) + format, _ := strconv.Unquote(fmtstrArg.Value) + + ops, err := fmtstr.Parse(format, 0) + if err != nil { + log.Printf("%s: %v", fset.Position(fmtstrArg.Pos()), err) + return true + } + + fmt.Printf("%s: %s(%s, ...)\n", + fset.Position(fmtstrArg.Pos()), + name, + fmtstrArg.Value) + for _, op := range ops { + // TODO(adonovan): show more detail. + fmt.Printf("\t%q\t%v\n", + op.Text, + formatNode(fset, call.Args[op.Verb.ArgIndex])) + } + } + } + } + } + return true + }) +} + +func is[T any](x any) bool { + _, ok := x.(T) + return ok +} + +func formatNode(fset *token.FileSet, n ast.Node) string { + var buf strings.Builder + if err := printer.Fprint(&buf, fset, n); err != nil { + return "" + } + return buf.String() +} diff --git a/internal/fmtstr/parse.go b/internal/fmtstr/parse.go new file mode 100644 index 00000000000..9ab264f45d6 --- /dev/null +++ b/internal/fmtstr/parse.go @@ -0,0 +1,370 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package fmtstr defines a parser for format strings as used by [fmt.Printf]. +package fmtstr + +import ( + "fmt" + "strconv" + "strings" + "unicode/utf8" +) + +// Operation holds the parsed representation of a printf operation such as "%3.*[4]d". +// It is constructed by [Parse]. +type Operation struct { + Text string // full text of the operation, e.g. "%[2]*.3d" + Verb Verb // verb specifier, guaranteed to exist, e.g., 'd' in '%[1]d' + Range Range // the range of Text within the overall format string + Flags string // formatting flags, e.g. "-0" + Width Size // width specifier, e.g., '3' in '%3d' + Prec Size // precision specifier, e.g., '.4' in '%.4f' +} + +// Size describes an optional width or precision in a format operation. +// It may represent no value, a literal number, an asterisk, or an indexed asterisk. +type Size struct { + // At most one of these two fields is non-negative. + Fixed int // e.g. 4 from "%4d", otherwise -1 + Dynamic int // index of argument providing dynamic size (e.g. %*d or %[3]*d), otherwise -1 + + Index int // If the width or precision uses an indexed argument (e.g. 2 in %[2]*d), this is the index, otherwise -1 + Range Range // position of the size specifier within the operation +} + +// Verb represents the verb character of a format operation (e.g., 'd', 's', 'f'). +// It also includes positional information and any explicit argument indexing. +type Verb struct { + Verb rune + Range Range // positional range of the verb in the format string + Index int // index of an indexed argument, (e.g. 2 in %[2]d), otherwise -1 + ArgIndex int // argument index (0-based) associated with this verb, relative to CallExpr +} + +// byte offsets of format string +type Range struct { + Start, End int +} + +// Parse takes a format string and its index in the printf-like call, +// parses out all format operations, returns a slice of parsed +// [Operation] which describes flags, width, precision, verb, and argument indexing, +// or an error if parsing fails. +// +// All error messages are in predicate form ("call has a problem") +// so that they may be affixed into a subject ("log.Printf "). +// +// The flags will only be a subset of ['#', '0', '+', '-', ' ']. +// It does not perform any validation of verbs, nor the +// existence of corresponding arguments (obviously it can't). The provided format string may differ +// from the one in CallExpr, such as a concatenated string or a string +// referred to by the argument in the CallExpr. +func Parse(format string, idx int) ([]*Operation, error) { + if !strings.Contains(format, "%") { + return nil, fmt.Errorf("call has arguments but no formatting directives") + } + + firstArg := idx + 1 // Arguments are immediately after format string. + argNum := firstArg + var operations []*Operation + for i, w := 0, 0; i < len(format); i += w { + w = 1 + if format[i] != '%' { + continue + } + state, err := parseOperation(format[i:], firstArg, argNum) + if err != nil { + return nil, err + } + + state.operation.addOffset(i) + operations = append(operations, state.operation) + + w = len(state.operation.Text) + // Do not waste an argument for '%'. + if state.operation.Verb.Verb != '%' { + argNum = state.argNum + 1 + } + } + return operations, nil +} + +// Internal parsing state to operation. +type state struct { + operation *Operation + firstArg int // index of the first argument after the format string + argNum int // which argument we're expecting to format now + hasIndex bool // whether the argument is indexed + index int // the encountered index + indexPos int // the encountered index's offset + indexPending bool // whether we have an indexed argument that has not resolved + nbytes int // number of bytes of the format string consumed +} + +// parseOperation parses one format operation starting at the given substring `format`, +// which should begin with '%'. It returns a fully populated state or an error +// if the operation is malformed. The firstArg and argNum parameters help determine how +// arguments map to this operation. +// +// Parse sequence: '%' -> flags -> {[N]* or width} -> .{[N]* or precision} -> [N] -> verb. +func parseOperation(format string, firstArg, argNum int) (*state, error) { + state := &state{ + operation: &Operation{ + Text: format, + Width: Size{ + Fixed: -1, + Dynamic: -1, + Index: -1, + }, + Prec: Size{ + Fixed: -1, + Dynamic: -1, + Index: -1, + }, + }, + firstArg: firstArg, + argNum: argNum, + hasIndex: false, + index: 0, + indexPos: 0, + indexPending: false, + nbytes: len("%"), // There's guaranteed to be a percent sign. + } + // There may be flags. + state.parseFlags() + // There may be an index. + if err := state.parseIndex(); err != nil { + return nil, err + } + // There may be a width. + state.parseSize(Width) + // There may be a precision. + if err := state.parsePrecision(); err != nil { + return nil, err + } + // Now a verb, possibly prefixed by an index (which we may already have). + if !state.indexPending { + if err := state.parseIndex(); err != nil { + return nil, err + } + } + if state.nbytes == len(state.operation.Text) { + return nil, fmt.Errorf("format %s is missing verb at end of string", state.operation.Text) + } + verb, w := utf8.DecodeRuneInString(state.operation.Text[state.nbytes:]) + + // Ensure there must be a verb. + if state.indexPending { + state.operation.Verb = Verb{ + Verb: verb, + Range: Range{ + Start: state.indexPos, + End: state.nbytes + w, + }, + Index: state.index, + ArgIndex: state.argNum, + } + } else { + state.operation.Verb = Verb{ + Verb: verb, + Range: Range{ + Start: state.nbytes, + End: state.nbytes + w, + }, + Index: -1, + ArgIndex: state.argNum, + } + } + + state.nbytes += w + state.operation.Text = state.operation.Text[:state.nbytes] + return state, nil +} + +// addOffset adjusts the recorded positions in Verb, Width, Prec, and the +// operation's overall Range to be relative to the position in the full format string. +func (s *Operation) addOffset(parsedLen int) { + s.Verb.Range.Start += parsedLen + s.Verb.Range.End += parsedLen + + s.Range.Start = parsedLen + s.Range.End = s.Verb.Range.End + + // one of Fixed or Dynamic is non-negative means existence. + if s.Prec.Fixed != -1 || s.Prec.Dynamic != -1 { + s.Prec.Range.Start += parsedLen + s.Prec.Range.End += parsedLen + } + if s.Width.Fixed != -1 || s.Width.Dynamic != -1 { + s.Width.Range.Start += parsedLen + s.Width.Range.End += parsedLen + } +} + +// parseFlags accepts any printf flags. +func (s *state) parseFlags() { + s.operation.Flags = prefixOf(s.operation.Text[s.nbytes:], "#0+- ") + s.nbytes += len(s.operation.Flags) +} + +// prefixOf returns the prefix of s composed only of runes from the specified set. +func prefixOf(s, set string) string { + rest := strings.TrimLeft(s, set) + return s[:len(s)-len(rest)] +} + +// parseIndex parses an argument index of the form "[n]" that can appear +// in a printf operation (e.g., "%[2]d"). Returns an error if syntax is +// malformed or index is invalid. +func (s *state) parseIndex() error { + if s.nbytes == len(s.operation.Text) || s.operation.Text[s.nbytes] != '[' { + return nil + } + // Argument index present. + s.nbytes++ // skip '[' + start := s.nbytes + if num, ok := s.scanNum(); ok { + // Later consumed/stored by a '*' or verb. + s.index = num + s.indexPos = start - 1 + } + + ok := true + if s.nbytes == len(s.operation.Text) || s.nbytes == start || s.operation.Text[s.nbytes] != ']' { + ok = false // syntax error is either missing "]" or invalid index. + s.nbytes = strings.Index(s.operation.Text[start:], "]") + if s.nbytes < 0 { + return fmt.Errorf("format %s is missing closing ]", s.operation.Text) + } + s.nbytes = s.nbytes + start + } + arg32, err := strconv.ParseInt(s.operation.Text[start:s.nbytes], 10, 32) + if err != nil || !ok || arg32 <= 0 { + return fmt.Errorf("format has invalid argument index [%s]", s.operation.Text[start:s.nbytes]) + } + + s.nbytes++ // skip ']' + arg := int(arg32) + arg += s.firstArg - 1 // We want to zero-index the actual arguments. + s.argNum = arg + s.hasIndex = true + s.indexPending = true + return nil +} + +// scanNum advances through a decimal number if present, which represents a [Size] or [Index]. +func (s *state) scanNum() (int, bool) { + start := s.nbytes + for ; s.nbytes < len(s.operation.Text); s.nbytes++ { + c := s.operation.Text[s.nbytes] + if c < '0' || '9' < c { + if start < s.nbytes { + num, _ := strconv.ParseInt(s.operation.Text[start:s.nbytes], 10, 32) + return int(num), true + } else { + return 0, false + } + } + } + return 0, false +} + +type sizeType int + +const ( + Width sizeType = iota + Precision +) + +// parseSize parses a width or precision specifier. It handles literal numeric +// values (e.g., "%3d"), asterisk values (e.g., "%*d"), or indexed asterisk values (e.g., "%[2]*d"). +func (s *state) parseSize(kind sizeType) { + if s.nbytes < len(s.operation.Text) && s.operation.Text[s.nbytes] == '*' { + s.nbytes++ + if s.indexPending { + // Absorb it. + s.indexPending = false + size := Size{ + Fixed: -1, + Dynamic: s.argNum, + Index: s.index, + Range: Range{ + Start: s.indexPos, + End: s.nbytes, + }, + } + switch kind { + case Width: + s.operation.Width = size + case Precision: + // Include the leading '.'. + size.Range.Start -= len(".") + s.operation.Prec = size + default: + panic(kind) + } + } else { + // Non-indexed asterisk: "%*d". + size := Size{ + Dynamic: s.argNum, + Index: -1, + Fixed: -1, + Range: Range{ + Start: s.nbytes - 1, + End: s.nbytes, + }, + } + switch kind { + case Width: + s.operation.Width = size + case Precision: + // For precision, include the '.' in the range. + size.Range.Start -= 1 + s.operation.Prec = size + default: + panic(kind) + } + } + s.argNum++ + } else { // Literal number, e.g. "%10d" + start := s.nbytes + if num, ok := s.scanNum(); ok { + size := Size{ + Fixed: num, + Index: -1, + Dynamic: -1, + Range: Range{ + Start: start, + End: s.nbytes, + }, + } + switch kind { + case Width: + s.operation.Width = size + case Precision: + // Include the leading '.'. + size.Range.Start -= 1 + s.operation.Prec = size + default: + panic(kind) + } + } + } +} + +// parsePrecision checks if there's a precision specified after a '.' character. +// If found, it may also parse an index or an asterisk. Returns an error if any index +// parsing fails. +func (s *state) parsePrecision() error { + // If there's a period, there may be a precision. + if s.nbytes < len(s.operation.Text) && s.operation.Text[s.nbytes] == '.' { + s.nbytes++ + if err := s.parseIndex(); err != nil { + return err + } + s.parseSize(Precision) + } + return nil +} From b31dda4ab27383e2199b76966e61ab6f8279af54 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 10 Jan 2025 10:49:56 -0500 Subject: [PATCH 021/309] gopls/internal/analysis/modernize: fix bug in mapsloop A loop body of m[k] += v was spuriously matched because I forgot to check the assignment operator. + test Updates golang/go#70815 Change-Id: If74dcbb0ba920ebd475b1d0bd9191c9b44661a1a Reviewed-on: https://go-review.googlesource.com/c/tools/+/642076 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/maps.go | 6 +++++- .../analysis/modernize/testdata/src/mapsloop/mapsloop.go | 8 ++++++++ .../modernize/testdata/src/mapsloop/mapsloop.go.golden | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 6e8eaf8a1e8..20e87fb1585 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -179,7 +179,11 @@ func mapsloop(pass *analysis.Pass) { if rng.Tok == token.DEFINE && rng.Key != nil && rng.Value != nil && len(rng.Body.List) == 1 { // Have: for k, v := range x { S } - if assign, ok := rng.Body.List[0].(*ast.AssignStmt); ok && len(assign.Lhs) == 1 { + if assign, ok := rng.Body.List[0].(*ast.AssignStmt); ok && + assign.Tok == token.ASSIGN && + len(assign.Lhs) == 1 { + // Have: for k, v := range x { lhs = rhs } + if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok && equalSyntax(rng.Key, index.Index) && equalSyntax(rng.Value, assign.Rhs[0]) { diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go index ab1305d3b81..bf8127b9a7b 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go @@ -138,3 +138,11 @@ func nopeBodyNotASingleton(src map[int]string) { println() // nope: other things in the loop body } } + +// Regression test for https://github.com/golang/go/issues/70815#issuecomment-2581999787. +func nopeAssignmentHasIncrementOperator(src map[int]int) { + dst := make(map[int]int) + for k, v := range src { + dst[k] += v + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden index 6d95cc023ee..d62ebc1e9aa 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden @@ -110,3 +110,11 @@ func nopeBodyNotASingleton(src map[int]string) { println() // nope: other things in the loop body } } + +// Regression test for https://github.com/golang/go/issues/70815#issuecomment-2581999787. +func nopeAssignmentHasIncrementOperator(src map[int]int) { + dst := make(map[int]int) + for k, v := range src { + dst[k] += v + } +} From 1501321f0742fe2711b9d882af83afbd3f445e49 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 10 Jan 2025 10:49:56 -0500 Subject: [PATCH 022/309] gopls/internal/analysis/modernize: fix bug in minmax Fix another bug related to not checking AssignStmt.Op. + test Updates golang/go#70815 Change-Id: I426d27b4879d30f9fecb4d0f131b3c1a0b07773b Reviewed-on: https://go-review.googlesource.com/c/tools/+/642078 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/maps.go | 27 +++++++++---------- gopls/internal/analysis/modernize/minmax.go | 15 ++++++++--- .../modernize/testdata/src/minmax/minmax.go | 11 ++++++++ .../testdata/src/minmax/minmax.go.golden | 11 ++++++++ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 20e87fb1585..ba8dabe6948 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -177,20 +177,19 @@ func mapsloop(pass *analysis.Pass) { for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) { rng := curRange.Node().(*ast.RangeStmt) - if rng.Tok == token.DEFINE && rng.Key != nil && rng.Value != nil && len(rng.Body.List) == 1 { - // Have: for k, v := range x { S } - if assign, ok := rng.Body.List[0].(*ast.AssignStmt); ok && - assign.Tok == token.ASSIGN && - len(assign.Lhs) == 1 { - // Have: for k, v := range x { lhs = rhs } - - if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok && - equalSyntax(rng.Key, index.Index) && - equalSyntax(rng.Value, assign.Rhs[0]) { - - // Have: for k, v := range x { m[k] = v } - check(file, curRange, assign, index.X, rng.X) - } + if rng.Tok == token.DEFINE && + rng.Key != nil && + rng.Value != nil && + isAssignBlock(rng.Body) { + // Have: for k, v := range x { lhs = rhs } + + assign := rng.Body.List[0].(*ast.AssignStmt) + if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok && + equalSyntax(rng.Key, index.Index) && + equalSyntax(rng.Value, assign.Rhs[0]) { + + // Have: for k, v := range x { m[k] = v } + check(file, curRange, assign, index.X, rng.X) } } } diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index d17ad684d66..26b12341cad 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -95,7 +95,7 @@ func minmax(pass *analysis.Pass) { }) } - } else if prev, ok := curIfStmt.PrevSibling(); ok && is[*ast.AssignStmt](prev.Node()) { + } else if prev, ok := curIfStmt.PrevSibling(); ok && isSimpleAssign(prev.Node()) { fassign := prev.Node().(*ast.AssignStmt) // Have: lhs0 = rhs0; if a < b { lhs = rhs } @@ -193,8 +193,17 @@ func isAssignBlock(b *ast.BlockStmt) bool { if len(b.List) != 1 { return false } - assign, ok := b.List[0].(*ast.AssignStmt) - return ok && assign.Tok == token.ASSIGN && len(assign.Lhs) == 1 && len(assign.Rhs) == 1 + // Inv: the sole statement cannot be { lhs := rhs }. + return isSimpleAssign(b.List[0]) +} + +// isSimpleAssign reports whether n has the form "lhs = rhs" or "lhs := rhs". +func isSimpleAssign(n ast.Node) bool { + assign, ok := n.(*ast.AssignStmt) + return ok && + (assign.Tok == token.ASSIGN || assign.Tok == token.DEFINE) && + len(assign.Lhs) == 1 && + len(assign.Rhs) == 1 } // -- utils -- diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index 22747ed5547..c73bd30139b 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -81,3 +81,14 @@ func oops() { } print(y) } + +// Regression test for a bug: += is not a simple assignment. +func nopeAssignHasIncrementOperator() { + x := 1 + y := 0 + y += 2 + if x > y { + y = x + } + print(y) +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index c045fa35a85..11eac2c1418 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -58,3 +58,14 @@ func oops() { y := max(x, 2) print(y) } + +// Regression test for a bug: += is not a simple assignment. +func nopeAssignHasIncrementOperator() { + x := 1 + y := 0 + y += 2 + if x > y { + y = x + } + print(y) +} From cecec2c97a82be06aac5eb2aec8d3a1412a94455 Mon Sep 17 00:00:00 2001 From: Tim King Date: Thu, 9 Jan 2025 13:14:39 -0800 Subject: [PATCH 023/309] go/ssa: add typeset iteration helper Adds a typeset function for iterating over the type set of a type. This is effectively copying the internal function (go/types).typeset. Renames typeSetOf to termListOf. This additionally shifts usage away from termListOf to typeset where convenient. Change-Id: Ia51d2f9ef648b616646b063ee1adb178863485cf Reviewed-on: https://go-review.googlesource.com/c/tools/+/641839 Reviewed-by: Alan Donovan Reviewed-by: Robert Findley Commit-Queue: Tim King LUCI-TryBot-Result: Go LUCI --- go/ssa/builder.go | 2 +- go/ssa/emit.go | 26 +++--- go/ssa/print.go | 4 +- go/ssa/ssa.go | 5 +- go/ssa/{coretype.go => typeset.go} | 139 +++++++++++++++++++---------- 5 files changed, 105 insertions(+), 71 deletions(-) rename go/ssa/{coretype.go => typeset.go} (65%) diff --git a/go/ssa/builder.go b/go/ssa/builder.go index d2407e62fbd..4cd71260b61 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -856,7 +856,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value { if recv, ok := types.Unalias(sel.recv).(*types.TypeParam); ok { // Emit a nil check if any possible instantiation of the // type parameter is an interface type. - if len(typeSetOf(recv)) > 0 { + if !typeSetIsEmpty(recv) { // recv has a concrete term its typeset. // So it cannot be instantiated as an interface. // diff --git a/go/ssa/emit.go b/go/ssa/emit.go index 176c1e1a748..edd2ced3034 100644 --- a/go/ssa/emit.go +++ b/go/ssa/emit.go @@ -257,13 +257,6 @@ func emitConv(f *Function, val Value, typ types.Type) Value { return f.emit(mi) } - // In the common case, the typesets of src and dst are singletons - // and we emit an appropriate conversion. But if either contains - // a type parameter, the conversion may represent a cross product, - // in which case which we emit a MultiConvert. - dst_terms := typeSetOf(ut_dst) - src_terms := typeSetOf(ut_src) - // conversionCase describes an instruction pattern that maybe emitted to // model d <- s for d in dst_terms and s in src_terms. // Multiple conversions can match the same pattern. @@ -321,13 +314,14 @@ func emitConv(f *Function, val Value, typ types.Type) Value { } var classifications conversionCase - for _, s := range src_terms { - us := s.Type().Underlying() - for _, d := range dst_terms { - ud := d.Type().Underlying() - classifications |= classify(us, ud) - } - } + underIs(ut_src, func(us types.Type) bool { + return underIs(ut_dst, func(ud types.Type) bool { + if us != nil && ud != nil { + classifications |= classify(us, ud) + } + return classifications != 0 + }) + }) if classifications == 0 { panic(fmt.Sprintf("in %s: cannot convert %s (%s) to %s", f, val, val.Type(), typ)) } @@ -381,8 +375,8 @@ func emitConv(f *Function, val Value, typ types.Type) Value { c.setType(typ) return f.emit(c) - default: // multiple conversion - c := &MultiConvert{X: val, from: src_terms, to: dst_terms} + default: // The conversion represents a cross product. + c := &MultiConvert{X: val, from: t_src, to: typ} c.setType(typ) return f.emit(c) } diff --git a/go/ssa/print.go b/go/ssa/print.go index ef32672a26a..432c4b05b6d 100644 --- a/go/ssa/print.go +++ b/go/ssa/print.go @@ -180,8 +180,8 @@ func (v *MultiConvert) String() string { var b strings.Builder b.WriteString(printConv("multiconvert", v, v.X)) b.WriteString(" [") - for i, s := range v.from { - for j, d := range v.to { + for i, s := range termListOf(v.from) { + for j, d := range termListOf(v.to) { if i != 0 || j != 0 { b.WriteString(" | ") } diff --git a/go/ssa/ssa.go b/go/ssa/ssa.go index 4fa9831079c..ecad99d0340 100644 --- a/go/ssa/ssa.go +++ b/go/ssa/ssa.go @@ -719,9 +719,8 @@ type Convert struct { // t1 = multiconvert D <- S (t0) [*[2]rune <- []rune | string <- []rune] type MultiConvert struct { register - X Value - from []*types.Term - to []*types.Term + X Value + from, to types.Type } // ChangeInterface constructs a value of one interface type from a diff --git a/go/ssa/coretype.go b/go/ssa/typeset.go similarity index 65% rename from go/ssa/coretype.go rename to go/ssa/typeset.go index 082f8998b45..a5c36bf5471 100644 --- a/go/ssa/coretype.go +++ b/go/ssa/typeset.go @@ -10,36 +10,44 @@ import ( "golang.org/x/tools/internal/typeparams" ) -// Utilities for dealing with core types. +// Utilities for dealing with type sets. -// isBytestring returns true if T has the same terms as interface{[]byte | string}. -// These act like a core type for some operations: slice expressions, append and copy. -// -// See https://go.dev/ref/spec#Core_types for the details on bytestring. -func isBytestring(T types.Type) bool { - U := T.Underlying() - if _, ok := U.(*types.Interface); !ok { - return false - } +const debug = false - hasBytes, hasString := false, false - ok := underIs(U, func(t types.Type) bool { - switch { - case isString(t): - hasString = true - return true - case isByteSlice(t): - hasBytes = true - return true - default: - return false +// typeset is an iterator over the (type/underlying type) pairs of the +// specific type terms of the type set implied by t. +// If t is a type parameter, the implied type set is the type set of t's constraint. +// In that case, if there are no specific terms, typeset calls yield with (nil, nil). +// If t is not a type parameter, the implied type set consists of just t. +// In any case, typeset is guaranteed to call yield at least once. +func typeset(typ types.Type, yield func(t, u types.Type) bool) { + switch typ := types.Unalias(typ).(type) { + case *types.TypeParam, *types.Interface: + terms := termListOf(typ) + if len(terms) == 0 { + yield(nil, nil) + return } - }) - return ok && hasBytes && hasString + for _, term := range terms { + u := types.Unalias(term.Type()) + if !term.Tilde() { + u = u.Underlying() + } + if debug { + assert(types.Identical(u, u.Underlying()), "Unalias(x) == under(x) for ~x terms") + } + if !yield(term.Type(), u) { + break + } + } + return + default: + yield(typ, typ.Underlying()) + } } -// typeSetOf returns the type set of typ as a normalized term set. Returns an empty set on an error. -func typeSetOf(typ types.Type) []*types.Term { +// termListOf returns the type set of typ as a normalized term set. Returns an empty set on an error. +func termListOf(typ types.Type) []*types.Term { // This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on. var terms []*types.Term var err error @@ -64,21 +72,52 @@ func typeSetOf(typ types.Type) []*types.Term { return terms } +// typeSetIsEmpty returns true if a typeset is empty. +func typeSetIsEmpty(typ types.Type) bool { + var empty bool + typeset(typ, func(t, _ types.Type) bool { + empty = t == nil + return false + }) + return empty +} + +// isBytestring returns true if T has the same terms as interface{[]byte | string}. +// These act like a core type for some operations: slice expressions, append and copy. +// +// See https://go.dev/ref/spec#Core_types for the details on bytestring. +func isBytestring(T types.Type) bool { + U := T.Underlying() + if _, ok := U.(*types.Interface); !ok { + return false + } + + hasBytes, hasString := false, false + ok := underIs(U, func(t types.Type) bool { + switch { + case isString(t): + hasString = true + return true + case isByteSlice(t): + hasBytes = true + return true + default: + return false + } + }) + return ok && hasBytes && hasString +} + // underIs calls f with the underlying types of the type terms // of the type set of typ and reports whether all calls to f returned true. // If there are no specific terms, underIs returns the result of f(nil). func underIs(typ types.Type, f func(types.Type) bool) bool { - s := typeSetOf(typ) - if len(s) == 0 { - return f(nil) - } - for _, t := range s { - u := t.Type().Underlying() - if !f(u) { - return false - } - } - return true + var ok bool + typeset(typ, func(t, u types.Type) bool { + ok = f(u) + return ok + }) + return ok } // indexType returns the element type and index mode of a IndexExpr over a type. @@ -98,22 +137,24 @@ func indexType(typ types.Type) (types.Type, indexMode) { case *types.Basic: return tByte, ixValue // must be a string case *types.Interface: - tset := typeSetOf(U) - if len(tset) == 0 { - return nil, ixInvalid // no underlying terms or error is empty. - } - elem, mode := indexType(tset[0].Type()) - for _, t := range tset[1:] { - e, m := indexType(t.Type()) - if !types.Identical(elem, e) { // if type checked, just a sanity check - return nil, ixInvalid + var elem types.Type + mode := ixInvalid + typeset(typ, func(t, _ types.Type) bool { + if t == nil { + return false // empty set + } + e, m := indexType(t) + if elem == nil { + elem, mode = e, m + } + if debug && !types.Identical(elem, e) { // if type checked, just a sanity check + mode = ixInvalid + return false } // Update the mode to the most constrained address type. mode = mode.meet(m) - if mode == ixInvalid { - return nil, ixInvalid // fast exit - } - } + return mode != ixInvalid + }) return elem, mode } return nil, ixInvalid From 89127525e6027d943d94ca50fba0137dc03a62ae Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 10 Jan 2025 15:46:27 -0500 Subject: [PATCH 024/309] gopls/internal/protocol: optimized DocumentURI.Path for MODCACHE This CL removes an overly conservative check in DocumentURI.Path that caused files in the module cache to go through the slow path of URL parsing, unnecessarily. Thanks to Josh Bleecher Snyder for pointing it out. Change-Id: Id64ccb3b0a2b57258f9c4ebca11469fc77e37b3e Reviewed-on: https://go-review.googlesource.com/c/tools/+/642082 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- gopls/internal/protocol/uri.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gopls/internal/protocol/uri.go b/gopls/internal/protocol/uri.go index e4252909835..4105bd041f8 100644 --- a/gopls/internal/protocol/uri.go +++ b/gopls/internal/protocol/uri.go @@ -121,9 +121,13 @@ func filename(uri DocumentURI) (string, error) { if b < ' ' || b == 0x7f || // control character b == '%' || b == '+' || // URI escape b == ':' || // Windows drive letter - b == '@' || b == '&' || b == '?' { // authority or query + b == '&' || b == '?' { // authority or query goto slow } + // We do not reject '@' as it cannot be part of the + // authority (e.g. user:pass@example.com) in a + // "file:///" URL, and '@' commonly appears in file + // paths such as GOMODCACHE/module@version/... } return rest, nil } From 8f9869c7f0a36180f61a885c73bdd03f8fc37eb7 Mon Sep 17 00:00:00 2001 From: Tim King Date: Fri, 10 Jan 2025 12:42:02 -0800 Subject: [PATCH 025/309] go/ssa: use NormalTerms Use typeparams.NormalTerms in go/ssa. Adjusts NormalTerms to preserve names and aliases. As a part of this, NormalTerms now no longer uses the Underlying() on *types.TypeParams. Change-Id: Ic09c0fba46982b81066b396f11a5b0ea48739819 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642155 Reviewed-by: Robert Findley Commit-Queue: Tim King Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Tim King --- go/ssa/typeset.go | 19 +------------------ internal/typeparams/coretype.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/go/ssa/typeset.go b/go/ssa/typeset.go index a5c36bf5471..d0106dc6874 100644 --- a/go/ssa/typeset.go +++ b/go/ssa/typeset.go @@ -48,24 +48,7 @@ func typeset(typ types.Type, yield func(t, u types.Type) bool) { // termListOf returns the type set of typ as a normalized term set. Returns an empty set on an error. func termListOf(typ types.Type) []*types.Term { - // This is a adaptation of x/exp/typeparams.NormalTerms which x/tools cannot depend on. - var terms []*types.Term - var err error - // typeSetOf(t) == typeSetOf(Unalias(t)) - switch typ := types.Unalias(typ).(type) { - case *types.TypeParam: - terms, err = typeparams.StructuralTerms(typ) - case *types.Union: - terms, err = typeparams.UnionTermSet(typ) - case *types.Interface: - terms, err = typeparams.InterfaceTermSet(typ) - default: - // Common case. - // Specializing the len=1 case to avoid a slice - // had no measurable space/time benefit. - terms = []*types.Term{types.NewTerm(false, typ)} - } - + terms, err := typeparams.NormalTerms(typ) if err != nil { return nil } diff --git a/internal/typeparams/coretype.go b/internal/typeparams/coretype.go index 6e83c6fb1a2..27a2b179299 100644 --- a/internal/typeparams/coretype.go +++ b/internal/typeparams/coretype.go @@ -109,8 +109,13 @@ func CoreType(T types.Type) types.Type { // // NormalTerms makes no guarantees about the order of terms, except that it // is deterministic. -func NormalTerms(typ types.Type) ([]*types.Term, error) { - switch typ := typ.Underlying().(type) { +func NormalTerms(T types.Type) ([]*types.Term, error) { + // typeSetOf(T) == typeSetOf(Unalias(T)) + typ := types.Unalias(T) + if named, ok := typ.(*types.Named); ok { + typ = named.Underlying() + } + switch typ := typ.(type) { case *types.TypeParam: return StructuralTerms(typ) case *types.Union: @@ -118,7 +123,7 @@ func NormalTerms(typ types.Type) ([]*types.Term, error) { case *types.Interface: return InterfaceTermSet(typ) default: - return []*types.Term{types.NewTerm(false, typ)}, nil + return []*types.Term{types.NewTerm(false, T)}, nil } } From 0b95e04fcb96358ec856ca1158781bc13827a393 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 9 Jan 2025 18:59:45 +0000 Subject: [PATCH 026/309] gopls: filter out hints for closed files and make modernizers hints - Document Analyzer.Severity to describe heuristics for how severity should be determined. - Filter out hint diagnostics for closed files. VS Code already suppresses hint diagnostics from the Problems tab, but other clients do not. This change makes the visibility of Hint diagnostics more similar across clients. - Downgrade 'modernize' to Hint level severity. Updates golang/go#70815 Change-Id: If93b57d25ed3eb8dc253a3c7ef016c4148086dc9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641796 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/errors.go | 7 +--- gopls/internal/server/diagnostics.go | 19 ++++++++++ gopls/internal/settings/analysis.go | 38 ++++++++++++++++--- .../integration/diagnostics/analysis_test.go | 29 ++++++++++++++ 4 files changed, 81 insertions(+), 12 deletions(-) diff --git a/gopls/internal/cache/errors.go b/gopls/internal/cache/errors.go index 26747a63d33..816d6c6b0f8 100644 --- a/gopls/internal/cache/errors.go +++ b/gopls/internal/cache/errors.go @@ -270,15 +270,10 @@ func toSourceDiagnostic(srcAnalyzer *settings.Analyzer, gobDiag *gobDiagnostic) related = append(related, protocol.DiagnosticRelatedInformation(gobRelated)) } - severity := srcAnalyzer.Severity() - if severity == 0 { - severity = protocol.SeverityWarning - } - diag := &Diagnostic{ URI: gobDiag.Location.URI, Range: gobDiag.Location.Range, - Severity: severity, + Severity: srcAnalyzer.Severity(), Code: gobDiag.Code, CodeHref: gobDiag.CodeHref, Source: DiagnosticSource(gobDiag.Source), diff --git a/gopls/internal/server/diagnostics.go b/gopls/internal/server/diagnostics.go index e95bf297501..541ba22350c 100644 --- a/gopls/internal/server/diagnostics.go +++ b/gopls/internal/server/diagnostics.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "sort" "strings" "sync" @@ -511,6 +512,24 @@ func (s *server) diagnose(ctx context.Context, snapshot *cache.Snapshot) (diagMa // TODO(rfindley): here and above, we should avoid using the first result // if err is non-nil (though as of today it's OK). analysisDiags, err = golang.Analyze(ctx, snapshot, toAnalyze, s.progress) + + // Filter out Hint diagnostics for closed files. + // VS Code already omits Hint diagnostics in the Problems tab, but other + // clients do not. This filter makes the visibility of Hints more similar + // across clients. + for uri, diags := range analysisDiags { + if !snapshot.IsOpen(uri) { + newDiags := slices.DeleteFunc(diags, func(diag *cache.Diagnostic) bool { + return diag.Severity == protocol.SeverityHint + }) + if len(newDiags) == 0 { + delete(analysisDiags, uri) + } else { + analysisDiags[uri] = newDiags + } + } + } + if err != nil { event.Error(ctx, "warning: analyzing package", err, append(snapshot.Labels(), label.Package.Of(keys.Join(moremaps.KeySlice(toDiagnose))))...) return diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 0bd9fa8136b..7be5d896d75 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -88,12 +88,35 @@ func (a *Analyzer) EnabledByDefault() bool { return !a.nonDefault } // TODO(rfindley): revisit. func (a *Analyzer) ActionKinds() []protocol.CodeActionKind { return a.actionKinds } -// Severity is the severity set for diagnostics reported by this -// analyzer. If left unset it defaults to Warning. +// Severity is the severity set for diagnostics reported by this analyzer. +// The default severity is SeverityWarning. // -// Note: diagnostics with severity protocol.SeverityHint do not show up in -// the VS Code "problems" tab. -func (a *Analyzer) Severity() protocol.DiagnosticSeverity { return a.severity } +// While the LSP spec does not specify how severity should be used, here are +// some guiding heuristics: +// - Error: for parse and type errors, which would stop the build. +// - Warning: for analyzer diagnostics reporting likely bugs. +// - Info: for analyzer diagnostics that do not indicate bugs, but may +// suggest inaccurate or superfluous code. +// - Hint: for analyzer diagnostics that do not indicate mistakes, but offer +// simplifications or modernizations. By their nature, hints should +// generally carry quick fixes. +// +// The difference between Info and Hint is particularly subtle. Importantly, +// Hint diagnostics do not appear in the Problems tab in VS Code, so they are +// less intrusive than Info diagnostics. The rule of thumb is this: use Info if +// the diagnostic is not a bug, but the author probably didn't mean to write +// the code that way. Use Hint if the diagnostic is not a bug and the author +// indended to write the code that way, but there is a simpler or more modern +// way to express the same logic. An 'unused' diagnostic is Info level, since +// the author probably didn't mean to check in unreachable code. A 'modernize' +// or 'deprecated' diagnostic is Hint level, since the author intended to write +// the code that way, but now there is a better way. +func (a *Analyzer) Severity() protocol.DiagnosticSeverity { + if a.severity == 0 { + return protocol.SeverityWarning + } + return a.severity +} // Tags is extra tags (unnecessary, deprecated, etc) for diagnostics // reported by this analyzer. @@ -109,6 +132,7 @@ func (a *Analyzer) String() string { return a.analyzer.String() } var DefaultAnalyzers = make(map[string]*Analyzer) // initialized below func init() { + // See [Analyzer.Severity] for guidance on setting analyzer severity below. analyzers := []*Analyzer{ // The traditional vet suite: {analyzer: appends.Analyzer}, @@ -190,10 +214,12 @@ func init() { {analyzer: unusedparams.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedfunc.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedwrite.Analyzer, severity: protocol.SeverityInformation}, // uses go/ssa - {analyzer: modernize.Analyzer, severity: protocol.SeverityInformation}, + {analyzer: modernize.Analyzer, severity: protocol.SeverityHint}, // type-error analyzers // These analyzers enrich go/types errors with suggested fixes. + // Since they exist only to attach their fixes to type errors, their + // severity is irrelevant. {analyzer: fillreturns.Analyzer}, {analyzer: nonewvars.Analyzer}, {analyzer: noresultvalues.Analyzer}, diff --git a/gopls/internal/test/integration/diagnostics/analysis_test.go b/gopls/internal/test/integration/diagnostics/analysis_test.go index 8cb86f8f735..7e93398d57a 100644 --- a/gopls/internal/test/integration/diagnostics/analysis_test.go +++ b/gopls/internal/test/integration/diagnostics/analysis_test.go @@ -125,3 +125,32 @@ func main() { } }) } + +func TestAnalysisFiltering(t *testing.T) { + // This test checks that hint level diagnostics are only surfaced for open + // files. + + const src = ` +-- go.mod -- +module mod.com + +go 1.20 + +-- a.go -- +package p + +var x interface{} + +-- b.go -- +package p + +var y interface{} +` + Run(t, src, func(t *testing.T, env *Env) { + env.OpenFile("a.go") + env.AfterChange( + Diagnostics(ForFile("a.go"), WithMessage("replaced by any")), + NoDiagnostics(ForFile("b.go")), + ) + }) +} From 1335f053dbdc32a3dbd4ee58c69a773eaaebe184 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 13 Jan 2025 10:57:52 -0500 Subject: [PATCH 027/309] gopls/internal/util/frob: Decode: improve panic on empty Also, add justification for fall through. Updates golang/go#71244 Change-Id: I781d015a9d4659815588e95dea92eb350388b925 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642435 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/check.go | 1 + gopls/internal/util/frob/frob.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index 068fa70b4ed..1f35e684838 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -1290,6 +1290,7 @@ func (s *Snapshot) typerefData(ctx context.Context, id PackageID, imports map[Im return data, nil } else if err != filecache.ErrNotFound { bug.Reportf("internal error reading typerefs data: %v", err) + // Unexpected error: treat as cache miss, and fall through. } pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), parsego.Full&^parser.ParseComments, true, cgfs...) diff --git a/gopls/internal/util/frob/frob.go b/gopls/internal/util/frob/frob.go index c297e2a1014..00ef7c7f95e 100644 --- a/gopls/internal/util/frob/frob.go +++ b/gopls/internal/util/frob/frob.go @@ -244,8 +244,8 @@ func (fr *frob) Decode(data []byte, ptr any) { panic(fmt.Sprintf("got %v, want %v", rv.Type(), fr.t)) } rd := &reader{data} - if string(rd.bytes(4)) != magic { - panic("not a frob-encoded message") + if len(data) < len(magic) || string(rd.bytes(len(magic))) != magic { + panic("not a frob-encoded message") // (likely an empty message) } fr.decode(rd, rv) if len(rd.data) > 0 { From 5fef1f231d55df0372ab644b424cb32c249c95bc Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Wed, 8 Jan 2025 06:23:59 +0000 Subject: [PATCH 028/309] gopls/internal/telemetry/cmd/stacks: add cmd/compile support to readPCLineTable Building the compiler is actually simpler than gopls, since GOTOOLCHAIN is all you need. No need to explicitly git clone anything. Most of this is just minor refactoring to avoid hard-coding gopls details. Updates golang/go#71045. Change-Id: I6a6a636c5d950cec713e358dfd4dddcbd07554fc Reviewed-on: https://go-review.googlesource.com/c/tools/+/642418 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 113 +++++++++++------- .../telemetry/cmd/stacks/stacks_test.go | 77 ++++++++++++ 2 files changed, 147 insertions(+), 43 deletions(-) create mode 100644 gopls/internal/telemetry/cmd/stacks/stacks_test.go diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 1888267c021..b158f2ccb60 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -184,12 +184,12 @@ func main() { distinctStacks++ info := Info{ - Program: prog.Program, - Version: prog.Version, - GoVersion: prog.GoVersion, - GOOS: prog.GOOS, - GOARCH: prog.GOARCH, - Client: clientSuffix, + Program: prog.Program, + ProgramVersion: prog.Version, + GoVersion: prog.GoVersion, + GOOS: prog.GOOS, + GOARCH: prog.GOARCH, + Client: clientSuffix, } for stack, count := range prog.Stacks { counts := stacks[stack] @@ -432,15 +432,15 @@ func main() { // Info is used as a key for de-duping and aggregating. // Do not add detail about particular records (e.g. data, telemetry URL). type Info struct { - Program string // "golang.org/x/tools/gopls" - Version, GoVersion string // e.g. "gopls/v0.16.1", "go1.23" - GOOS, GOARCH string - Client string // e.g. "vscode" + Program string // "golang.org/x/tools/gopls" + ProgramVersion, GoVersion string // e.g. "v0.16.1", "go1.23" + GOOS, GOARCH string + Client string // e.g. "vscode" } func (info Info) String() string { return fmt.Sprintf("%s@%s %s %s/%s %s", - info.Program, info.Version, + info.Program, info.ProgramVersion, info.GoVersion, info.GOOS, info.GOARCH, info.Client) } @@ -543,7 +543,7 @@ func writeStackComment(body *bytes.Buffer, stack, id string, jsonURL string, cou id, jsonURL) // Read the mapping from symbols to file/line. - pclntab, err := readPCLineTable(info) + pclntab, err := readPCLineTable(info, defaultStacksDir) if err != nil { log.Fatal(err) } @@ -631,7 +631,7 @@ func frameURL(pclntab map[string]FileLine, info Info, frame string) string { } return fmt.Sprintf("https://cs.opensource.google/go/x/tools/+/%s:%s;l=%d", - "gopls/"+info.Version, rest, linenum) + "gopls/"+info.ProgramVersion, rest, linenum) } // other x/ module dependency? @@ -770,63 +770,90 @@ type FileLine struct { line int } +const defaultStacksDir = "/tmp/stacks-cache" + // readPCLineTable builds the gopls executable specified by info, // reads its PC-to-line-number table, and returns the file/line of // each TEXT symbol. -func readPCLineTable(info Info) (map[string]FileLine, error) { +// +// stacksDir is a semi-durable temp directory (i.e. lasts for at least a few +// hours) to hold recent sources and executables. +func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { // The stacks dir will be a semi-durable temp directory // (i.e. lasts for at least hours) holding source trees // and executables we have built recently. // // Each subdir will hold a specific revision. - stacksDir := "/tmp/gopls-stacks" if err := os.MkdirAll(stacksDir, 0777); err != nil { return nil, fmt.Errorf("can't create stacks dir: %v", err) } - // Fetch the source for the tools repo, - // shallow-cloning just the desired revision. - // (Skip if it's already cloned.) - revDir := filepath.Join(stacksDir, info.Version) - if !fileExists(filepath.Join(revDir, "go.mod")) { - // We check for presence of the go.mod file, - // not just the directory itself, as the /tmp reaper - // often removes stale files before removing their directories. - // Remove those stale directories now. - _ = os.RemoveAll(revDir) // ignore errors - - log.Printf("cloning tools@gopls/%s", info.Version) - if err := shallowClone(revDir, "https://go.googlesource.com/tools", "gopls/"+info.Version); err != nil { + // When building a subrepo tool, we must clone the source of the + // subrepo, and run go build from that checkout. + // + // When building a main repo tool, no need to clone or change + // directories. GOTOOLCHAIN is sufficient to fetch and build the + // appropriate version. + var buildDir string + switch info.Program { + case "golang.org/x/tools/gopls": + // Fetch the source for the tools repo, + // shallow-cloning just the desired revision. + // (Skip if it's already cloned.) + revDir := filepath.Join(stacksDir, info.ProgramVersion) + if !fileExists(filepath.Join(revDir, "go.mod")) { + // We check for presence of the go.mod file, + // not just the directory itself, as the /tmp reaper + // often removes stale files before removing their directories. + // Remove those stale directories now. _ = os.RemoveAll(revDir) // ignore errors - return nil, fmt.Errorf("clone: %v", err) + + log.Printf("cloning tools@gopls/%s", info.ProgramVersion) + if err := shallowClone(revDir, "https://go.googlesource.com/tools", "gopls/"+info.ProgramVersion); err != nil { + _ = os.RemoveAll(revDir) // ignore errors + return nil, fmt.Errorf("clone: %v", err) + } } + + // gopls is in its own module, we must build from there. + buildDir = filepath.Join(revDir, "gopls") + case "cmd/compile": + // Nothing to do, GOTOOLCHAIN is sufficient. + default: + return nil, fmt.Errorf("don't know how to build unknown program %s", info.Program) } + // No slashes in file name. + escapedProg := strings.Replace(info.Program, "/", "_", -1) + // Build the executable with the correct GOTOOLCHAIN, GOOS, GOARCH. // Use -trimpath for normalized file names. // (Skip if it's already built.) - exe := fmt.Sprintf("exe-%s.%s-%s", info.GoVersion, info.GOOS, info.GOARCH) - cmd := exec.Command("go", "build", "-trimpath", "-o", "../"+exe) - cmd.Stderr = os.Stderr - cmd.Dir = filepath.Join(revDir, "gopls") - cmd.Env = append(os.Environ(), - "GOTOOLCHAIN="+info.GoVersion, - "GOOS="+info.GOOS, - "GOARCH="+info.GOARCH, - ) - if !fileExists(filepath.Join(revDir, exe)) { + exe := fmt.Sprintf("exe-%s-%s.%s-%s", escapedProg, info.GoVersion, info.GOOS, info.GOARCH) + exe = filepath.Join(stacksDir, exe) + + if !fileExists(exe) { log.Printf("building %s@%s with %s for %s/%s", - info.Program, info.Version, info.GoVersion, info.GOOS, info.GOARCH) + info.Program, info.ProgramVersion, info.GoVersion, info.GOOS, info.GOARCH) + + cmd := exec.Command("go", "build", "-trimpath", "-o", exe, info.Program) + cmd.Stderr = os.Stderr + cmd.Dir = buildDir + cmd.Env = append(os.Environ(), + "GOTOOLCHAIN="+info.GoVersion, + "GOOS="+info.GOOS, + "GOARCH="+info.GOARCH, + "GOWORK=off", + ) if err := cmd.Run(); err != nil { - return nil, fmt.Errorf("building: %v (rm -fr /tmp/gopls-stacks?)", err) + return nil, fmt.Errorf("building: %v (rm -fr %s?)", err, stacksDir) } } // Read pclntab of executable. - cmd = exec.Command("go", "tool", "objdump", exe) + cmd := exec.Command("go", "tool", "objdump", exe) cmd.Stdout = new(strings.Builder) cmd.Stderr = os.Stderr - cmd.Dir = revDir cmd.Env = append(os.Environ(), "GOTOOLCHAIN="+info.GoVersion, "GOOS="+info.GOOS, diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go new file mode 100644 index 00000000000..47353a365cd --- /dev/null +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -0,0 +1,77 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build linux || darwin + +package main + +import ( + "testing" +) + +func TestReadPCLineTable(t *testing.T) { + if testing.Short() { + // TODO(prattmic): It would be nice to have a unit test that + // didn't require downloading. + t.Skip("downloads source from the internet, skipping in -short") + } + + type testCase struct { + name string + info Info + wantSymbol string + wantFileLine FileLine + } + + tests := []testCase{ + { + name: "gopls", + info: Info{ + Program: "golang.org/x/tools/gopls", + ProgramVersion: "v0.16.1", + GoVersion: "go1.23.4", + GOOS: "linux", + GOARCH: "amd64", + }, + wantSymbol: "golang.org/x/tools/gopls/internal/cmd.(*Application).Run", + wantFileLine: FileLine{ + file: "golang.org/x/tools/gopls/internal/cmd/cmd.go", + line: 230, + }, + }, + { + name: "compile", + info: Info{ + Program: "cmd/compile", + ProgramVersion: "go1.23.4", + GoVersion: "go1.23.4", + GOOS: "linux", + GOARCH: "amd64", + }, + wantSymbol: "runtime.main", + wantFileLine: FileLine{ + file: "runtime/proc.go", + line: 147, + }, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + stacksDir := t.TempDir() + pcln, err := readPCLineTable(tc.info, stacksDir) + if err != nil { + t.Fatalf("readPCLineTable got err %v want nil", err) + } + + got, ok := pcln[tc.wantSymbol] + if !ok { + t.Fatalf("PCLineTable want entry %s got !ok from pcln %+v", tc.wantSymbol, pcln) + } + + if got != tc.wantFileLine { + t.Fatalf("symbol %s got FileLine %+v want %+v", tc.wantSymbol, got, tc.wantFileLine) + } + }) + } +} From 8a5a6d752c3fc0e0970ae556fa94b07304bb6be6 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Wed, 8 Jan 2025 08:08:26 +0000 Subject: [PATCH 029/309] gopls/internal/telemetry/cmd/stacks: refactor report processing to support different programs Only gopls is actually supported for now. This CL is intended to be a no-op. It consists of two main pieces: * Refactoring the giant main function into smaller pieces that are easier to understand individually. * Adding a ProgramConfig to describe how to process reports from a specific program. Updates golang/go#71045. Change-Id: I6a6a636c5b8b56bf72354a8795320eac7de7ef93 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642419 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 371 ++++++++++++------ 1 file changed, 250 insertions(+), 121 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index b158f2ccb60..4964e53f8de 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -90,11 +90,63 @@ import ( // flags var ( + programFlag = flag.String("program", "golang.org/x/tools/gopls", "Package path of program to process") + daysFlag = flag.Int("days", 7, "number of previous days of telemetry data to read") authToken string // mandatory GitHub authentication token (for R/W issues access) ) +// ProgramConfig is the configuration for processing reports for a specific +// program. +type ProgramConfig struct { + // Program is the package path of the program to process. + Program string + + // IncludeClient indicates that stack Info should include gopls/client metadata. + IncludeClient bool + + // SearchLabel is the GitHub label used to find all existing reports. + SearchLabel string + + // NewIssuePrefix is the package prefix to apply to new issue titles. + NewIssuePrefix string + + // NewIssueLabels are the labels to apply to new issues. + NewIssueLabels []string + + // MatchSymbolPrefix is the prefix of "interesting" symbol names. + // + // A given stack will be "blamed" on the deepest symbol in the stack that: + // 1. Matches MatchSymbolPrefix + // 2. Is an exported function or any method on an exported Type. + // 3. Does _not_ match IgnoreSymbolContains. + MatchSymbolPrefix string + + // IgnoreSymbolContains are "uninteresting" symbol substrings. e.g., + // logging packages. + IgnoreSymbolContains []string +} + +var programs = map[string]ProgramConfig{ + "golang.org/x/tools/gopls": { + Program: "golang.org/x/tools/gopls", + IncludeClient: true, + SearchLabel: "gopls/telemetry-wins", + NewIssuePrefix: "x/tools/gopls", + NewIssueLabels: []string{ + "gopls", + "Tools", + "gopls/telemetry-wins", + "NeedsInvestigation", + }, + MatchSymbolPrefix: "golang.org/x/tools/gopls/", + IgnoreSymbolContains: []string{ + "internal/util/bug.", + }, + }, +} + func main() { log.SetFlags(0) log.SetPrefix("stacks: ") @@ -127,26 +179,125 @@ func main() { authToken = string(bytes.TrimSpace(content)) } - // Maps stack text to Info to count. - stacks := make(map[string]map[Info]int64) - var distinctStacks int - - // Maps stack to a telemetry URL. - stackToURL := make(map[string]string) + pcfg, ok := programs[*programFlag] + if !ok { + log.Fatalf("unknown -program %s", *programFlag) + } // Read all recent telemetry reports. + stacks, distinctStacks, stackToURL, err := readReports(pcfg, *daysFlag) + if err != nil { + log.Fatalf("Error reading reports: %v", err) + } + + issues, err := readIssues(pcfg) + if err != nil { + log.Fatalf("Error reading issues: %v", err) + } + + // Map stacks to existing issues (if any). + claimedBy := claimStacks(issues, stacks) + + // Update existing issues that claimed new stacks. + updateIssues(issues, stacks, stackToURL) + + // For each stack, show existing issue or create a new one. + // Aggregate stack IDs by issue summary. + var ( + // Both vars map the summary line to the stack count. + existingIssues = make(map[string]int64) + newIssues = make(map[string]int64) + ) + for stack, counts := range stacks { + id := stackID(stack) + + var total int64 + for _, count := range counts { + total += count + } + + if issue, ok := claimedBy[id]; ok { + // existing issue, already updated above, just store + // the summary. + summary := fmt.Sprintf("#%d: %s [%s]", + issue.Number, issue.Title, issue.State) + existingIssues[summary] += total + } else { + // new issue, need to create GitHub issue and store + // summary. + title := newIssue(pcfg, stack, id, stackToURL[stack], counts) + summary := fmt.Sprintf("%s: %s [%s]", id, title, "new") + newIssues[summary] += total + } + } + + fmt.Printf("Found %d distinct stacks in last %v days:\n", distinctStacks, *daysFlag) + print := func(caption string, issues map[string]int64) { + // Print items in descending frequency. + keys := moremaps.KeySlice(issues) + sort.Slice(keys, func(i, j int) bool { + return issues[keys[i]] > issues[keys[j]] + }) + fmt.Printf("%s issues:\n", caption) + for _, summary := range keys { + count := issues[summary] + // Show closed issues in "white". + if isTerminal(os.Stdout) && strings.Contains(summary, "[closed]") { + // ESC + "[" + n + "m" => change color to n + // (37 = white, 0 = default) + summary = "\x1B[37m" + summary + "\x1B[0m" + } + fmt.Printf("%s (n=%d)\n", summary, count) + } + } + print("Existing", existingIssues) + print("New", newIssues) +} + +// Info is used as a key for de-duping and aggregating. +// Do not add detail about particular records (e.g. data, telemetry URL). +type Info struct { + Program string // "golang.org/x/tools/gopls" + ProgramVersion string // "v0.16.1" + GoVersion string // "go1.23" + GOOS, GOARCH string + GoplsClient string // e.g. "vscode" (only set if Program == "golang.org/x/tools/gopls") +} + +func (info Info) String() string { + s := fmt.Sprintf("%s@%s %s %s/%s", + info.Program, info.ProgramVersion, + info.GoVersion, info.GOOS, info.GOARCH) + if info.GoplsClient != "" { + s += " " + info.GoplsClient + } + return s +} + +// readReports downloads telemetry stack reports for a program from the +// specified number of most recent days. +// +// stacks is a map of stack text to program metadata to stack+metadata report +// count. +// distinctStacks is the sum of all counts in stacks. +// stackToURL maps the stack text to the oldest telemetry JSON report it was +// included in. +func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64, distinctStacks int, stackToURL map[string]string, err error) { + stacks = make(map[string]map[Info]int64) + stackToURL = make(map[string]string) + t := time.Now() - for i := 0; i < *daysFlag; i++ { + for i := range days { date := t.Add(-time.Duration(i+1) * 24 * time.Hour).Format(time.DateOnly) url := fmt.Sprintf("https://storage.googleapis.com/prod-telemetry-merged/%s.json", date) resp, err := http.Get(url) if err != nil { - log.Fatalf("can't GET %s: %v", url, err) + return nil, 0, nil, fmt.Errorf("error on GET %s: %v", url, err) } defer resp.Body.Close() if resp.StatusCode != 200 { - log.Fatalf("GET %s returned %d %s", url, resp.StatusCode, resp.Status) + return nil, 0, nil, fmt.Errorf("GET %s returned %d %s", url, resp.StatusCode, resp.Status) } dec := json.NewDecoder(resp.Body) @@ -156,13 +307,20 @@ func main() { if err == io.EOF { break } - log.Fatal(err) + return nil, 0, nil, fmt.Errorf("error decoding report: %v", err) } for _, prog := range report.Programs { - if prog.Program == "golang.org/x/tools/gopls" && len(prog.Stacks) > 0 { - // Include applicable client names (e.g. vscode, eglot). + if prog.Program != pcfg.Program { + continue + } + if len(prog.Stacks) == 0 { + continue + } + + // Include applicable client names (e.g. vscode, eglot) for gopls. + var clientSuffix string + if pcfg.IncludeClient { var clients []string - var clientSuffix string for key := range prog.Counters { client := strings.TrimPrefix(key, "gopls/client:") if client != key { @@ -173,44 +331,50 @@ func main() { if len(clients) > 0 { clientSuffix = strings.Join(clients, ",") } + } - // Ignore @devel versions as they correspond to - // ephemeral (and often numerous) variations of - // the program as we work on a fix to a bug. - if prog.Version == "devel" { - continue - } + // Ignore @devel versions as they correspond to + // ephemeral (and often numerous) variations of + // the program as we work on a fix to a bug. + if prog.Version == "devel" { + continue + } - distinctStacks++ + distinctStacks++ - info := Info{ - Program: prog.Program, - ProgramVersion: prog.Version, - GoVersion: prog.GoVersion, - GOOS: prog.GOOS, - GOARCH: prog.GOARCH, - Client: clientSuffix, - } - for stack, count := range prog.Stacks { - counts := stacks[stack] - if counts == nil { - counts = make(map[Info]int64) - stacks[stack] = counts - } - counts[info] += count - stackToURL[stack] = url + info := Info{ + Program: prog.Program, + ProgramVersion: prog.Version, + GoVersion: prog.GoVersion, + GOOS: prog.GOOS, + GOARCH: prog.GOARCH, + GoplsClient: clientSuffix, + } + for stack, count := range prog.Stacks { + counts := stacks[stack] + if counts == nil { + counts = make(map[Info]int64) + stacks[stack] = counts } + counts[info] += count + stackToURL[stack] = url } } } } - // Query GitHub for all existing GitHub issues with label:gopls/telemetry-wins. + return stacks, distinctStacks, stackToURL, nil +} + +// readIssues returns all existing issues for the given program and parses any +// predicates. +func readIssues(pcfg ProgramConfig) ([]*Issue, error) { + // Query GitHub for all existing GitHub issues with the report label. // // TODO(adonovan): by default GitHub returns at most 30 // issues; we have lifted this to 100 using per_page=%d, but // that won't work forever; use paging. - const query = "is:issue label:gopls/telemetry-wins" + query := fmt.Sprintf("is:issue label:%s", pcfg.SearchLabel) res, err := searchIssues(query) if err != nil { log.Fatalf("GitHub issues query %q failed: %v", query, err) @@ -295,6 +459,25 @@ func main() { } } + return res.Items, nil +} + +// claimStack maps each stack ID to its issue (if any). +// +// It returns a map of stack text to the issue that claimed it. +// +// An issue can claim a stack two ways: +// +// 1. if the issue body contains the ID of the stack. Matching +// is a little loose but base64 will rarely produce words +// that appear in the body by chance. +// +// 2. if the issue body contains a ```#!stacks``` predicate +// that matches the stack. +// +// We log an error if two different issues attempt to claim +// the same stack. +func claimStacks(issues []*Issue, stacks map[string]map[Info]int64) map[string]*Issue { // Map each stack ID to its issue. // // An issue can claim a stack two ways: @@ -313,7 +496,7 @@ func main() { claimedBy := make(map[string]*Issue) for stack := range stacks { id := stackID(stack) - for _, issue := range res.Items { + for _, issue := range issues { byPredicate := false if strings.Contains(issue.Body, id) { // nop @@ -341,36 +524,12 @@ func main() { } } - // For each stack, show existing issue or create a new one. - // Aggregate stack IDs by issue summary. - var ( - // Both vars map the summary line to the stack count. - existingIssues = make(map[string]int64) - newIssues = make(map[string]int64) - ) - for stack, counts := range stacks { - id := stackID(stack) - - var total int64 - for _, count := range counts { - total += count - } - - if issue, ok := claimedBy[id]; ok { - // existing issue - summary := fmt.Sprintf("#%d: %s [%s]", - issue.Number, issue.Title, issue.State) - existingIssues[summary] += total - } else { - // new issue - title := newIssue(stack, id, stackToURL[stack], counts) - summary := fmt.Sprintf("%s: %s [%s]", id, title, "new") - newIssues[summary] += total - } - } + return claimedBy +} - // Update existing issues that claimed new stacks by predicate. - for _, issue := range res.Items { +// updateIssues updates existing issues that claimed new stacks by predicate. +func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL map[string]string) { + for _, issue := range issues { if len(issue.newStacks) == 0 { continue } @@ -405,44 +564,6 @@ func main() { log.Printf("added stacks %s to issue #%d", newStackIDs, issue.Number) } - - fmt.Printf("Found %d distinct stacks in last %v days:\n", distinctStacks, *daysFlag) - print := func(caption string, issues map[string]int64) { - // Print items in descending frequency. - keys := moremaps.KeySlice(issues) - sort.Slice(keys, func(i, j int) bool { - return issues[keys[i]] > issues[keys[j]] - }) - fmt.Printf("%s issues:\n", caption) - for _, summary := range keys { - count := issues[summary] - // Show closed issues in "white". - if isTerminal(os.Stdout) && strings.Contains(summary, "[closed]") { - // ESC + "[" + n + "m" => change color to n - // (37 = white, 0 = default) - summary = "\x1B[37m" + summary + "\x1B[0m" - } - fmt.Printf("%s (n=%d)\n", summary, count) - } - } - print("Existing", existingIssues) - print("New", newIssues) -} - -// Info is used as a key for de-duping and aggregating. -// Do not add detail about particular records (e.g. data, telemetry URL). -type Info struct { - Program string // "golang.org/x/tools/gopls" - ProgramVersion, GoVersion string // e.g. "v0.16.1", "go1.23" - GOOS, GOARCH string - Client string // e.g. "vscode" -} - -func (info Info) String() string { - return fmt.Sprintf("%s@%s %s %s/%s %s", - info.Program, info.ProgramVersion, - info.GoVersion, info.GOOS, info.GOARCH, - info.Client) } // stackID returns a 32-bit identifier for a stack @@ -469,24 +590,27 @@ func stackID(stack string) string { // manually de-dup the issue before deciding whether to submit the form.) // // It returns the title. -func newIssue(stack, id string, jsonURL string, counts map[Info]int64) string { - // Use a heuristic to find a suitable symbol to blame - // in the title: the first public function or method - // of a public type, in gopls, to appear in the stack - // trace. We can always refine it later. +func newIssue(pcfg ProgramConfig, stack, id, jsonURL string, counts map[Info]int64) string { + // Use a heuristic to find a suitable symbol to blame in the title: the + // first public function or method of a public type, in + // MatchSymbolPrefix, to appear in the stack trace. We can always + // refine it later. // // TODO(adonovan): include in the issue a source snippet ±5 // lines around the PC in this symbol. var symbol string +outer: for _, line := range strings.Split(stack, "\n") { - // Look for: - // gopls/.../pkg.Func - // gopls/.../pkg.Type.method - // gopls/.../pkg.(*Type).method - if strings.Contains(line, "internal/util/bug.") { - continue // not interesting + for _, s := range pcfg.IgnoreSymbolContains { + if strings.Contains(line, s) { + continue outer // not interesting + } } - if _, rest, ok := strings.Cut(line, "golang.org/x/tools/gopls/"); ok { + // Look for: + // pcfg.MatchSymbolPrefix/.../pkg.Func + // pcfg.MatchSymbolPrefix/.../pkg.Type.method + // pcfg.MatchSymbolPrefix/.../pkg.(*Type).method + if _, rest, ok := strings.Cut(line, pcfg.MatchSymbolPrefix); ok { if i := strings.IndexByte(rest, '.'); i >= 0 { rest = rest[i+1:] rest = strings.TrimPrefix(rest, "(*") @@ -500,7 +624,7 @@ func newIssue(stack, id string, jsonURL string, counts map[Info]int64) string { } // Populate the form (title, body, label) - title := fmt.Sprintf("x/tools/gopls: bug in %s", symbol) + title := fmt.Sprintf("%s: bug in %s", pcfg.NewIssuePrefix, symbol) body := new(bytes.Buffer) @@ -513,7 +637,7 @@ func newIssue(stack, id string, jsonURL string, counts map[Info]int64) string { writeStackComment(body, stack, id, jsonURL, counts) - const labels = "gopls,Tools,gopls/telemetry-wins,NeedsInvestigation" + labels := strings.Join(pcfg.NewIssueLabels, ",") // Report it. The user will interactively finish the task, // since they will typically de-dup it without even creating a new issue @@ -753,9 +877,12 @@ type Issue struct { CreatedAt time.Time `json:"created_at"` Body string // in Markdown format + // Set by readIssues. predicateText string // text of ```#!stacks...``` predicate block predicate func(string) bool // matching predicate over stack text - newStacks []string // new stacks to add to existing issue (comments and IDs) + + // Set by claimIssues. + newStacks []string // new stacks to add to existing issue (comments and IDs) } type User struct { @@ -808,6 +935,8 @@ func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { // Remove those stale directories now. _ = os.RemoveAll(revDir) // ignore errors + // TODO(prattmic): Consider using ProgramConfig + // configuration if we add more configurations. log.Printf("cloning tools@gopls/%s", info.ProgramVersion) if err := shallowClone(revDir, "https://go.googlesource.com/tools", "gopls/"+info.ProgramVersion); err != nil { _ = os.RemoveAll(revDir) // ignore errors From ee36e77d36dc98c9f3e11c4de7ad12a9e514a4b7 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Mon, 13 Jan 2025 17:37:22 +0000 Subject: [PATCH 030/309] gopls/internal/telemetry/cmd/stacks: support cmd/compile Updates golang/go#71045. Change-Id: I6a6a636cc93ecf5342a4577ad4faefc2e2bc0063 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642420 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 4964e53f8de..db36a34e1a6 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -145,6 +145,27 @@ var programs = map[string]ProgramConfig{ "internal/util/bug.", }, }, + "cmd/compile": { + Program: "cmd/compile", + SearchLabel: "compiler/telemetry-wins", + NewIssuePrefix: "cmd/compile", + NewIssueLabels: []string{ + "compiler/runtime", + "compiler/telemetry-wins", + "NeedsInvestigation", + }, + MatchSymbolPrefix: "cmd/compile", + IgnoreSymbolContains: []string{ + // Various "fatal" wrappers. + "Fatal", // base.Fatal*, ssa.Value.Fatal*, etc. + "cmd/compile/internal/base.Assert", + "cmd/compile/internal/noder.assert", + "cmd/compile/internal/ssa.Compile.func1", // basically a Fatalf wrapper. + // Panic recovery. + "cmd/compile/internal/types2.(*Checker).handleBailout", + "cmd/compile/internal/gc.handlePanic", + }, + }, } func main() { From fec8580380ebbc16029ad2d83bbd802e241272e6 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 29 Dec 2024 20:27:50 -0500 Subject: [PATCH 031/309] gopls/internal/analysis/modernize: replace loop with slices.Contains This CL adds a modernizer pass for slices.Contains{,Func}. Example: func assignTrueBreak(slice []int, needle int) { found := false for _, elem := range slice { // want "Loop can be simplified using strings.Contains" if elem == needle { found = true break } } print(found) } => func assignTrueBreak(slice []int, needle int) { found := slices.Contains(slice, needle) print(found) } Updates golang/go#70815 Change-Id: I72ad1c099481b6c9ae6f732e2d81674a98b79a9f Reviewed-on: https://go-review.googlesource.com/c/tools/+/640576 Auto-Submit: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan --- .../internal/analysis/modernize/modernize.go | 6 + .../analysis/modernize/modernize_test.go | 1 + gopls/internal/analysis/modernize/slices.go | 1 + .../analysis/modernize/slicescontains.go | 365 ++++++++++++++++++ .../src/slicescontains/slicescontains.go | 129 +++++++ .../slicescontains/slicescontains.go.golden | 85 ++++ internal/astutil/cursor/cursor.go | 6 +- 7 files changed, 591 insertions(+), 2 deletions(-) create mode 100644 gopls/internal/analysis/modernize/slicescontains.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 373461825d0..6cedc5eec73 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -49,6 +49,9 @@ func run(pass *analysis.Pass) (any, error) { } report := pass.Report pass.Report = func(diag analysis.Diagnostic) { + if diag.Category == "" { + panic("Diagnostic.Category is unset") + } if _, ok := generated[pass.Fset.File(diag.Pos)]; ok { return // skip checking if it's generated code } @@ -62,6 +65,7 @@ func run(pass *analysis.Pass) (any, error) { fmtappendf(pass) mapsloop(pass) minmax(pass) + slicescontains(pass) sortslice(pass) testingContext(pass) @@ -120,7 +124,9 @@ var ( builtinAny = types.Universe.Lookup("any") builtinAppend = types.Universe.Lookup("append") builtinBool = types.Universe.Lookup("bool") + builtinFalse = types.Universe.Lookup("false") builtinMake = types.Universe.Lookup("make") builtinNil = types.Universe.Lookup("nil") + builtinTrue = types.Universe.Lookup("true") byteSliceType = types.NewSlice(types.Typ[types.Byte]) ) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index bf3114e2382..d8d2d9a3d52 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -19,6 +19,7 @@ func Test(t *testing.T) { "fmtappendf", "mapsloop", "minmax", + "slicescontains", "sortslice", "testingcontext", ) diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index 13892989977..cb73f7e30cd 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -5,6 +5,7 @@ package modernize // This file defines modernizers that use the "slices" package. +// TODO(adonovan): actually let's split them up and rename this file. import ( "fmt" diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go new file mode 100644 index 00000000000..062083ca141 --- /dev/null +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -0,0 +1,365 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "fmt" + "go/ast" + "go/token" + "go/types" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typeparams" +) + +// The slicescontains pass identifies loops that can be replaced by a +// call to slices.Contains{,Func}. For example: +// +// for i, elem := range s { +// if elem == needle { +// ... +// break +// } +// } +// +// => +// +// if slices.Contains(s, needle) { ... } +// +// Variants: +// - if the if-condition is f(elem), the replacement +// uses slices.ContainsFunc(s, f). +// - if the if-body is "return true" and the fallthrough +// statement is "return false" (or vice versa), the +// loop becomes "return [!]slices.Contains(...)". +// - if the if-body is "found = true" and the previous +// statement is "found = false" (or vice versa), the +// loop becomes "found = [!]slices.Contains(...)". +// +// It may change cardinality of effects of the "needle" expression. +// (Mostly this appears to be a desirable optimization, avoiding +// redundantly repeated evaluation.) +func slicescontains(pass *analysis.Pass) { + // Don't modify the slices package itself. + if pass.Pkg.Path() == "slices" { + return + } + + info := pass.TypesInfo + + // check is called for each RangeStmt of this form: + // for i, elem := range s { if cond { ... } } + check := func(file *ast.File, curRange cursor.Cursor) { + rng := curRange.Node().(*ast.RangeStmt) + ifStmt := rng.Body.List[0].(*ast.IfStmt) + + // isSliceElem reports whether e denotes the + // current slice element (elem or s[i]). + isSliceElem := func(e ast.Expr) bool { + if rng.Value != nil && equalSyntax(e, rng.Value) { + return true // "elem" + } + if x, ok := e.(*ast.IndexExpr); ok && + equalSyntax(x.X, rng.X) && + equalSyntax(x.Index, rng.Key) { + return true // "s[i]" + } + return false + } + + // Examine the condition for one of these forms: + // + // - if elem or s[i] == needle { ... } => Contains + // - if predicate(s[i] or elem) { ... } => ContainsFunc + var ( + funcName string // "Contains" or "ContainsFunc" + arg2 ast.Expr // second argument to func (needle or predicate) + ) + switch cond := ifStmt.Cond.(type) { + case *ast.BinaryExpr: + if cond.Op == token.EQL { + if isSliceElem(cond.X) { + funcName = "Contains" + arg2 = cond.Y // "if elem == needle" + } else if isSliceElem(cond.Y) { + funcName = "Contains" + arg2 = cond.X // "if needle == elem" + } + } + + case *ast.CallExpr: + if len(cond.Args) == 1 && + isSliceElem(cond.Args[0]) && + typeutil.Callee(info, cond) != nil { // not a conversion + + funcName = "ContainsFunc" + arg2 = cond.Fun // "if predicate(elem)" + } + } + if funcName == "" { + return // not a candidate for Contains{,Func} + } + + // body is the "true" body. + body := ifStmt.Body + if len(body.List) == 0 { + // (We could perhaps delete the loop entirely.) + return + } + + // Reject if the body, needle or predicate references either range variable. + usesRangeVar := func(n ast.Node) bool { + cur, ok := curRange.FindNode(n) + if !ok { + panic(fmt.Sprintf("FindNode(%T) failed", n)) + } + return uses(info, cur, info.Defs[rng.Key.(*ast.Ident)]) || + rng.Value != nil && uses(info, cur, info.Defs[rng.Value.(*ast.Ident)]) + } + if usesRangeVar(body) { + // Body uses range var "i" or "elem". + // + // (The check for "i" could be relaxed when we + // generalize this to support slices.Index; + // and the check for "elem" could be relaxed + // if "elem" can safely be replaced in the + // body by "needle".) + return + } + if usesRangeVar(arg2) { + return + } + + // Prepare slices.Contains{,Func} call. + slicesName, importEdits := analysisinternal.AddImport(info, file, rng.Pos(), "slices", "slices") + contains := fmt.Sprintf("%s.%s(%s, %s)", + slicesName, + funcName, + analysisinternal.Format(pass.Fset, rng.X), + analysisinternal.Format(pass.Fset, arg2)) + + report := func(edits []analysis.TextEdit) { + pass.Report(analysis.Diagnostic{ + Pos: rng.Pos(), + End: rng.End(), + Category: "slicescontains", + Message: fmt.Sprintf("Loop can be simplified using slices.%s", funcName), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Replace loop by call to slices." + funcName, + TextEdits: append(edits, importEdits...), + }}, + }) + } + + // Last statement of body must return/break out of the loop. + curBody, _ := curRange.FindNode(body) + curLastStmt, _ := curBody.LastChild() + + // Reject if any statement in the body except the + // last has a free continuation (continue or break) + // that might affected by melting down the loop. + // + // TODO(adonovan): relax check by analyzing branch target. + for curBodyStmt := range curBody.Children() { + if curBodyStmt != curLastStmt { + for range curBodyStmt.Preorder((*ast.BranchStmt)(nil), (*ast.ReturnStmt)(nil)) { + return + } + } + } + + switch lastStmt := curLastStmt.Node().(type) { + case *ast.ReturnStmt: + // Have: for ... range seq { if ... { stmts; return x } } + + // Special case: + // body={ return true } next="return false" (or negation) + // => return [!]slices.Contains(...) + if curNext, ok := curRange.NextSibling(); ok { + nextStmt := curNext.Node().(ast.Stmt) + tval := isReturnTrueOrFalse(info, lastStmt) + fval := isReturnTrueOrFalse(info, nextStmt) + if len(body.List) == 1 && tval*fval < 0 { + // for ... { if ... { return true/false } } + // => return [!]slices.Contains(...) + report([]analysis.TextEdit{ + // Delete the range statement and following space. + { + Pos: rng.Pos(), + End: nextStmt.Pos(), + }, + // Change return to [!]slices.Contains(...). + { + Pos: nextStmt.Pos(), + End: nextStmt.End(), + NewText: fmt.Appendf(nil, "return %s%s", + cond(tval > 0, "", "!"), + contains), + }, + }) + return + } + } + + // General case: + // => if slices.Contains(...) { stmts; return x } + report([]analysis.TextEdit{ + // Replace "for ... { if ... " with "if slices.Contains(...)". + { + Pos: rng.Pos(), + End: ifStmt.Body.Pos(), + NewText: fmt.Appendf(nil, "if %s ", contains), + }, + // Delete '}' of range statement and preceding space. + { + Pos: ifStmt.Body.End(), + End: rng.End(), + }, + }) + return + + case *ast.BranchStmt: + if lastStmt.Tok == token.BREAK && lastStmt.Label == nil { // unlabeled break + // Have: for ... { if ... { stmts; break } } + + var prevStmt ast.Stmt // previous statement to range (if any) + if curPrev, ok := curRange.PrevSibling(); ok { + // If the RangeStmt's previous sibling is a Stmt, + // the RangeStmt must be among the Body list of + // a BlockStmt, CauseClause, or CommClause. + // In all cases, the prevStmt is the immediate + // predecessor of the RangeStmt during execution. + // + // (This is not true for Stmts in general; + // see [Cursor.Children] and #71074.) + prevStmt, _ = curPrev.Node().(ast.Stmt) + } + + // Special case: + // prev="lhs = false" body={ lhs = true; break } + // => lhs = slices.Contains(...) (or negation) + if assign, ok := body.List[0].(*ast.AssignStmt); ok && + len(body.List) == 2 && + assign.Tok == token.ASSIGN && + len(assign.Lhs) == 1 && + len(assign.Rhs) == 1 { + + // Have: body={ lhs = rhs; break } + + if prevAssign, ok := prevStmt.(*ast.AssignStmt); ok && + len(prevAssign.Lhs) == 1 && + len(prevAssign.Rhs) == 1 && + equalSyntax(prevAssign.Lhs[0], assign.Lhs[0]) && + is[*ast.Ident](assign.Rhs[0]) && + info.Uses[assign.Rhs[0].(*ast.Ident)] == builtinTrue { + + // Have: + // lhs = false + // for ... { if ... { lhs = true; break } } + // => + // lhs = slices.Contains(...) + // + // TODO(adonovan): + // - support "var lhs bool = false" and variants. + // - support negation. + // Both these variants seem quite significant. + // - allow the break to be omitted. + report([]analysis.TextEdit{ + // Replace "rhs" of previous assignment by slices.Contains(...) + { + Pos: prevAssign.Rhs[0].Pos(), + End: prevAssign.Rhs[0].End(), + NewText: []byte(contains), + }, + // Delete the loop and preceding space. + { + Pos: prevAssign.Rhs[0].End(), + End: rng.End(), + }, + }) + return + } + } + + // General case: + // for ... { if ... { stmts; break } } + // => if slices.Contains(...) { stmts } + report([]analysis.TextEdit{ + // Replace "for ... { if ... " with "if slices.Contains(...)". + { + Pos: rng.Pos(), + End: ifStmt.Body.Pos(), + NewText: fmt.Appendf(nil, "if %s ", contains), + }, + // Delete break statement and preceding space. + { + Pos: func() token.Pos { + if len(body.List) > 1 { + beforeBreak, _ := curLastStmt.PrevSibling() + return beforeBreak.Node().End() + } + return lastStmt.Pos() + }(), + End: lastStmt.End(), + }, + // Delete '}' of range statement and preceding space. + { + Pos: ifStmt.Body.End(), + End: rng.End(), + }, + }) + return + } + } + } + + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.21") { + file := curFile.Node().(*ast.File) + + for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) { + rng := curRange.Node().(*ast.RangeStmt) + + if is[*ast.Ident](rng.Key) && + rng.Tok == token.DEFINE && + len(rng.Body.List) == 1 && + is[*types.Slice](typeparams.CoreType(info.TypeOf(rng.X))) { + + // Have: + // - for _, elem := range s { S } + // - for i := range s { S } + + if ifStmt, ok := rng.Body.List[0].(*ast.IfStmt); ok && + ifStmt.Init == nil && ifStmt.Else == nil { + + // Have: for i, elem := range s { if cond { ... } } + check(file, curRange) + } + } + } + } +} + +// -- helpers -- + +// isReturnTrueOrFalse returns nonzero if stmt returns true (+1) or false (-1). +func isReturnTrueOrFalse(info *types.Info, stmt ast.Stmt) int { + if ret, ok := stmt.(*ast.ReturnStmt); ok && len(ret.Results) == 1 { + if id, ok := ret.Results[0].(*ast.Ident); ok { + switch info.Uses[id] { + case builtinTrue: + return +1 + case builtinFalse: + return -1 + } + } + } + return 0 +} diff --git a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go new file mode 100644 index 00000000000..ecb73719c0e --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go @@ -0,0 +1,129 @@ +package slicescontains + +import "slices" + +var _ = slices.Contains[[]int] // force import of "slices" to avoid duplicate import edits + +func nopeNoBreak(slice []int, needle int) { + for i := range slice { + if slice[i] == needle { + println("found") + } + } +} + +func rangeIndex(slice []int, needle int) { + for i := range slice { // want "Loop can be simplified using slices.Contains" + if slice[i] == needle { + println("found") + break + } + } +} + +func rangeValue(slice []int, needle int) { + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + println("found") + break + } + } +} + +func returns(slice []int, needle int) { + for i := range slice { // want "Loop can be simplified using slices.Contains" + if slice[i] == needle { + println("found") + return + } + } +} + +func assignTrueBreak(slice []int, needle int) { + found := false + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + found = true + break + } + } + print(found) +} + +func assignFalseBreak(slice []int, needle int) { // TODO: treat this specially like booleanTrue + found := true + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + found = false + break + } + } + print(found) +} + +func assignFalseBreakInSelectSwitch(slice []int, needle int) { + // Exercise RangeStmt in CommClause, CaseClause. + select { + default: + found := false + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + found = true + break + } + } + print(found) + } + switch { + default: + found := false + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + found = true + break + } + } + print(found) + } +} + +func returnTrue(slice []int, needle int) bool { + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + return true + } + } + return false +} + +func returnFalse(slice []int, needle int) bool { + for _, elem := range slice { // want "Loop can be simplified using slices.Contains" + if elem == needle { + return false + } + } + return true +} + +func containsFunc(slice []int, needle int) bool { + for _, elem := range slice { // want "Loop can be simplified using slices.ContainsFunc" + if predicate(elem) { + return true + } + } + return false +} + +func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool { + for _, elem := range slice { + if predicate(elem) { + if needle == 7 { + continue // this statement defeats loop elimination + } + return true + } + } + return false +} + +func predicate(int) bool diff --git a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden new file mode 100644 index 00000000000..561e42f7dd1 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden @@ -0,0 +1,85 @@ +package slicescontains + +import "slices" + +var _ = slices.Contains[[]int] // force import of "slices" to avoid duplicate import edits + +func nopeNoBreak(slice []int, needle int) { + for i := range slice { + if slice[i] == needle { + println("found") + } + } +} + +func rangeIndex(slice []int, needle int) { + if slices.Contains(slice, needle) { + println("found") + } +} + +func rangeValue(slice []int, needle int) { + if slices.Contains(slice, needle) { + println("found") + } +} + +func returns(slice []int, needle int) { + if slices.Contains(slice, needle) { + println("found") + return + } +} + +func assignTrueBreak(slice []int, needle int) { + found := slices.Contains(slice, needle) + print(found) +} + +func assignFalseBreak(slice []int, needle int) { // TODO: treat this specially like booleanTrue + found := true + if slices.Contains(slice, needle) { + found = false + } + print(found) +} + +func assignFalseBreakInSelectSwitch(slice []int, needle int) { + // Exercise RangeStmt in CommClause, CaseClause. + select { + default: + found := slices.Contains(slice, needle) + print(found) + } + switch { + default: + found := slices.Contains(slice, needle) + print(found) + } +} + +func returnTrue(slice []int, needle int) bool { + return slices.Contains(slice, needle) +} + +func returnFalse(slice []int, needle int) bool { + return !slices.Contains(slice, needle) +} + +func containsFunc(slice []int, needle int) bool { + return slices.ContainsFunc(slice, predicate) +} + +func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool { + for _, elem := range slice { + if predicate(elem) { + if needle == 7 { + continue // this statement defeats loop elimination + } + return true + } + } + return false +} + +func predicate(int) bool diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 89dd641c420..24fec99c8b3 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -304,8 +304,10 @@ func (c Cursor) LastChild() (Cursor, bool) { // - [ast.AssignStmt] (Lhs, Rhs) // // So, do not assume that the previous sibling of an ast.Stmt is also -// an ast.Stmt unless you have established that, say, its parent is a -// BlockStmt. +// an ast.Stmt, or if it is, that they are executed sequentially, +// unless you have established that, say, its parent is a BlockStmt. +// For example, given "for S1; ; S2 {}", the predecessor of S2 is S1, +// even though they are not executed in sequence. func (c Cursor) Children() iter.Seq[Cursor] { return func(yield func(Cursor) bool) { c, ok := c.FirstChild() From 73a70702fafa3c448945c6469f264b8c9f7c148b Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Mon, 13 Jan 2025 19:05:53 +0000 Subject: [PATCH 032/309] gopls/internal/telemetry/cmd/stacks: paginate issue search The GitHub API returns at most 100 results per page, but gopls already has 101 issues, so we are dropping issues and need pagination. The GitHub search API has a hard limit of 1000 results (https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28#about-search), which we'll hit eventually. As an alternative, use the "List repository issues" API (https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues). This allows filtering by label, which is all we need. Note that the old code was mistakenly searching all of GitHub, not just golang/go. That is now fixed. GitHub pagination uses an awkward header format (https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api?apiVersion=2022-11-28), but it is ultimately just ?page=1, ?page=2, etc, so we just keep trying new pages until we get no more results. Updates golang/go#71045. Change-Id: I6a6a636ccc17c9e1b1024369f98965f59456896a Reviewed-on: https://go-review.googlesource.com/c/tools/+/642436 Auto-Submit: Michael Pratt Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 88 ++++++++++++------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index db36a34e1a6..30a9f1ed220 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -391,19 +391,14 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 // predicates. func readIssues(pcfg ProgramConfig) ([]*Issue, error) { // Query GitHub for all existing GitHub issues with the report label. - // - // TODO(adonovan): by default GitHub returns at most 30 - // issues; we have lifted this to 100 using per_page=%d, but - // that won't work forever; use paging. - query := fmt.Sprintf("is:issue label:%s", pcfg.SearchLabel) - res, err := searchIssues(query) + issues, err := searchIssues(pcfg.SearchLabel) if err != nil { - log.Fatalf("GitHub issues query %q failed: %v", query, err) + log.Fatalf("GitHub issues label %q search failed: %v", pcfg.SearchLabel, err) } // Extract and validate predicate expressions in ```#!stacks...``` code blocks. // See the package doc comment for the grammar. - for _, issue := range res.Items { + for _, issue := range issues { block := findPredicateBlock(issue.Body) if block != "" { expr, err := parser.ParseExpr(block) @@ -480,7 +475,7 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { } } - return res.Items, nil + return issues, nil } // claimStack maps each stack ID to its issue (if any). @@ -798,28 +793,58 @@ func frameURL(pclntab map[string]FileLine, info Info, frame string) string { // -- GitHub search -- // searchIssues queries the GitHub issue tracker. -func searchIssues(query string) (*IssuesSearchResult, error) { - q := url.QueryEscape(query) +func searchIssues(label string) ([]*Issue, error) { + label = url.QueryEscape(label) - req, err := http.NewRequest("GET", "https://api.github.com/search/issues?q="+q+"&per_page=100", nil) - if err != nil { - return nil, err - } - req.Header.Add("Authorization", "Bearer "+authToken) - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("search query failed: %s (body: %s)", resp.Status, body) + // Slurp all issues with the telemetry label. + // + // The pagination link headers have an annoying format, but ultimately + // are just ?page=1, ?page=2, etc with no extra state. So just keep + // trying new pages until we get no more results. + // + // NOTE: With this scheme, GitHub clearly has no protection against + // race conditions, so presumably we could get duplicate issues or miss + // issues across pages. + + getPage := func(page int) ([]*Issue, error) { + url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues?state=all&labels=%s&per_page=100&page=%d", label, page) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + req.Header.Add("Authorization", "Bearer "+authToken) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("search query %s failed: %s (body: %s)", url, resp.Status, body) + } + var r []*Issue + if err := json.NewDecoder(resp.Body).Decode(&r); err != nil { + return nil, err + } + + return r, nil } - var result IssuesSearchResult - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - return nil, err + + var results []*Issue + for page := 1; ; page++ { + r, err := getPage(page) + if err != nil { + return nil, err + } + if len(r) == 0 { + // No more results. + break + } + + results = append(results, r...) } - return &result, nil + + return results, nil } // updateIssueBody updates the body of the numbered issue. @@ -882,12 +907,7 @@ func addIssueComment(number int, comment string) error { return nil } -// See https://developer.github.com/v3/search/#search-issues. - -type IssuesSearchResult struct { - TotalCount int `json:"total_count"` - Items []*Issue -} +// See https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues. type Issue struct { Number int From 09330211ae24158fd9a159878ea00649df005ff3 Mon Sep 17 00:00:00 2001 From: Tim King Date: Fri, 10 Jan 2025 15:38:41 -0800 Subject: [PATCH 033/309] go/ssa: remove coretype_test.go go/ssa/coretype_test.go was copied to internal/typeparams/coretype_test.go. Removing the old copy. Fixes error message in typeparams/coretype_test.go. Change-Id: If5553fb75b580411ea4d24923b38f15cccf1f0ba Reviewed-on: https://go-review.googlesource.com/c/tools/+/642156 Reviewed-by: Tim King Reviewed-by: Robert Griesemer LUCI-TryBot-Result: Go LUCI Auto-Submit: Tim King --- go/ssa/coretype_test.go | 101 --------------------------- internal/typeparams/coretype_test.go | 2 +- 2 files changed, 1 insertion(+), 102 deletions(-) delete mode 100644 go/ssa/coretype_test.go diff --git a/go/ssa/coretype_test.go b/go/ssa/coretype_test.go deleted file mode 100644 index 6fda54bf36a..00000000000 --- a/go/ssa/coretype_test.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2022 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa_test - -import ( - "go/ast" - "go/parser" - "go/token" - "go/types" - "testing" - - "golang.org/x/tools/internal/typeparams" -) - -func TestCoreType(t *testing.T) { - const source = ` - package P - - type Named int - - type A any - type B interface{~int} - type C interface{int} - type D interface{Named} - type E interface{~int|interface{Named}} - type F interface{~int|~float32} - type G interface{chan int|interface{chan int}} - type H interface{chan int|chan float32} - type I interface{chan<- int|chan int} - type J interface{chan int|chan<- int} - type K interface{<-chan int|chan int} - type L interface{chan int|<-chan int} - type M interface{chan int|chan Named} - type N interface{<-chan int|chan<- int} - type O interface{chan int|bool} - type P struct{ Named } - type Q interface{ Foo() } - type R interface{ Foo() ; Named } - type S interface{ Foo() ; ~int } - - type T interface{chan int|interface{chan int}|<-chan int} -` - - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, "hello.go", source, 0) - if err != nil { - t.Fatal(err) - } - - var conf types.Config - pkg, err := conf.Check("P", fset, []*ast.File{f}, nil) - if err != nil { - t.Fatal(err) - } - - for _, test := range []struct { - expr string // type expression of Named type - want string // expected core type (or "" if none) - }{ - {"Named", "int"}, // Underlying type is not interface. - {"A", ""}, // Interface has no terms. - {"B", "int"}, // Tilde term. - {"C", "int"}, // Non-tilde term. - {"D", "int"}, // Named term. - {"E", "int"}, // Identical underlying types. - {"F", ""}, // Differing underlying types. - {"G", "chan int"}, // Identical Element types. - {"H", ""}, // Element type int has differing underlying type to float32. - {"I", "chan<- int"}, // SendRecv followed by SendOnly - {"J", "chan<- int"}, // SendOnly followed by SendRecv - {"K", "<-chan int"}, // RecvOnly followed by SendRecv - {"L", "<-chan int"}, // SendRecv followed by RecvOnly - {"M", ""}, // Element type int is not *identical* to Named. - {"N", ""}, // Differing channel directions - {"O", ""}, // A channel followed by a non-channel. - {"P", "struct{P.Named}"}, // Embedded type. - {"Q", ""}, // interface type with no terms and functions - {"R", "int"}, // interface type with both terms and functions. - {"S", "int"}, // interface type with a tilde term - {"T", "<-chan int"}, // Prefix of 2 terms that are identical before switching to channel. - } { - // Eval() expr for its type. - tv, err := types.Eval(fset, pkg, 0, test.expr) - if err != nil { - t.Fatalf("Eval(%s) failed: %v", test.expr, err) - } - - ct := typeparams.CoreType(tv.Type) - var got string - if ct == nil { - got = "" - } else { - got = ct.String() - } - if got != test.want { - t.Errorf("CoreType(%s) = %v, want %v", test.expr, got, test.want) - } - } -} diff --git a/internal/typeparams/coretype_test.go b/internal/typeparams/coretype_test.go index a9575f9238e..371d9f8ed31 100644 --- a/internal/typeparams/coretype_test.go +++ b/internal/typeparams/coretype_test.go @@ -95,7 +95,7 @@ func TestCoreType(t *testing.T) { got = ct.String() } if got != test.want { - t.Errorf("coreType(%s) = %v, want %v", test.expr, got, test.want) + t.Errorf("CoreType(%s) = %v, want %v", test.expr, got, test.want) } } } From c9ef86130aa63c351b070521a15db6895d0bcdd9 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Tue, 14 Jan 2025 09:07:01 -0500 Subject: [PATCH 034/309] gopls/internal/telemetry/cmd/stacks: don't forward GOEXPERIMENT from env The test being executed may have some GOEXPERIMENT set. When invoking another go toolchain, since GOTOOLCHAIN is set to a different version that might be older and not support the same GOEXPERIMENT values, let it run with its own default experiments. Fixes golang/go#71260. Change-Id: Iadea59fbd8bf7a11e636208567ca0fd23cdb7fa1 Cq-Include-Trybots: luci.golang.try:x_tools-gotip-linux-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/tools/+/641859 LUCI-TryBot-Result: Go LUCI Auto-Submit: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 30a9f1ed220..f7b289fc070 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -1011,6 +1011,7 @@ func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { cmd.Dir = buildDir cmd.Env = append(os.Environ(), "GOTOOLCHAIN="+info.GoVersion, + "GOEXPERIMENT=", // Don't forward GOEXPERIMENT from current environment since the GOTOOLCHAIN selected might not support the same experiments. "GOOS="+info.GOOS, "GOARCH="+info.GOARCH, "GOWORK=off", @@ -1026,6 +1027,7 @@ func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { cmd.Stderr = os.Stderr cmd.Env = append(os.Environ(), "GOTOOLCHAIN="+info.GoVersion, + "GOEXPERIMENT=", // Don't forward GOEXPERIMENT from current environment since the GOTOOLCHAIN selected might not support the same experiments. "GOOS="+info.GOOS, "GOARCH="+info.GOARCH, ) From 4403100389e1e2f337e097f0b7b27293c6f05c91 Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Thu, 9 Jan 2025 17:18:47 -0500 Subject: [PATCH 035/309] gopls/internal/golang: customize semantic token types and modifiers We agreed to return full set of token types and modifiers from gopls by default (full means token types and modifiers that gopls understand) and provide configuraton options for users to disable some of them. - Two fields of type map[string]bool are introduced to gopls UIOptions (workspace/configuration) to customize semantic token types and modifiers. For now, only value of "false" is effective. Choose type of map over array to keep future compatibility in case we want to introduce enable capabilities. - VSCode-Go populate these options from user settings to gopls. - Gopls "initialize" protocol returns a pre-defined fixed legend including subset of standard legend defined LSP that gopls understand with additional customize modifiers gopls recoganized. - Gopls "textDocument/semanticTokens" protocol returns token types and modifiers based on configuration defined in workspace/configuration. Tested with vscode-go changes CL 642416, screenshot is at https://github.com/golang/vscode-go/issues/3632#issuecomment-2584506689 For golang/vscode-go#3632 Change-Id: Ie8220e12a4c8d6c84c54992d84277767e61ec023 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642077 Auto-Submit: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/settings.md | 24 +++- gopls/internal/cmd/cmd.go | 14 +- gopls/internal/cmd/semantictokens.go | 62 ++++++--- gopls/internal/doc/api.json | 28 +++- gopls/internal/golang/semtok.go | 16 +-- gopls/internal/protocol/semantic.go | 58 -------- gopls/internal/protocol/semtok/semtok.go | 131 ++++++++++++------ gopls/internal/server/general.go | 6 +- gopls/internal/settings/settings.go | 44 +++++- gopls/internal/template/implementations.go | 9 +- .../testdata/token/builtin_constant.txt | 21 --- .../test/marker/testdata/token/modifiers.txt | 61 ++++++++ gopls/internal/util/moreslices/slices.go | 10 ++ 13 files changed, 313 insertions(+), 171 deletions(-) delete mode 100644 gopls/internal/protocol/semantic.go delete mode 100644 gopls/internal/test/marker/testdata/token/builtin_constant.txt create mode 100644 gopls/internal/test/marker/testdata/token/modifiers.txt diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index 1350e8f7840..7dfe0870718 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -215,10 +215,32 @@ Default: `false`. **This setting is experimental and may be deleted.** -noSemanticNumber turns off the sending of the semantic token 'number' +noSemanticNumber turns off the sending of the semantic token 'number' Default: `false`. + +### `semanticTokenTypes map[string]bool` + +**This setting is experimental and may be deleted.** + +semanticTokenTypes configures the semantic token types. It allows +disabling types by setting each value to false. +By default, all types are enabled. + +Default: `{}`. + + +### `semanticTokenModifiers map[string]bool` + +**This setting is experimental and may be deleted.** + +semanticTokenModifiers configures the semantic token modifiers. It allows +disabling modifiers by setting each value to false. +By default, all modifiers are enabled. + +Default: `{}`. + ## Completion diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index d27542f79fb..1338773016b 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -27,10 +27,12 @@ import ( "golang.org/x/tools/gopls/internal/lsprpc" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/protocol/command" + "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/server" "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/util/browser" bugpkg "golang.org/x/tools/gopls/internal/util/bug" + "golang.org/x/tools/gopls/internal/util/moreslices" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/tool" @@ -299,7 +301,7 @@ func (app *Application) featureCommands() []tool.Application { &prepareRename{app: app}, &references{app: app}, &rename{app: app}, - &semtok{app: app}, + &semanticToken{app: app}, &signature{app: app}, &stats{app: app}, &symbols{app: app}, @@ -322,7 +324,6 @@ func (app *Application) connect(ctx context.Context) (*connection, error) { options := settings.DefaultOptions(app.options) svr = server.New(cache.NewSession(ctx, cache.New(nil)), client, options) ctx = protocol.WithClient(ctx, client) - } else { // remote netConn, err := lsprpc.ConnectToRemote(ctx, app.Remote) @@ -362,8 +363,8 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti params.Capabilities.TextDocument.SemanticTokens.Requests.Range = &protocol.Or_ClientSemanticTokensRequestOptions_range{Value: true} //params.Capabilities.TextDocument.SemanticTokens.Requests.Range.Value = true params.Capabilities.TextDocument.SemanticTokens.Requests.Full = &protocol.Or_ClientSemanticTokensRequestOptions_full{Value: true} - params.Capabilities.TextDocument.SemanticTokens.TokenTypes = protocol.SemanticTypes() - params.Capabilities.TextDocument.SemanticTokens.TokenModifiers = protocol.SemanticModifiers() + params.Capabilities.TextDocument.SemanticTokens.TokenTypes = moreslices.ConvertStrings[string](semtok.TokenTypes) + params.Capabilities.TextDocument.SemanticTokens.TokenModifiers = moreslices.ConvertStrings[string](semtok.TokenModifiers) params.Capabilities.TextDocument.CodeAction = protocol.CodeActionClientCapabilities{ CodeActionLiteralSupport: protocol.ClientCodeActionLiteralOptions{ CodeActionKind: protocol.ClientCodeActionKindOptions{ @@ -376,7 +377,7 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti params.InitializationOptions = map[string]interface{}{ "symbolMatcher": string(opts.SymbolMatcher), } - if _, err := c.Server.Initialize(ctx, params); err != nil { + if c.initializeResult, err = c.Server.Initialize(ctx, params); err != nil { return err } if err := c.Server.Initialized(ctx, &protocol.InitializedParams{}); err != nil { @@ -388,6 +389,9 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti type connection struct { protocol.Server client *cmdClient + // initializeResult keep the initialize protocol response from server + // including server capabilities. + initializeResult *protocol.InitializeResult } // cmdClient defines the protocol.Client interface behavior of the gopls CLI tool. diff --git a/gopls/internal/cmd/semantictokens.go b/gopls/internal/cmd/semantictokens.go index 77e8a03939c..8d3dff68e2b 100644 --- a/gopls/internal/cmd/semantictokens.go +++ b/gopls/internal/cmd/semantictokens.go @@ -14,6 +14,7 @@ import ( "unicode/utf8" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/settings" ) @@ -40,15 +41,15 @@ import ( // 0-based: lines and character positions are 1 less than in // the gopls coordinate system -type semtok struct { +type semanticToken struct { app *Application } -func (c *semtok) Name() string { return "semtok" } -func (c *semtok) Parent() string { return c.app.Name() } -func (c *semtok) Usage() string { return "" } -func (c *semtok) ShortHelp() string { return "show semantic tokens for the specified file" } -func (c *semtok) DetailedHelp(f *flag.FlagSet) { +func (c *semanticToken) Name() string { return "semtok" } +func (c *semanticToken) Parent() string { return c.app.Name() } +func (c *semanticToken) Usage() string { return "" } +func (c *semanticToken) ShortHelp() string { return "show semantic tokens for the specified file" } +func (c *semanticToken) DetailedHelp(f *flag.FlagSet) { fmt.Fprint(f.Output(), ` Example: show the semantic tokens for this file: @@ -59,7 +60,7 @@ Example: show the semantic tokens for this file: // Run performs the semtok on the files specified by args and prints the // results to stdout in the format described above. -func (c *semtok) Run(ctx context.Context, args ...string) error { +func (c *semanticToken) Run(ctx context.Context, args ...string) error { if len(args) != 1 { return fmt.Errorf("expected one file name, got %d", len(args)) } @@ -97,14 +98,16 @@ func (c *semtok) Run(ctx context.Context, args ...string) error { if err != nil { return err } - return decorate(file, resp.Data) + return decorate(conn.initializeResult.Capabilities.SemanticTokensProvider.(protocol.SemanticTokensOptions).Legend, file, resp.Data) } +// mark provides a human-readable representation of protocol.SemanticTokens. +// It translates token types and modifiers to strings instead of uint32 values. type mark struct { line, offset int // 1-based, from RangeSpan len int // bytes, not runes - typ string - mods []string + typ semtok.Type + mods []semtok.Modifier } // prefixes for semantic token comments @@ -136,8 +139,10 @@ func markLine(m mark, lines [][]byte) { lines[m.line-1] = l } -func decorate(file *cmdFile, result []uint32) error { - marks := newMarks(file, result) +// decorate translates semantic token data (protocol.SemanticTokens) from its +// raw []uint32 format into a human-readable representation and prints it to stdout. +func decorate(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32) error { + marks := newMarks(legend, file, data) if len(marks) == 0 { return nil } @@ -150,25 +155,25 @@ func decorate(file *cmdFile, result []uint32) error { return nil } -func newMarks(file *cmdFile, d []uint32) []mark { +func newMarks(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32) []mark { ans := []mark{} // the following two loops could be merged, at the cost // of making the logic slightly more complicated to understand // first, convert from deltas to absolute, in LSP coordinates - lspLine := make([]uint32, len(d)/5) - lspChar := make([]uint32, len(d)/5) + lspLine := make([]uint32, len(data)/5) + lspChar := make([]uint32, len(data)/5) var line, char uint32 - for i := 0; 5*i < len(d); i++ { - lspLine[i] = line + d[5*i+0] - if d[5*i+0] > 0 { + for i := 0; 5*i < len(data); i++ { + lspLine[i] = line + data[5*i+0] + if data[5*i+0] > 0 { char = 0 } - lspChar[i] = char + d[5*i+1] + lspChar[i] = char + data[5*i+1] char = lspChar[i] line = lspLine[i] } // second, convert to gopls coordinates - for i := 0; 5*i < len(d); i++ { + for i := 0; 5*i < len(data); i++ { pr := protocol.Range{ Start: protocol.Position{ Line: lspLine[i], @@ -176,19 +181,30 @@ func newMarks(file *cmdFile, d []uint32) []mark { }, End: protocol.Position{ Line: lspLine[i], - Character: lspChar[i] + d[5*i+2], + Character: lspChar[i] + data[5*i+2], }, } spn, err := file.rangeSpan(pr) if err != nil { log.Fatal(err) } + + var mods []semtok.Modifier + { + n := int(data[5*i+4]) + for i, mod := range legend.TokenModifiers { + if (n & (1 << i)) != 0 { + mods = append(mods, semtok.Modifier(mod)) + } + } + } + m := mark{ line: spn.Start().Line(), offset: spn.Start().Column(), len: spn.End().Column() - spn.Start().Column(), - typ: protocol.SemType(int(d[5*i+3])), - mods: protocol.SemMods(int(d[5*i+4])), + typ: semtok.Type(legend.TokenTypes[data[5*i+3]]), + mods: mods, } ans = append(ans, m) } diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b5673b6232f..b9f843fc63c 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -847,7 +847,7 @@ { "Name": "noSemanticNumber", "Type": "bool", - "Doc": "noSemanticNumber turns off the sending of the semantic token 'number'\n", + "Doc": "noSemanticNumber turns off the sending of the semantic token 'number'\n", "EnumKeys": { "ValueType": "", "Keys": null @@ -857,6 +857,32 @@ "Status": "experimental", "Hierarchy": "ui" }, + { + "Name": "semanticTokenTypes", + "Type": "map[string]bool", + "Doc": "semanticTokenTypes configures the semantic token types. It allows\ndisabling types by setting each value to false.\nBy default, all types are enabled.\n", + "EnumKeys": { + "ValueType": "", + "Keys": null + }, + "EnumValues": null, + "Default": "{}", + "Status": "experimental", + "Hierarchy": "ui" + }, + { + "Name": "semanticTokenModifiers", + "Type": "map[string]bool", + "Doc": "semanticTokenModifiers configures the semantic token modifiers. It allows\ndisabling modifiers by setting each value to false.\nBy default, all modifiers are enabled.\n", + "EnumKeys": { + "ValueType": "", + "Keys": null + }, + "EnumValues": null, + "Default": "{}", + "Status": "experimental", + "Hierarchy": "ui" + }, { "Name": "local", "Type": "string", diff --git a/gopls/internal/golang/semtok.go b/gopls/internal/golang/semtok.go index 2043f9aaacc..84fad43a47f 100644 --- a/gopls/internal/golang/semtok.go +++ b/gopls/internal/golang/semtok.go @@ -82,10 +82,8 @@ func SemanticTokens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handl return &protocol.SemanticTokens{ Data: semtok.Encode( tv.tokens, - snapshot.Options().NoSemanticString, - snapshot.Options().NoSemanticNumber, - snapshot.Options().SemanticTypes, - snapshot.Options().SemanticMods), + snapshot.Options().EnabledSemanticTokenTypes(), + snapshot.Options().EnabledSemanticTokenModifiers()), ResultID: time.Now().String(), // for delta requests, but we've never seen any }, nil } @@ -242,7 +240,7 @@ func (tv *tokenVisitor) comment(c *ast.Comment, importByName map[string]*types.P } // token emits a token of the specified extent and semantics. -func (tv *tokenVisitor) token(start token.Pos, length int, typ semtok.TokenType, modifiers ...semtok.Modifier) { +func (tv *tokenVisitor) token(start token.Pos, length int, typ semtok.Type, modifiers ...semtok.Modifier) { if !start.IsValid() { return } @@ -463,7 +461,7 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) { return true } -func (tv *tokenVisitor) appendObjectModifiers(mods []semtok.Modifier, obj types.Object) (semtok.TokenType, []semtok.Modifier) { +func (tv *tokenVisitor) appendObjectModifiers(mods []semtok.Modifier, obj types.Object) (semtok.Type, []semtok.Modifier) { if obj.Pkg() == nil { mods = append(mods, semtok.ModDefaultLibrary) } @@ -559,7 +557,7 @@ func appendTypeModifiers(mods []semtok.Modifier, t types.Type) []semtok.Modifier func (tv *tokenVisitor) ident(id *ast.Ident) { var ( - tok semtok.TokenType + tok semtok.Type mods []semtok.Modifier obj types.Object ok bool @@ -623,7 +621,7 @@ func (tv *tokenVisitor) isParam(pos token.Pos) bool { // def), use the parse stack. // A lot of these only happen when the package doesn't compile, // but in that case it is all best-effort from the parse tree. -func (tv *tokenVisitor) unkIdent(id *ast.Ident) (semtok.TokenType, []semtok.Modifier) { +func (tv *tokenVisitor) unkIdent(id *ast.Ident) (semtok.Type, []semtok.Modifier) { def := []semtok.Modifier{semtok.ModDefinition} n := len(tv.stack) - 2 // parent of Ident; stack is [File ... Ident] if n < 0 { @@ -746,7 +744,7 @@ func (tv *tokenVisitor) unkIdent(id *ast.Ident) (semtok.TokenType, []semtok.Modi } // multiline emits a multiline token (`string` or /*comment*/). -func (tv *tokenVisitor) multiline(start, end token.Pos, tok semtok.TokenType) { +func (tv *tokenVisitor) multiline(start, end token.Pos, tok semtok.Type) { // TODO(adonovan): test with non-ASCII. f := tv.fset.File(start) diff --git a/gopls/internal/protocol/semantic.go b/gopls/internal/protocol/semantic.go deleted file mode 100644 index 23356dd8ef2..00000000000 --- a/gopls/internal/protocol/semantic.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package protocol - -// The file defines helpers for semantics tokens. - -import "fmt" - -// SemanticTypes to use in case there is no client, as in the command line, or tests. -func SemanticTypes() []string { - return semanticTypes[:] -} - -// SemanticModifiers to use in case there is no client. -func SemanticModifiers() []string { - return semanticModifiers[:] -} - -// SemType returns a string equivalent of the type, for gopls semtok -func SemType(n int) string { - tokTypes := SemanticTypes() - tokMods := SemanticModifiers() - if n >= 0 && n < len(tokTypes) { - return tokTypes[n] - } - // not found for some reason - return fmt.Sprintf("?%d[%d,%d]?", n, len(tokTypes), len(tokMods)) -} - -// SemMods returns the []string equivalent of the mods, for gopls semtok. -func SemMods(n int) []string { - tokMods := SemanticModifiers() - mods := []string{} - for i := 0; i < len(tokMods); i++ { - if (n & (1 << uint(i))) != 0 { - mods = append(mods, tokMods[i]) - } - } - return mods -} - -// From https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens -var ( - semanticTypes = [...]string{ - "namespace", "type", "class", "enum", "interface", - "struct", "typeParameter", "parameter", "variable", "property", "enumMember", - "event", "function", "method", "macro", "keyword", "modifier", "comment", - "string", "number", "regexp", "operator", - } - semanticModifiers = [...]string{ - "declaration", "definition", "readonly", "static", - "deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary", - // Additional modifiers - "interface", "struct", "signature", "pointer", "array", "map", "slice", "chan", "string", "number", "bool", "invalid", - } -) diff --git a/gopls/internal/protocol/semtok/semtok.go b/gopls/internal/protocol/semtok/semtok.go index fc269c38759..a40f2b5482f 100644 --- a/gopls/internal/protocol/semtok/semtok.go +++ b/gopls/internal/protocol/semtok/semtok.go @@ -11,33 +11,35 @@ import "sort" type Token struct { Line, Start uint32 Len uint32 - Type TokenType + Type Type Modifiers []Modifier } -type TokenType string +type Type string const ( // These are the tokens defined by LSP 3.18, but a client is // free to send its own set; any tokens that the server emits // that are not in this set are simply not encoded in the bitfield. + TokComment Type = "comment" // for a comment + TokFunction Type = "function" // for a function + TokKeyword Type = "keyword" // for a keyword + TokLabel Type = "label" // for a control label (LSP 3.18) + TokMacro Type = "macro" // for text/template tokens + TokMethod Type = "method" // for a method + TokNamespace Type = "namespace" // for an imported package name + TokNumber Type = "number" // for a numeric literal + TokOperator Type = "operator" // for an operator + TokParameter Type = "parameter" // for a parameter variable + TokString Type = "string" // for a string literal + TokType Type = "type" // for a type name (plus other uses) + TokTypeParam Type = "typeParameter" // for a type parameter + TokVariable Type = "variable" // for a var or const + // The section below defines a subset of token types in standard token types + // that gopls does not use. // - // If you add or uncomment a token type, document it in + // If you move types to above, document it in // gopls/doc/features/passive.md#semantic-tokens. - TokComment TokenType = "comment" // for a comment - TokFunction TokenType = "function" // for a function - TokKeyword TokenType = "keyword" // for a keyword - TokLabel TokenType = "label" // for a control label (LSP 3.18) - TokMacro TokenType = "macro" // for text/template tokens - TokMethod TokenType = "method" // for a method - TokNamespace TokenType = "namespace" // for an imported package name - TokNumber TokenType = "number" // for a numeric literal - TokOperator TokenType = "operator" // for an operator - TokParameter TokenType = "parameter" // for a parameter variable - TokString TokenType = "string" // for a string literal - TokType TokenType = "type" // for a type name (plus other uses) - TokTypeParam TokenType = "typeParameter" // for a type parameter - TokVariable TokenType = "variable" // for a var or const // TokClass TokenType = "class" // TokDecorator TokenType = "decorator" // TokEnum TokenType = "enum" @@ -50,24 +52,47 @@ const ( // TokStruct TokenType = "struct" ) +// TokenTypes is a slice of types gopls will return as its server capabilities. +var TokenTypes = []Type{ + TokNamespace, + TokType, + TokTypeParam, + TokParameter, + TokVariable, + TokFunction, + TokMethod, + TokMacro, + TokKeyword, + TokComment, + TokString, + TokNumber, + TokOperator, + TokLabel, +} + type Modifier string const ( // LSP 3.18 standard modifiers // As with TokenTypes, clients get only the modifiers they request. // - // If you add or uncomment a modifier, document it in - // gopls/doc/features/passive.md#semantic-tokens. + // The section below defines a subset of modifiers in standard modifiers + // that gopls understand. ModDefaultLibrary Modifier = "defaultLibrary" // for predeclared symbols ModDefinition Modifier = "definition" // for the declaring identifier of a symbol ModReadonly Modifier = "readonly" // for constants (TokVariable) - // ModAbstract Modifier = "abstract" - // ModAsync Modifier = "async" - // ModDeclaration Modifier = "declaration" - // ModDeprecated Modifier = "deprecated" - // ModDocumentation Modifier = "documentation" - // ModModification Modifier = "modification" - // ModStatic Modifier = "static" + // The section below defines the rest of the modifiers in standard modifiers + // that gopls does not use. + // + // If you move modifiers to above, document it in + // gopls/doc/features/passive.md#semantic-tokens. + // ModAbstract Modifier = "abstract" + // ModAsync Modifier = "async" + // ModDeclaration Modifier = "declaration" + // ModDeprecated Modifier = "deprecated" + // ModDocumentation Modifier = "documentation" + // ModModification Modifier = "modification" + // ModStatic Modifier = "static" // non-standard modifiers // @@ -87,13 +112,35 @@ const ( ModStruct Modifier = "struct" ) +// TokenModifiers is a slice of modifiers gopls will return as its server +// capabilities. +var TokenModifiers = []Modifier{ + // LSP 3.18 standard modifiers. + ModDefinition, + ModReadonly, + ModDefaultLibrary, + // Additional custom modifiers. + ModArray, + ModBool, + ModChan, + ModInterface, + ModMap, + ModNumber, + ModPointer, + ModSignature, + ModSlice, + ModString, + ModStruct, +} + // Encode returns the LSP encoding of a sequence of tokens. -// The noStrings, noNumbers options cause strings, numbers to be skipped. -// The lists of types and modifiers determines the bitfield encoding. +// encodeType and encodeModifier maps control which types and modifiers are +// excluded in the response. If a type or modifier maps to false, it will be +// omitted from the output. func Encode( tokens []Token, - noStrings, noNumbers bool, - types, modifiers []string) []uint32 { + encodeType map[Type]bool, + encodeModifier map[Modifier]bool) []uint32 { // binary operators, at least, will be out of order sort.Slice(tokens, func(i, j int) bool { @@ -103,17 +150,23 @@ func Encode( return tokens[i].Start < tokens[j].Start }) - typeMap := make(map[TokenType]int) - for i, t := range types { - typeMap[TokenType(t)] = i + typeMap := make(map[Type]int) + for i, t := range TokenTypes { + if enable, ok := encodeType[t]; ok && !enable { + continue + } + typeMap[Type(t)] = i } modMap := make(map[Modifier]int) - for i, m := range modifiers { + for i, m := range TokenModifiers { + if enable, ok := encodeModifier[m]; ok && !enable { + continue + } modMap[Modifier(m)] = 1 << i } - // each semantic token needs five values + // each semantic token needs five values but some tokens might be skipped. // (see Integer Encoding for Tokens in the LSP spec) x := make([]uint32, 5*len(tokens)) var j int @@ -122,13 +175,7 @@ func Encode( item := tokens[i] typ, ok := typeMap[item.Type] if !ok { - continue // client doesn't want typeStr - } - if item.Type == TokString && noStrings { - continue - } - if item.Type == TokNumber && noNumbers { - continue + continue // client doesn't want semantic token info. } if j == 0 { x[0] = tokens[0].Line diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go index 3a3a5efcd70..35614945f9d 100644 --- a/gopls/internal/server/general.go +++ b/gopls/internal/server/general.go @@ -26,10 +26,12 @@ import ( debuglog "golang.org/x/tools/gopls/internal/debug/log" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/goversion" "golang.org/x/tools/gopls/internal/util/moremaps" + "golang.org/x/tools/gopls/internal/util/moreslices" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/jsonrpc2" ) @@ -163,8 +165,8 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ Range: &protocol.Or_SemanticTokensOptions_range{Value: true}, Full: &protocol.Or_SemanticTokensOptions_full{Value: true}, Legend: protocol.SemanticTokensLegend{ - TokenTypes: protocol.NonNilSlice(options.SemanticTypes), - TokenModifiers: protocol.NonNilSlice(options.SemanticMods), + TokenTypes: moreslices.ConvertStrings[string](semtok.TokenTypes), + TokenModifiers: moreslices.ConvertStrings[string](semtok.TokenModifiers), }, }, SignatureHelpProvider: &protocol.SignatureHelpOptions{ diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index cd0c77e3c82..496062c40ec 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -13,6 +13,7 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/util/frob" ) @@ -173,8 +174,18 @@ type UIOptions struct { // NoSemanticString turns off the sending of the semantic token 'string' NoSemanticString bool `status:"experimental"` - // NoSemanticNumber turns off the sending of the semantic token 'number' + // NoSemanticNumber turns off the sending of the semantic token 'number' NoSemanticNumber bool `status:"experimental"` + + // SemanticTokenTypes configures the semantic token types. It allows + // disabling types by setting each value to false. + // By default, all types are enabled. + SemanticTokenTypes map[string]bool `status:"experimental"` + + // SemanticTokenModifiers configures the semantic token modifiers. It allows + // disabling modifiers by setting each value to false. + // By default, all modifiers are enabled. + SemanticTokenModifiers map[string]bool `status:"experimental"` } // A CodeLensSource identifies an (algorithmic) source of code lenses. @@ -1082,12 +1093,19 @@ func (o *Options) setOne(name string, value any) error { case "semanticTokens": return setBool(&o.SemanticTokens, value) + // TODO(hxjiang): deprecate noSemanticString and noSemanticNumber. case "noSemanticString": return setBool(&o.NoSemanticString, value) case "noSemanticNumber": return setBool(&o.NoSemanticNumber, value) + case "semanticTokenTypes": + return setBoolMap(&o.SemanticTokenTypes, value) + + case "semanticTokenModifiers": + return setBoolMap(&o.SemanticTokenModifiers, value) + case "expandWorkspaceToModule": // See golang/go#63536: we can consider deprecating // expandWorkspaceToModule, but probably need to change the default @@ -1233,6 +1251,30 @@ func (o *Options) setOne(name string, value any) error { return nil } +// EnabledSemanticTokenModifiers returns a map of modifiers to boolean. +func (o *Options) EnabledSemanticTokenModifiers() map[semtok.Modifier]bool { + copy := make(map[semtok.Modifier]bool, len(o.SemanticTokenModifiers)) + for k, v := range o.SemanticTokenModifiers { + copy[semtok.Modifier(k)] = v + } + return copy +} + +// EncodeSemanticTokenTypes returns a map of types to boolean. +func (o *Options) EnabledSemanticTokenTypes() map[semtok.Type]bool { + copy := make(map[semtok.Type]bool, len(o.SemanticTokenTypes)) + for k, v := range o.SemanticTokenModifiers { + copy[semtok.Type(k)] = v + } + if o.NoSemanticString { + copy[semtok.TokString] = false + } + if o.NoSemanticNumber { + copy[semtok.TokNumber] = false + } + return copy +} + // A SoftError is an error that does not affect the functionality of gopls. type SoftError struct { msg string diff --git a/gopls/internal/template/implementations.go b/gopls/internal/template/implementations.go index 19a27620b57..4ed485cfee2 100644 --- a/gopls/internal/template/implementations.go +++ b/gopls/internal/template/implementations.go @@ -199,15 +199,8 @@ func SemanticTokens(ctx context.Context, snapshot *cache.Snapshot, spn protocol. line, col := p.LineCol(t.Start) add(line, col, uint32(sz)) } - const noStrings = false - const noNumbers = false ans := &protocol.SemanticTokens{ - Data: semtok.Encode( - items, - noStrings, - noNumbers, - snapshot.Options().SemanticTypes, - snapshot.Options().SemanticMods), + Data: semtok.Encode(items, nil, nil), // for small cache, some day. for now, the LSP client ignores this // (that is, when the LSP client starts returning these, we can cache) ResultID: fmt.Sprintf("%v", time.Now()), diff --git a/gopls/internal/test/marker/testdata/token/builtin_constant.txt b/gopls/internal/test/marker/testdata/token/builtin_constant.txt deleted file mode 100644 index 79736d625b7..00000000000 --- a/gopls/internal/test/marker/testdata/token/builtin_constant.txt +++ /dev/null @@ -1,21 +0,0 @@ -This test checks semanticTokens on builtin constants. -(test for #70219.) - --- settings.json -- -{ - "semanticTokens": true -} - --- flags -- --ignore_extra_diags - --- default_lib_const.go -- -package p - -func _() { - a, b := false, true //@ token("false", "variable", "readonly defaultLibrary"), token("true", "variable", "readonly defaultLibrary") -} - -const ( - c = iota //@ token("iota", "variable", "readonly defaultLibrary number") -) diff --git a/gopls/internal/test/marker/testdata/token/modifiers.txt b/gopls/internal/test/marker/testdata/token/modifiers.txt new file mode 100644 index 00000000000..86789e3b956 --- /dev/null +++ b/gopls/internal/test/marker/testdata/token/modifiers.txt @@ -0,0 +1,61 @@ +This test checks the output of semanticTokens modifiers. +(including test for #70219.) + +-- settings.json -- +{ + "semanticTokens": true +} + +-- flags -- +-ignore_extra_diags + +-- standard.go -- +package modifiers + +func _() { + a, b := false, true //@ token("false", "variable", "readonly defaultLibrary"), token("true", "variable", "readonly defaultLibrary") +} + +const ( + c = iota //@ token("iota", "variable", "readonly defaultLibrary number") +) + +-- custom.go -- +package modifiers + +type Foo struct{} + +func _() { + var array [2]string //@ token("array", "variable", "definition array") + array = [2]string{"", ""} //@ token("array", "variable", "array") + + var b bool //@ token("b", "variable", "definition bool") + b = true //@ token("b", "variable", "bool") + + var c chan string //@ token("c", "variable", "definition chan") + c = make(chan string) //@ token("c", "variable", "chan") + + type inter interface{} //@ token("inter", "type", "definition interface") + + var m map[string]string //@ token("m", "variable", "definition map") + m = make(map[string]string) //@ token("m", "variable", "map") + + var number int //@ token("number", "variable", "definition number") + number = 1 //@ token("number", "variable", "number") + + var ptr *Foo //@ token("ptr", "variable", "definition pointer") + ptr = nil //@ token("ptr", "variable", "pointer") + + var sig func(string) //@ token("sig", "variable", "definition signature") + sig = nil //@ token("sig", "variable", "signature") + + var slice []string //@ token("slice", "variable", "definition slice") + slice = nil //@ token("slice", "variable", "slice") + + var str string //@ token("str", "variable", "definition string") + str = "" //@ token("str", "variable", "string") + + var foo Foo //@ token("foo", "variable", "definition struct") + foo = Foo{} //@ token("foo", "variable", "struct") +} + diff --git a/gopls/internal/util/moreslices/slices.go b/gopls/internal/util/moreslices/slices.go index 5905e360bfa..7658cd8b536 100644 --- a/gopls/internal/util/moreslices/slices.go +++ b/gopls/internal/util/moreslices/slices.go @@ -18,3 +18,13 @@ func Remove[T comparable](slice []T, elem T) []T { } return out } + +// ConvertStrings converts a slice of type A (with underlying type string) +// to a slice of type B (with underlying type string). +func ConvertStrings[B, A ~string](input []A) []B { + result := make([]B, len(input)) + for i, v := range input { + result[i] = B(string(v)) + } + return result +} From 7d99ad7fd29bdcf7f52c1b8fae2a3e942332d06d Mon Sep 17 00:00:00 2001 From: xzb <2598514867@qq.com> Date: Tue, 14 Jan 2025 20:00:01 +0000 Subject: [PATCH 036/309] gopls/internal/highlight: DocumentHighlight for format strings This CL introduces functionality for highlighting printf-style directives and their associated variadic arguments. Also fix some comments/names in CL 623156 Updates golang/go#70050 Change-Id: I1c4e6678317aa8cf522f41765f5a6600793f3746 GitHub-Last-Rev: d15a3b3f5fdbf1bbe58109b9fc26579d4e7c7e5f GitHub-Pull-Request: golang/tools#555 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642095 LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- gopls/doc/release/v0.18.0.md | 12 ++ gopls/internal/golang/codeaction.go | 6 +- gopls/internal/golang/fix.go | 2 +- gopls/internal/golang/highlight.go | 182 +++++++++++++++++- gopls/internal/golang/undeclared.go | 4 +- .../testdata/highlight/highlight_printf.txt | 55 ++++++ 6 files changed, 253 insertions(+), 8 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/highlight/highlight_printf.txt diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index b785b2fae9c..155570d5a2f 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -97,3 +97,15 @@ The Definition query now supports additional locations: When invoked on a return statement, hover reports the types of the function's result variables. + +## Improvements to "DocumentHighlight" + +When your cursor is inside a printf-like function, gopls now highlights the relationship between +formatting verbs and arguments as visual cues to differentiate how operands are used in the format string. + +```go +fmt.Printf("Hello %s, you scored %d", name, score) +``` + +If the cursor is either on `%s` or `name`, gopls will highlight `%s` as a write operation, +and `name` as a read operation. diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 627ba1a60d6..9a7bee7f817 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -335,9 +335,9 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error { req.addApplyFixAction(msg, fixMissingCalledFunction, req.loc) } - // "undeclared name: x" or "undefined: x" compiler error. - // Offer a "Create variable/function x" code action. - // See [fixUndeclared] for command implementation. + // "undeclared name: X" or "undefined: X" compiler error. + // Offer a "Create variable/function X" code action. + // See [createUndeclared] for command implementation. case strings.HasPrefix(msg, "undeclared name: "), strings.HasPrefix(msg, "undefined: "): path, _ := astutil.PathEnclosingInterval(req.pgf.File, start, end) diff --git a/gopls/internal/golang/fix.go b/gopls/internal/golang/fix.go index 7e83c1d6700..e812c677541 100644 --- a/gopls/internal/golang/fix.go +++ b/gopls/internal/golang/fix.go @@ -112,7 +112,7 @@ func ApplyFix(ctx context.Context, fix string, snapshot *cache.Snapshot, fh file fixInvertIfCondition: singleFile(invertIfCondition), fixSplitLines: singleFile(splitLines), fixJoinLines: singleFile(joinLines), - fixCreateUndeclared: singleFile(CreateUndeclared), + fixCreateUndeclared: singleFile(createUndeclared), fixMissingInterfaceMethods: stubMissingInterfaceMethodsFixer, fixMissingCalledFunction: stubMissingCalledFunctionFixer, } diff --git a/gopls/internal/golang/highlight.go b/gopls/internal/golang/highlight.go index 1174ce7f7d4..096cd7b77da 100644 --- a/gopls/internal/golang/highlight.go +++ b/gopls/internal/golang/highlight.go @@ -10,12 +10,16 @@ import ( "go/ast" "go/token" "go/types" + "strconv" + "strings" + "unicode/utf8" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/internal/event" + "golang.org/x/tools/internal/fmtstr" ) func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) ([]protocol.DocumentHighlight, error) { @@ -49,7 +53,7 @@ func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, po } } } - result, err := highlightPath(path, pgf.File, pkg.TypesInfo()) + result, err := highlightPath(pkg.TypesInfo(), path, pos) if err != nil { return nil, err } @@ -69,8 +73,22 @@ func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, po // highlightPath returns ranges to highlight for the given enclosing path, // which should be the result of astutil.PathEnclosingInterval. -func highlightPath(path []ast.Node, file *ast.File, info *types.Info) (map[posRange]protocol.DocumentHighlightKind, error) { +func highlightPath(info *types.Info, path []ast.Node, pos token.Pos) (map[posRange]protocol.DocumentHighlightKind, error) { result := make(map[posRange]protocol.DocumentHighlightKind) + + // Inside a call to a printf-like function (as identified + // by a simple heuristic). + // Treat each corresponding ("%v", arg) pair as a highlight class. + for _, node := range path { + if call, ok := node.(*ast.CallExpr); ok { + lit, idx := formatStringAndIndex(info, call) + if idx != -1 { + highlightPrintf(call, idx, pos, lit, result) + } + } + } + + file := path[len(path)-1].(*ast.File) switch node := path[0].(type) { case *ast.BasicLit: // Import path string literal? @@ -131,6 +149,166 @@ func highlightPath(path []ast.Node, file *ast.File, info *types.Info) (map[posRa return result, nil } +// formatStringAndIndex returns the BasicLit and index of the BasicLit (the last +// non-variadic parameter) within the given printf-like call +// expression, returns -1 as index if unknown. +func formatStringAndIndex(info *types.Info, call *ast.CallExpr) (*ast.BasicLit, int) { + typ := info.Types[call.Fun].Type + if typ == nil { + return nil, -1 // missing type + } + sig, ok := typ.(*types.Signature) + if !ok { + return nil, -1 // ill-typed + } + if !sig.Variadic() { + // Skip checking non-variadic functions. + return nil, -1 + } + idx := sig.Params().Len() - 2 + if idx < 0 { + // Skip checking variadic functions without + // fixed arguments. + return nil, -1 + } + // We only care about literal format strings, so fmt.Sprint("a"+"b%s", "bar") won't be highlighted. + if lit, ok := call.Args[idx].(*ast.BasicLit); ok && lit.Kind == token.STRING { + return lit, idx + } + return nil, -1 +} + +// highlightPrintf highlights operations in a format string and their corresponding +// variadic arguments in a (possible) printf-style function call. +// For example: +// +// fmt.Printf("Hello %s, you scored %d", name, score) +// +// If the cursor is on %s or name, it will highlight %s as a write operation, +// and name as a read operation. +func highlightPrintf(call *ast.CallExpr, idx int, cursorPos token.Pos, lit *ast.BasicLit, result map[posRange]protocol.DocumentHighlightKind) { + format, err := strconv.Unquote(lit.Value) + if err != nil { + return + } + if !strings.Contains(format, "%") { + return + } + operations, err := fmtstr.Parse(format, idx) + if err != nil { + return + } + + // fmt.Printf("%[1]d %[1].2d", 3) + // + // When cursor is in `%[1]d`, we record `3` being successfully highlighted. + // And because we will also record `%[1].2d`'s corresponding arguments index is `3` + // in `visited`, even though it will not highlight any item in the first pass, + // in the second pass we can correctly highlight it. So the three are the same class. + succeededArg := 0 + visited := make(map[posRange]int, 0) + + // highlightPair highlights the operation and its potential argument pair if the cursor is within either range. + highlightPair := func(rang fmtstr.Range, argIndex int) { + rangeStart, err := posInStringLiteral(lit, rang.Start) + if err != nil { + return + } + rangeEnd, err := posInStringLiteral(lit, rang.End) + if err != nil { + return + } + visited[posRange{rangeStart, rangeEnd}] = argIndex + + var arg ast.Expr + if argIndex < len(call.Args) { + arg = call.Args[argIndex] + } + + // cursorPos can't equal to end position, otherwise the two + // neighborhood such as (%[2]*d) are both highlighted if cursor in "*" (ending of [2]*). + if rangeStart <= cursorPos && cursorPos < rangeEnd || + arg != nil && arg.Pos() <= cursorPos && cursorPos < arg.End() { + highlightRange(result, rangeStart, rangeEnd, protocol.Write) + if arg != nil { + succeededArg = argIndex + highlightRange(result, arg.Pos(), arg.End(), protocol.Read) + } + } + } + + for _, op := range operations { + // If width or prec has any *, we can not highlight the full range from % to verb, + // because it will overlap with the sub-range of *, for example: + // + // fmt.Printf("%*[3]d", 4, 5, 6) + // ^ ^ we can only highlight this range when cursor in 6. '*' as a one-rune range will + // highlight for 4. + hasAsterisk := false + + // Try highlight Width if there is a *. + if op.Width.Dynamic != -1 { + hasAsterisk = true + highlightPair(op.Width.Range, op.Width.Dynamic) + } + + // Try highlight Precision if there is a *. + if op.Prec.Dynamic != -1 { + hasAsterisk = true + highlightPair(op.Prec.Range, op.Prec.Dynamic) + } + + // Try highlight Verb. + if op.Verb.Verb != '%' { + // If any * is found inside operation, narrow the highlight range. + if hasAsterisk { + highlightPair(op.Verb.Range, op.Verb.ArgIndex) + } else { + highlightPair(op.Range, op.Verb.ArgIndex) + } + } + } + + // Second pass, try to highlight those missed operations. + for rang, argIndex := range visited { + if succeededArg == argIndex { + highlightRange(result, rang.start, rang.end, protocol.Write) + } + } +} + +// posInStringLiteral returns the position within a string literal +// corresponding to the specified byte offset within the logical +// string that it denotes. +func posInStringLiteral(lit *ast.BasicLit, offset int) (token.Pos, error) { + raw := lit.Value + + value, err := strconv.Unquote(raw) + if err != nil { + return 0, err + } + if !(0 <= offset && offset <= len(value)) { + return 0, fmt.Errorf("invalid offset") + } + + // remove quotes + quote := raw[0] // '"' or '`' + raw = raw[1 : len(raw)-1] + + var ( + i = 0 // byte index within logical value + pos = lit.ValuePos + 1 // position within literal + ) + for raw != "" && i < offset { + r, _, rest, _ := strconv.UnquoteChar(raw, quote) // can't fail + sz := len(raw) - len(rest) // length of literal char in raw bytes + pos += token.Pos(sz) + raw = raw[sz:] + i += utf8.RuneLen(r) + } + return pos, nil +} + type posRange struct { start, end token.Pos } diff --git a/gopls/internal/golang/undeclared.go b/gopls/internal/golang/undeclared.go index 35a5c7a1e57..0615386e9bf 100644 --- a/gopls/internal/golang/undeclared.go +++ b/gopls/internal/golang/undeclared.go @@ -68,8 +68,8 @@ func undeclaredFixTitle(path []ast.Node, errMsg string) string { return fmt.Sprintf("Create %s %s", noun, name) } -// CreateUndeclared generates a suggested declaration for an undeclared variable or function. -func CreateUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { +// createUndeclared generates a suggested declaration for an undeclared variable or function. +func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { pos := start // don't use the end path, _ := astutil.PathEnclosingInterval(file, pos, pos) if len(path) < 2 { diff --git a/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt b/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt new file mode 100644 index 00000000000..05fc86c0ee1 --- /dev/null +++ b/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt @@ -0,0 +1,55 @@ + +This test checks functionality of the printf-like directives and operands highlight. +-- flags -- +-ignore_extra_diags +-- highlights.go -- +package highlightprintf +import ( + "fmt" +) + +func BasicPrintfHighlights() { + fmt.Printf("Hello %s, you have %d new messages!", "Alice", 5) //@hiloc(normals, "%s", write),hiloc(normalarg0, "\"Alice\"", read),highlightall(normals, normalarg0) + fmt.Printf("Hello %s, you have %d new messages!", "Alice", 5) //@hiloc(normald, "%d", write),hiloc(normalargs1, "5", read),highlightall(normald, normalargs1) +} + +func ComplexPrintfHighlights() { + fmt.Printf("Hello %#3.4s, you have %-2.3d new messages!", "Alice", 5) //@hiloc(complexs, "%#3.4s", write),hiloc(complexarg0, "\"Alice\"", read),highlightall(complexs, complexarg0) + fmt.Printf("Hello %#3.4s, you have %-2.3d new messages!", "Alice", 5) //@hiloc(complexd, "%-2.3d", write),hiloc(complexarg1, "5", read),highlightall(complexd, complexarg1) +} + +func MissingDirectives() { + fmt.Printf("Hello %s, you have 5 new messages!", "Alice", 5) //@hiloc(missings, "%s", write),hiloc(missingargs0, "\"Alice\"", read),highlightall(missings, missingargs0) +} + +func TooManyDirectives() { + fmt.Printf("Hello %s, you have %d new %s %q messages!", "Alice", 5) //@hiloc(toomanys, "%s", write),hiloc(toomanyargs0, "\"Alice\"", read),highlightall(toomanys, toomanyargs0) + fmt.Printf("Hello %s, you have %d new %s %q messages!", "Alice", 5) //@hiloc(toomanyd, "%d", write),hiloc(toomanyargs1, "5", read),highlightall(toomanyd, toomanyargs1) +} + +func VerbIsPercentage() { + fmt.Printf("%4.2% %d", 6) //@hiloc(z1, "%d", write),hiloc(z2, "6", read),highlightall(z1, z2) +} + +func SpecialChars() { + fmt.Printf("Hello \n %s, you \t \n have %d new messages!", "Alice", 5) //@hiloc(specials, "%s", write),hiloc(specialargs0, "\"Alice\"", read),highlightall(specials, specialargs0) + fmt.Printf("Hello \n %s, you \t \n have %d new messages!", "Alice", 5) //@hiloc(speciald, "%d", write),hiloc(specialargs1, "5", read),highlightall(speciald, specialargs1) +} + +func Escaped() { + fmt.Printf("Hello %% \n %s, you \t%% \n have %d new m%%essages!", "Alice", 5) //@hiloc(escapeds, "%s", write),hiloc(escapedargs0, "\"Alice\"", read),highlightall(escapeds, escapedargs0) + fmt.Printf("Hello %% \n %s, you \t%% \n have %d new m%%essages!", "Alice", 5) //@hiloc(escapedd, "%s", write),hiloc(escapedargs1, "\"Alice\"", read),highlightall(escapedd, escapedargs1) + fmt.Printf("%d \nss \x25[2]d", 234, 123) //@hiloc(zz1, "%d", write),hiloc(zz2, "234", read),highlightall(zz1,zz2) + fmt.Printf("%d \nss \x25[2]d", 234, 123) //@hiloc(zz3, "\\x25[2]d", write),hiloc(zz4, "123", read),highlightall(zz3,zz4) +} + +func Indexed() { + fmt.Printf("%[1]d", 3) //@hiloc(i1, "%[1]d", write),hiloc(i2, "3", read),highlightall(i1, i2) + fmt.Printf("%[1]*d", 3, 6) //@hiloc(i3, "[1]*", write),hiloc(i4, "3", read),hiloc(i5, "d", write),hiloc(i6, "6", read),highlightall(i3, i4),highlightall(i5, i6) + fmt.Printf("%[2]*[1]d", 3, 4) //@hiloc(i7, "[2]*", write),hiloc(i8, "4", read),hiloc(i9, "[1]d", write),hiloc(i10, "3", read),highlightall(i7, i8),highlightall(i9, i10) + fmt.Printf("%[2]*.[1]*[3]d", 4, 5, 6) //@hiloc(i11, "[2]*", write),hiloc(i12, "5", read),hiloc(i13, ".[1]*", write),hiloc(i14, "4", read),hiloc(i15, "[3]d", write),hiloc(i16, "6", read),highlightall(i11, i12),highlightall(i13, i14),highlightall(i15, i16) +} + +func MultipleIndexed() { + fmt.Printf("%[1]d %[1].2d", 3) //@hiloc(m1, "%[1]d", write),hiloc(m2, "3", read),hiloc(m3, "%[1].2d", write),highlightall(m1, m2, m3) +} From 79cde829efb5a649c7405f494828e150415f4147 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 14 Jan 2025 15:56:29 +0000 Subject: [PATCH 037/309] gopls/internal/protocol/command: remove the redundant gopls.test command As discussed in golang/go#67920, this command is no longer necessary. Remove it, since it does not follow our pattern for command arguments. Fixes golang/go#67920 Change-Id: Ia87535e333a35912247ff37f7a6dfb65f11f0745 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642616 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan --- gopls/internal/cmd/codelens.go | 4 ++-- gopls/internal/cmd/integration_test.go | 4 ++-- gopls/internal/cmd/usage/codelens.hlp | 4 ++-- gopls/internal/golang/code_lens.go | 15 ++++++++++++--- gopls/internal/golang/codeaction.go | 6 +++++- gopls/internal/protocol/command/command_gen.go | 18 ------------------ gopls/internal/protocol/command/interface.go | 14 +------------- gopls/internal/server/command.go | 9 --------- 8 files changed, 24 insertions(+), 50 deletions(-) diff --git a/gopls/internal/cmd/codelens.go b/gopls/internal/cmd/codelens.go index 75db4d04843..074733e58f5 100644 --- a/gopls/internal/cmd/codelens.go +++ b/gopls/internal/cmd/codelens.go @@ -44,8 +44,8 @@ Example: $ gopls codelens a_test.go # list code lenses in a file $ gopls codelens a_test.go:10 # list code lenses on line 10 - $ gopls codelens a_test.go gopls.test # list gopls.test commands - $ gopls codelens -exec a_test.go:10 gopls.test # run a specific test + $ gopls codelens a_test.go "run test" # list gopls.run_tests commands + $ gopls codelens -exec a_test.go:10 "run test" # run a specific test codelens-flags: `) diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index d819279d699..a74bafddf43 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -213,8 +213,8 @@ func TestFail(t *testing.T) { t.Fatal("fail") } { res := gopls(t, tree, "codelens", "./a/a_test.go") res.checkExit(true) - res.checkStdout(`a_test.go:3: "run test" \[gopls.test\]`) - res.checkStdout(`a_test.go:4: "run test" \[gopls.test\]`) + res.checkStdout(`a_test.go:3: "run test" \[gopls.run_tests\]`) + res.checkStdout(`a_test.go:4: "run test" \[gopls.run_tests\]`) } // no codelens with title/position { diff --git a/gopls/internal/cmd/usage/codelens.hlp b/gopls/internal/cmd/usage/codelens.hlp index 59afe0d3a27..f72bb465e07 100644 --- a/gopls/internal/cmd/usage/codelens.hlp +++ b/gopls/internal/cmd/usage/codelens.hlp @@ -19,8 +19,8 @@ Example: $ gopls codelens a_test.go # list code lenses in a file $ gopls codelens a_test.go:10 # list code lenses on line 10 - $ gopls codelens a_test.go gopls.test # list gopls.test commands - $ gopls codelens -exec a_test.go:10 gopls.test # run a specific test + $ gopls codelens a_test.go "run test" # list gopls.run_tests commands + $ gopls codelens -exec a_test.go:10 "run test" # run a specific test codelens-flags: -d,-diff diff --git a/gopls/internal/golang/code_lens.go b/gopls/internal/golang/code_lens.go index 1359d0d0148..b04724e0cbc 100644 --- a/gopls/internal/golang/code_lens.go +++ b/gopls/internal/golang/code_lens.go @@ -47,13 +47,19 @@ func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand } puri := fh.URI() for _, fn := range testFuncs { - cmd := command.NewTestCommand("run test", puri, []string{fn.name}, nil) + cmd := command.NewRunTestsCommand("run test", command.RunTestsArgs{ + URI: puri, + Tests: []string{fn.name}, + }) rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start} codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) } for _, fn := range benchFuncs { - cmd := command.NewTestCommand("run benchmark", puri, nil, []string{fn.name}) + cmd := command.NewRunTestsCommand("run benchmark", command.RunTestsArgs{ + URI: puri, + Benchmarks: []string{fn.name}, + }) rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start} codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) } @@ -72,7 +78,10 @@ func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand for _, fn := range benchFuncs { benches = append(benches, fn.name) } - cmd := command.NewTestCommand("run file benchmarks", puri, nil, benches) + cmd := command.NewRunTestsCommand("run file benchmarks", command.RunTestsArgs{ + URI: puri, + Benchmarks: benches, + }) codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) } return codeLens, nil diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 9a7bee7f817..0df8602fb33 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -803,7 +803,11 @@ func goTest(ctx context.Context, req *codeActionsRequest) error { return nil } - cmd := command.NewTestCommand("Run tests and benchmarks", req.loc.URI, tests, benchmarks) + cmd := command.NewRunTestsCommand("Run tests and benchmarks", command.RunTestsArgs{ + URI: req.loc.URI, + Tests: tests, + Benchmarks: benchmarks, + }) req.addCommandAction(cmd, false) return nil } diff --git a/gopls/internal/protocol/command/command_gen.go b/gopls/internal/protocol/command/command_gen.go index a5527064ef9..36938a41f14 100644 --- a/gopls/internal/protocol/command/command_gen.go +++ b/gopls/internal/protocol/command/command_gen.go @@ -58,7 +58,6 @@ const ( StartDebugging Command = "gopls.start_debugging" StartProfile Command = "gopls.start_profile" StopProfile Command = "gopls.stop_profile" - Test Command = "gopls.test" Tidy Command = "gopls.tidy" UpdateGoSum Command = "gopls.update_go_sum" UpgradeDependency Command = "gopls.upgrade_dependency" @@ -103,7 +102,6 @@ var Commands = []Command{ StartDebugging, StartProfile, StopProfile, - Test, Tidy, UpdateGoSum, UpgradeDependency, @@ -310,14 +308,6 @@ func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Inte return nil, err } return s.StopProfile(ctx, a0) - case Test: - var a0 protocol.DocumentURI - var a1 []string - var a2 []string - if err := UnmarshalArgs(params.Arguments, &a0, &a1, &a2); err != nil { - return nil, err - } - return nil, s.Test(ctx, a0, a1, a2) case Tidy: var a0 URIArgs if err := UnmarshalArgs(params.Arguments, &a0); err != nil { @@ -628,14 +618,6 @@ func NewStopProfileCommand(title string, a0 StopProfileArgs) *protocol.Command { } } -func NewTestCommand(title string, a0 protocol.DocumentURI, a1 []string, a2 []string) *protocol.Command { - return &protocol.Command{ - Title: title, - Command: Test.String(), - Arguments: MustMarshalArgs(a0, a1, a2), - } -} - func NewTidyCommand(title string, a0 URIArgs) *protocol.Command { return &protocol.Command{ Title: title, diff --git a/gopls/internal/protocol/command/interface.go b/gopls/internal/protocol/command/interface.go index b0e80a4129e..e7386f7fd8f 100644 --- a/gopls/internal/protocol/command/interface.go +++ b/gopls/internal/protocol/command/interface.go @@ -47,19 +47,7 @@ type Interface interface { // Applies a fix to a region of source code. ApplyFix(context.Context, ApplyFixArgs) (*protocol.WorkspaceEdit, error) - // Test: Run test(s) (legacy) - // - // Runs `go test` for a specific set of test or benchmark functions. - // - // This command is asynchronous; wait for the 'end' progress notification. - // - // This command is an alias for RunTests; the only difference - // is the form of the parameters. - // - // TODO(adonovan): eliminate it. - Test(context.Context, protocol.DocumentURI, []string, []string) error - - // Test: Run test(s) + // RunTests: Run tests // // Runs `go test` for a specific set of test or benchmark functions. // diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index e785625655e..1f7aa4802c7 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -683,15 +683,6 @@ func dropDependency(pm *cache.ParsedModule, modulePath string) ([]protocol.TextE return protocol.EditsFromDiffEdits(pm.Mapper, diff) } -// Test is an alias for RunTests (with splayed arguments). -func (c *commandHandler) Test(ctx context.Context, uri protocol.DocumentURI, tests, benchmarks []string) error { - return c.RunTests(ctx, command.RunTestsArgs{ - URI: uri, - Tests: tests, - Benchmarks: benchmarks, - }) -} - func (c *commandHandler) Doc(ctx context.Context, args command.DocArgs) (protocol.URI, error) { if args.Location.URI == "" { return "", errors.New("missing location URI") From 66ef73e1ec0e4ab85ce5f05b47b64c8d022f677c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sat, 4 Jan 2025 12:35:30 -0500 Subject: [PATCH 038/309] gopls/internal/golang: improve "toggle compiler opt details" This CL improves the usability of the "Toggle compiler optimization details" code action by: - making it work for _test.go files too (by running "go test -c" instead of "go build"); - changing the flag to be per directory, not per metadata.Package, as this led to confusing behaviour w.r.t. in-package test files. - presenting specific "Show"/"Hide" command titles depending on the current state, instead of the vague "Toggle". It includes a test for all the new functionality. Also, document the "transitively error free" requirement. Fixes golang/go#71106 Fixes golang/go#42812 (amusingly) Change-Id: I842d7e3f53d1c4f0e94b8741ca792c3d999d6ff3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640395 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/doc/features/diagnostics.md | 6 +- gopls/doc/release/v0.18.0.md | 4 +- gopls/internal/cache/snapshot.go | 24 ++--- gopls/internal/cache/view.go | 3 +- gopls/internal/cmd/integration_test.go | 18 ++-- gopls/internal/golang/codeaction.go | 23 ++++- gopls/internal/golang/compileropt.go | 32 +++---- gopls/internal/server/command.go | 15 +-- gopls/internal/server/diagnostics.go | 33 +++---- .../test/integration/misc/compileropt_test.go | 93 ++++++++++++++++++- 10 files changed, 178 insertions(+), 73 deletions(-) diff --git a/gopls/doc/features/diagnostics.md b/gopls/doc/features/diagnostics.md index 09b3cc33e90..ceec607c123 100644 --- a/gopls/doc/features/diagnostics.md +++ b/gopls/doc/features/diagnostics.md @@ -65,9 +65,13 @@ There is an optional third source of diagnostics: This source is disabled by default but can be enabled on a package-by-package basis by invoking the - `source.toggleCompilerOptDetails` ("Toggle compiler optimization + `source.toggleCompilerOptDetails` ("{Show,Hide} compiler optimization details") code action. + Remember that the compiler's optimizer runs only on packages that + are transitively free from errors, so optimization diagnostics + will not be shown on packages that do not build. + ## Recomputation of diagnostics diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 155570d5a2f..e2b730052bc 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -12,10 +12,10 @@ # New features -## "Toggle compiler optimization details" code action +## "{Show,Hide} compiler optimization details" code action This code action, accessible through the "Source Action" menu in VS -Code, toggles a per-package flag that causes Go compiler optimization +Code, toggles a per-directory flag that causes Go compiler optimization details to be reported as diagnostics. For example, it indicates which variables escape to the heap, and which array accesses require bounds checks. diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index ffca1dc00ec..c17dade773e 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -183,9 +183,9 @@ type Snapshot struct { // vulns maps each go.mod file's URI to its known vulnerabilities. vulns *persistent.Map[protocol.DocumentURI, *vulncheck.Result] - // compilerOptDetails describes the packages for which we want - // compiler optimization details to be included in the diagnostics. - compilerOptDetails map[metadata.PackageID]unit + // compilerOptDetails is the set of directories whose packages + // and tests need compiler optimization details in the diagnostics. + compilerOptDetails map[protocol.DocumentURI]unit // Concurrent type checking: // typeCheckMu guards the ongoing type checking batch, and reference count of @@ -1523,15 +1523,15 @@ func (s *Snapshot) clone(ctx, bgCtx context.Context, changed StateChange, done f // Compute the new set of packages for which we want compiler // optimization details, after applying changed.CompilerOptDetails. if len(s.compilerOptDetails) > 0 || len(changed.CompilerOptDetails) > 0 { - newCompilerOptDetails := make(map[metadata.PackageID]unit) - for id := range s.compilerOptDetails { - if _, ok := changed.CompilerOptDetails[id]; !ok { - newCompilerOptDetails[id] = unit{} // no change + newCompilerOptDetails := make(map[protocol.DocumentURI]unit) + for dir := range s.compilerOptDetails { + if _, ok := changed.CompilerOptDetails[dir]; !ok { + newCompilerOptDetails[dir] = unit{} // no change } } - for id, want := range changed.CompilerOptDetails { + for dir, want := range changed.CompilerOptDetails { if want { - newCompilerOptDetails[id] = unit{} + newCompilerOptDetails[dir] = unit{} } } if len(newCompilerOptDetails) > 0 { @@ -2160,9 +2160,9 @@ func (s *Snapshot) setBuiltin(path string) { } // WantCompilerOptDetails reports whether to compute compiler -// optimization details for the specified package. -func (s *Snapshot) WantCompilerOptDetails(id metadata.PackageID) bool { - _, ok := s.compilerOptDetails[id] +// optimization details for packages and tests in the given directory. +func (s *Snapshot) WantCompilerOptDetails(dir protocol.DocumentURI) bool { + _, ok := s.compilerOptDetails[dir] return ok } diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index a6cdae4d2e8..0169b8394b7 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -27,7 +27,6 @@ import ( "sync" "time" - "golang.org/x/tools/gopls/internal/cache/metadata" "golang.org/x/tools/gopls/internal/cache/typerefs" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" @@ -745,7 +744,7 @@ type StateChange struct { Files map[protocol.DocumentURI]file.Handle ModuleUpgrades map[protocol.DocumentURI]map[string]string Vulns map[protocol.DocumentURI]*vulncheck.Result - CompilerOptDetails map[metadata.PackageID]bool // package -> whether or not we want details + CompilerOptDetails map[protocol.DocumentURI]bool // package directory -> whether or not we want details } // InvalidateView processes the provided state change, invalidating any derived diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index a74bafddf43..42812a870a4 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -950,12 +950,12 @@ func TestCodeAction(t *testing.T) { module example.com go 1.18 --- a.go -- +-- a/a.go -- package a type T int func f() (int, string) { return } --- b.go -- +-- a/b.go -- package a import "io" var _ io.Reader = C{} @@ -970,14 +970,14 @@ type C struct{} } // list code actions in file { - res := gopls(t, tree, "codeaction", "a.go") + res := gopls(t, tree, "codeaction", "a/a.go") res.checkExit(true) res.checkStdout(`edit "Fill in return values" \[quickfix\]`) res.checkStdout(`command "Browse documentation for package a" \[source.doc\]`) } // list code actions in file, filtering by title { - res := gopls(t, tree, "codeaction", "-title=Browse.*doc", "a.go") + res := gopls(t, tree, "codeaction", "-title=Browse.*doc", "a/a.go") res.checkExit(true) got := res.stdout want := `command "Browse gopls feature documentation" [gopls.doc.features]` + @@ -990,12 +990,12 @@ type C struct{} } // list code actions in file, filtering (hierarchically) by kind { - res := gopls(t, tree, "codeaction", "-kind=source", "a.go") + res := gopls(t, tree, "codeaction", "-kind=source", "a/a.go") res.checkExit(true) got := res.stdout want := `command "Browse documentation for package a" [source.doc]` + "\n" + - `command "Toggle compiler optimization details" [source.toggleCompilerOptDetails]` + + `command "Show compiler optimization details for \"a\"" [source.toggleCompilerOptDetails]` + "\n" if got != want { t.Errorf("codeaction: got <<%s>>, want <<%s>>\nstderr:\n%s", got, want, res.stderr) @@ -1003,13 +1003,13 @@ type C struct{} } // list code actions at position (of io.Reader) { - res := gopls(t, tree, "codeaction", "b.go:#31") + res := gopls(t, tree, "codeaction", "a/b.go:#31") res.checkExit(true) res.checkStdout(`command "Browse documentation for type io.Reader" \[source.doc]`) } // list quick fixes at position (of type T) { - res := gopls(t, tree, "codeaction", "-kind=quickfix", "a.go:#15") + res := gopls(t, tree, "codeaction", "-kind=quickfix", "a/a.go:#15") res.checkExit(true) got := res.stdout want := `edit "Fill in return values" [quickfix]` + "\n" @@ -1019,7 +1019,7 @@ type C struct{} } // success, with explicit CodeAction kind and diagnostics span. { - res := gopls(t, tree, "codeaction", "-kind=quickfix", "-exec", "b.go:#40") + res := gopls(t, tree, "codeaction", "-kind=quickfix", "-exec", "a/b.go:#40") res.checkExit(true) got := res.stdout want := ` diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 0df8602fb33..17f7236f815 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -11,6 +11,7 @@ import ( "go/ast" "go/token" "go/types" + "path/filepath" "reflect" "slices" "sort" @@ -105,6 +106,8 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, req.pkg = nil } if err := p.fn(ctx, req); err != nil { + // TODO(adonovan): most errors in code action providers should + // not block other providers; see https://go.dev/issue/71275. return nil, err } } @@ -879,10 +882,22 @@ func goAssembly(ctx context.Context, req *codeActionsRequest) error { return nil } -// toggleCompilerOptDetails produces "Toggle compiler optimization details" code action. -// See [server.commandHandler.ToggleCompilerOptDetails] for command implementation. +// toggleCompilerOptDetails produces "{Show,Hide} compiler optimization details" code action. +// See [server.commandHandler.GCDetails] for command implementation. func toggleCompilerOptDetails(ctx context.Context, req *codeActionsRequest) error { - cmd := command.NewGCDetailsCommand("Toggle compiler optimization details", req.fh.URI()) - req.addCommandAction(cmd, false) + // TODO(adonovan): errors from code action providers should probably be + // logged, even if they aren't visible to the client; see https://go.dev/issue/71275. + if meta, err := NarrowestMetadataForFile(ctx, req.snapshot, req.fh.URI()); err == nil { + if len(meta.CompiledGoFiles) == 0 { + return fmt.Errorf("package %q does not compile file %q", meta.ID, req.fh.URI()) + } + dir := meta.CompiledGoFiles[0].Dir() + + title := fmt.Sprintf("%s compiler optimization details for %q", + cond(req.snapshot.WantCompilerOptDetails(dir), "Hide", "Show"), + filepath.Base(dir.Path())) + cmd := command.NewGCDetailsCommand(title, req.fh.URI()) + req.addCommandAction(cmd, false) + } return nil } diff --git a/gopls/internal/golang/compileropt.go b/gopls/internal/golang/compileropt.go index 2a39a5b5ee1..f9f046463f6 100644 --- a/gopls/internal/golang/compileropt.go +++ b/gopls/internal/golang/compileropt.go @@ -11,22 +11,19 @@ import ( "fmt" "os" "path/filepath" + "runtime" "strings" "golang.org/x/tools/gopls/internal/cache" - "golang.org/x/tools/gopls/internal/cache/metadata" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/internal/event" ) // CompilerOptDetails invokes the Go compiler with the "-json=0,dir" -// flag on the specified package, parses its log of optimization -// decisions, and returns them as a set of diagnostics. -func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, mp *metadata.Package) (map[protocol.DocumentURI][]*cache.Diagnostic, error) { - if len(mp.CompiledGoFiles) == 0 { - return nil, nil - } - pkgDir := mp.CompiledGoFiles[0].DirPath() +// flag on the packages and tests in the specified directory, parses +// its log of optimization decisions, and returns them as a set of +// diagnostics. +func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, pkgDir protocol.DocumentURI) (map[protocol.DocumentURI][]*cache.Diagnostic, error) { outDir, err := os.MkdirTemp("", fmt.Sprintf("gopls-%d.details", os.Getpid())) if err != nil { return nil, err @@ -37,22 +34,20 @@ func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, mp *metad } }() - tmpFile, err := os.CreateTemp(os.TempDir(), "gopls-x") - if err != nil { - return nil, err - } - tmpFile.Close() // ignore error - defer os.Remove(tmpFile.Name()) - outDirURI := protocol.URIFromPath(outDir) // details doesn't handle Windows URIs in the form of "file:///C:/...", // so rewrite them to "file://C:/...". See golang/go#41614. if !strings.HasPrefix(outDir, "/") { outDirURI = protocol.DocumentURI(strings.Replace(string(outDirURI), "file:///", "file://", 1)) } - inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkgDir, "build", []string{ + + // We use "go test -c" not "go build" as it covers all three packages + // (p, "p [p.test]", "p_test [p.test]") in the directory, if they exist. + inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkgDir.Path(), "test", []string{ + "-c", + "-vet=off", // weirdly -c doesn't disable vet fmt.Sprintf("-gcflags=-json=0,%s", outDirURI), // JSON schema version 0 - fmt.Sprintf("-o=%s", tmpFile.Name()), + fmt.Sprintf("-o=%s", cond(runtime.GOOS == "windows", "NUL", "/dev/null")), ".", }) if err != nil { @@ -79,7 +74,8 @@ func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, mp *metad if fh == nil { continue } - if pkgDir != fh.URI().DirPath() { + if pkgDir != fh.URI().Dir() { + // Filter compiler diagnostics to the requested directory. // https://github.com/golang/go/issues/42198 // sometimes the detail diagnostics generated for files // outside the package can never be taken back. diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 1f7aa4802c7..2d936f2bc41 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -1017,19 +1017,22 @@ func (s *server) getUpgrades(ctx context.Context, snapshot *cache.Snapshot, uri func (c *commandHandler) GCDetails(ctx context.Context, uri protocol.DocumentURI) error { return c.run(ctx, commandConfig{ - progress: "Toggling display of compiler optimization details", - forURI: uri, + forURI: uri, }, func(ctx context.Context, deps commandDeps) error { return c.modifyState(ctx, FromToggleCompilerOptDetails, func() (*cache.Snapshot, func(), error) { + // Don't blindly use "dir := deps.fh.URI().Dir()"; validate. meta, err := golang.NarrowestMetadataForFile(ctx, deps.snapshot, deps.fh.URI()) if err != nil { return nil, nil, err } - want := !deps.snapshot.WantCompilerOptDetails(meta.ID) // toggle per-package flag + if len(meta.CompiledGoFiles) == 0 { + return nil, nil, fmt.Errorf("package %q does not compile file %q", meta.ID, deps.fh.URI()) + } + dir := meta.CompiledGoFiles[0].Dir() + + want := !deps.snapshot.WantCompilerOptDetails(dir) // toggle per-directory flag return c.s.session.InvalidateView(ctx, deps.snapshot.View(), cache.StateChange{ - CompilerOptDetails: map[metadata.PackageID]bool{ - meta.ID: want, - }, + CompilerOptDetails: map[protocol.DocumentURI]bool{dir: want}, }) }) }) diff --git a/gopls/internal/server/diagnostics.go b/gopls/internal/server/diagnostics.go index 541ba22350c..b4e764b1233 100644 --- a/gopls/internal/server/diagnostics.go +++ b/gopls/internal/server/diagnostics.go @@ -561,25 +561,26 @@ func (s *server) compilerOptDetailsDiagnostics(ctx context.Context, snapshot *ca // TODO(rfindley): This should memoize its results if the package has not changed. // Consider that these points, in combination with the note below about // races, suggest that compiler optimization details should be tracked on the Snapshot. - var detailPkgs map[metadata.PackageID]*metadata.Package - for _, mp := range toDiagnose { - if snapshot.WantCompilerOptDetails(mp.ID) { - if detailPkgs == nil { - detailPkgs = make(map[metadata.PackageID]*metadata.Package) - } - detailPkgs[mp.ID] = mp - } - } - diagnostics := make(diagMap) - for _, mp := range detailPkgs { - perFileDiags, err := golang.CompilerOptDetails(ctx, snapshot, mp) - if err != nil { - event.Error(ctx, "warning: compiler optimization details", err, append(snapshot.Labels(), label.Package.Of(string(mp.ID)))...) + seenDirs := make(map[protocol.DocumentURI]bool) + for _, mp := range toDiagnose { + if len(mp.CompiledGoFiles) == 0 { continue } - for uri, diags := range perFileDiags { - diagnostics[uri] = append(diagnostics[uri], diags...) + dir := mp.CompiledGoFiles[0].Dir() + if snapshot.WantCompilerOptDetails(dir) { + if !seenDirs[dir] { + seenDirs[dir] = true + + perFileDiags, err := golang.CompilerOptDetails(ctx, snapshot, dir) + if err != nil { + event.Error(ctx, "warning: compiler optimization details", err, append(snapshot.Labels(), label.URI.Of(dir))...) + continue + } + for uri, diags := range perFileDiags { + diagnostics[uri] = append(diagnostics[uri], diags...) + } + } } } return diagnostics, nil diff --git a/gopls/internal/test/integration/misc/compileropt_test.go b/gopls/internal/test/integration/misc/compileropt_test.go index 8b8f78cd62d..175ec640042 100644 --- a/gopls/internal/test/integration/misc/compileropt_test.go +++ b/gopls/internal/test/integration/misc/compileropt_test.go @@ -14,7 +14,7 @@ import ( . "golang.org/x/tools/gopls/internal/test/integration" ) -// TestCompilerOptDetails exercises the "Toggle compiler optimization details" code action. +// TestCompilerOptDetails exercises the "{Show,Hide} compiler optimization details" code action. func TestCompilerOptDetails(t *testing.T) { if runtime.GOOS == "android" { t.Skipf("the compiler optimization details code action doesn't work on Android") @@ -24,7 +24,8 @@ func TestCompilerOptDetails(t *testing.T) { -- go.mod -- module mod.com -go 1.15 +go 1.18 + -- main.go -- package main @@ -38,7 +39,7 @@ func main() { env.OpenFile("main.go") actions := env.CodeActionForFile("main.go", nil) - // Execute the "Toggle compiler optimization details" command. + // Execute the "Show compiler optimization details" command. docAction, err := codeActionByKind(actions, settings.GoToggleCompilerOptDetails) if err != nil { t.Fatal(err) @@ -79,3 +80,89 @@ func f(x int) *int { return &x }`) ) }) } + +// TestCompilerOptDetails_perDirectory exercises that the "want +// optimization details" flag has per-directory cardinality. +func TestCompilerOptDetails_perDirectory(t *testing.T) { + if runtime.GOOS == "android" { + t.Skipf("the compiler optimization details code action doesn't work on Android") + } + + const mod = ` +-- go.mod -- +module mod.com +go 1.18 + +-- a/a.go -- +package a + +func F(x int) any { return &x } + +-- a/a_test.go -- +package a + +func G(x int) any { return &x } + +-- a/a_x_test.go -- +package a_test + +func H(x int) any { return &x } +` + + Run(t, mod, func(t *testing.T, env *Env) { + // toggle executes the "Toggle compiler optimization details" + // command within a file, and asserts that it has the specified title. + toggle := func(filename, wantTitle string) { + env.OpenFile(filename) + actions := env.CodeActionForFile(filename, nil) + + docAction, err := codeActionByKind(actions, settings.GoToggleCompilerOptDetails) + if err != nil { + t.Fatal(err) + } + if docAction.Title != wantTitle { + t.Errorf("CodeAction.Title = %q, want %q", docAction.Title, wantTitle) + } + params := &protocol.ExecuteCommandParams{ + Command: docAction.Command.Command, + Arguments: docAction.Command.Arguments, + } + env.ExecuteCommand(params, nil) + } + + // Show diagnostics for directory a/ from one file. + // Diagnostics are reported for all three packages. + toggle("a/a.go", `Show compiler optimization details for "a"`) + env.OnceMet( + CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 1, true), + Diagnostics( + ForFile("a/a.go"), + AtPosition("a/a.go", 2, 7), + WithMessage("x escapes to heap"), + WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), + ), + Diagnostics( + ForFile("a/a_test.go"), + AtPosition("a/a_test.go", 2, 7), + WithMessage("x escapes to heap"), + WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), + ), + Diagnostics( + ForFile("a/a_x_test.go"), + AtPosition("a/a_x_test.go", 2, 7), + WithMessage("x escapes to heap"), + WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), + ), + ) + + // Hide diagnostics for the directory from a different file. + // All diagnostics disappear. + toggle("a/a_test.go", `Hide compiler optimization details for "a"`) + env.OnceMet( + CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 2, true), + NoDiagnostics(ForFile("a/a.go")), + NoDiagnostics(ForFile("a/a_test.go")), + NoDiagnostics(ForFile("a/a_x_test.go")), + ) + }) +} From 9d9b0b6e546c97608d8d52f87995980144ffb66c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 14 Jan 2025 13:18:08 -0800 Subject: [PATCH 039/309] go/packages: use go.dev/issue links in comment Change-Id: I248213b1c112017b827c75fb23df39ce249ee180 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642755 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Ian Lance Taylor Commit-Queue: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- go/packages/packages.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/packages/packages.go b/go/packages/packages.go index 0147d9080aa..c3a59b8ebf4 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -59,10 +59,10 @@ import ( // // Unfortunately there are a number of open bugs related to // interactions among the LoadMode bits: -// - https://github.com/golang/go/issues/56633 -// - https://github.com/golang/go/issues/56677 -// - https://github.com/golang/go/issues/58726 -// - https://github.com/golang/go/issues/63517 +// - https://go.dev/issue/56633 +// - https://go.dev/issue/56677 +// - https://go.dev/issue/58726 +// - https://go.dev/issue/63517 type LoadMode int const ( From 3f87563a23a2cd14d853519141bd10a0a4718a2d Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sun, 22 Dec 2024 18:10:45 +0000 Subject: [PATCH 040/309] go/cfg: remove empty goto (without label) from test case See CL 638395 Change-Id: Idf5495b0d8c70484f31d87c81ed00ef862a3a3f3 GitHub-Last-Rev: 7513ba91f2b4f5499d4d0bbb535c797acb36189e GitHub-Pull-Request: golang/tools#550 Reviewed-on: https://go-review.googlesource.com/c/tools/+/638435 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- go/cfg/cfg_test.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/go/cfg/cfg_test.go b/go/cfg/cfg_test.go index 536d2fe5df7..d5f04ed5731 100644 --- a/go/cfg/cfg_test.go +++ b/go/cfg/cfg_test.go @@ -127,12 +127,6 @@ func f10(ch chan int) { } live() } - -func f11() { - goto; // mustn't crash - dead() -} - ` func TestDeadCode(t *testing.T) { From 4828981da72e737139cfc9a25b635c36e9341505 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Wed, 15 Jan 2025 10:30:58 -0500 Subject: [PATCH 041/309] gopls/internal/telemetry/cmd/stacks: build compiler from root Currently we build cmd/compile from the current directory. Unfortunately, if the current directory happens to contain a go.mod file, the `go` directive restricts the GOTOOLCHAINs that are allowed. For example, gopls go.mod says `go 1.23.4`. Running stacks from its directory is thus unable to build a compiler older than 1.23.4. To avoid this, switch to root when building the compiler, assuming that root won't contain a go.mod. We can now drop GOWORK=off, as root presumably won't contain a go.work either. Change-Id: I6a6a636ccb6fd2b04db3352e17dc28310fc8d069 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642935 LUCI-TryBot-Result: Go LUCI Auto-Submit: Michael Pratt Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index f7b289fc070..75e67b7bd84 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -989,6 +989,11 @@ func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { buildDir = filepath.Join(revDir, "gopls") case "cmd/compile": // Nothing to do, GOTOOLCHAIN is sufficient. + + // Switch build directories so if we happen to be in Go module + // directory its go.mod doesn't restrict the toolchain versions + // we're allowed to use. + buildDir = "/" default: return nil, fmt.Errorf("don't know how to build unknown program %s", info.Program) } @@ -1014,7 +1019,6 @@ func readPCLineTable(info Info, stacksDir string) (map[string]FileLine, error) { "GOEXPERIMENT=", // Don't forward GOEXPERIMENT from current environment since the GOTOOLCHAIN selected might not support the same experiments. "GOOS="+info.GOOS, "GOARCH="+info.GOARCH, - "GOWORK=off", ) if err := cmd.Run(); err != nil { return nil, fmt.Errorf("building: %v (rm -fr %s?)", err, stacksDir) From 344e48255740736de8c8277e9a286cf3231c7e13 Mon Sep 17 00:00:00 2001 From: xzb <2598514867@qq.com> Date: Wed, 15 Jan 2025 18:06:04 +0000 Subject: [PATCH 042/309] golang/internal/highlight: check idx < len before indexing Change-Id: I2d36c2b64e3af00861ce9913f733247877b2239e GitHub-Last-Rev: f6b43124679882aed19431acbdae6c3a5ea9e2f6 GitHub-Pull-Request: golang/tools#556 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642756 Reviewed-by: Robert Findley Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- gopls/internal/golang/highlight.go | 6 +++--- .../test/marker/testdata/highlight/highlight_printf.txt | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gopls/internal/golang/highlight.go b/gopls/internal/golang/highlight.go index 096cd7b77da..252485306b5 100644 --- a/gopls/internal/golang/highlight.go +++ b/gopls/internal/golang/highlight.go @@ -166,9 +166,9 @@ func formatStringAndIndex(info *types.Info, call *ast.CallExpr) (*ast.BasicLit, return nil, -1 } idx := sig.Params().Len() - 2 - if idx < 0 { - // Skip checking variadic functions without - // fixed arguments. + if !(0 <= idx && idx < len(call.Args)) { + // Skip checking functions without a format string parameter, or + // missing the corresponding format argument. return nil, -1 } // We only care about literal format strings, so fmt.Sprint("a"+"b%s", "bar") won't be highlighted. diff --git a/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt b/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt index 05fc86c0ee1..5c9bc21f016 100644 --- a/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt +++ b/gopls/internal/test/marker/testdata/highlight/highlight_printf.txt @@ -53,3 +53,10 @@ func Indexed() { func MultipleIndexed() { fmt.Printf("%[1]d %[1].2d", 3) //@hiloc(m1, "%[1]d", write),hiloc(m2, "3", read),hiloc(m3, "%[1].2d", write),highlightall(m1, m2, m3) } + +// This test checks that gopls doesn't crash (index out of bounds) +// while haven't fill the last non-variadic argument. +func NoEffectOnUnfinishedArg() { + var s string //@hiloc(var, "s", write) + fmt.Fprintf(s) //@hiloc(firstArg, "s", read),highlightall(var, firstArg) +} From 85e8b42f03f45b3a480c179ccf5cb0065a806cde Mon Sep 17 00:00:00 2001 From: Madeline Kalil Date: Fri, 3 Jan 2025 17:26:57 -0500 Subject: [PATCH 043/309] gopls/internal/analysis/modernize: omitzero Adds a new modernizer that suggests removing or replacing instances of "omitempty" on struct fields with "omitzero." Example (before): type Foo struct { A struct{ b int } `json:"name,omitempty" } Example (after - replace): type Foo struct { A struct{ b int } `json:"name,omitzero" } Example (after - remove): type Foo struct { A struct{ b int } `json:"name" } Updates golang/go#70815 Change-Id: I7d651880340d24929ea5cae4751557a1f60e5f8e Reviewed-on: https://go-review.googlesource.com/c/tools/+/640041 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/analyzers.md | 1 + gopls/internal/analysis/modernize/doc.go | 1 + .../internal/analysis/modernize/modernize.go | 19 +-- .../analysis/modernize/modernize_test.go | 1 + gopls/internal/analysis/modernize/omitzero.go | 108 ++++++++++++++++++ .../testdata/src/omitzero/omitzero.go | 30 +++++ .../testdata/src/omitzero/omitzero.go.golden | 63 ++++++++++ gopls/internal/doc/api.json | 4 +- gopls/internal/golang/highlight.go | 40 +------ internal/astutil/util.go | 45 ++++++++ 10 files changed, 266 insertions(+), 46 deletions(-) create mode 100644 gopls/internal/analysis/modernize/omitzero.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go.golden create mode 100644 internal/astutil/util.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index c7f03b55019..26830628f74 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -482,6 +482,7 @@ existing code by using more modern features of Go, such as: added in go1.19; - replacing uses of context.WithCancel in tests with t.Context, added in go1.24; + - replacing omitempty by omitzero on structs, added in go 1.24 Default: on. diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 35514357d0f..139f1e45520 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -25,4 +25,5 @@ // added in go1.19; // - replacing uses of context.WithCancel in tests with t.Context, added in // go1.24; +// - replacing omitempty by omitzero on structs, added in go 1.24 package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 6cedc5eec73..55f15e655ad 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -11,6 +11,7 @@ import ( "go/token" "go/types" "iter" + "regexp" "strings" "golang.org/x/tools/go/analysis" @@ -65,6 +66,7 @@ func run(pass *analysis.Pass) (any, error) { fmtappendf(pass) mapsloop(pass) minmax(pass) + omitzero(pass) slicescontains(pass) sortslice(pass) testingContext(pass) @@ -121,12 +123,13 @@ func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) } var ( - builtinAny = types.Universe.Lookup("any") - builtinAppend = types.Universe.Lookup("append") - builtinBool = types.Universe.Lookup("bool") - builtinFalse = types.Universe.Lookup("false") - builtinMake = types.Universe.Lookup("make") - builtinNil = types.Universe.Lookup("nil") - builtinTrue = types.Universe.Lookup("true") - byteSliceType = types.NewSlice(types.Typ[types.Byte]) + builtinAny = types.Universe.Lookup("any") + builtinAppend = types.Universe.Lookup("append") + builtinBool = types.Universe.Lookup("bool") + builtinFalse = types.Universe.Lookup("false") + builtinMake = types.Universe.Lookup("make") + builtinNil = types.Universe.Lookup("nil") + builtinTrue = types.Universe.Lookup("true") + byteSliceType = types.NewSlice(types.Typ[types.Byte]) + omitemptyRegex = regexp.MustCompile(`(?:^json| json):"[^"]*(,omitempty)(?:"|,[^"]*")\s?`) ) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index d8d2d9a3d52..b95895eb55d 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -19,6 +19,7 @@ func Test(t *testing.T) { "fmtappendf", "mapsloop", "minmax", + "omitzero", "slicescontains", "sortslice", "testingcontext", diff --git a/gopls/internal/analysis/modernize/omitzero.go b/gopls/internal/analysis/modernize/omitzero.go new file mode 100644 index 00000000000..706cb4ea5ef --- /dev/null +++ b/gopls/internal/analysis/modernize/omitzero.go @@ -0,0 +1,108 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "go/ast" + "go/types" + "reflect" + "strconv" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/astutil" +) + +func checkOmitEmptyField(pass *analysis.Pass, info *types.Info, curField *ast.Field) { + typ := info.TypeOf(curField.Type) + _, ok := typ.Underlying().(*types.Struct) + if !ok { + // Not a struct + return + } + tag := curField.Tag + if tag == nil { + // No tag to check + return + } + // The omitempty tag may be used by other packages besides json, but we should only modify its use with json + tagconv, _ := strconv.Unquote(tag.Value) + match := omitemptyRegex.FindStringSubmatchIndex(tagconv) + if match == nil { + // No omitempty in json tag + return + } + omitEmptyPos, err := astutil.PosInStringLiteral(curField.Tag, match[2]) + if err != nil { + return + } + omitEmptyEnd, err := astutil.PosInStringLiteral(curField.Tag, match[3]) + if err != nil { + return + } + removePos, removeEnd := omitEmptyPos, omitEmptyEnd + + jsonTag := reflect.StructTag(tagconv).Get("json") + if jsonTag == ",omitempty" { + // Remove the entire struct tag if json is the only package used + if match[1]-match[0] == len(tagconv) { + removePos = curField.Tag.Pos() + removeEnd = curField.Tag.End() + } else { + // Remove the json tag if omitempty is the only field + removePos, err = astutil.PosInStringLiteral(curField.Tag, match[0]) + if err != nil { + return + } + removeEnd, err = astutil.PosInStringLiteral(curField.Tag, match[1]) + if err != nil { + return + } + } + } + pass.Report(analysis.Diagnostic{ + Pos: curField.Tag.Pos(), + End: curField.Tag.End(), + Category: "omitzero", + Message: "Omitempty has no effect on nested struct fields", + SuggestedFixes: []analysis.SuggestedFix{ + { + Message: "Remove redundant omitempty tag", + TextEdits: []analysis.TextEdit{ + { + Pos: removePos, + End: removeEnd, + }, + }, + }, + { + Message: "Replace omitempty with omitzero (behavior change)", + TextEdits: []analysis.TextEdit{ + { + Pos: omitEmptyPos, + End: omitEmptyEnd, + NewText: []byte(",omitzero"), + }, + }, + }, + }}) +} + +// The omitzero pass searches for instances of "omitempty" in a json field tag on a +// struct. Since "omitempty" does not have any effect when applied to a struct field, +// it suggests either deleting "omitempty" or replacing it with "omitzero", which +// correctly excludes structs from a json encoding. +func omitzero(pass *analysis.Pass) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + info := pass.TypesInfo + for curFile := range filesUsing(inspect, info, "go1.24") { + for curStruct := range curFile.Preorder((*ast.StructType)(nil)) { + for _, curField := range curStruct.Node().(*ast.StructType).Fields.List { + checkOmitEmptyField(pass, info, curField) + } + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go b/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go new file mode 100644 index 00000000000..f6c50cc93bb --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go @@ -0,0 +1,30 @@ +package omitzero + +type Foo struct { + EmptyStruct struct{} `json:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} + +type Bar struct { + NonEmptyStruct struct{ a int } `json:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} + +type C struct { + D string `json:",omitempty"` +} + +type R struct { + M string `json:",omitempty"` +} + +type A struct { + C C `json:"test,omitempty"` // want "Omitempty has no effect on nested struct fields" + R R `json:"test"` +} + +type X struct { + NonEmptyStruct struct{ a int } `json:",omitempty" yaml:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} + +type Y struct { + NonEmptyStruct struct{ a int } `yaml:",omitempty" json:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} diff --git a/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go.golden b/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go.golden new file mode 100644 index 00000000000..daf0ea8235b --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/omitzero/omitzero.go.golden @@ -0,0 +1,63 @@ +-- Replace omitempty with omitzero (behavior change) -- +package omitzero + +type Foo struct { + EmptyStruct struct{} `json:",omitzero"` // want "Omitempty has no effect on nested struct fields" +} + +type Bar struct { + NonEmptyStruct struct{ a int } `json:",omitzero"` // want "Omitempty has no effect on nested struct fields" +} + +type C struct { + D string `json:",omitempty"` +} + +type R struct { + M string `json:",omitempty"` +} + +type A struct { + C C `json:"test,omitzero"` // want "Omitempty has no effect on nested struct fields" + R R `json:"test"` +} + +type X struct { + NonEmptyStruct struct{ a int } `json:",omitzero" yaml:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} + +type Y struct { + NonEmptyStruct struct{ a int } `yaml:",omitempty" json:",omitzero"` // want "Omitempty has no effect on nested struct fields" +} + +-- Remove redundant omitempty tag -- +package omitzero + +type Foo struct { + EmptyStruct struct{} // want "Omitempty has no effect on nested struct fields" +} + +type Bar struct { + NonEmptyStruct struct{ a int } // want "Omitempty has no effect on nested struct fields" +} + +type C struct { + D string `json:",omitempty"` +} + +type R struct { + M string `json:",omitempty"` +} + +type A struct { + C C `json:"test"` // want "Omitempty has no effect on nested struct fields" + R R `json:"test"` +} + +type X struct { + NonEmptyStruct struct{ a int } `yaml:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} + +type Y struct { + NonEmptyStruct struct{ a int } `yaml:",omitempty"` // want "Omitempty has no effect on nested struct fields" +} diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b9f843fc63c..9ee05159c03 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -472,7 +472,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24", "Default": "true" }, { @@ -1129,7 +1129,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, diff --git a/gopls/internal/golang/highlight.go b/gopls/internal/golang/highlight.go index 252485306b5..a4f81e35153 100644 --- a/gopls/internal/golang/highlight.go +++ b/gopls/internal/golang/highlight.go @@ -12,12 +12,12 @@ import ( "go/types" "strconv" "strings" - "unicode/utf8" - "golang.org/x/tools/go/ast/astutil" + astutil "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/fmtstr" ) @@ -210,11 +210,11 @@ func highlightPrintf(call *ast.CallExpr, idx int, cursorPos token.Pos, lit *ast. // highlightPair highlights the operation and its potential argument pair if the cursor is within either range. highlightPair := func(rang fmtstr.Range, argIndex int) { - rangeStart, err := posInStringLiteral(lit, rang.Start) + rangeStart, err := internalastutil.PosInStringLiteral(lit, rang.Start) if err != nil { return } - rangeEnd, err := posInStringLiteral(lit, rang.End) + rangeEnd, err := internalastutil.PosInStringLiteral(lit, rang.End) if err != nil { return } @@ -277,38 +277,6 @@ func highlightPrintf(call *ast.CallExpr, idx int, cursorPos token.Pos, lit *ast. } } -// posInStringLiteral returns the position within a string literal -// corresponding to the specified byte offset within the logical -// string that it denotes. -func posInStringLiteral(lit *ast.BasicLit, offset int) (token.Pos, error) { - raw := lit.Value - - value, err := strconv.Unquote(raw) - if err != nil { - return 0, err - } - if !(0 <= offset && offset <= len(value)) { - return 0, fmt.Errorf("invalid offset") - } - - // remove quotes - quote := raw[0] // '"' or '`' - raw = raw[1 : len(raw)-1] - - var ( - i = 0 // byte index within logical value - pos = lit.ValuePos + 1 // position within literal - ) - for raw != "" && i < offset { - r, _, rest, _ := strconv.UnquoteChar(raw, quote) // can't fail - sz := len(raw) - len(rest) // length of literal char in raw bytes - pos += token.Pos(sz) - raw = raw[sz:] - i += utf8.RuneLen(r) - } - return pos, nil -} - type posRange struct { start, end token.Pos } diff --git a/internal/astutil/util.go b/internal/astutil/util.go new file mode 100644 index 00000000000..3b3c6259568 --- /dev/null +++ b/internal/astutil/util.go @@ -0,0 +1,45 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package astutil + +import ( + "fmt" + "go/ast" + "go/token" + "strconv" + "unicode/utf8" +) + +// PosInStringLiteral returns the position within a string literal +// corresponding to the specified byte offset within the logical +// string that it denotes. +func PosInStringLiteral(lit *ast.BasicLit, offset int) (token.Pos, error) { + raw := lit.Value + + value, err := strconv.Unquote(raw) + if err != nil { + return 0, err + } + if !(0 <= offset && offset <= len(value)) { + return 0, fmt.Errorf("invalid offset") + } + + // remove quotes + quote := raw[0] // '"' or '`' + raw = raw[1 : len(raw)-1] + + var ( + i = 0 // byte index within logical value + pos = lit.ValuePos + 1 // position within literal + ) + for raw != "" && i < offset { + r, _, rest, _ := strconv.UnquoteChar(raw, quote) // can't fail + sz := len(raw) - len(rest) // length of literal char in raw bytes + pos += token.Pos(sz) + raw = raw[sz:] + i += utf8.RuneLen(r) + } + return pos, nil +} From 32c4665a21f1021972e0362562bdfa17aad8afa1 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 15 Jan 2025 14:26:22 +0000 Subject: [PATCH 044/309] gopls/internal/golang/completion: avoid crash in comment completion Avoid an observed crash in comment completion, as well as another nearby crash that was discovered in the course of debugging. Add debugging bug reports for the case of a missing function definition. Fixes golang/go#71273 Change-Id: Ibac993e0cf041fc074e5f00a7ce6d5718d77052f Reviewed-on: https://go-review.googlesource.com/c/tools/+/642877 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/golang/completion/completion.go | 64 +++++++++++++------ .../golang/completion/postfix_snippets.go | 2 +- gopls/internal/golang/completion/util.go | 2 +- .../marker/testdata/completion/comment.txt | 6 ++ internal/packagesinternal/packages.go | 4 +- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index f438a220000..4c340055233 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -31,6 +31,7 @@ import ( "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/cache/metadata" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/fuzzy" "golang.org/x/tools/gopls/internal/golang" @@ -218,17 +219,16 @@ type completer struct { // filename is the name of the file associated with this completion request. filename string - // file is the AST of the file associated with this completion request. - file *ast.File + // pgf is the AST of the file associated with this completion request. + pgf *parsego.File // debugging // goversion is the version of Go in force in the file, as // defined by x/tools/internal/versions. Empty if unknown. // Since go1.22 it should always be known. goversion string - // (tokFile, pos) is the position at which the request was triggered. - tokFile *token.File - pos token.Pos + // pos is the position at which the request was triggered. + pos token.Pos // path is the path of AST nodes enclosing the position. path []ast.Node @@ -410,7 +410,7 @@ func (c *completer) setSurrounding(ident *ast.Ident) { content: ident.Name, cursor: c.pos, // Overwrite the prefix only. - tokFile: c.tokFile, + tokFile: c.pgf.Tok, start: ident.Pos(), end: ident.End(), mapper: c.mapper, @@ -435,7 +435,7 @@ func (c *completer) getSurrounding() *Selection { c.surrounding = &Selection{ content: "", cursor: c.pos, - tokFile: c.tokFile, + tokFile: c.pgf.Tok, start: c.pos, end: c.pos, mapper: c.mapper, @@ -609,8 +609,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p }, fh: fh, filename: fh.URI().Path(), - tokFile: pgf.Tok, - file: pgf.File, + pgf: pgf, goversion: goversion, path: path, pos: pos, @@ -711,7 +710,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p // search queue or completion items directly for different completion contexts. func (c *completer) collectCompletions(ctx context.Context) error { // Inside import blocks, return completions for unimported packages. - for _, importSpec := range c.file.Imports { + for _, importSpec := range c.pgf.File.Imports { if !(importSpec.Path.Pos() <= c.pos && c.pos <= importSpec.Path.End()) { continue } @@ -719,7 +718,7 @@ func (c *completer) collectCompletions(ctx context.Context) error { } // Inside comments, offer completions for the name of the relevant symbol. - for _, comment := range c.file.Comments { + for _, comment := range c.pgf.File.Comments { if comment.Pos() < c.pos && c.pos <= comment.End() { c.populateCommentCompletions(comment) return nil @@ -749,7 +748,7 @@ func (c *completer) collectCompletions(ctx context.Context) error { switch n := c.path[0].(type) { case *ast.Ident: - if c.file.Name == n { + if c.pgf.File.Name == n { return c.packageNameCompletions(ctx, c.fh.URI(), n) } else if sel, ok := c.path[1].(*ast.SelectorExpr); ok && sel.Sel == n { // Is this the Sel part of a selector? @@ -921,14 +920,14 @@ func (c *completer) populateImportCompletions(searchImport *ast.ImportSpec) erro c.surrounding = &Selection{ content: content, cursor: c.pos, - tokFile: c.tokFile, + tokFile: c.pgf.Tok, start: start, end: end, mapper: c.mapper, } seenImports := make(map[string]struct{}) - for _, importSpec := range c.file.Imports { + for _, importSpec := range c.pgf.File.Imports { if importSpec.Path.Value == importPath { continue } @@ -1024,7 +1023,7 @@ func (c *completer) populateCommentCompletions(comment *ast.CommentGroup) { c.setSurroundingForComment(comment) // Using the next line pos, grab and parse the exported symbol on that line - for _, n := range c.file.Decls { + for _, n := range c.pgf.File.Decls { declLine := safetoken.Line(file, n.Pos()) // if the comment is not in, directly above or on the same line as a declaration if declLine != commentLine && declLine != commentLine+1 && @@ -1080,8 +1079,33 @@ func (c *completer) populateCommentCompletions(comment *ast.CommentGroup) { // collect receiver struct fields if node.Recv != nil { - sig := c.pkg.TypesInfo().Defs[node.Name].(*types.Func).Signature() - _, named := typesinternal.ReceiverNamed(sig.Recv()) // may be nil if ill-typed + obj := c.pkg.TypesInfo().Defs[node.Name] + switch obj.(type) { + case nil: + report := func() { + bug.Reportf("missing def for func %s", node.Name) + } + // Debugging golang/go#71273. + if !slices.Contains(c.pkg.CompiledGoFiles(), c.pgf) { + if c.snapshot.View().Type() == cache.GoPackagesDriverView { + report() + } else { + report() + } + } else { + report() + } + continue + case *types.Func: + default: + bug.Reportf("unexpected func obj type %T for %s", obj, node.Name) + } + sig := obj.(*types.Func).Signature() + recv := sig.Recv() + if recv == nil { + continue // may be nil if ill-typed + } + _, named := typesinternal.ReceiverNamed(recv) if named != nil { if recvStruct, ok := named.Underlying().(*types.Struct); ok { for i := 0; i < recvStruct.NumFields(); i++ { @@ -1133,7 +1157,7 @@ func (c *completer) setSurroundingForComment(comments *ast.CommentGroup) { c.surrounding = &Selection{ content: cursorComment.Text[start:end], cursor: c.pos, - tokFile: c.tokFile, + tokFile: c.pgf.Tok, start: token.Pos(int(cursorComment.Slash) + start), end: token.Pos(int(cursorComment.Slash) + end), mapper: c.mapper, @@ -1437,7 +1461,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error { return nil } - goversion := c.pkg.TypesInfo().FileVersions[c.file] + goversion := c.pkg.TypesInfo().FileVersions[c.pgf.File] // Extract the package-level candidates using a quick parse. var g errgroup.Group @@ -1694,7 +1718,7 @@ func (c *completer) lexical(ctx context.Context) error { // Make sure the package name isn't already in use by another // object, and that this file doesn't import the package yet. // TODO(adonovan): what if pkg.Path has vendor/ prefix? - if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.Types() && !alreadyImports(c.file, golang.ImportPath(pkg.Path())) { + if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.Types() && !alreadyImports(c.pgf.File, golang.ImportPath(pkg.Path())) { seen[pkg.Name()] = struct{}{} obj := types.NewPkgName(0, nil, pkg.Name(), pkg) imp := &importInfo{ diff --git a/gopls/internal/golang/completion/postfix_snippets.go b/gopls/internal/golang/completion/postfix_snippets.go index 4ffd14225fa..1bafe848490 100644 --- a/gopls/internal/golang/completion/postfix_snippets.go +++ b/gopls/internal/golang/completion/postfix_snippets.go @@ -653,7 +653,7 @@ func (c *completer) importIfNeeded(pkgPath string, scope *types.Scope) (string, defaultName := imports.ImportPathToAssumedName(pkgPath) // Check if file already imports pkgPath. - for _, s := range c.file.Imports { + for _, s := range c.pgf.File.Imports { // TODO(adonovan): what if pkgPath has a vendor/ suffix? // This may be the cause of go.dev/issue/56291. if string(metadata.UnquoteImportPath(s)) == pkgPath { diff --git a/gopls/internal/golang/completion/util.go b/gopls/internal/golang/completion/util.go index 766484e2fc8..cb51d65ffee 100644 --- a/gopls/internal/golang/completion/util.go +++ b/gopls/internal/golang/completion/util.go @@ -284,7 +284,7 @@ func isBasicKind(t types.Type, k types.BasicInfo) bool { } func (c *completer) editText(from, to token.Pos, newText string) ([]protocol.TextEdit, error) { - start, end, err := safetoken.Offsets(c.tokFile, from, to) + start, end, err := safetoken.Offsets(c.pgf.Tok, from, to) if err != nil { return nil, err // can't happen: from/to came from c } diff --git a/gopls/internal/test/marker/testdata/completion/comment.txt b/gopls/internal/test/marker/testdata/completion/comment.txt index f66bfdab186..34ef242e2f9 100644 --- a/gopls/internal/test/marker/testdata/completion/comment.txt +++ b/gopls/internal/test/marker/testdata/completion/comment.txt @@ -79,3 +79,9 @@ func Multiline() int { //@item(multiline, "Multiline", "func() int", "func") // //@complete(" ", multiline) return 0 } + +// This test checks that gopls does not panic if the receiver is syntactically +// present but empty. +// +// //@complete(" ") +func () _() {} diff --git a/internal/packagesinternal/packages.go b/internal/packagesinternal/packages.go index 66e69b4389d..784605914e0 100644 --- a/internal/packagesinternal/packages.go +++ b/internal/packagesinternal/packages.go @@ -5,7 +5,7 @@ // Package packagesinternal exposes internal-only fields from go/packages. package packagesinternal -var GetDepsErrors = func(p interface{}) []*PackageError { return nil } +var GetDepsErrors = func(p any) []*PackageError { return nil } type PackageError struct { ImportStack []string // shortest path from package named on command line to this one @@ -16,5 +16,5 @@ type PackageError struct { var TypecheckCgo int var DepsErrors int // must be set as a LoadMode to call GetDepsErrors -var SetModFlag = func(config interface{}, value string) {} +var SetModFlag = func(config any, value string) {} var SetModFile = func(config interface{}, value string) {} From cab66080832619aeb74ac19cf1792b3c034ef119 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 15 Jan 2025 21:52:19 +0000 Subject: [PATCH 045/309] gopls/internal/golang/completion: fix crash adding receiver type params Fix an inaccurate assumption that type names in receiver position must be Named or Alias types. In the presence of invalid code, such objects could also be have Basic type. Fixes golang/go#71044 Change-Id: Iad6a3f09aa210e7eee2edf82cec6a990bc137e17 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643016 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/golang/completion/format.go | 10 ++++++---- .../marker/testdata/fixedbugs/issue71044.txt | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/fixedbugs/issue71044.txt diff --git a/gopls/internal/golang/completion/format.go b/gopls/internal/golang/completion/format.go index f4fc7339b95..cf46463078a 100644 --- a/gopls/internal/golang/completion/format.go +++ b/gopls/internal/golang/completion/format.go @@ -20,7 +20,6 @@ import ( "golang.org/x/tools/gopls/internal/util/typesutil" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/imports" - "golang.org/x/tools/internal/typesinternal" ) var ( @@ -60,9 +59,12 @@ func (c *completer) item(ctx context.Context, cand candidate) (CompletionItem, e if obj.Type() == nil { detail = "" } - if isTypeName(obj) && c.wantTypeParams() { - // obj is a *types.TypeName, so its type must be Alias|Named. - tparams := typesinternal.TypeParams(obj.Type().(typesinternal.NamedOrAlias)) + + type hasTypeParams interface{ TypeParams() *types.TypeParamList } + if genericType, _ := obj.Type().(hasTypeParams); genericType != nil && isTypeName(obj) && c.wantTypeParams() { + // golang/go#71044: note that type names can be basic types, even in + // receiver position, for invalid code. + tparams := genericType.TypeParams() label += typesutil.FormatTypeParams(tparams) insert = label // maintain invariant above (label == insert) } diff --git a/gopls/internal/test/marker/testdata/fixedbugs/issue71044.txt b/gopls/internal/test/marker/testdata/fixedbugs/issue71044.txt new file mode 100644 index 00000000000..4b0f2045343 --- /dev/null +++ b/gopls/internal/test/marker/testdata/fixedbugs/issue71044.txt @@ -0,0 +1,18 @@ +This test checks that we don't crash while completing receivers that may happen +to be builtin types (due to invalid code). This crash was reported by telemetry +in golang/go#71044. + +-- flags -- +-ignore_extra_diags + +-- go.mod -- +module example.com/amap + +go 1.18 + +-- a.go -- +package amap + +import "unsafe" + +func (unsafe.Pointer) _() {} //@ rank("unsafe") From 1261a24ceb1867ea7439eda244e53e7ace4ad777 Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Tue, 7 Jan 2025 14:40:47 -0500 Subject: [PATCH 046/309] gopls/internal/analysis/modernize: slicesdelete Adds a new modernizer that suggests replacing instances of append(s[:i], s[i+k:]...) with slices.Delete(s, i, i+k) Handles other variations like append(s[:i-1], s[i:]...) and append(s[:i+2], s[i+3:]...) Updates golang/go#70815 Change-Id: I71981d6e8d6973bca17153b95f2eb9f1f229522d Reviewed-on: https://go-review.googlesource.com/c/tools/+/641357 Reviewed-by: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/doc/analyzers.md | 4 +- gopls/internal/analysis/modernize/doc.go | 4 +- .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + .../analysis/modernize/slicesdelete.go | 125 ++++++++++++++++++ .../testdata/src/slicesdelete/slicesdelete.go | 36 +++++ .../src/slicesdelete/slicesdelete.go.golden | 36 +++++ gopls/internal/doc/api.json | 4 +- 8 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 gopls/internal/analysis/modernize/slicesdelete.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 26830628f74..04b91400c92 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -482,7 +482,9 @@ existing code by using more modern features of Go, such as: added in go1.19; - replacing uses of context.WithCancel in tests with t.Context, added in go1.24; - - replacing omitempty by omitzero on structs, added in go 1.24 + - replacing omitempty by omitzero on structs, added in go 1.24; + - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), + added in go1.21 Default: on. diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 139f1e45520..78cc6a6d11f 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -25,5 +25,7 @@ // added in go1.19; // - replacing uses of context.WithCancel in tests with t.Context, added in // go1.24; -// - replacing omitempty by omitzero on structs, added in go 1.24 +// - replacing omitempty by omitzero on structs, added in go 1.24; +// - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), +// added in go1.21 package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 55f15e655ad..9c1be95a7fd 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -68,6 +68,7 @@ func run(pass *analysis.Pass) (any, error) { minmax(pass) omitzero(pass) slicescontains(pass) + slicesdelete(pass) sortslice(pass) testingContext(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index b95895eb55d..4710440b6a4 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -21,6 +21,7 @@ func Test(t *testing.T) { "minmax", "omitzero", "slicescontains", + "slicesdelete", "sortslice", "testingcontext", ) diff --git a/gopls/internal/analysis/modernize/slicesdelete.go b/gopls/internal/analysis/modernize/slicesdelete.go new file mode 100644 index 00000000000..f1f96c7d5fc --- /dev/null +++ b/gopls/internal/analysis/modernize/slicesdelete.go @@ -0,0 +1,125 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "go/ast" + "go/constant" + "go/token" + "go/types" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" +) + +// The slicesdelete pass attempts to replace instances of append(s[:i], s[i+k:]...) +// with slices.Delete(s, i, i+k) where k is some positive constant. +// Other variations that will also have suggested replacements include: +// append(s[:i-1], s[i:]...) and append(s[:i+k1], s[i+k2:]) where k2 > k1. +func slicesdelete(pass *analysis.Pass) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + info := pass.TypesInfo + report := func(call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) { + pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Category: "slicesdelete", + Message: "Replace append with slices.Delete", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Replace append with slices.Delete", + TextEdits: []analysis.TextEdit{ + // Change name of called function. + { + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: []byte("slices.Delete"), + }, + // Delete ellipsis. + { + Pos: call.Ellipsis, + End: call.Ellipsis + token.Pos(len("...")), // delete ellipsis + }, + // Remove second slice variable name. + { + Pos: slice2.X.Pos(), + End: slice2.X.End(), + }, + // Insert after first slice variable name. + { + Pos: slice1.X.End(), + NewText: []byte(", "), + }, + // Remove brackets and colons. + { + Pos: slice1.Lbrack, + End: slice1.High.Pos(), + }, + { + Pos: slice1.Rbrack, + End: slice1.Rbrack + 1, + }, + { + Pos: slice2.Lbrack, + End: slice2.Lbrack + 1, + }, + { + Pos: slice2.Low.End(), + End: slice2.Rbrack + 1, + }, + }, + }}, + }) + } + for curFile := range filesUsing(inspect, info, "go1.21") { + for curCall := range curFile.Preorder((*ast.CallExpr)(nil)) { + call := curCall.Node().(*ast.CallExpr) + if id, ok := call.Fun.(*ast.Ident); ok && len(call.Args) == 2 { + // Verify we have append with two slices and ... operator, + // the first slice has no low index and second slice has no + // high index, and not a three-index slice. + if call.Ellipsis.IsValid() && info.Uses[id] == builtinAppend { + slice1, ok1 := call.Args[0].(*ast.SliceExpr) + slice2, ok2 := call.Args[1].(*ast.SliceExpr) + if ok1 && slice1.Low == nil && !slice1.Slice3 && + ok2 && slice2.High == nil && !slice2.Slice3 && + equalSyntax(slice1.X, slice2.X) && + increasingSliceIndices(info, slice1.High, slice2.Low) { + // Have append(s[:a], s[b:]...) where we can verify a < b. + report(call, slice1, slice2) + } + } + } + } + } +} + +// Given two slice indices a and b, returns true if we can verify that a < b. +// It recognizes certain forms such as i+k1 < i+k2 where k1 < k2. +func increasingSliceIndices(info *types.Info, a, b ast.Expr) bool { + + // Given an expression of the form i±k, returns (i, k) + // where k is a signed constant. Otherwise it returns (e, 0). + split := func(e ast.Expr) (ast.Expr, constant.Value) { + if binary, ok := e.(*ast.BinaryExpr); ok && (binary.Op == token.SUB || binary.Op == token.ADD) { + // Negate constants if operation is subtract instead of add + if k := info.Types[binary.Y].Value; k != nil { + return binary.X, constant.UnaryOp(binary.Op, k, 0) // i ± k + } + } + return e, constant.MakeInt64(0) + } + + // Handle case where either a or b is a constant + ak := info.Types[a].Value + bk := info.Types[b].Value + if ak != nil || bk != nil { + return ak != nil && bk != nil && constant.Compare(ak, token.LSS, bk) + } + + ai, ak := split(a) + bi, bk := split(b) + return equalSyntax(ai, bi) && constant.Compare(ak, token.LSS, bk) +} diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go new file mode 100644 index 00000000000..a710d06f2fe --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go @@ -0,0 +1,36 @@ +package slicesdelete + +var g struct{ f []int } + +func slicesdelete(test, other []byte, i int) { + const k = 1 + _ = append(test[:i], test[i+1:]...) // want "Replace append with slices.Delete" + + _ = append(test[:i+1], test[i+2:]...) // want "Replace append with slices.Delete" + + _ = append(test[:i+1], test[i+1:]...) // not deleting any slice elements + + _ = append(test[:i], test[i-1:]...) // not deleting any slice elements + + _ = append(test[:i-1], test[i:]...) // want "Replace append with slices.Delete" + + _ = append(test[:i-2], test[i+1:]...) // want "Replace append with slices.Delete" + + _ = append(test[:i-2], other[i+1:]...) // different slices "test" and "other" + + _ = append(test[:i-2], other[i+1+k:]...) // cannot verify a < b + + _ = append(test[:i-2], test[11:]...) // cannot verify a < b + + _ = append(test[:1], test[3:]...) // want "Replace append with slices.Delete" + + _ = append(g.f[:i], g.f[i+k:]...) // want "Replace append with slices.Delete" + + _ = append(test[:3], test[i+1:]...) // cannot verify a < b + + _ = append(test[:i-4], test[i-1:]...) // want "Replace append with slices.Delete" + + _ = append(test[:1+2], test[3+4:]...) // want "Replace append with slices.Delete" + + _ = append(test[:1+2], test[i-1:]...) // cannot verify a < b +} diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden new file mode 100644 index 00000000000..8c2f21a2782 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden @@ -0,0 +1,36 @@ +package slicesdelete + +var g struct{ f []int } + +func slicesdelete(test, other []byte, i int) { + const k = 1 + _ = slices.Delete(test, i, i+1) // want "Replace append with slices.Delete" + + _ = slices.Delete(test, i+1, i+2) // want "Replace append with slices.Delete" + + _ = append(test[:i+1], test[i+1:]...) // not deleting any slice elements + + _ = append(test[:i], test[i-1:]...) // not deleting any slice elements + + _ = slices.Delete(test, i-1, i) // want "Replace append with slices.Delete" + + _ = slices.Delete(test, i-2, i+1) // want "Replace append with slices.Delete" + + _ = append(test[:i-2], other[i+1:]...) // different slices "test" and "other" + + _ = append(test[:i-2], other[i+1+k:]...) // cannot verify a < b + + _ = append(test[:i-2], test[11:]...) // cannot verify a < b + + _ = slices.Delete(test, 1, 3) // want "Replace append with slices.Delete" + + _ = slices.Delete(g.f, i, i+k) // want "Replace append with slices.Delete" + + _ = append(test[:3], test[i+1:]...) // cannot verify a < b + + _ = slices.Delete(test, i-4, i-1) // want "Replace append with slices.Delete" + + _ = slices.Delete(test, 1+2, 3+4) // want "Replace append with slices.Delete" + + _ = append(test[:1+2], test[i-1:]...) // cannot verify a < b +} \ No newline at end of file diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 9ee05159c03..bf9a06ccaad 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -472,7 +472,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21", "Default": "true" }, { @@ -1129,7 +1129,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From df4e4ef61cb05515e337ca7d8a1c7906bf3775fa Mon Sep 17 00:00:00 2001 From: Merrick Clay Date: Mon, 20 Jan 2025 23:35:39 -0700 Subject: [PATCH 047/309] ssa: fix typo in doc comment for Program.FuncValue Change-Id: I0828d0a7265d1e7c4f8c7a17832357826fbdfcfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/643575 Auto-Submit: Alan Donovan Commit-Queue: Ian Lance Taylor Reviewed-by: Alan Donovan Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- go/ssa/source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ssa/source.go b/go/ssa/source.go index 7b71c88d120..055a6b1ef5f 100644 --- a/go/ssa/source.go +++ b/go/ssa/source.go @@ -191,7 +191,7 @@ func (prog *Program) packageLevelMember(obj types.Object) Member { } // FuncValue returns the SSA function or (non-interface) method -// denoted by the specified func symbol. It returns nil id the symbol +// denoted by the specified func symbol. It returns nil if the symbol // denotes an interface method, or belongs to a package that was not // created by prog.CreatePackage. func (prog *Program) FuncValue(obj *types.Func) *Function { From 96a07bb5a10cad767e4a70c7fad8c2d09f663712 Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Wed, 15 Jan 2025 20:35:13 -0500 Subject: [PATCH 048/309] gopls/internal/settings: include deprecation message in api-json - gopls api-json will return deprecation message as additional property of a configuration. - go generate ./... will parse the comment of a given field as doc(entire doc comment) and deprecation message(introduced by prefix "Deprecated: "). Follow pattern defined in https://go.dev/wiki/Deprecated. VSCode-Go side CL 643056. For golang/vscode-go#3632 Change-Id: Ia6a67948c75dd51b5cb76bbd6d7385b95ea979e4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642998 Auto-Submit: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/generate/generate.go | 20 ++-- gopls/doc/settings.md | 6 + .../analysis/deprecated/deprecated.go | 45 +++----- gopls/internal/doc/api.go | 17 +-- gopls/internal/doc/api.json | 109 ++++++++++++------ gopls/internal/golang/completion/format.go | 6 +- gopls/internal/settings/settings.go | 16 ++- .../integration/completion/completion_test.go | 4 +- internal/astutil/comment.go | 28 +++++ 9 files changed, 160 insertions(+), 91 deletions(-) create mode 100644 internal/astutil/comment.go diff --git a/gopls/doc/generate/generate.go b/gopls/doc/generate/generate.go index 7d92b2629d5..42d41bbb1b6 100644 --- a/gopls/doc/generate/generate.go +++ b/gopls/doc/generate/generate.go @@ -44,6 +44,7 @@ import ( "golang.org/x/tools/gopls/internal/mod" "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/util/safetoken" + internalastutil "golang.org/x/tools/internal/astutil" ) func main() { @@ -221,11 +222,13 @@ func loadOptions(category reflect.Value, optsType types.Object, pkg *packages.Pa if len(path) < 2 { return nil, fmt.Errorf("could not find AST node for field %v", typesField) } + // The AST field gives us the doc. astField, ok := path[1].(*ast.Field) if !ok { return nil, fmt.Errorf("unexpected AST path %v", path) } + description, deprecation := astField.Doc.Text(), internalastutil.Deprecation(astField.Doc) // The reflect field gives us the default value. reflectField := category.FieldByName(typesField.Name()) @@ -285,14 +288,15 @@ func loadOptions(category reflect.Value, optsType types.Object, pkg *packages.Pa status := reflectStructField.Tag.Get("status") opts = append(opts, &doc.Option{ - Name: name, - Type: typ, - Doc: lowerFirst(astField.Doc.Text()), - Default: def, - EnumKeys: enumKeys, - EnumValues: enums[typesField.Type()], - Status: status, - Hierarchy: hierarchy, + Name: name, + Type: typ, + Doc: lowerFirst(description), + Default: def, + EnumKeys: enumKeys, + EnumValues: enums[typesField.Type()], + Status: status, + Hierarchy: hierarchy, + DeprecationMessage: lowerFirst(strings.TrimPrefix(deprecation, "Deprecated: ")), }) } return opts, nil diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index 7dfe0870718..3d170b00dc3 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -208,6 +208,9 @@ Default: `false`. noSemanticString turns off the sending of the semantic token 'string' +Deprecated: Use SemanticTokenTypes["string"] = false instead. See +golang/vscode-go#3632 + Default: `false`. @@ -217,6 +220,9 @@ Default: `false`. noSemanticNumber turns off the sending of the semantic token 'number' +Deprecated: Use SemanticTokenTypes["number"] = false instead. See +golang/vscode-go#3632. + Default: `false`. diff --git a/gopls/internal/analysis/deprecated/deprecated.go b/gopls/internal/analysis/deprecated/deprecated.go index 1a8c4c56766..c6df00b4f50 100644 --- a/gopls/internal/analysis/deprecated/deprecated.go +++ b/gopls/internal/analysis/deprecated/deprecated.go @@ -19,6 +19,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/internal/analysisinternal" + internalastutil "golang.org/x/tools/internal/astutil" ) //go:embed doc.go @@ -155,26 +156,8 @@ type deprecatedNames struct { // them both as Facts and the return value. This is a simplified copy // of staticcheck's fact_deprecated analyzer. func collectDeprecatedNames(pass *analysis.Pass, ins *inspector.Inspector) (deprecatedNames, error) { - extractDeprecatedMessage := func(docs []*ast.CommentGroup) string { - for _, doc := range docs { - if doc == nil { - continue - } - parts := strings.Split(doc.Text(), "\n\n") - for _, part := range parts { - if !strings.HasPrefix(part, "Deprecated: ") { - continue - } - alt := part[len("Deprecated: "):] - alt = strings.Replace(alt, "\n", " ", -1) - return strings.TrimSpace(alt) - } - } - return "" - } - doDocs := func(names []*ast.Ident, docs *ast.CommentGroup) { - alt := extractDeprecatedMessage([]*ast.CommentGroup{docs}) + alt := strings.TrimPrefix(internalastutil.Deprecation(docs), "Deprecated: ") if alt == "" { return } @@ -185,19 +168,21 @@ func collectDeprecatedNames(pass *analysis.Pass, ins *inspector.Inspector) (depr } } - var docs []*ast.CommentGroup - for _, f := range pass.Files { - docs = append(docs, f.Doc) - } - if alt := extractDeprecatedMessage(docs); alt != "" { - // Don't mark package syscall as deprecated, even though - // it is. A lot of people still use it for simple - // constants like SIGKILL, and I am not comfortable - // telling them to use x/sys for that. - if pass.Pkg.Path() != "syscall" { - pass.ExportPackageFact(&deprecationFact{alt}) + // Is package deprecated? + // + // Don't mark package syscall as deprecated, even though + // it is. A lot of people still use it for simple + // constants like SIGKILL, and I am not comfortable + // telling them to use x/sys for that. + if pass.Pkg.Path() != "syscall" { + for _, f := range pass.Files { + if depr := internalastutil.Deprecation(f.Doc); depr != "" { + pass.ExportPackageFact(&deprecationFact{depr}) + break + } } } + nodeFilter := []ast.Node{ (*ast.GenDecl)(nil), (*ast.FuncDecl)(nil), diff --git a/gopls/internal/doc/api.go b/gopls/internal/doc/api.go index a096f5ad63e..258f90d49ae 100644 --- a/gopls/internal/doc/api.go +++ b/gopls/internal/doc/api.go @@ -27,14 +27,15 @@ type API struct { } type Option struct { - Name string - Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration - Doc string - EnumKeys EnumKeys - EnumValues []EnumValue - Default string - Status string - Hierarchy string + Name string + Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration + Doc string + EnumKeys EnumKeys + EnumValues []EnumValue + Default string + Status string + Hierarchy string + DeprecationMessage string } type EnumKeys struct { diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index bf9a06ccaad..4a8e10f6132 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -12,7 +12,8 @@ "EnumValues": null, "Default": "[]", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "env", @@ -25,7 +26,8 @@ "EnumValues": null, "Default": "{}", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "directoryFilters", @@ -38,7 +40,8 @@ "EnumValues": null, "Default": "[\"-**/node_modules\"]", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "templateExtensions", @@ -51,7 +54,8 @@ "EnumValues": null, "Default": "[]", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "memoryMode", @@ -64,7 +68,8 @@ "EnumValues": null, "Default": "\"\"", "Status": "experimental", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "expandWorkspaceToModule", @@ -77,7 +82,8 @@ "EnumValues": null, "Default": "true", "Status": "experimental", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "standaloneTags", @@ -90,7 +96,8 @@ "EnumValues": null, "Default": "[\"ignore\"]", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "hoverKind", @@ -120,7 +127,8 @@ ], "Default": "\"FullDocumentation\"", "Status": "", - "Hierarchy": "ui.documentation" + "Hierarchy": "ui.documentation", + "DeprecationMessage": "" }, { "Name": "linkTarget", @@ -133,7 +141,8 @@ "EnumValues": null, "Default": "\"pkg.go.dev\"", "Status": "", - "Hierarchy": "ui.documentation" + "Hierarchy": "ui.documentation", + "DeprecationMessage": "" }, { "Name": "linksInHover", @@ -159,7 +168,8 @@ ], "Default": "true", "Status": "", - "Hierarchy": "ui.documentation" + "Hierarchy": "ui.documentation", + "DeprecationMessage": "" }, { "Name": "usePlaceholders", @@ -172,7 +182,8 @@ "EnumValues": null, "Default": "false", "Status": "", - "Hierarchy": "ui.completion" + "Hierarchy": "ui.completion", + "DeprecationMessage": "" }, { "Name": "completionBudget", @@ -185,7 +196,8 @@ "EnumValues": null, "Default": "\"100ms\"", "Status": "debug", - "Hierarchy": "ui.completion" + "Hierarchy": "ui.completion", + "DeprecationMessage": "" }, { "Name": "matcher", @@ -211,7 +223,8 @@ ], "Default": "\"Fuzzy\"", "Status": "advanced", - "Hierarchy": "ui.completion" + "Hierarchy": "ui.completion", + "DeprecationMessage": "" }, { "Name": "experimentalPostfixCompletions", @@ -224,7 +237,8 @@ "EnumValues": null, "Default": "true", "Status": "experimental", - "Hierarchy": "ui.completion" + "Hierarchy": "ui.completion", + "DeprecationMessage": "" }, { "Name": "completeFunctionCalls", @@ -237,7 +251,8 @@ "EnumValues": null, "Default": "true", "Status": "", - "Hierarchy": "ui.completion" + "Hierarchy": "ui.completion", + "DeprecationMessage": "" }, { "Name": "importShortcut", @@ -263,7 +278,8 @@ ], "Default": "\"Both\"", "Status": "", - "Hierarchy": "ui.navigation" + "Hierarchy": "ui.navigation", + "DeprecationMessage": "" }, { "Name": "symbolMatcher", @@ -293,7 +309,8 @@ ], "Default": "\"FastFuzzy\"", "Status": "advanced", - "Hierarchy": "ui.navigation" + "Hierarchy": "ui.navigation", + "DeprecationMessage": "" }, { "Name": "symbolStyle", @@ -319,7 +336,8 @@ ], "Default": "\"Dynamic\"", "Status": "advanced", - "Hierarchy": "ui.navigation" + "Hierarchy": "ui.navigation", + "DeprecationMessage": "" }, { "Name": "symbolScope", @@ -341,7 +359,8 @@ ], "Default": "\"all\"", "Status": "", - "Hierarchy": "ui.navigation" + "Hierarchy": "ui.navigation", + "DeprecationMessage": "" }, { "Name": "analyses", @@ -630,7 +649,8 @@ "EnumValues": null, "Default": "{}", "Status": "", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "staticcheck", @@ -643,7 +663,8 @@ "EnumValues": null, "Default": "false", "Status": "experimental", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "vulncheck", @@ -665,7 +686,8 @@ ], "Default": "\"Off\"", "Status": "experimental", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "diagnosticsDelay", @@ -678,7 +700,8 @@ "EnumValues": null, "Default": "\"1s\"", "Status": "advanced", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "diagnosticsTrigger", @@ -700,7 +723,8 @@ ], "Default": "\"Edit\"", "Status": "experimental", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "analysisProgressReporting", @@ -713,7 +737,8 @@ "EnumValues": null, "Default": "true", "Status": "", - "Hierarchy": "ui.diagnostic" + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" }, { "Name": "hints", @@ -762,7 +787,8 @@ "EnumValues": null, "Default": "{}", "Status": "experimental", - "Hierarchy": "ui.inlayhint" + "Hierarchy": "ui.inlayhint", + "DeprecationMessage": "" }, { "Name": "codelenses", @@ -816,7 +842,8 @@ "EnumValues": null, "Default": "{\"generate\":true,\"regenerate_cgo\":true,\"run_govulncheck\":false,\"tidy\":true,\"upgrade_dependency\":true,\"vendor\":true}", "Status": "", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "" }, { "Name": "semanticTokens", @@ -829,12 +856,13 @@ "EnumValues": null, "Default": "false", "Status": "experimental", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "" }, { "Name": "noSemanticString", "Type": "bool", - "Doc": "noSemanticString turns off the sending of the semantic token 'string'\n", + "Doc": "noSemanticString turns off the sending of the semantic token 'string'\n\nDeprecated: Use SemanticTokenTypes[\"string\"] = false instead. See\ngolang/vscode-go#3632\n", "EnumKeys": { "ValueType": "", "Keys": null @@ -842,12 +870,13 @@ "EnumValues": null, "Default": "false", "Status": "experimental", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "use SemanticTokenTypes[\"string\"] = false instead. See\ngolang/vscode-go#3632\n" }, { "Name": "noSemanticNumber", "Type": "bool", - "Doc": "noSemanticNumber turns off the sending of the semantic token 'number'\n", + "Doc": "noSemanticNumber turns off the sending of the semantic token 'number'\n\nDeprecated: Use SemanticTokenTypes[\"number\"] = false instead. See\ngolang/vscode-go#3632.\n", "EnumKeys": { "ValueType": "", "Keys": null @@ -855,7 +884,8 @@ "EnumValues": null, "Default": "false", "Status": "experimental", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "use SemanticTokenTypes[\"number\"] = false instead. See\ngolang/vscode-go#3632.\n" }, { "Name": "semanticTokenTypes", @@ -868,7 +898,8 @@ "EnumValues": null, "Default": "{}", "Status": "experimental", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "" }, { "Name": "semanticTokenModifiers", @@ -881,7 +912,8 @@ "EnumValues": null, "Default": "{}", "Status": "experimental", - "Hierarchy": "ui" + "Hierarchy": "ui", + "DeprecationMessage": "" }, { "Name": "local", @@ -894,7 +926,8 @@ "EnumValues": null, "Default": "\"\"", "Status": "", - "Hierarchy": "formatting" + "Hierarchy": "formatting", + "DeprecationMessage": "" }, { "Name": "gofumpt", @@ -907,7 +940,8 @@ "EnumValues": null, "Default": "false", "Status": "", - "Hierarchy": "formatting" + "Hierarchy": "formatting", + "DeprecationMessage": "" }, { "Name": "verboseOutput", @@ -920,7 +954,8 @@ "EnumValues": null, "Default": "false", "Status": "debug", - "Hierarchy": "" + "Hierarchy": "", + "DeprecationMessage": "" } ] }, diff --git a/gopls/internal/golang/completion/format.go b/gopls/internal/golang/completion/format.go index cf46463078a..69339bffe84 100644 --- a/gopls/internal/golang/completion/format.go +++ b/gopls/internal/golang/completion/format.go @@ -18,6 +18,7 @@ import ( "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/gopls/internal/util/typesutil" + internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/imports" ) @@ -261,10 +262,7 @@ Suffixes: } else { item.Documentation = doc.Synopsis(comment.Text()) } - // The desired pattern is `^// Deprecated`, but the prefix has been removed - // TODO(rfindley): It doesn't look like this does the right thing for - // multi-line comments. - if strings.HasPrefix(comment.Text(), "Deprecated") { + if internalastutil.Deprecation(comment) != "" { if c.snapshot.Options().CompletionTags { item.Tags = []protocol.CompletionItemTag{protocol.ComplDeprecated} } else if c.snapshot.Options().CompletionDeprecated { diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 496062c40ec..13aaa61bdd9 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -172,9 +172,15 @@ type UIOptions struct { SemanticTokens bool `status:"experimental"` // NoSemanticString turns off the sending of the semantic token 'string' + // + // Deprecated: Use SemanticTokenTypes["string"] = false instead. See + // golang/vscode-go#3632 NoSemanticString bool `status:"experimental"` // NoSemanticNumber turns off the sending of the semantic token 'number' + // + // Deprecated: Use SemanticTokenTypes["number"] = false instead. See + // golang/vscode-go#3632. NoSemanticNumber bool `status:"experimental"` // SemanticTokenTypes configures the semantic token types. It allows @@ -1095,10 +1101,16 @@ func (o *Options) setOne(name string, value any) error { // TODO(hxjiang): deprecate noSemanticString and noSemanticNumber. case "noSemanticString": - return setBool(&o.NoSemanticString, value) + if err := setBool(&o.NoSemanticString, value); err != nil { + return err + } + return &SoftError{fmt.Sprintf("noSemanticString setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being).")} case "noSemanticNumber": - return setBool(&o.NoSemanticNumber, value) + if err := setBool(&o.NoSemanticNumber, value); err != nil { + return nil + } + return &SoftError{fmt.Sprintf("noSemanticNumber setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being).")} case "semanticTokenTypes": return setBoolMap(&o.SemanticTokenTypes, value) diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index 1f6eb2fe0fb..fe6a367e71b 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go @@ -471,12 +471,12 @@ module test.com go 1.16 -- prog.go -- package waste -// Deprecated, use newFoof +// Deprecated: use newFoof. func fooFunc() bool { return false } -// Deprecated +// Deprecated: bad. const badPi = 3.14 func doit() { diff --git a/internal/astutil/comment.go b/internal/astutil/comment.go new file mode 100644 index 00000000000..192d6430de0 --- /dev/null +++ b/internal/astutil/comment.go @@ -0,0 +1,28 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package astutil + +import ( + "go/ast" + "strings" +) + +// Deprecation returns the paragraph of the doc comment that starts with the +// conventional "Deprecation: " marker, as defined by +// https://go.dev/wiki/Deprecated, or "" if the documented symbol is not +// deprecated. +func Deprecation(doc *ast.CommentGroup) string { + for _, p := range strings.Split(doc.Text(), "\n\n") { + // There is still some ambiguity for deprecation message. This function + // only returns the paragraph introduced by "Deprecated: ". More + // information related to the deprecation may follow in additional + // paragraphs, but the deprecation message should be able to stand on + // its own. See golang/go#38743. + if strings.HasPrefix(p, "Deprecated: ") { + return p + } + } + return "" +} From b0164fc0abdbbdf9b536f4c09abc0b3b228c00d3 Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Tue, 21 Jan 2025 11:10:34 -0500 Subject: [PATCH 049/309] gopls/doc/release: add semantic token config change For golang/vscode-go#3632 Change-Id: I8d8a219c380ac8ac07a1baaef3bc89701894b985 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643497 Reviewed-by: Alan Donovan Auto-Submit: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI --- gopls/doc/release/v0.18.0.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index e2b730052bc..39590a7333e 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -10,6 +10,26 @@ VS Code's special "Go: Toggle GC details" command continues to work. +- The experimental `settings.semanticTokenTypes` configures the semantic token + types. It allows disabling types by setting each value to false. By default, + all types are enabled. + + The experimental `settings.semanticTokenModifiers` configures the semantic + token modifiers. It allows disabling modifiers by setting each value to false. + By default, all modifiers are enabled. + + The experimental `settings.noSemanticTokenString` and + `settings.noSemanticToken` settings are deprecated in favor of + `settings.semanticTokenTypes`. + + Users can set `settings.semanticTokenTypes[string] = false` to achieve the + same result as `settings.noSemanticTokenString`. The same applies to + `settings.noSemanticTokenNumber`. + + For now, gopls still honors `settings.noSemanticTokenString` and + `settings.noSemanticToken`, but will stop honoring the settings in the + upcoming release. + # New features ## "{Show,Hide} compiler optimization details" code action From 38d063139d92978fdd7bb971c0323cbab7c444d1 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 21 Jan 2025 22:19:39 +0000 Subject: [PATCH 050/309] gopls/internal/test: update hover test to be tolerant proxy changes In general, we should avoid using a hard-coded go.sum file when it is not important to the test. Update a hover test under discussion to avoid such a hard-coded file. Change-Id: Ib4241d29398572ac38c8d1b6e3344fc49dc397e2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643717 Reviewed-by: Peter Weinberger LUCI-TryBot-Result: Go LUCI --- gopls/internal/test/integration/misc/hover_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/gopls/internal/test/integration/misc/hover_test.go b/gopls/internal/test/integration/misc/hover_test.go index 1592b899b1d..7be50efe6d4 100644 --- a/gopls/internal/test/integration/misc/hover_test.go +++ b/gopls/internal/test/integration/misc/hover_test.go @@ -21,7 +21,7 @@ func TestHoverUnexported(t *testing.T) { -- golang.org/x/structs@v1.0.0/go.mod -- module golang.org/x/structs -go 1.12 +go 1.21 -- golang.org/x/structs@v1.0.0/types.go -- package structs @@ -40,12 +40,9 @@ func printMixed(m Mixed) { -- go.mod -- module mod.com -go 1.12 +go 1.21 require golang.org/x/structs v1.0.0 --- go.sum -- -golang.org/x/structs v1.0.0 h1:Ito/a7hBYZaNKShFrZKjfBA/SIPvmBrcPCBWPx5QeKk= -golang.org/x/structs v1.0.0/go.mod h1:47gkSIdo5AaQaWJS0upVORsxfEr1LL1MWv9dmYF3iq4= -- main.go -- package main @@ -60,6 +57,7 @@ func main() { // TODO: use a nested workspace folder here. WithOptions( ProxyFiles(proxy), + WriteGoSum("."), ).Run(t, mod, func(t *testing.T, env *Env) { env.OpenFile("main.go") mixedLoc := env.RegexpSearch("main.go", "Mixed") From 9f4a509fb8f68850262b3f937eb9926bd257b146 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Wed, 8 Jan 2025 13:13:14 +0000 Subject: [PATCH 051/309] gopls/internal/telemetry/cmd/stacks: add dry run flag This flag avoids updating existing issues. It still requires a GitHub auth token to determine which issues would get updated. It also still opens a browser window for new issues. For golang/go#71045. Change-Id: I6a6a636c26a402c9ea66160e14cd388b490b74b9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642421 Auto-Submit: Michael Pratt LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 75e67b7bd84..b14edc3f757 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -94,6 +94,8 @@ var ( daysFlag = flag.Int("days", 7, "number of previous days of telemetry data to read") + dryRun = flag.Bool("n", false, "dry run, avoid updating issues") + authToken string // mandatory GitHub authentication token (for R/W issues access) ) @@ -559,6 +561,12 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL newStackIDs = append(newStackIDs, id) writeStackComment(comment, stack, id, stackToURL[stack], stacks[stack]) } + + if *dryRun { + log.Printf("DRY RUN: would add stacks %s to issue #%d", newStackIDs, issue.Number) + continue + } + if err := addIssueComment(issue.Number, comment.String()); err != nil { log.Println(err) continue From 726ba3201f01ec88f3e3e845d5101fa8b415bec0 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 09:41:46 -0500 Subject: [PATCH 052/309] internal/telemetry/cmd/stacks: minor tweaks Edit some documentation, make other minor changes. Also minor tweaks to internal/util/moremaps. Change-Id: I04956af5e85e1c45e18da63532fb5f11c47c50d4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643775 Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 35 ++++++------------- gopls/internal/util/moremaps/maps.go | 4 +-- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index b14edc3f757..79749eb6f76 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -21,7 +21,8 @@ // single ID in the issue body suffices to record the // association. But most problems are exhibited in a variety of // ways, leading to multiple field reports of similar but -// distinct stacks. +// distinct stacks. Hence the following way to associate stacks +// with issues. // // 2. Each GitHub issue body may start with a code block of this form: // @@ -302,6 +303,7 @@ func (info Info) String() string { // // stacks is a map of stack text to program metadata to stack+metadata report // count. +// TODO(jba): fix distinctStacks doc? It seems to be the number of telemetry.ProgramReports, not the number of stacks. // distinctStacks is the sum of all counts in stacks. // stackToURL maps the stack text to the oldest telemetry JSON report it was // included in. @@ -339,14 +341,19 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 if len(prog.Stacks) == 0 { continue } + // Ignore @devel versions as they correspond to + // ephemeral (and often numerous) variations of + // the program as we work on a fix to a bug. + if prog.Version == "devel" { + continue + } // Include applicable client names (e.g. vscode, eglot) for gopls. var clientSuffix string if pcfg.IncludeClient { var clients []string for key := range prog.Counters { - client := strings.TrimPrefix(key, "gopls/client:") - if client != key { + if client, ok := strings.CutPrefix(key, "gopls/client:"); ok { clients = append(clients, client) } } @@ -356,13 +363,6 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 } } - // Ignore @devel versions as they correspond to - // ephemeral (and often numerous) variations of - // the program as we work on a fix to a bug. - if prog.Version == "devel" { - continue - } - distinctStacks++ info := Info{ @@ -395,6 +395,7 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { // Query GitHub for all existing GitHub issues with the report label. issues, err := searchIssues(pcfg.SearchLabel) if err != nil { + // TODO(jba): return error instead of dying, or doc. log.Fatalf("GitHub issues label %q search failed: %v", pcfg.SearchLabel, err) } @@ -496,20 +497,6 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { // We log an error if two different issues attempt to claim // the same stack. func claimStacks(issues []*Issue, stacks map[string]map[Info]int64) map[string]*Issue { - // Map each stack ID to its issue. - // - // An issue can claim a stack two ways: - // - // 1. if the issue body contains the ID of the stack. Matching - // is a little loose but base64 will rarely produce words - // that appear in the body by chance. - // - // 2. if the issue body contains a ```#!stacks``` predicate - // that matches the stack. - // - // We report an error if two different issues attempt to claim - // the same stack. - // // This is O(new stacks x existing issues). claimedBy := make(map[string]*Issue) for stack := range stacks { diff --git a/gopls/internal/util/moremaps/maps.go b/gopls/internal/util/moremaps/maps.go index 00dd1e4210b..e25627d67b5 100644 --- a/gopls/internal/util/moremaps/maps.go +++ b/gopls/internal/util/moremaps/maps.go @@ -31,7 +31,7 @@ func KeySlice[M ~map[K]V, K comparable, V any](m M) []K { return r } -// Values returns the values of the map M, like slices.Collect(maps.Values(m)). +// ValueSlice returns the values of the map M, like slices.Collect(maps.Values(m)). func ValueSlice[M ~map[K]V, K comparable, V any](m M) []V { r := make([]V, 0, len(m)) for _, v := range m { @@ -60,7 +60,7 @@ func Sorted[M ~map[K]V, K cmp.Ordered, V any](m M) iter.Seq2[K, V] { } } -// SortedFunc returns an iterator over the entries of m in key order. +// SortedFunc returns an iterator over the entries of m in the key order determined by cmp. func SortedFunc[M ~map[K]V, K comparable, V any](m M, cmp func(x, y K) int) iter.Seq2[K, V] { // TODO(adonovan): use maps.SortedFunc if proposal #68598 is accepted. return func(yield func(K, V) bool) { From 7479e1b98eed4cca4e2ca276beca9d10b7f70cf7 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 10:18:23 -0500 Subject: [PATCH 053/309] internal/telemetry/cmd/stacks: test predicates Factor out parsing and evaluation of #stacks predicates. Add a test. For golang/go#71045. Change-Id: I677e34e555a1f1ebb0722088d55e0c9edd3b3f40 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643776 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 134 ++++++++++-------- .../telemetry/cmd/stacks/stacks_test.go | 50 +++++++ 2 files changed, 125 insertions(+), 59 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 79749eb6f76..d7d79602f16 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -404,81 +404,97 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { for _, issue := range issues { block := findPredicateBlock(issue.Body) if block != "" { - expr, err := parser.ParseExpr(block) + pred, err := parsePredicate(block) if err != nil { log.Printf("invalid predicate in issue #%d: %v\n<<%s>>", issue.Number, err, block) continue } - var validate func(ast.Expr) error - validate = func(e ast.Expr) error { - switch e := e.(type) { - case *ast.UnaryExpr: - if e.Op != token.NOT { - return fmt.Errorf("invalid op: %s", e.Op) - } - return validate(e.X) - - case *ast.BinaryExpr: - if e.Op != token.LAND && e.Op != token.LOR { - return fmt.Errorf("invalid op: %s", e.Op) - } - if err := validate(e.X); err != nil { - return err - } - return validate(e.Y) + issue.predicateText = block + issue.predicate = pred + } + } - case *ast.ParenExpr: - return validate(e.X) + return issues, nil +} - case *ast.BasicLit: - if e.Kind != token.STRING { - return fmt.Errorf("invalid literal (%s)", e.Kind) - } - if _, err := strconv.Unquote(e.Value); err != nil { - return err - } +// parsePredicate parses a predicate expression, returning a function that evaluates +// the predicate on a stack. +// The expression must match this grammar: +// +// expr = "string literal" +// | ( expr ) +// | ! expr +// | expr && expr +// | expr || expr +func parsePredicate(s string) (func(string) bool, error) { + expr, err := parser.ParseExpr(s) + if err != nil { + return nil, fmt.Errorf("parse error: %w", err) + } + var validate func(ast.Expr) error + validate = func(e ast.Expr) error { + switch e := e.(type) { + case *ast.UnaryExpr: + if e.Op != token.NOT { + return fmt.Errorf("invalid op: %s", e.Op) + } + return validate(e.X) - default: - return fmt.Errorf("syntax error (%T)", e) - } - return nil + case *ast.BinaryExpr: + if e.Op != token.LAND && e.Op != token.LOR { + return fmt.Errorf("invalid op: %s", e.Op) } - if err := validate(expr); err != nil { - log.Printf("invalid predicate in issue #%d: %v\n<<%s>>", - issue.Number, err, block) - continue + if err := validate(e.X); err != nil { + return err } - issue.predicateText = block - issue.predicate = func(stack string) bool { - var eval func(ast.Expr) bool - eval = func(e ast.Expr) bool { - switch e := e.(type) { - case *ast.UnaryExpr: - return !eval(e.X) - - case *ast.BinaryExpr: - if e.Op == token.LAND { - return eval(e.X) && eval(e.Y) - } else { - return eval(e.X) || eval(e.Y) - } + return validate(e.Y) - case *ast.ParenExpr: - return eval(e.X) + case *ast.ParenExpr: + return validate(e.X) - case *ast.BasicLit: - substr, _ := strconv.Unquote(e.Value) - return strings.Contains(stack, substr) - } - panic("unreachable") - } - return eval(expr) + case *ast.BasicLit: + if e.Kind != token.STRING { + return fmt.Errorf("invalid literal (%s)", e.Kind) + } + if _, err := strconv.Unquote(e.Value); err != nil { + return err } + + default: + return fmt.Errorf("syntax error (%T)", e) } + return nil + } + if err := validate(expr); err != nil { + return nil, err } - return issues, nil + return func(stack string) bool { + var eval func(ast.Expr) bool + eval = func(e ast.Expr) bool { + switch e := e.(type) { + case *ast.UnaryExpr: + return !eval(e.X) + + case *ast.BinaryExpr: + if e.Op == token.LAND { + return eval(e.X) && eval(e.Y) + } else { + return eval(e.X) || eval(e.Y) + } + + case *ast.ParenExpr: + return eval(e.X) + + case *ast.BasicLit: + substr, _ := strconv.Unquote(e.Value) + return strings.Contains(stack, substr) + } + panic("unreachable") + } + return eval(expr) + }, nil } // claimStack maps each stack ID to its issue (if any). diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index 47353a365cd..b9c1b7c8009 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -75,3 +75,53 @@ func TestReadPCLineTable(t *testing.T) { }) } } + +func TestParsePredicate(t *testing.T) { + for _, tc := range []struct { + expr string + arg string + want bool + }{ + {`"x"`, `"x"`, true}, + {`"x"`, `"axe"`, true}, // literals match by strings.Contains + {`"x"`, `"y"`, false}, + {`!"x"`, "x", false}, + {`!"x"`, "y", true}, + {`"x" && "y"`, "xy", true}, + {`"x" && "y"`, "x", false}, + {`"x" && "y"`, "y", false}, + {`"xz" && "zy"`, "xzy", true}, // matches need not be disjoint + {`"x" || "y"`, "xy", true}, + {`"x" || "y"`, "x", true}, + {`"x" || "y"`, "y", true}, + {`"x" || "y"`, "z", false}, + } { + eval, err := parsePredicate(tc.expr) + if err != nil { + t.Fatal(err) + } + got := eval(tc.arg) + if got != tc.want { + t.Errorf("%s applied to %q: got %t, want %t", tc.expr, tc.arg, got, tc.want) + } + } +} + +func TestParsePredicateError(t *testing.T) { + // Validate that bad predicates return errors. + for _, expr := range []string{ + ``, + `1`, + `foo`, // an identifier, not a literal + `"x" + "y"`, + `"x" &&`, + `~"x"`, + `f(1)`, + } { + if _, err := parsePredicate(expr); err == nil { + t.Errorf("%s: got nil, want error", expr) + } else { + t.Logf("%s: %v", expr, err) + } + } +} From 8bf2b65e42cadcab203b4ed7c7f733dc8c9b5e8d Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 22 Jan 2025 15:48:33 +0000 Subject: [PATCH 054/309] gopls/internal/cache: add more debugging for golang/go#64235 After several hours of attempts, I am yet again unable to reproduce golang/go#64235. Add additional filtering of bug reports to try to narrow down potential root causes. For golang/go#64235 Change-Id: I30abd08f01ebea221a2ff13bceb4823ae3ac470a Reviewed-on: https://go-review.googlesource.com/c/tools/+/643778 Reviewed-by: Alan Donovan Auto-Submit: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/check.go | 56 +++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index 1f35e684838..4faa1a73375 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -492,9 +492,59 @@ func (b *typeCheckBatch) importPackage(ctx context.Context, mp *metadata.Package return bug.Errorf("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904) (using GOPACKAGESDRIVER)", pkg.Name(), item.Name, id, item.Path) } else { - return bug.Errorf("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904)", - pkg.Name(), item.Name, id, item.Path) - + // There's a package in the export data with the same path as the + // imported package, but a different name. + // + // This is observed to occur (very frequently!) in telemetry, yet + // we don't yet have a plausible explanation: any self import or + // circular import should have resulted in a broken import, which + // can't be referenced by export data. (Any type qualified by the + // broken import name will be invalid.) + // + // However, there are some mechanisms that could potentially be + // involved: + // 1. go/types will synthesize package names based on the import + // path for fake packages (but as mentioned above, I don't think + // these can be referenced by export data.) + // 2. Test variants have the same path as non-test variant. Could + // that somehow be involved? (I don't see how, particularly using + // the go list driver, but nevertheless it's worth considering.) + // 3. Command-line arguments and main packages may have special + // handling that we don't fully understand. + // Try to sort these potential causes into unique stacks, as well + // as a few other pathological scenarios. + report := func() error { + return bug.Errorf("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904)", + pkg.Name(), item.Name, id, item.Path) + } + impliedName := "" + if i := strings.LastIndex(item.Path, "/"); i >= 0 { + impliedName = item.Path[i+1:] + } + switch { + case pkg.Name() == "": + return report() + case item.Name == "": + return report() + case metadata.IsCommandLineArguments(mp.ID): + return report() + case mp.ForTest != "": + return report() + case len(mp.CompiledGoFiles) == 0: + return report() + case len(mp.Errors) > 0: + return report() + case impliedName != "" && impliedName != string(mp.Name): + return report() + case len(mp.CompiledGoFiles) != len(mp.GoFiles): + return report() + case mp.Module == nil: + return report() + case mp.Name == "main": + return report() + default: + return report() + } } } } else { From e4adc385aa6dfdb85e7742443adbdb450ec4d058 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 11:16:44 -0500 Subject: [PATCH 055/309] internal/telemetry/cmd/stacks: remove Issue.predicateText It was unused. Change-Id: I2520b27f5662d2da67d922b8c28c8df1c90cf70f Reviewed-on: https://go-review.googlesource.com/c/tools/+/643796 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index d7d79602f16..37fcaa6328e 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -410,7 +410,6 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { issue.Number, err, block) continue } - issue.predicateText = block issue.predicate = pred } } @@ -930,8 +929,7 @@ type Issue struct { Body string // in Markdown format // Set by readIssues. - predicateText string // text of ```#!stacks...``` predicate block - predicate func(string) bool // matching predicate over stack text + predicate func(string) bool // matching predicate over stack text // Set by claimIssues. newStacks []string // new stacks to add to existing issue (comments and IDs) From 30bd6fdf335279c439a39759b676fbbf47c51282 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 11:46:41 -0500 Subject: [PATCH 056/309] internal/telemetry/cmd/stacks: move dry run checks down Check for dry run at the point where we actually request modifications to GitHub. Change-Id: Id92d078d2db82ff6487b42f5b0f10df0e9b90791 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643815 LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt --- gopls/internal/telemetry/cmd/stacks/stacks.go | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 37fcaa6328e..05a96a9aada 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -564,11 +564,6 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL writeStackComment(comment, stack, id, stackToURL[stack], stacks[stack]) } - if *dryRun { - log.Printf("DRY RUN: would add stacks %s to issue #%d", newStackIDs, issue.Number) - continue - } - if err := addIssueComment(issue.Number, comment.String()); err != nil { log.Println(err) continue @@ -870,19 +865,8 @@ func updateIssueBody(number int, body string) error { } url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d", number) - req, err := http.NewRequest("PATCH", url, bytes.NewReader(data)) - if err != nil { - return err - } - req.Header.Add("Authorization", "Bearer "+authToken) - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("issue update failed: %s (body: %s)", resp.Status, body) + if err := requestChange("PATCH", url, data, http.StatusOK); err != nil { + return fmt.Errorf("updating issue: %v", err) } return nil } @@ -900,7 +884,20 @@ func addIssueComment(number int, comment string) error { } url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d/comments", number) - req, err := http.NewRequest("POST", url, bytes.NewReader(data)) + if err := requestChange("POST", url, data, http.StatusCreated); err != nil { + return fmt.Errorf("creating issue comment: %v", err) + } + return nil +} + +// requestChange sends a request to url using method, which may change the state at the server. +// The data is sent as the request body, and wantStatus is the expected response status code. +func requestChange(method, url string, data []byte, wantStatus int) error { + if *dryRun { + log.Printf("DRY RUN: %s %s", method, url) + return nil + } + req, err := http.NewRequest(method, url, bytes.NewReader(data)) if err != nil { return err } @@ -910,9 +907,9 @@ func addIssueComment(number int, comment string) error { return err } defer resp.Body.Close() - if resp.StatusCode != http.StatusCreated { + if resp.StatusCode != wantStatus { body, _ := io.ReadAll(resp.Body) - return fmt.Errorf("failed to create issue comment: %s (body: %s)", resp.Status, body) + return fmt.Errorf("request failed: %s (body: %s)", resp.Status, body) } return nil } From d5cd1f8920cb2c8f8e23fa18cf70d92ce6d8c5e2 Mon Sep 17 00:00:00 2001 From: Ellie Ford Date: Fri, 3 Jan 2025 14:13:26 -0500 Subject: [PATCH 057/309] gopls: add WorkspaceFiles option WorkspaceFiles allows an end-user to specify a set of files which, when modified, will trigger a full reload of any views currently open in a session. This is especially important for users who use custom GOPACKAGESDRIVERS, as previously, you were forced to restart the language server in order to get up-to-date diagnostics in some certain instances. For golang/go#59625 Change-Id: Iba7a6137cb0b88a59318217a9a28d079100192a4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/640076 Auto-Submit: Robert Findley Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/settings.md | 10 ++++++++ gopls/internal/cache/session.go | 40 +++++++++++++++++------------ gopls/internal/cache/snapshot.go | 33 ++++++++++++++++-------- gopls/internal/cache/workspace.go | 16 ++++++++++++ gopls/internal/doc/api.json | 13 ++++++++++ gopls/internal/settings/default.go | 1 + gopls/internal/settings/settings.go | 8 ++++++ 7 files changed, 93 insertions(+), 28 deletions(-) diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index 3d170b00dc3..dc601ea8b17 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -143,6 +143,16 @@ This setting is only supported when gopls is built with Go 1.16 or later. Default: `["ignore"]`. + +### `workspaceFiles []string` + +workspaceFiles configures the set of globs that match files defining the logical build of the current workspace. +Any on-disk changes to any files matching a glob specified here will trigger a reload of the workspace. + +This setting need only be customized in environments with a custom GOPACKAGESDRIVER. + +Default: `[]`. + ## Formatting diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index 99f7ecae957..5d85e2b606f 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -775,6 +775,25 @@ func (s *Session) DidModifyFiles(ctx context.Context, modifications []file.Modif // changed on disk. checkViews := false + // Hack: collect folders from existing views. + // TODO(golang/go#57979): we really should track folders independent of + // Views, but since we always have a default View for each folder, this + // works for now. + var folders []*Folder // preserve folder order + workspaceFileGlobsSet := make(map[string]bool) + seen := make(map[*Folder]unit) + for _, v := range s.views { + if _, ok := seen[v.folder]; ok { + continue + } + seen[v.folder] = unit{} + folders = append(folders, v.folder) + for _, glob := range v.folder.Options.WorkspaceFiles { + workspaceFileGlobsSet[glob] = true + } + } + workspaceFileGlobs := slices.Collect(maps.Keys(workspaceFileGlobsSet)) + changed := make(map[protocol.DocumentURI]file.Handle) for _, c := range modifications { fh := mustReadFile(ctx, s, c.URI) @@ -790,7 +809,7 @@ func (s *Session) DidModifyFiles(ctx context.Context, modifications []file.Modif // TODO(rfindley): go.work files need not be named "go.work" -- we need to // check each view's source to handle the case of an explicit GOWORK value. // Write a test that fails, and fix this. - if (isGoWork(c.URI) || isGoMod(c.URI)) && (c.Action == file.Save || c.OnDisk) { + if (isGoWork(c.URI) || isGoMod(c.URI) || isWorkspaceFile(c.URI, workspaceFileGlobs)) && (c.Action == file.Save || c.OnDisk) { checkViews = true } @@ -817,20 +836,6 @@ func (s *Session) DidModifyFiles(ctx context.Context, modifications []file.Modif } if checkViews { - // Hack: collect folders from existing views. - // TODO(golang/go#57979): we really should track folders independent of - // Views, but since we always have a default View for each folder, this - // works for now. - var folders []*Folder // preserve folder order - seen := make(map[*Folder]unit) - for _, v := range s.views { - if _, ok := seen[v.folder]; ok { - continue - } - seen[v.folder] = unit{} - folders = append(folders, v.folder) - } - var openFiles []protocol.DocumentURI for _, o := range s.Overlays() { openFiles = append(openFiles, o.URI()) @@ -1085,11 +1090,12 @@ func (b brokenFile) Content() ([]byte, error) { return nil, b.err } // // This set includes // 1. all go.mod and go.work files in the workspace; and -// 2. for each Snapshot, its modules (or directory for ad-hoc views). In +// 2. all files defined by the WorkspaceFiles option in BuildOptions (to support custom GOPACKAGESDRIVERS); and +// 3. for each Snapshot, its modules (or directory for ad-hoc views). In // module mode, this is the set of active modules (and for VS Code, all // workspace directories within them, due to golang/go#42348). // -// The watch for workspace go.work and go.mod files in (1) is sufficient to +// The watch for workspace files in (1) is sufficient to // capture changes to the repo structure that may affect the set of views. // Whenever this set changes, we reload the workspace and invalidate memoized // files. diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index c17dade773e..4a2ae2431d7 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -799,6 +799,10 @@ func (s *Snapshot) fileWatchingGlobPatterns() map[protocol.RelativePattern]unit patterns[workPattern] = unit{} } + for _, glob := range s.Options().WorkspaceFiles { + patterns[protocol.RelativePattern{Pattern: glob}] = unit{} + } + extensions := "go,mod,sum,work" for _, ext := range s.Options().TemplateExtensions { extensions += "," + ext @@ -1540,24 +1544,31 @@ func (s *Snapshot) clone(ctx, bgCtx context.Context, changed StateChange, done f } reinit := false - - // Changes to vendor tree may require reinitialization, - // either because of an initialization error - // (e.g. "inconsistent vendoring detected"), or because - // one or more modules may have moved into or out of the - // vendor tree after 'go mod vendor' or 'rm -fr vendor/'. - // - // In this case, we consider the actual modification to see if was a creation - // or deletion. - // - // TODO(rfindley): revisit the location of this check. for _, mod := range changed.Modifications { + // Changes to vendor tree may require reinitialization, + // either because of an initialization error + // (e.g. "inconsistent vendoring detected"), or because + // one or more modules may have moved into or out of the + // vendor tree after 'go mod vendor' or 'rm -fr vendor/'. + // + // In this case, we consider the actual modification to see if was a creation + // or deletion. + // + // TODO(rfindley): revisit the location of this check. if inVendor(mod.URI) && (mod.Action == file.Create || mod.Action == file.Delete) || strings.HasSuffix(string(mod.URI), "/vendor/modules.txt") { reinit = true break } + + // Changes to workspace files, as a rule of thumb, should require reinitialization. Since their behavior + // is generally user-defined, we want to do something sensible by re-triggering a query to the active GOPACKAGESDRIVER, + // and reloading the state of the workspace. + if isWorkspaceFile(mod.URI, s.view.folder.Options.WorkspaceFiles) && (mod.Action == file.Save || mod.OnDisk) { + reinit = true + break + } } // Collect observed file handles for changed URIs from the old snapshot, if diff --git a/gopls/internal/cache/workspace.go b/gopls/internal/cache/workspace.go index 07134b3da00..0621d17a537 100644 --- a/gopls/internal/cache/workspace.go +++ b/gopls/internal/cache/workspace.go @@ -13,6 +13,7 @@ import ( "golang.org/x/mod/modfile" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/test/integration/fake/glob" ) // isGoWork reports if uri is a go.work file. @@ -65,6 +66,21 @@ func isGoMod(uri protocol.DocumentURI) bool { return filepath.Base(uri.Path()) == "go.mod" } +// isWorkspaceFile reports if uri matches a set of globs defined in workspaceFiles +func isWorkspaceFile(uri protocol.DocumentURI, workspaceFiles []string) bool { + for _, workspaceFile := range workspaceFiles { + g, err := glob.Parse(workspaceFile) + if err != nil { + continue + } + + if g.Match(uri.Path()) { + return true + } + } + return false +} + // goModModules returns the URIs of "workspace" go.mod files defined by a // go.mod file. This set is defined to be the given go.mod file itself, as well // as the modfiles of any locally replaced modules in the go.mod file. diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 4a8e10f6132..a489f983aad 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -99,6 +99,19 @@ "Hierarchy": "build", "DeprecationMessage": "" }, + { + "Name": "workspaceFiles", + "Type": "[]string", + "Doc": "workspaceFiles configures the set of globs that match files defining the logical build of the current workspace.\nAny on-disk changes to any files matching a glob specified here will trigger a reload of the workspace.\n\nThis setting need only be customized in environments with a custom GOPACKAGESDRIVER.\n", + "EnumKeys": { + "ValueType": "", + "Keys": null + }, + "EnumValues": null, + "Default": "[]", + "Status": "", + "Hierarchy": "build" + }, { "Name": "hoverKind", "Type": "enum", diff --git a/gopls/internal/settings/default.go b/gopls/internal/settings/default.go index 50f6f2ba3ea..cd275e37ffb 100644 --- a/gopls/internal/settings/default.go +++ b/gopls/internal/settings/default.go @@ -87,6 +87,7 @@ func DefaultOptions(overrides ...func(*Options)) *Options { DirectoryFilters: []string{"-**/node_modules"}, TemplateExtensions: []string{}, StandaloneTags: []string{"ignore"}, + WorkspaceFiles: []string{}, }, UIOptions: UIOptions{ DiagnosticOptions: DiagnosticOptions{ diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 13aaa61bdd9..3252858402d 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -141,6 +141,12 @@ type BuildOptions struct { // // This setting is only supported when gopls is built with Go 1.16 or later. StandaloneTags []string + + // WorkspaceFiles configures the set of globs that match files defining the logical build of the current workspace. + // Any on-disk changes to any files matching a glob specified here will trigger a reload of the workspace. + // + // This setting need only be customized in environments with a custom GOPACKAGESDRIVER. + WorkspaceFiles []string } // Note: UIOptions must be comparable with reflect.DeepEqual. @@ -970,6 +976,8 @@ func (o *Options) setOne(name string, value any) error { } o.DirectoryFilters = filters + case "workspaceFiles": + return setStringSlice(&o.WorkspaceFiles, value) case "completionDocumentation": return setBool(&o.CompletionDocumentation, value) case "usePlaceholders": From 684910f578e9c81b10bd998f010f17023c8503d7 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 12:08:34 -0500 Subject: [PATCH 058/309] internal/telemetry/cmd/stacks: fix distinctStacks Compute the total stack counts, as the doc says. Change-Id: Ia7ab82a3ee84ab1b91971b7ef20855e48b0c033a Reviewed-on: https://go-review.googlesource.com/c/tools/+/643779 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 05a96a9aada..f3dd8ec2989 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -303,8 +303,7 @@ func (info Info) String() string { // // stacks is a map of stack text to program metadata to stack+metadata report // count. -// TODO(jba): fix distinctStacks doc? It seems to be the number of telemetry.ProgramReports, not the number of stacks. -// distinctStacks is the sum of all counts in stacks. +// distinctStacks is the number of distinct stacks across all reports. // stackToURL maps the stack text to the oldest telemetry JSON report it was // included in. func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64, distinctStacks int, stackToURL map[string]string, err error) { @@ -363,8 +362,6 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 } } - distinctStacks++ - info := Info{ Program: prog.Program, ProgramVersion: prog.Version, @@ -382,6 +379,7 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 counts[info] += count stackToURL[stack] = url } + distinctStacks += len(prog.Stacks) } } } From fcc9d81f7cff5c606414ca0cf218b7d0f7db991f Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 22 Jan 2025 14:19:05 -0500 Subject: [PATCH 059/309] internal/telemetry/cmd/stacks: anchored literals Literals in predicates are re-interpreted as matching at word boundaries. A literal like "fu+12" will no longer match "fu+123" or "snafu+12". For golang/go#71045. Change-Id: Id5b6c8ad536dadebdb9593cbfa13ff8dd81b6645 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643835 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/doc/api.json | 3 ++- gopls/internal/telemetry/cmd/stacks/stacks.go | 26 ++++++++++++++++--- .../telemetry/cmd/stacks/stacks_test.go | 16 +++++++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index a489f983aad..0ae6103c8db 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -110,7 +110,8 @@ "EnumValues": null, "Default": "[]", "Status": "", - "Hierarchy": "build" + "Hierarchy": "build", + "DeprecationMessage": "" }, { "Name": "hoverKind", diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index f3dd8ec2989..3780cf3145c 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -41,8 +41,10 @@ // > | expr && expr // > | expr || expr // -// Each string literal implies a substring match on the stack; +// Each string literal must match complete words on the stack; // the other productions are boolean operations. +// As an example of literal matching, "fu+12" matches "x:fu+12 " +// but not "fu:123" or "snafu+12". // // The stacks command gathers all such predicates out of the // labelled issues and evaluates each one against each new stack. @@ -76,6 +78,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "sort" "strconv" @@ -424,11 +427,21 @@ func readIssues(pcfg ProgramConfig) ([]*Issue, error) { // | ! expr // | expr && expr // | expr || expr +// +// The value of a string literal is whether it is a substring of the stack, respecting word boundaries. +// That is, a literal L behaves like the regular expression \bL'\b, where L' is L with +// regexp metacharacters quoted. func parsePredicate(s string) (func(string) bool, error) { expr, err := parser.ParseExpr(s) if err != nil { return nil, fmt.Errorf("parse error: %w", err) } + + // Cache compiled regexps since we need them more than once. + literalRegexps := make(map[*ast.BasicLit]*regexp.Regexp) + + // Check for errors in the predicate so we can report them now, + // ensuring that evaluation is error-free. var validate func(ast.Expr) error validate = func(e ast.Expr) error { switch e := e.(type) { @@ -454,9 +467,15 @@ func parsePredicate(s string) (func(string) bool, error) { if e.Kind != token.STRING { return fmt.Errorf("invalid literal (%s)", e.Kind) } - if _, err := strconv.Unquote(e.Value); err != nil { + lit, err := strconv.Unquote(e.Value) + if err != nil { return err } + // The literal should match complete words. It may match multiple words, + // if it contains non-word runes like whitespace; but it must match word + // boundaries at each end. + // The constructed regular expression is always valid. + literalRegexps[e] = regexp.MustCompile(`\b` + regexp.QuoteMeta(lit) + `\b`) default: return fmt.Errorf("syntax error (%T)", e) @@ -485,8 +504,7 @@ func parsePredicate(s string) (func(string) bool, error) { return eval(e.X) case *ast.BasicLit: - substr, _ := strconv.Unquote(e.Value) - return strings.Contains(stack, substr) + return literalRegexps[e].MatchString(stack) } panic("unreachable") } diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index b9c1b7c8009..714bb9499fe 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -83,15 +83,23 @@ func TestParsePredicate(t *testing.T) { want bool }{ {`"x"`, `"x"`, true}, - {`"x"`, `"axe"`, true}, // literals match by strings.Contains + {`"x"`, `"axe"`, false}, // literals match whole words + {`"x"`, "val:x+5", true}, + {`"fu+12"`, "x:fu+12,", true}, + {`"fu+12"`, "snafu+12,", false}, + {`"fu+12"`, "x:fu+123,", false}, + {`"a.*b"`, "a.*b", true}, // regexp metachars are escaped + {`"a.*b"`, "axxb", false}, // ditto {`"x"`, `"y"`, false}, {`!"x"`, "x", false}, {`!"x"`, "y", true}, - {`"x" && "y"`, "xy", true}, + {`"x" && "y"`, "xy", false}, + {`"x" && "y"`, "x y", true}, {`"x" && "y"`, "x", false}, {`"x" && "y"`, "y", false}, - {`"xz" && "zy"`, "xzy", true}, // matches need not be disjoint - {`"x" || "y"`, "xy", true}, + {`"xz" && "zy"`, "xzy", false}, + {`"xz" && "zy"`, "zy,xz", true}, + {`"x" || "y"`, "x\ny", true}, {`"x" || "y"`, "x", true}, {`"x" || "y"`, "y", true}, {`"x" || "y"`, "z", false}, From 7a015ab4eb02bf00dfb17a4b11dce94640936383 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 23 Jan 2025 14:33:28 +0000 Subject: [PATCH 060/309] internal/gocommand: send SIGQUIT to hanging go commands on posix Existing debug information has not proven useful for understanding hanging go commands. On posix systems we can send SIGQUIT, and peek at stderr, to try to see what the go command is doing. Tested locally by setting the timeout to 10ms. Updates golang/go#54461 Change-Id: I1bc38d6da82b0dffb55081364b0af8f20a3afcfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/643915 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- internal/gocommand/invoke.go | 38 ++++++++++++++++++++-------- internal/gocommand/invoke_notunix.go | 13 ++++++++++ internal/gocommand/invoke_unix.go | 13 ++++++++++ 3 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 internal/gocommand/invoke_notunix.go create mode 100644 internal/gocommand/invoke_unix.go diff --git a/internal/gocommand/invoke.go b/internal/gocommand/invoke.go index e333efc87f9..fcde93c665d 100644 --- a/internal/gocommand/invoke.go +++ b/internal/gocommand/invoke.go @@ -179,7 +179,7 @@ type Invocation struct { CleanEnv bool Env []string WorkingDir string - Logf func(format string, args ...interface{}) + Logf func(format string, args ...any) } // Postcondition: both error results have same nilness. @@ -388,7 +388,9 @@ func runCmdContext(ctx context.Context, cmd *exec.Cmd) (err error) { case err := <-resChan: return err case <-timer.C: - HandleHangingGoCommand(startTime, cmd) + // HandleHangingGoCommand terminates this process. + // Pass off resChan in case we can collect the command error. + handleHangingGoCommand(startTime, cmd, resChan) case <-ctx.Done(): } } else { @@ -413,8 +415,6 @@ func runCmdContext(ctx context.Context, cmd *exec.Cmd) (err error) { } // Didn't shut down in response to interrupt. Kill it hard. - // TODO(rfindley): per advice from bcmills@, it may be better to send SIGQUIT - // on certain platforms, such as unix. if err := cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) && debug { log.Printf("error killing the Go command: %v", err) } @@ -422,15 +422,17 @@ func runCmdContext(ctx context.Context, cmd *exec.Cmd) (err error) { return <-resChan } -func HandleHangingGoCommand(start time.Time, cmd *exec.Cmd) { +// handleHangingGoCommand outputs debugging information to help diagnose the +// cause of a hanging Go command, and then exits with log.Fatalf. +func handleHangingGoCommand(start time.Time, cmd *exec.Cmd, resChan chan error) { switch runtime.GOOS { case "linux", "darwin", "freebsd", "netbsd": fmt.Fprintln(os.Stderr, `DETECTED A HANGING GO COMMAND -The gopls test runner has detected a hanging go command. In order to debug -this, the output of ps and lsof/fstat is printed below. + The gopls test runner has detected a hanging go command. In order to debug + this, the output of ps and lsof/fstat is printed below. -See golang/go#54461 for more details.`) + See golang/go#54461 for more details.`) fmt.Fprintln(os.Stderr, "\nps axo ppid,pid,command:") fmt.Fprintln(os.Stderr, "-------------------------") @@ -438,7 +440,7 @@ See golang/go#54461 for more details.`) psCmd.Stdout = os.Stderr psCmd.Stderr = os.Stderr if err := psCmd.Run(); err != nil { - panic(fmt.Sprintf("running ps: %v", err)) + log.Printf("Handling hanging Go command: running ps: %v", err) } listFiles := "lsof" @@ -452,10 +454,24 @@ See golang/go#54461 for more details.`) listFilesCmd.Stdout = os.Stderr listFilesCmd.Stderr = os.Stderr if err := listFilesCmd.Run(); err != nil { - panic(fmt.Sprintf("running %s: %v", listFiles, err)) + log.Printf("Handling hanging Go command: running %s: %v", listFiles, err) + } + // Try to extract information about the slow go process by issuing a SIGQUIT. + if err := cmd.Process.Signal(sigStuckProcess); err == nil { + select { + case err := <-resChan: + stderr := "not a bytes.Buffer" + if buf, _ := cmd.Stderr.(*bytes.Buffer); buf != nil { + stderr = buf.String() + } + log.Printf("Quit hanging go command:\n\terr:%v\n\tstderr:\n%v\n\n", err, stderr) + case <-time.After(5 * time.Second): + } + } else { + log.Printf("Sending signal %d to hanging go command: %v", sigStuckProcess, err) } } - panic(fmt.Sprintf("detected hanging go command (golang/go#54461); waited %s\n\tcommand:%s\n\tpid:%d", time.Since(start), cmd, cmd.Process.Pid)) + log.Fatalf("detected hanging go command (golang/go#54461); waited %s\n\tcommand:%s\n\tpid:%d", time.Since(start), cmd, cmd.Process.Pid) } func cmdDebugStr(cmd *exec.Cmd) string { diff --git a/internal/gocommand/invoke_notunix.go b/internal/gocommand/invoke_notunix.go new file mode 100644 index 00000000000..469c648e4d8 --- /dev/null +++ b/internal/gocommand/invoke_notunix.go @@ -0,0 +1,13 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !unix + +package gocommand + +import "os" + +// sigStuckProcess is the signal to send to kill a hanging subprocess. +// On Unix we send SIGQUIT, but on non-Unix we only have os.Kill. +var sigStuckProcess = os.Kill diff --git a/internal/gocommand/invoke_unix.go b/internal/gocommand/invoke_unix.go new file mode 100644 index 00000000000..169d37c8e93 --- /dev/null +++ b/internal/gocommand/invoke_unix.go @@ -0,0 +1,13 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build unix + +package gocommand + +import "syscall" + +// Sigstuckprocess is the signal to send to kill a hanging subprocess. +// Send SIGQUIT to get a stack trace. +var sigStuckProcess = syscall.SIGQUIT From 71c7ff32afaa8bcf083c45fae06e59e65d8758dd Mon Sep 17 00:00:00 2001 From: xzb <2598514867@qq.com> Date: Wed, 22 Jan 2025 04:56:11 +0000 Subject: [PATCH 061/309] gopls: report SemanticHighlight for format string directives This CL uses "format" as modifier type for format string directives. Fixes golang/go#71295 Change-Id: I0a6538152902db11a3f0612cc4e2964f47a2b7f3 GitHub-Last-Rev: bc9a21f219f5c9f86ce6b49a3e5cc8539c501af2 GitHub-Pull-Request: golang/tools#557 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643138 Reviewed-by: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/doc/release/v0.18.0.md | 15 +++- gopls/doc/semantictokens.md | 11 +-- gopls/internal/analysis/modernize/omitzero.go | 6 +- gopls/internal/golang/highlight.go | 11 ++- gopls/internal/golang/semtok.go | 70 ++++++++++++++++--- gopls/internal/protocol/semtok/semtok.go | 2 + .../test/marker/testdata/token/format.txt | 26 +++++++ internal/astutil/util.go | 14 ++++ 8 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/token/format.txt diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 39590a7333e..d22221d1b7e 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -118,7 +118,9 @@ The Definition query now supports additional locations: When invoked on a return statement, hover reports the types of the function's result variables. -## Improvements to "DocumentHighlight" +## UX improvements to format strings + +### "DocumentHighlight" When your cursor is inside a printf-like function, gopls now highlights the relationship between formatting verbs and arguments as visual cues to differentiate how operands are used in the format string. @@ -129,3 +131,14 @@ fmt.Printf("Hello %s, you scored %d", name, score) If the cursor is either on `%s` or `name`, gopls will highlight `%s` as a write operation, and `name` as a read operation. + +### "SemanticHighlight" + +Similar to the improvements to DocumentHighlight, gopls also reports formatting verbs +as "format" modifier for token type "string" to better distinguish them with other parts of the format string. + +```go +fmt.Printf("Hello %s, you scored %d", name, score) +``` + +`%s` and `%d` will have token type "string" and modifier "format". diff --git a/gopls/doc/semantictokens.md b/gopls/doc/semantictokens.md index f17ea7f06d8..9856d3720a5 100644 --- a/gopls/doc/semantictokens.md +++ b/gopls/doc/semantictokens.md @@ -54,14 +54,15 @@ and change over time. (Nonetheless, a minimal implementation would not return `k `number`, `comment`, or `string`.) The maximal position isn't particularly well-specified either. To chose one example, a -format string might have formatting codes (`%[4]-3.6f`), escape sequences (`\U00010604`), and regular +format string might have formatting codes (`%-[4].6f`), escape sequences (`\U00010604`), and regular characters. Should these all be distinguished? One could even imagine distinguishing different runes by their Unicode language assignment, or some other Unicode property, such as -being [confusable](http://www.unicode.org/Public/security/10.0.0/confusables.txt). +being [confusable](http://www.unicode.org/Public/security/10.0.0/confusables.txt). While gopls does not fully adhere to such distinctions, +it does recognizes formatting directives within strings, decorating them with "format" modifiers, +providing more precise semantic highlighting in format strings. -Gopls does not come close to either of these principles. Semantic tokens are returned for -identifiers, keywords, operators, comments, and literals. (Semantic tokens do not -cover the file. They are not returned for +Semantic tokens are returned for identifiers, keywords, operators, comments, and literals. +(Semantic tokens do not cover the file. They are not returned for white space or punctuation, and there is no semantic token for labels.) The following describes more precisely what gopls does, with a few notes on possible alternative choices. diff --git a/gopls/internal/analysis/modernize/omitzero.go b/gopls/internal/analysis/modernize/omitzero.go index 706cb4ea5ef..02b7e3fbcd0 100644 --- a/gopls/internal/analysis/modernize/omitzero.go +++ b/gopls/internal/analysis/modernize/omitzero.go @@ -35,11 +35,7 @@ func checkOmitEmptyField(pass *analysis.Pass, info *types.Info, curField *ast.Fi // No omitempty in json tag return } - omitEmptyPos, err := astutil.PosInStringLiteral(curField.Tag, match[2]) - if err != nil { - return - } - omitEmptyEnd, err := astutil.PosInStringLiteral(curField.Tag, match[3]) + omitEmptyPos, omitEmptyEnd, err := astutil.RangeInStringLiteral(curField.Tag, match[2], match[3]) if err != nil { return } diff --git a/gopls/internal/golang/highlight.go b/gopls/internal/golang/highlight.go index a4f81e35153..ee82b622a71 100644 --- a/gopls/internal/golang/highlight.go +++ b/gopls/internal/golang/highlight.go @@ -17,6 +17,7 @@ import ( "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + goplsastutil "golang.org/x/tools/gopls/internal/util/astutil" internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/fmtstr" @@ -210,11 +211,7 @@ func highlightPrintf(call *ast.CallExpr, idx int, cursorPos token.Pos, lit *ast. // highlightPair highlights the operation and its potential argument pair if the cursor is within either range. highlightPair := func(rang fmtstr.Range, argIndex int) { - rangeStart, err := internalastutil.PosInStringLiteral(lit, rang.Start) - if err != nil { - return - } - rangeEnd, err := internalastutil.PosInStringLiteral(lit, rang.End) + rangeStart, rangeEnd, err := internalastutil.RangeInStringLiteral(lit, rang.Start, rang.End) if err != nil { return } @@ -226,9 +223,9 @@ func highlightPrintf(call *ast.CallExpr, idx int, cursorPos token.Pos, lit *ast. } // cursorPos can't equal to end position, otherwise the two - // neighborhood such as (%[2]*d) are both highlighted if cursor in "*" (ending of [2]*). + // neighborhood such as (%[2]*d) are both highlighted if cursor in "d" (ending of [2]*). if rangeStart <= cursorPos && cursorPos < rangeEnd || - arg != nil && arg.Pos() <= cursorPos && cursorPos < arg.End() { + arg != nil && goplsastutil.NodeContains(arg, cursorPos) { highlightRange(result, rangeStart, rangeEnd, protocol.Write) if arg != nil { succeededArg = argIndex diff --git a/gopls/internal/golang/semtok.go b/gopls/internal/golang/semtok.go index 84fad43a47f..cb3f2cfd478 100644 --- a/gopls/internal/golang/semtok.go +++ b/gopls/internal/golang/semtok.go @@ -17,6 +17,7 @@ import ( "log" "path/filepath" "regexp" + "strconv" "strings" "time" @@ -28,7 +29,9 @@ import ( "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/event" + "golang.org/x/tools/internal/fmtstr" ) // semDebug enables comprehensive logging of decisions @@ -323,16 +326,17 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) { case *ast.AssignStmt: tv.token(n.TokPos, len(n.Tok.String()), semtok.TokOperator) case *ast.BasicLit: - if strings.Contains(n.Value, "\n") { - // has to be a string. - tv.multiline(n.Pos(), n.End(), semtok.TokString) - break - } - what := semtok.TokNumber if n.Kind == token.STRING { - what = semtok.TokString + if strings.Contains(n.Value, "\n") { + // has to be a string. + tv.multiline(n.Pos(), n.End(), semtok.TokString) + } else if !tv.formatString(n) { + // not a format string, color the whole as a TokString. + tv.token(n.Pos(), len(n.Value), semtok.TokString) + } + } else { + tv.token(n.Pos(), len(n.Value), semtok.TokNumber) } - tv.token(n.Pos(), len(n.Value), what) case *ast.BinaryExpr: tv.token(n.OpPos, len(n.Op.String()), semtok.TokOperator) case *ast.BlockStmt: @@ -461,6 +465,56 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) { return true } +// formatString tries to report directives and string literals +// inside a (possible) printf-like call, it returns false and does nothing +// if the string is not a format string. +func (tv *tokenVisitor) formatString(lit *ast.BasicLit) bool { + if len(tv.stack) <= 1 { + return false + } + call, ok := tv.stack[len(tv.stack)-2].(*ast.CallExpr) + if !ok { + return false + } + lastNonVariadic, idx := formatStringAndIndex(tv.info, call) + if idx == -1 || lit != lastNonVariadic { + return false + } + format, err := strconv.Unquote(lit.Value) + if err != nil { + return false + } + if !strings.Contains(format, "%") { + return false + } + operations, err := fmtstr.Parse(format, idx) + if err != nil { + return false + } + + // It's a format string, compute interleaved sub range of directives and literals. + // pos tracks literal substring position within the overall BasicLit. + pos := lit.ValuePos + for _, op := range operations { + // Skip "%%". + if op.Verb.Verb == '%' { + continue + } + rangeStart, rangeEnd, err := astutil.RangeInStringLiteral(lit, op.Range.Start, op.Range.End) + if err != nil { + return false + } + // Report literal substring. + tv.token(pos, int(rangeStart-pos), semtok.TokString) + // Report formatting directive. + tv.token(rangeStart, int(rangeEnd-rangeStart), semtok.TokString, semtok.ModFormat) + pos = rangeEnd + } + // Report remaining literal substring. + tv.token(pos, int(lit.End()-pos), semtok.TokString) + return true +} + func (tv *tokenVisitor) appendObjectModifiers(mods []semtok.Modifier, obj types.Object) (semtok.Type, []semtok.Modifier) { if obj.Pkg() == nil { mods = append(mods, semtok.ModDefaultLibrary) diff --git a/gopls/internal/protocol/semtok/semtok.go b/gopls/internal/protocol/semtok/semtok.go index a40f2b5482f..6b05b8bb5e2 100644 --- a/gopls/internal/protocol/semtok/semtok.go +++ b/gopls/internal/protocol/semtok/semtok.go @@ -102,6 +102,7 @@ const ( ModArray Modifier = "array" ModBool Modifier = "bool" ModChan Modifier = "chan" + ModFormat Modifier = "format" // for format string directives such as "%s" ModInterface Modifier = "interface" ModMap Modifier = "map" ModNumber Modifier = "number" @@ -123,6 +124,7 @@ var TokenModifiers = []Modifier{ ModArray, ModBool, ModChan, + ModFormat, ModInterface, ModMap, ModNumber, diff --git a/gopls/internal/test/marker/testdata/token/format.txt b/gopls/internal/test/marker/testdata/token/format.txt new file mode 100644 index 00000000000..c577cc666af --- /dev/null +++ b/gopls/internal/test/marker/testdata/token/format.txt @@ -0,0 +1,26 @@ +This test checks semanticTokens for format string placeholders. + +-- settings.json -- +{ + "semanticTokens": true +} + +-- flags -- +-ignore_extra_diags + +-- format.go -- +package format + +import "fmt" + +func PrintfTests() { + var i int + var x float64 + fmt.Printf("%b %d %f", 3, i, x) //@ token("%b", "string", "format"), token("%d", "string", "format"),token("%f", "string", "format"), + fmt.Printf("lit1%blit2%dlit3%flit4", 3, i, x) //@ token("%b", "string", "format"), token("%d", "string", "format"),token("%f", "string", "format"),token("lit1", "string", ""),token("lit2", "string", ""),token("lit3", "string", ""), + fmt.Printf("%% %d lit2", 3, i, x) //@ token("%d", "string", "format"),token("%%", "string", ""),token("lit2", "string", ""), + fmt.Printf("Hello %% \n %s, you \t%% \n have %d new m%%essages!", "Alice", 5) //@ token("%s", "string", "format"),token("%d", "string", "format") + fmt.Printf("%d \nss \x25[2]d", 234, 123) //@ token("%d", "string", "format"),token("\\x25[2]d", "string", "format") + fmt.Printf("start%[2]*.[1]*[3]dmiddle%send", 4, 5, 6) //@ token("%[2]*.[1]*[3]d", "string", "format"),token("start", "string", ""),token("%s", "string", "format"),token("middle", "string", ""),token("end", "string", "") +} + diff --git a/internal/astutil/util.go b/internal/astutil/util.go index 3b3c6259568..849d45d8539 100644 --- a/internal/astutil/util.go +++ b/internal/astutil/util.go @@ -12,6 +12,20 @@ import ( "unicode/utf8" ) +// RangeInStringLiteral calculates the positional range within a string literal +// corresponding to the specified start and end byte offsets within the logical string. +func RangeInStringLiteral(lit *ast.BasicLit, start, end int) (token.Pos, token.Pos, error) { + startPos, err := PosInStringLiteral(lit, start) + if err != nil { + return 0, 0, fmt.Errorf("start: %v", err) + } + endPos, err := PosInStringLiteral(lit, end) + if err != nil { + return 0, 0, fmt.Errorf("end: %v", err) + } + return startPos, endPos, nil +} + // PosInStringLiteral returns the position within a string literal // corresponding to the specified byte offset within the logical // string that it denotes. From 45227b6df52050831bc7a092b0b7ad4f9e7e254e Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Sun, 19 Jan 2025 15:40:49 -0500 Subject: [PATCH 062/309] internal/modindex: add LookupAll(pkg, names) The new function LookupAll returns a map from import paths to []Candidate, where the []Candidate contains all the names. This improves the current situation where the caller of Lookup() has to figure out if all the missing names have been found. Also, the parallelism while building a new index is now limited to half the number of cpus. Change-Id: I7c5543ed8675d49f630775adea204090c201930c Reviewed-on: https://go-review.googlesource.com/c/tools/+/643495 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- internal/modindex/lookup.go | 30 +++++++++++++++ internal/modindex/lookup_test.go | 65 +++++++++++++++++++++++++++++++- internal/modindex/symbols.go | 7 ++-- 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/internal/modindex/lookup.go b/internal/modindex/lookup.go index 012fdd7134c..5499c5c67f3 100644 --- a/internal/modindex/lookup.go +++ b/internal/modindex/lookup.go @@ -35,6 +35,36 @@ const ( Func ) +// LookupAll only returns those Candidates whose import path +// finds all the nms. +func (ix *Index) LookupAll(pkg string, names ...string) map[string][]Candidate { + // this can be made faster when benchmarks show that it needs to be + names = uniquify(names) + byImpPath := make(map[string][]Candidate) + for _, nm := range names { + cands := ix.Lookup(pkg, nm, false) + for _, c := range cands { + byImpPath[c.ImportPath] = append(byImpPath[c.ImportPath], c) + } + } + for k, v := range byImpPath { + if len(v) != len(names) { + delete(byImpPath, k) + } + } + return byImpPath +} + +// remove duplicates +func uniquify(in []string) []string { + if len(in) == 0 { + return in + } + in = slices.Clone(in) + slices.Sort(in) + return slices.Compact(in) +} + // Lookup finds all the symbols in the index with the given PkgName and name. // If prefix is true, it finds all of these with name as a prefix. func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate { diff --git a/internal/modindex/lookup_test.go b/internal/modindex/lookup_test.go index 4c5ae35695d..191395cffc9 100644 --- a/internal/modindex/lookup_test.go +++ b/internal/modindex/lookup_test.go @@ -68,7 +68,6 @@ func okresult(r result, p Candidate) bool { } func TestLookup(t *testing.T) { - log.SetFlags(log.Lshortfile) dir := testModCache(t) wrtData(t, dir, thedata) if _, err := indexModCache(dir, true); err != nil { @@ -133,3 +132,67 @@ func wrtData(t *testing.T, dir string, data tdata) { fd.WriteString(item.code + "\n") } } + +func TestLookupAll(t *testing.T) { + log.SetFlags(log.Lshortfile) + dir := testModCache(t) + wrtModule := func(mod string, nms ...string) { + dname := filepath.Join(dir, mod) + if err := os.MkdirAll(dname, 0755); err != nil { + t.Fatal(err) + } + fname := filepath.Join(dname, "foo.go") + fd, err := os.Create(fname) + if err != nil { + t.Fatal(err) + } + defer fd.Close() + if _, err := fd.WriteString(fmt.Sprintf("package foo\n")); err != nil { + t.Fatal(err) + } + for _, nm := range nms { + fd.WriteString(fmt.Sprintf("func %s() {}\n", nm)) + } + } + wrtModule("a.com/go/x4@v1.1.1", "A", "B", "C", "D") + wrtModule("b.com/go/x3@v1.2.1", "A", "B", "C") + wrtModule("c.com/go/x5@v1.3.1", "A", "B", "C", "D", "E") + + if _, err := indexModCache(dir, true); err != nil { + t.Fatal(err) + } + ix, err := ReadIndex(dir) + if err != nil { + t.Fatal(err) + } + cands := ix.Lookup("foo", "A", false) + if len(cands) != 3 { + t.Errorf("got %d candidates for A, expected 3", len(cands)) + } + got := ix.LookupAll("foo", "A", "B", "C", "D") + if len(got) != 2 { + t.Errorf("got %d candidates for A,B,C,D, expected 2", len(got)) + } + got = ix.LookupAll("foo", []string{"A", "B", "C", "D", "E"}...) + if len(got) != 1 { + t.Errorf("got %d candidates for A,B,C,D,E, expected 1", len(got)) + } +} + +func TestUniquify(t *testing.T) { + var v []string + for i := 1; i < 4; i++ { + v = append(v, "A") + w := uniquify(v) + if len(w) != 1 { + t.Errorf("got %d, expected 1", len(w)) + } + } + for i := 1; i < 3; i++ { + v = append(v, "B", "C") + w := uniquify(v) + if len(w) != 3 { + t.Errorf("got %d, expected 3", len(w)) + } + } +} diff --git a/internal/modindex/symbols.go b/internal/modindex/symbols.go index 33bf2641f7b..b918529d43e 100644 --- a/internal/modindex/symbols.go +++ b/internal/modindex/symbols.go @@ -12,6 +12,7 @@ import ( "go/types" "os" "path/filepath" + "runtime" "slices" "strings" @@ -29,14 +30,14 @@ import ( type symbol struct { pkg string // name of the symbols's package name string // declared name - kind string // T, C, V, or F + kind string // T, C, V, or F, follwed by D if deprecated sig string // signature information, for F } // find the symbols for the best directories func getSymbols(cd Abspath, dirs map[string][]*directory) { var g errgroup.Group - g.SetLimit(-1) // maybe throttle this some day + g.SetLimit(max(2, runtime.GOMAXPROCS(0)/2)) for _, vv := range dirs { // throttling some day? d := vv[0] @@ -111,7 +112,7 @@ func getFileExports(f *ast.File) []symbol { // print struct tags. So for this to happen the type of a formal parameter // has to be a explict struct, e.g. foo(x struct{a int "$"}) and ExprString // would have to show the struct tag. Even testing for this case seems - // a waste of effort, but let's not ignore such pathologies + // a waste of effort, but let's remember the possibility if strings.Contains(tp, "$") { continue } From 3e68f53e14ac69e5a0c1aaab34a0d5f994332fd3 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 23 Jan 2025 09:03:18 -0500 Subject: [PATCH 063/309] internal/telemetry/cmd/stacks: add GitHub client Add a type for a GitHub client. The type holds the auth token, and its methods are the only way to access GitHub. The first of two CLs that will make it possible to test more of this program, by shunting changes to GitHub off to the side during testing. (See the following CL for details.) Change-Id: Ic487714fe75e19b016a132c0eeaaaf74d5c7cd42 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643936 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 3780cf3145c..5ffe101eff4 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -99,8 +99,6 @@ var ( daysFlag = flag.Int("days", 7, "number of previous days of telemetry data to read") dryRun = flag.Bool("n", false, "dry run, avoid updating issues") - - authToken string // mandatory GitHub authentication token (for R/W issues access) ) // ProgramConfig is the configuration for processing reports for a specific @@ -179,6 +177,8 @@ func main() { log.SetPrefix("stacks: ") flag.Parse() + var ghclient *githubClient + // Read GitHub authentication token from $HOME/.stacks.token. // // You can create one using the flow at: GitHub > You > Settings > @@ -198,12 +198,9 @@ func main() { tokenFile := filepath.Join(home, ".stacks.token") content, err := os.ReadFile(tokenFile) if err != nil { - if !os.IsNotExist(err) { - log.Fatalf("cannot read GitHub authentication token: %v", err) - } - log.Fatalf("no file %s containing GitHub authentication token.", tokenFile) + log.Fatalf("cannot read GitHub authentication token: %v", err) } - authToken = string(bytes.TrimSpace(content)) + ghclient = &githubClient{authToken: string(bytes.TrimSpace(content))} } pcfg, ok := programs[*programFlag] @@ -217,7 +214,7 @@ func main() { log.Fatalf("Error reading reports: %v", err) } - issues, err := readIssues(pcfg) + issues, err := readIssues(ghclient, pcfg) if err != nil { log.Fatalf("Error reading issues: %v", err) } @@ -226,7 +223,7 @@ func main() { claimedBy := claimStacks(issues, stacks) // Update existing issues that claimed new stacks. - updateIssues(issues, stacks, stackToURL) + updateIssues(ghclient, issues, stacks, stackToURL) // For each stack, show existing issue or create a new one. // Aggregate stack IDs by issue summary. @@ -392,9 +389,9 @@ func readReports(pcfg ProgramConfig, days int) (stacks map[string]map[Info]int64 // readIssues returns all existing issues for the given program and parses any // predicates. -func readIssues(pcfg ProgramConfig) ([]*Issue, error) { +func readIssues(cli *githubClient, pcfg ProgramConfig) ([]*Issue, error) { // Query GitHub for all existing GitHub issues with the report label. - issues, err := searchIssues(pcfg.SearchLabel) + issues, err := cli.searchIssues(pcfg.SearchLabel) if err != nil { // TODO(jba): return error instead of dying, or doc. log.Fatalf("GitHub issues label %q search failed: %v", pcfg.SearchLabel, err) @@ -564,7 +561,7 @@ func claimStacks(issues []*Issue, stacks map[string]map[Info]int64) map[string]* } // updateIssues updates existing issues that claimed new stacks by predicate. -func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL map[string]string) { +func updateIssues(cli *githubClient, issues []*Issue, stacks map[string]map[Info]int64, stackToURL map[string]string) { for _, issue := range issues { if len(issue.newStacks) == 0 { continue @@ -580,7 +577,7 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL writeStackComment(comment, stack, id, stackToURL[stack], stacks[stack]) } - if err := addIssueComment(issue.Number, comment.String()); err != nil { + if err := cli.addIssueComment(issue.Number, comment.String()); err != nil { log.Println(err) continue } @@ -593,7 +590,7 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL body += "\nDups:" } body += " " + strings.Join(newStackIDs, " ") - if err := updateIssueBody(issue.Number, body); err != nil { + if err := cli.updateIssueBody(issue.Number, body); err != nil { log.Printf("added comment to issue #%d but failed to update body: %v", issue.Number, err) continue @@ -811,10 +808,17 @@ func frameURL(pclntab map[string]FileLine, info Info, frame string) string { return "" } +// -- GitHub client -- + +// A githubClient interacts with GitHub. +type githubClient struct { + authToken string // mandatory GitHub authentication token (for R/W issues access) +} + // -- GitHub search -- // searchIssues queries the GitHub issue tracker. -func searchIssues(label string) ([]*Issue, error) { +func (cli *githubClient) searchIssues(label string) ([]*Issue, error) { label = url.QueryEscape(label) // Slurp all issues with the telemetry label. @@ -833,7 +837,7 @@ func searchIssues(label string) ([]*Issue, error) { if err != nil { return nil, err } - req.Header.Add("Authorization", "Bearer "+authToken) + req.Header.Add("Authorization", "Bearer "+cli.authToken) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err @@ -869,7 +873,7 @@ func searchIssues(label string) ([]*Issue, error) { } // updateIssueBody updates the body of the numbered issue. -func updateIssueBody(number int, body string) error { +func (cli *githubClient) updateIssueBody(number int, body string) error { // https://docs.github.com/en/rest/issues/comments#update-an-issue var payload struct { Body string `json:"body"` @@ -881,14 +885,14 @@ func updateIssueBody(number int, body string) error { } url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d", number) - if err := requestChange("PATCH", url, data, http.StatusOK); err != nil { + if err := cli.requestChange("PATCH", url, data, http.StatusOK); err != nil { return fmt.Errorf("updating issue: %v", err) } return nil } // addIssueComment adds a markdown comment to the numbered issue. -func addIssueComment(number int, comment string) error { +func (cli *githubClient) addIssueComment(number int, comment string) error { // https://docs.github.com/en/rest/issues/comments#create-an-issue-comment var payload struct { Body string `json:"body"` @@ -900,7 +904,7 @@ func addIssueComment(number int, comment string) error { } url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d/comments", number) - if err := requestChange("POST", url, data, http.StatusCreated); err != nil { + if err := cli.requestChange("POST", url, data, http.StatusCreated); err != nil { return fmt.Errorf("creating issue comment: %v", err) } return nil @@ -908,7 +912,7 @@ func addIssueComment(number int, comment string) error { // requestChange sends a request to url using method, which may change the state at the server. // The data is sent as the request body, and wantStatus is the expected response status code. -func requestChange(method, url string, data []byte, wantStatus int) error { +func (cli *githubClient) requestChange(method, url string, data []byte, wantStatus int) error { if *dryRun { log.Printf("DRY RUN: %s %s", method, url) return nil @@ -917,7 +921,7 @@ func requestChange(method, url string, data []byte, wantStatus int) error { if err != nil { return err } - req.Header.Add("Authorization", "Bearer "+authToken) + req.Header.Add("Authorization", "Bearer "+cli.authToken) resp, err := http.DefaultClient.Do(req) if err != nil { return err From f055343eb1f9b9c9c98449be61c7743400d13ac5 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 23 Jan 2025 11:33:02 -0500 Subject: [PATCH 064/309] internal/telemetry/cmd/stacks: divert GitHub changes during testing When testing, the GitHub client saves changes instead of applying them. This makes it possible to test functions that would otherwise alter GitHub. Add a test for one such function, updateIssues. Change-Id: I77a716dac346ab591ff1f94cd38e414d0082d513 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643937 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/telemetry/cmd/stacks/stacks.go | 28 +++++++++- .../telemetry/cmd/stacks/stacks_test.go | 51 ++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 5ffe101eff4..7ac031ce5d9 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -811,8 +811,24 @@ func frameURL(pclntab map[string]FileLine, info Info, frame string) string { // -- GitHub client -- // A githubClient interacts with GitHub. +// During testing, updates to GitHub are saved in changes instead of being applied. +// Reads from GitHub occur normally. type githubClient struct { - authToken string // mandatory GitHub authentication token (for R/W issues access) + authToken string // mandatory GitHub authentication token (for R/W issues access) + divertChanges bool // divert attempted GitHub changes to the changes field instead of executing them + changes []any // slice of (addIssueComment | updateIssueBody) +} + +// addIssueComment is a change for creating a comment on an issue. +type addIssueComment struct { + number int + comment string +} + +// updateIssueBody is a change for modifying an existing issue's body. +type updateIssueBody struct { + number int + body string } // -- GitHub search -- @@ -874,6 +890,11 @@ func (cli *githubClient) searchIssues(label string) ([]*Issue, error) { // updateIssueBody updates the body of the numbered issue. func (cli *githubClient) updateIssueBody(number int, body string) error { + if cli.divertChanges { + cli.changes = append(cli.changes, updateIssueBody{number, body}) + return nil + } + // https://docs.github.com/en/rest/issues/comments#update-an-issue var payload struct { Body string `json:"body"` @@ -893,6 +914,11 @@ func (cli *githubClient) updateIssueBody(number int, body string) error { // addIssueComment adds a markdown comment to the numbered issue. func (cli *githubClient) addIssueComment(number int, comment string) error { + if cli.divertChanges { + cli.changes = append(cli.changes, addIssueComment{number, comment}) + return nil + } + // https://docs.github.com/en/rest/issues/comments#create-an-issue-comment var payload struct { Body string `json:"body"` diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index 714bb9499fe..94e02cc6936 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -7,6 +7,7 @@ package main import ( + "strings" "testing" ) @@ -128,8 +129,54 @@ func TestParsePredicateError(t *testing.T) { } { if _, err := parsePredicate(expr); err == nil { t.Errorf("%s: got nil, want error", expr) - } else { - t.Logf("%s: %v", expr, err) } } } + +// which takes the bulk of the time. +func TestUpdateIssues(t *testing.T) { + if testing.Short() { + t.Skip("downloads source from the internet, skipping in -short") + } + c := &githubClient{divertChanges: true} + issues := []*Issue{{Number: 1, newStacks: []string{"stack1"}}} + info := Info{ + Program: "golang.org/x/tools/gopls", + ProgramVersion: "v0.16.1", + } + const stack1 = "stack1" + id1 := stackID(stack1) + stacks := map[string]map[Info]int64{stack1: map[Info]int64{info: 3}} + stacksToURL := map[string]string{stack1: "URL1"} + updateIssues(c, issues, stacks, stacksToURL) + + if g, w := len(c.changes), 2; g != w { + t.Fatalf("got %d changes, want %d", g, w) + } + // The first change creates an issue comment. + cic, ok := c.changes[0].(addIssueComment) + if !ok { + t.Fatalf("got %T, want addIssueComment", c.changes[0]) + } + if cic.number != 1 { + t.Errorf("issue number: got %d, want 1", cic.number) + } + for _, want := range []string{"URL1", stack1, id1, "golang.org/x/tools/gopls@v0.16.1"} { + if !strings.Contains(cic.comment, want) { + t.Errorf("missing %q in comment:\n%s", want, cic.comment) + } + } + + // The second change updates the issue body. + ui, ok := c.changes[1].(updateIssueBody) + if !ok { + t.Fatalf("got %T, want updateIssueBody", c.changes[1]) + } + if ui.number != 1 { + t.Errorf("issue number: got %d, want 1", cic.number) + } + want := "Dups: " + id1 + if !strings.Contains(ui.body, want) { + t.Errorf("missing %q in body %q", want, ui.body) + } +} From 1c9f00266fbf51558e5e75872306521c430fe082 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 24 Jan 2025 18:29:27 +0000 Subject: [PATCH 065/309] internal/gocommand: add openbsd to the set of GOOS to debug We encountered a hanging go command on openbsd, which unfortunately did not have logic to SIGQUIT. Add openbsd to the special set of GOOS in handleHandingGoCommand. Updates golang/go#54461 Change-Id: I36e32559f23a3ace28a1088a1f910642eb0074ec Reviewed-on: https://go-review.googlesource.com/c/tools/+/644016 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- internal/gocommand/invoke.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/gocommand/invoke.go b/internal/gocommand/invoke.go index fcde93c665d..5db1ed6fe1a 100644 --- a/internal/gocommand/invoke.go +++ b/internal/gocommand/invoke.go @@ -426,7 +426,7 @@ func runCmdContext(ctx context.Context, cmd *exec.Cmd) (err error) { // cause of a hanging Go command, and then exits with log.Fatalf. func handleHangingGoCommand(start time.Time, cmd *exec.Cmd, resChan chan error) { switch runtime.GOOS { - case "linux", "darwin", "freebsd", "netbsd": + case "linux", "darwin", "freebsd", "netbsd", "openbsd": fmt.Fprintln(os.Stderr, `DETECTED A HANGING GO COMMAND The gopls test runner has detected a hanging go command. In order to debug From 114ac8273d6a7b810aa726d61cf49574e6f5bdbd Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 17 Jan 2025 20:33:22 -0500 Subject: [PATCH 066/309] go/analysis: preparatory cleanups This change contains some minor cleanups split out of the forthcoming three-way merge work. 1. Plumb pass.ReadFile down from a (hidden) checker option. Factor CheckedReadFile helper. 2. In "assign" checker, improve SuggestedFix title. 3. Fix bug in error handling in fix_test.go. 4. Define testenv.RedirectStderr helper to temporarily redirect stderr and log output to t.Log, to declutter the test output. Update golang/go#68765 Update golang/go#67049 Change-Id: Icac62afdeb160a2dfa3cc3637b79fe7d89e92272 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643695 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan --- go/analysis/analysistest/analysistest.go | 5 ++- go/analysis/checker/checker.go | 10 ++++- go/analysis/diagnostic.go | 4 +- go/analysis/internal/checker/checker.go | 4 ++ go/analysis/internal/checker/checker_test.go | 9 ++++- go/analysis/internal/checker/fix_test.go | 24 ++++++++---- go/analysis/internal/checker/start_test.go | 1 + go/analysis/passes/assign/assign.go | 10 +++-- go/analysis/unitchecker/unitchecker.go | 2 +- go/analysis/unitchecker/unitchecker_test.go | 2 +- internal/analysisinternal/analysis.go | 26 +++++-------- internal/testenv/testenv.go | 39 ++++++++++++++++++++ 12 files changed, 101 insertions(+), 35 deletions(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 7a27e006033..e63dd16c06b 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -167,6 +167,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns act := result.Action // file -> message -> edits + // TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers. fileEdits := make(map[*token.File]map[string][]diff.Edit) fileContents := make(map[*token.File][]byte) @@ -179,6 +180,8 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns t.Errorf("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command") } + // TODO(adonovan): factor in common with go/analysis/internal/checker.validateEdits. + for _, edit := range fix.TextEdits { start, end := edit.Pos, edit.End if !end.IsValid() { @@ -275,7 +278,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns } } else { // all suggested fixes are represented by a single file - + // TODO(adonovan): fix: this makes no sense if len(fixes) > 1. var catchallEdits []diff.Edit for _, edits := range fixes { catchallEdits = append(catchallEdits, edits...) diff --git a/go/analysis/checker/checker.go b/go/analysis/checker/checker.go index 5935a62abaf..a563f7cbeda 100644 --- a/go/analysis/checker/checker.go +++ b/go/analysis/checker/checker.go @@ -35,6 +35,7 @@ import ( "go/types" "io" "log" + "os" "reflect" "sort" "strings" @@ -55,9 +56,10 @@ type Options struct { SanityCheck bool // check fact encoding is ok and deterministic FactLog io.Writer // if non-nil, log each exported fact to it - // TODO(adonovan): add ReadFile so that an Overlay specified + // TODO(adonovan): expose ReadFile so that an Overlay specified // in the [packages.Config] can be communicated via // Pass.ReadFile to each Analyzer. + readFile analysisinternal.ReadFileFunc } // Graph holds the results of a round of analysis, including the graph @@ -344,7 +346,11 @@ func (act *Action) execOnce() { AllObjectFacts: act.AllObjectFacts, AllPackageFacts: act.AllPackageFacts, } - pass.ReadFile = analysisinternal.MakeReadFile(pass) + readFile := os.ReadFile + if act.opts.readFile != nil { + readFile = act.opts.readFile + } + pass.ReadFile = analysisinternal.CheckedReadFile(pass, readFile) act.pass = pass act.Result, act.Err = func() (any, error) { diff --git a/go/analysis/diagnostic.go b/go/analysis/diagnostic.go index ee083a2d686..f6118bec647 100644 --- a/go/analysis/diagnostic.go +++ b/go/analysis/diagnostic.go @@ -65,7 +65,9 @@ type RelatedInformation struct { // user can choose to apply to their code. Usually the SuggestedFix is // meant to fix the issue flagged by the diagnostic. // -// The TextEdits must not overlap, nor contain edits for other packages. +// The TextEdits must not overlap, nor contain edits for other +// packages. Edits need not be totally ordered, but the order +// determines how insertions at the same point will be applied. type SuggestedFix struct { // A verb phrase describing the fix, to be shown to // a user trying to decide whether to accept it. diff --git a/go/analysis/internal/checker/checker.go b/go/analysis/internal/checker/checker.go index 0c2fc5e59db..a4cddeb2c6e 100644 --- a/go/analysis/internal/checker/checker.go +++ b/go/analysis/internal/checker/checker.go @@ -20,6 +20,7 @@ import ( "go/token" "io" "io/ioutil" + "log" "os" "runtime" @@ -139,6 +140,9 @@ func Run(args []string, analyzers []*analysis.Analyzer) int { return 1 } + // TODO(adonovan): simplify exit code logic by using a single + // exit code variable and applying "code = max(code, X)" each + // time an error of code X occurs. pkgsExitCode := 0 // Print package and module errors regardless of RunDespiteErrors. // Do not exit if there are errors, yet. diff --git a/go/analysis/internal/checker/checker_test.go b/go/analysis/internal/checker/checker_test.go index 77a57f5119c..7f38ad1a094 100644 --- a/go/analysis/internal/checker/checker_test.go +++ b/go/analysis/internal/checker/checker_test.go @@ -25,6 +25,7 @@ import ( func TestApplyFixes(t *testing.T) { testenv.NeedsGoPackages(t) + testenv.RedirectStderr(t) // associated checker.Run output with this test files := map[string]string{ "rename/test.go": `package rename @@ -114,10 +115,12 @@ func run(pass *analysis.Pass) (interface{}, error) { {Pos: ident.Pos(), End: ident.End(), NewText: []byte("lorem ipsum")}, }...) case duplicate: + // Duplicate (non-insertion) edits are disallowed, + // so this is a buggy analyzer, and validatedFixes should reject it. edits = append(edits, edits...) case other: if pass.Analyzer.Name == other { - edits[0].Pos = edits[0].Pos + 1 // shift by one to mismatch analyzer and other + edits[0].Pos++ // shift by one to mismatch analyzer and other } } pass.Report(analysis.Diagnostic{ @@ -133,6 +136,7 @@ func run(pass *analysis.Pass) (interface{}, error) { func TestRunDespiteErrors(t *testing.T) { testenv.NeedsGoPackages(t) + testenv.RedirectStderr(t) // associate checker.Run output with this test files := map[string]string{ "rderr/test.go": `package rderr @@ -360,4 +364,7 @@ hello from other if !ran { t.Error("analyzer did not run") } + + // TODO(adonovan): test that fixes are applied to the + // pass.ReadFile virtual file tree. } diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index b169d79a087..81bc569e861 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -77,16 +77,23 @@ func fix(t *testing.T, dir, analyzers string, wantExit int, patterns ...string) return strings.ReplaceAll(s, os.TempDir(), "os.TempDir/") } outBytes, err := cmd.CombinedOutput() - out := clean(string(outBytes)) - t.Logf("$ %s\n%s", clean(fmt.Sprint(cmd)), out) - if err, ok := err.(*exec.ExitError); !ok { - t.Fatalf("failed to execute multichecker: %v", err) - } else if err.ExitCode() != wantExit { - // plan9 ExitCode() currently only returns 0 for success or 1 for failure - if !(runtime.GOOS == "plan9" && wantExit != exitCodeSuccess && err.ExitCode() != exitCodeSuccess) { - t.Errorf("exit code was %d, want %d", err.ExitCode(), wantExit) + switch err := err.(type) { + case nil: + // success + case *exec.ExitError: + if code := err.ExitCode(); code != wantExit { + // plan9 ExitCode() currently only returns 0 for success or 1 for failure + if !(runtime.GOOS == "plan9" && wantExit != exitCodeSuccess && code != exitCodeSuccess) { + t.Errorf("exit code was %d, want %d", code, wantExit) + } } + default: + t.Fatalf("failed to execute multichecker: %v", err) } + + out := clean(string(outBytes)) + t.Logf("$ %s\n%s", clean(fmt.Sprint(cmd)), out) + return out } @@ -253,6 +260,7 @@ func Foo() { } defer cleanup() + // The 'rename' and 'other' analyzers suggest conflicting fixes. out := fix(t, dir, "rename,other", exitCodeFailed, "other") pattern := `.*conflicting edits from other and rename on .*foo.go` diff --git a/go/analysis/internal/checker/start_test.go b/go/analysis/internal/checker/start_test.go index af4dc42c85c..618ccd09b93 100644 --- a/go/analysis/internal/checker/start_test.go +++ b/go/analysis/internal/checker/start_test.go @@ -22,6 +22,7 @@ import ( // of the file takes effect. func TestStartFixes(t *testing.T) { testenv.NeedsGoPackages(t) + testenv.RedirectStderr(t) // associated checker.Run output with this test files := map[string]string{ "comment/doc.go": `/* Package comment */ diff --git a/go/analysis/passes/assign/assign.go b/go/analysis/passes/assign/assign.go index ff94c271c45..1413ee13d29 100644 --- a/go/analysis/passes/assign/assign.go +++ b/go/analysis/passes/assign/assign.go @@ -63,10 +63,12 @@ func run(pass *analysis.Pass) (any, error) { if le == re { pass.Report(analysis.Diagnostic{ Pos: stmt.Pos(), Message: fmt.Sprintf("self-assignment of %s to %s", re, le), - SuggestedFixes: []analysis.SuggestedFix{ - {Message: "Remove", TextEdits: []analysis.TextEdit{ - {Pos: stmt.Pos(), End: stmt.End(), NewText: []byte{}}, - }}, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Remove self-assignment", + TextEdits: []analysis.TextEdit{{ + Pos: stmt.Pos(), + End: stmt.End(), + }}}, }, }) } diff --git a/go/analysis/unitchecker/unitchecker.go b/go/analysis/unitchecker/unitchecker.go index 1a9b3094e5e..f723349010e 100644 --- a/go/analysis/unitchecker/unitchecker.go +++ b/go/analysis/unitchecker/unitchecker.go @@ -386,7 +386,7 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re AllPackageFacts: func() []analysis.PackageFact { return facts.AllPackageFacts(factFilter) }, Module: module, } - pass.ReadFile = analysisinternal.MakeReadFile(pass) + pass.ReadFile = analysisinternal.CheckedReadFile(pass, os.ReadFile) t0 := time.Now() act.result, act.err = a.Run(pass) diff --git a/go/analysis/unitchecker/unitchecker_test.go b/go/analysis/unitchecker/unitchecker_test.go index 1801b49cfe8..173d76348f7 100644 --- a/go/analysis/unitchecker/unitchecker_test.go +++ b/go/analysis/unitchecker/unitchecker_test.go @@ -133,7 +133,7 @@ func _() { "message": "self-assignment of i to i", "suggested_fixes": \[ \{ - "message": "Remove", + "message": "Remove self-assignment", "edits": \[ \{ "filename": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go", diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 39583a401b0..8bfba325b49 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -14,7 +14,6 @@ import ( "go/scanner" "go/token" "go/types" - "os" pathpkg "path" "slices" "strings" @@ -178,20 +177,25 @@ func equivalentTypes(want, got types.Type) bool { return types.AssignableTo(want, got) } -// MakeReadFile returns a simple implementation of the Pass.ReadFile function. -func MakeReadFile(pass *analysis.Pass) func(filename string) ([]byte, error) { +// A ReadFileFunc is a function that returns the +// contents of a file, such as [os.ReadFile]. +type ReadFileFunc = func(filename string) ([]byte, error) + +// CheckedReadFile returns a wrapper around a Pass.ReadFile +// function that performs the appropriate checks. +func CheckedReadFile(pass *analysis.Pass, readFile ReadFileFunc) ReadFileFunc { return func(filename string) ([]byte, error) { if err := CheckReadable(pass, filename); err != nil { return nil, err } - return os.ReadFile(filename) + return readFile(filename) } } // CheckReadable enforces the access policy defined by the ReadFile field of [analysis.Pass]. func CheckReadable(pass *analysis.Pass, filename string) error { - if slicesContains(pass.OtherFiles, filename) || - slicesContains(pass.IgnoredFiles, filename) { + if slices.Contains(pass.OtherFiles, filename) || + slices.Contains(pass.IgnoredFiles, filename) { return nil } for _, f := range pass.Files { @@ -202,16 +206,6 @@ func CheckReadable(pass *analysis.Pass, filename string) error { return fmt.Errorf("Pass.ReadFile: %s is not among OtherFiles, IgnoredFiles, or names of Files", filename) } -// TODO(adonovan): use go1.21 slices.Contains. -func slicesContains[S ~[]E, E comparable](slice S, x E) bool { - for _, elem := range slice { - if elem == x { - return true - } - } - return false -} - // AddImport checks whether this file already imports pkgpath and // that import is in scope at pos. If so, it returns the name under // which it was imported and a zero edit. Otherwise, it adds a new diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index d217e28462c..360ff0ffbe8 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -7,10 +7,12 @@ package testenv import ( + "bufio" "bytes" "context" "fmt" "go/build" + "log" "os" "os/exec" "path/filepath" @@ -553,3 +555,40 @@ func NeedsGOROOTDir(t *testing.T, dir string) { } } } + +// RedirectStderr causes os.Stderr (and the global logger) to be +// temporarily replaced so that writes to it are sent to t.Log. +// It is restored at test cleanup. +func RedirectStderr(t testing.TB) { + t.Setenv("RedirectStderr", "") // side effect: assert t.Parallel wasn't called + + // TODO(adonovan): if https://go.dev/issue/59928 is accepted, + // simply set w = t.Output() and dispense with the pipe. + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("pipe: %v", err) + } + go func() { + for sc := bufio.NewScanner(r); sc.Scan(); { + t.Log(sc.Text()) + } + r.Close() + }() + + // Also do the same for the global logger. + savedWriter, savedPrefix, savedFlags := log.Writer(), log.Prefix(), log.Flags() + log.SetPrefix("log: ") + log.SetOutput(w) + log.SetFlags(0) + + oldStderr := os.Stderr + os.Stderr = w + t.Cleanup(func() { + w.Close() // ignore error + os.Stderr = oldStderr + + log.SetOutput(savedWriter) + log.SetPrefix(savedPrefix) + log.SetFlags(savedFlags) + }) +} From 4f1e910bdce94e3b7a73824e4c408377e12dcf52 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 23 Jan 2025 08:27:28 -0500 Subject: [PATCH 067/309] internal/telemetry/cmd/stacks: reopen issues Re-open an issue if it was closed as fixed, but we encounter a new stack in a later version. For golang/go#71045. Change-Id: If6a4fe4091588e42b6f6c47e8705313352dc295e Reviewed-on: https://go-review.googlesource.com/c/tools/+/644115 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/golang/assembly.go | 11 +- gopls/internal/telemetry/cmd/stacks/stacks.go | 122 ++++++++--- .../telemetry/cmd/stacks/stacks_test.go | 207 +++++++++++++++--- gopls/internal/util/morestrings/strings.go | 15 ++ 4 files changed, 290 insertions(+), 65 deletions(-) create mode 100644 gopls/internal/util/morestrings/strings.go diff --git a/gopls/internal/golang/assembly.go b/gopls/internal/golang/assembly.go index 7f0ace4daf6..3b778a54697 100644 --- a/gopls/internal/golang/assembly.go +++ b/gopls/internal/golang/assembly.go @@ -21,6 +21,7 @@ import ( "strings" "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/util/morestrings" ) // AssemblyHTML returns an HTML document containing an assembly listing of the selected function. @@ -103,7 +104,7 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Pack // Skip filenames of the form "". if parts := insnRx.FindStringSubmatch(line); parts != nil { link := " " // if unknown - if file, linenum, ok := cutLast(parts[2], ":"); ok && !strings.HasPrefix(file, "<") { + if file, linenum, ok := morestrings.CutLast(parts[2], ":"); ok && !strings.HasPrefix(file, "<") { if linenum, err := strconv.Atoi(linenum); err == nil { text := fmt.Sprintf("L%04d", linenum) link = sourceLink(text, web.SrcURL(file, linenum, 1)) @@ -117,11 +118,3 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Pack } return buf.Bytes(), nil } - -// cutLast is the "last" analogue of [strings.Cut]. -func cutLast(s, sep string) (before, after string, ok bool) { - if i := strings.LastIndex(s, sep); i >= 0 { - return s[:i], s[i+len(sep):], true - } - return s, "", false -} diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 7ac031ce5d9..fca230e3acd 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -77,6 +77,7 @@ import ( "net/url" "os" "os/exec" + "path" "path/filepath" "regexp" "runtime" @@ -86,10 +87,12 @@ import ( "time" "unicode" + "golang.org/x/mod/semver" "golang.org/x/sys/unix" "golang.org/x/telemetry" "golang.org/x/tools/gopls/internal/util/browser" "golang.org/x/tools/gopls/internal/util/moremaps" + "golang.org/x/tools/gopls/internal/util/morestrings" ) // flags @@ -243,8 +246,15 @@ func main() { if issue, ok := claimedBy[id]; ok { // existing issue, already updated above, just store // the summary. + state := issue.State + if issue.State == "closed" && issue.StateReason == "completed" { + state = "completed" + } summary := fmt.Sprintf("#%d: %s [%s]", - issue.Number, issue.Title, issue.State) + issue.Number, issue.Title, state) + if state == "completed" && issue.Milestone != nil { + summary += " milestone " + strings.TrimPrefix(issue.Milestone.Title, "gopls/") + } existingIssues[summary] += total } else { // new issue, need to create GitHub issue and store @@ -266,7 +276,7 @@ func main() { for _, summary := range keys { count := issues[summary] // Show closed issues in "white". - if isTerminal(os.Stdout) && strings.Contains(summary, "[closed]") { + if isTerminal(os.Stdout) && (strings.Contains(summary, "[closed]") || strings.Contains(summary, "[completed]")) { // ESC + "[" + n + "m" => change color to n // (37 = white, 0 = default) summary = "\x1B[37m" + summary + "\x1B[0m" @@ -590,8 +600,14 @@ func updateIssues(cli *githubClient, issues []*Issue, stacks map[string]map[Info body += "\nDups:" } body += " " + strings.Join(newStackIDs, " ") - if err := cli.updateIssueBody(issue.Number, body); err != nil { - log.Printf("added comment to issue #%d but failed to update body: %v", + + update := updateIssue{number: issue.Number, Body: body} + if shouldReopen(issue, stacks) { + update.State = "open" + update.StateReason = "reopened" + } + if err := cli.updateIssue(update); err != nil { + log.Printf("added comment to issue #%d but failed to update: %v", issue.Number, err) continue } @@ -600,6 +616,50 @@ func updateIssues(cli *githubClient, issues []*Issue, stacks map[string]map[Info } } +// An issue should be re-opened if it was closed as fixed, and at least one of the +// new stacks happened since the version containing the fix. +func shouldReopen(issue *Issue, stacks map[string]map[Info]int64) bool { + if !issue.isFixed() { + return false + } + issueProgram, issueVersion, ok := parseMilestone(issue.Milestone) + if !ok { + return false + } + // TODO(jba?): handle other programs + if issueProgram != "gopls" { + return false + } + for _, stack := range issue.newStacks { + for info := range stacks[stack] { + if path.Base(info.Program) == issueProgram && semver.Compare(info.ProgramVersion, issueVersion) >= 0 { + log.Printf("reopening issue #%d: purportedly fixed in %s@%s, but found a new stack from version %s", + issue.Number, issueProgram, issueVersion, info.ProgramVersion) + return true + } + } + } + return false +} + +// An issue is fixed if it was closed because it was completed. +func (i *Issue) isFixed() bool { + return i.State == "closed" && i.StateReason == "completed" +} + +// parseMilestone parses a the title of a GitHub milestone that is in the format +// PROGRAM/VERSION. For example, "gopls/v0.17.0". +func parseMilestone(m *Milestone) (program, version string, ok bool) { + if m == nil { + return "", "", false + } + program, version, ok = morestrings.CutLast(m.Title, "/") + if !ok || program == "" || version == "" || version[0] != 'v' { + return "", "", false + } + return program, version, true +} + // stackID returns a 32-bit identifier for a stack // suitable for use in GitHub issue titles. func stackID(stack string) string { @@ -819,16 +879,27 @@ type githubClient struct { changes []any // slice of (addIssueComment | updateIssueBody) } +func (cli *githubClient) takeChanges() []any { + r := cli.changes + cli.changes = nil + return r +} + // addIssueComment is a change for creating a comment on an issue. type addIssueComment struct { number int comment string } -// updateIssueBody is a change for modifying an existing issue's body. -type updateIssueBody struct { - number int - body string +// updateIssue is a change for modifying an existing issue. +// It includes the issue number and the fields that can be updated on a GitHub issue. +// A JSON-marshaled updateIssue can be used as the body of the update request sent to GitHub. +// See https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#update-an-issue. +type updateIssue struct { + number int // issue number; must be unexported + Body string `json:"body,omitempty"` + State string `json:"state,omitempty"` // "open" or "closed" + StateReason string `json:"state_reason,omitempty"` // "completed", "not_planned", "reopened" } // -- GitHub search -- @@ -888,24 +959,19 @@ func (cli *githubClient) searchIssues(label string) ([]*Issue, error) { return results, nil } -// updateIssueBody updates the body of the numbered issue. -func (cli *githubClient) updateIssueBody(number int, body string) error { +// updateIssue updates the numbered issue. +func (cli *githubClient) updateIssue(update updateIssue) error { if cli.divertChanges { - cli.changes = append(cli.changes, updateIssueBody{number, body}) + cli.changes = append(cli.changes, update) return nil } - // https://docs.github.com/en/rest/issues/comments#update-an-issue - var payload struct { - Body string `json:"body"` - } - payload.Body = body - data, err := json.Marshal(payload) + data, err := json.Marshal(update) if err != nil { return err } - url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d", number) + url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d", update.number) if err := cli.requestChange("PATCH", url, data, http.StatusOK); err != nil { return fmt.Errorf("updating issue: %v", err) } @@ -963,13 +1029,15 @@ func (cli *githubClient) requestChange(method, url string, data []byte, wantStat // See https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues. type Issue struct { - Number int - HTMLURL string `json:"html_url"` - Title string - State string - User *User - CreatedAt time.Time `json:"created_at"` - Body string // in Markdown format + Number int + HTMLURL string `json:"html_url"` + Title string + State string + StateReason string `json:"state_reason"` + User *User + CreatedAt time.Time `json:"created_at"` + Body string // in Markdown format + Milestone *Milestone // Set by readIssues. predicate func(string) bool // matching predicate over stack text @@ -983,6 +1051,10 @@ type User struct { HTMLURL string `json:"html_url"` } +type Milestone struct { + Title string +} + // -- pclntab -- type FileLine struct { diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index 94e02cc6936..1f3cbef1f29 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -7,6 +7,7 @@ package main import ( + "encoding/json" "strings" "testing" ) @@ -138,45 +139,189 @@ func TestUpdateIssues(t *testing.T) { if testing.Short() { t.Skip("downloads source from the internet, skipping in -short") } + c := &githubClient{divertChanges: true} - issues := []*Issue{{Number: 1, newStacks: []string{"stack1"}}} - info := Info{ - Program: "golang.org/x/tools/gopls", - ProgramVersion: "v0.16.1", - } const stack1 = "stack1" id1 := stackID(stack1) - stacks := map[string]map[Info]int64{stack1: map[Info]int64{info: 3}} stacksToURL := map[string]string{stack1: "URL1"} - updateIssues(c, issues, stacks, stacksToURL) - if g, w := len(c.changes), 2; g != w { - t.Fatalf("got %d changes, want %d", g, w) - } - // The first change creates an issue comment. - cic, ok := c.changes[0].(addIssueComment) - if !ok { - t.Fatalf("got %T, want addIssueComment", c.changes[0]) - } - if cic.number != 1 { - t.Errorf("issue number: got %d, want 1", cic.number) - } - for _, want := range []string{"URL1", stack1, id1, "golang.org/x/tools/gopls@v0.16.1"} { - if !strings.Contains(cic.comment, want) { - t.Errorf("missing %q in comment:\n%s", want, cic.comment) + // checkIssueComment asserts that the change adds an issue of the specified + // number, with a body that contains various strings. + checkIssueComment := func(t *testing.T, change any, number int, version string) { + t.Helper() + cic, ok := change.(addIssueComment) + if !ok { + t.Fatalf("got %T, want addIssueComment", change) + } + if cic.number != number { + t.Errorf("issue number: got %d, want %d", cic.number, number) + } + for _, want := range []string{"URL1", stack1, id1, "golang.org/x/tools/gopls@" + version} { + if !strings.Contains(cic.comment, want) { + t.Errorf("missing %q in comment:\n%s", want, cic.comment) + } } } - // The second change updates the issue body. - ui, ok := c.changes[1].(updateIssueBody) - if !ok { - t.Fatalf("got %T, want updateIssueBody", c.changes[1]) - } - if ui.number != 1 { - t.Errorf("issue number: got %d, want 1", cic.number) + t.Run("open issue", func(t *testing.T) { + issues := []*Issue{{ + Number: 1, + State: "open", + newStacks: []string{stack1}, + }} + + info := Info{ + Program: "golang.org/x/tools/gopls", + ProgramVersion: "v0.16.1", + } + stacks := map[string]map[Info]int64{stack1: map[Info]int64{info: 3}} + updateIssues(c, issues, stacks, stacksToURL) + changes := c.takeChanges() + + if g, w := len(changes), 2; g != w { + t.Fatalf("got %d changes, want %d", g, w) + } + + // The first change creates an issue comment. + checkIssueComment(t, changes[0], 1, "v0.16.1") + + // The second change updates the issue body, and only the body. + ui, ok := changes[1].(updateIssue) + if !ok { + t.Fatalf("got %T, want updateIssue", changes[1]) + } + if ui.number != 1 { + t.Errorf("issue number: got %d, want 1", ui.number) + } + if ui.Body == "" || ui.State != "" || ui.StateReason != "" { + t.Errorf("updating other than just the body:\n%+v", ui) + } + want := "Dups: " + id1 + if !strings.Contains(ui.Body, want) { + t.Errorf("missing %q in body %q", want, ui.Body) + } + }) + t.Run("should be reopened", func(t *testing.T) { + issues := []*Issue{{ + // Issue purportedly fixed in v0.16.0 + Number: 2, + State: "closed", + StateReason: "completed", + Milestone: &Milestone{Title: "gopls/v0.16.0"}, + newStacks: []string{stack1}, + }} + // New stack in a later version. + info := Info{ + Program: "golang.org/x/tools/gopls", + ProgramVersion: "v0.17.0", + } + stacks := map[string]map[Info]int64{stack1: map[Info]int64{info: 3}} + updateIssues(c, issues, stacks, stacksToURL) + + changes := c.takeChanges() + if g, w := len(changes), 2; g != w { + t.Fatalf("got %d changes, want %d", g, w) + } + // The first change creates an issue comment. + checkIssueComment(t, changes[0], 2, "v0.17.0") + + // The second change updates the issue body, state, and state reason. + ui, ok := changes[1].(updateIssue) + if !ok { + t.Fatalf("got %T, want updateIssue", changes[1]) + } + if ui.number != 2 { + t.Errorf("issue number: got %d, want 2", ui.number) + } + if ui.Body == "" || ui.State != "open" || ui.StateReason != "reopened" { + t.Errorf(`update fields should be non-empty body, state "open", state reason "reopened":\n%+v`, ui) + } + want := "Dups: " + id1 + if !strings.Contains(ui.Body, want) { + t.Errorf("missing %q in body %q", want, ui.Body) + } + + }) + +} + +func TestMarshalUpdateIssueFields(t *testing.T) { + // Verify that only the non-empty fields of updateIssueFields are marshalled. + for _, tc := range []struct { + fields updateIssue + want string + }{ + {updateIssue{Body: "b"}, `{"body":"b"}`}, + {updateIssue{State: "open"}, `{"state":"open"}`}, + {updateIssue{State: "open", StateReason: "reopened"}, `{"state":"open","state_reason":"reopened"}`}, + } { + bytes, err := json.Marshal(tc.fields) + if err != nil { + t.Fatal(err) + } + got := string(bytes) + if got != tc.want { + t.Errorf("%+v: got %s, want %s", tc.fields, got, tc.want) + } } - want := "Dups: " + id1 - if !strings.Contains(ui.body, want) { - t.Errorf("missing %q in body %q", want, ui.body) +} + +func TestShouldReopen(t *testing.T) { + const stack = "stack" + const gopls = "golang.org/x/tools/gopls" + const milestoneVersion = "v0.2.0" + + for _, tc := range []struct { + name string + issue Issue + info Info + want bool + }{ + { + "issue open", + Issue{State: "open"}, + Info{Program: gopls, ProgramVersion: "v0.2.0"}, + false, + }, + { + "issue closed but not fixed", + Issue{State: "closed", StateReason: "not_planned"}, + Info{Program: gopls, ProgramVersion: "v0.2.0"}, + false, + }, + { + "different program", + Issue{State: "closed", StateReason: "completed"}, + Info{Program: "other", ProgramVersion: "v0.2.0"}, + false, + }, + { + "later version", + Issue{State: "closed", StateReason: "completed"}, + Info{Program: gopls, ProgramVersion: "v0.3.0"}, + true, + }, + { + "earlier version", + Issue{State: "closed", StateReason: "completed"}, + Info{Program: gopls, ProgramVersion: "v0.1.0"}, + false, + }, + { + "same version", + Issue{State: "closed", StateReason: "completed"}, + Info{Program: gopls, ProgramVersion: "v0.2.0"}, + true, + }, + } { + t.Run(tc.name, func(t *testing.T) { + tc.issue.Number = 1 + tc.issue.Milestone = &Milestone{Title: "gopls/" + milestoneVersion} + tc.issue.newStacks = []string{stack} + got := shouldReopen(&tc.issue, map[string]map[Info]int64{stack: map[Info]int64{tc.info: 1}}) + if got != tc.want { + t.Errorf("got %t, want %t", got, tc.want) + } + }) } } diff --git a/gopls/internal/util/morestrings/strings.go b/gopls/internal/util/morestrings/strings.go new file mode 100644 index 00000000000..5632006a40f --- /dev/null +++ b/gopls/internal/util/morestrings/strings.go @@ -0,0 +1,15 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package morestrings + +import "strings" + +// CutLast is the "last" analogue of [strings.Cut]. +func CutLast(s, sep string) (before, after string, ok bool) { + if i := strings.LastIndex(s, sep); i >= 0 { + return s[:i], s[i+len(sep):], true + } + return s, "", false +} From 891e3b67e3bfc42021ccf399dba2300c097bbed3 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 24 Jan 2025 14:35:43 -0500 Subject: [PATCH 068/309] internal/telemetry/cmd/stacks: cmd/compile reopen Support re-opening issues for the compiler. Fixes golang/go#71045. Change-Id: I6e5dad81220c74923919b4c72f7bc1089af6c37d Reviewed-on: https://go-review.googlesource.com/c/tools/+/644018 Reviewed-by: Alan Donovan Auto-Submit: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI TryBot-Result: Gopher Robot --- gopls/internal/telemetry/cmd/stacks/stacks.go | 49 ++++++++++++++++--- .../telemetry/cmd/stacks/stacks_test.go | 34 ++++++++++--- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index fca230e3acd..64b71606272 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -626,13 +626,23 @@ func shouldReopen(issue *Issue, stacks map[string]map[Info]int64) bool { if !ok { return false } - // TODO(jba?): handle other programs - if issueProgram != "gopls" { - return false + + matchProgram := func(infoProg string) bool { + switch issueProgram { + case "gopls": + return path.Base(infoProg) == issueProgram + case "go": + // At present, we only care about compiler stacks. + // Issues should have milestones like "Go1.24". + return infoProg == "cmd/compile" + default: + return false + } } + for _, stack := range issue.newStacks { for info := range stacks[stack] { - if path.Base(info.Program) == issueProgram && semver.Compare(info.ProgramVersion, issueVersion) >= 0 { + if matchProgram(info.Program) && semver.Compare(semVer(info.ProgramVersion), issueVersion) >= 0 { log.Printf("reopening issue #%d: purportedly fixed in %s@%s, but found a new stack from version %s", issue.Number, issueProgram, issueVersion, info.ProgramVersion) return true @@ -647,12 +657,23 @@ func (i *Issue) isFixed() bool { return i.State == "closed" && i.StateReason == "completed" } -// parseMilestone parses a the title of a GitHub milestone that is in the format -// PROGRAM/VERSION. For example, "gopls/v0.17.0". +// parseMilestone parses a the title of a GitHub milestone. +// If it is in the format PROGRAM/VERSION (for example, "gopls/v0.17.0"), +// then it returns PROGRAM and VERSION. +// If it is in the format Go1.X, then it returns "go" as the program and +// "v1.X" or "v1.X.0" as the version. +// Otherwise, the last return value is false. func parseMilestone(m *Milestone) (program, version string, ok bool) { if m == nil { return "", "", false } + if strings.HasPrefix(m.Title, "Go") { + v := semVer(m.Title) + if !semver.IsValid(v) { + return "", "", false + } + return "go", v, true + } program, version, ok = morestrings.CutLast(m.Title, "/") if !ok || program == "" || version == "" || version[0] != 'v' { return "", "", false @@ -660,6 +681,22 @@ func parseMilestone(m *Milestone) (program, version string, ok bool) { return program, version, true } +// semVer returns a semantic version for its argument, which may already be +// a semantic version, or may be a Go version. +// +// v1.2.3 => v1.2.3 +// go1.24 => v1.24 +// Go1.23.5 => v1.23.5 +// goHome => vHome +// +// It returns "", false if the go version is in the wrong format. +func semVer(v string) string { + if strings.HasPrefix(v, "go") || strings.HasPrefix(v, "Go") { + return "v" + v[2:] + } + return v +} + // stackID returns a 32-bit identifier for a stack // suitable for use in GitHub issue titles. func stackID(stack string) string { diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index 1f3cbef1f29..452113a1581 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -269,7 +269,8 @@ func TestMarshalUpdateIssueFields(t *testing.T) { func TestShouldReopen(t *testing.T) { const stack = "stack" const gopls = "golang.org/x/tools/gopls" - const milestoneVersion = "v0.2.0" + goplsMilestone := &Milestone{Title: "gopls/v0.2.0"} + goMilestone := &Milestone{Title: "Go1.23"} for _, tc := range []struct { name string @@ -279,44 +280,61 @@ func TestShouldReopen(t *testing.T) { }{ { "issue open", - Issue{State: "open"}, + Issue{State: "open", Milestone: goplsMilestone}, Info{Program: gopls, ProgramVersion: "v0.2.0"}, false, }, { "issue closed but not fixed", - Issue{State: "closed", StateReason: "not_planned"}, + Issue{State: "closed", StateReason: "not_planned", Milestone: goplsMilestone}, Info{Program: gopls, ProgramVersion: "v0.2.0"}, false, }, { "different program", - Issue{State: "closed", StateReason: "completed"}, + Issue{State: "closed", StateReason: "completed", Milestone: goplsMilestone}, Info{Program: "other", ProgramVersion: "v0.2.0"}, false, }, { "later version", - Issue{State: "closed", StateReason: "completed"}, + Issue{State: "closed", StateReason: "completed", Milestone: goplsMilestone}, Info{Program: gopls, ProgramVersion: "v0.3.0"}, true, }, { "earlier version", - Issue{State: "closed", StateReason: "completed"}, + Issue{State: "closed", StateReason: "completed", Milestone: goplsMilestone}, Info{Program: gopls, ProgramVersion: "v0.1.0"}, false, }, { "same version", - Issue{State: "closed", StateReason: "completed"}, + Issue{State: "closed", StateReason: "completed", Milestone: goplsMilestone}, Info{Program: gopls, ProgramVersion: "v0.2.0"}, true, }, + { + "compiler later version", + Issue{State: "closed", StateReason: "completed", Milestone: goMilestone}, + Info{Program: "cmd/compile", ProgramVersion: "go1.24"}, + true, + }, + { + "compiler earlier version", + Issue{State: "closed", StateReason: "completed", Milestone: goMilestone}, + Info{Program: "cmd/compile", ProgramVersion: "go1.22"}, + false, + }, + { + "compiler same version", + Issue{State: "closed", StateReason: "completed", Milestone: goMilestone}, + Info{Program: "cmd/compile", ProgramVersion: "go1.23"}, + true, + }, } { t.Run(tc.name, func(t *testing.T) { tc.issue.Number = 1 - tc.issue.Milestone = &Milestone{Title: "gopls/" + milestoneVersion} tc.issue.newStacks = []string{stack} got := shouldReopen(&tc.issue, map[string]map[Info]int64{stack: map[Info]int64{tc.info: 1}}) if got != tc.want { From edafbe5df5df9f7b3127d458e50a8651111f0569 Mon Sep 17 00:00:00 2001 From: Merrick Clay Date: Fri, 24 Jan 2025 23:26:31 -0700 Subject: [PATCH 069/309] tools: fix typos in docs and comments Change-Id: I24d372c18122805b14c4f1214a7e18345397af9e Reviewed-on: https://go-review.googlesource.com/c/tools/+/644136 Reviewed-by: Alan Donovan Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Auto-Submit: Ian Lance Taylor Commit-Queue: Ian Lance Taylor --- cmd/file2fuzz/main.go | 2 +- go/analysis/passes/pkgfact/pkgfact.go | 2 +- go/analysis/passes/unreachable/doc.go | 2 +- go/callgraph/vta/propagation_test.go | 2 +- go/expect/extract.go | 2 +- go/loader/loader_test.go | 2 +- go/packages/packagestest/expect.go | 2 +- go/ssa/builder_test.go | 2 +- go/ssa/emit.go | 2 +- gopls/doc/analyzers.md | 2 +- gopls/doc/generate/generate.go | 2 +- gopls/internal/cache/diagnostics.go | 2 +- gopls/internal/doc/api.json | 4 ++-- gopls/internal/server/rename.go | 2 +- gopls/internal/test/integration/bench/repo_test.go | 2 +- gopls/internal/vulncheck/vulntest/report.go | 2 +- internal/event/export/metric/info.go | 2 +- internal/expect/extract.go | 2 +- internal/gocommand/invoke.go | 2 +- internal/jsonrpc2/messages.go | 2 +- internal/jsonrpc2/serve.go | 2 +- internal/packagestest/expect.go | 2 +- internal/typesinternal/errorcode.go | 2 +- playground/socket/socket.go | 2 +- 24 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/file2fuzz/main.go b/cmd/file2fuzz/main.go index c2b7ee52089..2a86c2ece88 100644 --- a/cmd/file2fuzz/main.go +++ b/cmd/file2fuzz/main.go @@ -13,7 +13,7 @@ // output to stdout. If any position arguments are provided stdin is ignored // and the arguments are assumed to be input files to convert. // -// The -o flag provides an path to write output files to. If only one positional +// The -o flag provides a path to write output files to. If only one positional // argument is specified it may be a file path or an existing directory, if there are // multiple inputs specified it must be a directory. If a directory is provided // the name of the file will be the SHA-256 hash of its contents. diff --git a/go/analysis/passes/pkgfact/pkgfact.go b/go/analysis/passes/pkgfact/pkgfact.go index 4bf33d45f50..077c8780815 100644 --- a/go/analysis/passes/pkgfact/pkgfact.go +++ b/go/analysis/passes/pkgfact/pkgfact.go @@ -45,7 +45,7 @@ var Analyzer = &analysis.Analyzer{ } // A pairsFact is a package-level fact that records -// an set of key=value strings accumulated from constant +// a set of key=value strings accumulated from constant // declarations in this package and its dependencies. // Elements are ordered by keys, which are unique. type pairsFact []string diff --git a/go/analysis/passes/unreachable/doc.go b/go/analysis/passes/unreachable/doc.go index d17d0d9444e..325a15358d5 100644 --- a/go/analysis/passes/unreachable/doc.go +++ b/go/analysis/passes/unreachable/doc.go @@ -9,6 +9,6 @@ // unreachable: check for unreachable code // // The unreachable analyzer finds statements that execution can never reach -// because they are preceded by an return statement, a call to panic, an +// because they are preceded by a return statement, a call to panic, an // infinite loop, or similar constructs. package unreachable diff --git a/go/callgraph/vta/propagation_test.go b/go/callgraph/vta/propagation_test.go index 1a274f38f84..492258f81e3 100644 --- a/go/callgraph/vta/propagation_test.go +++ b/go/callgraph/vta/propagation_test.go @@ -336,7 +336,7 @@ func TestPropagation(t *testing.T) { "Local(t2)": "A;B;C", }, }, - // The outer loop of subsumed-scc pushes A an B through the graph. + // The outer loop of subsumed-scc pushes A and B through the graph. {name: "subsumed-scc", graph: suite["subsumed-scc"], want: map[string]string{ "Local(t0)": "A;B", diff --git a/go/expect/extract.go b/go/expect/extract.go index 1ca67d24958..902b1e806e4 100644 --- a/go/expect/extract.go +++ b/go/expect/extract.go @@ -21,7 +21,7 @@ import ( const commentStart = "@" const commentStartLen = len(commentStart) -// Identifier is the type for an identifier in an Note argument list. +// Identifier is the type for an identifier in a Note argument list. type Identifier string // Parse collects all the notes present in a file. diff --git a/go/loader/loader_test.go b/go/loader/loader_test.go index 4729ba34559..2276b49ad6f 100644 --- a/go/loader/loader_test.go +++ b/go/loader/loader_test.go @@ -558,7 +558,7 @@ func TestVendorCwdIssue16580(t *testing.T) { // - TypeCheckFuncBodies hook func TestTransitivelyErrorFreeFlag(t *testing.T) { - // Create an minimal custom build.Context + // Create a minimal custom build.Context // that fakes the following packages: // // a --> b --> c! c has an error diff --git a/go/packages/packagestest/expect.go b/go/packages/packagestest/expect.go index 14a6446138f..dc41894a6ed 100644 --- a/go/packages/packagestest/expect.go +++ b/go/packages/packagestest/expect.go @@ -411,7 +411,7 @@ func (e *Exported) rangeConverter(n *expect.Note, args []interface{}) (Range, [] eof := tokFile.Pos(tokFile.Size()) return newRange(tokFile, eof, eof), args, nil default: - // look up an marker by name + // look up a marker by name mark, ok := e.markers[string(arg)] if !ok { return Range{}, nil, fmt.Errorf("cannot find marker %v", arg) diff --git a/go/ssa/builder_test.go b/go/ssa/builder_test.go index 59d8a91ea6a..2589cc82bb6 100644 --- a/go/ssa/builder_test.go +++ b/go/ssa/builder_test.go @@ -214,7 +214,7 @@ func TestRuntimeTypes(t *testing.T) { input string want []string }{ - // An package-level type is needed. + // A package-level type is needed. {`package A; type T struct{}; func (T) f() {}; var x any = T{}`, []string{"*p.T", "p.T"}, }, diff --git a/go/ssa/emit.go b/go/ssa/emit.go index edd2ced3034..a3d41ad95a4 100644 --- a/go/ssa/emit.go +++ b/go/ssa/emit.go @@ -18,7 +18,7 @@ import ( // emitAlloc emits to f a new Alloc instruction allocating a variable // of type typ. // -// The caller must set Alloc.Heap=true (for an heap-allocated variable) +// The caller must set Alloc.Heap=true (for a heap-allocated variable) // or add the Alloc to f.Locals (for a frame-allocated variable). // // During building, a variable in f.Locals may have its Heap flag diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 04b91400c92..aa0081df9d0 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -912,7 +912,7 @@ Package documentation: [unmarshal](https://pkg.go.dev/golang.org/x/tools/go/anal The unreachable analyzer finds statements that execution can never reach -because they are preceded by an return statement, a call to panic, an +because they are preceded by a return statement, a call to panic, an infinite loop, or similar constructs. Default: on. diff --git a/gopls/doc/generate/generate.go b/gopls/doc/generate/generate.go index 42d41bbb1b6..b0d3e8c49f6 100644 --- a/gopls/doc/generate/generate.go +++ b/gopls/doc/generate/generate.go @@ -414,7 +414,7 @@ func formatDefault(reflectField reflect.Value) (string, error) { return string(defBytes), err } -// valueDoc transforms a docstring documenting an constant identifier to a +// valueDoc transforms a docstring documenting a constant identifier to a // docstring documenting its value. // // If doc is of the form "Foo is a bar", it returns '`"fooValue"` is a bar'. If diff --git a/gopls/internal/cache/diagnostics.go b/gopls/internal/cache/diagnostics.go index 68c1632594f..d43c2f395dd 100644 --- a/gopls/internal/cache/diagnostics.go +++ b/gopls/internal/cache/diagnostics.go @@ -31,7 +31,7 @@ type InitializationError struct { func byURI(d *Diagnostic) protocol.DocumentURI { return d.URI } // For use in maps.Group. -// An Diagnostic corresponds to an LSP Diagnostic. +// A Diagnostic corresponds to an LSP Diagnostic. // https://microsoft.github.io/language-server-protocol/specification#diagnostic // // It is (effectively) gob-serializable; see {encode,decode}Diagnostics. diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 0ae6103c8db..043907227a3 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -615,7 +615,7 @@ }, { "Name": "\"unreachable\"", - "Doc": "check for unreachable code\n\nThe unreachable analyzer finds statements that execution can never reach\nbecause they are preceded by an return statement, a call to panic, an\ninfinite loop, or similar constructs.", + "Doc": "check for unreachable code\n\nThe unreachable analyzer finds statements that execution can never reach\nbecause they are preceded by a return statement, a call to panic, an\ninfinite loop, or similar constructs.", "Default": "true" }, { @@ -1310,7 +1310,7 @@ }, { "Name": "unreachable", - "Doc": "check for unreachable code\n\nThe unreachable analyzer finds statements that execution can never reach\nbecause they are preceded by an return statement, a call to panic, an\ninfinite loop, or similar constructs.", + "Doc": "check for unreachable code\n\nThe unreachable analyzer finds statements that execution can never reach\nbecause they are preceded by a return statement, a call to panic, an\ninfinite loop, or similar constructs.", "URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/unreachable", "Default": true }, diff --git a/gopls/internal/server/rename.go b/gopls/internal/server/rename.go index cdfb9c7a8fe..b6fac8ba219 100644 --- a/gopls/internal/server/rename.go +++ b/gopls/internal/server/rename.go @@ -31,7 +31,7 @@ func (s *server) Rename(ctx context.Context, params *protocol.RenameParams) (*pr } // Because we don't handle directory renaming within golang.Rename, golang.Rename returns - // boolean value isPkgRenaming to determine whether an DocumentChanges of type RenameFile should + // boolean value isPkgRenaming to determine whether any DocumentChanges of type RenameFile should // be added to the return protocol.WorkspaceEdit value. edits, isPkgRenaming, err := golang.Rename(ctx, snapshot, fh, params.Position, params.NewName) if err != nil { diff --git a/gopls/internal/test/integration/bench/repo_test.go b/gopls/internal/test/integration/bench/repo_test.go index 0e86f3e1da7..50370e73491 100644 --- a/gopls/internal/test/integration/bench/repo_test.go +++ b/gopls/internal/test/integration/bench/repo_test.go @@ -147,7 +147,7 @@ type repo struct { // reusableDir return a reusable directory for benchmarking, or "". // // If the user specifies a directory, the test will create and populate it -// on the first run an re-use it on subsequent runs. Otherwise it will +// on the first run and re-use it on subsequent runs. Otherwise it will // create, populate, and delete a temporary directory. func (r *repo) reusableDir() string { if r.inDir == nil { diff --git a/gopls/internal/vulncheck/vulntest/report.go b/gopls/internal/vulncheck/vulntest/report.go index 7dbebca6d6b..6aa87221866 100644 --- a/gopls/internal/vulncheck/vulntest/report.go +++ b/gopls/internal/vulncheck/vulntest/report.go @@ -104,7 +104,7 @@ type Package struct { DerivedSymbols []string `yaml:"derived_symbols,omitempty"` } -// Version is an SemVer 2.0.0 semantic version with no leading "v" prefix, +// Version is a SemVer 2.0.0 semantic version with no leading "v" prefix, // as used by OSV. type Version string diff --git a/internal/event/export/metric/info.go b/internal/event/export/metric/info.go index a178343b2ef..5662fbeaef6 100644 --- a/internal/event/export/metric/info.go +++ b/internal/event/export/metric/info.go @@ -31,7 +31,7 @@ type HistogramInt64 struct { Buckets []int64 } -// HistogramFloat64 represents the construction information for an float64 histogram metric. +// HistogramFloat64 represents the construction information for a float64 histogram metric. type HistogramFloat64 struct { // Name is the unique name of this metric. Name string diff --git a/internal/expect/extract.go b/internal/expect/extract.go index db6b66aaf21..1fb4349c48e 100644 --- a/internal/expect/extract.go +++ b/internal/expect/extract.go @@ -21,7 +21,7 @@ import ( const commentStart = "@" const commentStartLen = len(commentStart) -// Identifier is the type for an identifier in an Note argument list. +// Identifier is the type for an identifier in a Note argument list. type Identifier string // Parse collects all the notes present in a file. diff --git a/internal/gocommand/invoke.go b/internal/gocommand/invoke.go index 5db1ed6fe1a..7ea9013447b 100644 --- a/internal/gocommand/invoke.go +++ b/internal/gocommand/invoke.go @@ -28,7 +28,7 @@ import ( "golang.org/x/tools/internal/event/label" ) -// An Runner will run go command invocations and serialize +// A Runner will run go command invocations and serialize // them if it sees a concurrency error. type Runner struct { // once guards the runner initialization. diff --git a/internal/jsonrpc2/messages.go b/internal/jsonrpc2/messages.go index 721168fd4f2..e87d772f398 100644 --- a/internal/jsonrpc2/messages.go +++ b/internal/jsonrpc2/messages.go @@ -27,7 +27,7 @@ type Request interface { Message // Method is a string containing the method name to invoke. Method() string - // Params is an JSON value (object, array, null, or "") with the parameters of the method. + // Params is a JSON value (object, array, null, or "") with the parameters of the method. Params() json.RawMessage // isJSONRPC2Request is used to make the set of request implementations closed. isJSONRPC2Request() diff --git a/internal/jsonrpc2/serve.go b/internal/jsonrpc2/serve.go index cfbcbcb021c..76df52cd43b 100644 --- a/internal/jsonrpc2/serve.go +++ b/internal/jsonrpc2/serve.go @@ -46,7 +46,7 @@ func HandlerServer(h Handler) StreamServer { }) } -// ListenAndServe starts an jsonrpc2 server on the given address. If +// ListenAndServe starts a jsonrpc2 server on the given address. If // idleTimeout is non-zero, ListenAndServe exits after there are no clients for // this duration, otherwise it exits only on error. func ListenAndServe(ctx context.Context, network, addr string, server StreamServer, idleTimeout time.Duration) error { diff --git a/internal/packagestest/expect.go b/internal/packagestest/expect.go index 053d8e8a9db..e3e3509579d 100644 --- a/internal/packagestest/expect.go +++ b/internal/packagestest/expect.go @@ -411,7 +411,7 @@ func (e *Exported) rangeConverter(n *expect.Note, args []interface{}) (Range, [] eof := tokFile.Pos(tokFile.Size()) return newRange(tokFile, eof, eof), args, nil default: - // look up an marker by name + // look up a marker by name mark, ok := e.markers[string(arg)] if !ok { return Range{}, nil, fmt.Errorf("cannot find marker %v", arg) diff --git a/internal/typesinternal/errorcode.go b/internal/typesinternal/errorcode.go index 131caab2847..235a6defc4c 100644 --- a/internal/typesinternal/errorcode.go +++ b/internal/typesinternal/errorcode.go @@ -966,7 +966,7 @@ const ( // var _ = string(x) InvalidConversion - // InvalidUntypedConversion occurs when an there is no valid implicit + // InvalidUntypedConversion occurs when there is no valid implicit // conversion from an untyped value satisfying the type constraints of the // context in which it is used. // diff --git a/playground/socket/socket.go b/playground/socket/socket.go index 797dcc6dd4c..9e5b4a954d2 100644 --- a/playground/socket/socket.go +++ b/playground/socket/socket.go @@ -5,7 +5,7 @@ //go:build !appengine // +build !appengine -// Package socket implements an WebSocket-based playground backend. +// Package socket implements a WebSocket-based playground backend. // Clients connect to a websocket handler and send run/kill commands, and // the server sends the output and exit status of the running processes. // Multiple clients running multiple processes may be served concurrently. From bb0a9cda62f3c25b9f311456ce78804b86ce4214 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Sat, 25 Jan 2025 14:56:04 -0500 Subject: [PATCH 070/309] gopls: remove go.sum files from integration tests Explicit go.sum files in the txtar data for tests makes it harder to modify the tests. This CL replaces the ones in the gopls integration tests with calls to WriteGoSum in test setup. And as part of modernization, a few 'interface{}'s have been replaced with 'any's. Change-Id: I951fa6d7b2ed780df68f4bc0d043f0738612da28 Reviewed-on: https://go-review.googlesource.com/c/tools/+/644335 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../test/integration/codelens/codelens_test.go | 14 +++++--------- .../integration/completion/completion_test.go | 11 ++++------- .../integration/diagnostics/diagnostics_test.go | 8 ++------ .../test/integration/misc/configuration_test.go | 2 +- .../test/integration/misc/definition_test.go | 5 +---- .../test/integration/misc/highlight_test.go | 4 +--- .../test/integration/misc/references_test.go | 5 +---- .../test/integration/misc/vendor_test.go | 8 ++------ .../internal/test/integration/misc/vuln_test.go | 14 ++------------ .../test/integration/modfile/modfile_test.go | 16 +--------------- .../test/integration/watch/setting_test.go | 2 +- .../test/integration/watch/watch_test.go | 7 +++---- .../test/integration/workspace/metadata_test.go | 4 +--- .../integration/workspace/standalone_test.go | 2 +- .../test/integration/workspace/vendor_test.go | 5 +---- .../test/integration/workspace/workspace_test.go | 4 +--- 16 files changed, 28 insertions(+), 83 deletions(-) diff --git a/gopls/internal/test/integration/codelens/codelens_test.go b/gopls/internal/test/integration/codelens/codelens_test.go index bb8ad95ee19..c1f2c524232 100644 --- a/gopls/internal/test/integration/codelens/codelens_test.go +++ b/gopls/internal/test/integration/codelens/codelens_test.go @@ -261,9 +261,6 @@ module mod.com/a go 1.22 require golang.org/x/hello v1.2.3 --- go.sum -- -golang.org/x/hello v1.2.3 h1:7Wesfkx/uBd+eFgPrq0irYj/1XfmbvLV8jZ/W7C2Dwg= -golang.org/x/hello v1.2.3/go.mod h1:OgtlzsxVMUUdsdQCIDYgaauCTH47B8T8vofouNJfzgY= -- main.go -- package main @@ -282,6 +279,7 @@ require golang.org/x/hello v1.3.3 ` WithOptions( + WriteGoSum("."), ProxyFiles(proxyWithLatest), ).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) { env.RunGoCommand("mod", "vendor") @@ -335,11 +333,6 @@ require golang.org/x/hello v1.0.0 require golang.org/x/unused v1.0.0 // EOF --- go.sum -- -golang.org/x/hello v1.0.0 h1:qbzE1/qT0/zojAMd/JcPsO2Vb9K4Bkeyq0vB2JGMmsw= -golang.org/x/hello v1.0.0/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco= -golang.org/x/unused v1.0.0 h1:LecSbCn5P3vTcxubungSt1Pn4D/WocCaiWOPDC0y0rw= -golang.org/x/unused v1.0.0/go.mod h1:ihoW8SgWzugwwj0N2SfLfPZCxTB1QOVfhMfB5PWTQ8U= -- main.go -- package main @@ -349,7 +342,10 @@ func main() { _ = hi.Goodbye } ` - WithOptions(ProxyFiles(proxy)).Run(t, shouldRemoveDep, func(t *testing.T, env *Env) { + WithOptions( + WriteGoSum("."), + ProxyFiles(proxy), + ).Run(t, shouldRemoveDep, func(t *testing.T, env *Env) { env.OpenFile("go.mod") env.RegexpReplace("go.mod", "// EOF", "// EOF unsaved edit") // unsaved edits ok env.ExecuteCodeLensCommand("go.mod", command.Tidy, nil) diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index fe6a367e71b..1d293fe9019 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go @@ -276,9 +276,6 @@ module mod.com go 1.14 require example.com v1.2.3 --- go.sum -- -example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -- main.go -- package main @@ -295,6 +292,7 @@ func _() { } ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), ).Run(t, mod, func(t *testing.T, env *Env) { // Make sure the dependency is in the module cache and accessible for @@ -347,9 +345,6 @@ module mod.com go 1.14 require example.com v1.2.3 --- go.sum -- -example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -- useblah.go -- // +build hidden @@ -361,7 +356,9 @@ package mainmod const Name = "mainmod" ` - WithOptions(ProxyFiles(proxy)).Run(t, files, func(t *testing.T, env *Env) { + WithOptions( + WriteGoSum("."), + ProxyFiles(proxy)).Run(t, files, func(t *testing.T, env *Env) { env.CreateBuffer("import.go", "package pkg\nvar _ = mainmod.Name\n") env.SaveBuffer("import.go") content := env.ReadWorkspaceFile("import.go") diff --git a/gopls/internal/test/integration/diagnostics/diagnostics_test.go b/gopls/internal/test/integration/diagnostics/diagnostics_test.go index 9e6c504cc86..c496f6464a3 100644 --- a/gopls/internal/test/integration/diagnostics/diagnostics_test.go +++ b/gopls/internal/test/integration/diagnostics/diagnostics_test.go @@ -421,9 +421,6 @@ module mod.com go 1.12 require foo.test v1.2.3 --- go.sum -- -foo.test v1.2.3 h1:TMA+lyd1ck0TqjSFpNe4T6cf/K6TYkoHwOOcMBMjaEw= -foo.test v1.2.3/go.mod h1:Ij3kyLIe5lzjycjh13NL8I2gX0quZuTdW0MnmlwGBL4= -- print.go -- package lib @@ -451,6 +448,7 @@ const Answer = 42 func TestResolveDiagnosticWithDownload(t *testing.T) { WithOptions( + WriteGoSum("."), ProxyFiles(testPackageWithRequireProxy), ).Run(t, testPackageWithRequire, func(t *testing.T, env *Env) { env.OpenFile("print.go") @@ -1753,9 +1751,6 @@ module mod.com go 1.12 require nested.com v1.0.0 --- go.sum -- -nested.com v1.0.0 h1:I6spLE4CgFqMdBPc+wTV2asDO2QJ3tU0YAT+jkLeN1I= -nested.com v1.0.0/go.mod h1:ly53UzXQgVjSlV7wicdBB4p8BxfytuGT1Xcyv0ReJfI= -- main.go -- package main @@ -1779,6 +1774,7 @@ package hello func helloHelper() {} ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), Modes(Default), ).Run(t, nested, func(t *testing.T, env *Env) { diff --git a/gopls/internal/test/integration/misc/configuration_test.go b/gopls/internal/test/integration/misc/configuration_test.go index 1077c21ac36..6d588a7d3da 100644 --- a/gopls/internal/test/integration/misc/configuration_test.go +++ b/gopls/internal/test/integration/misc/configuration_test.go @@ -186,7 +186,7 @@ var ErrFoo = errors.New("foo") cfg.Env = map[string]string{ "AN_ARBITRARY_VAR": "FOO", } - cfg.Settings = map[string]interface{}{ + cfg.Settings = map[string]any{ "staticcheck": true, } env.ChangeConfiguration(cfg) diff --git a/gopls/internal/test/integration/misc/definition_test.go b/gopls/internal/test/integration/misc/definition_test.go index 95054977e14..d36bb024672 100644 --- a/gopls/internal/test/integration/misc/definition_test.go +++ b/gopls/internal/test/integration/misc/definition_test.go @@ -466,10 +466,6 @@ module example.com/a go 1.14 require other.com/b v1.0.0 --- go.sum -- -other.com/b v1.0.0 h1:1wb3PMGdet5ojzrKl+0iNksRLnOM9Jw+7amBNqmYwqk= -other.com/b v1.0.0/go.mod h1:TgHQFucl04oGT+vrUm/liAzukYHNxCwKNkQZEyn3m9g= - -- a.go -- package a import "other.com/b" @@ -477,6 +473,7 @@ const _ = b.K ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), Modes(Default), // fails in 'experimental' mode ).Run(t, src, func(t *testing.T, env *Env) { diff --git a/gopls/internal/test/integration/misc/highlight_test.go b/gopls/internal/test/integration/misc/highlight_test.go index 9e3dd980464..e4da558e5d0 100644 --- a/gopls/internal/test/integration/misc/highlight_test.go +++ b/gopls/internal/test/integration/misc/highlight_test.go @@ -95,9 +95,6 @@ module mod.com go 1.12 require example.com v1.2.3 --- go.sum -- -example.com v1.2.3 h1:WFzrgiQJwEDJNLDUOV1f9qlasQkvzXf2UNLaNIqbWsI= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -- main.go -- package main @@ -110,6 +107,7 @@ func main() {}` WithOptions( ProxyFiles(proxy), + WriteGoSum("."), ).Run(t, mod, func(t *testing.T, env *Env) { env.OpenFile("main.go") diff --git a/gopls/internal/test/integration/misc/references_test.go b/gopls/internal/test/integration/misc/references_test.go index 73e4fffe3b8..e84dcd71dc3 100644 --- a/gopls/internal/test/integration/misc/references_test.go +++ b/gopls/internal/test/integration/misc/references_test.go @@ -376,10 +376,6 @@ module example.com/a go 1.14 require other.com/b v1.0.0 --- go.sum -- -other.com/b v1.0.0 h1:9WyCKS+BLAMRQM0CegP6zqP2beP+ShTbPaARpNY31II= -other.com/b v1.0.0/go.mod h1:TgHQFucl04oGT+vrUm/liAzukYHNxCwKNkQZEyn3m9g= - -- a.go -- package a import "other.com/b" @@ -388,6 +384,7 @@ var _ b.B ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), Modes(Default), // fails in 'experimental' mode ).Run(t, src, func(t *testing.T, env *Env) { diff --git a/gopls/internal/test/integration/misc/vendor_test.go b/gopls/internal/test/integration/misc/vendor_test.go index f3bed9082b7..6606772737e 100644 --- a/gopls/internal/test/integration/misc/vendor_test.go +++ b/gopls/internal/test/integration/misc/vendor_test.go @@ -31,9 +31,6 @@ module mod.com go 1.14 require golang.org/x/hello v1.2.3 --- go.sum -- -golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho= -golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco= -- vendor/modules.txt -- -- a/a1.go -- package a @@ -48,6 +45,7 @@ func _() { WithOptions( Modes(Default), ProxyFiles(basicProxy), + WriteGoSum("."), ).Run(t, pkgThatUsesVendoring, func(t *testing.T, env *Env) { env.OpenFile("a/a1.go") d := &protocol.PublishDiagnosticsParams{} @@ -71,9 +69,6 @@ module mod.com go 1.14 require golang.org/x/hello v1.2.3 --- go.sum -- -golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho= -golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco= -- main.go -- package main @@ -86,6 +81,7 @@ func main() { WithOptions( Modes(Default), ProxyFiles(basicProxy), + WriteGoSum("."), ).Run(t, src, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.AfterChange(NoDiagnostics()) diff --git a/gopls/internal/test/integration/misc/vuln_test.go b/gopls/internal/test/integration/misc/vuln_test.go index 9f6061c43d9..9dad13179af 100644 --- a/gopls/internal/test/integration/misc/vuln_test.go +++ b/gopls/internal/test/integration/misc/vuln_test.go @@ -368,13 +368,6 @@ require ( golang.org/amod v1.0.0 // indirect golang.org/bmod v0.5.0 // indirect ) --- go.sum -- -golang.org/amod v1.0.0 h1:EUQOI2m5NhQZijXZf8WimSnnWubaFNrrKUH/PopTN8k= -golang.org/amod v1.0.0/go.mod h1:yvny5/2OtYFomKt8ax+WJGvN6pfN1pqjGnn7DQLUi6E= -golang.org/bmod v0.5.0 h1:KgvUulMyMiYRB7suKA0x+DfWRVdeyPgVJvcishTH+ng= -golang.org/bmod v0.5.0/go.mod h1:f6o+OhF66nz/0BBc/sbCsshyPRKMSxZIlG50B/bsM4c= -golang.org/cmod v1.1.3 h1:PJ7rZFTk7xGAunBRDa0wDe7rZjZ9R/vr1S2QkVVCngQ= -golang.org/cmod v1.1.3/go.mod h1:eCR8dnmvLYQomdeAZRCPgS5JJihXtqOQrpEkNj5feQA= -- x/x.go -- package x @@ -497,7 +490,7 @@ func vulnTestEnv(proxyData string) (*vulntest.DB, []RunOption, error) { "_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", // needed to run `gopls vulncheck`. "GOSUMDB": "off", } - return db, []RunOption{ProxyFiles(proxyData), ev, settings}, nil + return db, []RunOption{ProxyFiles(proxyData), ev, settings, WriteGoSum(".")}, nil } func TestRunVulncheckPackageDiagnostics(t *testing.T) { @@ -675,7 +668,7 @@ func TestRunGovulncheck_Expiry(t *testing.T) { }) } -func stringify(a interface{}) string { +func stringify(a any) string { data, _ := json.Marshal(a) return string(data) } @@ -814,9 +807,6 @@ go 1.18 require golang.org/bmod v0.5.0 --- go.sum -- -golang.org/bmod v0.5.0 h1:MT/ysNRGbCiURc5qThRFWaZ5+rK3pQRPo9w7dYZfMDk= -golang.org/bmod v0.5.0/go.mod h1:k+zl+Ucu4yLIjndMIuWzD/MnOHy06wqr3rD++y0abVs= -- x/x.go -- package x diff --git a/gopls/internal/test/integration/modfile/modfile_test.go b/gopls/internal/test/integration/modfile/modfile_test.go index 243bb04e960..5a194246a42 100644 --- a/gopls/internal/test/integration/modfile/modfile_test.go +++ b/gopls/internal/test/integration/modfile/modfile_test.go @@ -808,7 +808,6 @@ go 1.12 require ( example.com v1.2.3 ) --- go.sum -- -- main.go -- package main @@ -918,11 +917,6 @@ module mod.com go 1.12 require hasdep.com v1.2.3 --- go.sum -- -example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -hasdep.com v1.2.3 h1:00y+N5oD+SpKoqV1zP2VOPawcW65Zb9NebANY3GSzGI= -hasdep.com v1.2.3/go.mod h1:ePVZOlez+KZEOejfLPGL2n4i8qiAjrkhQZ4wcImqAes= -- main.go -- package main @@ -957,19 +951,13 @@ go 1.12 require hasdep.com v1.2.3 require random.com v1.2.3 --- go.sum -- -example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -hasdep.com v1.2.3 h1:00y+N5oD+SpKoqV1zP2VOPawcW65Zb9NebANY3GSzGI= -hasdep.com v1.2.3/go.mod h1:ePVZOlez+KZEOejfLPGL2n4i8qiAjrkhQZ4wcImqAes= -random.com v1.2.3 h1:PzYTykzqqH6+qU0dIgh9iPFbfb4Mm8zNBjWWreRKtx0= -random.com v1.2.3/go.mod h1:8EGj+8a4Hw1clAp8vbaeHAsKE4sbm536FP7nKyXO+qQ= -- main.go -- package main func main() {} ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), ).Run(t, mod, func(t *testing.T, env *Env) { d := &protocol.PublishDiagnosticsParams{} @@ -1010,7 +998,6 @@ go 1.12 require ( example.com v1.2.3 ) --- go.sum -- -- main.go -- package main @@ -1078,7 +1065,6 @@ func Goodbye() { module mod.com go 1.12 --- go.sum -- -- main.go -- package main diff --git a/gopls/internal/test/integration/watch/setting_test.go b/gopls/internal/test/integration/watch/setting_test.go index abd9799c584..2a825a5b937 100644 --- a/gopls/internal/test/integration/watch/setting_test.go +++ b/gopls/internal/test/integration/watch/setting_test.go @@ -60,7 +60,7 @@ package subdir // use (true|false) or some other truthy value. func TestSubdirWatchPatterns_BadValues(t *testing.T) { tests := []struct { - badValue interface{} + badValue any wantMessage string }{ {true, "invalid type bool (want string)"}, diff --git a/gopls/internal/test/integration/watch/watch_test.go b/gopls/internal/test/integration/watch/watch_test.go index 3fb1ab546a6..340ceb5ebf7 100644 --- a/gopls/internal/test/integration/watch/watch_test.go +++ b/gopls/internal/test/integration/watch/watch_test.go @@ -525,9 +525,6 @@ module mod.com go 1.12 require example.com v1.2.2 --- go.sum -- -example.com v1.2.3 h1:OnPPkx+rW63kj9pgILsu12MORKhSlnFa3DVRJq1HZ7g= -example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= -- main.go -- package main @@ -537,7 +534,9 @@ func main() { blah.X() } ` - WithOptions(ProxyFiles(proxy)).Run(t, mod, func(t *testing.T, env *Env) { + WithOptions( + WriteGoSum("."), + ProxyFiles(proxy)).Run(t, mod, func(t *testing.T, env *Env) { env.WriteWorkspaceFiles(map[string]string{ "go.mod": `module mod.com diff --git a/gopls/internal/test/integration/workspace/metadata_test.go b/gopls/internal/test/integration/workspace/metadata_test.go index 59dfec3ad97..71ca4329777 100644 --- a/gopls/internal/test/integration/workspace/metadata_test.go +++ b/gopls/internal/test/integration/workspace/metadata_test.go @@ -217,9 +217,6 @@ module b.com/nested go 1.18 require b.com/other v1.4.6 --- go.sum -- -b.com/other v1.4.6 h1:pHXSzGsk6DamYXp9uRdDB9A/ZQqAN9it+JudU0sBf94= -b.com/other v1.4.6/go.mod h1:T0TYuGdAHw4p/l0+1P/yhhYHfZRia7PaadNVDu58OWM= -- nested.go -- package nested @@ -228,6 +225,7 @@ import "b.com/other/foo" const C = foo.Foo ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), ).Run(t, files, func(t *testing.T, env *Env) { env.OnceMet( diff --git a/gopls/internal/test/integration/workspace/standalone_test.go b/gopls/internal/test/integration/workspace/standalone_test.go index d837899f7fb..3b690465744 100644 --- a/gopls/internal/test/integration/workspace/standalone_test.go +++ b/gopls/internal/test/integration/workspace/standalone_test.go @@ -194,7 +194,7 @@ func main() {} ) cfg := env.Editor.Config() - cfg.Settings = map[string]interface{}{ + cfg.Settings = map[string]any{ "standaloneTags": []string{"ignore"}, } env.ChangeConfiguration(cfg) diff --git a/gopls/internal/test/integration/workspace/vendor_test.go b/gopls/internal/test/integration/workspace/vendor_test.go index f14cf539de0..10826430164 100644 --- a/gopls/internal/test/integration/workspace/vendor_test.go +++ b/gopls/internal/test/integration/workspace/vendor_test.go @@ -36,10 +36,6 @@ module example.com/a go 1.14 require other.com/b v1.0.0 --- go.sum -- -other.com/b v1.0.0 h1:ct1+0RPozzMvA2rSYnVvIfr/GDHcd7oVnw147okdi3g= -other.com/b v1.0.0/go.mod h1:bfTSZo/4ZtAQJWBYScopwW6n9Ctfsl2mi8nXsqjDXR8= - -- a.go -- package a @@ -49,6 +45,7 @@ var _ b.B ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), Modes(Default), ).Run(t, src, func(t *testing.T, env *Env) { diff --git a/gopls/internal/test/integration/workspace/workspace_test.go b/gopls/internal/test/integration/workspace/workspace_test.go index 587ac522c41..00d4d81e021 100644 --- a/gopls/internal/test/integration/workspace/workspace_test.go +++ b/gopls/internal/test/integration/workspace/workspace_test.go @@ -309,9 +309,6 @@ module a.com require c.com v1.2.3 exclude b.com v1.2.3 --- go.sum -- -c.com v1.2.3 h1:n07Dz9fYmpNqvZMwZi5NEqFcSHbvLa9lacMX+/g25tw= -c.com v1.2.3/go.mod h1:/4TyYgU9Nu5tA4NymP5xyqE8R2VMzGD3TbJCwCOvHAg= -- main.go -- package a @@ -320,6 +317,7 @@ func main() { } ` WithOptions( + WriteGoSum("."), ProxyFiles(proxy), ).Run(t, files, func(t *testing.T, env *Env) { env.OnceMet( From bce67c43734e5f628ab1590ff8f5ece11ac06d22 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 21 Jan 2025 15:16:58 -0500 Subject: [PATCH 071/309] go/analysis/internal/checker: validate SuggestedFixes This change causes the Pass.Report operation of all our drivers: - internal/checker, used by {single,multi}checker, analysistest, and the public checker API; - unitchecker, used by cmd/vet; and - gopls' analysis driver to assert that SuggestedFixes are valid, and to establish postconditions such as the fix.End is valid. Also, add a test that pass.Report panics informatively. Change-Id: I7ee4ac621852ab0a39d47edce1ab6e2304bfc53b Reviewed-on: https://go-review.googlesource.com/c/tools/+/643715 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- go/analysis/checker/checker.go | 10 +- go/analysis/internal/checker/checker_test.go | 39 ++++- go/analysis/internal/checker/fix_test.go | 147 ++++++++++++++++--- go/analysis/unitchecker/unitchecker.go | 31 ++-- gopls/internal/cache/analysis.go | 5 + internal/analysisinternal/analysis.go | 88 +++++++++++ 6 files changed, 283 insertions(+), 37 deletions(-) diff --git a/go/analysis/checker/checker.go b/go/analysis/checker/checker.go index a563f7cbeda..502ec922179 100644 --- a/go/analysis/checker/checker.go +++ b/go/analysis/checker/checker.go @@ -337,8 +337,14 @@ func (act *Action) execOnce() { TypeErrors: act.Package.TypeErrors, Module: module, - ResultOf: inputs, - Report: func(d analysis.Diagnostic) { act.Diagnostics = append(act.Diagnostics, d) }, + ResultOf: inputs, + Report: func(d analysis.Diagnostic) { + // Assert that SuggestedFixes are well formed. + if err := analysisinternal.ValidateFixes(act.Package.Fset, act.Analyzer, d.SuggestedFixes); err != nil { + panic(err) + } + act.Diagnostics = append(act.Diagnostics, d) + }, ImportObjectFact: act.ObjectFact, ExportObjectFact: act.exportObjectFact, ImportPackageFact: act.PackageFact, diff --git a/go/analysis/internal/checker/checker_test.go b/go/analysis/internal/checker/checker_test.go index 7f38ad1a094..76d45adceef 100644 --- a/go/analysis/internal/checker/checker_test.go +++ b/go/analysis/internal/checker/checker_test.go @@ -84,6 +84,7 @@ var otherAnalyzer = &analysis.Analyzer{ // like analyzer but with a different Na } func run(pass *analysis.Pass) (interface{}, error) { + // TODO(adonovan): replace this entangled test with something completely data-driven. const ( from = "bar" to = "baz" @@ -109,11 +110,39 @@ func run(pass *analysis.Pass) (interface{}, error) { } switch pass.Pkg.Name() { case conflict: - edits = append(edits, []analysis.TextEdit{ - {Pos: ident.Pos() - 1, End: ident.End(), NewText: []byte(to)}, - {Pos: ident.Pos(), End: ident.End() - 1, NewText: []byte(to)}, - {Pos: ident.Pos(), End: ident.End(), NewText: []byte("lorem ipsum")}, - }...) + // Conflicting edits are legal, so long as they appear in different fixes. + pass.Report(analysis.Diagnostic{ + Pos: ident.Pos(), + End: ident.End(), + Message: msg, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: msg, TextEdits: []analysis.TextEdit{ + {Pos: ident.Pos() - 1, End: ident.End(), NewText: []byte(to)}, + }, + }}, + }) + pass.Report(analysis.Diagnostic{ + Pos: ident.Pos(), + End: ident.End(), + Message: msg, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: msg, TextEdits: []analysis.TextEdit{ + {Pos: ident.Pos(), End: ident.End() - 1, NewText: []byte(to)}, + }, + }}, + }) + pass.Report(analysis.Diagnostic{ + Pos: ident.Pos(), + End: ident.End(), + Message: msg, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: msg, TextEdits: []analysis.TextEdit{ + {Pos: ident.Pos(), End: ident.End(), NewText: []byte("lorem ipsum")}, + }, + }}, + }) + return + case duplicate: // Duplicate (non-insertion) edits are disallowed, // so this is a buggy analyzer, and validatedFixes should reject it. diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index 81bc569e861..4063aed35cd 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -19,7 +19,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/analysistest" + "golang.org/x/tools/go/analysis/checker" "golang.org/x/tools/go/analysis/multichecker" + "golang.org/x/tools/go/packages" "golang.org/x/tools/internal/testenv" ) @@ -126,15 +128,6 @@ func Foo() { _ = bar } -// the end -`, - "duplicate/dup.go": `package duplicate - -func Foo() { - bar := 14 - _ = bar -} - // the end `, } @@ -164,15 +157,6 @@ func Foo() { _ = baz } -// the end -`, - "duplicate/dup.go": `package duplicate - -func Foo() { - baz := 14 - _ = baz -} - // the end `, } @@ -182,7 +166,7 @@ func Foo() { } defer cleanup() - fix(t, dir, "rename,other", exitCodeDiagnostics, "rename", "duplicate") + fix(t, dir, "rename,other", exitCodeDiagnostics, "rename") for name, want := range fixed { path := path.Join(dir, "src", name) @@ -196,6 +180,117 @@ func Foo() { } } +// TestReportInvalidDiagnostic tests that a call to pass.Report with +// certain kind of invalid diagnostic (e.g. conflicting fixes) +// promptly results in a panic. +func TestReportInvalidDiagnostic(t *testing.T) { + testenv.NeedsGoPackages(t) + + // Load the errors package. + cfg := &packages.Config{Mode: packages.LoadAllSyntax} + initial, err := packages.Load(cfg, "errors") + if err != nil { + t.Fatal(err) + } + + for _, test := range []struct { + name string + want string + diag func(pos token.Pos) analysis.Diagnostic + }{ + // Diagnostic has two alternative fixes with the same Message. + { + "duplicate message", + `analyzer "a" suggests two fixes with same Message \(fix\)`, + func(pos token.Pos) analysis.Diagnostic { + return analysis.Diagnostic{ + Pos: pos, + Message: "oops", + SuggestedFixes: []analysis.SuggestedFix{ + {Message: "fix"}, + {Message: "fix"}, + }, + } + }, + }, + // TextEdit has invalid Pos. + { + "bad Pos", + `analyzer "a" suggests invalid fix .*: missing file info for pos`, + func(pos token.Pos) analysis.Diagnostic { + return analysis.Diagnostic{ + Pos: pos, + Message: "oops", + SuggestedFixes: []analysis.SuggestedFix{ + { + Message: "fix", + TextEdits: []analysis.TextEdit{{}}, + }, + }, + } + }, + }, + // TextEdit has invalid End. + { + "End < Pos", + `analyzer "a" suggests invalid fix .*: pos .* > end`, + func(pos token.Pos) analysis.Diagnostic { + return analysis.Diagnostic{ + Pos: pos, + Message: "oops", + SuggestedFixes: []analysis.SuggestedFix{ + { + Message: "fix", + TextEdits: []analysis.TextEdit{{ + Pos: pos + 2, + End: pos, + }}, + }, + }, + } + }, + }, + // Two TextEdits overlap. + { + "overlapping edits", + `analyzer "a" suggests invalid fix .*: overlapping edits to .*errors.go \(1:1-1:3 and 1:2-1:4\)`, + func(pos token.Pos) analysis.Diagnostic { + return analysis.Diagnostic{ + Pos: pos, + Message: "oops", + SuggestedFixes: []analysis.SuggestedFix{ + { + Message: "fix", + TextEdits: []analysis.TextEdit{ + {Pos: pos, End: pos + 2}, + {Pos: pos + 1, End: pos + 3}, + }, + }, + }, + } + }, + }, + } { + t.Run(test.name, func(t *testing.T) { + reached := false + a := &analysis.Analyzer{Name: "a", Doc: "doc", Run: func(pass *analysis.Pass) (any, error) { + reached = true + panics(t, test.want, func() { + pos := pass.Files[0].FileStart + pass.Report(test.diag(pos)) + }) + return nil, nil + }} + if _, err := checker.Analyze([]*analysis.Analyzer{a}, initial, &checker.Options{}); err != nil { + t.Fatalf("Analyze failed: %v", err) + } + if !reached { + t.Error("analyzer was never invoked") + } + }) + } +} + // TestConflict ensures that checker.Run detects conflicts correctly. // This test fork/execs the main function above. func TestConflict(t *testing.T) { @@ -333,3 +428,17 @@ func init() { }, } } + +// panics asserts that f() panics with with a value whose printed form matches the regexp want. +func panics(t *testing.T, want string, f func()) { + defer func() { + if x := recover(); x == nil { + t.Errorf("function returned normally, wanted panic") + } else if m, err := regexp.MatchString(want, fmt.Sprint(x)); err != nil { + t.Errorf("panics: invalid regexp %q", want) + } else if !m { + t.Errorf("function panicked with value %q, want match for %q", x, want) + } + }() + f() +} diff --git a/go/analysis/unitchecker/unitchecker.go b/go/analysis/unitchecker/unitchecker.go index f723349010e..82c3db6a39d 100644 --- a/go/analysis/unitchecker/unitchecker.go +++ b/go/analysis/unitchecker/unitchecker.go @@ -367,17 +367,26 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re } pass := &analysis.Pass{ - Analyzer: a, - Fset: fset, - Files: files, - OtherFiles: cfg.NonGoFiles, - IgnoredFiles: cfg.IgnoredFiles, - Pkg: pkg, - TypesInfo: info, - TypesSizes: tc.Sizes, - TypeErrors: nil, // unitchecker doesn't RunDespiteErrors - ResultOf: inputs, - Report: func(d analysis.Diagnostic) { act.diagnostics = append(act.diagnostics, d) }, + Analyzer: a, + Fset: fset, + Files: files, + OtherFiles: cfg.NonGoFiles, + IgnoredFiles: cfg.IgnoredFiles, + Pkg: pkg, + TypesInfo: info, + TypesSizes: tc.Sizes, + TypeErrors: nil, // unitchecker doesn't RunDespiteErrors + ResultOf: inputs, + Report: func(d analysis.Diagnostic) { + // Unitchecker doesn't apply fixes, but it does report them in the JSON output. + if err := analysisinternal.ValidateFixes(fset, a, d.SuggestedFixes); err != nil { + // Since we have diagnostics, the exit code will be nonzero, + // so logging these errors is sufficient. + log.Println(err) + d.SuggestedFixes = nil + } + act.diagnostics = append(act.diagnostics, d) + }, ImportObjectFact: facts.ImportObjectFact, ExportObjectFact: facts.ExportObjectFact, AllObjectFacts: func() []analysis.ObjectFact { return facts.AllObjectFacts(factFilter) }, diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go index 4c5abbc23ce..d570c0a46ae 100644 --- a/gopls/internal/cache/analysis.go +++ b/gopls/internal/cache/analysis.go @@ -1131,6 +1131,11 @@ func (act *action) exec(ctx context.Context) (any, *actionSummary, error) { TypeErrors: apkg.typeErrors, ResultOf: inputs, Report: func(d analysis.Diagnostic) { + // Assert that SuggestedFixes are well formed. + if err := analysisinternal.ValidateFixes(apkg.pkg.FileSet(), analyzer, d.SuggestedFixes); err != nil { + bug.Reportf("invalid SuggestedFixes: %v", err) + d.SuggestedFixes = nil + } diagnostic, err := toGobDiagnostic(posToLocation, analyzer, d) if err != nil { // Don't bug.Report here: these errors all originate in diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 8bfba325b49..8f38fa604d8 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -8,6 +8,7 @@ package analysisinternal import ( "bytes" + "cmp" "fmt" "go/ast" "go/printer" @@ -356,3 +357,90 @@ func IsMethodNamed(obj types.Object, pkgPath string, typeName string, names ...s func isPackageLevel(obj types.Object) bool { return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope() } + +// ValidateFixes validates the set of fixes for a single diagnostic. +// Any error indicates a bug in the originating analyzer. +// +// It updates fixes so that fixes[*].End.IsValid(). +// +// It may be used as part of an analysis driver implementation. +func ValidateFixes(fset *token.FileSet, a *analysis.Analyzer, fixes []analysis.SuggestedFix) error { + fixMessages := make(map[string]bool) + for i := range fixes { + fix := &fixes[i] + if fixMessages[fix.Message] { + return fmt.Errorf("analyzer %q suggests two fixes with same Message (%s)", a.Name, fix.Message) + } + fixMessages[fix.Message] = true + if err := validateFix(fset, fix); err != nil { + return fmt.Errorf("analyzer %q suggests invalid fix (%s): %v", a.Name, fix.Message, err) + } + } + return nil +} + +// validateFix validates a single fix. +// Any error indicates a bug in the originating analyzer. +// +// It updates fix so that fix.End.IsValid(). +func validateFix(fset *token.FileSet, fix *analysis.SuggestedFix) error { + + // Stably sort edits by Pos. This ordering puts insertions + // (end = start) before deletions (end > start) at the same + // point, but uses a stable sort to preserve the order of + // multiple insertions at the same point. + slices.SortStableFunc(fix.TextEdits, func(x, y analysis.TextEdit) int { + if sign := cmp.Compare(x.Pos, y.Pos); sign != 0 { + return sign + } + return cmp.Compare(x.End, y.End) + }) + + var prev *analysis.TextEdit + for i := range fix.TextEdits { + edit := &fix.TextEdits[i] + + // Validate edit individually. + start := edit.Pos + file := fset.File(start) + if file == nil { + return fmt.Errorf("missing file info for pos (%v)", edit.Pos) + } + if end := edit.End; end.IsValid() { + if end < start { + return fmt.Errorf("pos (%v) > end (%v)", edit.Pos, edit.End) + } + endFile := fset.File(end) + if endFile == nil { + return fmt.Errorf("malformed end position %v", end) + } + if endFile != file { + return fmt.Errorf("edit spans files %v and %v", file.Name(), endFile.Name()) + } + } else { + edit.End = start // update the SuggestedFix + } + if eof := token.Pos(file.Base() + file.Size()); edit.End > eof { + return fmt.Errorf("end is (%v) beyond end of file (%v)", edit.End, eof) + } + + // Validate the sequence of edits: + // properly ordered, no overlapping deletions + if prev != nil && edit.Pos < prev.End { + xpos := fset.Position(prev.Pos) + xend := fset.Position(prev.End) + ypos := fset.Position(edit.Pos) + yend := fset.Position(edit.End) + return fmt.Errorf("overlapping edits to %s (%d:%d-%d:%d and %d:%d-%d:%d)", + xpos.Filename, + xpos.Line, xpos.Column, + xend.Line, xend.Column, + ypos.Line, ypos.Column, + yend.Line, yend.Column, + ) + } + prev = edit + } + + return nil +} From ac81e9f3132418f4f19bc468a58650588d623488 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 27 Jan 2025 12:27:35 -0500 Subject: [PATCH 072/309] internal/testenv: RedirectStderr: fix race Fixes golang/go#71430 Change-Id: Ie614d3b9fc49b6f8878b82997c1aa50b25523a68 Reviewed-on: https://go-review.googlesource.com/c/tools/+/644677 Auto-Submit: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- internal/testenv/testenv.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 360ff0ffbe8..144f4f8fd64 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -568,11 +568,13 @@ func RedirectStderr(t testing.TB) { if err != nil { t.Fatalf("pipe: %v", err) } + done := make(chan struct{}) go func() { for sc := bufio.NewScanner(r); sc.Scan(); { t.Log(sc.Text()) } r.Close() + close(done) }() // Also do the same for the global logger. @@ -590,5 +592,8 @@ func RedirectStderr(t testing.TB) { log.SetOutput(savedWriter) log.SetPrefix(savedPrefix) log.SetFlags(savedFlags) + + // Don't let test finish before final t.Log. + <-done }) } From 8171d94fe98a51fb8471b40726983dd098a3fde6 Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Tue, 21 Jan 2025 16:42:55 -0500 Subject: [PATCH 073/309] gopls/internal/analysis/fillstruct: preserve existing formatting Modifies fillstruct refactoring to preserve the formatting and order of prefilled struct elements and comments. Fixes golang/go#70690, golang/go#71312 Change-Id: I0879d22a392e6c3ab85621420e54eb2e4651a1db Reviewed-on: https://go-review.googlesource.com/c/tools/+/643696 Reviewed-by: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- .../analysis/fillstruct/fillstruct.go | 142 ++++++++++-------- .../testdata/codeaction/fill_struct.txt | 48 ++++-- .../codeaction/fill_struct_resolve.txt | 22 ++- 3 files changed, 124 insertions(+), 88 deletions(-) diff --git a/gopls/internal/analysis/fillstruct/fillstruct.go b/gopls/internal/analysis/fillstruct/fillstruct.go index 1181693c3d9..a8a861f0651 100644 --- a/gopls/internal/analysis/fillstruct/fillstruct.go +++ b/gopls/internal/analysis/fillstruct/fillstruct.go @@ -17,6 +17,7 @@ import ( "fmt" "go/ast" "go/format" + "go/printer" "go/token" "go/types" "strings" @@ -168,26 +169,16 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, fil // Check which types have already been filled in. (we only want to fill in // the unfilled types, or else we'll blat user-supplied details) prefilledFields := map[string]ast.Expr{} + var elts []ast.Expr for _, e := range expr.Elts { if kv, ok := e.(*ast.KeyValueExpr); ok { if key, ok := kv.Key.(*ast.Ident); ok { prefilledFields[key.Name] = kv.Value + elts = append(elts, kv) } } } - // Use a new fileset to build up a token.File for the new composite - // literal. We need one line for foo{, one line for }, and one line for - // each field we're going to set. format.Node only cares about line - // numbers, so we don't need to set columns, and each line can be - // 1 byte long. - // TODO(adonovan): why is this necessary? The position information - // is going to be wrong for the existing trees in prefilledFields. - // Can't the formatter just do its best with an empty fileset? - fakeFset := token.NewFileSet() - tok := fakeFset.AddFile("", -1, fieldCount+2) - - line := 2 // account for 1-based lines and the left brace var fieldTyps []types.Type for i := 0; i < fieldCount; i++ { field := tStruct.Field(i) @@ -200,47 +191,41 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, fil } matches := analysisinternal.MatchingIdents(fieldTyps, file, start, info, pkg) qual := typesinternal.FileQualifier(file, pkg) - var elts []ast.Expr + for i, fieldTyp := range fieldTyps { if fieldTyp == nil { continue // TODO(adonovan): is this reachable? } fieldName := tStruct.Field(i).Name() - - tok.AddLine(line - 1) // add 1 byte per line - if line > tok.LineCount() { - panic(fmt.Sprintf("invalid line number %v (of %v) for fillstruct", line, tok.LineCount())) + if _, ok := prefilledFields[fieldName]; ok { + // We already stored these when looping over expr.Elt. + // Want to preserve the original order of prefilled fields + continue } - pos := tok.LineStart(line) kv := &ast.KeyValueExpr{ Key: &ast.Ident{ - NamePos: pos, - Name: fieldName, + Name: fieldName, }, - Colon: pos, } - if expr, ok := prefilledFields[fieldName]; ok { + + names, ok := matches[fieldTyp] + if !ok { + return nil, nil, fmt.Errorf("invalid struct field type: %v", fieldTyp) + } + + // Find the name most similar to the field name. + // If no name matches the pattern, generate a zero value. + // NOTE: We currently match on the name of the field key rather than the field type. + if best := fuzzy.BestMatch(fieldName, names); best != "" { + kv.Value = ast.NewIdent(best) + } else if expr, isValid := populateValue(fieldTyp, qual); isValid { kv.Value = expr } else { - names, ok := matches[fieldTyp] - if !ok { - return nil, nil, fmt.Errorf("invalid struct field type: %v", fieldTyp) - } - - // Find the name most similar to the field name. - // If no name matches the pattern, generate a zero value. - // NOTE: We currently match on the name of the field key rather than the field type. - if best := fuzzy.BestMatch(fieldName, names); best != "" { - kv.Value = ast.NewIdent(best) - } else if expr, isValid := populateValue(fieldTyp, qual); isValid { - kv.Value = expr - } else { - return nil, nil, nil // no fix to suggest - } + return nil, nil, nil // no fix to suggest } + elts = append(elts, kv) - line++ } // If all of the struct's fields are unexported, we have nothing to do. @@ -248,21 +233,6 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, fil return nil, nil, fmt.Errorf("no elements to fill") } - // Add the final line for the right brace. Offset is the number of - // bytes already added plus 1. - tok.AddLine(len(elts) + 1) - line = len(elts) + 2 - if line > tok.LineCount() { - panic(fmt.Sprintf("invalid line number %v (of %v) for fillstruct", line, tok.LineCount())) - } - - cl := &ast.CompositeLit{ - Type: expr.Type, - Lbrace: tok.LineStart(1), - Elts: elts, - Rbrace: tok.LineStart(line), - } - // Find the line on which the composite literal is declared. split := bytes.Split(content, []byte("\n")) lineNumber := safetoken.StartPosition(fset, expr.Lbrace).Line @@ -274,26 +244,66 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, fil index := bytes.Index(firstLine, trimmed) whitespace := firstLine[:index] - // First pass through the formatter: turn the expr into a string. - var formatBuf bytes.Buffer - if err := format.Node(&formatBuf, fakeFset, cl); err != nil { - return nil, nil, fmt.Errorf("failed to run first format on:\n%s\ngot err: %v", cl.Type, err) - } - sug := indent(formatBuf.Bytes(), whitespace) + // Write a new composite literal "_{...}" composed of all prefilled and new elements, + // preserving existing formatting and comments. + // An alternative would be to only format the new fields, + // but by printing the entire composite literal, we ensure + // that the result is gofmt'ed. + var buf bytes.Buffer + buf.WriteString("_{\n") + fcmap := ast.NewCommentMap(fset, file, file.Comments) + comments := fcmap.Filter(expr).Comments() // comments inside the expr, in source order + for _, elt := range elts { + // Print comments before the current elt + for len(comments) > 0 && comments[0].Pos() < elt.Pos() { + for _, co := range comments[0].List { + fmt.Fprintln(&buf, co.Text) + } + comments = comments[1:] + } + + // Print the current elt with comments + eltcomments := fcmap.Filter(elt).Comments() + if err := format.Node(&buf, fset, &printer.CommentedNode{Node: elt, Comments: eltcomments}); err != nil { + return nil, nil, err + } + buf.WriteString(",") - if len(prefilledFields) > 0 { - // Attempt a second pass through the formatter to line up columns. - sourced, err := format.Source(sug) - if err == nil { - sug = indent(sourced, whitespace) + // Prune comments up to the end of the elt + for len(comments) > 0 && comments[0].Pos() < elt.End() { + comments = comments[1:] } + + // Write comments associated with the current elt that appear after it + // printer.CommentedNode only prints comments inside the elt. + for _, cg := range eltcomments { + for _, co := range cg.List { + if co.Pos() >= elt.End() { + fmt.Fprintln(&buf, co.Text) + if len(comments) > 0 { + comments = comments[1:] + } + } + } + } + buf.WriteString("\n") + } + buf.WriteString("}") + formatted, err := format.Source(buf.Bytes()) + if err != nil { + return nil, nil, err } + sug := indent(formatted, whitespace) + // Remove _ + idx := bytes.IndexByte(sug, '{') // cannot fail + sug = sug[idx:] + return fset, &analysis.SuggestedFix{ TextEdits: []analysis.TextEdit{ { - Pos: expr.Pos(), - End: expr.End(), + Pos: expr.Lbrace, + End: expr.Rbrace + token.Pos(len("}")), NewText: sug, }, }, diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt b/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt index 600119dad8e..6c71175eb04 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt @@ -364,12 +364,15 @@ func fill() { _ := StructAnon{} //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) } -- @fillStruct_anon/fillStruct_anon.go -- -@@ -13 +13,5 @@ +@@ -13 +13,8 @@ - _ := StructAnon{} //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) + _ := StructAnon{ + a: struct{}{}, + b: map[string]any{}, -+ c: map[string]struct{d int; e bool}{}, ++ c: map[string]struct { ++ d int ++ e bool ++ }{}, + } //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) -- fillStruct_nested.go -- package fillstruct @@ -457,13 +460,8 @@ func fill() { + UnfilledInt: 0, + StructPartialB: StructPartialB{}, -- @fillStruct_partial2/fillStruct_partial.go -- -@@ -19,4 +19,2 @@ -- /* this comment should disappear */ -- PrefilledInt: 7, // This comment should be blown away. -- /* As should -- this one */ -+ PrefilledInt: 7, -+ UnfilledInt: 0, +@@ -23 +23 @@ ++ UnfilledInt: 0, -- fillStruct_spaces.go -- package fillstruct @@ -566,7 +564,7 @@ func _[T any]() { + bar: 0, +} //@codeaction("}", "refactor.rewrite.fillStruct", edit=typeparams2) -- @typeparams3/typeparams.go -- -@@ -21 +21 @@ +@@ -22 +22 @@ + foo: 0, -- @typeparams4/typeparams.go -- @@ -29 +29,4 @@ @@ -723,3 +721,33 @@ func _() { + aliasArray: aliasArray{}, + aliasNamed: aliasNamed{}, + } //@codeaction("}", "refactor.rewrite.fillStruct", edit=alias) +-- preserveformat/preserveformat.go -- +package preserveformat + +type ( + Node struct { + Value int + } + Graph struct { + Nodes []*Node `json:""` + Edges map[*Node]*Node + Other string + } +) + +func _() { + _ := &Graph{ + // comments at the start preserved + Nodes: []*Node{ + {Value: 0}, // comments in the middle preserved + // between lines + {Value: 0}, + }, // another comment + // comment group + // below + } //@codeaction("}", "refactor.rewrite.fillStruct", edit=preserveformat) +} +-- @preserveformat/preserveformat/preserveformat.go -- +@@ -24 +24,2 @@ ++ Edges: map[*Node]*Node{}, ++ Other: "", diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt index 6d1250e26aa..d7746eef28e 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt @@ -373,12 +373,15 @@ func fill() { _ := StructAnon{} //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) } -- @fillStruct_anon/fillStruct_anon.go -- -@@ -13 +13,5 @@ +@@ -13 +13,8 @@ - _ := StructAnon{} //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) + _ := StructAnon{ + a: struct{}{}, + b: map[string]any{}, -+ c: map[string]struct{d int; e bool}{}, ++ c: map[string]struct { ++ d int ++ e bool ++ }{}, + } //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_anon) -- fillStruct_nested.go -- package fillstruct @@ -452,8 +455,8 @@ func fill() { PrefilledInt: 5, } //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_partial1) b := StructPartialB{ - /* this comment should disappear */ - PrefilledInt: 7, // This comment should be blown away. + /* this comment should be preserved */ + PrefilledInt: 7, // This comment should be preserved. /* As should this one */ } //@codeaction("}", "refactor.rewrite.fillStruct", edit=fillStruct_partial2) @@ -466,13 +469,8 @@ func fill() { + UnfilledInt: 0, + StructPartialB: StructPartialB{}, -- @fillStruct_partial2/fillStruct_partial.go -- -@@ -19,4 +19,2 @@ -- /* this comment should disappear */ -- PrefilledInt: 7, // This comment should be blown away. -- /* As should -- this one */ -+ PrefilledInt: 7, -+ UnfilledInt: 0, +@@ -23 +23 @@ ++ UnfilledInt: 0, -- fillStruct_spaces.go -- package fillstruct @@ -575,7 +573,7 @@ func _[T any]() { + bar: 0, +} //@codeaction("}", "refactor.rewrite.fillStruct", edit=typeparams2) -- @typeparams3/typeparams.go -- -@@ -21 +21 @@ +@@ -22 +22 @@ + foo: 0, -- @typeparams4/typeparams.go -- @@ -29 +29,4 @@ From e4266160ff24673eb4644707c219806e916f77e6 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Tue, 28 Jan 2025 13:59:08 -0500 Subject: [PATCH 074/309] godoc,present,refactor: modernize Apply modernizations to godoc, present, and refactor. Almost all of these are changing interface{} to any. Change-Id: Ib0a524a0c73efa7a467026cb16808cc4a1b64e57 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645376 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- godoc/analysis/analysis.go | 6 +++--- godoc/godoc.go | 12 ++++++------ godoc/index.go | 30 +++++++++++++++--------------- godoc/search.go | 4 ++-- godoc/server.go | 6 +++--- godoc/spec.go | 2 +- godoc/template.go | 12 ++++++------ godoc/util/util.go | 6 +++--- godoc/vfs/emptyvfs.go | 2 +- godoc/vfs/mapfs/mapfs.go | 6 +++--- godoc/vfs/namespace.go | 2 +- godoc/vfs/zipfs/zipfs.go | 2 +- present/args.go | 4 ++-- present/code.go | 4 ++-- present/parse.go | 7 ++++--- refactor/importgraph/graph.go | 2 +- refactor/rename/check.go | 2 +- 17 files changed, 55 insertions(+), 54 deletions(-) diff --git a/godoc/analysis/analysis.go b/godoc/analysis/analysis.go index 54611e87d96..54d692a59ec 100644 --- a/godoc/analysis/analysis.go +++ b/godoc/analysis/analysis.go @@ -62,15 +62,15 @@ type Link interface { // FileInfo holds analysis information for the source file view. // Clients must not mutate it. type FileInfo struct { - Data []interface{} // JSON serializable values - Links []Link // HTML link markup + Data []any // JSON serializable values + Links []Link // HTML link markup } // A fileInfo is the server's store of hyperlinks and JSON data for a // particular file. type fileInfo struct { mu sync.Mutex - data []interface{} // JSON objects + data []any // JSON objects links []Link sorted bool hasErrors bool // TODO(adonovan): surface this in the UI diff --git a/godoc/godoc.go b/godoc/godoc.go index a9d806f7e8b..ac6ab23a0a1 100644 --- a/godoc/godoc.go +++ b/godoc/godoc.go @@ -190,13 +190,13 @@ func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string { return `no snippet text available` } -func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string { +func (p *Presentation) nodeFunc(info *PageInfo, node any) string { var buf bytes.Buffer p.writeNode(&buf, info, info.FSet, node) return buf.String() } -func (p *Presentation) node_htmlFunc(info *PageInfo, node interface{}, linkify bool) string { +func (p *Presentation) node_htmlFunc(info *PageInfo, node any, linkify bool) string { var buf1 bytes.Buffer p.writeNode(&buf1, info, info.FSet, node) @@ -477,9 +477,9 @@ func srcBreadcrumbFunc(relpath string) string { return buf.String() } -func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string { +func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n any) string { // n must be an ast.Node or a *doc.Note - return func(info *PageInfo, n interface{}) string { + return func(info *PageInfo, n any) string { var pos, end token.Pos switch n := n.(type) { @@ -839,7 +839,7 @@ func replaceLeadingIndentation(body, oldIndent, newIndent string) string { // The provided fset must be non-nil. The pageInfo is optional. If // present, the pageInfo is used to add comments to struct fields to // say which version of Go introduced them. -func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x interface{}) { +func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x any) { // convert trailing tabs into spaces using a tconv filter // to ensure a good outcome in most browsers (there may still // be tabs in comments and strings, but converting those into @@ -918,7 +918,7 @@ var slashSlash = []byte("//") // WriteNode writes x to w. // TODO(bgarcia) Is this method needed? It's just a wrapper for p.writeNode. -func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x interface{}) { +func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x any) { p.writeNode(w, nil, fset, x) } diff --git a/godoc/index.go b/godoc/index.go index 377837a0b36..05a1a9441ee 100644 --- a/godoc/index.go +++ b/godoc/index.go @@ -71,10 +71,10 @@ import ( // InterfaceSlice is a helper type for sorting interface // slices according to some slice-specific sort criteria. -type comparer func(x, y interface{}) bool +type comparer func(x, y any) bool type interfaceSlice struct { - slice []interface{} + slice []any less comparer } @@ -87,7 +87,7 @@ type interfaceSlice struct { // runs. For instance, a RunList containing pairs (x, y) may be compressed // into a RunList containing pair runs (x, {y}) where each run consists of // a list of y's with the same x. -type RunList []interface{} +type RunList []any func (h RunList) sort(less comparer) { sort.Sort(&interfaceSlice{h, less}) @@ -99,7 +99,7 @@ func (p *interfaceSlice) Swap(i, j int) { p.slice[i], p.slice[j] = p.slice[ // Compress entries which are the same according to a sort criteria // (specified by less) into "runs". -func (h RunList) reduce(less comparer, newRun func(h RunList) interface{}) RunList { +func (h RunList) reduce(less comparer, newRun func(h RunList) any) RunList { if len(h) == 0 { return nil } @@ -143,10 +143,10 @@ func (k KindRun) Less(i, j int) bool { return k[i].Lori() < k[j].Lori() } func (k KindRun) Swap(i, j int) { k[i], k[j] = k[j], k[i] } // FileRun contents are sorted by Kind for the reduction into KindRuns. -func lessKind(x, y interface{}) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() } +func lessKind(x, y any) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() } // newKindRun allocates a new KindRun from the SpotInfo run h. -func newKindRun(h RunList) interface{} { +func newKindRun(h RunList) any { run := make(KindRun, len(h)) for i, x := range h { run[i] = x.(SpotInfo) @@ -214,7 +214,7 @@ type FileRun struct { } // Spots are sorted by file path for the reduction into FileRuns. -func lessSpot(x, y interface{}) bool { +func lessSpot(x, y any) bool { fx := x.(Spot).File fy := y.(Spot).File // same as "return fx.Path() < fy.Path()" but w/o computing the file path first @@ -224,7 +224,7 @@ func lessSpot(x, y interface{}) bool { } // newFileRun allocates a new FileRun from the Spot run h. -func newFileRun(h RunList) interface{} { +func newFileRun(h RunList) any { file := h[0].(Spot).File // reduce the list of Spots into a list of KindRuns @@ -257,12 +257,12 @@ func (p *PakRun) Less(i, j int) bool { return p.Files[i].File.Name < p.Files[j]. func (p *PakRun) Swap(i, j int) { p.Files[i], p.Files[j] = p.Files[j], p.Files[i] } // FileRuns are sorted by package for the reduction into PakRuns. -func lessFileRun(x, y interface{}) bool { +func lessFileRun(x, y any) bool { return x.(*FileRun).File.Pak.less(y.(*FileRun).File.Pak) } // newPakRun allocates a new PakRun from the *FileRun run h. -func newPakRun(h RunList) interface{} { +func newPakRun(h RunList) any { pak := h[0].(*FileRun).File.Pak files := make([]*FileRun, len(h)) for i, x := range h { @@ -280,7 +280,7 @@ func newPakRun(h RunList) interface{} { type HitList []*PakRun // PakRuns are sorted by package. -func lessPakRun(x, y interface{}) bool { return x.(*PakRun).Pak.less(y.(*PakRun).Pak) } +func lessPakRun(x, y any) bool { return x.(*PakRun).Pak.less(y.(*PakRun).Pak) } func reduce(h0 RunList) HitList { // reduce a list of Spots into a list of FileRuns @@ -325,10 +325,10 @@ type AltWords struct { } // wordPairs are sorted by their canonical spelling. -func lessWordPair(x, y interface{}) bool { return x.(*wordPair).canon < y.(*wordPair).canon } +func lessWordPair(x, y any) bool { return x.(*wordPair).canon < y.(*wordPair).canon } // newAltWords allocates a new AltWords from the *wordPair run h. -func newAltWords(h RunList) interface{} { +func newAltWords(h RunList) any { canon := h[0].(*wordPair).canon alts := make([]string, len(h)) for i, x := range h { @@ -1159,7 +1159,7 @@ func (x *Index) WriteTo(w io.Writer) (n int64, err error) { return 0, err } if fulltext { - encode := func(x interface{}) error { + encode := func(x any) error { return gob.NewEncoder(w).Encode(x) } if err := x.fset.Write(encode); err != nil { @@ -1199,7 +1199,7 @@ func (x *Index) ReadFrom(r io.Reader) (n int64, err error) { x.opts = fx.Opts if fx.Fulltext { x.fset = token.NewFileSet() - decode := func(x interface{}) error { + decode := func(x any) error { return gob.NewDecoder(r).Decode(x) } if err := x.fset.Read(decode); err != nil { diff --git a/godoc/search.go b/godoc/search.go index 33e4febfaaa..a0afb8bf97b 100644 --- a/godoc/search.go +++ b/godoc/search.go @@ -36,7 +36,7 @@ func (c *Corpus) Lookup(query string) SearchResult { // identifier search if r, err := index.Lookup(query); err == nil { result = r - } else if err != nil && !c.IndexFullText { + } else if !c.IndexFullText { // ignore the error if full text search is enabled // since the query may be a valid regular expression result.Alert = "Error in query string: " + err.Error() @@ -127,7 +127,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) { func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/opensearchdescription+xml") - data := map[string]interface{}{ + data := map[string]any{ "BaseURL": fmt.Sprintf("http://%s", r.Host), } applyTemplateToResponseWriter(w, p.SearchDescXML, &data) diff --git a/godoc/server.go b/godoc/server.go index afb28e2e187..92d1ec48d61 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -502,7 +502,7 @@ func packageExports(fset *token.FileSet, pkg *ast.Package) { } } -func applyTemplate(t *template.Template, name string, data interface{}) []byte { +func applyTemplate(t *template.Template, name string, data any) []byte { var buf bytes.Buffer if err := t.Execute(&buf, data); err != nil { log.Printf("%s.Execute: %s", name, err) @@ -529,7 +529,7 @@ func (w *writerCapturesErr) Write(p []byte) (int, error) { // they come from the template processing and not the Writer; this avoid // polluting log files with error messages due to networking issues, such as // client disconnects and http HEAD protocol violations. -func applyTemplateToResponseWriter(rw http.ResponseWriter, t *template.Template, data interface{}) { +func applyTemplateToResponseWriter(rw http.ResponseWriter, t *template.Template, data any) { w := &writerCapturesErr{w: rw} err := t.Execute(w, data) // There are some cases where template.Execute does not return an error when @@ -839,7 +839,7 @@ func (p *Presentation) ServeText(w http.ResponseWriter, text []byte) { w.Write(text) } -func marshalJSON(x interface{}) []byte { +func marshalJSON(x any) []byte { var data []byte var err error const indentJSON = false // for easier debugging diff --git a/godoc/spec.go b/godoc/spec.go index 9ec94278db5..c8142363e9b 100644 --- a/godoc/spec.go +++ b/godoc/spec.go @@ -38,7 +38,7 @@ func (p *ebnfParser) next() { p.lit = p.scanner.TokenText() } -func (p *ebnfParser) printf(format string, args ...interface{}) { +func (p *ebnfParser) printf(format string, args ...any) { p.flush() fmt.Fprintf(p.out, format, args...) } diff --git a/godoc/template.go b/godoc/template.go index 1e4e42e30e5..4418bea09b5 100644 --- a/godoc/template.go +++ b/godoc/template.go @@ -55,7 +55,7 @@ func (c *Corpus) contents(name string) string { } // stringFor returns a textual representation of the arg, formatted according to its nature. -func stringFor(arg interface{}) string { +func stringFor(arg any) string { switch arg := arg.(type) { case int: return fmt.Sprintf("%d", arg) @@ -70,7 +70,7 @@ func stringFor(arg interface{}) string { return "" } -func (p *Presentation) code(file string, arg ...interface{}) (s string, err error) { +func (p *Presentation) code(file string, arg ...any) (s string, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("%v", r) @@ -85,7 +85,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro command = fmt.Sprintf("code %q", file) case 1: command = fmt.Sprintf("code %q %s", file, stringFor(arg[0])) - text = p.Corpus.oneLine(file, text, arg[0]) + text = p.Corpus.oneLine(file, arg[0]) case 2: command = fmt.Sprintf("code %q %s %s", file, stringFor(arg[0]), stringFor(arg[1])) text = p.Corpus.multipleLines(file, text, arg[0], arg[1]) @@ -105,7 +105,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro } // parseArg returns the integer or string value of the argument and tells which it is. -func parseArg(arg interface{}, file string, max int) (ival int, sval string, isInt bool) { +func parseArg(arg any, file string, max int) (ival int, sval string, isInt bool) { switch n := arg.(type) { case int: if n <= 0 || n > max { @@ -120,7 +120,7 @@ func parseArg(arg interface{}, file string, max int) (ival int, sval string, isI } // oneLine returns the single line generated by a two-argument code invocation. -func (c *Corpus) oneLine(file, text string, arg interface{}) string { +func (c *Corpus) oneLine(file string, arg any) string { lines := strings.SplitAfter(c.contents(file), "\n") line, pattern, isInt := parseArg(arg, file, len(lines)) if isInt { @@ -130,7 +130,7 @@ func (c *Corpus) oneLine(file, text string, arg interface{}) string { } // multipleLines returns the text generated by a three-argument code invocation. -func (c *Corpus) multipleLines(file, text string, arg1, arg2 interface{}) string { +func (c *Corpus) multipleLines(file, text string, arg1, arg2 any) string { lines := strings.SplitAfter(c.contents(file), "\n") line1, pattern1, isInt1 := parseArg(arg1, file, len(lines)) line2, pattern2, isInt2 := parseArg(arg2, file, len(lines)) diff --git a/godoc/util/util.go b/godoc/util/util.go index c08ca785fed..21390556e7f 100644 --- a/godoc/util/util.go +++ b/godoc/util/util.go @@ -18,18 +18,18 @@ import ( // access to it and records the time the value was last set. type RWValue struct { mutex sync.RWMutex - value interface{} + value any timestamp time.Time // time of last set() } -func (v *RWValue) Set(value interface{}) { +func (v *RWValue) Set(value any) { v.mutex.Lock() v.value = value v.timestamp = time.Now() v.mutex.Unlock() } -func (v *RWValue) Get() (interface{}, time.Time) { +func (v *RWValue) Get() (any, time.Time) { v.mutex.RLock() defer v.mutex.RUnlock() return v.value, v.timestamp diff --git a/godoc/vfs/emptyvfs.go b/godoc/vfs/emptyvfs.go index 521bf71a51b..4ab5c7c649e 100644 --- a/godoc/vfs/emptyvfs.go +++ b/godoc/vfs/emptyvfs.go @@ -84,6 +84,6 @@ func (e *emptyVFS) IsDir() bool { return true } -func (e *emptyVFS) Sys() interface{} { +func (e *emptyVFS) Sys() any { return nil } diff --git a/godoc/vfs/mapfs/mapfs.go b/godoc/vfs/mapfs/mapfs.go index 9d0f465eb5e..06fb4f09543 100644 --- a/godoc/vfs/mapfs/mapfs.go +++ b/godoc/vfs/mapfs/mapfs.go @@ -158,9 +158,9 @@ func (fi mapFI) Mode() os.FileMode { } return 0444 } -func (fi mapFI) Name() string { return pathpkg.Base(fi.name) } -func (fi mapFI) Size() int64 { return int64(fi.size) } -func (fi mapFI) Sys() interface{} { return nil } +func (fi mapFI) Name() string { return pathpkg.Base(fi.name) } +func (fi mapFI) Size() int64 { return int64(fi.size) } +func (fi mapFI) Sys() any { return nil } type nopCloser struct { io.ReadSeeker diff --git a/godoc/vfs/namespace.go b/godoc/vfs/namespace.go index 23dd9794312..2566051a293 100644 --- a/godoc/vfs/namespace.go +++ b/godoc/vfs/namespace.go @@ -275,7 +275,7 @@ func (d dirInfo) Size() int64 { return 0 } func (d dirInfo) Mode() os.FileMode { return os.ModeDir | 0555 } func (d dirInfo) ModTime() time.Time { return startTime } func (d dirInfo) IsDir() bool { return true } -func (d dirInfo) Sys() interface{} { return nil } +func (d dirInfo) Sys() any { return nil } var startTime = time.Now() diff --git a/godoc/vfs/zipfs/zipfs.go b/godoc/vfs/zipfs/zipfs.go index 14c9820a1c7..cdf231a1abd 100644 --- a/godoc/vfs/zipfs/zipfs.go +++ b/godoc/vfs/zipfs/zipfs.go @@ -68,7 +68,7 @@ func (fi zipFI) IsDir() bool { return fi.file == nil } -func (fi zipFI) Sys() interface{} { +func (fi zipFI) Sys() any { return nil } diff --git a/present/args.go b/present/args.go index b4f7503b6da..17b9d4e87e8 100644 --- a/present/args.go +++ b/present/args.go @@ -96,7 +96,7 @@ func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err error j = i } pattern := addr[1:i] - lo, hi, err = addrRegexp(data, lo, hi, dir, pattern) + lo, hi, err = addrRegexp(data, hi, dir, pattern) prevc = c addr = addr[j:] continue @@ -202,7 +202,7 @@ func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int, // addrRegexp searches for pattern in the given direction starting at lo, hi. // The direction dir is '+' (search forward from hi) or '-' (search backward from lo). // Backward searches are unimplemented. -func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, error) { +func addrRegexp(data []byte, hi int, dir byte, pattern string) (int, int, error) { // We want ^ and $ to work as in sam/acme, so use ?m. re, err := regexp.Compile("(?m:" + pattern + ")") if err != nil { diff --git a/present/code.go b/present/code.go index f00f1f49d0b..d98f8384414 100644 --- a/present/code.go +++ b/present/code.go @@ -238,8 +238,8 @@ func codeLines(src []byte, start, end int) (lines []codeLine) { return } -func parseArgs(name string, line int, args []string) (res []interface{}, err error) { - res = make([]interface{}, len(args)) +func parseArgs(name string, line int, args []string) (res []any, err error) { + res = make([]any, len(args)) for i, v := range args { if len(v) == 0 { return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v) diff --git a/present/parse.go b/present/parse.go index 162a382b060..8b41dd2df52 100644 --- a/present/parse.go +++ b/present/parse.go @@ -15,6 +15,7 @@ import ( "net/url" "os" "regexp" + "slices" "strings" "time" "unicode" @@ -166,7 +167,7 @@ type Elem interface { // renderElem implements the elem template function, used to render // sub-templates. func renderElem(t *template.Template, e Elem) (template.HTML, error) { - var data interface{} = e + var data any = e if s, ok := e.(Section); ok { data = struct { Section @@ -191,7 +192,7 @@ func init() { // execTemplate is a helper to execute a template and return the output as a // template.HTML value. -func execTemplate(t *template.Template, name string, data interface{}) (template.HTML, error) { +func execTemplate(t *template.Template, name string, data any) (template.HTML, error) { b := new(bytes.Buffer) err := t.ExecuteTemplate(b, name, data) if err != nil { @@ -394,7 +395,7 @@ func parseSections(ctx *Context, name, prefix string, lines *Lines, number []int } } section := Section{ - Number: append(append([]int{}, number...), i), + Number: append(slices.Clone(number), i), Title: title, ID: id, } diff --git a/refactor/importgraph/graph.go b/refactor/importgraph/graph.go index d2d8f098b3f..c24ff882c7b 100644 --- a/refactor/importgraph/graph.go +++ b/refactor/importgraph/graph.go @@ -68,7 +68,7 @@ func Build(ctxt *build.Context) (forward, reverse Graph, errors map[string]error err error } - ch := make(chan interface{}) + ch := make(chan any) go func() { sema := make(chan int, 20) // I/O concurrency limiting semaphore diff --git a/refactor/rename/check.go b/refactor/rename/check.go index 8350ad7bc32..4a058321ca4 100644 --- a/refactor/rename/check.go +++ b/refactor/rename/check.go @@ -19,7 +19,7 @@ import ( ) // errorf reports an error (e.g. conflict) and prevents file modification. -func (r *renamer) errorf(pos token.Pos, format string, args ...interface{}) { +func (r *renamer) errorf(pos token.Pos, format string, args ...any) { r.hadConflicts = true reportError(r.iprog.Fset.Position(pos), fmt.Sprintf(format, args...)) } From 9f450b061cce9ade250237ffe62343132e90d69d Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 30 Jan 2025 16:52:13 +0000 Subject: [PATCH 075/309] go/analysis/passes/printf: suppress errors for non-const format strings The new check added in golang/go#60529 reports errors for non-constant format strings with no arguments. These are almost always bugs, but are often mild or inconsequential, and can be numerous in existing code bases. To reduce friction from this change, gate the new check on the implied language version. For golang/go#71485 Change-Id: I4926da2809dd14ba70ae530cd1657119f5377ad5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645595 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- go/analysis/passes/printf/printf.go | 48 ++++++++++----- go/analysis/passes/printf/printf_test.go | 20 +++++- .../printf/testdata/nonconst_go123.txtar | 61 +++++++++++++++++++ .../printf/testdata/nonconst_go124.txtar | 59 ++++++++++++++++++ .../passes/printf/testdata/src/fix/fix.go | 20 ------ .../printf/testdata/src/fix/fix.go.golden | 20 ------ .../printf/testdata/src/nonconst/nonconst.go | 23 +++++++ 7 files changed, 195 insertions(+), 56 deletions(-) create mode 100644 go/analysis/passes/printf/testdata/nonconst_go123.txtar create mode 100644 go/analysis/passes/printf/testdata/nonconst_go124.txtar delete mode 100644 go/analysis/passes/printf/testdata/src/fix/fix.go delete mode 100644 go/analysis/passes/printf/testdata/src/fix/fix.go.golden create mode 100644 go/analysis/passes/printf/testdata/src/nonconst/nonconst.go diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go index b95e2fd6f1a..81600a283aa 100644 --- a/go/analysis/passes/printf/printf.go +++ b/go/analysis/passes/printf/printf.go @@ -24,6 +24,7 @@ import ( "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/fmtstr" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/versions" ) func init() { @@ -107,12 +108,12 @@ func (f *isWrapper) String() string { } } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { res := &Result{ funcs: make(map[*types.Func]Kind), } findPrintfLike(pass, res) - checkCall(pass) + checkCalls(pass) return res, nil } @@ -181,7 +182,7 @@ func maybePrintfWrapper(info *types.Info, decl ast.Decl) *printfWrapper { } // findPrintfLike scans the entire package to find printf-like functions. -func findPrintfLike(pass *analysis.Pass, res *Result) (interface{}, error) { +func findPrintfLike(pass *analysis.Pass, res *Result) (any, error) { // Gather potential wrappers and call graph between them. byObj := make(map[*types.Func]*printfWrapper) var wrappers []*printfWrapper @@ -408,20 +409,29 @@ func stringConstantExpr(pass *analysis.Pass, expr ast.Expr) (string, bool) { return "", false } -// checkCall triggers the print-specific checks if the call invokes a print function. -func checkCall(pass *analysis.Pass) { +// checkCalls triggers the print-specific checks for calls that invoke a print +// function. +func checkCalls(pass *analysis.Pass) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ + (*ast.File)(nil), (*ast.CallExpr)(nil), } + + var fileVersion string // for selectively suppressing checks; "" if unknown. inspect.Preorder(nodeFilter, func(n ast.Node) { - call := n.(*ast.CallExpr) - fn, kind := printfNameAndKind(pass, call) - switch kind { - case KindPrintf, KindErrorf: - checkPrintf(pass, kind, call, fn.FullName()) - case KindPrint: - checkPrint(pass, call, fn.FullName()) + switch n := n.(type) { + case *ast.File: + fileVersion = versions.Lang(versions.FileVersion(pass.TypesInfo, n)) + + case *ast.CallExpr: + fn, kind := printfNameAndKind(pass, n) + switch kind { + case KindPrintf, KindErrorf: + checkPrintf(pass, fileVersion, kind, n, fn.FullName()) + case KindPrint: + checkPrint(pass, n, fn.FullName()) + } } }) } @@ -484,7 +494,7 @@ func isFormatter(typ types.Type) bool { } // checkPrintf checks a call to a formatted print routine such as Printf. -func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, name string) { +func checkPrintf(pass *analysis.Pass, fileVersion string, kind Kind, call *ast.CallExpr, name string) { idx := formatStringIndex(pass, call) if idx < 0 || idx >= len(call.Args) { return @@ -498,7 +508,17 @@ func checkPrintf(pass *analysis.Pass, kind Kind, call *ast.CallExpr, name string // non-constant format string and no arguments: // if msg contains "%", misformatting occurs. // Report the problem and suggest a fix: fmt.Printf("%s", msg). - if !suppressNonconstants && idx == len(call.Args)-1 { + // + // However, as described in golang/go#71485, this analysis can produce a + // significant number of diagnostics in existing code, and the bugs it + // finds are sometimes unlikely or inconsequential, and may not be worth + // fixing for some users. Gating on language version allows us to avoid + // breaking existing tests and CI scripts. + if !suppressNonconstants && + idx == len(call.Args)-1 && + fileVersion != "" && // fail open + versions.AtLeast(fileVersion, "go1.24") { + pass.Report(analysis.Diagnostic{ Pos: formatArg.Pos(), End: formatArg.End(), diff --git a/go/analysis/passes/printf/printf_test.go b/go/analysis/passes/printf/printf_test.go index 198cf6ec549..1ce9c28c103 100644 --- a/go/analysis/passes/printf/printf_test.go +++ b/go/analysis/passes/printf/printf_test.go @@ -5,10 +5,13 @@ package printf_test import ( + "path/filepath" "testing" "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/printf" + "golang.org/x/tools/internal/testenv" + "golang.org/x/tools/internal/testfiles" ) func Test(t *testing.T) { @@ -16,6 +19,19 @@ func Test(t *testing.T) { printf.Analyzer.Flags.Set("funcs", "Warn,Warnf") analysistest.Run(t, testdata, printf.Analyzer, - "a", "b", "nofmt", "typeparams", "issue68744", "issue70572") - analysistest.RunWithSuggestedFixes(t, testdata, printf.Analyzer, "fix") + "a", "b", "nofmt", "nonconst", "typeparams", "issue68744", "issue70572") +} + +func TestNonConstantFmtString_Go123(t *testing.T) { + testenv.NeedsGo1Point(t, 23) + + dir := testfiles.ExtractTxtarFileToTmp(t, filepath.Join(analysistest.TestData(), "nonconst_go123.txtar")) + analysistest.RunWithSuggestedFixes(t, dir, printf.Analyzer, "example.com/nonconst") +} + +func TestNonConstantFmtString_Go124(t *testing.T) { + testenv.NeedsGo1Point(t, 24) + + dir := testfiles.ExtractTxtarFileToTmp(t, filepath.Join(analysistest.TestData(), "nonconst_go124.txtar")) + analysistest.RunWithSuggestedFixes(t, dir, printf.Analyzer, "example.com/nonconst") } diff --git a/go/analysis/passes/printf/testdata/nonconst_go123.txtar b/go/analysis/passes/printf/testdata/nonconst_go123.txtar new file mode 100644 index 00000000000..87982917d9e --- /dev/null +++ b/go/analysis/passes/printf/testdata/nonconst_go123.txtar @@ -0,0 +1,61 @@ +This test checks for the correct suppression (or activation) of the +non-constant format string check (golang/go#60529), in a go1.23 module. + +See golang/go#71485 for details. + +-- go.mod -- +module example.com/nonconst + +go 1.23 + +-- nonconst.go -- +package nonconst + +import ( + "fmt" + "log" + "os" +) + +func _(s string) { + fmt.Printf(s) + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, s) + log.Printf(s) +} + +-- nonconst_go124.go -- +//go:build go1.24 +package nonconst + +import ( + "fmt" + "log" + "os" +) + +// With Go 1.24, the analyzer should be activated, as this is a go1.24 file. +func _(s string) { + fmt.Printf(s) // want `non-constant format string in call to fmt.Printf` + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, s) // want `non-constant format string in call to fmt.Fprintf` + log.Printf(s) // want `non-constant format string in call to log.Printf` +} + +-- nonconst_go124.go.golden -- +//go:build go1.24 +package nonconst + +import ( + "fmt" + "log" + "os" +) + +// With Go 1.24, the analyzer should be activated, as this is a go1.24 file. +func _(s string) { + fmt.Printf("%s", s) // want `non-constant format string in call to fmt.Printf` + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, "%s", s) // want `non-constant format string in call to fmt.Fprintf` + log.Printf("%s", s) // want `non-constant format string in call to log.Printf` +} diff --git a/go/analysis/passes/printf/testdata/nonconst_go124.txtar b/go/analysis/passes/printf/testdata/nonconst_go124.txtar new file mode 100644 index 00000000000..34d944ce970 --- /dev/null +++ b/go/analysis/passes/printf/testdata/nonconst_go124.txtar @@ -0,0 +1,59 @@ +This test checks for the correct suppression (or activation) of the +non-constant format string check (golang/go#60529), in a go1.24 module. + +See golang/go#71485 for details. + +-- go.mod -- +module example.com/nonconst + +go 1.24 + +-- nonconst.go -- +package nonconst + +import ( + "fmt" + "log" + "os" +) + +func _(s string) { + fmt.Printf(s) // want `non-constant format string in call to fmt.Printf` + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, s) // want `non-constant format string in call to fmt.Fprintf` + log.Printf(s) // want `non-constant format string in call to log.Printf` +} + +-- nonconst.go.golden -- +package nonconst + +import ( + "fmt" + "log" + "os" +) + +func _(s string) { + fmt.Printf("%s", s) // want `non-constant format string in call to fmt.Printf` + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, "%s", s) // want `non-constant format string in call to fmt.Fprintf` + log.Printf("%s", s) // want `non-constant format string in call to log.Printf` +} + +-- nonconst_go123.go -- +//go:build go1.23 +package nonconst + +import ( + "fmt" + "log" + "os" +) + +// The analyzer should be silent, as this is a go1.23 file. +func _(s string) { + fmt.Printf(s) + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, s) + log.Printf(s) +} diff --git a/go/analysis/passes/printf/testdata/src/fix/fix.go b/go/analysis/passes/printf/testdata/src/fix/fix.go deleted file mode 100644 index f5c9f654165..00000000000 --- a/go/analysis/passes/printf/testdata/src/fix/fix.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// This file contains tests of the printf checker's suggested fixes. - -package fix - -import ( - "fmt" - "log" - "os" -) - -func nonConstantFormat(s string) { // #60529 - fmt.Printf(s) // want `non-constant format string in call to fmt.Printf` - fmt.Printf(s, "arg") - fmt.Fprintf(os.Stderr, s) // want `non-constant format string in call to fmt.Fprintf` - log.Printf(s) // want `non-constant format string in call to log.Printf` -} diff --git a/go/analysis/passes/printf/testdata/src/fix/fix.go.golden b/go/analysis/passes/printf/testdata/src/fix/fix.go.golden deleted file mode 100644 index 57e5bb7db91..00000000000 --- a/go/analysis/passes/printf/testdata/src/fix/fix.go.golden +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// This file contains tests of the printf checker's suggested fixes. - -package fix - -import ( - "fmt" - "log" - "os" -) - -func nonConstantFormat(s string) { // #60529 - fmt.Printf("%s", s) // want `non-constant format string in call to fmt.Printf` - fmt.Printf(s, "arg") - fmt.Fprintf(os.Stderr, "%s", s) // want `non-constant format string in call to fmt.Fprintf` - log.Printf("%s", s) // want `non-constant format string in call to log.Printf` -} diff --git a/go/analysis/passes/printf/testdata/src/nonconst/nonconst.go b/go/analysis/passes/printf/testdata/src/nonconst/nonconst.go new file mode 100644 index 00000000000..40779123a52 --- /dev/null +++ b/go/analysis/passes/printf/testdata/src/nonconst/nonconst.go @@ -0,0 +1,23 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains tests of the printf checker's handling of non-constant +// format strings (golang/go#60529). + +package nonconst + +import ( + "fmt" + "log" + "os" +) + +// As the language version is empty here, and the new check is gated on go1.24, +// diagnostics are suppressed here. +func nonConstantFormat(s string) { + fmt.Printf(s) + fmt.Printf(s, "arg") + fmt.Fprintf(os.Stderr, s) + log.Printf(s) +} From d68fc51f28b0d6ea8e4fa70418d7eb8c475c6257 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 17 Jan 2025 20:33:22 -0500 Subject: [PATCH 076/309] internal/diff: Merge This CL addes a Merge operator to the diff package. It performs a simple three-way merge on two ordered lists of valid Edits, and reports a conflict if any edit could not be applied cleanly. I suspect there is considerable latitude in the implementation. This versions considers two identical insertions as non-conflicting, as is the case for redundant imports of the same package; however, it may be inappropriate for, say, identical statements that increment a counter, where the correct resolution is to keep both copies. + tests. Update golang/go#68765 Update golang/go#67049 Change-Id: I7d8bf5b0b2e601c15d3ee787499e6adc012f884b Reviewed-on: https://go-review.googlesource.com/c/tools/+/643196 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Reviewed-by: Robert Findley --- internal/diff/merge.go | 81 +++++++++++++++++++++++++++++++++++++ internal/diff/merge_test.go | 65 +++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 internal/diff/merge.go create mode 100644 internal/diff/merge_test.go diff --git a/internal/diff/merge.go b/internal/diff/merge.go new file mode 100644 index 00000000000..eeae98adf76 --- /dev/null +++ b/internal/diff/merge.go @@ -0,0 +1,81 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package diff + +import ( + "slices" +) + +// Merge merges two valid, ordered lists of edits. +// It returns zero if there was a conflict. +// +// If corresponding edits in x and y are identical, +// they are coalesced in the result. +// +// If x and y both provide different insertions at the same point, +// the insertions from x will be first in the result. +// +// TODO(adonovan): this algorithm could be improved, for example by +// working harder to coalesce non-identical edits that share a common +// deletion or common prefix of insertion (see the tests). +// Survey the academic literature for insights. +func Merge(x, y []Edit) ([]Edit, bool) { + // Make a defensive (premature) copy of the arrays. + x = slices.Clone(x) + y = slices.Clone(y) + + var merged []Edit + add := func(edit Edit) { + merged = append(merged, edit) + } + var xi, yi int + for xi < len(x) && yi < len(y) { + px := &x[xi] + py := &y[yi] + + if *px == *py { + // x and y are identical: coalesce. + add(*px) + xi++ + yi++ + + } else if px.End <= py.Start { + // x is entirely before y, + // or an insertion at start of y. + add(*px) + xi++ + + } else if py.End <= px.Start { + // y is entirely before x, + // or an insertion at start of x. + add(*py) + yi++ + + } else if px.Start < py.Start { + // x is partly before y: + // split it into a deletion and an edit. + add(Edit{px.Start, py.Start, ""}) + px.Start = py.Start + + } else if py.Start < px.Start { + // y is partly before x: + // split it into a deletion and an edit. + add(Edit{py.Start, px.Start, ""}) + py.Start = px.Start + + } else { + // x and y are unequal non-insertions + // at the same point: conflict. + return nil, false + } + } + for ; xi < len(x); xi++ { + add(x[xi]) + } + for ; yi < len(y); yi++ { + add(y[yi]) + } + return merged, true +} diff --git a/internal/diff/merge_test.go b/internal/diff/merge_test.go new file mode 100644 index 00000000000..637a13abd46 --- /dev/null +++ b/internal/diff/merge_test.go @@ -0,0 +1,65 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package diff_test + +import ( + "testing" + + "golang.org/x/tools/internal/diff" +) + +func TestMerge(t *testing.T) { + // For convenience, we test Merge using strings, not sequences + // of edits, though this does put us at the mercy of the diff + // algorithm. + for _, test := range []struct { + base, x, y string + want string // "!" => conflict + }{ + // independent insertions + {"abcdef", "abXcdef", "abcdeYf", "abXcdeYf"}, + // independent deletions + {"abcdef", "acdef", "abcdf", "acdf"}, + // colocated insertions (X first) + {"abcdef", "abcXdef", "abcYdef", "abcXYdef"}, + // colocated identical insertions (coalesced) + {"abcdef", "abcXdef", "abcXdef", "abcXdef"}, + // colocated insertions with common prefix (X first) + // TODO(adonovan): would "abcXYdef" be better? + // i.e. should we dissect the insertions? + {"abcdef", "abcXdef", "abcXYdef", "abcXXYdef"}, + // mix of identical and independent insertions (X first) + {"abcdef", "aIbcdXef", "aIbcdYef", "aIbcdXYef"}, + // independent deletions + {"abcdef", "def", "abc", ""}, + // overlapping deletions: conflict + {"abcdef", "adef", "abef", "!"}, + // overlapping deletions with distinct insertions, X first + {"abcdef", "abXef", "abcYf", "!"}, + // overlapping deletions with distinct insertions, Y first + {"abcdef", "abcXf", "abYef", "!"}, + // overlapping deletions with common insertions + {"abcdef", "abXef", "abcXf", "!"}, + // trailing insertions in X (observe X bias) + {"abcdef", "aXbXcXdXeXfX", "aYbcdef", "aXYbXcXdXeXfX"}, + // trailing insertions in Y (observe X bias) + {"abcdef", "aXbcdef", "aYbYcYdYeYfY", "aXYbYcYdYeYfY"}, + } { + dx := diff.Strings(test.base, test.x) + dy := diff.Strings(test.base, test.y) + got := "!" // conflict + if dz, ok := diff.Merge(dx, dy); ok { + var err error + got, err = diff.Apply(test.base, dz) + if err != nil { + t.Errorf("Merge(%q, %q, %q) produced invalid edits %v: %v", test.base, test.x, test.y, dz, err) + continue + } + } + if test.want != got { + t.Errorf("base=%q x=%q y=%q: got %q, want %q", test.base, test.x, test.y, got, test.want) + } + } +} From d648f9153323b3423de77faafa4023a709124096 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 13 Jan 2025 17:51:41 -0500 Subject: [PATCH 077/309] go/ast/inspector: fork ast.Inspect This change forks ast.Inspect and specializes it to avoid all unnecessary dynamic behavior. It improves the NewInspector benchmark by about 15%. It also paves the way for new features. Change-Id: I6deaef2ff7b709bf7599dca1bbc16479501c4167 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642476 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- go/ast/inspector/inspector.go | 81 ++++---- go/ast/inspector/walk.go | 339 ++++++++++++++++++++++++++++++++++ 2 files changed, 380 insertions(+), 40 deletions(-) create mode 100644 go/ast/inspector/walk.go diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index cfda8934332..fa4b7e4f769 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -194,49 +194,50 @@ func traverse(files []*ast.File) []event { extent += int(f.End() - f.Pos()) } // This estimate is based on the net/http package. - capacity := extent * 33 / 100 - if capacity > 1e6 { - capacity = 1e6 // impose some reasonable maximum + capacity := min(extent*33/100, 1e6) // impose some reasonable maximum (1M) + + v := &visitor{ + events: make([]event, 0, capacity), + stack: []event{{index: -1}}, // include an extra event so file nodes have a parent + } + for _, file := range files { + walk(v, file) } - events := make([]event, 0, capacity) + return v.events +} - var stack []event - stack = append(stack, event{index: -1}) // include an extra event so file nodes have a parent - for _, f := range files { - ast.Inspect(f, func(n ast.Node) bool { - if n != nil { - // push - ev := event{ - node: n, - typ: 0, // temporarily used to accumulate type bits of subtree - index: int32(len(events)), // push event temporarily holds own index - parent: stack[len(stack)-1].index, - } - stack = append(stack, ev) - events = append(events, ev) +type visitor struct { + events []event + stack []event +} - // 2B nodes ought to be enough for anyone! - if int32(len(events)) < 0 { - panic("event index exceeded int32") - } - } else { - // pop - top := len(stack) - 1 - ev := stack[top] - typ := typeOf(ev.node) - push := ev.index - parent := top - 1 - - events[push].typ = typ // set type of push - stack[parent].typ |= typ | ev.typ // parent's typ contains push and pop's typs. - events[push].index = int32(len(events)) // make push refer to pop - - stack = stack[:top] - events = append(events, ev) - } - return true - }) +func (v *visitor) push(n ast.Node) { + ev := event{ + node: n, + typ: 0, // temporarily used to accumulate type bits of subtree + index: int32(len(v.events)), // push event temporarily holds own index + parent: v.stack[len(v.stack)-1].index, + } + v.stack = append(v.stack, ev) + v.events = append(v.events, ev) + + // 2B nodes ought to be enough for anyone! + if int32(len(v.events)) < 0 { + panic("event index exceeded int32") } +} + +func (v *visitor) pop() { + top := len(v.stack) - 1 + ev := v.stack[top] + typ := typeOf(ev.node) + push := ev.index + parent := top - 1 + + v.events[push].typ = typ // set type of push + v.stack[parent].typ |= typ | ev.typ // parent's typ contains push and pop's typs. + v.events[push].index = int32(len(v.events)) // make push refer to pop - return events + v.stack = v.stack[:top] + v.events = append(v.events, ev) } diff --git a/go/ast/inspector/walk.go b/go/ast/inspector/walk.go new file mode 100644 index 00000000000..ed2bf72c22b --- /dev/null +++ b/go/ast/inspector/walk.go @@ -0,0 +1,339 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package inspector + +// This file is a fork of ast.Inspect to reduce unnecessary dynamic +// calls and to gather edge information. +// +// Consistency with the original is ensured by TestInspectAllNodes. + +import ( + "fmt" + "go/ast" +) + +func walkList[N ast.Node](v *visitor, list []N) { + for _, node := range list { + walk(v, node) + } +} + +func walk(v *visitor, node ast.Node) { + v.push(node) + + // walk children + // (the order of the cases matches the order + // of the corresponding node types in ast.go) + switch n := node.(type) { + // Comments and fields + case *ast.Comment: + // nothing to do + + case *ast.CommentGroup: + walkList(v, n.List) + + case *ast.Field: + if n.Doc != nil { + walk(v, n.Doc) + } + walkList(v, n.Names) + if n.Type != nil { + walk(v, n.Type) + } + if n.Tag != nil { + walk(v, n.Tag) + } + if n.Comment != nil { + walk(v, n.Comment) + } + + case *ast.FieldList: + walkList(v, n.List) + + // Expressions + case *ast.BadExpr, *ast.Ident, *ast.BasicLit: + // nothing to do + + case *ast.Ellipsis: + if n.Elt != nil { + walk(v, n.Elt) + } + + case *ast.FuncLit: + walk(v, n.Type) + walk(v, n.Body) + + case *ast.CompositeLit: + if n.Type != nil { + walk(v, n.Type) + } + walkList(v, n.Elts) + + case *ast.ParenExpr: + walk(v, n.X) + + case *ast.SelectorExpr: + walk(v, n.X) + walk(v, n.Sel) + + case *ast.IndexExpr: + walk(v, n.X) + walk(v, n.Index) + + case *ast.IndexListExpr: + walk(v, n.X) + walkList(v, n.Indices) + + case *ast.SliceExpr: + walk(v, n.X) + if n.Low != nil { + walk(v, n.Low) + } + if n.High != nil { + walk(v, n.High) + } + if n.Max != nil { + walk(v, n.Max) + } + + case *ast.TypeAssertExpr: + walk(v, n.X) + if n.Type != nil { + walk(v, n.Type) + } + + case *ast.CallExpr: + walk(v, n.Fun) + walkList(v, n.Args) + + case *ast.StarExpr: + walk(v, n.X) + + case *ast.UnaryExpr: + walk(v, n.X) + + case *ast.BinaryExpr: + walk(v, n.X) + walk(v, n.Y) + + case *ast.KeyValueExpr: + walk(v, n.Key) + walk(v, n.Value) + + // Types + case *ast.ArrayType: + if n.Len != nil { + walk(v, n.Len) + } + walk(v, n.Elt) + + case *ast.StructType: + walk(v, n.Fields) + + case *ast.FuncType: + if n.TypeParams != nil { + walk(v, n.TypeParams) + } + if n.Params != nil { + walk(v, n.Params) + } + if n.Results != nil { + walk(v, n.Results) + } + + case *ast.InterfaceType: + walk(v, n.Methods) + + case *ast.MapType: + walk(v, n.Key) + walk(v, n.Value) + + case *ast.ChanType: + walk(v, n.Value) + + // Statements + case *ast.BadStmt: + // nothing to do + + case *ast.DeclStmt: + walk(v, n.Decl) + + case *ast.EmptyStmt: + // nothing to do + + case *ast.LabeledStmt: + walk(v, n.Label) + walk(v, n.Stmt) + + case *ast.ExprStmt: + walk(v, n.X) + + case *ast.SendStmt: + walk(v, n.Chan) + walk(v, n.Value) + + case *ast.IncDecStmt: + walk(v, n.X) + + case *ast.AssignStmt: + walkList(v, n.Lhs) + walkList(v, n.Rhs) + + case *ast.GoStmt: + walk(v, n.Call) + + case *ast.DeferStmt: + walk(v, n.Call) + + case *ast.ReturnStmt: + walkList(v, n.Results) + + case *ast.BranchStmt: + if n.Label != nil { + walk(v, n.Label) + } + + case *ast.BlockStmt: + walkList(v, n.List) + + case *ast.IfStmt: + if n.Init != nil { + walk(v, n.Init) + } + walk(v, n.Cond) + walk(v, n.Body) + if n.Else != nil { + walk(v, n.Else) + } + + case *ast.CaseClause: + walkList(v, n.List) + walkList(v, n.Body) + + case *ast.SwitchStmt: + if n.Init != nil { + walk(v, n.Init) + } + if n.Tag != nil { + walk(v, n.Tag) + } + walk(v, n.Body) + + case *ast.TypeSwitchStmt: + if n.Init != nil { + walk(v, n.Init) + } + walk(v, n.Assign) + walk(v, n.Body) + + case *ast.CommClause: + if n.Comm != nil { + walk(v, n.Comm) + } + walkList(v, n.Body) + + case *ast.SelectStmt: + walk(v, n.Body) + + case *ast.ForStmt: + if n.Init != nil { + walk(v, n.Init) + } + if n.Cond != nil { + walk(v, n.Cond) + } + if n.Post != nil { + walk(v, n.Post) + } + walk(v, n.Body) + + case *ast.RangeStmt: + if n.Key != nil { + walk(v, n.Key) + } + if n.Value != nil { + walk(v, n.Value) + } + walk(v, n.X) + walk(v, n.Body) + + // Declarations + case *ast.ImportSpec: + if n.Doc != nil { + walk(v, n.Doc) + } + if n.Name != nil { + walk(v, n.Name) + } + walk(v, n.Path) + if n.Comment != nil { + walk(v, n.Comment) + } + + case *ast.ValueSpec: + if n.Doc != nil { + walk(v, n.Doc) + } + walkList(v, n.Names) + if n.Type != nil { + walk(v, n.Type) + } + walkList(v, n.Values) + if n.Comment != nil { + walk(v, n.Comment) + } + + case *ast.TypeSpec: + if n.Doc != nil { + walk(v, n.Doc) + } + walk(v, n.Name) + if n.TypeParams != nil { + walk(v, n.TypeParams) + } + walk(v, n.Type) + if n.Comment != nil { + walk(v, n.Comment) + } + + case *ast.BadDecl: + // nothing to do + + case *ast.GenDecl: + if n.Doc != nil { + walk(v, n.Doc) + } + walkList(v, n.Specs) + + case *ast.FuncDecl: + if n.Doc != nil { + walk(v, n.Doc) + } + if n.Recv != nil { + walk(v, n.Recv) + } + walk(v, n.Name) + walk(v, n.Type) + if n.Body != nil { + walk(v, n.Body) + } + + case *ast.File: + if n.Doc != nil { + walk(v, n.Doc) + } + walk(v, n.Name) + walkList(v, n.Decls) + // don't walk n.Comments - they have been + // visited already through the individual + // nodes + + default: + // (includes *ast.Package) + panic(fmt.Sprintf("Walk: unexpected node type %T", n)) + } + + v.pop() +} From db7fffcc5a68909518dfa14cc3004c6887c8b0c4 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 13 Jan 2025 22:45:40 -0500 Subject: [PATCH 078/309] go/ast/inspector: separate stack + event types This CL introduces a distinct data type for the elements of the traversal stack instead of reusing the event type This allows us to more clearly define the roles of each field in preparation for the recording of edge information, which would need them to diverge. Change-Id: Id47c262e1bb28536aa04937c0ccf730388772e01 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642477 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/ast/inspector/inspector.go | 56 ++++++++++++++++++++++------------- go/ast/inspector/walk.go | 2 +- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index fa4b7e4f769..7f32e62f155 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -198,7 +198,7 @@ func traverse(files []*ast.File) []event { v := &visitor{ events: make([]event, 0, capacity), - stack: []event{{index: -1}}, // include an extra event so file nodes have a parent + stack: []item{{index: -1}}, // include an extra event so file nodes have a parent } for _, file := range files { walk(v, file) @@ -208,18 +208,30 @@ func traverse(files []*ast.File) []event { type visitor struct { events []event - stack []event + stack []item } -func (v *visitor) push(n ast.Node) { - ev := event{ - node: n, - typ: 0, // temporarily used to accumulate type bits of subtree - index: int32(len(v.events)), // push event temporarily holds own index - parent: v.stack[len(v.stack)-1].index, - } - v.stack = append(v.stack, ev) - v.events = append(v.events, ev) +type item struct { + index int32 // index of current node's push event + parentIndex int32 // index of parent node's push event + typAccum uint64 // accumulated type bits of current node's descendents +} + +func (v *visitor) push(node ast.Node) { + var ( + index = int32(len(v.events)) + parentIndex = v.stack[len(v.stack)-1].index + ) + v.events = append(v.events, event{ + node: node, + parent: parentIndex, + typ: typeOf(node), + index: 0, // (pop index is set later by visitor.pop) + }) + v.stack = append(v.stack, item{ + index: index, + parentIndex: parentIndex, + }) // 2B nodes ought to be enough for anyone! if int32(len(v.events)) < 0 { @@ -227,17 +239,21 @@ func (v *visitor) push(n ast.Node) { } } -func (v *visitor) pop() { +func (v *visitor) pop(node ast.Node) { top := len(v.stack) - 1 - ev := v.stack[top] - typ := typeOf(ev.node) - push := ev.index - parent := top - 1 + current := v.stack[top] - v.events[push].typ = typ // set type of push - v.stack[parent].typ |= typ | ev.typ // parent's typ contains push and pop's typs. - v.events[push].index = int32(len(v.events)) // make push refer to pop + push := &v.events[current.index] + parent := &v.stack[top-1] + + push.index = int32(len(v.events)) // make push event refer to pop + parent.typAccum |= current.typAccum | push.typ // accumulate type bits into parent v.stack = v.stack[:top] - v.events = append(v.events, ev) + + v.events = append(v.events, event{ + node: node, + typ: current.typAccum, + index: current.index, + }) } diff --git a/go/ast/inspector/walk.go b/go/ast/inspector/walk.go index ed2bf72c22b..4d0b6693c3f 100644 --- a/go/ast/inspector/walk.go +++ b/go/ast/inspector/walk.go @@ -335,5 +335,5 @@ func walk(v *visitor, node ast.Node) { panic(fmt.Sprintf("Walk: unexpected node type %T", n)) } - v.pop() + v.pop(node) } From 80ffd3ce7471d44b73cc53ebc99ea38905d14acc Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 13 Jan 2025 22:46:13 -0500 Subject: [PATCH 079/309] internal/astutil/cursor: add Cursor.Edge method This CL defines the Cursor.Edge method, which returns the identity of the field in the parent node that refers to the current node and, if it is a slice, the index. For example, the CallExpr f(x, y) has three children, all Idents: f has edge.CallExpr_Fun and index -1, and x and y have edge.CallExpr_Args and indices 0 and 1 respectively. The edge enumeration lives in the internal/astutil/edge pacakge so that it can be shared by inspector and cursor. The inspector records the edge and index during its walk, and squirrels the information in the hitherto unused event.parent field of each pop event. The edge and index are packed into 32 bits, giving us an upper bound of 32M elements in any []ast.Node, which seems like plenty. Change-Id: Iad92d56629a119a9d69d486b2628ac9071f88bd3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642478 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- go/ast/inspector/inspector.go | 47 ++++- go/ast/inspector/walk.go | 220 +++++++++---------- internal/astutil/cursor/cursor.go | 23 +- internal/astutil/cursor/cursor_test.go | 48 +++++ internal/astutil/cursor/hooks.go | 6 +- internal/astutil/edge/edge.go | 278 +++++++++++++++++++++++++ 6 files changed, 500 insertions(+), 122 deletions(-) create mode 100644 internal/astutil/edge/edge.go diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index 7f32e62f155..8f6a510f248 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -37,6 +37,8 @@ package inspector import ( "go/ast" _ "unsafe" + + "golang.org/x/tools/internal/astutil/edge" ) // An Inspector provides methods for inspecting @@ -48,6 +50,21 @@ type Inspector struct { //go:linkname events func events(in *Inspector) []event { return in.events } +func packEdgeKindAndIndex(ek edge.Kind, index int) int32 { + return int32(uint32(index+1)<<7 | uint32(ek)) +} + +// unpackEdgeKindAndIndex unpacks the edge kind and edge index (within +// an []ast.Node slice) from the parent field of a pop event. +// +//go:linkname unpackEdgeKindAndIndex +func unpackEdgeKindAndIndex(x int32) (edge.Kind, int) { + // The "parent" field of a pop node holds the + // edge Kind in the lower 7 bits and the index+1 + // in the upper 25. + return edge.Kind(x & 0x7f), int(x>>7) - 1 +} + // New returns an Inspector for the specified syntax trees. func New(files []*ast.File) *Inspector { return &Inspector{traverse(files)} @@ -59,7 +76,7 @@ type event struct { node ast.Node typ uint64 // typeOf(node) on push event, or union of typ strictly between push and pop events on pop events index int32 // index of corresponding push or pop event - parent int32 // index of parent's push node (defined for push nodes only) + parent int32 // index of parent's push node (push nodes only), or packed edge kind/index (pop nodes only) } // TODO: Experiment with storing only the second word of event.node (unsafe.Pointer). @@ -201,7 +218,7 @@ func traverse(files []*ast.File) []event { stack: []item{{index: -1}}, // include an extra event so file nodes have a parent } for _, file := range files { - walk(v, file) + walk(v, edge.Invalid, -1, file) } return v.events } @@ -212,12 +229,13 @@ type visitor struct { } type item struct { - index int32 // index of current node's push event - parentIndex int32 // index of parent node's push event - typAccum uint64 // accumulated type bits of current node's descendents + index int32 // index of current node's push event + parentIndex int32 // index of parent node's push event + typAccum uint64 // accumulated type bits of current node's descendents + edgeKindAndIndex int32 // edge.Kind and index, bit packed } -func (v *visitor) push(node ast.Node) { +func (v *visitor) push(ek edge.Kind, eindex int, node ast.Node) { var ( index = int32(len(v.events)) parentIndex = v.stack[len(v.stack)-1].index @@ -229,14 +247,20 @@ func (v *visitor) push(node ast.Node) { index: 0, // (pop index is set later by visitor.pop) }) v.stack = append(v.stack, item{ - index: index, - parentIndex: parentIndex, + index: index, + parentIndex: parentIndex, + edgeKindAndIndex: packEdgeKindAndIndex(ek, eindex), }) // 2B nodes ought to be enough for anyone! if int32(len(v.events)) < 0 { panic("event index exceeded int32") } + + // 32M elements in an []ast.Node ought to be enough for anyone! + if ek2, eindex2 := unpackEdgeKindAndIndex(packEdgeKindAndIndex(ek, eindex)); ek2 != ek || eindex2 != eindex { + panic("Node slice index exceeded uint25") + } } func (v *visitor) pop(node ast.Node) { @@ -252,8 +276,9 @@ func (v *visitor) pop(node ast.Node) { v.stack = v.stack[:top] v.events = append(v.events, event{ - node: node, - typ: current.typAccum, - index: current.index, + node: node, + typ: current.typAccum, + index: current.index, + parent: int32(current.edgeKindAndIndex), // see [unpackEdgeKindAndIndex] }) } diff --git a/go/ast/inspector/walk.go b/go/ast/inspector/walk.go index 4d0b6693c3f..5a42174a0a0 100644 --- a/go/ast/inspector/walk.go +++ b/go/ast/inspector/walk.go @@ -12,16 +12,18 @@ package inspector import ( "fmt" "go/ast" + + "golang.org/x/tools/internal/astutil/edge" ) -func walkList[N ast.Node](v *visitor, list []N) { - for _, node := range list { - walk(v, node) +func walkList[N ast.Node](v *visitor, ek edge.Kind, list []N) { + for i, node := range list { + walk(v, ek, i, node) } } -func walk(v *visitor, node ast.Node) { - v.push(node) +func walk(v *visitor, ek edge.Kind, index int, node ast.Node) { + v.push(ek, index, node) // walk children // (the order of the cases matches the order @@ -32,25 +34,25 @@ func walk(v *visitor, node ast.Node) { // nothing to do case *ast.CommentGroup: - walkList(v, n.List) + walkList(v, edge.CommentGroup_List, n.List) case *ast.Field: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.Field_Doc, -1, n.Doc) } - walkList(v, n.Names) + walkList(v, edge.Field_Names, n.Names) if n.Type != nil { - walk(v, n.Type) + walk(v, edge.Field_Type, -1, n.Type) } if n.Tag != nil { - walk(v, n.Tag) + walk(v, edge.Field_Tag, -1, n.Tag) } if n.Comment != nil { - walk(v, n.Comment) + walk(v, edge.Field_Comment, -1, n.Comment) } case *ast.FieldList: - walkList(v, n.List) + walkList(v, edge.FieldList_List, n.List) // Expressions case *ast.BadExpr, *ast.Ident, *ast.BasicLit: @@ -58,244 +60,244 @@ func walk(v *visitor, node ast.Node) { case *ast.Ellipsis: if n.Elt != nil { - walk(v, n.Elt) + walk(v, edge.Ellipsis_Elt, -1, n.Elt) } case *ast.FuncLit: - walk(v, n.Type) - walk(v, n.Body) + walk(v, edge.FuncLit_Type, -1, n.Type) + walk(v, edge.FuncLit_Body, -1, n.Body) case *ast.CompositeLit: if n.Type != nil { - walk(v, n.Type) + walk(v, edge.CompositeLit_Type, -1, n.Type) } - walkList(v, n.Elts) + walkList(v, edge.CompositeLit_Elts, n.Elts) case *ast.ParenExpr: - walk(v, n.X) + walk(v, edge.ParenExpr_X, -1, n.X) case *ast.SelectorExpr: - walk(v, n.X) - walk(v, n.Sel) + walk(v, edge.SelectorExpr_X, -1, n.X) + walk(v, edge.SelectorExpr_Sel, -1, n.Sel) case *ast.IndexExpr: - walk(v, n.X) - walk(v, n.Index) + walk(v, edge.IndexExpr_X, -1, n.X) + walk(v, edge.IndexExpr_Index, -1, n.Index) case *ast.IndexListExpr: - walk(v, n.X) - walkList(v, n.Indices) + walk(v, edge.IndexListExpr_X, -1, n.X) + walkList(v, edge.IndexListExpr_Indices, n.Indices) case *ast.SliceExpr: - walk(v, n.X) + walk(v, edge.SliceExpr_X, -1, n.X) if n.Low != nil { - walk(v, n.Low) + walk(v, edge.SliceExpr_Low, -1, n.Low) } if n.High != nil { - walk(v, n.High) + walk(v, edge.SliceExpr_High, -1, n.High) } if n.Max != nil { - walk(v, n.Max) + walk(v, edge.SliceExpr_Max, -1, n.Max) } case *ast.TypeAssertExpr: - walk(v, n.X) + walk(v, edge.TypeAssertExpr_X, -1, n.X) if n.Type != nil { - walk(v, n.Type) + walk(v, edge.TypeAssertExpr_Type, -1, n.Type) } case *ast.CallExpr: - walk(v, n.Fun) - walkList(v, n.Args) + walk(v, edge.CallExpr_Fun, -1, n.Fun) + walkList(v, edge.CallExpr_Args, n.Args) case *ast.StarExpr: - walk(v, n.X) + walk(v, edge.StarExpr_X, -1, n.X) case *ast.UnaryExpr: - walk(v, n.X) + walk(v, edge.UnaryExpr_X, -1, n.X) case *ast.BinaryExpr: - walk(v, n.X) - walk(v, n.Y) + walk(v, edge.BinaryExpr_X, -1, n.X) + walk(v, edge.BinaryExpr_Y, -1, n.Y) case *ast.KeyValueExpr: - walk(v, n.Key) - walk(v, n.Value) + walk(v, edge.KeyValueExpr_Key, -1, n.Key) + walk(v, edge.KeyValueExpr_Value, -1, n.Value) // Types case *ast.ArrayType: if n.Len != nil { - walk(v, n.Len) + walk(v, edge.ArrayType_Len, -1, n.Len) } - walk(v, n.Elt) + walk(v, edge.ArrayType_Elt, -1, n.Elt) case *ast.StructType: - walk(v, n.Fields) + walk(v, edge.StructType_Fields, -1, n.Fields) case *ast.FuncType: if n.TypeParams != nil { - walk(v, n.TypeParams) + walk(v, edge.FuncType_TypeParams, -1, n.TypeParams) } if n.Params != nil { - walk(v, n.Params) + walk(v, edge.FuncType_Params, -1, n.Params) } if n.Results != nil { - walk(v, n.Results) + walk(v, edge.FuncType_Results, -1, n.Results) } case *ast.InterfaceType: - walk(v, n.Methods) + walk(v, edge.InterfaceType_Methods, -1, n.Methods) case *ast.MapType: - walk(v, n.Key) - walk(v, n.Value) + walk(v, edge.MapType_Key, -1, n.Key) + walk(v, edge.MapType_Value, -1, n.Value) case *ast.ChanType: - walk(v, n.Value) + walk(v, edge.ChanType_Value, -1, n.Value) // Statements case *ast.BadStmt: // nothing to do case *ast.DeclStmt: - walk(v, n.Decl) + walk(v, edge.DeclStmt_Decl, -1, n.Decl) case *ast.EmptyStmt: // nothing to do case *ast.LabeledStmt: - walk(v, n.Label) - walk(v, n.Stmt) + walk(v, edge.LabeledStmt_Label, -1, n.Label) + walk(v, edge.LabeledStmt_Stmt, -1, n.Stmt) case *ast.ExprStmt: - walk(v, n.X) + walk(v, edge.ExprStmt_X, -1, n.X) case *ast.SendStmt: - walk(v, n.Chan) - walk(v, n.Value) + walk(v, edge.SendStmt_Chan, -1, n.Chan) + walk(v, edge.SendStmt_Value, -1, n.Value) case *ast.IncDecStmt: - walk(v, n.X) + walk(v, edge.IncDecStmt_X, -1, n.X) case *ast.AssignStmt: - walkList(v, n.Lhs) - walkList(v, n.Rhs) + walkList(v, edge.AssignStmt_Lhs, n.Lhs) + walkList(v, edge.AssignStmt_Rhs, n.Rhs) case *ast.GoStmt: - walk(v, n.Call) + walk(v, edge.GoStmt_Call, -1, n.Call) case *ast.DeferStmt: - walk(v, n.Call) + walk(v, edge.DeferStmt_Call, -1, n.Call) case *ast.ReturnStmt: - walkList(v, n.Results) + walkList(v, edge.ReturnStmt_Results, n.Results) case *ast.BranchStmt: if n.Label != nil { - walk(v, n.Label) + walk(v, edge.BranchStmt_Label, -1, n.Label) } case *ast.BlockStmt: - walkList(v, n.List) + walkList(v, edge.BlockStmt_List, n.List) case *ast.IfStmt: if n.Init != nil { - walk(v, n.Init) + walk(v, edge.IfStmt_Init, -1, n.Init) } - walk(v, n.Cond) - walk(v, n.Body) + walk(v, edge.IfStmt_Cond, -1, n.Cond) + walk(v, edge.IfStmt_Body, -1, n.Body) if n.Else != nil { - walk(v, n.Else) + walk(v, edge.IfStmt_Else, -1, n.Else) } case *ast.CaseClause: - walkList(v, n.List) - walkList(v, n.Body) + walkList(v, edge.CaseClause_List, n.List) + walkList(v, edge.CaseClause_Body, n.Body) case *ast.SwitchStmt: if n.Init != nil { - walk(v, n.Init) + walk(v, edge.SwitchStmt_Init, -1, n.Init) } if n.Tag != nil { - walk(v, n.Tag) + walk(v, edge.SwitchStmt_Tag, -1, n.Tag) } - walk(v, n.Body) + walk(v, edge.SwitchStmt_Body, -1, n.Body) case *ast.TypeSwitchStmt: if n.Init != nil { - walk(v, n.Init) + walk(v, edge.TypeSwitchStmt_Init, -1, n.Init) } - walk(v, n.Assign) - walk(v, n.Body) + walk(v, edge.TypeSwitchStmt_Assign, -1, n.Assign) + walk(v, edge.TypeSwitchStmt_Body, -1, n.Body) case *ast.CommClause: if n.Comm != nil { - walk(v, n.Comm) + walk(v, edge.CommClause_Comm, -1, n.Comm) } - walkList(v, n.Body) + walkList(v, edge.CommClause_Body, n.Body) case *ast.SelectStmt: - walk(v, n.Body) + walk(v, edge.SelectStmt_Body, -1, n.Body) case *ast.ForStmt: if n.Init != nil { - walk(v, n.Init) + walk(v, edge.ForStmt_Init, -1, n.Init) } if n.Cond != nil { - walk(v, n.Cond) + walk(v, edge.ForStmt_Cond, -1, n.Cond) } if n.Post != nil { - walk(v, n.Post) + walk(v, edge.ForStmt_Post, -1, n.Post) } - walk(v, n.Body) + walk(v, edge.ForStmt_Body, -1, n.Body) case *ast.RangeStmt: if n.Key != nil { - walk(v, n.Key) + walk(v, edge.RangeStmt_Key, -1, n.Key) } if n.Value != nil { - walk(v, n.Value) + walk(v, edge.RangeStmt_Value, -1, n.Value) } - walk(v, n.X) - walk(v, n.Body) + walk(v, edge.RangeStmt_X, -1, n.X) + walk(v, edge.RangeStmt_Body, -1, n.Body) // Declarations case *ast.ImportSpec: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.ImportSpec_Doc, -1, n.Doc) } if n.Name != nil { - walk(v, n.Name) + walk(v, edge.ImportSpec_Name, -1, n.Name) } - walk(v, n.Path) + walk(v, edge.ImportSpec_Path, -1, n.Path) if n.Comment != nil { - walk(v, n.Comment) + walk(v, edge.ImportSpec_Comment, -1, n.Comment) } case *ast.ValueSpec: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.ValueSpec_Doc, -1, n.Doc) } - walkList(v, n.Names) + walkList(v, edge.ValueSpec_Names, n.Names) if n.Type != nil { - walk(v, n.Type) + walk(v, edge.ValueSpec_Type, -1, n.Type) } - walkList(v, n.Values) + walkList(v, edge.ValueSpec_Values, n.Values) if n.Comment != nil { - walk(v, n.Comment) + walk(v, edge.ValueSpec_Comment, -1, n.Comment) } case *ast.TypeSpec: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.TypeSpec_Doc, -1, n.Doc) } - walk(v, n.Name) + walk(v, edge.TypeSpec_Name, -1, n.Name) if n.TypeParams != nil { - walk(v, n.TypeParams) + walk(v, edge.TypeSpec_TypeParams, -1, n.TypeParams) } - walk(v, n.Type) + walk(v, edge.TypeSpec_Type, -1, n.Type) if n.Comment != nil { - walk(v, n.Comment) + walk(v, edge.TypeSpec_Comment, -1, n.Comment) } case *ast.BadDecl: @@ -303,29 +305,29 @@ func walk(v *visitor, node ast.Node) { case *ast.GenDecl: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.GenDecl_Doc, -1, n.Doc) } - walkList(v, n.Specs) + walkList(v, edge.GenDecl_Specs, n.Specs) case *ast.FuncDecl: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.FuncDecl_Doc, -1, n.Doc) } if n.Recv != nil { - walk(v, n.Recv) + walk(v, edge.FuncDecl_Recv, -1, n.Recv) } - walk(v, n.Name) - walk(v, n.Type) + walk(v, edge.FuncDecl_Name, -1, n.Name) + walk(v, edge.FuncDecl_Type, -1, n.Type) if n.Body != nil { - walk(v, n.Body) + walk(v, edge.FuncDecl_Body, -1, n.Body) } case *ast.File: if n.Doc != nil { - walk(v, n.Doc) + walk(v, edge.File_Doc, -1, n.Doc) } - walk(v, n.Name) - walkList(v, n.Decls) + walk(v, edge.File_Name, -1, n.Name) + walkList(v, edge.File_Decls, n.Decls) // don't walk n.Comments - they have been // visited already through the individual // nodes diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 24fec99c8b3..38a35f64ce0 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -22,6 +22,7 @@ import ( "slices" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/astutil/edge" ) // A Cursor represents an [ast.Node]. It is immutable. @@ -207,6 +208,25 @@ func (c Cursor) Parent() Cursor { return Cursor{c.in, c.events()[c.index].parent} } +// Edge returns the identity of the field in the parent node +// that holds this cursor's node, and if it is a list, the index within it. +// +// For example, f(x, y) is a CallExpr whose three children are Idents. +// f has edge kind [edge.CallExpr_Fun] and index -1. +// x and y have kind [edge.CallExpr_Args] and indices 0 and 1, respectively. +// +// Edge must not be called on the Root node (whose [Cursor.Node] returns nil). +// +// If called on a child of the Root node, it returns ([edge.Invalid], -1). +func (c Cursor) Edge() (edge.Kind, int) { + if c.index < 0 { + panic("Cursor.Edge called on Root node") + } + events := c.events() + pop := events[c.index].index + return unpackEdgeKindAndIndex(events[pop].parent) +} + // NextSibling returns the cursor for the next sibling node in the same list // (for example, of files, decls, specs, statements, fields, or expressions) as // the current node. It returns (zero, false) if the node is the last node in @@ -305,7 +325,8 @@ func (c Cursor) LastChild() (Cursor, bool) { // // So, do not assume that the previous sibling of an ast.Stmt is also // an ast.Stmt, or if it is, that they are executed sequentially, -// unless you have established that, say, its parent is a BlockStmt. +// unless you have established that, say, its parent is a BlockStmt +// or its [Cursor.Edge] is [edge.BlockStmt_List]. // For example, given "for S1; ; S2 {}", the predecessor of S2 is S1, // even though they are not executed in sequence. func (c Cursor) Children() iter.Seq[Cursor] { diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 06cd358c22e..e04f8c24b89 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -22,6 +22,7 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" ) // net/http package @@ -127,6 +128,13 @@ func g() { for curFunc := range cursor.Root(inspect).Preorder(funcDecls...) { _ = curFunc.Node().(*ast.FuncDecl) + + // Check edge and index. + if e, idx := curFunc.Edge(); e != edge.File_Decls || idx != nfuncs { + t.Errorf("%v.Edge() = (%v, %v), want edge.File_Decls, %d", + curFunc, e, idx, nfuncs) + } + nfuncs++ stack := curFunc.Stack(nil) @@ -328,6 +336,46 @@ func TestCursor_FindNode(t *testing.T) { // TODO(adonovan): FindPos needs a test (not just a benchmark). } +func TestCursor_Edge(t *testing.T) { + root := cursor.Root(netInspect) + for cur := range root.Preorder() { + if cur == root { + continue // root node + } + + e, idx := cur.Edge() + parent := cur.Parent() + + // ast.File, child of root? + if parent.Node() == nil { + if e != edge.Invalid || idx != -1 { + t.Errorf("%v.Edge = (%v, %d), want (Invalid, -1)", cur, e, idx) + } + continue + } + + // Check Edge.NodeType matches type of Parent.Node. + if e.NodeType() != reflect.TypeOf(parent.Node()) { + t.Errorf("Edge.NodeType = %v, Parent.Node has type %T", + e.NodeType(), parent.Node()) + } + + // Check that reflection on the parent finds the current node. + fv := reflect.ValueOf(parent.Node()).Elem().FieldByName(e.FieldName()) + if idx >= 0 { + fv = fv.Index(idx) // element of []ast.Node + } + if fv.Kind() == reflect.Interface { + fv = fv.Elem() // e.g. ast.Expr -> *ast.Ident + } + got := fv.Interface().(ast.Node) + if got != cur.Node() { + t.Errorf("%v.Edge = (%v, %d); FieldName/Index reflection gave %T@%s, not original node", + cur, e, idx, got, netFset.Position(got.Pos())) + } + } +} + func is[T any](x any) bool { _, ok := x.(T) return ok diff --git a/internal/astutil/cursor/hooks.go b/internal/astutil/cursor/hooks.go index 47aaaae37e0..8b61f5ddc11 100644 --- a/internal/astutil/cursor/hooks.go +++ b/internal/astutil/cursor/hooks.go @@ -11,6 +11,7 @@ import ( _ "unsafe" // for go:linkname "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/astutil/edge" ) // This file defines backdoor access to inspector. @@ -21,7 +22,7 @@ type event struct { node ast.Node typ uint64 // typeOf(node) on push event, or union of typ strictly between push and pop events on pop events index int32 // index of corresponding push or pop event (relative to this event's index, +ve=push, -ve=pop) - parent int32 // index of parent's push node (defined for push nodes only) + parent int32 // index of parent's push node (push nodes only); or edge and index, bit packed (pop nodes only) } //go:linkname maskOf golang.org/x/tools/go/ast/inspector.maskOf @@ -30,4 +31,7 @@ func maskOf(nodes []ast.Node) uint64 //go:linkname events golang.org/x/tools/go/ast/inspector.events func events(in *inspector.Inspector) []event +//go:linkname unpackEdgeKindAndIndex golang.org/x/tools/go/ast/inspector.unpackEdgeKindAndIndex +func unpackEdgeKindAndIndex(int32) (edge.Kind, int) + func (c Cursor) events() []event { return events(c.in) } diff --git a/internal/astutil/edge/edge.go b/internal/astutil/edge/edge.go new file mode 100644 index 00000000000..bf945a8f632 --- /dev/null +++ b/internal/astutil/edge/edge.go @@ -0,0 +1,278 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package edge defines identifiers for each field of an ast.Node +// struct type that refers to another Node. +package edge + +import ( + "fmt" + "go/ast" + "reflect" +) + +// A Kind describes a field of an ast.Node struct. +type Kind uint8 + +// String returns a description of the edge kind. +func (k Kind) String() string { + if k == Invalid { + return "" + } + info := fieldInfos[k] + return fmt.Sprintf("%v.%s", info.nodeType.Elem().Name(), info.fieldName) +} + +// NodeType returns the pointer-to-struct type of the ast.Node implementation. +func (k Kind) NodeType() reflect.Type { return fieldInfos[k].nodeType } + +// FieldName returns the name of the field. +func (k Kind) FieldName() string { return fieldInfos[k].fieldName } + +// FieldType returns the declared type of the field. +func (k Kind) FieldType() reflect.Type { return fieldInfos[k].fieldType } + +const ( + Invalid Kind = iota // for nodes at the root of the traversal + + // Kinds are sorted alphabetically. + // Numbering is not stable. + // Each is named Type_Field, where Type is the + // ast.Node struct type and Field is the name of the field + + ArrayType_Elt + ArrayType_Len + AssignStmt_Lhs + AssignStmt_Rhs + BinaryExpr_X + BinaryExpr_Y + BlockStmt_List + BranchStmt_Label + CallExpr_Args + CallExpr_Fun + CaseClause_Body + CaseClause_List + ChanType_Value + CommClause_Body + CommClause_Comm + CommentGroup_List + CompositeLit_Elts + CompositeLit_Type + DeclStmt_Decl + DeferStmt_Call + Ellipsis_Elt + ExprStmt_X + FieldList_List + Field_Comment + Field_Doc + Field_Names + Field_Tag + Field_Type + File_Decls + File_Doc + File_Name + ForStmt_Body + ForStmt_Cond + ForStmt_Init + ForStmt_Post + FuncDecl_Body + FuncDecl_Doc + FuncDecl_Name + FuncDecl_Recv + FuncDecl_Type + FuncLit_Body + FuncLit_Type + FuncType_Params + FuncType_Results + FuncType_TypeParams + GenDecl_Doc + GenDecl_Specs + GoStmt_Call + IfStmt_Body + IfStmt_Cond + IfStmt_Else + IfStmt_Init + ImportSpec_Comment + ImportSpec_Doc + ImportSpec_Name + ImportSpec_Path + IncDecStmt_X + IndexExpr_Index + IndexExpr_X + IndexListExpr_Indices + IndexListExpr_X + InterfaceType_Methods + KeyValueExpr_Key + KeyValueExpr_Value + LabeledStmt_Label + LabeledStmt_Stmt + MapType_Key + MapType_Value + ParenExpr_X + RangeStmt_Body + RangeStmt_Key + RangeStmt_Value + RangeStmt_X + ReturnStmt_Results + SelectStmt_Body + SelectorExpr_Sel + SelectorExpr_X + SendStmt_Chan + SendStmt_Value + SliceExpr_High + SliceExpr_Low + SliceExpr_Max + SliceExpr_X + StarExpr_X + StructType_Fields + SwitchStmt_Body + SwitchStmt_Init + SwitchStmt_Tag + TypeAssertExpr_Type + TypeAssertExpr_X + TypeSpec_Comment + TypeSpec_Doc + TypeSpec_Name + TypeSpec_Type + TypeSpec_TypeParams + TypeSwitchStmt_Assign + TypeSwitchStmt_Body + TypeSwitchStmt_Init + UnaryExpr_X + ValueSpec_Comment + ValueSpec_Doc + ValueSpec_Names + ValueSpec_Type + ValueSpec_Values + + maxKind +) + +// Assert that the encoding fits in 7 bits, +// as the inspector relies on this. +// (We are currently at 104.) +var _ = [1 << 7]struct{}{}[maxKind] + +type fieldInfo struct { + nodeType reflect.Type // pointer-to-struct type of ast.Node implementation + fieldName string + fieldType reflect.Type +} + +func info[N ast.Node](fieldName string) fieldInfo { + nodePtrType := reflect.TypeFor[N]() + f, ok := nodePtrType.Elem().FieldByName(fieldName) + if !ok { + panic(fieldName) + } + return fieldInfo{nodePtrType, fieldName, f.Type} +} + +var fieldInfos = [...]fieldInfo{ + Invalid: {}, + ArrayType_Elt: info[*ast.ArrayType]("Elt"), + ArrayType_Len: info[*ast.ArrayType]("Len"), + AssignStmt_Lhs: info[*ast.AssignStmt]("Lhs"), + AssignStmt_Rhs: info[*ast.AssignStmt]("Rhs"), + BinaryExpr_X: info[*ast.BinaryExpr]("X"), + BinaryExpr_Y: info[*ast.BinaryExpr]("Y"), + BlockStmt_List: info[*ast.BlockStmt]("List"), + BranchStmt_Label: info[*ast.BranchStmt]("Label"), + CallExpr_Args: info[*ast.CallExpr]("Args"), + CallExpr_Fun: info[*ast.CallExpr]("Fun"), + CaseClause_Body: info[*ast.CaseClause]("Body"), + CaseClause_List: info[*ast.CaseClause]("List"), + ChanType_Value: info[*ast.ChanType]("Value"), + CommClause_Body: info[*ast.CommClause]("Body"), + CommClause_Comm: info[*ast.CommClause]("Comm"), + CommentGroup_List: info[*ast.CommentGroup]("List"), + CompositeLit_Elts: info[*ast.CompositeLit]("Elts"), + CompositeLit_Type: info[*ast.CompositeLit]("Type"), + DeclStmt_Decl: info[*ast.DeclStmt]("Decl"), + DeferStmt_Call: info[*ast.DeferStmt]("Call"), + Ellipsis_Elt: info[*ast.Ellipsis]("Elt"), + ExprStmt_X: info[*ast.ExprStmt]("X"), + FieldList_List: info[*ast.FieldList]("List"), + Field_Comment: info[*ast.Field]("Comment"), + Field_Doc: info[*ast.Field]("Doc"), + Field_Names: info[*ast.Field]("Names"), + Field_Tag: info[*ast.Field]("Tag"), + Field_Type: info[*ast.Field]("Type"), + File_Decls: info[*ast.File]("Decls"), + File_Doc: info[*ast.File]("Doc"), + File_Name: info[*ast.File]("Name"), + ForStmt_Body: info[*ast.ForStmt]("Body"), + ForStmt_Cond: info[*ast.ForStmt]("Cond"), + ForStmt_Init: info[*ast.ForStmt]("Init"), + ForStmt_Post: info[*ast.ForStmt]("Post"), + FuncDecl_Body: info[*ast.FuncDecl]("Body"), + FuncDecl_Doc: info[*ast.FuncDecl]("Doc"), + FuncDecl_Name: info[*ast.FuncDecl]("Name"), + FuncDecl_Recv: info[*ast.FuncDecl]("Recv"), + FuncDecl_Type: info[*ast.FuncDecl]("Type"), + FuncLit_Body: info[*ast.FuncLit]("Body"), + FuncLit_Type: info[*ast.FuncLit]("Type"), + FuncType_Params: info[*ast.FuncType]("Params"), + FuncType_Results: info[*ast.FuncType]("Results"), + FuncType_TypeParams: info[*ast.FuncType]("TypeParams"), + GenDecl_Doc: info[*ast.GenDecl]("Doc"), + GenDecl_Specs: info[*ast.GenDecl]("Specs"), + GoStmt_Call: info[*ast.GoStmt]("Call"), + IfStmt_Body: info[*ast.IfStmt]("Body"), + IfStmt_Cond: info[*ast.IfStmt]("Cond"), + IfStmt_Else: info[*ast.IfStmt]("Else"), + IfStmt_Init: info[*ast.IfStmt]("Init"), + ImportSpec_Comment: info[*ast.ImportSpec]("Comment"), + ImportSpec_Doc: info[*ast.ImportSpec]("Doc"), + ImportSpec_Name: info[*ast.ImportSpec]("Name"), + ImportSpec_Path: info[*ast.ImportSpec]("Path"), + IncDecStmt_X: info[*ast.IncDecStmt]("X"), + IndexExpr_Index: info[*ast.IndexExpr]("Index"), + IndexExpr_X: info[*ast.IndexExpr]("X"), + IndexListExpr_Indices: info[*ast.IndexListExpr]("Indices"), + IndexListExpr_X: info[*ast.IndexListExpr]("X"), + InterfaceType_Methods: info[*ast.InterfaceType]("Methods"), + KeyValueExpr_Key: info[*ast.KeyValueExpr]("Key"), + KeyValueExpr_Value: info[*ast.KeyValueExpr]("Value"), + LabeledStmt_Label: info[*ast.LabeledStmt]("Label"), + LabeledStmt_Stmt: info[*ast.LabeledStmt]("Stmt"), + MapType_Key: info[*ast.MapType]("Key"), + MapType_Value: info[*ast.MapType]("Value"), + ParenExpr_X: info[*ast.ParenExpr]("X"), + RangeStmt_Body: info[*ast.RangeStmt]("Body"), + RangeStmt_Key: info[*ast.RangeStmt]("Key"), + RangeStmt_Value: info[*ast.RangeStmt]("Value"), + RangeStmt_X: info[*ast.RangeStmt]("X"), + ReturnStmt_Results: info[*ast.ReturnStmt]("Results"), + SelectStmt_Body: info[*ast.SelectStmt]("Body"), + SelectorExpr_Sel: info[*ast.SelectorExpr]("Sel"), + SelectorExpr_X: info[*ast.SelectorExpr]("X"), + SendStmt_Chan: info[*ast.SendStmt]("Chan"), + SendStmt_Value: info[*ast.SendStmt]("Value"), + SliceExpr_High: info[*ast.SliceExpr]("High"), + SliceExpr_Low: info[*ast.SliceExpr]("Low"), + SliceExpr_Max: info[*ast.SliceExpr]("Max"), + SliceExpr_X: info[*ast.SliceExpr]("X"), + StarExpr_X: info[*ast.StarExpr]("X"), + StructType_Fields: info[*ast.StructType]("Fields"), + SwitchStmt_Body: info[*ast.SwitchStmt]("Body"), + SwitchStmt_Init: info[*ast.SwitchStmt]("Init"), + SwitchStmt_Tag: info[*ast.SwitchStmt]("Tag"), + TypeAssertExpr_Type: info[*ast.TypeAssertExpr]("Type"), + TypeAssertExpr_X: info[*ast.TypeAssertExpr]("X"), + TypeSpec_Comment: info[*ast.TypeSpec]("Comment"), + TypeSpec_Doc: info[*ast.TypeSpec]("Doc"), + TypeSpec_Name: info[*ast.TypeSpec]("Name"), + TypeSpec_Type: info[*ast.TypeSpec]("Type"), + TypeSpec_TypeParams: info[*ast.TypeSpec]("TypeParams"), + TypeSwitchStmt_Assign: info[*ast.TypeSwitchStmt]("Assign"), + TypeSwitchStmt_Body: info[*ast.TypeSwitchStmt]("Body"), + TypeSwitchStmt_Init: info[*ast.TypeSwitchStmt]("Init"), + UnaryExpr_X: info[*ast.UnaryExpr]("X"), + ValueSpec_Comment: info[*ast.ValueSpec]("Comment"), + ValueSpec_Doc: info[*ast.ValueSpec]("Doc"), + ValueSpec_Names: info[*ast.ValueSpec]("Names"), + ValueSpec_Type: info[*ast.ValueSpec]("Type"), + ValueSpec_Values: info[*ast.ValueSpec]("Values"), +} From f0ddc4b70a6cf6537a632be80b159e5db12ec32e Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 14 Jan 2025 10:30:20 -0500 Subject: [PATCH 080/309] gopls/internal/analysis: use Cursor.Edge in two analyzers Change-Id: I2003182cd097e174836c4bf8cf92312ab6d42b8c Reviewed-on: https://go-review.googlesource.com/c/tools/+/642615 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/bloop.go | 2 +- .../analysis/modernize/testingcontext.go | 20 +++++-------- .../analysis/unusedparams/unusedparams.go | 29 +++++++++---------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 2f004c7ffb2..f851a6688e1 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -164,7 +164,7 @@ func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { // enclosingFunc returns the cursor for the innermost Func{Decl,Lit} // that encloses c, if any. func enclosingFunc(c cursor.Cursor) (cursor.Cursor, bool) { - for curAncestor := range c.Ancestors((*ast.FuncLit)(nil), (*ast.FuncDecl)(nil)) { + for curAncestor := range c.Ancestors((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { return curAncestor, true } return cursor.Cursor{}, false diff --git a/gopls/internal/analysis/modernize/testingcontext.go b/gopls/internal/analysis/modernize/testingcontext.go index daedb2e8f85..9bdc11ccfca 100644 --- a/gopls/internal/analysis/modernize/testingcontext.go +++ b/gopls/internal/analysis/modernize/testingcontext.go @@ -9,7 +9,6 @@ import ( "go/ast" "go/token" "go/types" - "slices" "strings" "unicode" "unicode/utf8" @@ -20,6 +19,7 @@ import ( "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" ) // The testingContext pass replaces calls to context.WithCancel from within @@ -107,27 +107,23 @@ func testingContext(pass *analysis.Pass) { // Check that we are in a test func. var testObj types.Object // relevant testing.{T,B,F}, or nil - - // TODO(rfindley): use cur.Ancestors when it is available. - stack := cur.Stack(nil) - slices.Reverse(stack) - findTestObj: - for _, ancestor := range stack { - switch n := ancestor.Node().(type) { + if curFunc, ok := enclosingFunc(cur); ok { + switch n := curFunc.Node().(type) { case *ast.FuncLit: - if call, ok := ancestor.Parent().Node().(*ast.CallExpr); ok && len(call.Args) == 2 && call.Args[1] == n { - obj := typeutil.Callee(info, call) + if e, idx := curFunc.Edge(); e == edge.CallExpr_Args && idx == 1 { + // Have: call(..., func(...) { ...context.WithCancel(...)... }) + obj := typeutil.Callee(info, curFunc.Parent().Node().(*ast.CallExpr)) if (analysisinternal.IsMethodNamed(obj, "testing", "T", "Run") || analysisinternal.IsMethodNamed(obj, "testing", "B", "Run")) && len(n.Type.Params.List[0].Names) == 1 { + // Have tb.Run(..., func(..., tb *testing.[TB]) { ...context.WithCancel(...)... } testObj = info.Defs[n.Type.Params.List[0].Names[0]] } } - break findTestObj + case *ast.FuncDecl: testObj = isTestFn(info, n) - break findTestObj } } diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index 2b74328021d..61bdf51834e 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/gopls/internal/util/moreslices" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" ) //go:embed doc.go @@ -187,24 +188,22 @@ func run(pass *analysis.Pass) (any, error) { case *ast.AssignStmt: // f = func() {...} // f := func() {...} - for i, rhs := range parent.Rhs { - if rhs == n { - if id, ok := parent.Lhs[i].(*ast.Ident); ok { - fn = pass.TypesInfo.ObjectOf(id) + if e, idx := c.Edge(); e == edge.AssignStmt_Rhs { + // Inv: n == AssignStmt.Rhs[idx] + if id, ok := parent.Lhs[idx].(*ast.Ident); ok { + fn = pass.TypesInfo.ObjectOf(id) - // Edge case: f = func() {...} - // should not count as a use. - if pass.TypesInfo.Uses[id] != nil { - usesOutsideCall[fn] = moreslices.Remove(usesOutsideCall[fn], id) - } + // Edge case: f = func() {...} + // should not count as a use. + if pass.TypesInfo.Uses[id] != nil { + usesOutsideCall[fn] = moreslices.Remove(usesOutsideCall[fn], id) + } - if fn == nil && id.Name == "_" { - // Edge case: _ = func() {...} - // has no var. Fake one. - fn = types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) - } + if fn == nil && id.Name == "_" { + // Edge case: _ = func() {...} + // has no var. Fake one. + fn = types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) } - break } } From a721d4cae8b994c3d81bd050c493b40472ae8137 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 30 Jan 2025 16:34:09 -0500 Subject: [PATCH 081/309] internal/typesinternal: factor out IsPackageLevel Define IsPackageLevel in one place instead of three. Replace all calls to the new function. Delete the old ones. Change-Id: I30e0a41908e72ef4a5c0715489672a32209c55b1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645696 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/freesymbols.go | 2 +- gopls/internal/golang/hover.go | 8 ++++---- gopls/internal/golang/pkgdoc.go | 2 +- gopls/internal/golang/rename.go | 6 +++--- gopls/internal/golang/rename_check.go | 9 +-------- internal/analysisinternal/analysis.go | 12 ++---------- internal/typesinternal/types.go | 5 +++++ refactor/rename/check.go | 2 +- refactor/rename/util.go | 4 ---- 9 files changed, 18 insertions(+), 32 deletions(-) diff --git a/gopls/internal/golang/freesymbols.go b/gopls/internal/golang/freesymbols.go index bbda8f7d948..2c9e25165f6 100644 --- a/gopls/internal/golang/freesymbols.go +++ b/gopls/internal/golang/freesymbols.go @@ -297,7 +297,7 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t // Compute dotted path. objects := append(suffix, obj) - if obj.Pkg() != nil && obj.Pkg() != pkg && isPackageLevel(obj) { // dot import + if obj.Pkg() != nil && obj.Pkg() != pkg && typesinternal.IsPackageLevel(obj) { // dot import // Synthesize the implicit PkgName. pkgName := types.NewPkgName(token.NoPos, pkg, obj.Pkg().Name(), obj.Pkg()) parent = fileScope diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index 80c47470215..ead5ec4a5db 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -587,13 +587,13 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro pkg := obj.Pkg() if recv != nil { linkName = fmt.Sprintf("(%s.%s).%s", pkg.Name(), recv.Name(), obj.Name()) - if obj.Exported() && recv.Exported() && isPackageLevel(recv) { + if obj.Exported() && recv.Exported() && typesinternal.IsPackageLevel(recv) { linkPath = pkg.Path() anchor = fmt.Sprintf("%s.%s", recv.Name(), obj.Name()) } } else { linkName = fmt.Sprintf("%s.%s", pkg.Name(), obj.Name()) - if obj.Exported() && isPackageLevel(obj) { + if obj.Exported() && typesinternal.IsPackageLevel(obj) { linkPath = pkg.Path() anchor = obj.Name() } @@ -1333,7 +1333,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol { } // Handle Function, Type, Const & Var. - if isPackageLevel(obj) { + if obj != nil && typesinternal.IsPackageLevel(obj) { for _, s := range symbols { if s.Kind == stdlib.Method || s.Kind == stdlib.Field { continue @@ -1348,7 +1348,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol { // Handle Method. if fn, _ := obj.(*types.Func); fn != nil { isPtr, named := typesinternal.ReceiverNamed(fn.Signature().Recv()) - if named != nil && isPackageLevel(named.Obj()) { + if named != nil && typesinternal.IsPackageLevel(named.Obj()) { for _, s := range symbols { if s.Kind != stdlib.Method { continue diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 8050937a88b..115fbf979f8 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -140,7 +140,7 @@ func DocFragment(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (p } // package-level symbol? - if isPackageLevel(sym) { + if typesinternal.IsPackageLevel(sym) { return pkgpath, sym.Name(), makeTitle(objectKind(sym), sym.Pkg(), sym.Name()) } diff --git a/gopls/internal/golang/rename.go b/gopls/internal/golang/rename.go index 914cd2b66ed..26e9d0a5a52 100644 --- a/gopls/internal/golang/rename.go +++ b/gopls/internal/golang/rename.go @@ -555,7 +555,7 @@ func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, f file.Handle // objectpath, the classifies them as local vars, but as // they came from export data they lack syntax and the // correct scope tree (issue #61294). - if !obj.(*types.Var).IsField() && !isPackageLevel(obj) { + if !obj.(*types.Var).IsField() && !typesinternal.IsPackageLevel(obj) { goto skipObjectPath } } @@ -1345,7 +1345,7 @@ func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit, recvName := "" // Doc links can reference only exported package-level objects // and methods of exported package-level named types. - if !isPackageLevel(obj) { + if !typesinternal.IsPackageLevel(obj) { obj, isFunc := obj.(*types.Func) if !isFunc { continue @@ -1363,7 +1363,7 @@ func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit, continue } name := named.Origin().Obj() - if !name.Exported() || !isPackageLevel(name) { + if !name.Exported() || !typesinternal.IsPackageLevel(name) { continue } recvName = name.Name() diff --git a/gopls/internal/golang/rename_check.go b/gopls/internal/golang/rename_check.go index ed6424c918f..280795abe5e 100644 --- a/gopls/internal/golang/rename_check.go +++ b/gopls/internal/golang/rename_check.go @@ -100,7 +100,7 @@ func (r *renamer) check(from types.Object) { r.checkInFileBlock(from_) } else if from_, ok := from.(*types.Label); ok { r.checkLabel(from_) - } else if isPackageLevel(from) { + } else if typesinternal.IsPackageLevel(from) { r.checkInPackageBlock(from) } else if v, ok := from.(*types.Var); ok && v.IsField() { r.checkStructField(v) @@ -949,13 +949,6 @@ func isLocal(obj types.Object) bool { return depth >= 4 } -func isPackageLevel(obj types.Object) bool { - if obj == nil { - return false - } - return obj.Pkg().Scope().Lookup(obj.Name()) == obj -} - // -- Plundered from go/scanner: --------------------------------------- func isLetter(ch rune) bool { diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 8f38fa604d8..7608692b750 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -299,7 +299,7 @@ func IsTypeNamed(t types.Type, pkgPath string, names ...string) bool { if named, ok := types.Unalias(t).(*types.Named); ok { tname := named.Obj() return tname != nil && - isPackageLevel(tname) && + typesinternal.IsPackageLevel(tname) && tname.Pkg().Path() == pkgPath && slices.Contains(names, tname.Name()) } @@ -326,7 +326,7 @@ func IsPointerToNamed(t types.Type, pkgPath string, names ...string) bool { func IsFunctionNamed(obj types.Object, pkgPath string, names ...string) bool { f, ok := obj.(*types.Func) return ok && - isPackageLevel(obj) && + typesinternal.IsPackageLevel(obj) && f.Pkg().Path() == pkgPath && f.Type().(*types.Signature).Recv() == nil && slices.Contains(names, f.Name()) @@ -350,14 +350,6 @@ func IsMethodNamed(obj types.Object, pkgPath string, typeName string, names ...s return false } -// isPackageLevel reports whether obj is a package-level symbol. -// -// TODO(adonovan): publish in typesinternal and factor with -// gopls/internal/golang/rename_check.go, refactor/rename/util.go. -func isPackageLevel(obj types.Object) bool { - return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope() -} - // ValidateFixes validates the set of fixes for a single diagnostic. // Any error indicates a bug in the originating analyzer. // diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index a93d51f9882..34534879630 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -120,3 +120,8 @@ func Origin(t NamedOrAlias) NamedOrAlias { } return t } + +// IsPackageLevel reports whether obj is a package-level symbol. +func IsPackageLevel(obj types.Object) bool { + return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope() +} diff --git a/refactor/rename/check.go b/refactor/rename/check.go index 4a058321ca4..7b29dbf6a72 100644 --- a/refactor/rename/check.go +++ b/refactor/rename/check.go @@ -36,7 +36,7 @@ func (r *renamer) check(from types.Object) { r.checkInFileBlock(from_) } else if from_, ok := from.(*types.Label); ok { r.checkLabel(from_) - } else if isPackageLevel(from) { + } else if typesinternal.IsPackageLevel(from) { r.checkInPackageBlock(from) } else if v, ok := from.(*types.Var); ok && v.IsField() { r.checkStructField(v) diff --git a/refactor/rename/util.go b/refactor/rename/util.go index bc6dc10cac9..7c1a634e4ed 100644 --- a/refactor/rename/util.go +++ b/refactor/rename/util.go @@ -61,10 +61,6 @@ func isLocal(obj types.Object) bool { return depth >= 4 } -func isPackageLevel(obj types.Object) bool { - return obj.Pkg().Scope().Lookup(obj.Name()) == obj -} - // -- Plundered from go/scanner: --------------------------------------- func isLetter(ch rune) bool { From 058d583f496657e62984548c4f609f5446df41c4 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 28 Jan 2025 18:43:22 +0000 Subject: [PATCH 082/309] gopls/internal/golang: don't apply edits in codeAction/resolve Two recently added code actions were registered with allowResolveEdits set to true, when in fact the corresponding commands were not implemented with resolve edit support. As a result, calls to codeAction/resolve actually had the side effects of immediately applying the edits. Fix this by amending the relevant allowResolveEdits values, and update tests to catch these types of bugs. The fake editor is updated to always advertise resolve support. In my opinion, these bugs are really a result of the awkward command API, which itself is a historical artifact from before codeAction/resolve existed. A TODO is left to address this larger problem. Fixes golang/go#71405 Change-Id: Id1b33feac16897338715547efa38195886bd91c9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645018 LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley Reviewed-by: Alan Donovan --- gopls/internal/golang/codeaction.go | 13 +++++++++--- .../internal/test/integration/fake/editor.go | 4 ++++ .../test/integration/misc/fix_test.go | 7 ++++--- gopls/internal/test/marker/marker_test.go | 20 +++++++++++-------- .../testdata/codeaction/extract_variable.txt | 10 ++++++++++ .../codeaction/extract_variable_all.txt | 12 ++++++++++- .../extract_variable_all_resolve.txt | 2 +- .../codeaction/extract_variable_resolve.txt | 11 ---------- .../testdata/codeaction/fill_struct.txt | 10 ++++++++++ .../codeaction/fill_struct_resolve.txt | 11 ---------- .../testdata/codeaction/fill_switch.txt | 9 +++++++++ .../codeaction/fill_switch_resolve.txt | 11 ---------- .../marker/testdata/codeaction/inline.txt | 10 ++++++++++ .../testdata/codeaction/inline_resolve.txt | 11 ---------- .../testdata/codeaction/removeparam.txt | 10 ++++++++++ .../codeaction/removeparam_resolve.txt | 11 ---------- .../testdata/quickfix/stubmethods/basic.txt | 10 ++++++++++ .../quickfix/stubmethods/basic_resolve.txt | 11 ---------- 18 files changed, 101 insertions(+), 82 deletions(-) diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 17f7236f815..34ac7426019 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -154,7 +154,14 @@ func (req *codeActionsRequest) addApplyFixAction(title, fix string, loc protocol // then the command is embedded into the code action data field so // that the client can later ask the server to "resolve" a command // into an edit that they can preview and apply selectively. -// Set allowResolveEdits only for actions that generate edits. +// IMPORTANT: set allowResolveEdits only for actions that are 'edit aware', +// meaning they can detect when they are being executed in the context of a +// codeAction/resolve request, and return edits rather than applying them using +// workspace/applyEdit. In golang/go#71405, edits were being apply during the +// codeAction/resolve request handler. +// TODO(rfindley): refactor the command and code lens registration APIs so that +// resolve edit support is inferred from the command signature, not dependent +// on coordination between codeAction and command logic. // // Otherwise, the command is set as the code action operation. func (req *codeActionsRequest) addCommandAction(cmd *protocol.Command, allowResolveEdits bool) { @@ -528,7 +535,7 @@ func refactorExtractVariableAll(ctx context.Context, req *codeActionsRequest) er func refactorExtractToNewFile(ctx context.Context, req *codeActionsRequest) error { if canExtractToNewFile(req.pgf, req.start, req.end) { cmd := command.NewExtractToNewFileCommand("Extract declarations to new file", req.loc) - req.addCommandAction(cmd, true) + req.addCommandAction(cmd, false) } return nil } @@ -562,7 +569,7 @@ func addTest(ctx context.Context, req *codeActionsRequest) error { } cmd := command.NewAddTestCommand("Add test for "+decl.Name.String(), req.loc) - req.addCommandAction(cmd, true) + req.addCommandAction(cmd, false) // TODO(hxjiang): add code action for generate test for package/file. return nil diff --git a/gopls/internal/test/integration/fake/editor.go b/gopls/internal/test/integration/fake/editor.go index 1b1e0f170a2..adc9df6c17d 100644 --- a/gopls/internal/test/integration/fake/editor.go +++ b/gopls/internal/test/integration/fake/editor.go @@ -379,6 +379,10 @@ func clientCapabilities(cfg EditorConfig) (protocol.ClientCapabilities, error) { } // Request that the server provide its complete list of code action kinds. capabilities.TextDocument.CodeAction = protocol.CodeActionClientCapabilities{ + DataSupport: true, + ResolveSupport: &protocol.ClientCodeActionResolveOptions{ + Properties: []string{"edit"}, + }, CodeActionLiteralSupport: protocol.ClientCodeActionLiteralOptions{ CodeActionKind: protocol.ClientCodeActionKindOptions{ ValueSet: []protocol.CodeActionKind{protocol.Empty}, // => all diff --git a/gopls/internal/test/integration/misc/fix_test.go b/gopls/internal/test/integration/misc/fix_test.go index 5a01afe2400..261b5841109 100644 --- a/gopls/internal/test/integration/misc/fix_test.go +++ b/gopls/internal/test/integration/misc/fix_test.go @@ -21,9 +21,10 @@ func TestFillStruct(t *testing.T) { capabilities string wantCommand bool }{ - {"default", "{}", true}, - {"no data", `{ "textDocument": {"codeAction": { "resolveSupport": { "properties": ["edit"] } } } }`, true}, - {"resolve support", `{ "textDocument": {"codeAction": { "dataSupport": true, "resolveSupport": { "properties": ["edit"] } } } }`, false}, + {"default", "{}", false}, + {"no data support", `{"textDocument": {"codeAction": {"dataSupport": false, "resolveSupport": {"properties": ["edit"]}}}}`, true}, + {"no resolve support", `{"textDocument": {"codeAction": {"dataSupport": true, "resolveSupport": {"properties": []}}}}`, true}, + {"data and resolve support", `{"textDocument": {"codeAction": {"dataSupport": true, "resolveSupport": {"properties": ["edit"]}}}}`, false}, } const basic = ` diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index 654bca4ae5b..516dfeb3881 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -2223,6 +2223,18 @@ func codeAction(env *integration.Env, uri protocol.DocumentURI, rng protocol.Ran // specified location and kind, and captures the resulting document changes. // If diag is non-nil, it is used as the code action context. func codeActionChanges(env *integration.Env, uri protocol.DocumentURI, rng protocol.Range, kind protocol.CodeActionKind, diag *protocol.Diagnostic) ([]protocol.DocumentChange, error) { + // Collect any server-initiated changes created by workspace/applyEdit. + // + // We set up this handler immediately, not right before executing the code + // action command, so we can assert that neither the codeAction request nor + // codeAction resolve request cause edits as a side effect (golang/go#71405). + var changes []protocol.DocumentChange + restore := env.Editor.Client().SetApplyEditHandler(func(ctx context.Context, wsedit *protocol.WorkspaceEdit) error { + changes = append(changes, wsedit.DocumentChanges...) + return nil + }) + defer restore() + // Request all code actions that apply to the diagnostic. // A production client would set Only=[kind], // but we can give a better error if we don't filter. @@ -2312,14 +2324,6 @@ func codeActionChanges(env *integration.Env, uri protocol.DocumentURI, rng proto // whose WorkspaceEditFunc hook temporarily gathers the edits // instead of applying them. - var changes []protocol.DocumentChange - cli := env.Editor.Client() - restore := cli.SetApplyEditHandler(func(ctx context.Context, wsedit *protocol.WorkspaceEdit) error { - changes = append(changes, wsedit.DocumentChanges...) - return nil - }) - defer restore() - if _, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{ Command: action.Command.Command, Arguments: action.Command.Arguments, diff --git a/gopls/internal/test/marker/testdata/codeaction/extract_variable.txt b/gopls/internal/test/marker/testdata/codeaction/extract_variable.txt index 9dd0f766e05..0fba1afe003 100644 --- a/gopls/internal/test/marker/testdata/codeaction/extract_variable.txt +++ b/gopls/internal/test/marker/testdata/codeaction/extract_variable.txt @@ -1,6 +1,16 @@ This test checks the behavior of the 'extract variable/constant' code action. See extract_variable_resolve.txt for the same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} + -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/extract_variable_all.txt b/gopls/internal/test/marker/testdata/codeaction/extract_variable_all.txt index 050f29bfec7..5916c0696cc 100644 --- a/gopls/internal/test/marker/testdata/codeaction/extract_variable_all.txt +++ b/gopls/internal/test/marker/testdata/codeaction/extract_variable_all.txt @@ -1,5 +1,15 @@ This test checks the behavior of the 'replace all occurrences of expression' code action, with resolve support. -See extract_expressions.txt for the same test without resolve support. +See extract_variable_all_resolve.txt for the same test with resolve support. + +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/extract_variable_all_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/extract_variable_all_resolve.txt index 02c03929567..8f6544f19df 100644 --- a/gopls/internal/test/marker/testdata/codeaction/extract_variable_all_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/extract_variable_all_resolve.txt @@ -1,5 +1,5 @@ This test checks the behavior of the 'replace all occurrences of expression' code action, with resolve support. -See extract_expressions.txt for the same test without resolve support. +See extract_variable_all.txt for the same test without resolve support. -- capabilities.json -- { diff --git a/gopls/internal/test/marker/testdata/codeaction/extract_variable_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/extract_variable_resolve.txt index 203b6d1eadc..819717897ab 100644 --- a/gopls/internal/test/marker/testdata/codeaction/extract_variable_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/extract_variable_resolve.txt @@ -1,17 +1,6 @@ This test checks the behavior of the 'extract variable/constant' code action, with resolve support. See extract_variable.txt for the same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt b/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt index 6c71175eb04..5a50978ad5e 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_struct.txt @@ -1,6 +1,16 @@ This test checks the behavior of the 'fill struct' code action. See fill_struct_resolve.txt for same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} + -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt index d7746eef28e..9c1f8f728ca 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_struct_resolve.txt @@ -1,17 +1,6 @@ This test checks the behavior of the 'fill struct' code action, with resolve support. See fill_struct.txt for same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_switch.txt b/gopls/internal/test/marker/testdata/codeaction/fill_switch.txt index 1912c92c19a..a92a895287f 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_switch.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_switch.txt @@ -1,6 +1,15 @@ This test checks the behavior of the 'fill switch' code action. See fill_switch_resolve.txt for same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/fill_switch_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/fill_switch_resolve.txt index c8380a7d6d6..39a7eae7779 100644 --- a/gopls/internal/test/marker/testdata/codeaction/fill_switch_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/fill_switch_resolve.txt @@ -1,17 +1,6 @@ This test checks the behavior of the 'fill switch' code action, with resolve support. See fill_switch.txt for same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- flags -- -ignore_extra_diags diff --git a/gopls/internal/test/marker/testdata/codeaction/inline.txt b/gopls/internal/test/marker/testdata/codeaction/inline.txt index 4c2bf15c207..1871a303d2b 100644 --- a/gopls/internal/test/marker/testdata/codeaction/inline.txt +++ b/gopls/internal/test/marker/testdata/codeaction/inline.txt @@ -1,6 +1,16 @@ This is a minimal test of the refactor.inline.call code action, without resolve support. See inline_resolve.txt for same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} + -- go.mod -- module example.com/codeaction go 1.18 diff --git a/gopls/internal/test/marker/testdata/codeaction/inline_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/inline_resolve.txt index c889ed8bba3..cf311838706 100644 --- a/gopls/internal/test/marker/testdata/codeaction/inline_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/inline_resolve.txt @@ -1,17 +1,6 @@ This is a minimal test of the refactor.inline.call code actions, with resolve support. See inline.txt for same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- go.mod -- module example.com/codeaction go 1.18 diff --git a/gopls/internal/test/marker/testdata/codeaction/removeparam.txt b/gopls/internal/test/marker/testdata/codeaction/removeparam.txt index 8bebfc29c40..7ba21a6a876 100644 --- a/gopls/internal/test/marker/testdata/codeaction/removeparam.txt +++ b/gopls/internal/test/marker/testdata/codeaction/removeparam.txt @@ -1,6 +1,16 @@ This test exercises the refactoring to remove unused parameters. See removeparam_resolve.txt for same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} + -- go.mod -- module unused.mod diff --git a/gopls/internal/test/marker/testdata/codeaction/removeparam_resolve.txt b/gopls/internal/test/marker/testdata/codeaction/removeparam_resolve.txt index 3d92d758b13..a10251a87ee 100644 --- a/gopls/internal/test/marker/testdata/codeaction/removeparam_resolve.txt +++ b/gopls/internal/test/marker/testdata/codeaction/removeparam_resolve.txt @@ -1,17 +1,6 @@ This test exercises the refactoring to remove unused parameters, with resolve support. See removeparam.txt for same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- go.mod -- module unused.mod diff --git a/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic.txt b/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic.txt index 96f992f8aaa..1ddee2cfe98 100644 --- a/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic.txt +++ b/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic.txt @@ -1,6 +1,16 @@ This test exercises basic 'stub methods' functionality. See basic_resolve.txt for the same test with resolve support. +-- capabilities.json -- +{ + "textDocument": { + "codeAction": { + "dataSupport": false, + "resolveSupport": {} + } + } +} + -- go.mod -- module example.com go 1.12 diff --git a/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic_resolve.txt b/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic_resolve.txt index 502cc40bb74..f3e3dfefb71 100644 --- a/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic_resolve.txt +++ b/gopls/internal/test/marker/testdata/quickfix/stubmethods/basic_resolve.txt @@ -1,17 +1,6 @@ This test exercises basic 'stub methods' functionality, with resolve support. See basic.txt for the same test without resolve support. --- capabilities.json -- -{ - "textDocument": { - "codeAction": { - "dataSupport": true, - "resolveSupport": { - "properties": ["edit"] - } - } - } -} -- go.mod -- module example.com go 1.12 From bbe00fb5654ddf7090177afd3f85c492525dbe9b Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Mon, 27 Jan 2025 21:32:00 -0500 Subject: [PATCH 083/309] gopls/internal/server: gopls.vulncheck return both vuln report and token The client (vscode-go) need token to correlate the executeCommand response with the workDoneProgress. vscode-go will surface both the $/progress.Message and executeCommand.Res in the same terminal. For golang/vscode-go#3572 Change-Id: Idb9b0c36783817d6c9f6d672e82b39ae9c501a21 Reviewed-on: https://go-review.googlesource.com/c/tools/+/644896 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Hongxiang Jiang --- gopls/internal/protocol/command/interface.go | 3 +++ gopls/internal/server/command.go | 1 + 2 files changed, 4 insertions(+) diff --git a/gopls/internal/protocol/command/interface.go b/gopls/internal/protocol/command/interface.go index e7386f7fd8f..060f72ce548 100644 --- a/gopls/internal/protocol/command/interface.go +++ b/gopls/internal/protocol/command/interface.go @@ -516,6 +516,9 @@ type RunVulncheckResult struct { type VulncheckResult struct { // Result holds the result of running vulncheck. Result *vulncheck.Result + // Token holds the progress token used to report progress during back to the + // LSP client during vulncheck execution. + Token protocol.ProgressToken } // MemStatsResult holds selected fields from runtime.MemStats. diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 2d936f2bc41..7bba71100ca 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -1229,6 +1229,7 @@ func (c *commandHandler) Vulncheck(ctx context.Context, args command.VulncheckAr return err } commandResult.Result = result + commandResult.Token = deps.work.Token() snapshot, release, err := c.s.session.InvalidateView(ctx, deps.snapshot.View(), cache.StateChange{ Vulns: map[protocol.DocumentURI]*vulncheck.Result{args.URI: result}, From b7813756c2adaa3e2a2d099e1c576aeb2a552058 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 31 Jan 2025 10:51:15 -0500 Subject: [PATCH 084/309] gopls/internal/protocol: delete MappedRange It has slowly made itself entirely redundant. Updates golang/go#71489 Change-Id: Ib15128b9dc9652c1138a1372ba89c507667e7fcd Reviewed-on: https://go-review.googlesource.com/c/tools/+/645815 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- gopls/internal/cache/errors.go | 4 +- gopls/internal/cache/parsego/file.go | 12 ---- gopls/internal/cmd/cmd.go | 3 - gopls/internal/cmd/span.go | 3 +- gopls/internal/golang/folding_range.go | 26 ++++--- gopls/internal/protocol/mapper.go | 94 +------------------------- gopls/internal/server/folding_range.go | 2 +- 7 files changed, 19 insertions(+), 125 deletions(-) diff --git a/gopls/internal/cache/errors.go b/gopls/internal/cache/errors.go index 816d6c6b0f8..39eb8387702 100644 --- a/gopls/internal/cache/errors.go +++ b/gopls/internal/cache/errors.go @@ -453,14 +453,14 @@ func parseGoListImportCycleError(ctx context.Context, e packages.Error, mp *meta // Search file imports for the import that is causing the import cycle. for _, imp := range pgf.File.Imports { if imp.Path.Value == circImp { - rng, err := pgf.NodeMappedRange(imp) + rng, err := pgf.NodeRange(imp) if err != nil { return nil, nil } return &Diagnostic{ URI: pgf.URI, - Range: rng.Range(), + Range: rng, Severity: protocol.SeverityError, Source: ListError, Message: msg, diff --git a/gopls/internal/cache/parsego/file.go b/gopls/internal/cache/parsego/file.go index ea8db19b4ff..41fd1937ec1 100644 --- a/gopls/internal/cache/parsego/file.go +++ b/gopls/internal/cache/parsego/file.go @@ -76,12 +76,6 @@ func (pgf *File) PosRange(start, end token.Pos) (protocol.Range, error) { return pgf.Mapper.PosRange(pgf.Tok, start, end) } -// PosMappedRange returns a MappedRange for the token.Pos interval in this file. -// A MappedRange can be converted to any other form. -func (pgf *File) PosMappedRange(start, end token.Pos) (protocol.MappedRange, error) { - return pgf.Mapper.PosMappedRange(pgf.Tok, start, end) -} - // PosLocation returns a protocol Location for the token.Pos interval in this file. func (pgf *File) PosLocation(start, end token.Pos) (protocol.Location, error) { return pgf.Mapper.PosLocation(pgf.Tok, start, end) @@ -97,12 +91,6 @@ func (pgf *File) NodeOffsets(node ast.Node) (start int, end int, _ error) { return safetoken.Offsets(pgf.Tok, node.Pos(), node.End()) } -// NodeMappedRange returns a MappedRange for the ast.Node interval in this file. -// A MappedRange can be converted to any other form. -func (pgf *File) NodeMappedRange(node ast.Node) (protocol.MappedRange, error) { - return pgf.Mapper.NodeMappedRange(pgf.Tok, node) -} - // NodeLocation returns a protocol Location for the ast.Node interval in this file. func (pgf *File) NodeLocation(node ast.Node) (protocol.Location, error) { return pgf.Mapper.PosLocation(pgf.Tok, node.Pos(), node.End()) diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index 1338773016b..a647b3198df 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -772,9 +772,6 @@ func (c *cmdClient) openFile(uri protocol.DocumentURI) *cmdFile { return c.getFile(uri) } -// TODO(adonovan): provide convenience helpers to: -// - map a (URI, protocol.Range) to a MappedRange; -// - parse a command-line argument to a MappedRange. func (c *connection) openFile(ctx context.Context, uri protocol.DocumentURI) (*cmdFile, error) { file := c.client.openFile(uri) if file.err != nil { diff --git a/gopls/internal/cmd/span.go b/gopls/internal/cmd/span.go index 4753d534350..44a3223c235 100644 --- a/gopls/internal/cmd/span.go +++ b/gopls/internal/cmd/span.go @@ -185,8 +185,7 @@ func (p *_point) clean() { // The format produced is one that can be read back in using parseSpan. // // TODO(adonovan): this is esoteric, and the formatting options are -// never used outside of TestFormat. Replace with something simpler -// along the lines of MappedRange.String. +// never used outside of TestFormat. func (s span) Format(f fmt.State, c rune) { fullForm := f.Flag('+') preferOffset := f.Flag('#') diff --git a/gopls/internal/golang/folding_range.go b/gopls/internal/golang/folding_range.go index c61802d1b58..9d80cc8de29 100644 --- a/gopls/internal/golang/folding_range.go +++ b/gopls/internal/golang/folding_range.go @@ -9,7 +9,7 @@ import ( "context" "go/ast" "go/token" - "sort" + "slices" "strings" "golang.org/x/tools/gopls/internal/cache" @@ -22,8 +22,8 @@ import ( // FoldingRangeInfo holds range and kind info of folding for an ast.Node type FoldingRangeInfo struct { - MappedRange protocol.MappedRange - Kind protocol.FoldingRangeKind + Range protocol.Range + Kind protocol.FoldingRangeKind } // FoldingRange gets all of the folding range for f. @@ -60,10 +60,8 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, // Walk the ast and collect folding ranges. ast.Inspect(pgf.File, visit) - sort.Slice(ranges, func(i, j int) bool { - irng := ranges[i].MappedRange.Range() - jrng := ranges[j].MappedRange.Range() - return protocol.CompareRange(irng, jrng) < 0 + slices.SortFunc(ranges, func(x, y *FoldingRangeInfo) int { + return protocol.CompareRange(x.Range, y.Range) }) return ranges, nil @@ -121,14 +119,14 @@ func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *Fold if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) { return nil } - mrng, err := pgf.PosMappedRange(start, end) + rng, err := pgf.PosRange(start, end) if err != nil { - bug.Reportf("failed to create mapped range: %s", err) // can't happen + bug.Reportf("failed to create range: %s", err) // can't happen return nil } return &FoldingRangeInfo{ - MappedRange: mrng, - Kind: kind, + Range: rng, + Kind: kind, } } @@ -215,15 +213,15 @@ func commentsFoldingRange(pgf *parsego.File) (comments []*FoldingRangeInfo) { // folding range start at the end of the first line. endLinePos = token.Pos(int(startPos) + len(strings.Split(firstComment.Text, "\n")[0])) } - mrng, err := pgf.PosMappedRange(endLinePos, commentGrp.End()) + rng, err := pgf.PosRange(endLinePos, commentGrp.End()) if err != nil { bug.Reportf("failed to create mapped range: %s", err) // can't happen continue } comments = append(comments, &FoldingRangeInfo{ // Fold from the end of the first line comment to the end of the comment block. - MappedRange: mrng, - Kind: protocol.Comment, + Range: rng, + Kind: protocol.Comment, }) } return comments diff --git a/gopls/internal/protocol/mapper.go b/gopls/internal/protocol/mapper.go index 85997c24dc4..a4aa2e2efe8 100644 --- a/gopls/internal/protocol/mapper.go +++ b/gopls/internal/protocol/mapper.go @@ -39,10 +39,9 @@ package protocol // All fields are optional. // // These types are useful as intermediate conversions of validated -// ranges (though MappedRange is superior as it is self contained -// and universally convertible). Since their fields are optional -// they are also useful for parsing user-provided positions (e.g. in -// the CLI) before we have access to file contents. +// ranges. Since their fields are optional they are also useful for +// parsing user-provided positions (e.g. in the CLI) before we have +// access to file contents. // // 4. protocol, the LSP RPC message format. // @@ -56,10 +55,6 @@ package protocol // protocol.Mapper holds the (URI, Content) of a file, enabling // efficient mapping between byte offsets, cmd ranges, and // protocol ranges. -// -// protocol.MappedRange holds a protocol.Mapper and valid (start, -// end int) byte offsets, enabling infallible, efficient conversion -// to any other format. import ( "bytes" @@ -67,7 +62,6 @@ import ( "go/ast" "go/token" "sort" - "strings" "sync" "unicode/utf8" @@ -195,7 +189,6 @@ func (m *Mapper) OffsetPosition(offset int) (Position, error) { } // No error may be returned after this point, // even if the offset does not fall at a rune boundary. - // (See panic in MappedRange.Range reachable.) line, col16 := m.lineCol16(offset) return Position{Line: uint32(line), Character: uint32(col16)}, nil @@ -251,15 +244,6 @@ func (m *Mapper) line(offset int) (int, int, bool) { return line, m.lineStart[line], cr } -// OffsetMappedRange returns a MappedRange for the given byte offsets. -// A MappedRange can be converted to any other form. -func (m *Mapper) OffsetMappedRange(start, end int) (MappedRange, error) { - if !(0 <= start && start <= end && end <= len(m.Content)) { - return MappedRange{}, fmt.Errorf("invalid offsets (%d, %d) (file %s has size %d)", start, end, m.URI, len(m.Content)) - } - return MappedRange{m, start, end}, nil -} - // -- conversions from protocol (UTF-16) domain -- // RangeOffsets converts a protocol (UTF-16) range to start/end byte offsets. @@ -362,78 +346,6 @@ func (m *Mapper) RangeLocation(rng Range) Location { return Location{URI: m.URI, Range: rng} } -// PosMappedRange returns a MappedRange for the given token.Pos range. -func (m *Mapper) PosMappedRange(tf *token.File, start, end token.Pos) (MappedRange, error) { - startOffset, endOffset, err := safetoken.Offsets(tf, start, end) - if err != nil { - return MappedRange{}, nil - } - return m.OffsetMappedRange(startOffset, endOffset) -} - -// NodeMappedRange returns a MappedRange for the given node range. -func (m *Mapper) NodeMappedRange(tf *token.File, node ast.Node) (MappedRange, error) { - return m.PosMappedRange(tf, node.Pos(), node.End()) -} - -// -- MappedRange -- - -// A MappedRange represents a valid byte-offset range of a file. -// Through its Mapper it can be converted into other forms such -// as protocol.Range or UTF-8. -// -// Construct one by calling Mapper.OffsetMappedRange with start/end offsets. -// From the go/token domain, call safetoken.Offsets first, -// or use a helper such as parsego.File.MappedPosRange. -// -// Two MappedRanges produced the same Mapper are equal if and only if they -// denote the same range. Two MappedRanges produced by different Mappers -// are unequal even when they represent the same range of the same file. -type MappedRange struct { - Mapper *Mapper - start, end int // valid byte offsets: 0 <= start <= end <= len(Mapper.Content) -} - -// Offsets returns the (start, end) byte offsets of this range. -func (mr MappedRange) Offsets() (start, end int) { return mr.start, mr.end } - -// -- convenience functions -- - -// URI returns the URI of the range's file. -func (mr MappedRange) URI() DocumentURI { - return mr.Mapper.URI -} - -// Range returns the range in protocol (UTF-16) form. -func (mr MappedRange) Range() Range { - rng, err := mr.Mapper.OffsetRange(mr.start, mr.end) - if err != nil { - panic(err) // can't happen - } - return rng -} - -// Location returns the range in protocol location (UTF-16) form. -func (mr MappedRange) Location() Location { - return mr.Mapper.RangeLocation(mr.Range()) -} - -// String formats the range in UTF-8 notation. -func (mr MappedRange) String() string { - var s strings.Builder - startLine, startCol8 := mr.Mapper.OffsetLineCol8(mr.start) - fmt.Fprintf(&s, "%d:%d", startLine, startCol8) - if mr.end != mr.start { - endLine, endCol8 := mr.Mapper.OffsetLineCol8(mr.end) - if endLine == startLine { - fmt.Fprintf(&s, "-%d", endCol8) - } else { - fmt.Fprintf(&s, "-%d:%d", endLine, endCol8) - } - } - return s.String() -} - // LocationTextDocumentPositionParams converts its argument to its result. func LocationTextDocumentPositionParams(loc Location) TextDocumentPositionParams { return TextDocumentPositionParams{ diff --git a/gopls/internal/server/folding_range.go b/gopls/internal/server/folding_range.go index 0ad00e54c8d..95b2ffc0744 100644 --- a/gopls/internal/server/folding_range.go +++ b/gopls/internal/server/folding_range.go @@ -36,7 +36,7 @@ func (s *server) FoldingRange(ctx context.Context, params *protocol.FoldingRange func toProtocolFoldingRanges(ranges []*golang.FoldingRangeInfo) ([]protocol.FoldingRange, error) { result := make([]protocol.FoldingRange, 0, len(ranges)) for _, info := range ranges { - rng := info.MappedRange.Range() + rng := info.Range result = append(result, protocol.FoldingRange{ StartLine: rng.Start.Line, StartCharacter: rng.Start.Character, From dace8c8b7316f156b70b078b4d941961b3afc458 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 31 Jan 2025 12:50:44 -0500 Subject: [PATCH 085/309] gopls/internal/analysis/modernize: fix bug in slicescontains Previously, a fix was offered to use slices.Contains(haystack, needle) even when the slice element type was not identical to the needle, resulting in a type error. (We really should make RunWithSuggestedFixes verify that the fixed code is still well-typed.) Now, we don't report a diagnostic if the types differ. (We could insert a widening cast in one of the two cases, but that's left for a follow-up.) Fixes golang/go#71313 Change-Id: I41764da93085eef8ea5d30337d01fe03ee345f6d Reviewed-on: https://go-review.googlesource.com/c/tools/+/645856 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- .../analysis/modernize/slicescontains.go | 20 +++++++++++++++++++ .../src/slicescontains/slicescontains.go | 19 ++++++++++++++++++ .../slicescontains/slicescontains.go.golden | 19 ++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index 062083ca141..dc0aa613a50 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -85,13 +85,33 @@ func slicescontains(pass *analysis.Pass) { switch cond := ifStmt.Cond.(type) { case *ast.BinaryExpr: if cond.Op == token.EQL { + var elem ast.Expr if isSliceElem(cond.X) { funcName = "Contains" + elem = cond.X arg2 = cond.Y // "if elem == needle" } else if isSliceElem(cond.Y) { funcName = "Contains" + elem = cond.Y arg2 = cond.X // "if needle == elem" } + + // Reject if elem and needle have different types. + if elem != nil { + tElem := info.TypeOf(elem) + tNeedle := info.TypeOf(arg2) + if !types.Identical(tElem, tNeedle) { + // Avoid ill-typed slices.Contains([]error, any). + if !types.AssignableTo(tNeedle, tElem) { + return + } + // TODO(adonovan): relax this check to allow + // slices.Contains([]error, error(any)), + // inserting an explicit widening conversion + // around the needle. + return + } + } } case *ast.CallExpr: diff --git a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go index ecb73719c0e..6116ce14838 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go +++ b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go @@ -127,3 +127,22 @@ func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool { } func predicate(int) bool + +// Regression tests for bad fixes when needle +// and haystack have different types (#71313): + +func nopeNeedleHaystackDifferentTypes(x any, args []error) { + for _, arg := range args { + if arg == x { + return + } + } +} + +func nopeNeedleHaystackDifferentTypes2(x error, args []any) { + for _, arg := range args { + if arg == x { + return + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden index 561e42f7dd1..2d67395f203 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/slicescontains/slicescontains.go.golden @@ -83,3 +83,22 @@ func nopeLoopBodyHasFreeContinuation(slice []int, needle int) bool { } func predicate(int) bool + +// Regression tests for bad fixes when needle +// and haystack have different types (#71313): + +func nopeNeedleHaystackDifferentTypes(x any, args []error) { + for _, arg := range args { + if arg == x { + return + } + } +} + +func nopeNeedleHaystackDifferentTypes2(x error, args []any) { + for _, arg := range args { + if arg == x { + return + } + } +} From 5ffcf75f4073ef2d957e27bd8d22df7d0fe2ba4e Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 31 Jan 2025 16:38:01 +0000 Subject: [PATCH 086/309] internal/refactor/inline: avoid crash when inlining empty function When the call to an empty function is inside a go or defer statement, the callStmt helper can return a nil ExprStmt. Previously, this would result in a typed nil for res.old, leading to the crash of golang/go#71486. Fixes golang/go#71486 Change-Id: Ib956be2cf0d628567734f18b33e529999e081ab9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645816 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/inline.go | 84 ++++++++++--------- .../refactor/inline/testdata/empty-body.txtar | 17 ++++ 2 files changed, 61 insertions(+), 40 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index c981599b5b0..7d54cca7ccd 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -1004,53 +1004,57 @@ func (st *state) inlineCall() (*inlineCallResult, error) { logf("strategy: reduce call to empty body") // Evaluate the arguments for effects and delete the call entirely. - stmt := callStmt(caller.path, false) // cannot fail - res.old = stmt - if nargs := len(remainingArgs); nargs > 0 { - // Emit "_, _ = args" to discard results. - - // TODO(adonovan): if args is the []T{a1, ..., an} - // literal synthesized during variadic simplification, - // consider unwrapping it to its (pure) elements. - // Perhaps there's no harm doing this for any slice literal. - - // Make correction for spread calls - // f(g()) or recv.f(g()) where g() is a tuple. - if last := last(args); last != nil && last.spread { - nspread := last.typ.(*types.Tuple).Len() - if len(args) > 1 { // [recv, g()] - // A single AssignStmt cannot discard both, so use a 2-spec var decl. - res.new = &ast.GenDecl{ - Tok: token.VAR, - Specs: []ast.Spec{ - &ast.ValueSpec{ - Names: []*ast.Ident{makeIdent("_")}, - Values: []ast.Expr{args[0].expr}, - }, - &ast.ValueSpec{ - Names: blanks[*ast.Ident](nspread), - Values: []ast.Expr{args[1].expr}, + // Note(golang/go#71486): stmt can be nil if the call is in a go or defer + // statement. + // TODO: discard go or defer statements as well. + if stmt := callStmt(caller.path, false); stmt != nil { + res.old = stmt + if nargs := len(remainingArgs); nargs > 0 { + // Emit "_, _ = args" to discard results. + + // TODO(adonovan): if args is the []T{a1, ..., an} + // literal synthesized during variadic simplification, + // consider unwrapping it to its (pure) elements. + // Perhaps there's no harm doing this for any slice literal. + + // Make correction for spread calls + // f(g()) or recv.f(g()) where g() is a tuple. + if last := last(args); last != nil && last.spread { + nspread := last.typ.(*types.Tuple).Len() + if len(args) > 1 { // [recv, g()] + // A single AssignStmt cannot discard both, so use a 2-spec var decl. + res.new = &ast.GenDecl{ + Tok: token.VAR, + Specs: []ast.Spec{ + &ast.ValueSpec{ + Names: []*ast.Ident{makeIdent("_")}, + Values: []ast.Expr{args[0].expr}, + }, + &ast.ValueSpec{ + Names: blanks[*ast.Ident](nspread), + Values: []ast.Expr{args[1].expr}, + }, }, - }, + } + return res, nil } - return res, nil + + // Sole argument is spread call. + nargs = nspread } - // Sole argument is spread call. - nargs = nspread - } + res.new = &ast.AssignStmt{ + Lhs: blanks[ast.Expr](nargs), + Tok: token.ASSIGN, + Rhs: remainingArgs, + } - res.new = &ast.AssignStmt{ - Lhs: blanks[ast.Expr](nargs), - Tok: token.ASSIGN, - Rhs: remainingArgs, + } else { + // No remaining arguments: delete call statement entirely + res.new = &ast.EmptyStmt{} } - - } else { - // No remaining arguments: delete call statement entirely - res.new = &ast.EmptyStmt{} + return res, nil } - return res, nil } // If all parameters have been substituted and no result diff --git a/internal/refactor/inline/testdata/empty-body.txtar b/internal/refactor/inline/testdata/empty-body.txtar index 8983fda8c6e..fa0689a2125 100644 --- a/internal/refactor/inline/testdata/empty-body.txtar +++ b/internal/refactor/inline/testdata/empty-body.txtar @@ -101,3 +101,20 @@ func _() { var x T _ = x //@ inline(re"empty", empty4) } + +-- a/a5.go -- +package a + +func _() { + go empty() //@ inline(re"empty", empty5) +} + +func empty() {} +-- empty5 -- +package a + +func _() { + go func() {}() //@ inline(re"empty", empty5) +} + +func empty() {} From 51f179cad1fb92b2d4761e6596623eaa49ff0a35 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 31 Jan 2025 18:01:59 +0000 Subject: [PATCH 087/309] gopls/internal/golang: downgrade bug report in ExtractToNewFile While we shouldn't serve 'extract' code actions containing an invalid range, we also as a matter of principle should not report bugs for invalid commands from the client, even if for well-behaved clients they should only originate from our own code action. In other words, this could easily be a manifestation of a client bug, and we should focus our attention on certain gopls bugs. Fixes golang/go#71473 Change-Id: Id740d03e5043dc17c6ed50a9f9c767f9de445ed5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645875 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/extracttofile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/internal/golang/extracttofile.go b/gopls/internal/golang/extracttofile.go index cda9cd51e6d..39fb28e624b 100644 --- a/gopls/internal/golang/extracttofile.go +++ b/gopls/internal/golang/extracttofile.go @@ -95,7 +95,7 @@ func ExtractToNewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Han start, end, firstSymbol, ok := selectedToplevelDecls(pgf, start, end) if !ok { - return nil, bug.Errorf("invalid selection") + return nil, fmt.Errorf("invalid selection") } pgf.CheckPos(start) // #70553 // Inv: start is valid wrt pgf.Tok. From b3bde130c3a4b833e5149c87c60c2c59a807629b Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 31 Jan 2025 15:49:17 -0500 Subject: [PATCH 088/309] x/tools: use types.VarKind consistently This CL is a clean-up in preparation for the new types.VarKind API of go1.25. It sets VarKind appropriately for every call to types.NewVar (and replaces calls to NewVar with New{Param,Field} where possible). This doesn't actually do anything yet; once CL 645115 lands we can define a go1.25-tagged version of this API that actually gets/sets the Var.Kind field. Updates golang/go#70250 Change-Id: I576fcb0c6291c07ef6f98551051edd6d3127abb1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645656 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/passes/asmdecl/asmdecl.go | 4 +- .../passes/unusedresult/unusedresult.go | 2 +- go/internal/gccgoimporter/parser.go | 1 + go/ssa/interp/reflect.go | 2 +- go/ssa/subst.go | 2 +- .../analysis/unusedparams/unusedparams.go | 7 +++- gopls/internal/golang/completion/util.go | 5 ++- gopls/internal/golang/hover.go | 7 ++-- gopls/internal/golang/pkgdoc.go | 2 +- internal/apidiff/apidiff.go | 2 +- internal/gcimporter/iimport.go | 4 +- internal/gcimporter/ureader_yes.go | 6 ++- internal/refactor/inline/inline.go | 5 ++- internal/typesinternal/varkind.go | 40 +++++++++++++++++++ 14 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 internal/typesinternal/varkind.go diff --git a/go/analysis/passes/asmdecl/asmdecl.go b/go/analysis/passes/asmdecl/asmdecl.go index b622dfdf3a0..a47ecbae731 100644 --- a/go/analysis/passes/asmdecl/asmdecl.go +++ b/go/analysis/passes/asmdecl/asmdecl.go @@ -542,8 +542,8 @@ func appendComponentsRecursive(arch *asmArch, t types.Type, cc []component, suff elem := tu.Elem() // Calculate offset of each element array. fields := []*types.Var{ - types.NewVar(token.NoPos, nil, "fake0", elem), - types.NewVar(token.NoPos, nil, "fake1", elem), + types.NewField(token.NoPos, nil, "fake0", elem, false), + types.NewField(token.NoPos, nil, "fake1", elem, false), } offsets := arch.sizes.Offsetsof(fields) elemoff := int(offsets[1]) diff --git a/go/analysis/passes/unusedresult/unusedresult.go b/go/analysis/passes/unusedresult/unusedresult.go index c27d26dd6ec..d7cc1e6ae2c 100644 --- a/go/analysis/passes/unusedresult/unusedresult.go +++ b/go/analysis/passes/unusedresult/unusedresult.go @@ -131,7 +131,7 @@ func run(pass *analysis.Pass) (interface{}, error) { // func() string var sigNoArgsStringResult = types.NewSignature(nil, nil, - types.NewTuple(types.NewVar(token.NoPos, nil, "", types.Typ[types.String])), + types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])), false) type stringSetFlag map[string]bool diff --git a/go/internal/gccgoimporter/parser.go b/go/internal/gccgoimporter/parser.go index 7a021ebb4b2..f315ec41004 100644 --- a/go/internal/gccgoimporter/parser.go +++ b/go/internal/gccgoimporter/parser.go @@ -309,6 +309,7 @@ func (p *parser) parseParam(pkg *types.Package) (param *types.Var, isVariadic bo func (p *parser) parseVar(pkg *types.Package) *types.Var { name := p.parseName() v := types.NewVar(token.NoPos, pkg, name, p.parseType(pkg)) + typesinternal.SetVarKind(v, typesinternal.PackageVar) if name[0] == '.' || name[0] == '<' { // This is an unexported variable, // or a variable defined in a different package. diff --git a/go/ssa/interp/reflect.go b/go/ssa/interp/reflect.go index 3143c077790..8259e56d860 100644 --- a/go/ssa/interp/reflect.go +++ b/go/ssa/interp/reflect.go @@ -510,7 +510,7 @@ func newMethod(pkg *ssa.Package, recvType types.Type, name string) *ssa.Function // that is needed is the "pointerness" of Recv.Type, and for // now, we'll set it to always be false since we're only // concerned with rtype. Encapsulate this better. - sig := types.NewSignature(types.NewVar(token.NoPos, nil, "recv", recvType), nil, nil, false) + sig := types.NewSignature(types.NewParam(token.NoPos, nil, "recv", recvType), nil, nil, false) fn := pkg.Prog.NewFunction(name, sig, "fake reflect method") fn.Pkg = pkg return fn diff --git a/go/ssa/subst.go b/go/ssa/subst.go index fc870235c42..bbe5796d703 100644 --- a/go/ssa/subst.go +++ b/go/ssa/subst.go @@ -227,7 +227,7 @@ func (subst *subster) var_(v *types.Var) *types.Var { if v.IsField() { return types.NewField(v.Pos(), v.Pkg(), v.Name(), typ, v.Embedded()) } - return types.NewVar(v.Pos(), v.Pkg(), v.Name(), typ) + return types.NewParam(v.Pos(), v.Pkg(), v.Name(), typ) } } return v diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index 61bdf51834e..ff7fdc4418c 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -17,6 +17,7 @@ import ( "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal" ) //go:embed doc.go @@ -201,8 +202,10 @@ func run(pass *analysis.Pass) (any, error) { if fn == nil && id.Name == "_" { // Edge case: _ = func() {...} - // has no var. Fake one. - fn = types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) + // has no local var. Fake one. + v := types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) + typesinternal.SetVarKind(v, typesinternal.LocalVar) + fn = v } } } diff --git a/gopls/internal/golang/completion/util.go b/gopls/internal/golang/completion/util.go index cb51d65ffee..7a4729413ae 100644 --- a/gopls/internal/golang/completion/util.go +++ b/gopls/internal/golang/completion/util.go @@ -15,6 +15,7 @@ import ( "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/typesinternal" ) // exprAtPos returns the index of the expression containing pos. @@ -126,7 +127,9 @@ func resolveInvalid(fset *token.FileSet, obj types.Object, node ast.Node, info * // Construct a fake type for the object and return a fake object with this type. typename := golang.FormatNode(fset, resultExpr) typ := types.NewNamed(types.NewTypeName(token.NoPos, obj.Pkg(), typename, nil), types.Typ[types.Invalid], nil) - return types.NewVar(obj.Pos(), obj.Pkg(), obj.Name(), typ) + v := types.NewVar(obj.Pos(), obj.Pkg(), obj.Name(), typ) + typesinternal.SetVarKind(v, typesinternal.PackageVar) + return v } // TODO(adonovan): inline these. diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index ead5ec4a5db..7fc584f2c1a 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -280,12 +280,13 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro // // There's not much useful information to provide. if selectedType != nil { - fakeObj := types.NewVar(obj.Pos(), obj.Pkg(), obj.Name(), selectedType) - signature := types.ObjectString(fakeObj, qual) + v := types.NewVar(obj.Pos(), obj.Pkg(), obj.Name(), selectedType) + typesinternal.SetVarKind(v, typesinternal.LocalVar) + signature := types.ObjectString(v, qual) return *hoverRange, &hoverResult{ signature: signature, singleLine: signature, - symbolName: fakeObj.Name(), + symbolName: v.Name(), }, nil } diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 115fbf979f8..a5f9cc97fa4 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -667,7 +667,7 @@ window.addEventListener('load', function() { cloneTparams(sig.TypeParams()), types.NewTuple(append( slices.Collect(tupleVariables(sig.Params()))[:3], - types.NewVar(0, nil, "", types.Typ[types.Invalid]))...), + types.NewParam(0, nil, "", types.Typ[types.Invalid]))...), sig.Results(), false) // any final ...T parameter is truncated } diff --git a/internal/apidiff/apidiff.go b/internal/apidiff/apidiff.go index a37d5daca38..4bf70d9b42d 100644 --- a/internal/apidiff/apidiff.go +++ b/internal/apidiff/apidiff.go @@ -216,7 +216,7 @@ func removeNamesFromSignature(t types.Type) types.Type { var vars []*types.Var for i := 0; i < p.Len(); i++ { v := p.At(i) - vars = append(vars, types.NewVar(v.Pos(), v.Pkg(), "", types.Unalias(v.Type()))) + vars = append(vars, types.NewParam(v.Pos(), v.Pkg(), "", types.Unalias(v.Type()))) } return types.NewTuple(vars...) } diff --git a/internal/gcimporter/iimport.go b/internal/gcimporter/iimport.go index 69b1d697cbe..12943927159 100644 --- a/internal/gcimporter/iimport.go +++ b/internal/gcimporter/iimport.go @@ -671,7 +671,9 @@ func (r *importReader) obj(name string) { case varTag: typ := r.typ() - r.declare(types.NewVar(pos, r.currPkg, name, typ)) + v := types.NewVar(pos, r.currPkg, name, typ) + typesinternal.SetVarKind(v, typesinternal.PackageVar) + r.declare(v) default: errorf("unexpected tag: %v", tag) diff --git a/internal/gcimporter/ureader_yes.go b/internal/gcimporter/ureader_yes.go index 6cdab448eca..522287d18d6 100644 --- a/internal/gcimporter/ureader_yes.go +++ b/internal/gcimporter/ureader_yes.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/internal/aliases" "golang.org/x/tools/internal/pkgbits" + "golang.org/x/tools/internal/typesinternal" ) // A pkgReader holds the shared state for reading a unified IR package @@ -572,6 +573,7 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) { sig := fn.Type().(*types.Signature) recv := types.NewVar(fn.Pos(), fn.Pkg(), "", named) + typesinternal.SetVarKind(recv, typesinternal.RecvVar) methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignature(recv, sig.Params(), sig.Results(), sig.Variadic())) } @@ -619,7 +621,9 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) { case pkgbits.ObjVar: pos := r.pos() typ := r.typ() - declare(types.NewVar(pos, objPkg, objName, typ)) + v := types.NewVar(pos, objPkg, objName, typ) + typesinternal.SetVarKind(v, typesinternal.PackageVar) + declare(v) } } diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 7d54cca7ccd..2c897c24954 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -25,6 +25,7 @@ import ( "golang.org/x/tools/imports" internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/typesinternal" ) // A Caller describes the function call and its enclosing context. @@ -1950,7 +1951,9 @@ func checkFalconConstraints(logf logger, params []*parameter, args []*argument, logf("falcon env: const %s %s = %v", name, param.info.FalconType, arg.constant) nconst++ } else { - pkg.Scope().Insert(types.NewVar(token.NoPos, pkg, name, arg.typ)) + v := types.NewVar(token.NoPos, pkg, name, arg.typ) + typesinternal.SetVarKind(v, typesinternal.PackageVar) + pkg.Scope().Insert(v) logf("falcon env: var %s %s", name, arg.typ) } } diff --git a/internal/typesinternal/varkind.go b/internal/typesinternal/varkind.go new file mode 100644 index 00000000000..e5da0495111 --- /dev/null +++ b/internal/typesinternal/varkind.go @@ -0,0 +1,40 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typesinternal + +// TODO(adonovan): when CL 645115 lands, define the go1.25 version of +// this API that actually does something. + +import "go/types" + +type VarKind uint8 + +const ( + _ VarKind = iota // (not meaningful) + PackageVar // a package-level variable + LocalVar // a local variable + RecvVar // a method receiver variable + ParamVar // a function parameter variable + ResultVar // a function result variable + FieldVar // a struct field +) + +func (kind VarKind) String() string { + return [...]string{ + 0: "VarKind(0)", + PackageVar: "PackageVar", + LocalVar: "LocalVar", + RecvVar: "RecvVar", + ParamVar: "ParamVar", + ResultVar: "ResultVar", + FieldVar: "FieldVar", + }[kind] +} + +// GetVarKind returns an invalid VarKind. +func GetVarKind(v *types.Var) VarKind { return 0 } + +// SetVarKind has no effect. +func SetVarKind(v *types.Var, kind VarKind) {} From da3a6b293cba6bffbf795a07db8031c8d8b6a88d Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 28 Jan 2025 16:40:33 -0500 Subject: [PATCH 089/309] internal/settings: add inliner to analyses This analyzer will supply a code action on a call to a function that is annotated "//go:fix inline". Updates golang/go#32816. Change-Id: I9e0da21a4e1cb8dd9c167099108c7d8adfcaf012 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645155 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/doc/analyzers.md | 9 +++++++++ gopls/internal/doc/api.json | 11 +++++++++++ gopls/internal/settings/analysis.go | 2 ++ gopls/internal/test/marker/doc.go | 2 +- .../test/marker/testdata/diagnostics/analyzers.txt | 8 ++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index aa0081df9d0..282a9f436c6 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -376,6 +376,15 @@ Default: on. Package documentation: [infertypeargs](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/infertypeargs) + +## `inline`: inline calls to functions with "//go:fix inline" doc comment + + + +Default: on. + +Package documentation: [inline](https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer) + ## `loopclosure`: check references to loop variables from within nested functions diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 043907227a3..83151ae6373 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -493,6 +493,11 @@ "Doc": "check for unnecessary type arguments in call expressions\n\nExplicit type arguments may be omitted from call expressions if they can be\ninferred from function arguments, or from other type arguments:\n\n\tfunc f[T any](T) {}\n\t\n\tfunc _() {\n\t\tf[string](\"foo\") // string could be inferred\n\t}\n", "Default": "true" }, + { + "Name": "\"inline\"", + "Doc": "inline calls to functions with \"//go:fix inline\" doc comment", + "Default": "true" + }, { "Name": "\"loopclosure\"", "Doc": "check references to loop variables from within nested functions\n\nThis analyzer reports places where a function literal references the\niteration variable of an enclosing loop, and the loop calls the function\nin such a way (e.g. with go or defer) that it may outlive the loop\niteration and possibly observe the wrong value of the variable.\n\nNote: An iteration variable can only outlive a loop iteration in Go versions \u003c=1.21.\nIn Go 1.22 and later, the loop variable lifetimes changed to create a new\niteration variable per loop iteration. (See go.dev/issue/60078.)\n\nIn this example, all the deferred functions run after the loop has\ncompleted, so all observe the final value of v [\u003cgo1.22].\n\n\tfor _, v := range list {\n\t defer func() {\n\t use(v) // incorrect\n\t }()\n\t}\n\nOne fix is to create a new variable for each iteration of the loop:\n\n\tfor _, v := range list {\n\t v := v // new var per iteration\n\t defer func() {\n\t use(v) // ok\n\t }()\n\t}\n\nAfter Go version 1.22, the previous two for loops are equivalent\nand both are correct.\n\nThe next example uses a go statement and has a similar problem [\u003cgo1.22].\nIn addition, it has a data race because the loop updates v\nconcurrent with the goroutines accessing it.\n\n\tfor _, v := range elem {\n\t go func() {\n\t use(v) // incorrect, and a data race\n\t }()\n\t}\n\nA fix is the same as before. The checker also reports problems\nin goroutines started by golang.org/x/sync/errgroup.Group.\nA hard-to-spot variant of this form is common in parallel tests:\n\n\tfunc Test(t *testing.T) {\n\t for _, test := range tests {\n\t t.Run(test.name, func(t *testing.T) {\n\t t.Parallel()\n\t use(test) // incorrect, and a data race\n\t })\n\t }\n\t}\n\nThe t.Parallel() call causes the rest of the function to execute\nconcurrent with the loop [\u003cgo1.22].\n\nThe analyzer reports references only in the last statement,\nas it is not deep enough to understand the effects of subsequent\nstatements that might render the reference benign.\n(\"Last statement\" is defined recursively in compound\nstatements such as if, switch, and select.)\n\nSee: https://golang.org/doc/go_faq.html#closures_and_goroutines", @@ -1164,6 +1169,12 @@ "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/infertypeargs", "Default": true }, + { + "Name": "inline", + "Doc": "inline calls to functions with \"//go:fix inline\" doc comment", + "URL": "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", + "Default": true + }, { "Name": "loopclosure", "Doc": "check references to loop variables from within nested functions\n\nThis analyzer reports places where a function literal references the\niteration variable of an enclosing loop, and the loop calls the function\nin such a way (e.g. with go or defer) that it may outlive the loop\niteration and possibly observe the wrong value of the variable.\n\nNote: An iteration variable can only outlive a loop iteration in Go versions \u003c=1.21.\nIn Go 1.22 and later, the loop variable lifetimes changed to create a new\niteration variable per loop iteration. (See go.dev/issue/60078.)\n\nIn this example, all the deferred functions run after the loop has\ncompleted, so all observe the final value of v [\u003cgo1.22].\n\n\tfor _, v := range list {\n\t defer func() {\n\t use(v) // incorrect\n\t }()\n\t}\n\nOne fix is to create a new variable for each iteration of the loop:\n\n\tfor _, v := range list {\n\t v := v // new var per iteration\n\t defer func() {\n\t use(v) // ok\n\t }()\n\t}\n\nAfter Go version 1.22, the previous two for loops are equivalent\nand both are correct.\n\nThe next example uses a go statement and has a similar problem [\u003cgo1.22].\nIn addition, it has a data race because the loop updates v\nconcurrent with the goroutines accessing it.\n\n\tfor _, v := range elem {\n\t go func() {\n\t use(v) // incorrect, and a data race\n\t }()\n\t}\n\nA fix is the same as before. The checker also reports problems\nin goroutines started by golang.org/x/sync/errgroup.Group.\nA hard-to-spot variant of this form is common in parallel tests:\n\n\tfunc Test(t *testing.T) {\n\t for _, test := range tests {\n\t t.Run(test.name, func(t *testing.T) {\n\t t.Parallel()\n\t use(test) // incorrect, and a data race\n\t })\n\t }\n\t}\n\nThe t.Parallel() call causes the rest of the function to execute\nconcurrent with the loop [\u003cgo1.22].\n\nThe analyzer reports references only in the last statement,\nas it is not deep enough to understand the effects of subsequent\nstatements that might render the reference benign.\n(\"Last statement\" is defined recursively in compound\nstatements such as if, switch, and select.)\n\nSee: https://golang.org/doc/go_faq.html#closures_and_goroutines", diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 7be5d896d75..cd0254e5886 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -62,6 +62,7 @@ import ( "golang.org/x/tools/gopls/internal/analysis/unusedvariable" "golang.org/x/tools/gopls/internal/analysis/yield" "golang.org/x/tools/gopls/internal/protocol" + inline "golang.org/x/tools/internal/refactor/inline/analyzer" ) // Analyzer augments a [analysis.Analyzer] with additional LSP configuration. @@ -210,6 +211,7 @@ func init() { severity: protocol.SeverityInformation, }, // other simplifiers + {analyzer: inline.Analyzer, severity: protocol.SeverityHint}, {analyzer: infertypeargs.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedparams.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedfunc.Analyzer, severity: protocol.SeverityInformation}, diff --git a/gopls/internal/test/marker/doc.go b/gopls/internal/test/marker/doc.go index abddbddacd3..dff8dfa109f 100644 --- a/gopls/internal/test/marker/doc.go +++ b/gopls/internal/test/marker/doc.go @@ -120,7 +120,7 @@ Here is the list of supported value markers: argument may be specified only as a string or regular expression in the first pass. - - defloc(name, location): performs a textDocument/defintiion request at the + - defloc(name, location): performs a textDocument/definition request at the src location, and binds the result to the given name. This may be used to refer to positions in the standard library. diff --git a/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt b/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt index 7ba338032e9..459ff985d7e 100644 --- a/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt +++ b/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt @@ -74,6 +74,14 @@ func _() { }() } +// inline +func _() { + f() //@diag("f", re"inline call of analyzer.f") +} + +//go:fix inline +func f() { fmt.Println(1) } + -- cgocall/cgocall.go -- package cgocall From 269282df3656104304fb285c9c240ebed53824b9 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 10 Jan 2025 15:04:41 +0100 Subject: [PATCH 090/309] go/analysis/passes/stdversion: use Go 1.22 functionality Currently go.mod specifies go1.22, so make use of functions available in standard library package in Go 1.22. Change-Id: I3f0c197370747ed3c18ccf00f17e644e7095287b Reviewed-on: https://go-review.googlesource.com/c/tools/+/641877 Auto-Submit: Tobias Klauser Reviewed-by: Cherry Mui Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/passes/stdversion/stdversion.go | 32 +++------------------ 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/go/analysis/passes/stdversion/stdversion.go b/go/analysis/passes/stdversion/stdversion.go index 75d8697759e..429125a8b7d 100644 --- a/go/analysis/passes/stdversion/stdversion.go +++ b/go/analysis/passes/stdversion/stdversion.go @@ -11,6 +11,7 @@ import ( "go/build" "go/types" "regexp" + "slices" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -46,16 +47,14 @@ func run(pass *analysis.Pass) (any, error) { // Prior to go1.22, versions.FileVersion returns only the // toolchain version, which is of no use to us, so // disable this analyzer on earlier versions. - if !slicesContains(build.Default.ReleaseTags, "go1.22") { + if !slices.Contains(build.Default.ReleaseTags, "go1.22") { return nil, nil } // Don't report diagnostics for modules marked before go1.21, // since at that time the go directive wasn't clearly // specified as a toolchain requirement. - // - // TODO(adonovan): after go1.21, call GoVersion directly. - pkgVersion := any(pass.Pkg).(interface{ GoVersion() string }).GoVersion() + pkgVersion := pass.Pkg.GoVersion() if !versions.AtLeast(pkgVersion, "go1.21") { return nil, nil } @@ -88,7 +87,7 @@ func run(pass *analysis.Pass) (any, error) { inspect.Preorder(nodeFilter, func(n ast.Node) { switch n := n.(type) { case *ast.File: - if isGenerated(n) { + if ast.IsGenerated(n) { // Suppress diagnostics in generated files (such as cgo). fileVersion = "" } else { @@ -115,19 +114,6 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } -// Reduced from x/tools/gopls/internal/golang/util.go. Good enough for now. -// TODO(adonovan): use ast.IsGenerated in go1.21. -func isGenerated(f *ast.File) bool { - for _, group := range f.Comments { - for _, comment := range group.List { - if matched := generatedRx.MatchString(comment.Text); matched { - return true - } - } - } - return false -} - // Matches cgo generated comment as well as the proposed standard: // // https://golang.org/s/generatedcode @@ -147,13 +133,3 @@ func origin(obj types.Object) types.Object { } return obj } - -// TODO(adonovan): use go1.21 slices.Contains. -func slicesContains[S ~[]E, E comparable](slice S, x E) bool { - for _, elem := range slice { - if elem == x { - return true - } - } - return false -} From 88d91cbe4a98b46fe2426ed06ece5e781ca8466d Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 29 Jan 2025 16:18:31 -0500 Subject: [PATCH 091/309] internal/refactor/inline/analyzer: same-package consts Suggest inlining of consts marked //go:fix inline. This CL handles definitions and uses in the same package only. It also handles only top-level const declarations. Cross-package and local consts will be dealt with in followup CLs. For golang/go#32816. Change-Id: I67bfc38b432b0609cca44add648a9faa1d40ece6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645455 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- .../marker/testdata/diagnostics/analyzers.txt | 4 +- internal/refactor/inline/analyzer/analyzer.go | 269 ++++++++++++------ internal/refactor/inline/analyzer/const.go | 21 ++ .../inline/analyzer/testdata/src/a/a.go | 55 +++- .../analyzer/testdata/src/a/a.go.golden | 55 +++- .../inline/analyzer/testdata/src/b/b.go | 2 +- .../analyzer/testdata/src/b/b.go.golden | 2 +- 7 files changed, 318 insertions(+), 90 deletions(-) create mode 100644 internal/refactor/inline/analyzer/const.go diff --git a/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt b/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt index 459ff985d7e..fb7876a0492 100644 --- a/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt +++ b/gopls/internal/test/marker/testdata/diagnostics/analyzers.txt @@ -76,8 +76,8 @@ func _() { // inline func _() { - f() //@diag("f", re"inline call of analyzer.f") -} + f() //@diag("f", re"Call of analyzer.f should be inlined") +} //go:fix inline func f() { fmt.Println(1) } diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index 0e3fec82f95..a0d3bc9f530 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -26,7 +26,7 @@ var Analyzer = &analysis.Analyzer{ Doc: Doc, URL: "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", Run: run, - FactTypes: []analysis.Fact{new(goFixInlineFact)}, + FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixInlineConstFact)}, Requires: []*analysis.Analyzer{inspect.Analyzer}, } @@ -47,41 +47,89 @@ func run(pass *analysis.Pass) (any, error) { return content, nil } - // Pass 1: find functions annotated with a "//go:fix inline" + // Pass 1: find functions and constants annotated with a "//go:fix inline" // comment (the syntax proposed by #32816), // and export a fact for each one. - inlinable := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) + inlinableFuncs := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) + inlinableConsts := make(map[*types.Const]*goFixInlineConstFact) for _, file := range pass.Files { for _, decl := range file.Decls { - if decl, ok := decl.(*ast.FuncDecl); ok && - slices.ContainsFunc(directives(decl.Doc), func(d *directive) bool { - return d.Tool == "go" && d.Name == "fix" && d.Args == "inline" - }) { + switch decl := decl.(type) { + case *ast.FuncDecl: + if hasInlineDirective(decl.Doc) { + content, err := readFile(decl) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + continue + } + callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) + continue + } + fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) + pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) + inlinableFuncs[fn] = callee + } - content, err := readFile(decl) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + case *ast.GenDecl: + if decl.Tok != token.CONST { continue } - callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) - continue + // Accept inline directives on the entire decl as well as individual specs. + declInline := hasInlineDirective(decl.Doc) + for _, spec := range decl.Specs { + spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST + if declInline || hasInlineDirective(spec.Doc) { + for i, name := range spec.Names { + if i >= len(spec.Values) { + // Possible following an iota. + break + } + val := spec.Values[i] + var rhsID *ast.Ident + switch e := val.(type) { + case *ast.Ident: + if e.Name == "iota" { + continue + } + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + continue + } + lhs := pass.TypesInfo.Defs[name].(*types.Const) + rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program + con := &goFixInlineConstFact{ + RHSName: rhs.Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + pass.ExportObjectFact(lhs, con) + inlinableConsts[lhs] = con + } + } } - fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) - pass.ExportObjectFact(fn, &goFixInlineFact{callee}) - inlinable[fn] = callee + // TODO(jba): in user doc, warn that a comments within a spec, as in + // const a, + // //go:fix inline + // b = 1, 2 + // will go unnoticed. + // (They appear only in File.Comments, and it doesn't seem worthwhile to wade through those.) } } } - // Pass 2. Inline each static call to an inlinable function. + // Pass 2. Inline each static call to an inlinable function, + // and each reference to an inlinable constant. // // TODO(adonovan): handle multiple diffs that each add the same import. inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ (*ast.File)(nil), (*ast.CallExpr)(nil), + (*ast.Ident)(nil), } var currentFile *ast.File inspect.Preorder(nodeFilter, func(n ast.Node) { @@ -89,82 +137,139 @@ func run(pass *analysis.Pass) (any, error) { currentFile = file return } - call := n.(*ast.CallExpr) - if fn := typeutil.StaticCallee(pass.TypesInfo, call); fn != nil { - // Inlinable? - callee, ok := inlinable[fn] - if !ok { - var fact goFixInlineFact - if pass.ImportObjectFact(fn, &fact) { - callee = fact.Callee - inlinable[fn] = callee + switch n := n.(type) { + case *ast.CallExpr: + call := n + if fn := typeutil.StaticCallee(pass.TypesInfo, call); fn != nil { + // Inlinable? + callee, ok := inlinableFuncs[fn] + if !ok { + var fact goFixInlineFuncFact + if pass.ImportObjectFact(fn, &fact) { + callee = fact.Callee + inlinableFuncs[fn] = callee + } + } + if callee == nil { + return // nope } - } - if callee == nil { - return // nope - } - // Inline the call. - content, err := readFile(call) - if err != nil { - pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) - return - } - caller := &inline.Caller{ - Fset: pass.Fset, - Types: pass.Pkg, - Info: pass.TypesInfo, - File: currentFile, - Call: call, - Content: content, - } - res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) - if err != nil { - pass.Reportf(call.Lparen, "%v", err) - return - } - if res.Literalized { - // Users are not fond of inlinings that literalize - // f(x) to func() { ... }(), so avoid them. - // - // (Unfortunately the inliner is very timid, - // and often literalizes when it cannot prove that - // reducing the call is safe; the user of this tool - // has no indication of what the problem is.) - return + // Inline the call. + content, err := readFile(call) + if err != nil { + pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) + return + } + caller := &inline.Caller{ + Fset: pass.Fset, + Types: pass.Pkg, + Info: pass.TypesInfo, + File: currentFile, + Call: call, + Content: content, + } + res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) + if err != nil { + pass.Reportf(call.Lparen, "%v", err) + return + } + if res.Literalized { + // Users are not fond of inlinings that literalize + // f(x) to func() { ... }(), so avoid them. + // + // (Unfortunately the inliner is very timid, + // and often literalizes when it cannot prove that + // reducing the call is safe; the user of this tool + // has no indication of what the problem is.) + return + } + got := res.Content + + // Suggest the "fix". + var textEdits []analysis.TextEdit + for _, edit := range diff.Bytes(content, got) { + textEdits = append(textEdits, analysis.TextEdit{ + Pos: currentFile.FileStart + token.Pos(edit.Start), + End: currentFile.FileStart + token.Pos(edit.End), + NewText: []byte(edit.New), + }) + } + pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Message: fmt.Sprintf("Call of %v should be inlined", callee), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Inline call of %v", callee), + TextEdits: textEdits, + }}, + }) } - got := res.Content - - // Suggest the "fix". - var textEdits []analysis.TextEdit - for _, edit := range diff.Bytes(content, got) { - textEdits = append(textEdits, analysis.TextEdit{ - Pos: currentFile.FileStart + token.Pos(edit.Start), - End: currentFile.FileStart + token.Pos(edit.End), - NewText: []byte(edit.New), + + // TODO(jba): case *ast.SelectorExpr for RHSs that are qualified uses of constants. + + case *ast.Ident: + // If the identifier is a use of an inlinable constant, suggest inlining it. + if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { + incon, ok := inlinableConsts[con] + if !ok { + // TODO(jba): call ImportObjectFact. + var fact goFixInlineConstFact + if pass.ImportObjectFact(con, &fact) { + incon = &fact + inlinableConsts[con] = incon + } + } + if incon == nil { + return // nope + } + // We have an identifier A here (n), + // and an inlinable "const A = B" elsewhere (incon). + // Suggest replacing A with B. + importPrefix := "" + if incon.RHSPkgPath != con.Pkg().Path() { + importID := maybeAddImportPath(currentFile, incon.RHSPkgPath) + importPrefix = importID + "." + } + newText := importPrefix + incon.RHSName + pass.Report(analysis.Diagnostic{ + Pos: n.Pos(), + End: n.End(), + Message: fmt.Sprintf("Constant %s should be inlined", n.Name), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Inline constant %s", n.Name), + TextEdits: []analysis.TextEdit{{ + Pos: n.Pos(), + End: n.End(), + NewText: []byte(newText), + }}, + }}, }) } - msg := fmt.Sprintf("inline call of %v", callee) - pass.Report(analysis.Diagnostic{ - Pos: call.Pos(), - End: call.End(), - Message: msg, - SuggestedFixes: []analysis.SuggestedFix{{ - Message: msg, - TextEdits: textEdits, - }}, - }) } }) return nil, nil } -// A goFixInlineFact is exported for each function marked "//go:fix inline". +// hasInlineDirective reports whether cg has a directive +// of the form "//go:fix inline". +func hasInlineDirective(cg *ast.CommentGroup) bool { + return slices.ContainsFunc(directives(cg), func(d *directive) bool { + return d.Tool == "go" && d.Name == "fix" && d.Args == "inline" + }) +} + +func maybeAddImportPath(f *ast.File, path string) string { + // TODO(jba): implement this in terms of existing functions. + // TODO(adonovan): tell jba which functions. + return "unimp" +} + +// A goFixInlineFuncFact is exported for each function marked "//go:fix inline". // It holds information about the callee to support inlining. -type goFixInlineFact struct{ Callee *inline.Callee } +type goFixInlineFuncFact struct{ Callee *inline.Callee } -func (f *goFixInlineFact) String() string { return "goFixInline " + f.Callee.String() } -func (*goFixInlineFact) AFact() {} +func (f *goFixInlineFuncFact) String() string { return "goFixInline " + f.Callee.String() } +func (*goFixInlineFuncFact) AFact() {} func discard(string, ...any) {} diff --git a/internal/refactor/inline/analyzer/const.go b/internal/refactor/inline/analyzer/const.go new file mode 100644 index 00000000000..eba61bb7672 --- /dev/null +++ b/internal/refactor/inline/analyzer/const.go @@ -0,0 +1,21 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package analyzer + +import "fmt" + +// A goFixInlineConstFact is exported for each constant marked "//go:fix inline". +// It holds information about an inlinable constant. Gob-serializable. +type goFixInlineConstFact struct { + // Information about "const LHSName = RHSName". + RHSName string + RHSPkgPath string +} + +func (c *goFixInlineConstFact) String() string { + return fmt.Sprintf("goFixInline const %q.%s", c.RHSPkgPath, c.RHSName) +} + +func (*goFixInlineConstFact) AFact() {} diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/internal/refactor/inline/analyzer/testdata/src/a/a.go index 6e159a36894..20e1c1ee78b 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go @@ -1,9 +1,11 @@ package a +// Functions. + func f() { - One() // want `inline call of a.One` + One() // want `Call of a.One should be inlined` - new(T).Two() // want `inline call of \(a.T\).Two` + new(T).Two() // want `Call of \(a.T\).Two should be inlined` } type T struct{} @@ -15,3 +17,52 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` + +// Constants. + +//go:fix inline +const in1 = one // want in1: `goFixInline const "a".one` + +const ( + no1 = one + + //go:fix inline + in2 = one // want in2: `goFixInline const "a".one` +) + +//go:fix inline +const ( + in3 = one // want in3: `goFixInline const "a".one` + in4 = one // want in4: `goFixInline const "a".one` + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` +) + +//go:fix inline +const in5, // want in5: `goFixInline const "a".one` + in6, // want in6: `goFixInline const "a".one` + bad2 = one, one, + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +// Make sure we don't crash on iota consts, but still process the whole decl. +// +//go:fix inline +const ( + a = iota + b + in7 = one // want in7: `goFixInline const "a".one` +) + +func _() { + x := in1 // want `Constant in1 should be inlined` + x = in2 // want `Constant in2 should be inlined` + x = in3 // want `Constant in3 should be inlined` + x = in4 // want `Constant in4 should be inlined` + x = in5 // want `Constant in5 should be inlined` + x = in6 // want `Constant in6 should be inlined` + x = in7 // want `Constant in7 should be inlined` + x = no1 + _ = x + + in1 := 1 // don't inline lvalues + _ = in1 +} diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden index ea94f3b0175..0a6d2408073 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden @@ -1,9 +1,11 @@ package a +// Functions. + func f() { - _ = one // want `inline call of a.One` + _ = one // want `Call of a.One should be inlined` - _ = 2 // want `inline call of \(a.T\).Two` + _ = 2 // want `Call of \(a.T\).Two should be inlined` } type T struct{} @@ -15,3 +17,52 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` + +// Constants. + +//go:fix inline +const in1 = one // want in1: `goFixInline const "a".one` + +const ( + no1 = one + + //go:fix inline + in2 = one // want in2: `goFixInline const "a".one` +) + +//go:fix inline +const ( + in3 = one // want in3: `goFixInline const "a".one` + in4 = one // want in4: `goFixInline const "a".one` + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` +) + +//go:fix inline +const in5, // want in5: `goFixInline const "a".one` + in6, // want in6: `goFixInline const "a".one` + bad2 = one, one, + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +// Make sure we don't crash on iota consts, but still process the whole decl. +// +//go:fix inline +const ( + a = iota + b + in7 = one // want in7: `goFixInline const "a".one` +) + +func _() { + x := one // want `Constant in1 should be inlined` + x = one // want `Constant in2 should be inlined` + x = one // want `Constant in3 should be inlined` + x = one // want `Constant in4 should be inlined` + x = one // want `Constant in5 should be inlined` + x = one // want `Constant in6 should be inlined` + x = one // want `Constant in7 should be inlined` + x = no1 + _ = x + + in1 := 1 // don't inline lvalues + _ = in1 +} diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go b/internal/refactor/inline/analyzer/testdata/src/b/b.go index 069e670d51e..ab3cd2063e2 100644 --- a/internal/refactor/inline/analyzer/testdata/src/b/b.go +++ b/internal/refactor/inline/analyzer/testdata/src/b/b.go @@ -5,5 +5,5 @@ import "a" func f() { a.One() // want `cannot inline call to a.One because body refers to non-exported one` - new(a.T).Two() // want `inline call of \(a.T\).Two` + new(a.T).Two() // want `Call of \(a.T\).Two should be inlined` } diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden b/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden index b871b4b5100..f2099efdfeb 100644 --- a/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden @@ -5,5 +5,5 @@ import "a" func f() { a.One() // want `cannot inline call to a.One because body refers to non-exported one` - _ = 2 // want `inline call of \(a.T\).Two` + _ = 2 // want `Call of \(a.T\).Two should be inlined` } From 0abda08b17f5f477061eabd8a0950d2090dddfa8 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 31 Jan 2025 18:14:14 -0500 Subject: [PATCH 092/309] internal/refactor/inline/analyzer: export only cross-package facts Export only those constant facts that can be used on other packages: the constant to be inlined must begin with an uppercase letter and live at package scope. For golang/go#32816. Change-Id: I5da443cab65cadd90f557fa6d1892d85b83e2f2c Reviewed-on: https://go-review.googlesource.com/c/tools/+/645955 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/analyzer/analyzer.go | 8 ++++++- .../inline/analyzer/testdata/src/a/a.go | 22 ++++++++++--------- .../analyzer/testdata/src/a/a.go.golden | 22 ++++++++++--------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index a0d3bc9f530..97bb43ebb3d 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -17,6 +17,7 @@ import ( "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/refactor/inline" + "golang.org/x/tools/internal/typesinternal" ) const Doc = `inline calls to functions with "//go:fix inline" doc comment` @@ -106,8 +107,13 @@ func run(pass *analysis.Pass) (any, error) { RHSName: rhs.Name(), RHSPkgPath: rhs.Pkg().Path(), } - pass.ExportObjectFact(lhs, con) inlinableConsts[lhs] = con + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn about uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + pass.ExportObjectFact(lhs, con) + } } } } diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/internal/refactor/inline/analyzer/testdata/src/a/a.go index 20e1c1ee78b..2e3843ebe35 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go @@ -20,26 +20,28 @@ func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` // Constants. +const Uno = 1 + //go:fix inline -const in1 = one // want in1: `goFixInline const "a".one` +const In1 = Uno // want In1: `goFixInline const "a".Uno` const ( no1 = one //go:fix inline - in2 = one // want in2: `goFixInline const "a".one` + In2 = one // want In2: `goFixInline const "a".one` ) //go:fix inline const ( - in3 = one // want in3: `goFixInline const "a".one` - in4 = one // want in4: `goFixInline const "a".one` - bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + in3 = one + in4 = one + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` ) //go:fix inline -const in5, // want in5: `goFixInline const "a".one` - in6, // want in6: `goFixInline const "a".one` +const in5, + in6, bad2 = one, one, one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` @@ -49,12 +51,12 @@ const in5, // want in5: `goFixInline const "a".one` const ( a = iota b - in7 = one // want in7: `goFixInline const "a".one` + in7 = one ) func _() { - x := in1 // want `Constant in1 should be inlined` - x = in2 // want `Constant in2 should be inlined` + x := In1 // want `Constant In1 should be inlined` + x = In2 // want `Constant In2 should be inlined` x = in3 // want `Constant in3 should be inlined` x = in4 // want `Constant in4 should be inlined` x = in5 // want `Constant in5 should be inlined` diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden index 0a6d2408073..ea38dd022db 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden @@ -20,26 +20,28 @@ func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` // Constants. +const Uno = 1 + //go:fix inline -const in1 = one // want in1: `goFixInline const "a".one` +const In1 = Uno // want In1: `goFixInline const "a".Uno` const ( no1 = one //go:fix inline - in2 = one // want in2: `goFixInline const "a".one` + In2 = one // want In2: `goFixInline const "a".one` ) //go:fix inline const ( - in3 = one // want in3: `goFixInline const "a".one` - in4 = one // want in4: `goFixInline const "a".one` - bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + in3 = one + in4 = one + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` ) //go:fix inline -const in5, // want in5: `goFixInline const "a".one` - in6, // want in6: `goFixInline const "a".one` +const in5, + in6, bad2 = one, one, one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` @@ -49,12 +51,12 @@ const in5, // want in5: `goFixInline const "a".one` const ( a = iota b - in7 = one // want in7: `goFixInline const "a".one` + in7 = one ) func _() { - x := one // want `Constant in1 should be inlined` - x = one // want `Constant in2 should be inlined` + x := Uno // want `Constant In1 should be inlined` + x = one // want `Constant In2 should be inlined` x = one // want `Constant in3 should be inlined` x = one // want `Constant in4 should be inlined` x = one // want `Constant in5 should be inlined` From f912a4f263d5f6f973b2b212ad38b37eaad8fdee Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 30 Jan 2025 15:14:37 -0500 Subject: [PATCH 093/309] internal/refactor/inline/analyzer: inline consts into local scopes Warn about inline directives on constants defined to be the predeclared iota. Allow inlining when iota is redefined to be an ordinary identifier. Do not inline if the inlined value refers to a different object. For golang/go#32816. Change-Id: I890b3c7dab595bb6d27ada459db5b91e2364ee13 Reviewed-on: https://go-review.googlesource.com/c/tools/+/646255 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/analyzer/analyzer.go | 174 +++++++++++------- internal/refactor/inline/analyzer/const.go | 21 --- .../inline/analyzer/testdata/src/a/a.go | 30 ++- .../analyzer/testdata/src/a/a.go.golden | 30 ++- 4 files changed, 162 insertions(+), 93 deletions(-) delete mode 100644 internal/refactor/inline/analyzer/const.go diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index 97bb43ebb3d..706033d021b 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -53,86 +53,85 @@ func run(pass *analysis.Pass) (any, error) { // and export a fact for each one. inlinableFuncs := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) inlinableConsts := make(map[*types.Const]*goFixInlineConstFact) - for _, file := range pass.Files { - for _, decl := range file.Decls { - switch decl := decl.(type) { - case *ast.FuncDecl: - if hasInlineDirective(decl.Doc) { - content, err := readFile(decl) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) - continue - } - callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) - continue - } - fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) - pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) - inlinableFuncs[fn] = callee - } - case *ast.GenDecl: - if decl.Tok != token.CONST { - continue + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)} + inspect.Preorder(nodeFilter, func(n ast.Node) { + switch decl := n.(type) { + case *ast.FuncDecl: + if hasInlineDirective(decl.Doc) { + content, err := readFile(decl) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + return } - // Accept inline directives on the entire decl as well as individual specs. - declInline := hasInlineDirective(decl.Doc) - for _, spec := range decl.Specs { - spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST - if declInline || hasInlineDirective(spec.Doc) { - for i, name := range spec.Names { - if i >= len(spec.Values) { - // Possible following an iota. - break - } - val := spec.Values[i] - var rhsID *ast.Ident - switch e := val.(type) { - case *ast.Ident: - if e.Name == "iota" { - continue - } - rhsID = e - case *ast.SelectorExpr: - rhsID = e.Sel - default: - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) + return + } + fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) + pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) + inlinableFuncs[fn] = callee + } + + case *ast.GenDecl: + if decl.Tok != token.CONST { + return + } + // Accept inline directives on the entire decl as well as individual specs. + declInline := hasInlineDirective(decl.Doc) + for _, spec := range decl.Specs { + spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST + if declInline || hasInlineDirective(spec.Doc) { + for i, name := range spec.Names { + if i >= len(spec.Values) { + // Possible following an iota. + break + } + val := spec.Values[i] + var rhsID *ast.Ident + switch e := val.(type) { + case *ast.Ident: + // Constants defined with the predeclared iota cannot be inlined. + if pass.TypesInfo.Uses[e] == builtinIota { + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") continue } - lhs := pass.TypesInfo.Defs[name].(*types.Const) - rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program - con := &goFixInlineConstFact{ - RHSName: rhs.Name(), - RHSPkgPath: rhs.Pkg().Path(), - } - inlinableConsts[lhs] = con - // Create a fact only if the LHS is exported and defined at top level. - // We create a fact even if the RHS is non-exported, - // so we can warn about uses in other packages. - if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - pass.ExportObjectFact(lhs, con) - } + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + continue + } + lhs := pass.TypesInfo.Defs[name].(*types.Const) + rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program + con := &goFixInlineConstFact{ + RHSName: rhs.Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + if rhs.Pkg() == pass.Pkg { + con.rhsObj = rhs + } + inlinableConsts[lhs] = con + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + pass.ExportObjectFact(lhs, con) } } } - // TODO(jba): in user doc, warn that a comments within a spec, as in - // const a, - // //go:fix inline - // b = 1, 2 - // will go unnoticed. - // (They appear only in File.Comments, and it doesn't seem worthwhile to wade through those.) } } - } + }) // Pass 2. Inline each static call to an inlinable function, // and each reference to an inlinable constant. // // TODO(adonovan): handle multiple diffs that each add the same import. - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - nodeFilter := []ast.Node{ + nodeFilter = []ast.Node{ (*ast.File)(nil), (*ast.CallExpr)(nil), (*ast.Ident)(nil), @@ -218,7 +217,6 @@ func run(pass *analysis.Pass) (any, error) { if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { incon, ok := inlinableConsts[con] if !ok { - // TODO(jba): call ImportObjectFact. var fact goFixInlineConstFact if pass.ImportObjectFact(con, &fact) { incon = &fact @@ -228,9 +226,29 @@ func run(pass *analysis.Pass) (any, error) { if incon == nil { return // nope } + // // We have an identifier A here (n), // and an inlinable "const A = B" elsewhere (incon). - // Suggest replacing A with B. + // Consider replacing A with B. + // Check that the expression we are inlining (B) means the same thing + // (refers to the same object) in n's scope as it does in A's scope. + if incon.rhsObj != nil { + // Both expressions are in the current package. + // incon.rhsObj is the object referred to by B in the definition of A. + scope := pass.TypesInfo.Scopes[currentFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope + if obj == nil { + // Should be impossible: if code at n can refer to the LHS, + // it can refer to the RHS. + panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName)) + } + if obj != incon.rhsObj { + // "B" means something different here than at the inlinable const's scope + return + } + } else { + // TODO(jba): handle the cross-package case by checking the package ID. + } importPrefix := "" if incon.RHSPkgPath != con.Pkg().Path() { importID := maybeAddImportPath(currentFile, incon.RHSPkgPath) @@ -266,8 +284,7 @@ func hasInlineDirective(cg *ast.CommentGroup) bool { } func maybeAddImportPath(f *ast.File, path string) string { - // TODO(jba): implement this in terms of existing functions. - // TODO(adonovan): tell jba which functions. + // TODO(jba): implement this in terms of analysisinternal.AddImport(info, file, pos, path, localname). return "unimp" } @@ -278,4 +295,21 @@ type goFixInlineFuncFact struct{ Callee *inline.Callee } func (f *goFixInlineFuncFact) String() string { return "goFixInline " + f.Callee.String() } func (*goFixInlineFuncFact) AFact() {} +// A goFixInlineConstFact is exported for each constant marked "//go:fix inline". +// It holds information about an inlinable constant. Gob-serializable. +type goFixInlineConstFact struct { + // Information about "const LHSName = RHSName". + RHSName string + RHSPkgPath string + rhsObj types.Object // for current package +} + +func (c *goFixInlineConstFact) String() string { + return fmt.Sprintf("goFixInline const %q.%s", c.RHSPkgPath, c.RHSName) +} + +func (*goFixInlineConstFact) AFact() {} + func discard(string, ...any) {} + +var builtinIota = types.Universe.Lookup("iota") diff --git a/internal/refactor/inline/analyzer/const.go b/internal/refactor/inline/analyzer/const.go deleted file mode 100644 index eba61bb7672..00000000000 --- a/internal/refactor/inline/analyzer/const.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2025 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package analyzer - -import "fmt" - -// A goFixInlineConstFact is exported for each constant marked "//go:fix inline". -// It holds information about an inlinable constant. Gob-serializable. -type goFixInlineConstFact struct { - // Information about "const LHSName = RHSName". - RHSName string - RHSPkgPath string -} - -func (c *goFixInlineConstFact) String() string { - return fmt.Sprintf("goFixInline const %q.%s", c.RHSPkgPath, c.RHSName) -} - -func (*goFixInlineConstFact) AFact() {} diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/internal/refactor/inline/analyzer/testdata/src/a/a.go index 2e3843ebe35..ae486746e5b 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go @@ -49,7 +49,7 @@ const in5, // //go:fix inline const ( - a = iota + a = iota // want `invalid //go:fix inline directive: const value is iota` b in7 = one ) @@ -68,3 +68,31 @@ func _() { in1 := 1 // don't inline lvalues _ = in1 } + +const ( + x = 1 + //go:fix inline + in8 = x +) + +func shadow() { + var x int // shadows x at package scope + + //go:fix inline + const a = iota // want `invalid //go:fix inline directive: const value is iota` + + const iota = 2 + // Below this point, iota is an ordinary constant. + + //go:fix inline + const b = iota + + x = a // a is defined with the predeclared iota, so it cannot be inlined + x = b // want `Constant b should be inlined` + + // Don't offer to inline in8, because the result, "x", would mean something different + // in this scope than it does in the scope where in8 is defined. + x = in8 + + _ = x +} diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden index ea38dd022db..7d75a598fb7 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden @@ -49,7 +49,7 @@ const in5, // //go:fix inline const ( - a = iota + a = iota // want `invalid //go:fix inline directive: const value is iota` b in7 = one ) @@ -68,3 +68,31 @@ func _() { in1 := 1 // don't inline lvalues _ = in1 } + +const ( + x = 1 + //go:fix inline + in8 = x +) + +func shadow() { + var x int // shadows x at package scope + + //go:fix inline + const a = iota // want `invalid //go:fix inline directive: const value is iota` + + const iota = 2 + // Below this point, iota is an ordinary constant. + + //go:fix inline + const b = iota + + x = a // a is defined with the predeclared iota, so it cannot be inlined + x = iota // want `Constant b should be inlined` + + // Don't offer to inline in8, because the result, "x", would mean something different + // in this scope than it does in the scope where in8 is defined. + x = in8 + + _ = x +} From e9f7be9f13468661413bba805228434c534e6dbc Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 15 Jan 2025 12:29:12 -0500 Subject: [PATCH 094/309] internal/astutil/cursor: add Cursor.Child(Node) Cursor This method returns the cursor for a direct child, more efficiently than FindNode. Also, add edge.Kind.Get method. + tests Change-Id: I1176ac55713ef0c06b02a1e3a9c64f530caa9a09 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642936 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan --- internal/astutil/cursor/cursor.go | 29 ++++++++++++++++++++++++++ internal/astutil/cursor/cursor_test.go | 11 ++++++++++ internal/astutil/edge/edge.go | 25 ++++++++++++++++++---- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 38a35f64ce0..1052f65acfb 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -15,6 +15,7 @@ package cursor import ( + "fmt" "go/ast" "go/token" "iter" @@ -227,6 +228,34 @@ func (c Cursor) Edge() (edge.Kind, int) { return unpackEdgeKindAndIndex(events[pop].parent) } +// Child returns the cursor for n, which must be a direct child of c's Node. +// +// Child must not be called on the Root node (whose [Cursor.Node] returns nil). +func (c Cursor) Child(n ast.Node) Cursor { + if c.index < 0 { + panic("Cursor.Child called on Root node") + } + + if false { + // reference implementation + for child := range c.Children() { + if child.Node() == n { + return child + } + } + + } else { + // optimized implementation + events := c.events() + for i := c.index + 1; events[i].index > i; i = events[i].index + 1 { + if events[i].node == n { + return Cursor{c.in, i} + } + } + } + panic(fmt.Sprintf("Child(%T): not a child of %v", n, c)) +} + // NextSibling returns the cursor for the next sibling node in the same list // (for example, of files, decls, specs, statements, fields, or expressions) as // the current node. It returns (zero, false) if the node is the last node in diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index e04f8c24b89..01f791f2833 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -360,6 +360,12 @@ func TestCursor_Edge(t *testing.T) { e.NodeType(), parent.Node()) } + // Check consistency of c.Edge.Get(c.Parent().Node()) == c.Node(). + if got := e.Get(parent.Node(), idx); got != cur.Node() { + t.Errorf("cur=%v@%s: %s.Get(cur.Parent().Node(), %d) = %T@%s, want cur.Node()", + cur, netFset.Position(cur.Node().Pos()), e, idx, got, netFset.Position(got.Pos())) + } + // Check that reflection on the parent finds the current node. fv := reflect.ValueOf(parent.Node()).Elem().FieldByName(e.FieldName()) if idx >= 0 { @@ -373,6 +379,11 @@ func TestCursor_Edge(t *testing.T) { t.Errorf("%v.Edge = (%v, %d); FieldName/Index reflection gave %T@%s, not original node", cur, e, idx, got, netFset.Position(got.Pos())) } + + // Check that Cursor.Child is the reverse of Parent. + if cur.Parent().Child(cur.Node()) != cur { + t.Errorf("Cursor.Parent.Child = %v, want %v", cur.Parent().Child(cur.Node()), cur) + } } } diff --git a/internal/astutil/edge/edge.go b/internal/astutil/edge/edge.go index bf945a8f632..4f6ccfd6e5e 100644 --- a/internal/astutil/edge/edge.go +++ b/internal/astutil/edge/edge.go @@ -21,18 +21,34 @@ func (k Kind) String() string { return "" } info := fieldInfos[k] - return fmt.Sprintf("%v.%s", info.nodeType.Elem().Name(), info.fieldName) + return fmt.Sprintf("%v.%s", info.nodeType.Elem().Name(), info.name) } // NodeType returns the pointer-to-struct type of the ast.Node implementation. func (k Kind) NodeType() reflect.Type { return fieldInfos[k].nodeType } // FieldName returns the name of the field. -func (k Kind) FieldName() string { return fieldInfos[k].fieldName } +func (k Kind) FieldName() string { return fieldInfos[k].name } // FieldType returns the declared type of the field. func (k Kind) FieldType() reflect.Type { return fieldInfos[k].fieldType } +// Get returns the direct child of n identified by (k, idx). +// n's type must match k.NodeType(). +// idx must be a valid slice index, or -1 for a non-slice. +func (k Kind) Get(n ast.Node, idx int) ast.Node { + if k.NodeType() != reflect.TypeOf(n) { + panic(fmt.Sprintf("%v.Get(%T): invalid node type", k, n)) + } + v := reflect.ValueOf(n).Elem().Field(fieldInfos[k].index) + if idx != -1 { + v = v.Index(idx) // asserts valid index + } else { + // (The type assertion below asserts that v is not a slice.) + } + return v.Interface().(ast.Node) // may be nil +} + const ( Invalid Kind = iota // for nodes at the root of the traversal @@ -156,7 +172,8 @@ var _ = [1 << 7]struct{}{}[maxKind] type fieldInfo struct { nodeType reflect.Type // pointer-to-struct type of ast.Node implementation - fieldName string + name string + index int fieldType reflect.Type } @@ -166,7 +183,7 @@ func info[N ast.Node](fieldName string) fieldInfo { if !ok { panic(fieldName) } - return fieldInfo{nodePtrType, fieldName, f.Type} + return fieldInfo{nodePtrType, fieldName, f.Index[0], f.Type} } var fieldInfos = [...]fieldInfo{ From 0556adba16994a5771cab47720c75a3826bcc266 Mon Sep 17 00:00:00 2001 From: Sam Salisbury Date: Thu, 30 Jan 2025 08:26:02 +0000 Subject: [PATCH 095/309] gopls: skip unusedparams for generated files The unusedparams analyzer makes a lot of noise for some generated files, notably those generated by protoc-gen-go. Since we can't actually edit generated files to fix this issue, it seems better not to report it. Fixes golang/go#71481 Change-Id: I4e73c74312dfea6ef4ce631cc029764519d6b809 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645575 Reviewed-by: Sam Salisbury LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley Reviewed-by: Robert Findley Reviewed-by: Hongxiang Jiang --- gopls/doc/analyzers.md | 2 + gopls/internal/analysis/unusedparams/doc.go | 2 + .../src/generatedcode/generatedcode.go | 15 + .../src/generatedcode/generatedcode.go.golden | 15 + .../src/generatedcode/nongeneratedcode.go | 20 ++ .../generatedcode/nongeneratedcode.go.golden | 20 ++ .../analysis/unusedparams/unusedparams.go | 295 ++++++++++-------- .../unusedparams/unusedparams_test.go | 2 +- gopls/internal/doc/api.json | 4 +- 9 files changed, 235 insertions(+), 140 deletions(-) create mode 100644 gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go create mode 100644 gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go.golden create mode 100644 gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go create mode 100644 gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go.golden diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 282a9f436c6..27491520b3c 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -1000,6 +1000,8 @@ arguments at call sites, while taking care to preserve any side effects in the argument expressions; see https://github.com/golang/tools/releases/tag/gopls%2Fv0.14. +This analyzer ignores generated code. + Default: on. Package documentation: [unusedparams](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedparams) diff --git a/gopls/internal/analysis/unusedparams/doc.go b/gopls/internal/analysis/unusedparams/doc.go index 07e43c0d084..16d318e86fa 100644 --- a/gopls/internal/analysis/unusedparams/doc.go +++ b/gopls/internal/analysis/unusedparams/doc.go @@ -31,4 +31,6 @@ // arguments at call sites, while taking care to preserve any side // effects in the argument expressions; see // https://github.com/golang/tools/releases/tag/gopls%2Fv0.14. +// +// This analyzer ignores generated code. package unusedparams diff --git a/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go new file mode 100644 index 00000000000..fdbe64d9e90 --- /dev/null +++ b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go @@ -0,0 +1,15 @@ +// Code generated with somegen DO NOT EDIT. +// +// Because this file is generated, there should be no diagnostics +// reported for any unused parameters. + +package generatedcode + +// generatedInterface exists to ensure that the generated code +// is considered when determining whether parameters are used +// in non-generated code. +type generatedInterface interface{ n(f bool) } + +func a(x bool) { println() } + +var v = func(x bool) { println() } diff --git a/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go.golden b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go.golden new file mode 100644 index 00000000000..fdbe64d9e90 --- /dev/null +++ b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/generatedcode.go.golden @@ -0,0 +1,15 @@ +// Code generated with somegen DO NOT EDIT. +// +// Because this file is generated, there should be no diagnostics +// reported for any unused parameters. + +package generatedcode + +// generatedInterface exists to ensure that the generated code +// is considered when determining whether parameters are used +// in non-generated code. +type generatedInterface interface{ n(f bool) } + +func a(x bool) { println() } + +var v = func(x bool) { println() } diff --git a/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go new file mode 100644 index 00000000000..fe0ef94afbb --- /dev/null +++ b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go @@ -0,0 +1,20 @@ +package generatedcode + +// This file does not have the generated code comment. +// It exists to ensure that generated code is considered +// when determining whether or not function parameters +// are used. + +type implementsGeneratedInterface struct{} + +// The f parameter should not be reported as unused, +// because this method implements the parent interface defined +// in the generated code. +func (implementsGeneratedInterface) n(f bool) { + // The body must not be empty, otherwise unusedparams will + // not report the unused parameter regardles of the + // interface. + println() +} + +func b(x bool) { println() } // want "unused parameter: x" diff --git a/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go.golden b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go.golden new file mode 100644 index 00000000000..170dc85785c --- /dev/null +++ b/gopls/internal/analysis/unusedparams/testdata/src/generatedcode/nongeneratedcode.go.golden @@ -0,0 +1,20 @@ +package generatedcode + +// This file does not have the generated code comment. +// It exists to ensure that generated code is considered +// when determining whether or not function parameters +// are used. + +type implementsGeneratedInterface struct{} + +// The f parameter should not be reported as unused, +// because this method implements the parent interface defined +// in the generated code. +func (implementsGeneratedInterface) n(f bool) { + // The body must not be empty, otherwise unusedparams will + // not report the unused parameter regardles of the + // interface. + println() +} + +func b(_ bool) { println() } // want "unused parameter: x" diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index ff7fdc4418c..2986dfd6e41 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -139,172 +139,193 @@ func run(pass *analysis.Pass) (any, error) { } } - // Check each non-address-taken function's parameters are all used. - filter := []ast.Node{ - (*ast.FuncDecl)(nil), - (*ast.FuncLit)(nil), - } - cursor.Root(inspect).Inspect(filter, func(c cursor.Cursor, push bool) bool { - // (We always return true so that we visit nested FuncLits.) - + // Inspect each file to see if it is generated. + // + // We do not want to report unused parameters in generated code itself, + // however we need to include generated code in the overall analysis as + // it may be calling functions in non-generated code. + files := []ast.Node{(*ast.File)(nil)} + cursor.Root(inspect).Inspect(files, func(c cursor.Cursor, push bool) bool { if !push { return true } - var ( - fn types.Object // function symbol (*Func, possibly *Var for a FuncLit) - ftype *ast.FuncType - body *ast.BlockStmt - ) - switch n := c.Node().(type) { - case *ast.FuncDecl: - // We can't analyze non-Go functions. - if n.Body == nil { - return true - } + isGenerated := ast.IsGenerated(c.Node().(*ast.File)) - // Ignore exported functions and methods: we - // must assume they may be address-taken in - // another package. - if n.Name.IsExported() { + // Descend into the file, check each non-address-taken function's parameters + // are all used. + funcs := []ast.Node{ + (*ast.FuncDecl)(nil), + (*ast.FuncLit)(nil), + } + c.Inspect(funcs, func(c cursor.Cursor, push bool) bool { + // (We always return true so that we visit nested FuncLits.) + if !push { return true } - // Ignore methods that match the name of any - // interface method declared in this package, - // as the method's signature may need to conform - // to the interface. - if n.Recv != nil && unexportedIMethodNames[n.Name.Name] { - return true - } + var ( + fn types.Object // function symbol (*Func, possibly *Var for a FuncLit) + ftype *ast.FuncType + body *ast.BlockStmt + ) + switch n := c.Node().(type) { + case *ast.FuncDecl: + // We can't analyze non-Go functions. + if n.Body == nil { + return true + } - fn = pass.TypesInfo.Defs[n.Name].(*types.Func) - ftype, body = n.Type, n.Body - - case *ast.FuncLit: - // Find the symbol for the variable (if any) - // to which the FuncLit is bound. - // (We don't bother to allow ParenExprs.) - switch parent := c.Parent().Node().(type) { - case *ast.AssignStmt: - // f = func() {...} - // f := func() {...} - if e, idx := c.Edge(); e == edge.AssignStmt_Rhs { - // Inv: n == AssignStmt.Rhs[idx] - if id, ok := parent.Lhs[idx].(*ast.Ident); ok { - fn = pass.TypesInfo.ObjectOf(id) - - // Edge case: f = func() {...} - // should not count as a use. - if pass.TypesInfo.Uses[id] != nil { - usesOutsideCall[fn] = moreslices.Remove(usesOutsideCall[fn], id) - } + // Ignore exported functions and methods: we + // must assume they may be address-taken in + // another package. + if n.Name.IsExported() { + return true + } - if fn == nil && id.Name == "_" { - // Edge case: _ = func() {...} - // has no local var. Fake one. - v := types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) - typesinternal.SetVarKind(v, typesinternal.LocalVar) - fn = v + // Ignore methods that match the name of any + // interface method declared in this package, + // as the method's signature may need to conform + // to the interface. + if n.Recv != nil && unexportedIMethodNames[n.Name.Name] { + return true + } + + fn = pass.TypesInfo.Defs[n.Name].(*types.Func) + ftype, body = n.Type, n.Body + + case *ast.FuncLit: + // Find the symbol for the variable (if any) + // to which the FuncLit is bound. + // (We don't bother to allow ParenExprs.) + switch parent := c.Parent().Node().(type) { + case *ast.AssignStmt: + // f = func() {...} + // f := func() {...} + if e, idx := c.Edge(); e == edge.AssignStmt_Rhs { + // Inv: n == AssignStmt.Rhs[idx] + if id, ok := parent.Lhs[idx].(*ast.Ident); ok { + fn = pass.TypesInfo.ObjectOf(id) + + // Edge case: f = func() {...} + // should not count as a use. + if pass.TypesInfo.Uses[id] != nil { + usesOutsideCall[fn] = moreslices.Remove(usesOutsideCall[fn], id) + } + + if fn == nil && id.Name == "_" { + // Edge case: _ = func() {...} + // has no local var. Fake one. + v := types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) + typesinternal.SetVarKind(v, typesinternal.LocalVar) + fn = v + } } } - } - case *ast.ValueSpec: - // var f = func() { ... } - // (unless f is an exported package-level var) - for i, val := range parent.Values { - if val == n { - v := pass.TypesInfo.Defs[parent.Names[i]] - if !(v.Parent() == pass.Pkg.Scope() && v.Exported()) { - fn = v + case *ast.ValueSpec: + // var f = func() { ... } + // (unless f is an exported package-level var) + for i, val := range parent.Values { + if val == n { + v := pass.TypesInfo.Defs[parent.Names[i]] + if !(v.Parent() == pass.Pkg.Scope() && v.Exported()) { + fn = v + } + break } - break } } - } - ftype, body = n.Type, n.Body - } + ftype, body = n.Type, n.Body + } - // Ignore address-taken functions and methods: unused - // parameters may be needed to conform to a func type. - if fn == nil || len(usesOutsideCall[fn]) > 0 { - return true - } + // Ignore address-taken functions and methods: unused + // parameters may be needed to conform to a func type. + if fn == nil || len(usesOutsideCall[fn]) > 0 { + return true + } - // If there are no parameters, there are no unused parameters. - if ftype.Params.NumFields() == 0 { - return true - } + // If there are no parameters, there are no unused parameters. + if ftype.Params.NumFields() == 0 { + return true + } - // To reduce false positives, ignore functions with an - // empty or panic body. - // - // We choose not to ignore functions whose body is a - // single return statement (as earlier versions did) - // func f() { return } - // func f() { return g(...) } - // as we suspect that was just heuristic to reduce - // false positives in the earlier unsound algorithm. - switch len(body.List) { - case 0: - // Empty body. Although the parameter is - // unnecessary, it's pretty obvious to the - // reader that that's the case, so we allow it. - return true // func f() {} - case 1: - if stmt, ok := body.List[0].(*ast.ExprStmt); ok { - // We allow a panic body, as it is often a - // placeholder for a future implementation: - // func f() { panic(...) } - if call, ok := stmt.X.(*ast.CallExpr); ok { - if fun, ok := call.Fun.(*ast.Ident); ok && fun.Name == "panic" { - return true + // To reduce false positives, ignore functions with an + // empty or panic body. + // + // We choose not to ignore functions whose body is a + // single return statement (as earlier versions did) + // func f() { return } + // func f() { return g(...) } + // as we suspect that was just heuristic to reduce + // false positives in the earlier unsound algorithm. + switch len(body.List) { + case 0: + // Empty body. Although the parameter is + // unnecessary, it's pretty obvious to the + // reader that that's the case, so we allow it. + return true // func f() {} + case 1: + if stmt, ok := body.List[0].(*ast.ExprStmt); ok { + // We allow a panic body, as it is often a + // placeholder for a future implementation: + // func f() { panic(...) } + if call, ok := stmt.X.(*ast.CallExpr); ok { + if fun, ok := call.Fun.(*ast.Ident); ok && fun.Name == "panic" { + return true + } } } } - } - // Report each unused parameter. - for _, field := range ftype.Params.List { - for _, id := range field.Names { - if id.Name == "_" { - continue - } - param := pass.TypesInfo.Defs[id].(*types.Var) - if !usedVars[param] { - start, end := field.Pos(), field.End() - if len(field.Names) > 1 { - start, end = id.Pos(), id.End() + // Don't report diagnostics on generated files. + if isGenerated { + return true + } + + // Report each unused parameter. + for _, field := range ftype.Params.List { + for _, id := range field.Names { + if id.Name == "_" { + continue } - // This diagnostic carries both an edit-based fix to - // rename the unused parameter, and a command-based fix - // to remove it (see golang.RemoveUnusedParameter). - pass.Report(analysis.Diagnostic{ - Pos: start, - End: end, - Message: fmt.Sprintf("unused parameter: %s", id.Name), - Category: FixCategory, - SuggestedFixes: []analysis.SuggestedFix{ - { - Message: `Rename parameter to "_"`, - TextEdits: []analysis.TextEdit{{ - Pos: id.Pos(), - End: id.End(), - NewText: []byte("_"), - }}, - }, - { - Message: fmt.Sprintf("Remove unused parameter %q", id.Name), - // No TextEdits => computed by gopls command + param := pass.TypesInfo.Defs[id].(*types.Var) + if !usedVars[param] { + start, end := field.Pos(), field.End() + if len(field.Names) > 1 { + start, end = id.Pos(), id.End() + } + + // This diagnostic carries both an edit-based fix to + // rename the unused parameter, and a command-based fix + // to remove it (see golang.RemoveUnusedParameter). + pass.Report(analysis.Diagnostic{ + Pos: start, + End: end, + Message: fmt.Sprintf("unused parameter: %s", id.Name), + Category: FixCategory, + SuggestedFixes: []analysis.SuggestedFix{ + { + Message: `Rename parameter to "_"`, + TextEdits: []analysis.TextEdit{{ + Pos: id.Pos(), + End: id.End(), + NewText: []byte("_"), + }}, + }, + { + Message: fmt.Sprintf("Remove unused parameter %q", id.Name), + // No TextEdits => computed by gopls command + }, }, - }, - }) + }) + } } } - } + return true + }) return true }) return nil, nil diff --git a/gopls/internal/analysis/unusedparams/unusedparams_test.go b/gopls/internal/analysis/unusedparams/unusedparams_test.go index 1e2d8851b8b..e943c20d898 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams_test.go +++ b/gopls/internal/analysis/unusedparams/unusedparams_test.go @@ -13,5 +13,5 @@ import ( func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.RunWithSuggestedFixes(t, testdata, unusedparams.Analyzer, "a", "typeparams") + analysistest.RunWithSuggestedFixes(t, testdata, unusedparams.Analyzer, "a", "generatedcode", "typeparams") } diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 83151ae6373..74a61599bf4 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -635,7 +635,7 @@ }, { "Name": "\"unusedparams\"", - "Doc": "check for unused parameters of functions\n\nThe unusedparams analyzer checks functions to see if there are\nany parameters that are not being used.\n\nTo ensure soundness, it ignores:\n - \"address-taken\" functions, that is, functions that are used as\n a value rather than being called directly; their signatures may\n be required to conform to a func type.\n - exported functions or methods, since they may be address-taken\n in another package.\n - unexported methods whose name matches an interface method\n declared in the same package, since the method's signature\n may be required to conform to the interface type.\n - functions with empty bodies, or containing just a call to panic.\n - parameters that are unnamed, or named \"_\", the blank identifier.\n\nThe analyzer suggests a fix of replacing the parameter name by \"_\",\nbut in such cases a deeper fix can be obtained by invoking the\n\"Refactor: remove unused parameter\" code action, which will\neliminate the parameter entirely, along with all corresponding\narguments at call sites, while taking care to preserve any side\neffects in the argument expressions; see\nhttps://github.com/golang/tools/releases/tag/gopls%2Fv0.14.", + "Doc": "check for unused parameters of functions\n\nThe unusedparams analyzer checks functions to see if there are\nany parameters that are not being used.\n\nTo ensure soundness, it ignores:\n - \"address-taken\" functions, that is, functions that are used as\n a value rather than being called directly; their signatures may\n be required to conform to a func type.\n - exported functions or methods, since they may be address-taken\n in another package.\n - unexported methods whose name matches an interface method\n declared in the same package, since the method's signature\n may be required to conform to the interface type.\n - functions with empty bodies, or containing just a call to panic.\n - parameters that are unnamed, or named \"_\", the blank identifier.\n\nThe analyzer suggests a fix of replacing the parameter name by \"_\",\nbut in such cases a deeper fix can be obtained by invoking the\n\"Refactor: remove unused parameter\" code action, which will\neliminate the parameter entirely, along with all corresponding\narguments at call sites, while taking care to preserve any side\neffects in the argument expressions; see\nhttps://github.com/golang/tools/releases/tag/gopls%2Fv0.14.\n\nThis analyzer ignores generated code.", "Default": "true" }, { @@ -1339,7 +1339,7 @@ }, { "Name": "unusedparams", - "Doc": "check for unused parameters of functions\n\nThe unusedparams analyzer checks functions to see if there are\nany parameters that are not being used.\n\nTo ensure soundness, it ignores:\n - \"address-taken\" functions, that is, functions that are used as\n a value rather than being called directly; their signatures may\n be required to conform to a func type.\n - exported functions or methods, since they may be address-taken\n in another package.\n - unexported methods whose name matches an interface method\n declared in the same package, since the method's signature\n may be required to conform to the interface type.\n - functions with empty bodies, or containing just a call to panic.\n - parameters that are unnamed, or named \"_\", the blank identifier.\n\nThe analyzer suggests a fix of replacing the parameter name by \"_\",\nbut in such cases a deeper fix can be obtained by invoking the\n\"Refactor: remove unused parameter\" code action, which will\neliminate the parameter entirely, along with all corresponding\narguments at call sites, while taking care to preserve any side\neffects in the argument expressions; see\nhttps://github.com/golang/tools/releases/tag/gopls%2Fv0.14.", + "Doc": "check for unused parameters of functions\n\nThe unusedparams analyzer checks functions to see if there are\nany parameters that are not being used.\n\nTo ensure soundness, it ignores:\n - \"address-taken\" functions, that is, functions that are used as\n a value rather than being called directly; their signatures may\n be required to conform to a func type.\n - exported functions or methods, since they may be address-taken\n in another package.\n - unexported methods whose name matches an interface method\n declared in the same package, since the method's signature\n may be required to conform to the interface type.\n - functions with empty bodies, or containing just a call to panic.\n - parameters that are unnamed, or named \"_\", the blank identifier.\n\nThe analyzer suggests a fix of replacing the parameter name by \"_\",\nbut in such cases a deeper fix can be obtained by invoking the\n\"Refactor: remove unused parameter\" code action, which will\neliminate the parameter entirely, along with all corresponding\narguments at call sites, while taking care to preserve any side\neffects in the argument expressions; see\nhttps://github.com/golang/tools/releases/tag/gopls%2Fv0.14.\n\nThis analyzer ignores generated code.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedparams", "Default": true }, From 70a7d863a1dd83178e9abfc238ed590bd0c6fabf Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 4 Feb 2025 08:44:53 -0500 Subject: [PATCH 096/309] internal/refactor/inline/analyzer: use forward for consts Per the proposal, change the directive for constants to //go:fix forward. For golang/go#32816. Change-Id: Ib4dc2d870ccc05544861ab8e9eeafde7728f25f1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/646455 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/analyzer/analyzer.go | 75 +++++++++++-------- .../refactor/inline/analyzer/directive.go | 5 ++ .../inline/analyzer/testdata/src/a/a.go | 56 +++++++------- .../analyzer/testdata/src/a/a.go.golden | 56 +++++++------- 4 files changed, 110 insertions(+), 82 deletions(-) diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index 706033d021b..9583b2fd9e6 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -20,6 +20,7 @@ import ( "golang.org/x/tools/internal/typesinternal" ) +// TODO(jba): replace with better doc. const Doc = `inline calls to functions with "//go:fix inline" doc comment` var Analyzer = &analysis.Analyzer{ @@ -27,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ Doc: Doc, URL: "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", Run: run, - FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixInlineConstFact)}, + FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixForwardConstFact)}, Requires: []*analysis.Analyzer{inspect.Analyzer}, } @@ -48,18 +49,18 @@ func run(pass *analysis.Pass) (any, error) { return content, nil } - // Pass 1: find functions and constants annotated with a "//go:fix inline" + // Pass 1: find functions and constants annotated with an appropriate "//go:fix" // comment (the syntax proposed by #32816), // and export a fact for each one. inlinableFuncs := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) - inlinableConsts := make(map[*types.Const]*goFixInlineConstFact) + forwardableConsts := make(map[*types.Const]*goFixForwardConstFact) inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)} inspect.Preorder(nodeFilter, func(n ast.Node) { switch decl := n.(type) { case *ast.FuncDecl: - if hasInlineDirective(decl.Doc) { + if hasFixDirective(decl.Doc, "inline") { content, err := readFile(decl) if err != nil { pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) @@ -73,17 +74,27 @@ func run(pass *analysis.Pass) (any, error) { fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) inlinableFuncs[fn] = callee + } else if hasFixDirective(decl.Doc, "forward") { + pass.Reportf(decl.Doc.Pos(), "use //go:fix inline for functions") } case *ast.GenDecl: if decl.Tok != token.CONST { return } - // Accept inline directives on the entire decl as well as individual specs. - declInline := hasInlineDirective(decl.Doc) + if hasFixDirective(decl.Doc, "inline") { + pass.Reportf(decl.Doc.Pos(), "use //go:fix forward for constants") + return + } + // Accept forward directives on the entire decl as well as individual specs. + declForward := hasFixDirective(decl.Doc, "forward") for _, spec := range decl.Specs { spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST - if declInline || hasInlineDirective(spec.Doc) { + if hasFixDirective(spec.Doc, "inline") { + pass.Reportf(spec.Doc.Pos(), "use //go:fix forward for constants") + return + } + if declForward || hasFixDirective(spec.Doc, "forward") { for i, name := range spec.Names { if i >= len(spec.Values) { // Possible following an iota. @@ -93,28 +104,28 @@ func run(pass *analysis.Pass) (any, error) { var rhsID *ast.Ident switch e := val.(type) { case *ast.Ident: - // Constants defined with the predeclared iota cannot be inlined. + // Constants defined with the predeclared iota cannot be forwarded. if pass.TypesInfo.Uses[e] == builtinIota { - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") + pass.Reportf(val.Pos(), "invalid //go:fix forward directive: const value is iota") continue } rhsID = e case *ast.SelectorExpr: rhsID = e.Sel default: - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + pass.Reportf(val.Pos(), "invalid //go:fix forward directive: const value is not the name of another constant") continue } lhs := pass.TypesInfo.Defs[name].(*types.Const) rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program - con := &goFixInlineConstFact{ + con := &goFixForwardConstFact{ RHSName: rhs.Name(), RHSPkgPath: rhs.Pkg().Path(), } if rhs.Pkg() == pass.Pkg { con.rhsObj = rhs } - inlinableConsts[lhs] = con + forwardableConsts[lhs] = con // Create a fact only if the LHS is exported and defined at top level. // We create a fact even if the RHS is non-exported, // so we can warn uses in other packages. @@ -128,7 +139,7 @@ func run(pass *analysis.Pass) (any, error) { }) // Pass 2. Inline each static call to an inlinable function, - // and each reference to an inlinable constant. + // and forward each reference to a forwardable constant. // // TODO(adonovan): handle multiple diffs that each add the same import. nodeFilter = []ast.Node{ @@ -213,14 +224,14 @@ func run(pass *analysis.Pass) (any, error) { // TODO(jba): case *ast.SelectorExpr for RHSs that are qualified uses of constants. case *ast.Ident: - // If the identifier is a use of an inlinable constant, suggest inlining it. + // If the identifier is a use of a forwardable constant, suggest forwarding it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { - incon, ok := inlinableConsts[con] + incon, ok := forwardableConsts[con] if !ok { - var fact goFixInlineConstFact + var fact goFixForwardConstFact if pass.ImportObjectFact(con, &fact) { incon = &fact - inlinableConsts[con] = incon + forwardableConsts[con] = incon } } if incon == nil { @@ -228,7 +239,7 @@ func run(pass *analysis.Pass) (any, error) { } // // We have an identifier A here (n), - // and an inlinable "const A = B" elsewhere (incon). + // and an forwardable "const A = B" elsewhere (incon). // Consider replacing A with B. // Check that the expression we are inlining (B) means the same thing // (refers to the same object) in n's scope as it does in A's scope. @@ -240,10 +251,10 @@ func run(pass *analysis.Pass) (any, error) { if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. - panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName)) + panic(fmt.Sprintf("no object for forwardable const %s RHS %s", n.Name, incon.RHSName)) } if obj != incon.rhsObj { - // "B" means something different here than at the inlinable const's scope + // "B" means something different here than at the forwardable const's scope return } } else { @@ -258,9 +269,9 @@ func run(pass *analysis.Pass) (any, error) { pass.Report(analysis.Diagnostic{ Pos: n.Pos(), End: n.End(), - Message: fmt.Sprintf("Constant %s should be inlined", n.Name), + Message: fmt.Sprintf("Constant %s should be forwarded", n.Name), SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Inline constant %s", n.Name), + Message: fmt.Sprintf("Forward constant %s", n.Name), TextEdits: []analysis.TextEdit{{ Pos: n.Pos(), End: n.End(), @@ -275,11 +286,11 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } -// hasInlineDirective reports whether cg has a directive -// of the form "//go:fix inline". -func hasInlineDirective(cg *ast.CommentGroup) bool { +// hasFixDirective reports whether cg has a directive +// of the form "//go:fix " + name. +func hasFixDirective(cg *ast.CommentGroup, name string) bool { return slices.ContainsFunc(directives(cg), func(d *directive) bool { - return d.Tool == "go" && d.Name == "fix" && d.Args == "inline" + return d.Tool == "go" && d.Name == "fix" && d.Args == name }) } @@ -295,20 +306,20 @@ type goFixInlineFuncFact struct{ Callee *inline.Callee } func (f *goFixInlineFuncFact) String() string { return "goFixInline " + f.Callee.String() } func (*goFixInlineFuncFact) AFact() {} -// A goFixInlineConstFact is exported for each constant marked "//go:fix inline". -// It holds information about an inlinable constant. Gob-serializable. -type goFixInlineConstFact struct { +// A goFixForwardConstFact is exported for each constant marked "//go:fix forward". +// It holds information about a forwardable constant. Gob-serializable. +type goFixForwardConstFact struct { // Information about "const LHSName = RHSName". RHSName string RHSPkgPath string rhsObj types.Object // for current package } -func (c *goFixInlineConstFact) String() string { - return fmt.Sprintf("goFixInline const %q.%s", c.RHSPkgPath, c.RHSName) +func (c *goFixForwardConstFact) String() string { + return fmt.Sprintf("goFixForward const %q.%s", c.RHSPkgPath, c.RHSName) } -func (*goFixInlineConstFact) AFact() {} +func (*goFixForwardConstFact) AFact() {} func discard(string, ...any) {} diff --git a/internal/refactor/inline/analyzer/directive.go b/internal/refactor/inline/analyzer/directive.go index f4426c5ffa8..4e605021307 100644 --- a/internal/refactor/inline/analyzer/directive.go +++ b/internal/refactor/inline/analyzer/directive.go @@ -13,6 +13,8 @@ import ( // -- plundered from the future (CL 605517, issue #68021) -- // TODO(adonovan): replace with ast.Directive after go1.24 (#68021). +// Beware of our local mods to handle analysistest +// "want" comments on the same line. // A directive is a comment line with special meaning to the Go // toolchain or another tool. It has the form: @@ -48,6 +50,9 @@ func directives(g *ast.CommentGroup) (res []*directive) { tool, nameargs = "", tool } name, args, _ := strings.Cut(nameargs, " ") // tab?? + // Permit an additional line comment after the args, chiefly to support + // [golang.org/x/tools/go/analysis/analysistest]. + args, _, _ = strings.Cut(args, "//") res = append(res, &directive{ Pos: c.Slash, Tool: tool, diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/internal/refactor/inline/analyzer/testdata/src/a/a.go index ae486746e5b..009afd5c7af 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go @@ -18,81 +18,87 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` +//go:fix forward // want `use //go:fix inline for functions` +func Three() {} + // Constants. const Uno = 1 -//go:fix inline -const In1 = Uno // want In1: `goFixInline const "a".Uno` +//go:fix forward +const In1 = Uno // want In1: `goFixForward const "a".Uno` const ( no1 = one - //go:fix inline - In2 = one // want In2: `goFixInline const "a".one` + //go:fix forward + In2 = one // want In2: `goFixForward const "a".one` ) -//go:fix inline +//go:fix forward const ( in3 = one in4 = one - bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + bad1 = 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` ) -//go:fix inline +//go:fix forward const in5, in6, bad2 = one, one, - one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + one + 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` // Make sure we don't crash on iota consts, but still process the whole decl. // -//go:fix inline +//go:fix forward const ( - a = iota // want `invalid //go:fix inline directive: const value is iota` + a = iota // want `invalid //go:fix forward directive: const value is iota` b in7 = one ) func _() { - x := In1 // want `Constant In1 should be inlined` - x = In2 // want `Constant In2 should be inlined` - x = in3 // want `Constant in3 should be inlined` - x = in4 // want `Constant in4 should be inlined` - x = in5 // want `Constant in5 should be inlined` - x = in6 // want `Constant in6 should be inlined` - x = in7 // want `Constant in7 should be inlined` + x := In1 // want `Constant In1 should be forwarded` + x = In2 // want `Constant In2 should be forwarded` + x = in3 // want `Constant in3 should be forwarded` + x = in4 // want `Constant in4 should be forwarded` + x = in5 // want `Constant in5 should be forwarded` + x = in6 // want `Constant in6 should be forwarded` + x = in7 // want `Constant in7 should be forwarded` x = no1 _ = x - in1 := 1 // don't inline lvalues + in1 := 1 // don't forward lvalues _ = in1 } const ( x = 1 - //go:fix inline + //go:fix forward in8 = x ) func shadow() { var x int // shadows x at package scope - //go:fix inline - const a = iota // want `invalid //go:fix inline directive: const value is iota` + //go:fix forward + const a = iota // want `invalid //go:fix forward directive: const value is iota` const iota = 2 // Below this point, iota is an ordinary constant. - //go:fix inline + //go:fix forward const b = iota - x = a // a is defined with the predeclared iota, so it cannot be inlined - x = b // want `Constant b should be inlined` + x = a // a is defined with the predeclared iota, so it cannot be forwarded + x = b // want `Constant b should be forwarded` - // Don't offer to inline in8, because the result, "x", would mean something different + // Don't offer to forward in8, because the result, "x", would mean something different // in this scope than it does in the scope where in8 is defined. x = in8 _ = x } + +//go:fix inline // want `use //go:fix forward for constants` +const In9 = x diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden index 7d75a598fb7..decbcdd561f 100644 --- a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden @@ -18,81 +18,87 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` +//go:fix forward // want `use //go:fix inline for functions` +func Three() {} + // Constants. const Uno = 1 -//go:fix inline -const In1 = Uno // want In1: `goFixInline const "a".Uno` +//go:fix forward +const In1 = Uno // want In1: `goFixForward const "a".Uno` const ( no1 = one - //go:fix inline - In2 = one // want In2: `goFixInline const "a".one` + //go:fix forward + In2 = one // want In2: `goFixForward const "a".one` ) -//go:fix inline +//go:fix forward const ( in3 = one in4 = one - bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + bad1 = 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` ) -//go:fix inline +//go:fix forward const in5, in6, bad2 = one, one, - one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + one + 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` // Make sure we don't crash on iota consts, but still process the whole decl. // -//go:fix inline +//go:fix forward const ( - a = iota // want `invalid //go:fix inline directive: const value is iota` + a = iota // want `invalid //go:fix forward directive: const value is iota` b in7 = one ) func _() { - x := Uno // want `Constant In1 should be inlined` - x = one // want `Constant In2 should be inlined` - x = one // want `Constant in3 should be inlined` - x = one // want `Constant in4 should be inlined` - x = one // want `Constant in5 should be inlined` - x = one // want `Constant in6 should be inlined` - x = one // want `Constant in7 should be inlined` + x := Uno // want `Constant In1 should be forwarded` + x = one // want `Constant In2 should be forwarded` + x = one // want `Constant in3 should be forwarded` + x = one // want `Constant in4 should be forwarded` + x = one // want `Constant in5 should be forwarded` + x = one // want `Constant in6 should be forwarded` + x = one // want `Constant in7 should be forwarded` x = no1 _ = x - in1 := 1 // don't inline lvalues + in1 := 1 // don't forward lvalues _ = in1 } const ( x = 1 - //go:fix inline + //go:fix forward in8 = x ) func shadow() { var x int // shadows x at package scope - //go:fix inline - const a = iota // want `invalid //go:fix inline directive: const value is iota` + //go:fix forward + const a = iota // want `invalid //go:fix forward directive: const value is iota` const iota = 2 // Below this point, iota is an ordinary constant. - //go:fix inline + //go:fix forward const b = iota - x = a // a is defined with the predeclared iota, so it cannot be inlined - x = iota // want `Constant b should be inlined` + x = a // a is defined with the predeclared iota, so it cannot be forwarded + x = iota // want `Constant b should be forwarded` - // Don't offer to inline in8, because the result, "x", would mean something different + // Don't offer to forward in8, because the result, "x", would mean something different // in this scope than it does in the scope where in8 is defined. x = in8 _ = x } + +//go:fix inline // want `use //go:fix forward for constants` +const In9 = x From 33e624fee8130c31e07a78698cd87013bf884a8d Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 3 Feb 2025 13:00:16 -0500 Subject: [PATCH 097/309] internal/refactor/inline/analyzer: document For golang/go#32816. Change-Id: Ibcde7c94377efb2f07b8bab507a1aab18a410ef9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/646495 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/doc/analyzers.md | 4 +- gopls/internal/doc/api.json | 4 +- internal/refactor/inline/analyzer/analyzer.go | 9 +- internal/refactor/inline/analyzer/doc.go | 83 +++++++++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 internal/refactor/inline/analyzer/doc.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 27491520b3c..fa882243f35 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -377,9 +377,11 @@ Default: on. Package documentation: [infertypeargs](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/infertypeargs) -## `inline`: inline calls to functions with "//go:fix inline" doc comment +## `inline`: inline functions and forward constants +The inline analyzer inlines functions that are marked for inlining +and forwards constants that are marked for forwarding. Default: on. diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 74a61599bf4..9379733ea5e 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -495,7 +495,7 @@ }, { "Name": "\"inline\"", - "Doc": "inline calls to functions with \"//go:fix inline\" doc comment", + "Doc": "inline functions and forward constants\n\nThe inline analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", "Default": "true" }, { @@ -1171,7 +1171,7 @@ }, { "Name": "inline", - "Doc": "inline calls to functions with \"//go:fix inline\" doc comment", + "Doc": "inline functions and forward constants\n\nThe inline analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", "URL": "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", "Default": true }, diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index 9583b2fd9e6..5426a6a4b75 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -11,21 +11,24 @@ import ( "go/types" "slices" + _ "embed" + "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/refactor/inline" "golang.org/x/tools/internal/typesinternal" ) -// TODO(jba): replace with better doc. -const Doc = `inline calls to functions with "//go:fix inline" doc comment` +//go:embed doc.go +var doc string var Analyzer = &analysis.Analyzer{ Name: "inline", - Doc: Doc, + Doc: analysisinternal.MustExtractDoc(doc, "inline"), URL: "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", Run: run, FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixForwardConstFact)}, diff --git a/internal/refactor/inline/analyzer/doc.go b/internal/refactor/inline/analyzer/doc.go new file mode 100644 index 00000000000..a4ac0f30093 --- /dev/null +++ b/internal/refactor/inline/analyzer/doc.go @@ -0,0 +1,83 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +/* +Package analyzer defines an Analyzer that inlines calls to functions +marked with a "//go:fix inline" doc comment, +and forwards uses of constants +marked with a "//go:fix forward" doc comment. + +# Analyzer inline + +inline: inline functions and forward constants + +The inline analyzer inlines functions that are marked for inlining +and forwards constants that are marked for forwarding. + +# Functions + +Given a function that is marked for inlining, like this one: + + //go:fix inline + func Square(x int) int { return Pow(x, 2) } + +this analyzer will recommend that calls to the function elsewhere, in the same +or other packages, should be inlined. + +Inlining can be used to move off of a deprecated function: + + // Deprecated: prefer Pow(x, 2). + //go:fix inline + func Square(x int) int { return Pow(x, 2) } + +It can also be used to move off of an obsolete package, +as when the import path has changed or a higher major version is available: + + package pkg + + import pkg2 "pkg/v2" + + //go:fix inline + func F() { pkg2.F(nil) } + +Replacing a call pkg.F() by pkg2.F(nil) can have no effect on the program, +so this mechanism provides a low-risk way to update large numbers of calls. +We recommend, where possible, expressing the old API in terms of the new one +to enable automatic migration. + +# Constants + +Given a constant that is marked for forwarding, like this one: + + //go:fix forward + const Ptr = Pointer + +this analyzer will recommend that uses of Ptr should be replaced with Pointer. + +As with inlining, forwarding can be used to replace deprecated constants and +constants in obsolete packages. + +A constant definition can be marked for forwarding only if it refers to another +named constant. + +The "//go:fix forward" comment must appear before a single const declaration on its own, +as above; before a const declaration that is part of a group, as in this case: + + const ( + C = 1 + //go:fix forward + Ptr = Pointer + ) + +or before a group, applying to every constant in the group: + + //go:fix forward + const ( + Ptr = Pointer + Val = Value + ) + +The proposal https://go.dev/issue/32816 introduces the "//go:fix" directives. +*/ +package analyzer From 8e4c84189edf4f6d01107d2bca211e079539e472 Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Thu, 30 Jan 2025 14:27:34 -0500 Subject: [PATCH 098/309] gopls/internal/server: embed style metadata in vulncheck progress The client need to indicate the message style it support for work done progress message in ClientCapabilities.experimental['progress-message-style'] = ['log', ...] Gopls will keep track of the supported styling formats from client and will only embed style metadata in WorkDoneProgressBegin's message field as "style: ${STYLE}\n\n${MESSAGE}" only if the client support them. At this point, gopls will only send message without styling or with log styling. VSCode-Go side change CL 645116. For golang/vscode-go#3572 Change-Id: I792211fd5885c8ef932e054fe46cf75b2695f47a Reviewed-on: https://go-review.googlesource.com/c/tools/+/645695 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/server/command.go | 23 +++++++++++++++-------- gopls/internal/settings/settings.go | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 7bba71100ca..5196d69ee07 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -309,10 +309,11 @@ func (c *commandHandler) AddTest(ctx context.Context, loc protocol.Location) (*p // commandConfig configures common command set-up and execution. type commandConfig struct { - requireSave bool // whether all files must be saved for the command to work - progress string // title to use for progress reporting. If empty, no progress will be reported. - forView string // view to resolve to a snapshot; incompatible with forURI - forURI protocol.DocumentURI // URI to resolve to a snapshot. If unset, snapshot will be nil. + requireSave bool // whether all files must be saved for the command to work + progress string // title to use for progress reporting. If empty, no progress will be reported. + progressStyle settings.WorkDoneProgressStyle // style information for client-side progress display. + forView string // view to resolve to a snapshot; incompatible with forURI + forURI protocol.DocumentURI // URI to resolve to a snapshot. If unset, snapshot will be nil. } // commandDeps is evaluated from a commandConfig. Note that not all fields may @@ -382,7 +383,11 @@ func (c *commandHandler) run(ctx context.Context, cfg commandConfig, run command ctx, cancel := context.WithCancel(xcontext.Detach(ctx)) if cfg.progress != "" { - deps.work = c.s.progress.Start(ctx, cfg.progress, "Running...", c.params.WorkDoneToken, cancel) + header := "" + if _, ok := c.s.options.SupportedWorkDoneProgressFormats[cfg.progressStyle]; ok && cfg.progressStyle != "" { + header = fmt.Sprintf("style: %s\n\n", cfg.progressStyle) + } + deps.work = c.s.progress.Start(ctx, cfg.progress, header+"Running...", c.params.WorkDoneToken, cancel) } runcmd := func() error { defer release() @@ -1214,9 +1219,10 @@ func (c *commandHandler) Vulncheck(ctx context.Context, args command.VulncheckAr var commandResult command.VulncheckResult err := c.run(ctx, commandConfig{ - progress: GoVulncheckCommandTitle, - requireSave: true, // govulncheck cannot honor overlays - forURI: args.URI, + progress: GoVulncheckCommandTitle, + progressStyle: settings.WorkDoneProgressStyleLog, + requireSave: true, // govulncheck cannot honor overlays + forURI: args.URI, }, func(ctx context.Context, deps commandDeps) error { jsonrpc2.Async(ctx) // run this in parallel with other requests: vulncheck can be slow. @@ -1276,6 +1282,7 @@ func (c *commandHandler) Vulncheck(ctx context.Context, args command.VulncheckAr // slated for deletion. // // TODO(golang/vscode-go#3572) +// TODO(hxjiang): deprecate gopls.run_govulncheck. func (c *commandHandler) RunGovulncheck(ctx context.Context, args command.VulncheckArgs) (command.RunVulncheckResult, error) { if args.URI == "" { return command.RunVulncheckResult{}, errors.New("VulncheckArgs is missing URI field") diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 3252858402d..038c814b1b7 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -63,6 +63,9 @@ type ClientOptions struct { SupportedResourceOperations []protocol.ResourceOperationKind CodeActionResolveOptions []string ShowDocumentSupported bool + // SupportedWorkDoneProgressFormats specifies the formats supported by the + // client for handling workdone progress metadata. + SupportedWorkDoneProgressFormats map[WorkDoneProgressStyle]bool } // ServerOptions holds LSP-specific configuration that is provided by the @@ -585,6 +588,10 @@ func (u *UserOptions) SetEnvSlice(env []string) { } } +type WorkDoneProgressStyle string + +const WorkDoneProgressStyleLog WorkDoneProgressStyle = "log" + // InternalOptions contains settings that are not intended for use by the // average user. These may be settings used by tests or outdated settings that // will soon be deprecated. Some of these settings may not even be configurable @@ -900,6 +907,16 @@ func (o *Options) ForClientCapabilities(clientInfo *protocol.ClientInfo, caps pr if caps.TextDocument.CodeAction.DataSupport && caps.TextDocument.CodeAction.ResolveSupport != nil { o.CodeActionResolveOptions = caps.TextDocument.CodeAction.ResolveSupport.Properties } + + // Client experimental capabilities. + if experimental, ok := caps.Experimental.(map[string]any); ok { + if formats, ok := experimental["progressMessageStyles"].([]any); ok { + o.SupportedWorkDoneProgressFormats = make(map[WorkDoneProgressStyle]bool, len(formats)) + for _, f := range formats { + o.SupportedWorkDoneProgressFormats[WorkDoneProgressStyle(f.(string))] = true + } + } + } } var codec = frob.CodecFor[*Options]() From 36263672ce24cea89ad16de132869c7b51d36684 Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Thu, 30 Jan 2025 17:34:18 -0500 Subject: [PATCH 099/309] gopls/internal/golang: hide signature help Avoids displaying signature help info when cursor is inside a composite literal within a CallExpr. In this case, the user already knows the type of the argument so they don't need to be reminded. Updates golang/go#65681 Change-Id: If0d7281e6b5d4ef2b2b1d4f6096549fc7ba55ae1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645756 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/signature_help.go | 7 ++++--- .../test/marker/testdata/signature/signature.txt | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/gopls/internal/golang/signature_help.go b/gopls/internal/golang/signature_help.go index 2211a45de61..1dbd76d57d0 100644 --- a/gopls/internal/golang/signature_help.go +++ b/gopls/internal/golang/signature_help.go @@ -72,9 +72,10 @@ loop: fnval = callExpr.Fun break loop } - case *ast.FuncLit, *ast.FuncType: - // The user is within an anonymous function, - // which may be the parameter to the *ast.CallExpr. + case *ast.FuncLit, *ast.FuncType, *ast.CompositeLit: + // The user is within an anonymous function or + // a composite literal, which may be the argument + // to the *ast.CallExpr. // Don't show signature help in this case. return nil, 0, nil case *ast.BasicLit: diff --git a/gopls/internal/test/marker/testdata/signature/signature.txt b/gopls/internal/test/marker/testdata/signature/signature.txt index 4f4064397c6..74f53a20e64 100644 --- a/gopls/internal/test/marker/testdata/signature/signature.txt +++ b/gopls/internal/test/marker/testdata/signature/signature.txt @@ -26,8 +26,16 @@ func Foo(a string, b int) (c bool) { func Bar(float64, ...byte) { } +func FooArr(a []int) { + +} + type myStruct struct{} +type Bar struct { + A, B, C, D string +} + func (*myStruct) foo(e *json.Decoder) (*big.Int, error) { return nil, nil } @@ -114,6 +122,14 @@ func Qux() { AliasSlice() //@signature(")", "AliasSlice(a []*Alias) (b Alias)", 0) AliasMap() //@signature(")", "AliasMap(a map[*Alias]StringAlias) (b map[*Alias]StringAlias, c map[*Alias]StringAlias)", 0) OtherAliasMap() //@signature(")", "OtherAliasMap(a map[Alias]OtherAlias, b map[Alias]OtherAlias) map[Alias]OtherAlias", 0) + + var l []Foo + l = append(l, Foo{ //@signature(",", "append(slice []Type, elems ...Type) []Type", 0) + A: "hello", //@signature(",", "", 0) + B: "world", //@signature(",", "", 0) + }) + + FooArr([]int{1, 2, 3, 4, 5}) //@signature("1", "", 0) } func Hello(func()) {} From e8d53408c6824b3880b8dff9cf40561842f629a3 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Thu, 9 Jan 2025 14:17:08 -0500 Subject: [PATCH 100/309] gopls/imports: use a module cache index This CL switches to using an imports.Source that resolves missing imports by first using the metadata graph (Snapshot.MetadataGraph()) to find missing imports from the workspace. Failing that it uses the module cache index to find missing imports in the module cache. In either case, it uses heuristics if there is more than one possibility. For instance, it will prefer an alternative named in the go.mod file. Also, it will tend to prefer a /v2. The new behavior is controlled by an option 'importsSource' which is (temporarily) set to get the old behavior, except in some new tests. Change-Id: I83a6f2bb12af94c9bc079a61306de3c9d1dc6ce4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/641776 Reviewed-by: Robert Findley Reviewed-by: Peter Weinberger LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/imports.go | 31 ++ gopls/internal/cache/session.go | 3 +- gopls/internal/cache/snapshot.go | 2 +- gopls/internal/cache/source.go | 378 +++++++++++++++++- gopls/internal/cache/view.go | 1 + gopls/internal/settings/default.go | 2 +- .../test/integration/misc/imports_test.go | 302 ++++++++++++++ internal/imports/fix.go | 13 +- internal/imports/source_env.go | 2 +- 9 files changed, 720 insertions(+), 14 deletions(-) diff --git a/gopls/internal/cache/imports.go b/gopls/internal/cache/imports.go index aa274221669..31a1b9d42a5 100644 --- a/gopls/internal/cache/imports.go +++ b/gopls/internal/cache/imports.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "sync" + "testing" "time" "golang.org/x/tools/gopls/internal/file" @@ -167,6 +168,36 @@ func newModcacheState(dir string) *modcacheState { return s } +func (s *modcacheState) GetIndex() (*modindex.Index, error) { + s.mu.Lock() + defer s.mu.Unlock() + ix := s.index + if ix == nil || len(ix.Entries) == 0 { + var err error + // this should only happen near the beginning of a session + // (or in tests) + ix, err = modindex.ReadIndex(s.dir) + if err != nil { + return nil, fmt.Errorf("ReadIndex %w", err) + } + if !testing.Testing() { + return ix, nil + } + if ix == nil || len(ix.Entries) == 0 { + err = modindex.Create(s.dir) + if err != nil { + return nil, fmt.Errorf("creating index %w", err) + } + ix, err = modindex.ReadIndex(s.dir) + if err != nil { + return nil, fmt.Errorf("read index after create %w", err) + } + s.index = ix + } + } + return s.index, nil +} + func (s *modcacheState) refreshIndex() { ok, err := modindex.Update(s.dir) if err != nil || !ok { diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index 5d85e2b606f..a7fb618f679 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -23,6 +23,7 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/label" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/persistent" "golang.org/x/tools/gopls/internal/vulncheck" @@ -237,7 +238,7 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, * viewDefinition: def, importsState: newImportsState(backgroundCtx, s.cache.modCache, pe), } - if def.folder.Options.ImportsSource != "off" { + if def.folder.Options.ImportsSource != settings.ImportsSourceOff { v.modcacheState = newModcacheState(def.folder.Env.GOMODCACHE) } diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index 4a2ae2431d7..c341ac6e85a 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -198,7 +198,7 @@ type Snapshot struct { var _ memoize.RefCounted = (*Snapshot)(nil) // snapshots are reference-counted -func (s *Snapshot) awaitPromise(ctx context.Context, p *memoize.Promise) (interface{}, error) { +func (s *Snapshot) awaitPromise(ctx context.Context, p *memoize.Promise) (any, error) { return p.Get(ctx, s) } diff --git a/gopls/internal/cache/source.go b/gopls/internal/cache/source.go index b5e1e74b160..3e21c641651 100644 --- a/gopls/internal/cache/source.go +++ b/gopls/internal/cache/source.go @@ -6,30 +6,398 @@ package cache import ( "context" + "log" + "maps" + "slices" + "strings" + "golang.org/x/tools/gopls/internal/cache/metadata" + "golang.org/x/tools/gopls/internal/cache/symbols" + "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/imports" ) -// interim code for using the module cache index in imports -// This code just forwards everything to an imports.ProcessEnvSource - // goplsSource is an imports.Source that provides import information using // gopls and the module cache index. -// TODO(pjw): implement. Right now, this just forwards to the imports.ProcessEnvSource. type goplsSource struct { + S *Snapshot envSource *imports.ProcessEnvSource + + // set by each invocation of ResolveReferences + ctx context.Context } func (s *Snapshot) NewGoplsSource(is *imports.ProcessEnvSource) *goplsSource { return &goplsSource{ + S: s, envSource: is, } } func (s *goplsSource) LoadPackageNames(ctx context.Context, srcDir string, paths []imports.ImportPath) (map[imports.ImportPath]imports.PackageName, error) { + // TODO: use metadata graph. Aside from debugging, this is the only used of envSource return s.envSource.LoadPackageNames(ctx, srcDir, paths) } +type result struct { + res *imports.Result + deprecated bool +} + +// ResolveReferences tries to find resolving imports in the workspace, and failing +// that, in the module cache. It uses heuristics to decide among alternatives. +// The heuristics will usually prefer a v2 version, if there is one. +// TODO: It does not take advantage of hints provided by the user: +// 1. syntactic context: pkg.Name().Foo +// 3. already imported files in the same module func (s *goplsSource) ResolveReferences(ctx context.Context, filename string, missing imports.References) ([]*imports.Result, error) { - return s.envSource.ResolveReferences(ctx, filename, missing) + s.ctx = ctx + // get results from the workspace. There will at most one for each package name + fromWS, err := s.resolveWorkspaceReferences(filename, missing) + if err != nil { + return nil, err + } + // collect the ones that are still + needed := maps.Clone(missing) + for _, a := range fromWS { + if _, ok := needed[a.Package.Name]; ok { + delete(needed, a.Package.Name) + } + } + // when debug (below) is gone, change this to: if len(needed) == 0 {return fromWS, nil} + var fromCache []*result + if len(needed) != 0 { + var err error + fromCache, err = s.resolveCacheReferences(needed) + if err != nil { + return nil, err + } + // trim cans to one per missing package. + byPkgNm := make(map[string][]*result) + for _, c := range fromCache { + byPkgNm[c.res.Package.Name] = append(byPkgNm[c.res.Package.Name], c) + } + for k, v := range byPkgNm { + fromWS = append(fromWS, s.bestCache(k, v)) + } + } + const debug = false + if debug { // debugging. + // what does the old one find? + old, err := s.envSource.ResolveReferences(ctx, filename, missing) + if err != nil { + log.Fatal(err) + } + log.Printf("fromCache:%d %s", len(fromCache), filename) + for i, c := range fromCache { + log.Printf("cans%d %#v %#v %v", i, c.res.Import, c.res.Package, c.deprecated) + } + for k, v := range missing { + for x := range v { + log.Printf("missing %s.%s", k, x) + } + } + for k, v := range needed { + for x := range v { + log.Printf("needed %s.%s", k, x) + } + } + + dbgpr := func(hdr string, v []*imports.Result) { + for i := 0; i < len(v); i++ { + log.Printf("%s%d %+v %+v", hdr, i, v[i].Import, v[i].Package) + } + } + + dbgpr("fromWS", fromWS) + dbgpr("old", old) + s.S.workspacePackages.Range(func(k PackageID, v PackagePath) { + log.Printf("workspacePackages[%s]=%s", k, v) + }) + // anything in ans with >1 matches? + seen := make(map[string]int) + for _, a := range fromWS { + seen[a.Package.Name]++ + } + for k, v := range seen { + if v > 1 { + log.Printf("saw %d %s", v, k) + for i, x := range fromWS { + if x.Package.Name == k { + log.Printf("%d: %+v %+v", i, x.Package, x.Import) + } + } + } + } + } + return fromWS, nil + +} + +func (s *goplsSource) resolveCacheReferences(missing imports.References) ([]*result, error) { + state := s.S.view.modcacheState + ix, err := state.GetIndex() + if err != nil { + event.Error(s.ctx, "resolveCacheReferences", err) + } + + found := make(map[string]*result) + for pkg, nms := range missing { + var ks []string + for k := range nms { + ks = append(ks, k) + } + cs := ix.LookupAll(pkg, ks...) // map[importPath][]Candidate + for k, cands := range cs { + res := found[k] + if res == nil { + res = &result{ + &imports.Result{ + Import: &imports.ImportInfo{ImportPath: k}, + Package: &imports.PackageInfo{Name: pkg, Exports: make(map[string]bool)}, + }, + false, + } + found[k] = res + } + for _, c := range cands { + res.res.Package.Exports[c.Name] = true + // The import path is deprecated if a symbol that would be used is deprecated + res.deprecated = res.deprecated || c.Deprecated + } + } + + } + var ans []*result + for _, x := range found { + ans = append(ans, x) + } + return ans, nil +} + +type found struct { + sym *symbols.Package + res *imports.Result +} + +func (s *goplsSource) resolveWorkspaceReferences(filename string, missing imports.References) ([]*imports.Result, error) { + uri := protocol.URIFromPath(filename) + mypkgs, err := s.S.MetadataForFile(s.ctx, uri) + if len(mypkgs) != 1 { + // what does this mean? can it happen? + } + mypkg := mypkgs[0] + // search the metadata graph for package ids correstponding to missing + g := s.S.MetadataGraph() + var ids []metadata.PackageID + var pkgs []*metadata.Package + for pid, pkg := range g.Packages { + // no test packages, except perhaps for ourselves + if pkg.ForTest != "" && pkg != mypkg { + continue + } + if missingWants(missing, pkg.Name) { + ids = append(ids, pid) + pkgs = append(pkgs, pkg) + } + } + // find the symbols in those packages + // the syms occur in the same order as the ids and the pkgs + syms, err := s.S.Symbols(s.ctx, ids...) + if err != nil { + return nil, err + } + // keep track of used syms and found results by package name + // TODO: avoid import cycles (is current package in forward closure) + founds := make(map[string][]found) + for i := 0; i < len(ids); i++ { + nm := string(pkgs[i].Name) + if satisfies(syms[i], missing[nm]) { + got := &imports.Result{ + Import: &imports.ImportInfo{ + Name: "", + ImportPath: string(pkgs[i].PkgPath), + }, + Package: &imports.PackageInfo{ + Name: string(pkgs[i].Name), + Exports: missing[imports.PackageName(pkgs[i].Name)], + }, + } + founds[nm] = append(founds[nm], found{syms[i], got}) + } + } + var ans []*imports.Result + for _, v := range founds { + // make sure the elements of v are unique + // (Import.ImportPath or Package.Name must differ) + cmp := func(l, r found) int { + switch strings.Compare(l.res.Import.ImportPath, r.res.Import.ImportPath) { + case -1: + return -1 + case 1: + return 1 + } + return strings.Compare(l.res.Package.Name, r.res.Package.Name) + } + slices.SortFunc(v, cmp) + newv := make([]found, 0, len(v)) + newv = append(newv, v[0]) + for i := 1; i < len(v); i++ { + if cmp(v[i], v[i-1]) != 0 { + newv = append(newv, v[i]) + } + } + ans = append(ans, bestImport(filename, newv)) + } + return ans, nil +} + +// for each package name, choose one using heuristics +func bestImport(filename string, got []found) *imports.Result { + if len(got) == 1 { + return got[0].res + } + isTestFile := strings.HasSuffix(filename, "_test.go") + var leftovers []found + for _, g := range got { + // don't use _test packages unless isTestFile + testPkg := strings.HasSuffix(string(g.res.Package.Name), "_test") || strings.HasSuffix(string(g.res.Import.Name), "_test") + if testPkg && !isTestFile { + continue // no test covers this + } + if imports.CanUse(filename, g.sym.Files[0].DirPath()) { + leftovers = append(leftovers, g) + } + } + switch len(leftovers) { + case 0: + break // use got, they are all bad + case 1: + return leftovers[0].res // only one left + default: + got = leftovers // filtered some out + } + + // TODO: if there are versions (like /v2) prefer them + + // use distance to common ancestor with filename + // (TestDirectoryFilters_MultiRootImportScanning) + // filename is .../a/main.go, choices are + // .../a/hi/hi.go and .../b/hi/hi.go + longest := -1 + ix := -1 + for i := 0; i < len(got); i++ { + d := commonpref(filename, got[i].sym.Files[0].Path()) + if d > longest { + longest = d + ix = i + } + } + // it is possible that there were several tied, but we return the first + return got[ix].res +} + +// choose the best result for the package named nm from the module cache +func (s *goplsSource) bestCache(nm string, got []*result) *imports.Result { + if len(got) == 1 { + return got[0].res + } + // does the go.mod file choose one? + if ans := s.fromGoMod(got); ans != nil { + return ans + } + got = preferUndeprecated(got) + // want the best Import.ImportPath + // these are all for the package named nm, + // nm (probably) occurs in all the paths; + // choose the longest (after nm), so as to get /v2 + maxlen, which := -1, -1 + for i := 0; i < len(got); i++ { + ix := strings.Index(got[i].res.Import.ImportPath, nm) + if ix == -1 { + continue // now what? + } + cnt := len(got[i].res.Import.ImportPath) - ix + if cnt > maxlen { + maxlen = cnt + which = i + } + // what about ties? (e.g., /v2 and /v3) + } + if which >= 0 { + return got[which].res + } + return got[0].res // arbitrary guess +} + +// if go.mod requires one of the packages, return that +func (s *goplsSource) fromGoMod(got []*result) *imports.Result { + // should we use s.S.view.worsspaceModFiles, and the union of their requires? + // (note that there are no tests where it contains more than one) + modURI := s.S.view.gomod + modfh, ok := s.S.files.get(modURI) + if !ok { + return nil + } + parsed, err := s.S.ParseMod(s.ctx, modfh) + if err != nil { + return nil + } + reqs := parsed.File.Require + for _, g := range got { + for _, req := range reqs { + if strings.HasPrefix(g.res.Import.ImportPath, req.Syntax.Token[1]) { + return g.res + } + } + } + return nil +} + +func commonpref(filename string, path string) int { + k := 0 + for ; k < len(filename) && k < len(path) && filename[k] == path[k]; k++ { + } + return k +} + +func satisfies(pkg *symbols.Package, missing map[string]bool) bool { + syms := make(map[string]bool) + for _, x := range pkg.Symbols { + for _, s := range x { + syms[s.Name] = true + } + } + for k := range missing { + if !syms[k] { + return false + } + } + return true +} + +// does pkgPath potentially satisfy a missing reference? +func missingWants(missing imports.References, pkgPath metadata.PackageName) bool { + for k := range missing { + if string(k) == string(pkgPath) { + return true + } + } + return false +} + +// If there are both deprecated and undprecated ones +// then return only the undeprecated one +func preferUndeprecated(got []*result) []*result { + var ok []*result + for _, g := range got { + if !g.deprecated { + ok = append(ok, g) + } + } + if len(ok) > 0 { + return ok + } + return got } diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index 0169b8394b7..26f0de86125 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -492,6 +492,7 @@ func (v *View) shutdown() { // Cancel the initial workspace load if it is still running. v.cancelInitialWorkspaceLoad() v.importsState.stopTimer() + v.modcacheState.stopTimer() v.snapshotMu.Lock() if v.snapshot != nil { diff --git a/gopls/internal/settings/default.go b/gopls/internal/settings/default.go index cd275e37ffb..ebb3f1ccfae 100644 --- a/gopls/internal/settings/default.go +++ b/gopls/internal/settings/default.go @@ -39,7 +39,7 @@ func DefaultOptions(overrides ...func(*Options)) *Options { DynamicWatchedFilesSupported: true, LineFoldingOnly: false, HierarchicalDocumentSymbolSupport: true, - ImportsSource: ImportsSourceGopls, + ImportsSource: ImportsSourceGoimports, }, ServerOptions: ServerOptions{ SupportedCodeActions: map[file.Kind]map[protocol.CodeActionKind]bool{ diff --git a/gopls/internal/test/integration/misc/imports_test.go b/gopls/internal/test/integration/misc/imports_test.go index 5b8b020124d..98a70478ecf 100644 --- a/gopls/internal/test/integration/misc/imports_test.go +++ b/gopls/internal/test/integration/misc/imports_test.go @@ -8,11 +8,14 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" + "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/gopls/internal/test/compare" . "golang.org/x/tools/gopls/internal/test/integration" + "golang.org/x/tools/gopls/internal/test/integration/fake" "golang.org/x/tools/gopls/internal/protocol" ) @@ -246,6 +249,158 @@ var _, _ = x.X, y.Y }) } +// make sure it gets the v2 +/* marker test? + +Add proxy data with the special proxy/ prefix (see gopls/internal/test/marker/testdata/quickfix/unusedrequire.txt). +Invoke the organizeImports codeaction directly (see gopls/internal/test/marker/testdata/codeaction/imports.txt, but use the edit=golden named argument instead of result= to minimize the size of the golden output. +*/ +func Test58382(t *testing.T) { + files := `-- main.go -- +package main +import "fmt" +func main() { + fmt.Println(xurls.Relaxed().FindAllString()) +} +-- go.mod -- +module demo +go 1.20 +` + cache := `-- mvdan.cc/xurls@v2.5.0/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +-- github.com/mvdan/xurls/v2@v1.1.0/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +` + modcache := t.TempDir() + defer cleanModCache(t, modcache) + mx := fake.UnpackTxt(cache) + for k, v := range mx { + fname := filepath.Join(modcache, k) + dir := filepath.Dir(fname) + os.MkdirAll(dir, 0777) + if err := os.WriteFile(fname, v, 0644); err != nil { + t.Fatal(err) + } + } + WithOptions( + EnvVars{"GOMODCACHE": modcache}, + WriteGoSum("."), + Settings{"importsSource": settings.ImportsSourceGopls}, + ).Run(t, files, func(t *testing.T, env *Env) { + + env.OpenFile("main.go") + env.SaveBuffer("main.go") + out := env.BufferText("main.go") + if !strings.Contains(out, "xurls/v2") { + t.Errorf("did not get v2 in %q", out) + } + }) +} + +// get the version requested in the go.mod file, not /v2 +func Test61208(t *testing.T) { + files := `-- main.go -- +package main +import "fmt" +func main() { + fmt.Println(xurls.Relaxed().FindAllString()) +} +-- go.mod -- +module demo +go 1.20 +require github.com/mvdan/xurls v1.1.0 +` + cache := `-- mvdan.cc/xurls/v2@v2.5.0/a/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +-- github.com/mvdan/xurls@v1.1.0/a/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +` + modcache := t.TempDir() + defer cleanModCache(t, modcache) + mx := fake.UnpackTxt(cache) + for k, v := range mx { + fname := filepath.Join(modcache, k) + dir := filepath.Dir(fname) + os.MkdirAll(dir, 0777) + if err := os.WriteFile(fname, v, 0644); err != nil { + t.Fatal(err) + } + } + WithOptions( + EnvVars{"GOMODCACHE": modcache}, + WriteGoSum("."), + ).Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + env.SaveBuffer("main.go") + out := env.BufferText("main.go") + if !strings.Contains(out, "github.com/mvdan/xurls") { + t.Errorf("did not get github.com/mvdan/xurls in %q", out) + } + }) +} + +// get the version already used in the module +func Test60663(t *testing.T) { + files := `-- main.go -- +package main +import "fmt" +func main() { + fmt.Println(xurls.Relaxed().FindAllString()) +} +-- go.mod -- +module demo +go 1.20 +-- a.go -- +package main +import "github.com/mvdan/xurls" +var _ = xurls.Relaxed() +` + cache := `-- mvdan.cc/xurls/v2@v2.5.0/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +-- github.com/mvdan/xurls@v1.1.0/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +` + modcache := t.TempDir() + defer cleanModCache(t, modcache) + mx := fake.UnpackTxt(cache) + for k, v := range mx { + fname := filepath.Join(modcache, k) + dir := filepath.Dir(fname) + os.MkdirAll(dir, 0777) + if err := os.WriteFile(fname, v, 0644); err != nil { + t.Fatal(err) + } + } + WithOptions( + EnvVars{"GOMODCACHE": modcache}, + WriteGoSum("."), + ).Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + env.SaveBuffer("main.go") + out := env.BufferText("main.go") + if !strings.Contains(out, "github.com/mvdan/xurls") { + t.Errorf("did not get github.com/mvdan/xurls in %q", out) + } + }) +} func TestRelativeReplace(t *testing.T) { const files = ` -- go.mod -- @@ -342,6 +497,42 @@ func TestA(t *testing.T) { }) } +// Test of golang/go#70755 +func TestQuickFixIssue70755(t *testing.T) { + const files = ` +-- go.mod -- +module mod.com +go 1.19.0 // with go 1.23.0 this fails on some builders +-- bar/bar.go -- +package notbar +type NotBar struct {} +-- baz/baz.go -- +package baz +type Baz struct {} +-- foo/foo.go -- +package foo +type foo struct { + bar notbar.NotBar + baz baz.Baz +}` + WithOptions( + Settings{"importsSource": settings.ImportsSourceGopls}). + Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("foo/foo.go") + var d protocol.PublishDiagnosticsParams + env.AfterChange(ReadDiagnostics("foo/foo.go", &d)) + env.ApplyQuickFixes("foo/foo.go", d.Diagnostics) + // at this point 'import notbar "mod.com/bar"' has been added + // but it's still missing the import of "mod.com/baz" + y := env.BufferText("foo/foo.go") + if !strings.Contains(y, `notbar "mod.com/bar"`) { + t.Error("quick fix did not find notbar") + } + env.SaveBuffer("foo/foo.go") + env.AfterChange(NoDiagnostics(ForFile("foo/foo.go"))) + }) +} + // Test for golang/go#52784 func TestGoWorkImports(t *testing.T) { const pkg = ` @@ -386,3 +577,114 @@ func Test() { env.AfterChange(NoDiagnostics(ForFile("caller/caller.go"))) }) } + +// prefer the undeprecated alternative 70736 +func TestDeprecated70736(t *testing.T) { + t.Logf("GOOS %s, GARCH %s version %s", runtime.GOOS, runtime.GOARCH, runtime.Version()) + files := `-- main.go -- +package main +func main() { + var v = xurls.Relaxed().FindAllString() + var w = xurls.A +} +-- go.mod -- +module demo +go 1.20 +` + cache := `-- mvdan.cc/xurls/v2@v2.5.0/xurls.go -- +package xurls +// Deprecated: +func Relaxed() *regexp.Regexp { +return nil +} +var A int +-- github.com/mvdan/xurls@v1.1.0/xurls.go -- +package xurls +func Relaxed() *regexp.Regexp { +return nil +} +var A int +` + modcache := t.TempDir() + defer cleanModCache(t, modcache) + mx := fake.UnpackTxt(cache) + for k, v := range mx { + fname := filepath.Join(modcache, k) + dir := filepath.Dir(fname) + os.MkdirAll(dir, 0777) + if err := os.WriteFile(fname, v, 0644); err != nil { + t.Fatal(err) + } + } + WithOptions( + EnvVars{"GOMODCACHE": modcache}, + WriteGoSum("."), + Settings{"importsSource": settings.ImportsSourceGopls}, + ).Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + env.SaveBuffer("main.go") + out := env.BufferText("main.go") + if strings.Contains(out, "xurls/v2") { + t.Errorf("chose deprecated v2 in %q", out) + } + }) +} + +// Find the non-test package asked for in a test +func TestTestImports(t *testing.T) { + const pkg = ` +-- go.work -- +go 1.19 + +use ( + ./caller + ./mod + ./xxx +) +-- caller/go.mod -- +module caller.com + +go 1.18 + +require mod.com v0.0.0 +require xxx.com v0.0.0 + +replace mod.com => ../mod +replace xxx.com => ../xxx +-- caller/caller_test.go -- +package main + +var _ = a.Test +-- xxx/go.mod -- +module xxx.com + +go 1.18 +-- xxx/a/a_test.go -- +package a + +func Test() { +} +-- mod/go.mod -- +module mod.com + +go 1.18 +-- mod/a/a.go -- +package a + +func Test() { +} +` + WithOptions(Modes(Default)).Run(t, pkg, func(t *testing.T, env *Env) { + env.OpenFile("caller/caller_test.go") + env.AfterChange(Diagnostics(env.AtRegexp("caller/caller_test.go", "a.Test"))) + + // Saving caller_test.go should trigger goimports, which should find a.Test in + // the mod.com module, thanks to the go.work file. + env.SaveBuffer("caller/caller_test.go") + env.AfterChange(NoDiagnostics(ForFile("caller/caller_test.go"))) + buf := env.BufferText("caller/caller_test.go") + if !strings.Contains(buf, "mod.com/a") { + t.Errorf("got %q, expected a mod.com/a", buf) + } + }) +} diff --git a/internal/imports/fix.go b/internal/imports/fix.go index b1fac90fff9..bf6b0aaddde 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -780,7 +780,7 @@ func GetAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix return true }, dirFound: func(pkg *pkg) bool { - if !canUse(filename, pkg.dir) { + if !CanUse(filename, pkg.dir) { return false } // Try the assumed package name first, then a simpler path match @@ -815,7 +815,7 @@ func GetImportPaths(ctx context.Context, wrapped func(ImportFix), searchPrefix, return true }, dirFound: func(pkg *pkg) bool { - if !canUse(filename, pkg.dir) { + if !CanUse(filename, pkg.dir) { return false } return strings.HasPrefix(pkg.importPathShort, searchPrefix) @@ -1132,6 +1132,9 @@ func addStdlibCandidates(pass *pass, refs References) error { // but we have no way of figuring out what the user is using // TODO: investigate using the toolchain version to disambiguate in the stdlib add("math/rand/v2") + // math/rand has an overlapping API + // TestIssue66407 fails without this + add("math/rand") continue } for importPath := range stdlib.PackageSymbols { @@ -1736,7 +1739,7 @@ func (s *symbolSearcher) searchOne(ctx context.Context, c pkgDistance, symbols m // searching for "client.New") func pkgIsCandidate(filename string, refs References, pkg *pkg) bool { // Check "internal" and "vendor" visibility: - if !canUse(filename, pkg.dir) { + if !CanUse(filename, pkg.dir) { return false } @@ -1759,9 +1762,9 @@ func pkgIsCandidate(filename string, refs References, pkg *pkg) bool { return false } -// canUse reports whether the package in dir is usable from filename, +// CanUse reports whether the package in dir is usable from filename, // respecting the Go "internal" and "vendor" visibility rules. -func canUse(filename, dir string) bool { +func CanUse(filename, dir string) bool { // Fast path check, before any allocations. If it doesn't contain vendor // or internal, it's not tricky: // Note that this can false-negative on directories like "notinternal", diff --git a/internal/imports/source_env.go b/internal/imports/source_env.go index d14abaa3195..ec996c3ccf6 100644 --- a/internal/imports/source_env.go +++ b/internal/imports/source_env.go @@ -67,7 +67,7 @@ func (s *ProcessEnvSource) ResolveReferences(ctx context.Context, filename strin // same package name. Don't try to import ourselves. return false } - if !canUse(filename, pkg.dir) { + if !CanUse(filename, pkg.dir) { return false } mu.Lock() From 74b5526d8b9d1f5c1020b68fe44365f34071cf3f Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Thu, 30 Jan 2025 17:16:43 -0500 Subject: [PATCH 101/309] gopls/internal/golang: support package symbols Adds new command for fetching package symbols. Nests methods under their receiver type. Change-Id: I803c4023abee3e8d3f6cdeb226f5ee5cd421b062 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645755 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Reviewed-by: Hongxiang Jiang --- gopls/internal/golang/symbols.go | 106 ++++++++++++++++++ .../internal/protocol/command/command_gen.go | 16 +++ gopls/internal/protocol/command/interface.go | 35 ++++++ gopls/internal/server/command.go | 25 +++++ .../integration/misc/package_symbols_test.go | 100 +++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 gopls/internal/test/integration/misc/package_symbols_test.go diff --git a/gopls/internal/golang/symbols.go b/gopls/internal/golang/symbols.go index 35959c2de7a..14f2703441c 100644 --- a/gopls/internal/golang/symbols.go +++ b/gopls/internal/golang/symbols.go @@ -15,6 +15,8 @@ import ( "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/protocol/command" + "golang.org/x/tools/gopls/internal/util/astutil" "golang.org/x/tools/internal/event" ) @@ -74,6 +76,110 @@ func DocumentSymbols(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand return symbols, nil } +// PackageSymbols returns a list of symbols in the narrowest package for the given file (specified +// by its URI). +// Methods with receivers are stored as children under the symbol for their receiver type. +// The PackageSymbol data type contains the same fields as protocol.DocumentSymbol, with +// an additional int field "File" that stores the index of that symbol's file in the +// PackageSymbolsResult.Files. +func PackageSymbols(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (command.PackageSymbolsResult, error) { + ctx, done := event.Start(ctx, "source.PackageSymbols") + defer done() + + mp, err := NarrowestMetadataForFile(ctx, snapshot, uri) + if err != nil { + return command.PackageSymbolsResult{}, err + } + pkgfiles := mp.CompiledGoFiles + // Maps receiver name to the methods that use it + receiverToMethods := make(map[string][]command.PackageSymbol) + // Maps type symbol name to its index in symbols + typeSymbolToIdx := make(map[string]int) + var symbols []command.PackageSymbol + for fidx, f := range pkgfiles { + fh, err := snapshot.ReadFile(ctx, f) + if err != nil { + return command.PackageSymbolsResult{}, err + } + pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) + if err != nil { + return command.PackageSymbolsResult{}, err + } + for _, decl := range pgf.File.Decls { + switch decl := decl.(type) { + case *ast.FuncDecl: + if decl.Name.Name == "_" { + continue + } + if fs, err := funcSymbol(pgf.Mapper, pgf.Tok, decl); err == nil { + // If function is a method, prepend the type of the method. + // Don't add the method as its own symbol; store it so we can + // add it as a child of the receiver type later + if decl.Recv != nil && len(decl.Recv.List) > 0 { + _, rname, _ := astutil.UnpackRecv(decl.Recv.List[0].Type) + receiverToMethods[rname.String()] = append(receiverToMethods[rname.String()], toPackageSymbol(fidx, fs)) + } else { + symbols = append(symbols, toPackageSymbol(fidx, fs)) + } + } + case *ast.GenDecl: + for _, spec := range decl.Specs { + switch spec := spec.(type) { + case *ast.TypeSpec: + if spec.Name.Name == "_" { + continue + } + if ts, err := typeSymbol(pgf.Mapper, pgf.Tok, spec); err == nil { + typeSymbolToIdx[ts.Name] = len(symbols) + symbols = append(symbols, toPackageSymbol(fidx, ts)) + } + case *ast.ValueSpec: + for _, name := range spec.Names { + if name.Name == "_" { + continue + } + if vs, err := varSymbol(pgf.Mapper, pgf.Tok, spec, name, decl.Tok == token.CONST); err == nil { + symbols = append(symbols, toPackageSymbol(fidx, vs)) + } + } + } + } + } + } + } + // Add methods as the child of their receiver type symbol + for recv, methods := range receiverToMethods { + if i, ok := typeSymbolToIdx[recv]; ok { + symbols[i].Children = append(symbols[i].Children, methods...) + } + } + return command.PackageSymbolsResult{ + PackageName: string(mp.Name), + Files: pkgfiles, + Symbols: symbols, + }, nil + +} + +func toPackageSymbol(fileIndex int, s protocol.DocumentSymbol) command.PackageSymbol { + var res command.PackageSymbol + res.Name = s.Name + res.Detail = s.Detail + res.Kind = s.Kind + res.Tags = s.Tags + res.Range = s.Range + res.SelectionRange = s.SelectionRange + + children := make([]command.PackageSymbol, len(s.Children)) + for i, c := range s.Children { + children[i] = toPackageSymbol(fileIndex, c) + } + res.Children = children + + res.File = fileIndex + return res +} + func funcSymbol(m *protocol.Mapper, tf *token.File, decl *ast.FuncDecl) (protocol.DocumentSymbol, error) { s := protocol.DocumentSymbol{ Name: decl.Name.Name, diff --git a/gopls/internal/protocol/command/command_gen.go b/gopls/internal/protocol/command/command_gen.go index 36938a41f14..c9b18a40cb8 100644 --- a/gopls/internal/protocol/command/command_gen.go +++ b/gopls/internal/protocol/command/command_gen.go @@ -47,6 +47,7 @@ const ( MaybePromptForTelemetry Command = "gopls.maybe_prompt_for_telemetry" MemStats Command = "gopls.mem_stats" Modules Command = "gopls.modules" + PackageSymbols Command = "gopls.package_symbols" Packages Command = "gopls.packages" RegenerateCgo Command = "gopls.regenerate_cgo" RemoveDependency Command = "gopls.remove_dependency" @@ -91,6 +92,7 @@ var Commands = []Command{ MaybePromptForTelemetry, MemStats, Modules, + PackageSymbols, Packages, RegenerateCgo, RemoveDependency, @@ -246,6 +248,12 @@ func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Inte return nil, err } return s.Modules(ctx, a0) + case PackageSymbols: + var a0 PackageSymbolsArgs + if err := UnmarshalArgs(params.Arguments, &a0); err != nil { + return nil, err + } + return s.PackageSymbols(ctx, a0) case Packages: var a0 PackagesArgs if err := UnmarshalArgs(params.Arguments, &a0); err != nil { @@ -530,6 +538,14 @@ func NewModulesCommand(title string, a0 ModulesArgs) *protocol.Command { } } +func NewPackageSymbolsCommand(title string, a0 PackageSymbolsArgs) *protocol.Command { + return &protocol.Command{ + Title: title, + Command: PackageSymbols.String(), + Arguments: MustMarshalArgs(a0), + } +} + func NewPackagesCommand(title string, a0 PackagesArgs) *protocol.Command { return &protocol.Command{ Title: title, diff --git a/gopls/internal/protocol/command/interface.go b/gopls/internal/protocol/command/interface.go index 060f72ce548..32e03dd388a 100644 --- a/gopls/internal/protocol/command/interface.go +++ b/gopls/internal/protocol/command/interface.go @@ -294,6 +294,9 @@ type Interface interface { // language server client), there should never be a case where Modules is // called on a path that has not already been loaded. Modules(context.Context, ModulesArgs) (ModulesResult, error) + + // PackageSymbols: Return information about symbols in the given file's package. + PackageSymbols(context.Context, PackageSymbolsArgs) (PackageSymbolsResult, error) } type RunTestsArgs struct { @@ -792,3 +795,35 @@ type ModulesArgs struct { type ModulesResult struct { Modules []Module } + +type PackageSymbolsArgs struct { + URI protocol.DocumentURI +} + +type PackageSymbolsResult struct { + PackageName string + // Files is a list of files in the given URI's package. + Files []protocol.DocumentURI + Symbols []PackageSymbol +} + +// PackageSymbol has the same fields as DocumentSymbol, with an additional int field "File" +// which stores the index of the symbol's file in the PackageSymbolsResult.Files array +type PackageSymbol struct { + Name string `json:"name"` + + Detail string `json:"detail,omitempty"` + + Kind protocol.SymbolKind `json:"kind"` + + Tags []protocol.SymbolTag `json:"tags,omitempty"` + + Range protocol.Range `json:"range"` + + SelectionRange protocol.Range `json:"selectionRange"` + + Children []PackageSymbol `json:"children,omitempty"` + + // Index of this symbol's file in PackageSymbolsResult.Files + File int `json:"file,omitempty"` +} diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 5196d69ee07..2b5c282a28f 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -1735,3 +1735,28 @@ func (c *commandHandler) ScanImports(ctx context.Context) error { } return nil } + +func (c *commandHandler) PackageSymbols(ctx context.Context, args command.PackageSymbolsArgs) (command.PackageSymbolsResult, error) { + var result command.PackageSymbolsResult + err := c.run(ctx, commandConfig{ + forURI: args.URI, + }, func(ctx context.Context, deps commandDeps) error { + res, err := golang.PackageSymbols(ctx, deps.snapshot, args.URI) + if err != nil { + return err + } + result = res + return nil + }) + + // sort symbols for determinism + sort.SliceStable(result.Symbols, func(i, j int) bool { + iv, jv := result.Symbols[i], result.Symbols[j] + if iv.Name == jv.Name { + return iv.Range.Start.Line < jv.Range.Start.Line + } + return iv.Name < jv.Name + }) + + return result, err +} diff --git a/gopls/internal/test/integration/misc/package_symbols_test.go b/gopls/internal/test/integration/misc/package_symbols_test.go new file mode 100644 index 00000000000..860264f2bb0 --- /dev/null +++ b/gopls/internal/test/integration/misc/package_symbols_test.go @@ -0,0 +1,100 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package misc + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/protocol/command" + "golang.org/x/tools/gopls/internal/test/integration" +) + +func TestPackageSymbols(t *testing.T) { + const files = ` +-- a.go -- +package a + +var A = "var" +type S struct{} + +func (s *S) M1() {} +-- b.go -- +package a + +var b = 1 + +func (s *S) M2() {} + +func (s *S) M3() {} + +func F() {} +` + integration.Run(t, files, func(t *testing.T, env *integration.Env) { + a_uri := env.Sandbox.Workdir.URI("a.go") + b_uri := env.Sandbox.Workdir.URI("b.go") + args, err := command.MarshalArgs(command.PackageSymbolsArgs{ + URI: a_uri, + }) + if err != nil { + t.Fatalf("failed to MarshalArgs: %v", err) + } + + var res command.PackageSymbolsResult + env.ExecuteCommand(&protocol.ExecuteCommandParams{ + Command: "gopls.package_symbols", + Arguments: args, + }, &res) + + want := command.PackageSymbolsResult{ + PackageName: "a", + Files: []protocol.DocumentURI{a_uri, b_uri}, + Symbols: []command.PackageSymbol{ + { + Name: "A", + Kind: protocol.Variable, + File: 0, + }, + { + Name: "F", + Kind: protocol.Function, + File: 1, + }, + { + Name: "S", + Kind: protocol.Struct, + File: 0, + Children: []command.PackageSymbol{ + { + Name: "M1", + Kind: protocol.Method, + File: 0, + }, + { + Name: "M2", + Kind: protocol.Method, + File: 1, + }, + { + Name: "M3", + Kind: protocol.Method, + File: 1, + }, + }, + }, + { + Name: "b", + Kind: protocol.Variable, + File: 1, + }, + }, + } + if diff := cmp.Diff(want, res, cmpopts.IgnoreFields(command.PackageSymbol{}, "Range", "SelectionRange", "Detail")); diff != "" { + t.Errorf("gopls.package_symbols returned unexpected diff (-want +got):\n%s", diff) + } + }) +} From 6557d184e5700b6842a983d3955c58a3bce3a14e Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 4 Feb 2025 11:17:22 -0500 Subject: [PATCH 102/309] internal/refactor/inline/analyzer: handle cross-package constants Handle the cases where the constant being forwarded, or its right-hand side, are in a package other than the that of the expression being replaced. Also, fix test non-determinism due to map iteration order. For golang/go#32816. Change-Id: I7a623a13400f21cd5e20655180c583a8b476cc0c Reviewed-on: https://go-review.googlesource.com/c/tools/+/646655 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/analysistest/analysistest.go | 23 ++++- internal/refactor/inline/analyzer/analyzer.go | 92 ++++++++++++------- .../inline/analyzer/testdata/src/b/b.go | 21 +++++ .../analyzer/testdata/src/b/b.go.golden | 29 +++++- .../inline/analyzer/testdata/src/c/c.go | 5 + 5 files changed, 131 insertions(+), 39 deletions(-) create mode 100644 internal/refactor/inline/analyzer/testdata/src/c/c.go diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index e63dd16c06b..4300490a445 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -16,6 +16,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "sort" "strconv" "strings" @@ -254,8 +255,15 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns t.Errorf("%s.golden has leading comment; we don't know what to do with it", file.Name()) continue } - - for sf, edits := range fixes { + // Sort map keys for determinism in tests. + // TODO(jba): replace with slices.Sorted(maps.Keys(fixes)) when go.mod >= 1.23. + var keys []string + for k := range fixes { + keys = append(keys, k) + } + slices.Sort(keys) + for _, sf := range keys { + edits := fixes[sf] found := false for _, vf := range ar.Files { if vf.Name == sf { @@ -279,9 +287,16 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns } else { // all suggested fixes are represented by a single file // TODO(adonovan): fix: this makes no sense if len(fixes) > 1. + // Sort map keys for determinism in tests. + // TODO(jba): replace with slices.Sorted(maps.Keys(fixes)) when go.mod >= 1.23. + var keys []string + for k := range fixes { + keys = append(keys, k) + } + slices.Sort(keys) var catchallEdits []diff.Edit - for _, edits := range fixes { - catchallEdits = append(catchallEdits, edits...) + for _, k := range keys { + catchallEdits = append(catchallEdits, fixes[k]...) } if err := applyDiffsAndCompare(orig, ar.Comment, catchallEdits, file.Name()); err != nil { diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index 5426a6a4b75..c52168913d0 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -123,6 +123,7 @@ func run(pass *analysis.Pass) (any, error) { rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program con := &goFixForwardConstFact{ RHSName: rhs.Name(), + RHSPkgName: rhs.Pkg().Name(), RHSPkgPath: rhs.Pkg().Path(), } if rhs.Pkg() == pass.Pkg { @@ -148,9 +149,11 @@ func run(pass *analysis.Pass) (any, error) { nodeFilter = []ast.Node{ (*ast.File)(nil), (*ast.CallExpr)(nil), + (*ast.SelectorExpr)(nil), (*ast.Ident)(nil), } var currentFile *ast.File + var currentSel *ast.SelectorExpr inspect.Preorder(nodeFilter, func(n ast.Node) { if file, ok := n.(*ast.File); ok { currentFile = file @@ -224,62 +227,87 @@ func run(pass *analysis.Pass) (any, error) { }) } - // TODO(jba): case *ast.SelectorExpr for RHSs that are qualified uses of constants. + case *ast.SelectorExpr: + currentSel = n case *ast.Ident: // If the identifier is a use of a forwardable constant, suggest forwarding it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { - incon, ok := forwardableConsts[con] + fcon, ok := forwardableConsts[con] if !ok { var fact goFixForwardConstFact if pass.ImportObjectFact(con, &fact) { - incon = &fact - forwardableConsts[con] = incon + fcon = &fact + forwardableConsts[con] = fcon } } - if incon == nil { + if fcon == nil { return // nope } - // - // We have an identifier A here (n), - // and an forwardable "const A = B" elsewhere (incon). + + // If n is qualified by a package identifier, we'll need the full selector expression. + var sel *ast.SelectorExpr + if currentSel != nil && n == currentSel.Sel { + sel = currentSel + currentSel = nil + } + + // We have an identifier A here (n), possibly qualified by a package identifier (sel.X), + // and a forwardable "const A = B" elsewhere (fcon). // Consider replacing A with B. + // Check that the expression we are inlining (B) means the same thing // (refers to the same object) in n's scope as it does in A's scope. - if incon.rhsObj != nil { - // Both expressions are in the current package. - // incon.rhsObj is the object referred to by B in the definition of A. + // If the RHS is not in the current package, AddImport will handle + // shadowing, so we only need to worry about when both expressions + // are in the current package. + if pass.Pkg.Path() == fcon.RHSPkgPath { + // fcon.rhsObj is the object referred to by B in the definition of A. scope := pass.TypesInfo.Scopes[currentFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope + _, obj := scope.LookupParent(fcon.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. - panic(fmt.Sprintf("no object for forwardable const %s RHS %s", n.Name, incon.RHSName)) + panic(fmt.Sprintf("no object for forwardable const %s RHS %s", n.Name, fcon.RHSName)) } - if obj != incon.rhsObj { - // "B" means something different here than at the forwardable const's scope + if obj != fcon.rhsObj { + // "B" means something different here than at the forwardable const's scope. return } - } else { - // TODO(jba): handle the cross-package case by checking the package ID. } importPrefix := "" - if incon.RHSPkgPath != con.Pkg().Path() { - importID := maybeAddImportPath(currentFile, incon.RHSPkgPath) + var edits []analysis.TextEdit + if fcon.RHSPkgPath != pass.Pkg.Path() { + // TODO(jba): fix AddImport so that it returns "." if an existing dot import will work. + // We will need to tell AddImport the name of the identifier we want to qualify (fcon.RHSName here). + importID, eds := analysisinternal.AddImport( + pass.TypesInfo, currentFile, n.Pos(), fcon.RHSPkgPath, fcon.RHSPkgName) importPrefix = importID + "." + edits = eds } - newText := importPrefix + incon.RHSName + var ( + pos = n.Pos() + end = n.End() + name = n.Name + ) + // Replace the entire SelectorExpr if there is one. + if sel != nil { + pos = sel.Pos() + end = sel.End() + name = sel.X.(*ast.Ident).Name + "." + n.Name + } + edits = append(edits, analysis.TextEdit{ + Pos: pos, + End: end, + NewText: []byte(importPrefix + fcon.RHSName), + }) pass.Report(analysis.Diagnostic{ - Pos: n.Pos(), - End: n.End(), - Message: fmt.Sprintf("Constant %s should be forwarded", n.Name), + Pos: pos, + End: end, + Message: fmt.Sprintf("Constant %s should be forwarded", name), SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Forward constant %s", n.Name), - TextEdits: []analysis.TextEdit{{ - Pos: n.Pos(), - End: n.End(), - NewText: []byte(newText), - }}, + Message: fmt.Sprintf("Forward constant %s", name), + TextEdits: edits, }}, }) } @@ -297,11 +325,6 @@ func hasFixDirective(cg *ast.CommentGroup, name string) bool { }) } -func maybeAddImportPath(f *ast.File, path string) string { - // TODO(jba): implement this in terms of analysisinternal.AddImport(info, file, pos, path, localname). - return "unimp" -} - // A goFixInlineFuncFact is exported for each function marked "//go:fix inline". // It holds information about the callee to support inlining. type goFixInlineFuncFact struct{ Callee *inline.Callee } @@ -315,6 +338,7 @@ type goFixForwardConstFact struct { // Information about "const LHSName = RHSName". RHSName string RHSPkgPath string + RHSPkgName string rhsObj types.Object // for current package } diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go b/internal/refactor/inline/analyzer/testdata/src/b/b.go index ab3cd2063e2..72d4748a8d9 100644 --- a/internal/refactor/inline/analyzer/testdata/src/b/b.go +++ b/internal/refactor/inline/analyzer/testdata/src/b/b.go @@ -1,9 +1,30 @@ package b import "a" +import . "c" func f() { a.One() // want `cannot inline call to a.One because body refers to non-exported one` new(a.T).Two() // want `Call of \(a.T\).Two should be inlined` } + +//go:fix forward +const in2 = a.Uno + +//go:fix forward +const in3 = C // c.C, by dot import + +func g() { + x := a.In1 // want `Constant a\.In1 should be forwarded` + + a := 1 + // Although the package identifier "a" is shadowed here, + // a second import of "a" will be added with a new package identifer. + x = in2 // want `Constant in2 should be forwarded` + + x = in3 // want `Constant in3 should be forwarded` + + _ = a + _ = x +} diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden b/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden index f2099efdfeb..fdc83c5199c 100644 --- a/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden +++ b/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden @@ -1,9 +1,36 @@ package b -import "a" +import a0 "a" + +import "c" + +import ( + "a" + . "c" +) func f() { a.One() // want `cannot inline call to a.One because body refers to non-exported one` _ = 2 // want `Call of \(a.T\).Two should be inlined` } + +//go:fix forward +const in2 = a.Uno + +//go:fix forward +const in3 = C // c.C, by dot import + +func g() { + x := a.Uno // want `Constant a\.In1 should be forwarded` + + a := 1 + // Although the package identifier "a" is shadowed here, + // a second import of "a" will be added with a new package identifer. + x = a0.Uno // want `Constant in2 should be forwarded` + + x = c.C // want `Constant in3 should be forwarded` + + _ = a + _ = x +} diff --git a/internal/refactor/inline/analyzer/testdata/src/c/c.go b/internal/refactor/inline/analyzer/testdata/src/c/c.go new file mode 100644 index 00000000000..36504b886a7 --- /dev/null +++ b/internal/refactor/inline/analyzer/testdata/src/c/c.go @@ -0,0 +1,5 @@ +package c + +// This package is dot-imported by package b. + +const C = 1 From bcb63f992e7f712877eddd2856b5267c8473ec74 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 5 Feb 2025 12:18:02 -0500 Subject: [PATCH 103/309] internal/refactor/inline/analyzer: redo directive parsing Redo how we report which directives are present to avoid repeating work and simplify the code. For golang/go#32816. Change-Id: I7a85de12181cf1816d75b8de9d6c015e9063001b Reviewed-on: https://go-review.googlesource.com/c/tools/+/646875 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/analyzer/analyzer.go | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go index c52168913d0..68ad7b928f1 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/internal/refactor/inline/analyzer/analyzer.go @@ -9,7 +9,6 @@ import ( "go/ast" "go/token" "go/types" - "slices" _ "embed" @@ -63,41 +62,46 @@ func run(pass *analysis.Pass) (any, error) { inspect.Preorder(nodeFilter, func(n ast.Node) { switch decl := n.(type) { case *ast.FuncDecl: - if hasFixDirective(decl.Doc, "inline") { - content, err := readFile(decl) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) - return - } - callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) - if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) - return - } - fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) - pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) - inlinableFuncs[fn] = callee - } else if hasFixDirective(decl.Doc, "forward") { + hasInline, hasForward := fixDirectives(decl.Doc) + if hasForward { pass.Reportf(decl.Doc.Pos(), "use //go:fix inline for functions") + return } + if !hasInline { + return + } + content, err := readFile(decl) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + return + } + callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) + if err != nil { + pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) + return + } + fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) + pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) + inlinableFuncs[fn] = callee case *ast.GenDecl: if decl.Tok != token.CONST { return } - if hasFixDirective(decl.Doc, "inline") { + declInline, declForward := fixDirectives(decl.Doc) + if declInline { pass.Reportf(decl.Doc.Pos(), "use //go:fix forward for constants") return } // Accept forward directives on the entire decl as well as individual specs. - declForward := hasFixDirective(decl.Doc, "forward") for _, spec := range decl.Specs { spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST - if hasFixDirective(spec.Doc, "inline") { + specInline, specForward := fixDirectives(spec.Doc) + if specInline { pass.Reportf(spec.Doc.Pos(), "use //go:fix forward for constants") return } - if declForward || hasFixDirective(spec.Doc, "forward") { + if declForward || specForward { for i, name := range spec.Names { if i >= len(spec.Values) { // Possible following an iota. @@ -317,12 +321,20 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } -// hasFixDirective reports whether cg has a directive -// of the form "//go:fix " + name. -func hasFixDirective(cg *ast.CommentGroup, name string) bool { - return slices.ContainsFunc(directives(cg), func(d *directive) bool { - return d.Tool == "go" && d.Name == "fix" && d.Args == name - }) +// fixDirectives reports the presence of "//go:fix inline" and "//go:fix forward" +// directives in the comments. +func fixDirectives(cg *ast.CommentGroup) (inline, forward bool) { + for _, d := range directives(cg) { + if d.Tool == "go" && d.Name == "fix" { + switch d.Args { + case "inline": + inline = true + case "forward": + forward = true + } + } + } + return } // A goFixInlineFuncFact is exported for each function marked "//go:fix inline". From 0a1a6c721b438c8de8760cb6f40c5c1de99a3312 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 6 Feb 2025 15:22:52 +0000 Subject: [PATCH 104/309] gopls/doc/release: document the new workspaceFiles option Add release notes for the new workspaceFiles option. Also, clean up existing release notes a bit. Updates golang/go#59625 Change-Id: I8969a48b2fddd9c60a77c038deb9090bd2e9eb98 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647295 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- gopls/doc/release/v0.18.0.md | 50 ++++++++++++++--------------- gopls/doc/settings.md | 8 +++-- gopls/internal/doc/api.json | 2 +- gopls/internal/settings/settings.go | 8 +++-- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index d22221d1b7e..0af26d11caf 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -1,34 +1,31 @@ # Configuration Changes -- The experimental `hoverKind=Structured` setting is no longer supported. + -- The `gc_details` code lens has been deleted. (It was previously - disabled by default.) This functionality is now available through - the `settings.toggleCompilerOptDetails` code action (documented - below), as code actions are better supported than code lenses across - a range of clients. +- The experimental `Structured` value for the `hoverKind` option is no longer + supported. - VS Code's special "Go: Toggle GC details" command continues to work. - -- The experimental `settings.semanticTokenTypes` configures the semantic token - types. It allows disabling types by setting each value to false. By default, - all types are enabled. +- The `gc_details` code lens has been deleted. (It was previously disabled by + default.) This functionality is now available through the + `toggleCompilerOptDetails` code action (documented below), as code + actions are better supported than code lenses across a range of clients. - The experimental `settings.semanticTokenModifiers` configures the semantic - token modifiers. It allows disabling modifiers by setting each value to false. - By default, all modifiers are enabled. + VS Code's special "Go: Toggle GC details" command continues to work. - The experimental `settings.noSemanticTokenString` and - `settings.noSemanticToken` settings are deprecated in favor of - `settings.semanticTokenTypes`. +- The experimental `semanticTokenTypes` and `semanticTokenModifiers` options + allow selectively disabling certain types of tokens or token modifiers in + `textDocument/semanticTokens` responses. - Users can set `settings.semanticTokenTypes[string] = false` to achieve the - same result as `settings.noSemanticTokenString`. The same applies to - `settings.noSemanticTokenNumber`. + These options supersede the `noSemanticString` and `noSemanticTokenNumber` + options, which are now deprecated. Users can instead set + `"semanticTokenTypes": {"string": false, "number": false}` to achieve the + same result. For now, gopls still honors `noSemanticTokenString` and + `noSemanticToken`, but will stop supporting them in a future release. - For now, gopls still honors `settings.noSemanticTokenString` and - `settings.noSemanticToken`, but will stop honoring the settings in the - upcoming release. +- The new `workspaceFiles` option allows configuring glob patterns matching + files that define the logical build of the workspace. This option is only + needed in environments that use a custom golang.org/x/tools/go/packages + driver. # New features @@ -69,9 +66,10 @@ this fashion (or with `%s` for the port) is passed to `net.Dial` or a related function, and offers a fix to use `net.JoinHostPort` instead. -## `unusedvariable` analyzer now on by default +## Other analyzer changes -This analyzer suggests deleting the unused variable declaration. +- The `unusedvariable` quickfix is now on by default. +- The `unusedparams` analyzer no longer reports finding for generated files. ## "Implementations" supports generics @@ -116,7 +114,7 @@ The Definition query now supports additional locations: ## Improvements to "Hover" When invoked on a return statement, hover reports the types of - the function's result variables. +the function's result variables. ## UX improvements to format strings diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index dc601ea8b17..d989b2d19b9 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -146,10 +146,12 @@ Default: `["ignore"]`. ### `workspaceFiles []string` -workspaceFiles configures the set of globs that match files defining the logical build of the current workspace. -Any on-disk changes to any files matching a glob specified here will trigger a reload of the workspace. +workspaceFiles configures the set of globs that match files defining the +logical build of the current workspace. Any on-disk changes to any files +matching a glob specified here will trigger a reload of the workspace. -This setting need only be customized in environments with a custom GOPACKAGESDRIVER. +This setting need only be customized in environments with a custom +GOPACKAGESDRIVER. Default: `[]`. diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 9379733ea5e..1e9e6c9a299 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -102,7 +102,7 @@ { "Name": "workspaceFiles", "Type": "[]string", - "Doc": "workspaceFiles configures the set of globs that match files defining the logical build of the current workspace.\nAny on-disk changes to any files matching a glob specified here will trigger a reload of the workspace.\n\nThis setting need only be customized in environments with a custom GOPACKAGESDRIVER.\n", + "Doc": "workspaceFiles configures the set of globs that match files defining the\nlogical build of the current workspace. Any on-disk changes to any files\nmatching a glob specified here will trigger a reload of the workspace.\n\nThis setting need only be customized in environments with a custom\nGOPACKAGESDRIVER.\n", "EnumKeys": { "ValueType": "", "Keys": null diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 038c814b1b7..8f33bdae96b 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -145,10 +145,12 @@ type BuildOptions struct { // This setting is only supported when gopls is built with Go 1.16 or later. StandaloneTags []string - // WorkspaceFiles configures the set of globs that match files defining the logical build of the current workspace. - // Any on-disk changes to any files matching a glob specified here will trigger a reload of the workspace. + // WorkspaceFiles configures the set of globs that match files defining the + // logical build of the current workspace. Any on-disk changes to any files + // matching a glob specified here will trigger a reload of the workspace. // - // This setting need only be customized in environments with a custom GOPACKAGESDRIVER. + // This setting need only be customized in environments with a custom + // GOPACKAGESDRIVER. WorkspaceFiles []string } From 73edff8ef8e91d0c00dbf85f5f2fdc8e20be69f3 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 6 Feb 2025 16:58:03 +0000 Subject: [PATCH 105/309] gopls/internal/cache/testfuncs: fix matching of test names Fix the logic to match valid test function names. The testing package specifies only that the suffix must not start with a lowercase letter. Fixes golang/go#70929 Change-Id: Ica7a2df057705856da0301467a54167a1d8cbc44 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647296 LUCI-TryBot-Result: Go LUCI Reviewed-by: Hongxiang Jiang --- gopls/internal/cache/testfuncs/tests.go | 35 ++++++++++++------- .../integration/workspace/packages_test.go | 6 ++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/gopls/internal/cache/testfuncs/tests.go b/gopls/internal/cache/testfuncs/tests.go index fca25e5db19..1182795b37b 100644 --- a/gopls/internal/cache/testfuncs/tests.go +++ b/gopls/internal/cache/testfuncs/tests.go @@ -8,8 +8,9 @@ import ( "go/ast" "go/constant" "go/types" - "regexp" "strings" + "unicode" + "unicode/utf8" "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/protocol" @@ -234,13 +235,6 @@ func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr return nil, nil } -var ( - reTest = regexp.MustCompile(`^Test([A-Z]|$)`) - reBenchmark = regexp.MustCompile(`^Benchmark([A-Z]|$)`) - reFuzz = regexp.MustCompile(`^Fuzz([A-Z]|$)`) - reExample = regexp.MustCompile(`^Example([A-Z]|$)`) -) - // isTestOrExample reports whether the given func is a testing func or an // example func (or neither). isTestOrExample returns (true, false) for testing // funcs, (false, true) for example funcs, and (false, false) otherwise. @@ -248,7 +242,7 @@ func isTestOrExample(fn *types.Func) (isTest, isExample bool) { sig := fn.Type().(*types.Signature) if sig.Params().Len() == 0 && sig.Results().Len() == 0 { - return false, reExample.MatchString(fn.Name()) + return false, isTestName(fn.Name(), "Example") } kind, ok := testKind(sig) @@ -257,16 +251,33 @@ func isTestOrExample(fn *types.Func) (isTest, isExample bool) { } switch kind.Name() { case "T": - return reTest.MatchString(fn.Name()), false + return isTestName(fn.Name(), "Test"), false case "B": - return reBenchmark.MatchString(fn.Name()), false + return isTestName(fn.Name(), "Benchmark"), false case "F": - return reFuzz.MatchString(fn.Name()), false + return isTestName(fn.Name(), "Fuzz"), false default: return false, false // "can't happen" (see testKind) } } +// isTestName reports whether name is a valid test name for the test kind +// indicated by the given prefix ("Test", "Benchmark", etc.). +// +// Adapted from go/analysis/passes/tests. +func isTestName(name, prefix string) bool { + suffix, ok := strings.CutPrefix(name, prefix) + if !ok { + return false + } + if len(suffix) == 0 { + // "Test" is ok. + return true + } + r, _ := utf8.DecodeRuneInString(suffix) + return !unicode.IsLower(r) +} + // testKind returns the parameter type TypeName of a test, benchmark, or fuzz // function (one of testing.[TBF]). func testKind(sig *types.Signature) (*types.TypeName, bool) { diff --git a/gopls/internal/test/integration/workspace/packages_test.go b/gopls/internal/test/integration/workspace/packages_test.go index 7ee19bcca54..fdee21d822f 100644 --- a/gopls/internal/test/integration/workspace/packages_test.go +++ b/gopls/internal/test/integration/workspace/packages_test.go @@ -119,12 +119,14 @@ package foo import "testing" func Foo() func TestFoo2(t *testing.T) +func foo() -- foo_test.go -- package foo import "testing" func TestFoo(t *testing.T) func Issue70927(*error) +func Test_foo(t *testing.T) -- foo2_test.go -- package foo_test @@ -164,6 +166,7 @@ func Test(*testing.T) URI: env.Editor.DocumentURI("foo_test.go"), Tests: []command.TestCase{ {Name: "TestFoo"}, + {Name: "Test_foo"}, }, }, }, @@ -188,6 +191,7 @@ func Test(*testing.T) }, }, []string{ "func TestFoo(t *testing.T)", + "func Test_foo(t *testing.T)", "func TestBar(t *testing.T) {}", }) }) @@ -242,6 +246,7 @@ func Test(*testing.T) URI: env.Editor.DocumentURI("foo_test.go"), Tests: []command.TestCase{ {Name: "TestFoo"}, + {Name: "Test_foo"}, }, }, }, @@ -282,6 +287,7 @@ func Test(*testing.T) }, }, []string{ "func TestFoo(t *testing.T)", + "func Test_foo(t *testing.T)", "func TestBaz(*testing.T)", "func BenchmarkBaz(*testing.B)", "func FuzzBaz(*testing.F)", From bf4db91ca9586ce3f04865a04cd444ad692f519c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 5 Feb 2025 12:33:18 -0500 Subject: [PATCH 106/309] gopls/internal/analysis/modernize: for i := 0; i < n; i++ -> range n This CL adds a modernizer for 3-clause for loops that offers a fix to replace them with go1.22 "range n" loops. Updates golang/go#70815 Change-Id: I347179b8a308f380a9a894aa811ced66f7605df1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/646916 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/ast/inspector/inspector.go | 2 +- gopls/doc/analyzers.md | 4 +- gopls/internal/analysis/modernize/doc.go | 4 +- .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + gopls/internal/analysis/modernize/rangeint.go | 163 ++++++++++++++++++ .../analysis/modernize/slicescontains.go | 5 + .../testdata/src/rangeint/rangeint.go | 37 ++++ .../testdata/src/rangeint/rangeint.go.golden | 37 ++++ gopls/internal/doc/api.json | 4 +- 10 files changed, 253 insertions(+), 5 deletions(-) create mode 100644 gopls/internal/analysis/modernize/rangeint.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index 8f6a510f248..0d5050fe405 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -279,6 +279,6 @@ func (v *visitor) pop(node ast.Node) { node: node, typ: current.typAccum, index: current.index, - parent: int32(current.edgeKindAndIndex), // see [unpackEdgeKindAndIndex] + parent: current.edgeKindAndIndex, // see [unpackEdgeKindAndIndex] }) } diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index fa882243f35..a6cf89df27e 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -493,9 +493,11 @@ existing code by using more modern features of Go, such as: added in go1.19; - replacing uses of context.WithCancel in tests with t.Context, added in go1.24; - - replacing omitempty by omitzero on structs, added in go 1.24; + - replacing omitempty by omitzero on structs, added in go1.24; - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21 + - replacing a 3-clause for i := 0; i < n; i++ {} loop by + for i := range n {}, added in go1.22; Default: on. diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 78cc6a6d11f..6a247feccf4 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -25,7 +25,9 @@ // added in go1.19; // - replacing uses of context.WithCancel in tests with t.Context, added in // go1.24; -// - replacing omitempty by omitzero on structs, added in go 1.24; +// - replacing omitempty by omitzero on structs, added in go1.24; // - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), // added in go1.21 +// - replacing a 3-clause for i := 0; i < n; i++ {} loop by +// for i := range n {}, added in go1.22; package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 9c1be95a7fd..96ab3131833 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -67,6 +67,7 @@ func run(pass *analysis.Pass) (any, error) { mapsloop(pass) minmax(pass) omitzero(pass) + rangeint(pass) slicescontains(pass) slicesdelete(pass) sortslice(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 4710440b6a4..7e375c1c24c 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -20,6 +20,7 @@ func Test(t *testing.T) { "mapsloop", "minmax", "omitzero", + "rangeint", "slicescontains", "slicesdelete", "sortslice", diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go new file mode 100644 index 00000000000..c36203cef06 --- /dev/null +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -0,0 +1,163 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "fmt" + "go/ast" + "go/token" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" +) + +// rangeint offers a fix to replace a 3-clause 'for' loop: +// +// for i := 0; i < limit; i++ {} +// +// by a range loop with an integer operand: +// +// for i := range limit {} +// +// Variants: +// - The ':=' may be replaced by '='. +// - The fix may remove "i :=" if it would become unused. +// +// Restrictions: +// - The variable i must not be assigned or address-taken within the +// loop, because a "for range int" loop does not respect assignments +// to the loop index. +// - The limit must not be b.N, to avoid redundancy with bloop's fixes. +// +// Caveats: +// - The fix will cause the limit expression to be evaluated exactly +// once, instead of once per iteration. The limit may be a function call +// (e.g. seq.Len()). The fix may change the cardinality of side effects. +func rangeint(pass *analysis.Pass) { + info := pass.TypesInfo + + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.22") { + nextLoop: + for curLoop := range curFile.Preorder((*ast.ForStmt)(nil)) { + loop := curLoop.Node().(*ast.ForStmt) + if init, ok := loop.Init.(*ast.AssignStmt); ok && + isSimpleAssign(init) && + is[*ast.Ident](init.Lhs[0]) && + isZeroLiteral(init.Rhs[0]) { + // Have: for i = 0; ... (or i := 0) + index := init.Lhs[0].(*ast.Ident) + + if compare, ok := loop.Cond.(*ast.BinaryExpr); ok && + compare.Op == token.LSS && + equalSyntax(compare.X, init.Lhs[0]) { + // Have: for i = 0; i < limit; ... {} + limit := compare.Y + + // Skip loops up to b.N in benchmarks; see [bloop]. + if sel, ok := limit.(*ast.SelectorExpr); ok && + sel.Sel.Name == "N" && + analysisinternal.IsPointerToNamed(info.TypeOf(sel.X), "testing", "B") { + continue // skip b.N + } + + if inc, ok := loop.Post.(*ast.IncDecStmt); ok && + inc.Tok == token.INC && + equalSyntax(compare.X, inc.X) { + // Have: for i = 0; i < limit; i++ {} + + // Find references to i within the loop body. + v := info.Defs[index] + used := false + for curId := range curLoop.Child(loop.Body).Preorder((*ast.Ident)(nil)) { + id := curId.Node().(*ast.Ident) + if info.Uses[id] == v { + used = true + + // Reject if any is an l-value (assigned or address-taken): + // a "for range int" loop does not respect assignments to + // the loop variable. + if isScalarLvalue(curId) { + continue nextLoop + } + } + } + + // If i is no longer used, delete "i := ". + var edits []analysis.TextEdit + if !used && init.Tok == token.DEFINE { + edits = append(edits, analysis.TextEdit{ + Pos: index.Pos(), + End: init.Rhs[0].Pos(), + }) + } + + pass.Report(analysis.Diagnostic{ + Pos: init.Pos(), + End: inc.End(), + Category: "rangeint", + Message: "for loop can be modernized using range over int", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Replace for loop with range %s", + analysisinternal.Format(pass.Fset, limit)), + TextEdits: append(edits, []analysis.TextEdit{ + // for i := 0; i < limit; i++ {} + // ----- --- + // ------- + // for i := range limit {} + { + Pos: init.Rhs[0].Pos(), + End: limit.Pos(), + NewText: []byte("range "), + }, + { + Pos: limit.End(), + End: inc.End(), + }, + }...), + }}, + }) + } + } + } + } + } +} + +// isScalarLvalue reports whether the specified identifier is +// address-taken or appears on the left side of an assignment. +// +// This function is valid only for scalars (x = ...), +// not for aggregates (x.a[i] = ...) +func isScalarLvalue(curId cursor.Cursor) bool { + // Unfortunately we can't simply use info.Types[e].Assignable() + // as it is always true for a variable even when that variable is + // used only as an r-value. So we must inspect enclosing syntax. + + cur := curId + + // Strip enclosing parens. + ek, _ := cur.Edge() + for ek == edge.ParenExpr_X { + cur = cur.Parent() + ek, _ = cur.Edge() + } + + switch ek { + case edge.AssignStmt_Lhs: + return true // i = j + case edge.IncDecStmt_X: + return true // i++, i-- + case edge.UnaryExpr_X: + if cur.Parent().Node().(*ast.UnaryExpr).Op == token.AND { + return true // &i + } + } + return false +} diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index dc0aa613a50..d860d642743 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -179,6 +179,11 @@ func slicescontains(pass *analysis.Pass) { } // Last statement of body must return/break out of the loop. + // + // TODO(adonovan): opt:consider avoiding FindNode with new API of form: + // curRange.Get(edge.RangeStmt_Body, -1). + // Get(edge.BodyStmt_List, 0). + // Get(edge.IfStmt_Body) curBody, _ := curRange.FindNode(body) curLastStmt, _ := curBody.LastChild() diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go new file mode 100644 index 00000000000..e17dccac9d0 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -0,0 +1,37 @@ +package rangeint + +func _(i int, s struct{ i int }) { + for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" + println(i) + } + for i = 0; i < f(); i++ { // want "for loop can be modernized using range over int" + } + for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" + // i unused within loop + } + + // nope + for i := 0; i < 10; { // nope: missing increment + } + for i := 0; i < 10; i-- { // nope: negative increment + } + for i := 0; ; i++ { // nope: missing comparison + } + for i := 0; i <= 10; i++ { // nope: wrong comparison + } + for ; i < 10; i++ { // nope: missing init + } + for s.i = 0; s.i < 10; s.i++ { // nope: not an ident + } + for i := 0; i < 10; i++ { // nope: takes address of i + println(&i) + } + for i := 0; i < 10; i++ { // nope: increments i + i++ + } + for i := 0; i < 10; i++ { // nope: assigns i + i = 8 + } +} + +func f() int { return 0 } diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden new file mode 100644 index 00000000000..5a76229c858 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -0,0 +1,37 @@ +package rangeint + +func _(i int, s struct{ i int }) { + for i := range 10 { // want "for loop can be modernized using range over int" + println(i) + } + for i = range f() { // want "for loop can be modernized using range over int" + } + for range 10 { // want "for loop can be modernized using range over int" + // i unused within loop + } + + // nope + for i := 0; i < 10; { // nope: missing increment + } + for i := 0; i < 10; i-- { // nope: negative increment + } + for i := 0; ; i++ { // nope: missing comparison + } + for i := 0; i <= 10; i++ { // nope: wrong comparison + } + for ; i < 10; i++ { // nope: missing init + } + for s.i = 0; s.i < 10; s.i++ { // nope: not an ident + } + for i := 0; i < 10; i++ { // nope: takes address of i + println(&i) + } + for i := 0; i < 10; i++ { // nope: increments i + i++ + } + for i := 0; i < 10; i++ { // nope: assigns i + i = 8 + } +} + +func f() int { return 0 } diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 1e9e6c9a299..1ae2e0e4c17 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -510,7 +510,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;", "Default": "true" }, { @@ -1189,7 +1189,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go 1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From 03a72dbe14a62e4a43012271697e52ddcc6d3158 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 6 Feb 2025 13:13:55 -0500 Subject: [PATCH 107/309] gopls/internal/analysis/gofix: move and rename Rename the analyzer for "//go:fix" directives to "gofix", and move it next to the other gopls analyzers from its previous location under internal/refactor. For golang/go#32816. Change-Id: I084de477d104bda916c05ecb13ed81daee96851a Reviewed-on: https://go-review.googlesource.com/c/tools/+/647298 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Reviewed-by: Alan Donovan --- gopls/doc/analyzers.md | 22 +++++++++---------- .../internal/analysis/gofix}/directive.go | 2 +- .../internal/analysis/gofix}/doc.go | 10 ++++----- .../internal/analysis/gofix/gofix.go | 8 +++---- .../internal/analysis/gofix/gofix_test.go | 6 ++--- .../internal/analysis/gofix}/main.go | 6 ++--- .../analysis/gofix}/testdata/src/a/a.go | 0 .../gofix}/testdata/src/a/a.go.golden | 0 .../analysis/gofix}/testdata/src/b/b.go | 0 .../gofix}/testdata/src/b/b.go.golden | 0 .../analysis/gofix}/testdata/src/c/c.go | 0 gopls/internal/doc/api.json | 22 +++++++++---------- gopls/internal/settings/analysis.go | 4 ++-- 13 files changed, 40 insertions(+), 40 deletions(-) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/directive.go (99%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/doc.go (90%) rename internal/refactor/inline/analyzer/analyzer.go => gopls/internal/analysis/gofix/gofix.go (98%) rename internal/refactor/inline/analyzer/analyzer_test.go => gopls/internal/analysis/gofix/gofix_test.go (72%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/main.go (64%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/testdata/src/a/a.go (100%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/testdata/src/a/a.go.golden (100%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/testdata/src/b/b.go (100%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/testdata/src/b/b.go.golden (100%) rename {internal/refactor/inline/analyzer => gopls/internal/analysis/gofix}/testdata/src/c/c.go (100%) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index a6cf89df27e..8764791561d 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -290,6 +290,17 @@ Default: on. Package documentation: [framepointer](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer) + +## `gofix`: apply fixes based on go:fix comment directives + + +The gofix analyzer inlines functions that are marked for inlining +and forwards constants that are marked for forwarding. + +Default: on. + +Package documentation: [gofix](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix) + ## `hostport`: check format of addresses passed to net.Dial @@ -376,17 +387,6 @@ Default: on. Package documentation: [infertypeargs](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/infertypeargs) - -## `inline`: inline functions and forward constants - - -The inline analyzer inlines functions that are marked for inlining -and forwards constants that are marked for forwarding. - -Default: on. - -Package documentation: [inline](https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer) - ## `loopclosure`: check references to loop variables from within nested functions diff --git a/internal/refactor/inline/analyzer/directive.go b/gopls/internal/analysis/gofix/directive.go similarity index 99% rename from internal/refactor/inline/analyzer/directive.go rename to gopls/internal/analysis/gofix/directive.go index 4e605021307..796feb5189e 100644 --- a/internal/refactor/inline/analyzer/directive.go +++ b/gopls/internal/analysis/gofix/directive.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package analyzer +package gofix import ( "go/ast" diff --git a/internal/refactor/inline/analyzer/doc.go b/gopls/internal/analysis/gofix/doc.go similarity index 90% rename from internal/refactor/inline/analyzer/doc.go rename to gopls/internal/analysis/gofix/doc.go index a4ac0f30093..c3c453f841b 100644 --- a/internal/refactor/inline/analyzer/doc.go +++ b/gopls/internal/analysis/gofix/doc.go @@ -3,16 +3,16 @@ // license that can be found in the LICENSE file. /* -Package analyzer defines an Analyzer that inlines calls to functions +Package gofix defines an Analyzer that inlines calls to functions marked with a "//go:fix inline" doc comment, and forwards uses of constants marked with a "//go:fix forward" doc comment. -# Analyzer inline +# Analyzer gofix -inline: inline functions and forward constants +gofix: apply fixes based on go:fix comment directives -The inline analyzer inlines functions that are marked for inlining +The gofix analyzer inlines functions that are marked for inlining and forwards constants that are marked for forwarding. # Functions @@ -80,4 +80,4 @@ or before a group, applying to every constant in the group: The proposal https://go.dev/issue/32816 introduces the "//go:fix" directives. */ -package analyzer +package gofix diff --git a/internal/refactor/inline/analyzer/analyzer.go b/gopls/internal/analysis/gofix/gofix.go similarity index 98% rename from internal/refactor/inline/analyzer/analyzer.go rename to gopls/internal/analysis/gofix/gofix.go index 68ad7b928f1..90067ee3d3d 100644 --- a/internal/refactor/inline/analyzer/analyzer.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package analyzer +package gofix import ( "fmt" @@ -26,9 +26,9 @@ import ( var doc string var Analyzer = &analysis.Analyzer{ - Name: "inline", - Doc: analysisinternal.MustExtractDoc(doc, "inline"), - URL: "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", + Name: "gofix", + Doc: analysisinternal.MustExtractDoc(doc, "gofix"), + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", Run: run, FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixForwardConstFact)}, Requires: []*analysis.Analyzer{inspect.Analyzer}, diff --git a/internal/refactor/inline/analyzer/analyzer_test.go b/gopls/internal/analysis/gofix/gofix_test.go similarity index 72% rename from internal/refactor/inline/analyzer/analyzer_test.go rename to gopls/internal/analysis/gofix/gofix_test.go index 5ad85cfb821..32bd87b6cd2 100644 --- a/internal/refactor/inline/analyzer/analyzer_test.go +++ b/gopls/internal/analysis/gofix/gofix_test.go @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package analyzer_test +package gofix_test import ( "testing" "golang.org/x/tools/go/analysis/analysistest" - inlineanalyzer "golang.org/x/tools/internal/refactor/inline/analyzer" + "golang.org/x/tools/gopls/internal/analysis/gofix" ) func TestAnalyzer(t *testing.T) { - analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), inlineanalyzer.Analyzer, "a", "b") + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), gofix.Analyzer, "a", "b") } diff --git a/internal/refactor/inline/analyzer/main.go b/gopls/internal/analysis/gofix/main.go similarity index 64% rename from internal/refactor/inline/analyzer/main.go rename to gopls/internal/analysis/gofix/main.go index 4be223a80d6..fde633f2f62 100644 --- a/internal/refactor/inline/analyzer/main.go +++ b/gopls/internal/analysis/gofix/main.go @@ -8,12 +8,12 @@ // The inline command applies the inliner to the specified packages of // Go source code. Run with: // -// $ go run ./internal/refactor/inline/analyzer/main.go -fix packages... +// $ go run ./internal/analysis/gofix/main.go -fix packages... package main import ( "golang.org/x/tools/go/analysis/singlechecker" - inlineanalyzer "golang.org/x/tools/internal/refactor/inline/analyzer" + "golang.org/x/tools/gopls/internal/analysis/gofix" ) -func main() { singlechecker.Main(inlineanalyzer.Analyzer) } +func main() { singlechecker.Main(gofix.Analyzer) } diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go similarity index 100% rename from internal/refactor/inline/analyzer/testdata/src/a/a.go rename to gopls/internal/analysis/gofix/testdata/src/a/a.go diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden similarity index 100% rename from internal/refactor/inline/analyzer/testdata/src/a/a.go.golden rename to gopls/internal/analysis/gofix/testdata/src/a/a.go.golden diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go b/gopls/internal/analysis/gofix/testdata/src/b/b.go similarity index 100% rename from internal/refactor/inline/analyzer/testdata/src/b/b.go rename to gopls/internal/analysis/gofix/testdata/src/b/b.go diff --git a/internal/refactor/inline/analyzer/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden similarity index 100% rename from internal/refactor/inline/analyzer/testdata/src/b/b.go.golden rename to gopls/internal/analysis/gofix/testdata/src/b/b.go.golden diff --git a/internal/refactor/inline/analyzer/testdata/src/c/c.go b/gopls/internal/analysis/gofix/testdata/src/c/c.go similarity index 100% rename from internal/refactor/inline/analyzer/testdata/src/c/c.go rename to gopls/internal/analysis/gofix/testdata/src/c/c.go diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 1ae2e0e4c17..5079edc10a6 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -473,6 +473,11 @@ "Doc": "report assembly that clobbers the frame pointer before saving it", "Default": "true" }, + { + "Name": "\"gofix\"", + "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", + "Default": "true" + }, { "Name": "\"hostport\"", "Doc": "check format of addresses passed to net.Dial\n\nThis analyzer flags code that produce network address strings using\nfmt.Sprintf, as in this example:\n\n addr := fmt.Sprintf(\"%s:%d\", host, 12345) // \"will not work with IPv6\"\n ...\n conn, err := net.Dial(\"tcp\", addr) // \"when passed to dial here\"\n\nThe analyzer suggests a fix to use the correct approach, a call to\nnet.JoinHostPort:\n\n addr := net.JoinHostPort(host, \"12345\")\n ...\n conn, err := net.Dial(\"tcp\", addr)\n\nA similar diagnostic and fix are produced for a format string of \"%s:%s\".\n", @@ -493,11 +498,6 @@ "Doc": "check for unnecessary type arguments in call expressions\n\nExplicit type arguments may be omitted from call expressions if they can be\ninferred from function arguments, or from other type arguments:\n\n\tfunc f[T any](T) {}\n\t\n\tfunc _() {\n\t\tf[string](\"foo\") // string could be inferred\n\t}\n", "Default": "true" }, - { - "Name": "\"inline\"", - "Doc": "inline functions and forward constants\n\nThe inline analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", - "Default": "true" - }, { "Name": "\"loopclosure\"", "Doc": "check references to loop variables from within nested functions\n\nThis analyzer reports places where a function literal references the\niteration variable of an enclosing loop, and the loop calls the function\nin such a way (e.g. with go or defer) that it may outlive the loop\niteration and possibly observe the wrong value of the variable.\n\nNote: An iteration variable can only outlive a loop iteration in Go versions \u003c=1.21.\nIn Go 1.22 and later, the loop variable lifetimes changed to create a new\niteration variable per loop iteration. (See go.dev/issue/60078.)\n\nIn this example, all the deferred functions run after the loop has\ncompleted, so all observe the final value of v [\u003cgo1.22].\n\n\tfor _, v := range list {\n\t defer func() {\n\t use(v) // incorrect\n\t }()\n\t}\n\nOne fix is to create a new variable for each iteration of the loop:\n\n\tfor _, v := range list {\n\t v := v // new var per iteration\n\t defer func() {\n\t use(v) // ok\n\t }()\n\t}\n\nAfter Go version 1.22, the previous two for loops are equivalent\nand both are correct.\n\nThe next example uses a go statement and has a similar problem [\u003cgo1.22].\nIn addition, it has a data race because the loop updates v\nconcurrent with the goroutines accessing it.\n\n\tfor _, v := range elem {\n\t go func() {\n\t use(v) // incorrect, and a data race\n\t }()\n\t}\n\nA fix is the same as before. The checker also reports problems\nin goroutines started by golang.org/x/sync/errgroup.Group.\nA hard-to-spot variant of this form is common in parallel tests:\n\n\tfunc Test(t *testing.T) {\n\t for _, test := range tests {\n\t t.Run(test.name, func(t *testing.T) {\n\t t.Parallel()\n\t use(test) // incorrect, and a data race\n\t })\n\t }\n\t}\n\nThe t.Parallel() call causes the rest of the function to execute\nconcurrent with the loop [\u003cgo1.22].\n\nThe analyzer reports references only in the last statement,\nas it is not deep enough to understand the effects of subsequent\nstatements that might render the reference benign.\n(\"Last statement\" is defined recursively in compound\nstatements such as if, switch, and select.)\n\nSee: https://golang.org/doc/go_faq.html#closures_and_goroutines", @@ -1145,6 +1145,12 @@ "URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer", "Default": true }, + { + "Name": "gofix", + "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", + "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + "Default": true + }, { "Name": "hostport", "Doc": "check format of addresses passed to net.Dial\n\nThis analyzer flags code that produce network address strings using\nfmt.Sprintf, as in this example:\n\n addr := fmt.Sprintf(\"%s:%d\", host, 12345) // \"will not work with IPv6\"\n ...\n conn, err := net.Dial(\"tcp\", addr) // \"when passed to dial here\"\n\nThe analyzer suggests a fix to use the correct approach, a call to\nnet.JoinHostPort:\n\n addr := net.JoinHostPort(host, \"12345\")\n ...\n conn, err := net.Dial(\"tcp\", addr)\n\nA similar diagnostic and fix are produced for a format string of \"%s:%s\".\n", @@ -1169,12 +1175,6 @@ "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/infertypeargs", "Default": true }, - { - "Name": "inline", - "Doc": "inline functions and forward constants\n\nThe inline analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", - "URL": "https://pkg.go.dev/golang.org/x/tools/internal/refactor/inline/analyzer", - "Default": true - }, { "Name": "loopclosure", "Doc": "check references to loop variables from within nested functions\n\nThis analyzer reports places where a function literal references the\niteration variable of an enclosing loop, and the loop calls the function\nin such a way (e.g. with go or defer) that it may outlive the loop\niteration and possibly observe the wrong value of the variable.\n\nNote: An iteration variable can only outlive a loop iteration in Go versions \u003c=1.21.\nIn Go 1.22 and later, the loop variable lifetimes changed to create a new\niteration variable per loop iteration. (See go.dev/issue/60078.)\n\nIn this example, all the deferred functions run after the loop has\ncompleted, so all observe the final value of v [\u003cgo1.22].\n\n\tfor _, v := range list {\n\t defer func() {\n\t use(v) // incorrect\n\t }()\n\t}\n\nOne fix is to create a new variable for each iteration of the loop:\n\n\tfor _, v := range list {\n\t v := v // new var per iteration\n\t defer func() {\n\t use(v) // ok\n\t }()\n\t}\n\nAfter Go version 1.22, the previous two for loops are equivalent\nand both are correct.\n\nThe next example uses a go statement and has a similar problem [\u003cgo1.22].\nIn addition, it has a data race because the loop updates v\nconcurrent with the goroutines accessing it.\n\n\tfor _, v := range elem {\n\t go func() {\n\t use(v) // incorrect, and a data race\n\t }()\n\t}\n\nA fix is the same as before. The checker also reports problems\nin goroutines started by golang.org/x/sync/errgroup.Group.\nA hard-to-spot variant of this form is common in parallel tests:\n\n\tfunc Test(t *testing.T) {\n\t for _, test := range tests {\n\t t.Run(test.name, func(t *testing.T) {\n\t t.Parallel()\n\t use(test) // incorrect, and a data race\n\t })\n\t }\n\t}\n\nThe t.Parallel() call causes the rest of the function to execute\nconcurrent with the loop [\u003cgo1.22].\n\nThe analyzer reports references only in the last statement,\nas it is not deep enough to understand the effects of subsequent\nstatements that might render the reference benign.\n(\"Last statement\" is defined recursively in compound\nstatements such as if, switch, and select.)\n\nSee: https://golang.org/doc/go_faq.html#closures_and_goroutines", diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index cd0254e5886..5ba8bdd06b0 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -49,6 +49,7 @@ import ( "golang.org/x/tools/gopls/internal/analysis/deprecated" "golang.org/x/tools/gopls/internal/analysis/embeddirective" "golang.org/x/tools/gopls/internal/analysis/fillreturns" + "golang.org/x/tools/gopls/internal/analysis/gofix" "golang.org/x/tools/gopls/internal/analysis/hostport" "golang.org/x/tools/gopls/internal/analysis/infertypeargs" "golang.org/x/tools/gopls/internal/analysis/modernize" @@ -62,7 +63,6 @@ import ( "golang.org/x/tools/gopls/internal/analysis/unusedvariable" "golang.org/x/tools/gopls/internal/analysis/yield" "golang.org/x/tools/gopls/internal/protocol" - inline "golang.org/x/tools/internal/refactor/inline/analyzer" ) // Analyzer augments a [analysis.Analyzer] with additional LSP configuration. @@ -211,7 +211,7 @@ func init() { severity: protocol.SeverityInformation, }, // other simplifiers - {analyzer: inline.Analyzer, severity: protocol.SeverityHint}, + {analyzer: gofix.Analyzer, severity: protocol.SeverityHint}, {analyzer: infertypeargs.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedparams.Analyzer, severity: protocol.SeverityInformation}, {analyzer: unusedfunc.Analyzer, severity: protocol.SeverityInformation}, From 5a1ba4d16cb98c7416084cc0d4c669f39752b0f8 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 5 Feb 2025 13:03:33 -0500 Subject: [PATCH 108/309] gopls/doc/release/v0.18.0: describe inline analyzer Change-Id: I0ea0230afa048d4d17c2fef4b35e43f141d75b65 Reviewed-on: https://go-review.googlesource.com/c/tools/+/646915 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- gopls/doc/release/v0.18.0.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 0af26d11caf..9df179390f7 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -71,6 +71,33 @@ instead. - The `unusedvariable` quickfix is now on by default. - The `unusedparams` analyzer no longer reports finding for generated files. +## New `gofix` analyzer + +Gopls now reports when a function call should be inlined or a use of a constant +should be forwarded. +These diagnostics and the associated code actions are triggered by "//go:fix" +directives at the function and constant definitions. +(See [the go:fix proposal](https://go.dev/issue/32816).) + +For example, consider a package `intmath` with a function `Square(int) int`. +Later the more general `Pow(int, int) int` is introduced, and `Square` is deprecated +in favor of calling `Pow` with a second argument of 2. The author of `intmath` +can write this: +``` +//go:fix inline +func Square(x int) int { return Pow(x, 2) } +``` +If gopls sees a call to `intmath.Square` in your code, it will suggest inlining +it, and will offer a code action to do so. + +The same feature works for constants, only the directive is "//go:fix forward". +With a constant definition like this: +``` +//go:fix forward +const Ptr = Pointer +``` +gopls will suggest replacing `Ptr` in your code with `Pointer`. + ## "Implementations" supports generics At long last, the "Go to Implementations" feature now fully supports From 0fd02ca54dd02754183c055477d98046a278ef6f Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 2 Feb 2025 12:29:10 -0500 Subject: [PATCH 109/309] gopls/internal/telemetry/cmd/stacks: tweak IgnoreSymbolContains Older gopls binaries had a different directory layout. Change-Id: I9dfc57831f0f298d2c897c5ee908d79604dd4d2c Reviewed-on: https://go-review.googlesource.com/c/tools/+/645956 Commit-Queue: Alan Donovan Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/telemetry/cmd/stacks/stacks.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 64b71606272..7cb20012657 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -150,6 +150,7 @@ var programs = map[string]ProgramConfig{ MatchSymbolPrefix: "golang.org/x/tools/gopls/", IgnoreSymbolContains: []string{ "internal/util/bug.", + "internal/bug.", // former name in gopls/0.14.2 }, }, "cmd/compile": { From 208870315c030e71b9d64f09217fc60a5f6ba854 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 6 Feb 2025 15:09:12 -0500 Subject: [PATCH 110/309] gopls/internal/util/moreiters: iterator functions Add the moreiters package for additional iterator-related features. Its initial contents is the `First` function, for getting the first value of an iterator. Change-Id: Ic8db04adf793fbda92025639640a660ef7efc453 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647356 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/util/moreiters/iters.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 gopls/internal/util/moreiters/iters.go diff --git a/gopls/internal/util/moreiters/iters.go b/gopls/internal/util/moreiters/iters.go new file mode 100644 index 00000000000..e4d83ae8618 --- /dev/null +++ b/gopls/internal/util/moreiters/iters.go @@ -0,0 +1,16 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package moreiters + +import "iter" + +// First returns the first value of seq and true. +// If seq is empty, it returns the zero value of T and false. +func First[T any](seq iter.Seq[T]) (z T, ok bool) { + for t := range seq { + return t, true + } + return z, false +} From 0dc10dc85fe82379abc8f9568a282a3dbcba13bf Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 6 Feb 2025 14:44:02 -0500 Subject: [PATCH 111/309] gopls/internal/analysis/gofix: use cursor API Use a cursor for Pass 2, to simplify the code. For golang/go#32816. Change-Id: Ib7ea08636d0cb2bb6274aee4767343fcc98361c7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647299 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 60 ++++++++++++-------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 90067ee3d3d..7021d5092e7 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -16,7 +16,10 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/refactor/inline" "golang.org/x/tools/internal/typesinternal" @@ -51,6 +54,12 @@ func run(pass *analysis.Pass) (any, error) { return content, nil } + // Return the unique ast.File for a cursor. + currentFile := func(c cursor.Cursor) *ast.File { + cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) + return cf.Node().(*ast.File) + } + // Pass 1: find functions and constants annotated with an appropriate "//go:fix" // comment (the syntax proposed by #32816), // and export a fact for each one. @@ -150,19 +159,8 @@ func run(pass *analysis.Pass) (any, error) { // and forward each reference to a forwardable constant. // // TODO(adonovan): handle multiple diffs that each add the same import. - nodeFilter = []ast.Node{ - (*ast.File)(nil), - (*ast.CallExpr)(nil), - (*ast.SelectorExpr)(nil), - (*ast.Ident)(nil), - } - var currentFile *ast.File - var currentSel *ast.SelectorExpr - inspect.Preorder(nodeFilter, func(n ast.Node) { - if file, ok := n.(*ast.File); ok { - currentFile = file - return - } + for cur := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { + n := cur.Node() switch n := n.(type) { case *ast.CallExpr: call := n @@ -177,27 +175,28 @@ func run(pass *analysis.Pass) (any, error) { } } if callee == nil { - return // nope + continue // nope } // Inline the call. content, err := readFile(call) if err != nil { pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) - return + continue } + curFile := currentFile(cur) caller := &inline.Caller{ Fset: pass.Fset, Types: pass.Pkg, Info: pass.TypesInfo, - File: currentFile, + File: curFile, Call: call, Content: content, } res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) if err != nil { pass.Reportf(call.Lparen, "%v", err) - return + continue } if res.Literalized { // Users are not fond of inlinings that literalize @@ -207,7 +206,7 @@ func run(pass *analysis.Pass) (any, error) { // and often literalizes when it cannot prove that // reducing the call is safe; the user of this tool // has no indication of what the problem is.) - return + continue } got := res.Content @@ -215,8 +214,8 @@ func run(pass *analysis.Pass) (any, error) { var textEdits []analysis.TextEdit for _, edit := range diff.Bytes(content, got) { textEdits = append(textEdits, analysis.TextEdit{ - Pos: currentFile.FileStart + token.Pos(edit.Start), - End: currentFile.FileStart + token.Pos(edit.End), + Pos: curFile.FileStart + token.Pos(edit.Start), + End: curFile.FileStart + token.Pos(edit.End), NewText: []byte(edit.New), }) } @@ -231,9 +230,6 @@ func run(pass *analysis.Pass) (any, error) { }) } - case *ast.SelectorExpr: - currentSel = n - case *ast.Ident: // If the identifier is a use of a forwardable constant, suggest forwarding it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { @@ -246,15 +242,15 @@ func run(pass *analysis.Pass) (any, error) { } } if fcon == nil { - return // nope + continue // nope } // If n is qualified by a package identifier, we'll need the full selector expression. var sel *ast.SelectorExpr - if currentSel != nil && n == currentSel.Sel { - sel = currentSel - currentSel = nil + if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + sel = cur.Parent().Node().(*ast.SelectorExpr) } + curFile := currentFile(cur) // We have an identifier A here (n), possibly qualified by a package identifier (sel.X), // and a forwardable "const A = B" elsewhere (fcon). @@ -267,8 +263,8 @@ func run(pass *analysis.Pass) (any, error) { // are in the current package. if pass.Pkg.Path() == fcon.RHSPkgPath { // fcon.rhsObj is the object referred to by B in the definition of A. - scope := pass.TypesInfo.Scopes[currentFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(fcon.RHSName, n.Pos()) // what "B" means in n's scope + scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(fcon.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. @@ -276,7 +272,7 @@ func run(pass *analysis.Pass) (any, error) { } if obj != fcon.rhsObj { // "B" means something different here than at the forwardable const's scope. - return + continue } } importPrefix := "" @@ -285,7 +281,7 @@ func run(pass *analysis.Pass) (any, error) { // TODO(jba): fix AddImport so that it returns "." if an existing dot import will work. // We will need to tell AddImport the name of the identifier we want to qualify (fcon.RHSName here). importID, eds := analysisinternal.AddImport( - pass.TypesInfo, currentFile, n.Pos(), fcon.RHSPkgPath, fcon.RHSPkgName) + pass.TypesInfo, curFile, n.Pos(), fcon.RHSPkgPath, fcon.RHSPkgName) importPrefix = importID + "." edits = eds } @@ -316,7 +312,7 @@ func run(pass *analysis.Pass) (any, error) { }) } } - }) + } return nil, nil } From 1320197d6c7ed6da48496e5e311c7ee76701e035 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 6 Feb 2025 14:29:43 -0500 Subject: [PATCH 112/309] gopls/internal/analysis/modernize/cmd/modernize: create By moving the main.go file, gopls users will be able to run $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix after the gopls release to apply all modernizer fixes en masse. Updates golang/go#70815 Change-Id: I25352b7b77653c177310dfea7a050741949890f9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647355 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/{ => cmd/modernize}/main.go | 2 -- 1 file changed, 2 deletions(-) rename gopls/internal/analysis/modernize/{ => cmd/modernize}/main.go (96%) diff --git a/gopls/internal/analysis/modernize/main.go b/gopls/internal/analysis/modernize/cmd/modernize/main.go similarity index 96% rename from gopls/internal/analysis/modernize/main.go rename to gopls/internal/analysis/modernize/cmd/modernize/main.go index e1276e333ae..1e8a4b95682 100644 --- a/gopls/internal/analysis/modernize/main.go +++ b/gopls/internal/analysis/modernize/cmd/modernize/main.go @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build ignore - // The modernize command suggests (or, with -fix, applies) fixes that // clarify Go code by using more modern features. package main From 8baeceabcef6fd04778372724cca217b8f5a9b93 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 31 Jan 2025 13:06:04 -0500 Subject: [PATCH 113/309] gopls/internal/analysis/modernize: mapsloop: fix two bugs As with slices.Contains, there was a bug in the maps.Copy modernizer resulting from not checking for implicit widening conversions in m[k]=v; and another, from not checking that m is a map. This CL fixes both. Modernizers are hard. :( Fixes golang/go#71313 Change-Id: Ie59aa9419868b3010435b7113bb5e67f0abbb4d3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645876 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/maps.go | 5 +++- .../testdata/src/mapsloop/mapsloop.go | 28 +++++++++++++++++++ .../testdata/src/mapsloop/mapsloop.go.golden | 26 +++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index ba8dabe6948..7950b546683 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -186,9 +186,12 @@ func mapsloop(pass *analysis.Pass) { assign := rng.Body.List[0].(*ast.AssignStmt) if index, ok := assign.Lhs[0].(*ast.IndexExpr); ok && equalSyntax(rng.Key, index.Index) && - equalSyntax(rng.Value, assign.Rhs[0]) { + equalSyntax(rng.Value, assign.Rhs[0]) && + is[*types.Map](typeparams.CoreType(info.TypeOf(index.X))) && + types.Identical(info.TypeOf(index), info.TypeOf(rng.Value)) { // m[k], v // Have: for k, v := range x { m[k] = v } + // where there is no implicit conversion. check(file, curRange, assign, index.X, rng.X) } } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go index bf8127b9a7b..769b4c84f60 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go @@ -20,6 +20,13 @@ func useCopy(dst, src map[int]string) { } } +func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { + // Replace loop by maps.Copy. + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" + } +} + func useClone(src map[int]string) { // Replace make(...) by maps.Clone. dst := make(map[int]string, len(src)) @@ -146,3 +153,24 @@ func nopeAssignmentHasIncrementOperator(src map[int]int) { dst[k] += v } } + +func nopeNotAMap(src map[int]string) { + var dst []string + for k, v := range src { + dst[k] = v + } +} + +func nopeNotAMapGeneric[E any, M ~map[int]E, S ~[]E](src M) { + var dst S + for k, v := range src { + dst[k] = v + } +} + +func nopeHasImplicitWidening(src map[string]int) { + dst := make(map[string]any) + for k, v := range src { + dst[k] = v + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden index d62ebc1e9aa..b9aa39021e8 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden @@ -18,6 +18,11 @@ func useCopy(dst, src map[int]string) { maps.Copy(dst, src) } +func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { + // Replace loop by maps.Copy. + maps.Copy(dst, src) +} + func useClone(src map[int]string) { // Replace make(...) by maps.Clone. dst := maps.Clone(src) @@ -118,3 +123,24 @@ func nopeAssignmentHasIncrementOperator(src map[int]int) { dst[k] += v } } + +func nopeNotAMap(src map[int]string) { + var dst []string + for k, v := range src { + dst[k] = v + } +} + +func nopeNotAMapGeneric[E any, M ~map[int]E, S ~[]E](src M) { + var dst S + for k, v := range src { + dst[k] = v + } +} + +func nopeHasImplicitWidening(src map[string]int) { + dst := make(map[string]any) + for k, v := range src { + dst[k] = v + } +} From fa7774c8d23d03a9d3e142cc7a7eb6b8062471a5 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 6 Feb 2025 21:27:53 +0000 Subject: [PATCH 114/309] gopls/internal/test/integration: reduce flakes in TestTelemetryPrompt Issue golang/go#71590 describes the likely source of flakiness of TestTelemetryPrompt_Response: the test will flake if the UTC day has changed between the start of TestMain and the start of the test. While we cannot fix this flakiness without a change to x/telemetry, we can significantly reduce it by first causing a file rotation check at the start of the test. For golang/go#68659 Change-Id: I22b6b41ebadbea1f4af0b7d4dc64dbfcf617cefd Reviewed-on: https://go-review.googlesource.com/c/tools/+/647436 Auto-Submit: Robert Findley LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam --- gopls/internal/test/integration/misc/prompt_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gopls/internal/test/integration/misc/prompt_test.go b/gopls/internal/test/integration/misc/prompt_test.go index 9e87bd9ba36..37cd654b08d 100644 --- a/gopls/internal/test/integration/misc/prompt_test.go +++ b/gopls/internal/test/integration/misc/prompt_test.go @@ -276,6 +276,19 @@ func main() { allCounters = []string{acceptanceCounter, declinedCounter, attempt1Counter} ) + // To avoid (but not prevent) the flakes encountered in golang/go#68659, we + // need to perform our first read before starting to increment counters. + // + // ReadCounter checks to see if the counter file needs to be rotated before + // reading. When files are rotated, all previous counts are lost. Calling + // ReadCounter here reduces the window for a flake due to this rotation (the + // file was originally was located during countertest.Open in TestMain). + // + // golang/go#71590 tracks the larger problems with the countertest library. + // + // (The counter name below is arbitrary.) + _, _ = countertest.ReadCounter(counter.New("issue68659")) + // We must increment counters in order for the initial reads below to // succeed. // From 584f5567536fff2b7add4e5f7b0cb6c9f66b9708 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 7 Feb 2025 00:16:44 +0000 Subject: [PATCH 115/309] gopls/internal/cache: downgrade bug reports for inconsistent metadata As described in the lengthy comment included with this CL, it is possible for gopls to encounter inconsistent metadata when it does not observe all filesystem changes. This explains the frequent bug reports of this nature that we have been seeing in telemetry. However, this is not feasible to address without significant redesign of gopls' package loading, and likely changes to the go command and go/packages integration. For now, we must downgrade the report. Fixes golang/go#63822 Change-Id: I60d627eab00a33a1f68fdf9f2de9890bede33e73 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647515 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- gopls/internal/cache/check.go | 97 ++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index 4faa1a73375..d094c535d7a 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -548,7 +548,13 @@ func (b *typeCheckBatch) importPackage(ctx context.Context, mp *metadata.Package } } } else { - id = importLookup(PackagePath(item.Path)) + var alt PackageID + id, alt = importLookup(PackagePath(item.Path)) + if alt != "" { + // Any bug leading to this scenario would have already been reported + // in importLookup. + return fmt.Errorf("inconsistent metadata during import: for package path %q, found both IDs %q and %q", item.Path, id, alt) + } var err error pkg, err = b.getImportPackage(ctx, id) if err != nil { @@ -665,8 +671,12 @@ func (b *typeCheckBatch) checkPackageForImport(ctx context.Context, ph *packageH // a given package path, based on the forward transitive closure of the initial // package (id). // +// If the second result is non-empty, it is another ID discovered in the import +// graph for the same package path. This means the import graph is +// incoherent--see #63822 and the long comment below. +// // The resulting function is not concurrency safe. -func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath) PackageID { +func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath) (id, altID PackageID) { assert(mp != nil, "nil metadata") // This function implements an incremental depth first scan through the @@ -680,6 +690,10 @@ func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath mp.PkgPath: mp.ID, } + // altIDs records alternative IDs for the given path, to report inconsistent + // metadata. + var altIDs map[PackagePath]PackageID + // pending is a FIFO queue of package metadata that has yet to have its // dependencies fully scanned. // Invariant: all entries in pending are already mapped in impMap. @@ -695,13 +709,82 @@ func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath if prevID, ok := impMap[depPath]; ok { // debugging #63822 if prevID != depID { + if altIDs == nil { + altIDs = make(map[PackagePath]PackageID) + } + if _, ok := altIDs[depPath]; !ok { + altIDs[depPath] = depID + } prev := source.Metadata(prevID) curr := source.Metadata(depID) switch { case prev == nil || curr == nil: bug.Reportf("inconsistent view of dependencies (missing dep)") case prev.ForTest != curr.ForTest: - bug.Reportf("inconsistent view of dependencies (mismatching ForTest)") + // This case is unfortunately understood to be possible. + // + // To explain this, consider a package a_test testing the package + // a, and for brevity denote by b' the intermediate test variant of + // the package b, which is created for the import graph of a_test, + // if b imports a. + // + // Now imagine that we have the following import graph, where + // higher packages import lower ones. + // + // a_test + // / \ + // b' c + // / \ / + // a d + // + // In this graph, there is one intermediate test variant b', + // because b imports a and so b' must hold the test variant import. + // + // Now, imagine that an on-disk change (perhaps due to a branch + // switch) affects the above import graph such that d imports a. + // + // a_test + // / \ + // b' c* + // / \ / + // / d* + // a---/ + // + // In this case, c and d should really be intermediate test + // variants, because they reach a. However, suppose that gopls does + // not know this yet (as indicated by '*'). + // + // Now suppose that the metadata of package c is invalidated, for + // example due to a change in an unrelated import or an added file. + // This will invalidate the metadata of c and a_test (but NOT b), + // and now gopls observes this graph: + // + // a_test + // / \ + // b' c' + // /| | + // / d d' + // a-----/ + // + // That is: a_test now sees c', which sees d', but since b was not + // invalidated, gopls still thinks that b' imports d (not d')! + // + // The problem, of course, is that gopls never observed the change + // to d, which would have invalidated b. This may be due to racing + // file watching events, in which case the problem should + // self-correct when gopls sees the change to d, or it may be due + // to d being outside the coverage of gopls' file watching glob + // patterns, or it may be due to buggy or entirely absent + // client-side file watching. + // + // TODO(rfindley): fix this, one way or another. It would be hard + // or impossible to repair gopls' state here, during type checking. + // However, we could perhaps reload metadata in Snapshot.load until + // we achieve a consistent state, or better, until the loaded state + // is consistent with our view of the filesystem, by making the Go + // command report digests of the files it reads. Both of those are + // tricker than they may seem, and have significant performance + // implications. default: bug.Reportf("inconsistent view of dependencies") } @@ -723,16 +806,16 @@ func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath return id, found } - return func(pkgPath PackagePath) PackageID { + return func(pkgPath PackagePath) (id, altID PackageID) { if id, ok := impMap[pkgPath]; ok { - return id + return id, altIDs[pkgPath] } for len(pending) > 0 { if id, found := search(pkgPath); found { - return id + return id, altIDs[pkgPath] } } - return "" + return "", "" } } From 4d1de705f2152c55e7ce447c2b6a514490a17513 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Feb 2025 11:06:23 -0500 Subject: [PATCH 116/309] internal/apidiff: remove This was added in preparation for gorelease, which did not progress. Change-Id: Ieaf93feddcb3bcbf0bd191cad7941786f8e544b3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647695 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/apidiff/README.md | 624 ------------ internal/apidiff/apidiff.go | 225 ----- internal/apidiff/apidiff_test.go | 174 ---- internal/apidiff/compatibility.go | 355 ------- internal/apidiff/correspondence.go | 225 ----- internal/apidiff/messageset.go | 83 -- internal/apidiff/report.go | 75 -- .../apidiff/testdata/exported_fields/ef.go | 37 - internal/apidiff/testdata/tests.go | 924 ------------------ 9 files changed, 2722 deletions(-) delete mode 100644 internal/apidiff/README.md delete mode 100644 internal/apidiff/apidiff.go delete mode 100644 internal/apidiff/apidiff_test.go delete mode 100644 internal/apidiff/compatibility.go delete mode 100644 internal/apidiff/correspondence.go delete mode 100644 internal/apidiff/messageset.go delete mode 100644 internal/apidiff/report.go delete mode 100644 internal/apidiff/testdata/exported_fields/ef.go delete mode 100644 internal/apidiff/testdata/tests.go diff --git a/internal/apidiff/README.md b/internal/apidiff/README.md deleted file mode 100644 index 3d9576c2866..00000000000 --- a/internal/apidiff/README.md +++ /dev/null @@ -1,624 +0,0 @@ -# Checking Go Package API Compatibility - -The `apidiff` tool in this directory determines whether two versions of the same -package are compatible. The goal is to help the developer make an informed -choice of semantic version after they have changed the code of their module. - -`apidiff` reports two kinds of changes: incompatible ones, which require -incrementing the major part of the semantic version, and compatible ones, which -require a minor version increment. If no API changes are reported but there are -code changes that could affect client code, then the patch version should -be incremented. - -Because `apidiff` ignores package import paths, it may be used to display API -differences between any two packages, not just different versions of the same -package. - -The current version of `apidiff` compares only packages, not modules. - - -## Compatibility Desiderata - -Any tool that checks compatibility can offer only an approximation. No tool can -detect behavioral changes; and even if it could, whether a behavioral change is -a breaking change or not depends on many factors, such as whether it closes a -security hole or fixes a bug. Even a change that causes some code to fail to -compile may not be considered a breaking change by the developers or their -users. It may only affect code marked as experimental or unstable, for -example, or the break may only manifest in unlikely cases. - -For a tool to be useful, its notion of compatibility must be relaxed enough to -allow reasonable changes, like adding a field to a struct, but strict enough to -catch significant breaking changes. A tool that is too lax will miss important -incompatibilities, and users will stop trusting it; one that is too strict may -generate so much noise that users will ignore it. - -To a first approximation, this tool reports a change as incompatible if it could -cause client code to stop compiling. But `apidiff` ignores five ways in which -code may fail to compile after a change. Three of them are mentioned in the -[Go 1 Compatibility Guarantee](https://golang.org/doc/go1compat). - -### Unkeyed Struct Literals - -Code that uses an unkeyed struct literal would fail to compile if a field was -added to the struct, making any such addition an incompatible change. An example: - -``` -// old -type Point struct { X, Y int } - -// new -type Point struct { X, Y, Z int } - -// client -p := pkg.Point{1, 2} // fails in new because there are more fields than expressions -``` -Here and below, we provide three snippets: the code in the old version of the -package, the code in the new version, and the code written in a client of the package, -which refers to it by the name `pkg`. The client code compiles against the old -code but not the new. - -### Embedding and Shadowing - -Adding an exported field to a struct can break code that embeds that struct, -because the newly added field may conflict with an identically named field -at the same struct depth. A selector referring to the latter would become -ambiguous and thus erroneous. - - -``` -// old -type Point struct { X, Y int } - -// new -type Point struct { X, Y, Z int } - -// client -type z struct { Z int } - -var v struct { - pkg.Point - z -} - -_ = v.Z // fails in new -``` -In the new version, the last line fails to compile because there are two embedded `Z` -fields at the same depth, one from `z` and one from `pkg.Point`. - - -### Using an Identical Type Externally - -If it is possible for client code to write a type expression representing the -underlying type of a defined type in a package, then external code can use it in -assignments involving the package type, making any change to that type incompatible. -``` -// old -type Point struct { X, Y int } - -// new -type Point struct { X, Y, Z int } - -// client -var p struct { X, Y int } = pkg.Point{} // fails in new because of Point's extra field -``` -Here, the external code could have used the provided name `Point`, but chose not -to. I'll have more to say about this and related examples later. - -### unsafe.Sizeof and Friends - -Since `unsafe.Sizeof`, `unsafe.Offsetof` and `unsafe.Alignof` are constant -expressions, they can be used in an array type literal: - -``` -// old -type S struct{ X int } - -// new -type S struct{ X, y int } - -// client -var a [unsafe.Sizeof(pkg.S{})]int = [8]int{} // fails in new because S's size is not 8 -``` -Use of these operations could make many changes to a type potentially incompatible. - - -### Type Switches - -A package change that merges two different types (with same underlying type) -into a single new type may break type switches in clients that refer to both -original types: - -``` -// old -type T1 int -type T2 int - -// new -type T1 int -type T2 = T1 - -// client -switch x.(type) { -case T1: -case T2: -} // fails with new because two cases have the same type -``` -This sort of incompatibility is sufficiently esoteric to ignore; the tool allows -merging types. - -## First Attempt at a Definition - -Our first attempt at defining compatibility captures the idea that all the -exported names in the old package must have compatible equivalents in the new -package. - -A new package is compatible with an old one if and only if: -- For every exported package-level name in the old package, the same name is - declared in the new at package level, and -- the names denote the same kind of object (e.g. both are variables), and -- the types of the objects are compatible. - -We will work out the details (and make some corrections) below, but it is clear -already that we will need to determine what makes two types compatible. And -whatever the definition of type compatibility, it's certainly true that if two -types are the same, they are compatible. So we will need to decide what makes an -old and new type the same. We will call this sameness relation _correspondence_. - -## Type Correspondence - -Go already has a definition of when two types are the same: -[type identity](https://golang.org/ref/spec#Type_identity). -But identity isn't adequate for our purpose: it says that two defined -types are identical if they arise from the same definition, but it's unclear -what "same" means when talking about two different packages (or two versions of -a single package). - -The obvious change to the definition of identity is to require that old and new -[defined types](https://golang.org/ref/spec#Type_definitions) -have the same name instead. But that doesn't work either, for two -reasons. First, type aliases can equate two defined types with different names: - -``` -// old -type E int - -// new -type t int -type E = t -``` -Second, an unexported type can be renamed: - -``` -// old -type u1 int -var V u1 - -// new -type u2 int -var V u2 -``` -Here, even though `u1` and `u2` are unexported, their exported fields and -methods are visible to clients, so they are part of the API. But since the name -`u1` is not visible to clients, it can be changed compatibly. We say that `u1` -and `u2` are _exposed_: a type is exposed if a client package can declare variables of that type. - -We will say that an old defined type _corresponds_ to a new one if they have the -same name, or one can be renamed to the other without otherwise changing the -API. In the first example above, old `E` and new `t` correspond. In the second, -old `u1` and new `u2` correspond. - -Two or more old defined types can correspond to a single new type: we consider -"merging" two types into one to be a compatible change. As mentioned above, -code that uses both names in a type switch will fail, but we deliberately ignore -this case. However, a single old type can correspond to only one new type. - -So far, we've explained what correspondence means for defined types. To extend -the definition to all types, we parallel the language's definition of type -identity. So, for instance, an old and a new slice type correspond if their -element types correspond. - -## Definition of Compatibility - -We can now present the definition of compatibility used by `apidiff`. - -### Package Compatibility - -> A new package is compatible with an old one if: ->1. Each exported name in the old package's scope also appears in the new ->package's scope, and the object (constant, variable, function or type) denoted ->by that name in the old package is compatible with the object denoted by the ->name in the new package, and ->2. For every exposed type that implements an exposed interface in the old package, -> its corresponding type should implement the corresponding interface in the new package. -> ->Otherwise the packages are incompatible. - -As an aside, the tool also finds exported names in the new package that are not -exported in the old, and marks them as compatible changes. - -Clause 2 is discussed further in "Whole-Package Compatibility." - -### Object Compatibility - -This section provides compatibility rules for constants, variables, functions -and types. - -#### Constants - ->A new exported constant is compatible with an old one of the same name if and only if ->1. Their types correspond, and ->2. Their values are identical. - -It is tempting to allow changing a typed constant to an untyped one. That may -seem harmless, but it can break code like this: - -``` -// old -const C int64 = 1 - -// new -const C = 1 - -// client -var x = C // old type is int64, new is int -var y int64 = x // fails with new: different types in assignment -``` - -A change to the value of a constant can break compatibility if the value is used -in an array type: - -``` -// old -const C = 1 - -// new -const C = 2 - -// client -var a [C]int = [1]int{} // fails with new because [2]int and [1]int are different types -``` -Changes to constant values are rare, and determining whether they are compatible -or not is better left to the user, so the tool reports them. - -#### Variables - ->A new exported variable is compatible with an old one of the same name if and ->only if their types correspond. - -Correspondence doesn't look past names, so this rule does not prevent adding a -field to `MyStruct` if the package declares `var V MyStruct`. It does, however, mean that - -``` -var V struct { X int } -``` -is incompatible with -``` -var V struct { X, Y int } -``` -I discuss this at length below in the section "Compatibility, Types and Names." - -#### Functions - ->A new exported function or variable is compatible with an old function of the ->same name if and only if their types (signatures) correspond. - -This rule captures the fact that, although many signature changes are compatible -for all call sites, none are compatible for assignment: - -``` -var v func(int) = pkg.F -``` -Here, `F` must be of type `func(int)` and not, for instance, `func(...int)` or `func(interface{})`. - -Note that the rule permits changing a function to a variable. This is a common -practice, usually done for test stubbing, and cannot break any code at compile -time. - -#### Exported Types - -> A new exported type is compatible with an old one if and only if their -> names are the same and their types correspond. - -This rule seems far too strict. But, ignoring aliases for the moment, it demands only -that the old and new _defined_ types correspond. Consider: -``` -// old -type T struct { X int } - -// new -type T struct { X, Y int } -``` -The addition of `Y` is a compatible change, because this rule does not require -that the struct literals have to correspond, only that the defined types -denoted by `T` must correspond. (Remember that correspondence stops at type -names.) - -If one type is an alias that refers to the corresponding defined type, the -situation is the same: - -``` -// old -type T struct { X int } - -// new -type u struct { X, Y int } -type T = u -``` -Here, the only requirement is that old `T` corresponds to new `u`, not that the -struct types correspond. (We can't tell from this snippet that the old `T` and -the new `u` do correspond; that depends on whether `u` replaces `T` throughout -the API.) - -However, the following change is incompatible, because the names do not -denote corresponding types: - -``` -// old -type T = struct { X int } - -// new -type T = struct { X, Y int } -``` -### Type Literal Compatibility - -Only five kinds of types can differ compatibly: defined types, structs, -interfaces, channels and numeric types. We only consider the compatibility of -the last four when they are the underlying type of a defined type. See -"Compatibility, Types and Names" for a rationale. - -We justify the compatibility rules by enumerating all the ways a type -can be used, and by showing that the allowed changes cannot break any code that -uses values of the type in those ways. - -Values of all types can be used in assignments (including argument passing and -function return), but we do not require that old and new types are assignment -compatible. That is because we assume that the old and new packages are never -used together: any given binary will link in either the old package or the new. -So in describing how a type can be used in the sections below, we omit -assignment. - -Any type can also be used in a type assertion or conversion. The changes we allow -below may affect the run-time behavior of these operations, but they cannot affect -whether they compile. The only such breaking change would be to change -the type `T` in an assertion `x.T` so that it no longer implements the interface -type of `x`; but the rules for interfaces below disallow that. - -> A new type is compatible with an old one if and only if they correspond, or -> one of the cases below applies. - -#### Defined Types - -Other than assignment, the only ways to use a defined type are to access its -methods, or to make use of the properties of its underlying type. Rule 2 below -covers the latter, and rules 3 and 4 cover the former. - -> A new defined type is compatible with an old one if and only if all of the -> following hold: ->1. They correspond. ->2. Their underlying types are compatible. ->3. The new exported value method set is a superset of the old. ->4. The new exported pointer method set is a superset of the old. - -An exported method set is a method set with all unexported methods removed. -When comparing methods of a method set, we require identical names and -corresponding signatures. - -Removing an exported method is clearly a breaking change. But removing an -unexported one (or changing its signature) can be breaking as well, if it -results in the type no longer implementing an interface. See "Whole-Package -Compatibility," below. - -#### Channels - -> A new channel type is compatible with an old one if -> 1. The element types correspond, and -> 2. Either the directions are the same, or the new type has no direction. - -Other than assignment, the only ways to use values of a channel type are to send -and receive on them, to close them, and to use them as map keys. Changes to a -channel type cannot cause code that closes a channel or uses it as a map key to -fail to compile, so we need not consider those operations. - -Rule 1 ensures that any operations on the values sent or received will compile. -Rule 2 captures the fact that any program that compiles with a directed channel -must use either only sends, or only receives, so allowing the other operation -by removing the channel direction cannot break any code. - - -#### Interfaces - -> A new interface is compatible with an old one if and only if: -> 1. The old interface does not have an unexported method, and it corresponds -> to the new interfaces (i.e. they have the same method set), or -> 2. The old interface has an unexported method and the new exported method set is a -> superset of the old. - -Other than assignment, the only ways to use an interface are to implement it, -embed it, or call one of its methods. (Interface values can also be used as map -keys, but that cannot cause a compile-time error.) - -Certainly, removing an exported method from an interface could break a client -call, so neither rule allows it. - -Rule 1 also disallows adding a method to an interface without an existing unexported -method. Such an interface can be implemented in client code. If adding a method -were allowed, a type that implements the old interface could fail to implement -the new one: - -``` -type I interface { M1() } // old -type I interface { M1(); M2() } // new - -// client -type t struct{} -func (t) M1() {} -var i pkg.I = t{} // fails with new, because t lacks M2 -``` - -Rule 2 is based on the observation that if an interface has an unexported -method, the only way a client can implement it is to embed it. -Adding a method is compatible in this case, because the embedding struct will -continue to implement the interface. Adding a method also cannot break any call -sites, since no program that compiles could have any such call sites. - -#### Structs - -> A new struct is compatible with an old one if all of the following hold: -> 1. The new set of top-level exported fields is a superset of the old. -> 2. The new set of _selectable_ exported fields is a superset of the old. -> 3. If the old struct is comparable, so is the new one. - -The set of selectable exported fields is the set of exported fields `F` -such that `x.F` is a valid selector expression for a value `x` of the struct -type. `F` may be at the top level of the struct, or it may be a field of an -embedded struct. - -Two fields are the same if they have the same name and corresponding types. - -Other than assignment, there are only four ways to use a struct: write a struct -literal, select a field, use a value of the struct as a map key, or compare two -values for equality. The first clause ensures that struct literals compile; the -second, that selections compile; and the third, that equality expressions and -map index expressions compile. - -#### Numeric Types - -> A new numeric type is compatible with an old one if and only if they are -> both unsigned integers, both signed integers, both floats or both complex -> types, and the new one is at least as large as the old on both 32-bit and -> 64-bit architectures. - -Other than in assignments, numeric types appear in arithmetic and comparison -expressions. Since all arithmetic operations but shifts (see below) require that -operand types be identical, and by assumption the old and new types underly -defined types (see "Compatibility, Types and Names," below), there is no way for -client code to write an arithmetic expression that compiles with operands of the -old type but not the new. - -Numeric types can also appear in type switches and type assertions. Again, since -the old and new types underly defined types, type switches and type assertions -that compiled using the old defined type will continue to compile with the new -defined type. - -Going from an unsigned to a signed integer type is an incompatible change for -the sole reason that only an unsigned type can appear as the right operand of a -shift. If this rule is relaxed, then changes from an unsigned type to a larger -signed type would be compatible. See [this -issue](https://github.com/golang/go/issues/19113). - -Only integer types can be used in bitwise and shift operations, and for indexing -slices and arrays. That is why switching from an integer to a floating-point -type--even one that can represent all values of the integer type--is an -incompatible change. - - -Conversions from floating-point to complex types or vice versa are not permitted -(the predeclared functions real, imag, and complex must be used instead). To -prevent valid floating-point or complex conversions from becoming invalid, -changing a floating-point type to a complex type or vice versa is considered an -incompatible change. - -Although conversions between any two integer types are valid, assigning a -constant value to a variable of integer type that is too small to represent the -constant is not permitted. That is why the only compatible changes are to -a new type whose values are a superset of the old. The requirement that the new -set of values must include the old on both 32-bit and 64-bit machines allows -conversions from `int32` to `int` and from `int` to `int64`, but not the other -direction; and similarly for `uint`. - -Changing a type to or from `uintptr` is considered an incompatible change. Since -its size is not specified, there is no way to know whether the new type's values -are a superset of the old type's. - -## Whole-Package Compatibility - -Some changes that are compatible for a single type are not compatible when the -package is considered as a whole. For example, if you remove an unexported -method on a defined type, it may no longer implement an interface of the -package. This can break client code: - -``` -// old -type T int -func (T) m() {} -type I interface { m() } - -// new -type T int // no method m anymore - -// client -var i pkg.I = pkg.T{} // fails with new because T lacks m -``` - -Similarly, adding a method to an interface can cause defined types -in the package to stop implementing it. - -The second clause in the definition for package compatibility handles these -cases. To repeat: -> 2. For every exposed type that implements an exposed interface in the old package, -> its corresponding type should implement the corresponding interface in the new package. -Recall that a type is exposed if it is part of the package's API, even if it is -unexported. - -Other incompatibilities that involve more than one type in the package can arise -whenever two types with identical underlying types exist in the old or new -package. Here, a change "splits" an identical underlying type into two, breaking -conversions: - -``` -// old -type B struct { X int } -type C struct { X int } - -// new -type B struct { X int } -type C struct { X, Y int } - -// client -var b B -_ = C(b) // fails with new: cannot convert B to C -``` -Finally, changes that are compatible for the package in which they occur can -break downstream packages. That can happen even if they involve unexported -methods, thanks to embedding. - -The definitions given here don't account for these sorts of problems. - - -## Compatibility, Types and Names - -The above definitions state that the only types that can differ compatibly are -defined types and the types that underly them. Changes to other type literals -are considered incompatible. For instance, it is considered an incompatible -change to add a field to the struct in this variable declaration: - -``` -var V struct { X int } -``` -or this alias definition: -``` -type T = struct { X int } -``` - -We make this choice to keep the definition of compatibility (relatively) simple. -A more precise definition could, for instance, distinguish between - -``` -func F(struct { X int }) -``` -where any changes to the struct are incompatible, and - -``` -func F(struct { X, u int }) -``` -where adding a field is compatible (since clients cannot write the signature, -and thus cannot assign `F` to a variable of the signature type). The definition -should then also allow other function signature changes that only require -call-site compatibility, like - -``` -func F(struct { X, u int }, ...int) -``` -The result would be a much more complex definition with little benefit, since -the examples in this section rarely arise in practice. diff --git a/internal/apidiff/apidiff.go b/internal/apidiff/apidiff.go deleted file mode 100644 index 4bf70d9b42d..00000000000 --- a/internal/apidiff/apidiff.go +++ /dev/null @@ -1,225 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// TODO: test swap corresponding types (e.g. u1 <-> u2 and u2 <-> u1) -// TODO: test exported alias refers to something in another package -- does correspondence work then? -// TODO: CODE COVERAGE -// TODO: note that we may miss correspondences because we bail early when we compare a signature (e.g. when lengths differ; we could do up to the shorter) -// TODO: if you add an unexported method to an exposed interface, you have to check that -// every exposed type that previously implemented the interface still does. Otherwise -// an external assignment of the exposed type to the interface type could fail. -// TODO: check constant values: large values aren't representable by some types. -// TODO: Document all the incompatibilities we don't check for. - -package apidiff - -import ( - "fmt" - "go/constant" - "go/token" - "go/types" -) - -// Changes reports on the differences between the APIs of the old and new packages. -// It classifies each difference as either compatible or incompatible (breaking.) For -// a detailed discussion of what constitutes an incompatible change, see the package -// documentation. -func Changes(old, new *types.Package) Report { - d := newDiffer(old, new) - d.checkPackage() - r := Report{} - for _, m := range d.incompatibles.collect() { - r.Changes = append(r.Changes, Change{Message: m, Compatible: false}) - } - for _, m := range d.compatibles.collect() { - r.Changes = append(r.Changes, Change{Message: m, Compatible: true}) - } - return r -} - -type differ struct { - old, new *types.Package - // Correspondences between named types. - // Even though it is the named types (*types.Named) that correspond, we use - // *types.TypeName as a map key because they are canonical. - // The values can be either named types or basic types. - correspondMap map[*types.TypeName]types.Type - - // Messages. - incompatibles messageSet - compatibles messageSet -} - -func newDiffer(old, new *types.Package) *differ { - return &differ{ - old: old, - new: new, - correspondMap: map[*types.TypeName]types.Type{}, - incompatibles: messageSet{}, - compatibles: messageSet{}, - } -} - -func (d *differ) incompatible(obj types.Object, part, format string, args ...interface{}) { - addMessage(d.incompatibles, obj, part, format, args) -} - -func (d *differ) compatible(obj types.Object, part, format string, args ...interface{}) { - addMessage(d.compatibles, obj, part, format, args) -} - -func addMessage(ms messageSet, obj types.Object, part, format string, args []interface{}) { - ms.add(obj, part, fmt.Sprintf(format, args...)) -} - -func (d *differ) checkPackage() { - // Old changes. - for _, name := range d.old.Scope().Names() { - oldobj := d.old.Scope().Lookup(name) - if !oldobj.Exported() { - continue - } - newobj := d.new.Scope().Lookup(name) - if newobj == nil { - d.incompatible(oldobj, "", "removed") - continue - } - d.checkObjects(oldobj, newobj) - } - // New additions. - for _, name := range d.new.Scope().Names() { - newobj := d.new.Scope().Lookup(name) - if newobj.Exported() && d.old.Scope().Lookup(name) == nil { - d.compatible(newobj, "", "added") - } - } - - // Whole-package satisfaction. - // For every old exposed interface oIface and its corresponding new interface nIface... - for otn1, nt1 := range d.correspondMap { - oIface, ok := otn1.Type().Underlying().(*types.Interface) - if !ok { - continue - } - nIface, ok := nt1.Underlying().(*types.Interface) - if !ok { - // If nt1 isn't an interface but otn1 is, then that's an incompatibility that - // we've already noticed, so there's no need to do anything here. - continue - } - // For every old type that implements oIface, its corresponding new type must implement - // nIface. - for otn2, nt2 := range d.correspondMap { - if otn1 == otn2 { - continue - } - if types.Implements(otn2.Type(), oIface) && !types.Implements(nt2, nIface) { - d.incompatible(otn2, "", "no longer implements %s", objectString(otn1)) - } - } - } -} - -func (d *differ) checkObjects(old, new types.Object) { - switch old := old.(type) { - case *types.Const: - if new, ok := new.(*types.Const); ok { - d.constChanges(old, new) - return - } - case *types.Var: - if new, ok := new.(*types.Var); ok { - d.checkCorrespondence(old, "", old.Type(), new.Type()) - return - } - case *types.Func: - switch new := new.(type) { - case *types.Func: - d.checkCorrespondence(old, "", old.Type(), new.Type()) - return - case *types.Var: - d.compatible(old, "", "changed from func to var") - d.checkCorrespondence(old, "", old.Type(), new.Type()) - return - - } - case *types.TypeName: - if new, ok := new.(*types.TypeName); ok { - d.checkCorrespondence(old, "", old.Type(), new.Type()) - return - } - default: - panic("unexpected obj type") - } - // Here if kind of type changed. - d.incompatible(old, "", "changed from %s to %s", - objectKindString(old), objectKindString(new)) -} - -// Compare two constants. -func (d *differ) constChanges(old, new *types.Const) { - ot := old.Type() - nt := new.Type() - // Check for change of type. - if !d.correspond(ot, nt) { - d.typeChanged(old, "", ot, nt) - return - } - // Check for change of value. - // We know the types are the same, so constant.Compare shouldn't panic. - if !constant.Compare(old.Val(), token.EQL, new.Val()) { - d.incompatible(old, "", "value changed from %s to %s", old.Val(), new.Val()) - } -} - -func objectKindString(obj types.Object) string { - switch obj.(type) { - case *types.Const: - return "const" - case *types.Var: - return "var" - case *types.Func: - return "func" - case *types.TypeName: - return "type" - default: - return "???" - } -} - -func (d *differ) checkCorrespondence(obj types.Object, part string, old, new types.Type) { - if !d.correspond(old, new) { - d.typeChanged(obj, part, old, new) - } -} - -func (d *differ) typeChanged(obj types.Object, part string, old, new types.Type) { - old = removeNamesFromSignature(old) - new = removeNamesFromSignature(new) - olds := types.TypeString(old, types.RelativeTo(d.old)) - news := types.TypeString(new, types.RelativeTo(d.new)) - d.incompatible(obj, part, "changed from %s to %s", olds, news) -} - -// go/types always includes the argument and result names when formatting a signature. -// Since these can change without affecting compatibility, we don't want users to -// be distracted by them, so we remove them. -func removeNamesFromSignature(t types.Type) types.Type { - t = types.Unalias(t) - sig, ok := t.(*types.Signature) - if !ok { - return t - } - - dename := func(p *types.Tuple) *types.Tuple { - var vars []*types.Var - for i := 0; i < p.Len(); i++ { - v := p.At(i) - vars = append(vars, types.NewParam(v.Pos(), v.Pkg(), "", types.Unalias(v.Type()))) - } - return types.NewTuple(vars...) - } - - return types.NewSignature(sig.Recv(), dename(sig.Params()), dename(sig.Results()), sig.Variadic()) -} diff --git a/internal/apidiff/apidiff_test.go b/internal/apidiff/apidiff_test.go deleted file mode 100644 index 2c8479667b4..00000000000 --- a/internal/apidiff/apidiff_test.go +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package apidiff - -import ( - "bufio" - "fmt" - "go/types" - "os" - "path/filepath" - "sort" - "strings" - "testing" - - "github.com/google/go-cmp/cmp" - "golang.org/x/tools/go/packages" - "golang.org/x/tools/internal/testenv" -) - -func TestChanges(t *testing.T) { - dir, err := os.MkdirTemp("", "apidiff_test") - if err != nil { - t.Fatal(err) - } - dir = filepath.Join(dir, "go") - wanti, wantc := splitIntoPackages(t, dir) - defer os.RemoveAll(dir) - sort.Strings(wanti) - sort.Strings(wantc) - - oldpkg, err := load(t, "apidiff/old", dir) - if err != nil { - t.Fatal(err) - } - newpkg, err := load(t, "apidiff/new", dir) - if err != nil { - t.Fatal(err) - } - - report := Changes(oldpkg.Types, newpkg.Types) - - got := report.messages(false) - if diff := cmp.Diff(wanti, got); diff != "" { - t.Errorf("incompatibles (-want +got):\n%s", diff) - } - got = report.messages(true) - if diff := cmp.Diff(wantc, got); diff != "" { - t.Errorf("compatibles (-want +got):\n%s", diff) - } -} - -func splitIntoPackages(t *testing.T, dir string) (incompatibles, compatibles []string) { - // Read the input file line by line. - // Write a line into the old or new package, - // dependent on comments. - // Also collect expected messages. - f, err := os.Open("testdata/tests.go") - if err != nil { - t.Fatal(err) - } - defer f.Close() - - if err := os.MkdirAll(filepath.Join(dir, "src", "apidiff"), 0700); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(dir, "src", "apidiff", "go.mod"), []byte("module apidiff\n"), 0666); err != nil { - t.Fatal(err) - } - - oldd := filepath.Join(dir, "src/apidiff/old") - newd := filepath.Join(dir, "src/apidiff/new") - if err := os.MkdirAll(oldd, 0700); err != nil { - t.Fatal(err) - } - if err := os.Mkdir(newd, 0700); err != nil && !os.IsExist(err) { - t.Fatal(err) - } - - oldf, err := os.Create(filepath.Join(oldd, "old.go")) - if err != nil { - t.Fatal(err) - } - newf, err := os.Create(filepath.Join(newd, "new.go")) - if err != nil { - t.Fatal(err) - } - - wl := func(f *os.File, line string) { - if _, err := fmt.Fprintln(f, line); err != nil { - t.Fatal(err) - } - } - writeBoth := func(line string) { wl(oldf, line); wl(newf, line) } - writeln := writeBoth - s := bufio.NewScanner(f) - for s.Scan() { - line := s.Text() - tl := strings.TrimSpace(line) - switch { - case tl == "// old": - writeln = func(line string) { wl(oldf, line) } - case tl == "// new": - writeln = func(line string) { wl(newf, line) } - case tl == "// both": - writeln = writeBoth - case strings.HasPrefix(tl, "// i "): - incompatibles = append(incompatibles, strings.TrimSpace(tl[4:])) - case strings.HasPrefix(tl, "// c "): - compatibles = append(compatibles, strings.TrimSpace(tl[4:])) - default: - writeln(line) - } - } - if s.Err() != nil { - t.Fatal(s.Err()) - } - return -} - -func load(t *testing.T, importPath, goPath string) (*packages.Package, error) { - testenv.NeedsGoPackages(t) - - cfg := &packages.Config{ - Mode: packages.LoadTypes, - } - if goPath != "" { - cfg.Env = append(os.Environ(), "GOPATH="+goPath) - cfg.Dir = filepath.Join(goPath, "src", filepath.FromSlash(importPath)) - } - pkgs, err := packages.Load(cfg, importPath) - if err != nil { - return nil, err - } - if len(pkgs[0].Errors) > 0 { - return nil, pkgs[0].Errors[0] - } - return pkgs[0], nil -} - -func TestExportedFields(t *testing.T) { - pkg, err := load(t, "golang.org/x/tools/internal/apidiff/testdata/exported_fields", "") - if err != nil { - t.Fatal(err) - } - typeof := func(name string) types.Type { - return pkg.Types.Scope().Lookup(name).Type() - } - - s := typeof("S") - su := s.(*types.Named).Underlying().(*types.Struct) - - ef := exportedSelectableFields(su) - wants := []struct { - name string - typ types.Type - }{ - {"A1", typeof("A1")}, - {"D", types.Typ[types.Bool]}, - {"E", types.Typ[types.Int]}, - {"F", typeof("F")}, - {"S", types.NewPointer(s)}, - } - - if got, want := len(ef), len(wants); got != want { - t.Errorf("got %d fields, want %d\n%+v", got, want, ef) - } - for _, w := range wants { - if got := ef[w.name]; got != nil && !types.Identical(got.Type(), w.typ) { - t.Errorf("%s: got %v, want %v", w.name, got.Type(), w.typ) - } - } -} diff --git a/internal/apidiff/compatibility.go b/internal/apidiff/compatibility.go deleted file mode 100644 index f8e59d611bd..00000000000 --- a/internal/apidiff/compatibility.go +++ /dev/null @@ -1,355 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package apidiff - -import ( - "fmt" - "go/types" - "reflect" - - "golang.org/x/tools/internal/typesinternal" -) - -func (d *differ) checkCompatible(otn *types.TypeName, old, new types.Type) { - old = types.Unalias(old) - new = types.Unalias(new) - switch old := old.(type) { - case *types.Interface: - if new, ok := new.(*types.Interface); ok { - d.checkCompatibleInterface(otn, old, new) - return - } - - case *types.Struct: - if new, ok := new.(*types.Struct); ok { - d.checkCompatibleStruct(otn, old, new) - return - } - - case *types.Chan: - if new, ok := new.(*types.Chan); ok { - d.checkCompatibleChan(otn, old, new) - return - } - - case *types.Basic: - if new, ok := new.(*types.Basic); ok { - d.checkCompatibleBasic(otn, old, new) - return - } - - case *types.Named: - panic("unreachable") - - default: - d.checkCorrespondence(otn, "", old, new) - return - - } - // Here if old and new are different kinds of types. - d.typeChanged(otn, "", old, new) -} - -func (d *differ) checkCompatibleChan(otn *types.TypeName, old, new *types.Chan) { - d.checkCorrespondence(otn, ", element type", old.Elem(), new.Elem()) - if old.Dir() != new.Dir() { - if new.Dir() == types.SendRecv { - d.compatible(otn, "", "removed direction") - } else { - d.incompatible(otn, "", "changed direction") - } - } -} - -func (d *differ) checkCompatibleBasic(otn *types.TypeName, old, new *types.Basic) { - // Certain changes to numeric types are compatible. Approximately, the info must - // be the same, and the new values must be a superset of the old. - if old.Kind() == new.Kind() { - // old and new are identical - return - } - if compatibleBasics[[2]types.BasicKind{old.Kind(), new.Kind()}] { - d.compatible(otn, "", "changed from %s to %s", old, new) - } else { - d.typeChanged(otn, "", old, new) - } -} - -// All pairs (old, new) of compatible basic types. -var compatibleBasics = map[[2]types.BasicKind]bool{ - {types.Uint8, types.Uint16}: true, - {types.Uint8, types.Uint32}: true, - {types.Uint8, types.Uint}: true, - {types.Uint8, types.Uint64}: true, - {types.Uint16, types.Uint32}: true, - {types.Uint16, types.Uint}: true, - {types.Uint16, types.Uint64}: true, - {types.Uint32, types.Uint}: true, - {types.Uint32, types.Uint64}: true, - {types.Uint, types.Uint64}: true, - {types.Int8, types.Int16}: true, - {types.Int8, types.Int32}: true, - {types.Int8, types.Int}: true, - {types.Int8, types.Int64}: true, - {types.Int16, types.Int32}: true, - {types.Int16, types.Int}: true, - {types.Int16, types.Int64}: true, - {types.Int32, types.Int}: true, - {types.Int32, types.Int64}: true, - {types.Int, types.Int64}: true, - {types.Float32, types.Float64}: true, - {types.Complex64, types.Complex128}: true, -} - -// Interface compatibility: -// If the old interface has an unexported method, the new interface is compatible -// if its exported method set is a superset of the old. (Users could not implement, -// only embed.) -// -// If the old interface did not have an unexported method, the new interface is -// compatible if its exported method set is the same as the old, and it has no -// unexported methods. (Adding an unexported method makes the interface -// unimplementable outside the package.) -// -// TODO: must also check that if any methods were added or removed, every exposed -// type in the package that implemented the interface in old still implements it in -// new. Otherwise external assignments could fail. -func (d *differ) checkCompatibleInterface(otn *types.TypeName, old, new *types.Interface) { - // Method sets are checked in checkCompatibleDefined. - - // Does the old interface have an unexported method? - if unexportedMethod(old) != nil { - d.checkMethodSet(otn, old, new, additionsCompatible) - } else { - // Perform an equivalence check, but with more information. - d.checkMethodSet(otn, old, new, additionsIncompatible) - if u := unexportedMethod(new); u != nil { - d.incompatible(otn, u.Name(), "added unexported method") - } - } -} - -// Return an unexported method from the method set of t, or nil if there are none. -func unexportedMethod(t *types.Interface) *types.Func { - for i := 0; i < t.NumMethods(); i++ { - if m := t.Method(i); !m.Exported() { - return m - } - } - return nil -} - -// We need to check three things for structs: -// 1. The set of exported fields must be compatible. This ensures that keyed struct -// literals continue to compile. (There is no compatibility guarantee for unkeyed -// struct literals.) -// 2. The set of exported *selectable* fields must be compatible. This includes the exported -// fields of all embedded structs. This ensures that selections continue to compile. -// 3. If the old struct is comparable, so must the new one be. This ensures that equality -// expressions and uses of struct values as map keys continue to compile. -// -// An unexported embedded struct can't appear in a struct literal outside the -// package, so it doesn't have to be present, or have the same name, in the new -// struct. -// -// Field tags are ignored: they have no compile-time implications. -func (d *differ) checkCompatibleStruct(obj types.Object, old, new *types.Struct) { - d.checkCompatibleObjectSets(obj, exportedFields(old), exportedFields(new)) - d.checkCompatibleObjectSets(obj, exportedSelectableFields(old), exportedSelectableFields(new)) - // Removing comparability from a struct is an incompatible change. - if types.Comparable(old) && !types.Comparable(new) { - d.incompatible(obj, "", "old is comparable, new is not") - } -} - -// exportedFields collects all the immediate fields of the struct that are exported. -// This is also the set of exported keys for keyed struct literals. -func exportedFields(s *types.Struct) map[string]types.Object { - m := map[string]types.Object{} - for i := 0; i < s.NumFields(); i++ { - f := s.Field(i) - if f.Exported() { - m[f.Name()] = f - } - } - return m -} - -// exportedSelectableFields collects all the exported fields of the struct, including -// exported fields of embedded structs. -// -// We traverse the struct breadth-first, because of the rule that a lower-depth field -// shadows one at a higher depth. -func exportedSelectableFields(s *types.Struct) map[string]types.Object { - var ( - m = map[string]types.Object{} - next []*types.Struct // embedded structs at the next depth - seen []*types.Struct // to handle recursive embedding - ) - for cur := []*types.Struct{s}; len(cur) > 0; cur, next = next, nil { - seen = append(seen, cur...) - // We only want to consider unambiguous fields. Ambiguous fields (where there - // is more than one field of the same name at the same level) are legal, but - // cannot be selected. - for name, f := range unambiguousFields(cur) { - // Record an exported field we haven't seen before. If we have seen it, - // it occurred a lower depth, so it shadows this field. - if f.Exported() && m[name] == nil { - m[name] = f - } - // Remember embedded structs for processing at the next depth, - // but only if we haven't seen the struct at this depth or above. - if !f.Anonymous() { - continue - } - t := f.Type().Underlying() - if p, ok := t.(*types.Pointer); ok { - t = p.Elem().Underlying() - } - if t, ok := t.(*types.Struct); ok && !contains(seen, t) { - next = append(next, t) - } - } - } - return m -} - -func contains(ts []*types.Struct, t *types.Struct) bool { - for _, s := range ts { - if types.Identical(s, t) { - return true - } - } - return false -} - -// Given a set of structs at the same depth, the unambiguous fields are the ones whose -// names appear exactly once. -func unambiguousFields(structs []*types.Struct) map[string]*types.Var { - fields := map[string]*types.Var{} - seen := map[string]bool{} - for _, s := range structs { - for i := 0; i < s.NumFields(); i++ { - f := s.Field(i) - name := f.Name() - if seen[name] { - delete(fields, name) - } else { - seen[name] = true - fields[name] = f - } - } - } - return fields -} - -// Anything removed or change from the old set is an incompatible change. -// Anything added to the new set is a compatible change. -func (d *differ) checkCompatibleObjectSets(obj types.Object, old, new map[string]types.Object) { - for name, oldo := range old { - newo := new[name] - if newo == nil { - d.incompatible(obj, name, "removed") - } else { - d.checkCorrespondence(obj, name, oldo.Type(), newo.Type()) - } - } - for name := range new { - if old[name] == nil { - d.compatible(obj, name, "added") - } - } -} - -func (d *differ) checkCompatibleDefined(otn *types.TypeName, old *types.Named, new types.Type) { - // We've already checked that old and new correspond. - d.checkCompatible(otn, old.Underlying(), new.Underlying()) - // If there are different kinds of types (e.g. struct and interface), don't bother checking - // the method sets. - if reflect.TypeOf(old.Underlying()) != reflect.TypeOf(new.Underlying()) { - return - } - // Interface method sets are checked in checkCompatibleInterface. - if types.IsInterface(old) { - return - } - - // A new method set is compatible with an old if the new exported methods are a superset of the old. - d.checkMethodSet(otn, old, new, additionsCompatible) - d.checkMethodSet(otn, types.NewPointer(old), types.NewPointer(new), additionsCompatible) -} - -const ( - additionsCompatible = true - additionsIncompatible = false -) - -func (d *differ) checkMethodSet(otn *types.TypeName, oldt, newt types.Type, addcompat bool) { - // TODO: find a way to use checkCompatibleObjectSets for this. - oldMethodSet := exportedMethods(oldt) - newMethodSet := exportedMethods(newt) - msname := otn.Name() - if _, ok := types.Unalias(oldt).(*types.Pointer); ok { - msname = "*" + msname - } - for name, oldMethod := range oldMethodSet { - newMethod := newMethodSet[name] - if newMethod == nil { - var part string - // Due to embedding, it's possible that the method's receiver type is not - // the same as the defined type whose method set we're looking at. So for - // a type T with removed method M that is embedded in some other type U, - // we will generate two "removed" messages for T.M, one for its own type - // T and one for the embedded type U. We want both messages to appear, - // but the messageSet dedup logic will allow only one message for a given - // object. So use the part string to distinguish them. - recv := oldMethod.Type().(*types.Signature).Recv() - if _, named := typesinternal.ReceiverNamed(recv); named.Obj() != otn { - part = fmt.Sprintf(", method set of %s", msname) - } - d.incompatible(oldMethod, part, "removed") - } else { - obj := oldMethod - // If a value method is changed to a pointer method and has a signature - // change, then we can get two messages for the same method definition: one - // for the value method set that says it's removed, and another for the - // pointer method set that says it changed. To keep both messages (since - // messageSet dedups), use newMethod for the second. (Slight hack.) - if !hasPointerReceiver(oldMethod) && hasPointerReceiver(newMethod) { - obj = newMethod - } - d.checkCorrespondence(obj, "", oldMethod.Type(), newMethod.Type()) - } - } - - // Check for added methods. - for name, newMethod := range newMethodSet { - if oldMethodSet[name] == nil { - if addcompat { - d.compatible(newMethod, "", "added") - } else { - d.incompatible(newMethod, "", "added") - } - } - } -} - -// exportedMethods collects all the exported methods of type's method set. -func exportedMethods(t types.Type) map[string]*types.Func { - m := make(map[string]*types.Func) - ms := types.NewMethodSet(t) - for i := 0; i < ms.Len(); i++ { - obj := ms.At(i).Obj().(*types.Func) - if obj.Exported() { - m[obj.Name()] = obj - } - } - return m -} - -func hasPointerReceiver(method *types.Func) bool { - isptr, _ := typesinternal.ReceiverNamed(method.Type().(*types.Signature).Recv()) - return isptr -} diff --git a/internal/apidiff/correspondence.go b/internal/apidiff/correspondence.go deleted file mode 100644 index a626e066430..00000000000 --- a/internal/apidiff/correspondence.go +++ /dev/null @@ -1,225 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package apidiff - -import ( - "go/types" - "sort" -) - -// Two types are correspond if they are identical except for defined types, -// which must correspond. -// -// Two defined types correspond if they can be interchanged in the old and new APIs, -// possibly after a renaming. -// -// This is not a pure function. If we come across named types while traversing, -// we establish correspondence. -func (d *differ) correspond(old, new types.Type) bool { - return d.corr(old, new, nil) -} - -// corr determines whether old and new correspond. The argument p is a list of -// known interface identities, to avoid infinite recursion. -// -// corr calls itself recursively as much as possible, to establish more -// correspondences and so check more of the API. E.g. if the new function has more -// parameters than the old, compare all the old ones before returning false. -// -// Compare this to the implementation of go/types.Identical. -func (d *differ) corr(old, new types.Type, p *ifacePair) bool { - // Structure copied from types.Identical. - old = types.Unalias(old) - new = types.Unalias(new) - switch old := old.(type) { - case *types.Basic: - return types.Identical(old, new) - - case *types.Array: - if new, ok := new.(*types.Array); ok { - return d.corr(old.Elem(), new.Elem(), p) && old.Len() == new.Len() - } - - case *types.Slice: - if new, ok := new.(*types.Slice); ok { - return d.corr(old.Elem(), new.Elem(), p) - } - - case *types.Map: - if new, ok := new.(*types.Map); ok { - return d.corr(old.Key(), new.Key(), p) && d.corr(old.Elem(), new.Elem(), p) - } - - case *types.Chan: - if new, ok := new.(*types.Chan); ok { - return d.corr(old.Elem(), new.Elem(), p) && old.Dir() == new.Dir() - } - - case *types.Pointer: - if new, ok := new.(*types.Pointer); ok { - return d.corr(old.Elem(), new.Elem(), p) - } - - case *types.Signature: - if new, ok := new.(*types.Signature); ok { - pe := d.corr(old.Params(), new.Params(), p) - re := d.corr(old.Results(), new.Results(), p) - return old.Variadic() == new.Variadic() && pe && re - } - - case *types.Tuple: - if new, ok := new.(*types.Tuple); ok { - for i := 0; i < old.Len(); i++ { - if i >= new.Len() || !d.corr(old.At(i).Type(), new.At(i).Type(), p) { - return false - } - } - return old.Len() == new.Len() - } - - case *types.Struct: - if new, ok := new.(*types.Struct); ok { - for i := 0; i < old.NumFields(); i++ { - if i >= new.NumFields() { - return false - } - of := old.Field(i) - nf := new.Field(i) - if of.Anonymous() != nf.Anonymous() || - old.Tag(i) != new.Tag(i) || - !d.corr(of.Type(), nf.Type(), p) || - !d.corrFieldNames(of, nf) { - return false - } - } - return old.NumFields() == new.NumFields() - } - - case *types.Interface: - if new, ok := new.(*types.Interface); ok { - // Deal with circularity. See the comment in types.Identical. - q := &ifacePair{old, new, p} - for p != nil { - if p.identical(q) { - return true // same pair was compared before - } - p = p.prev - } - oldms := d.sortedMethods(old) - newms := d.sortedMethods(new) - for i, om := range oldms { - if i >= len(newms) { - return false - } - nm := newms[i] - if d.methodID(om) != d.methodID(nm) || !d.corr(om.Type(), nm.Type(), q) { - return false - } - } - return old.NumMethods() == new.NumMethods() - } - - case *types.Named: - if new, ok := new.(*types.Named); ok { - return d.establishCorrespondence(old, new) - } - if new, ok := new.(*types.Basic); ok { - // Basic types are defined types, too, so we have to support them. - - return d.establishCorrespondence(old, new) - } - - default: - panic("unknown type kind") - } - return false -} - -// Compare old and new field names. We are determining correspondence across packages, -// so just compare names, not packages. For an unexported, embedded field of named -// type (non-named embedded fields are possible with aliases), we check that the type -// names correspond. We check the types for correspondence before this is called, so -// we've established correspondence. -func (d *differ) corrFieldNames(of, nf *types.Var) bool { - if of.Anonymous() && nf.Anonymous() && !of.Exported() && !nf.Exported() { - if on, ok := of.Type().(*types.Named); ok { - nn := nf.Type().(*types.Named) - return d.establishCorrespondence(on, nn) - } - } - return of.Name() == nf.Name() -} - -// Establish that old corresponds with new if it does not already -// correspond to something else. -func (d *differ) establishCorrespondence(old *types.Named, new types.Type) bool { - oldname := old.Obj() - oldc := d.correspondMap[oldname] - if oldc == nil { - // For now, assume the types don't correspond unless they are from the old - // and new packages, respectively. - // - // This is too conservative. For instance, - // [old] type A = q.B; [new] type A q.C - // could be OK if in package q, B is an alias for C. - // Or, using p as the name of the current old/new packages: - // [old] type A = q.B; [new] type A int - // could be OK if in q, - // [old] type B int; [new] type B = p.A - // In this case, p.A and q.B name the same type in both old and new worlds. - // Note that this case doesn't imply circular package imports: it's possible - // that in the old world, p imports q, but in the new, q imports p. - // - // However, if we didn't do something here, then we'd incorrectly allow cases - // like the first one above in which q.B is not an alias for q.C - // - // What we should do is check that the old type, in the new world's package - // of the same path, doesn't correspond to something other than the new type. - // That is a bit hard, because there is no easy way to find a new package - // matching an old one. - if newn, ok := new.(*types.Named); ok { - if old.Obj().Pkg() != d.old || newn.Obj().Pkg() != d.new { - return old.Obj().Id() == newn.Obj().Id() - } - } - // If there is no correspondence, create one. - d.correspondMap[oldname] = new - // Check that the corresponding types are compatible. - d.checkCompatibleDefined(oldname, old, new) - return true - } - return types.Identical(oldc, new) -} - -func (d *differ) sortedMethods(iface *types.Interface) []*types.Func { - ms := make([]*types.Func, iface.NumMethods()) - for i := 0; i < iface.NumMethods(); i++ { - ms[i] = iface.Method(i) - } - sort.Slice(ms, func(i, j int) bool { return d.methodID(ms[i]) < d.methodID(ms[j]) }) - return ms -} - -func (d *differ) methodID(m *types.Func) string { - // If the method belongs to one of the two packages being compared, use - // just its name even if it's unexported. That lets us treat unexported names - // from the old and new packages as equal. - if m.Pkg() == d.old || m.Pkg() == d.new { - return m.Name() - } - return m.Id() -} - -// Copied from the go/types package: - -// An ifacePair is a node in a stack of interface type pairs compared for identity. -type ifacePair struct { - x, y *types.Interface - prev *ifacePair -} - -func (p *ifacePair) identical(q *ifacePair) bool { - return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x -} diff --git a/internal/apidiff/messageset.go b/internal/apidiff/messageset.go deleted file mode 100644 index 895e5f878a4..00000000000 --- a/internal/apidiff/messageset.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// TODO: show that two-non-empty dotjoin can happen, by using an anon struct as a field type -// TODO: don't report removed/changed methods for both value and pointer method sets? - -package apidiff - -import ( - "fmt" - "go/types" - "sort" - "strings" -) - -// There can be at most one message for each object or part thereof. -// Parts include interface methods and struct fields. -// -// The part thing is necessary. Method (Func) objects have sufficient info, but field -// Vars do not: they just have a field name and a type, without the enclosing struct. -type messageSet map[types.Object]map[string]string - -// Add a message for obj and part, overwriting a previous message -// (shouldn't happen). -// obj is required but part can be empty. -func (m messageSet) add(obj types.Object, part, msg string) { - s := m[obj] - if s == nil { - s = map[string]string{} - m[obj] = s - } - if f, ok := s[part]; ok && f != msg { - fmt.Printf("! second, different message for obj %s, part %q\n", obj, part) - fmt.Printf(" first: %s\n", f) - fmt.Printf(" second: %s\n", msg) - } - s[part] = msg -} - -func (m messageSet) collect() []string { - var s []string - for obj, parts := range m { - // Format each object name relative to its own package. - objstring := objectString(obj) - for part, msg := range parts { - var p string - - if strings.HasPrefix(part, ",") { - p = objstring + part - } else { - p = dotjoin(objstring, part) - } - s = append(s, p+": "+msg) - } - } - sort.Strings(s) - return s -} - -func objectString(obj types.Object) string { - if f, ok := obj.(*types.Func); ok { - sig := f.Type().(*types.Signature) - if recv := sig.Recv(); recv != nil { - tn := types.TypeString(recv.Type(), types.RelativeTo(obj.Pkg())) - if tn[0] == '*' { - tn = "(" + tn + ")" - } - return fmt.Sprintf("%s.%s", tn, obj.Name()) - } - } - return obj.Name() -} - -func dotjoin(s1, s2 string) string { - if s1 == "" { - return s2 - } - if s2 == "" { - return s1 - } - return s1 + "." + s2 -} diff --git a/internal/apidiff/report.go b/internal/apidiff/report.go deleted file mode 100644 index c3f08a9d396..00000000000 --- a/internal/apidiff/report.go +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package apidiff - -import ( - "bytes" - "fmt" - "io" -) - -// Report describes the changes detected by Changes. -type Report struct { - Changes []Change -} - -// A Change describes a single API change. -type Change struct { - Message string - Compatible bool -} - -func (r Report) messages(compatible bool) []string { - var msgs []string - for _, c := range r.Changes { - if c.Compatible == compatible { - msgs = append(msgs, c.Message) - } - } - return msgs -} - -func (r Report) String() string { - var buf bytes.Buffer - if err := r.Text(&buf); err != nil { - return fmt.Sprintf("!!%v", err) - } - return buf.String() -} - -func (r Report) Text(w io.Writer) error { - if err := r.TextIncompatible(w, true); err != nil { - return err - } - return r.TextCompatible(w) -} - -func (r Report) TextIncompatible(w io.Writer, withHeader bool) error { - if withHeader { - return r.writeMessages(w, "Incompatible changes:", r.messages(false)) - } - return r.writeMessages(w, "", r.messages(false)) -} - -func (r Report) TextCompatible(w io.Writer) error { - return r.writeMessages(w, "Compatible changes:", r.messages(true)) -} - -func (r Report) writeMessages(w io.Writer, header string, msgs []string) error { - if len(msgs) == 0 { - return nil - } - if header != "" { - if _, err := fmt.Fprintf(w, "%s\n", header); err != nil { - return err - } - } - for _, m := range msgs { - if _, err := fmt.Fprintf(w, "- %s\n", m); err != nil { - return err - } - } - return nil -} diff --git a/internal/apidiff/testdata/exported_fields/ef.go b/internal/apidiff/testdata/exported_fields/ef.go deleted file mode 100644 index 19da716c46d..00000000000 --- a/internal/apidiff/testdata/exported_fields/ef.go +++ /dev/null @@ -1,37 +0,0 @@ -package exported_fields - -// Used for testing exportedFields. -// Its exported fields are: -// A1 [1]int -// D bool -// E int -// F F -// S *S -type ( - S struct { - int - *embed2 - embed - E int // shadows embed.E - alias - A1 - *S - } - - A1 [1]int - - embed struct { - E string - } - - embed2 struct { - embed3 - F // shadows embed3.F - } - embed3 struct { - F bool - } - alias = struct{ D bool } - - F int -) diff --git a/internal/apidiff/testdata/tests.go b/internal/apidiff/testdata/tests.go deleted file mode 100644 index 567e6077758..00000000000 --- a/internal/apidiff/testdata/tests.go +++ /dev/null @@ -1,924 +0,0 @@ -// This file is split into two packages, old and new. -// It is syntactically valid Go so that gofmt can process it. -// -// If a comment begins with: Then: -// old write subsequent lines to the "old" package -// new write subsequent lines to the "new" package -// both write subsequent lines to both packages -// c expect a compatible error with the following text -// i expect an incompatible error with the following text -package ignore - -// both -import "io" - -//////////////// Basics - -//// Same type in both: OK. -// both -type A int - -//// Changing the type is an incompatible change. -// old -type B int - -// new -// i B: changed from int to string -type B string - -//// Adding a new type, whether alias or not, is a compatible change. -// new -// c AA: added -type AA = A - -// c B1: added -type B1 bool - -//// Change of type for an unexported name doesn't matter... -// old -type t int - -// new -type t string // OK: t isn't part of the API - -//// ...unless it is exposed. -// both -var V2 u - -// old -type u string - -// new -// i u: changed from string to int -type u int - -//// An exposed, unexported type can be renamed. -// both -type u2 int - -// old -type u1 int - -var V5 u1 - -// new -var V5 u2 // OK: V5 has changed type, but old u1 corresponds to new u2 - -//// Splitting a single type into two is an incompatible change. -// both -type u3 int - -// old -type ( - Split1 = u1 - Split2 = u1 -) - -// new -type ( - Split1 = u2 // OK, since old u1 corresponds to new u2 - - // This tries to make u1 correspond to u3 - // i Split2: changed from u1 to u3 - Split2 = u3 -) - -//// Merging two types into one is OK. -// old -type ( - GoodMerge1 = u2 - GoodMerge2 = u3 -) - -// new -type ( - GoodMerge1 = u3 - GoodMerge2 = u3 -) - -//// Merging isn't OK here because a method is lost. -// both -type u4 int - -func (u4) M() {} - -// old -type ( - BadMerge1 = u3 - BadMerge2 = u4 -) - -// new -type ( - BadMerge1 = u3 - // i u4.M: removed - // What's really happening here is that old u4 corresponds to new u3, - // and new u3's method set is not a superset of old u4's. - BadMerge2 = u3 -) - -// old -type Rem int - -// new -// i Rem: removed - -//////////////// Constants - -//// type changes -// old -const ( - C1 = 1 - C2 int = 2 - C3 = 3 - C4 u1 = 4 -) - -var V8 int - -// new -const ( - // i C1: changed from untyped int to untyped string - C1 = "1" - // i C2: changed from int to untyped int - C2 = -1 - // i C3: changed from untyped int to int - C3 int = 3 - // i V8: changed from var to const - V8 int = 1 - C4 u2 = 4 // OK: u1 corresponds to u2 -) - -// value change -// old -const ( - Cr1 = 1 - Cr2 = "2" - Cr3 = 3.5 - Cr4 = complex(0, 4.1) -) - -// new -const ( - // i Cr1: value changed from 1 to -1 - Cr1 = -1 - // i Cr2: value changed from "2" to "3" - Cr2 = "3" - // i Cr3: value changed from 3.5 to 3.8 - Cr3 = 3.8 - // i Cr4: value changed from (0 + 4.1i) to (4.1 + 0i) - Cr4 = complex(4.1, 0) -) - -//////////////// Variables - -//// simple type changes -// old -var ( - V1 string - V3 A - V7 <-chan int -) - -// new -var ( - // i V1: changed from string to []string - V1 []string - V3 A // OK: same - // i V7: changed from <-chan int to chan int - V7 chan int -) - -//// interface type changes -// old -var ( - V9 interface{ M() } - V10 interface{ M() } - V11 interface{ M() } -) - -// new -var ( - // i V9: changed from interface{M()} to interface{} - V9 interface{} - // i V10: changed from interface{M()} to interface{M(); M2()} - V10 interface { - M2() - M() - } - // i V11: changed from interface{M()} to interface{M(int)} - V11 interface{ M(int) } -) - -//// struct type changes -// old -var ( - VS1 struct{ A, B int } - VS2 struct{ A, B int } - VS3 struct{ A, B int } - VS4 struct { - A int - u1 - } -) - -// new -var ( - // i VS1: changed from struct{A int; B int} to struct{B int; A int} - VS1 struct{ B, A int } - // i VS2: changed from struct{A int; B int} to struct{A int} - VS2 struct{ A int } - // i VS3: changed from struct{A int; B int} to struct{A int; B int; C int} - VS3 struct{ A, B, C int } - VS4 struct { - A int - u2 - } -) - -//////////////// Types - -// old -const C5 = 3 - -type ( - A1 [1]int - A2 [2]int - A3 [C5]int -) - -// new -// i C5: value changed from 3 to 4 -const C5 = 4 - -type ( - A1 [1]int - // i A2: changed from [2]int to [2]bool - A2 [2]bool - // i A3: changed from [3]int to [4]int - A3 [C5]int -) - -// old -type ( - Sl []int - P1 *int - P2 *u1 -) - -// new -type ( - // i Sl: changed from []int to []string - Sl []string - // i P1: changed from *int to **bool - P1 **bool - P2 *u2 // OK: u1 corresponds to u2 -) - -// old -type Bc1 int32 -type Bc2 uint -type Bc3 float32 -type Bc4 complex64 - -// new -// c Bc1: changed from int32 to int -type Bc1 int - -// c Bc2: changed from uint to uint64 -type Bc2 uint64 - -// c Bc3: changed from float32 to float64 -type Bc3 float64 - -// c Bc4: changed from complex64 to complex128 -type Bc4 complex128 - -// old -type Bi1 int32 -type Bi2 uint -type Bi3 float64 -type Bi4 complex128 - -// new -// i Bi1: changed from int32 to int16 -type Bi1 int16 - -// i Bi2: changed from uint to uint32 -type Bi2 uint32 - -// i Bi3: changed from float64 to float32 -type Bi3 float32 - -// i Bi4: changed from complex128 to complex64 -type Bi4 complex64 - -// old -type ( - M1 map[string]int - M2 map[string]int - M3 map[string]int -) - -// new -type ( - M1 map[string]int - // i M2: changed from map[string]int to map[int]int - M2 map[int]int - // i M3: changed from map[string]int to map[string]string - M3 map[string]string -) - -// old -type ( - Ch1 chan int - Ch2 <-chan int - Ch3 chan int - Ch4 <-chan int -) - -// new -type ( - // i Ch1, element type: changed from int to bool - Ch1 chan bool - // i Ch2: changed direction - Ch2 chan<- int - // i Ch3: changed direction - Ch3 <-chan int - // c Ch4: removed direction - Ch4 chan int -) - -// old -type I1 interface { - M1() - M2() -} - -// new -type I1 interface { - // M1() - // i I1.M1: removed - M2(int) - // i I1.M2: changed from func() to func(int) - M3() - // i I1.M3: added - m() - // i I1.m: added unexported method -} - -// old -type I2 interface { - M1() - m() -} - -// new -type I2 interface { - M1() - // m() Removing an unexported method is OK. - m2() // OK, because old already had an unexported method - // c I2.M2: added - M2() -} - -// old -type I3 interface { - io.Reader - M() -} - -// new -// OK: what matters is the method set; the name of the embedded -// interface isn't important. -type I3 interface { - M() - Read([]byte) (int, error) -} - -// old -type I4 io.Writer - -// new -// OK: in both, I4 is a distinct type from io.Writer, and -// the old and new I4s have the same method set. -type I4 interface { - Write([]byte) (int, error) -} - -// old -type I5 = io.Writer - -// new -// i I5: changed from io.Writer to I5 -// In old, I5 and io.Writer are the same type; in new, -// they are different. That can break something like: -// var _ func(io.Writer) = func(pkg.I6) {} -type I5 io.Writer - -// old -type I6 interface{ Write([]byte) (int, error) } - -// new -// i I6: changed from I6 to io.Writer -// Similar to the above. -type I6 = io.Writer - -//// correspondence with a basic type -// Basic types are technically defined types, but they aren't -// represented that way in go/types, so the cases below are special. - -// both -type T1 int - -// old -var VT1 T1 - -// new -// i VT1: changed from T1 to int -// This fails because old T1 corresponds to both int and new T1. -var VT1 int - -// old -type t2 int - -var VT2 t2 - -// new -// OK: t2 corresponds to int. It's fine that old t2 -// doesn't exist in new. -var VT2 int - -// both -type t3 int - -func (t3) M() {} - -// old -var VT3 t3 - -// new -// i t3.M: removed -// Here the change from t3 to int is incompatible -// because old t3 has an exported method. -var VT3 int - -// old -var VT4 int - -// new -type t4 int - -// i VT4: changed from int to t4 -// This is incompatible because of code like -// VT4 + int(1) -// which works in old but fails in new. -// The difference from the above cases is that -// in those, we were merging two types into one; -// here, we are splitting int into t4 and int. -var VT4 t4 - -//////////////// Functions - -// old -func F1(a int, b string) map[u1]A { return nil } -func F2(int) {} -func F3(int) {} -func F4(int) int { return 0 } -func F5(int) int { return 0 } -func F6(int) {} -func F7(interface{}) {} - -// new -func F1(c int, d string) map[u2]AA { return nil } //OK: same (since u1 corresponds to u2) - -// i F2: changed from func(int) to func(int) bool -func F2(int) bool { return true } - -// i F3: changed from func(int) to func(int, int) -func F3(int, int) {} - -// i F4: changed from func(int) int to func(bool) int -func F4(bool) int { return 0 } - -// i F5: changed from func(int) int to func(int) string -func F5(int) string { return "" } - -// i F6: changed from func(int) to func(...int) -func F6(...int) {} - -// i F7: changed from func(interface{}) to func(interface{x()}) -func F7(a interface{ x() }) {} - -// old -func F8(bool) {} - -// new -// c F8: changed from func to var -var F8 func(bool) - -// old -var F9 func(int) - -// new -// i F9: changed from var to func -func F9(int) {} - -// both -// OK, even though new S1 is incompatible with old S1 (see below) -func F10(S1) {} - -//////////////// Structs - -// old -type S1 struct { - A int - B string - C bool - d float32 -} - -// new -type S1 = s1 - -type s1 struct { - C chan int - // i S1.C: changed from bool to chan int - A int - // i S1.B: removed - // i S1: old is comparable, new is not - x []int - d float32 - E bool - // c S1.E: added -} - -// old -type embed struct { - E string -} - -type S2 struct { - A int - embed -} - -// new -type embedx struct { - E string -} - -type S2 struct { - embedx // OK: the unexported embedded field changed names, but the exported field didn't - A int -} - -// both -type F int - -// old -type S3 struct { - A int - embed -} - -// new -type embed struct{ F int } - -type S3 struct { - // i S3.E: removed - embed - // c S3.F: added - A int -} - -// old -type embed2 struct { - embed3 - F // shadows embed3.F -} - -type embed3 struct { - F bool -} - -type alias = struct{ D bool } - -type S4 struct { - int - *embed2 - embed - E int // shadows embed.E - alias - A1 - *S4 -} - -// new -type S4 struct { - // OK: removed unexported fields - // D and F marked as added because they are now part of the immediate fields - D bool - // c S4.D: added - E int // OK: same as in old - F F - // c S4.F: added - A1 // OK: same - *S4 // OK: same (recursive embedding) -} - -//// Difference between exported selectable fields and exported immediate fields. -// both -type S5 struct{ A int } - -// old -// Exported immediate fields: A, S5 -// Exported selectable fields: A int, S5 S5 -type S6 struct { - S5 S5 - A int -} - -// new -// Exported immediate fields: S5 -// Exported selectable fields: A int, S5 S5. - -// i S6.A: removed -type S6 struct { - S5 -} - -//// Ambiguous fields can exist; they just can't be selected. -// both -type ( - embed7a struct{ E int } - embed7b struct{ E bool } -) - -// old -type S7 struct { // legal, but no selectable fields - embed7a - embed7b -} - -// new -type S7 struct { - embed7a - embed7b - // c S7.E: added - E string -} - -//////////////// Method sets - -// old -type SM struct { - embedm - Embedm -} - -func (SM) V1() {} -func (SM) V2() {} -func (SM) V3() {} -func (SM) V4() {} -func (SM) v() {} - -func (*SM) P1() {} -func (*SM) P2() {} -func (*SM) P3() {} -func (*SM) P4() {} -func (*SM) p() {} - -type embedm int - -func (embedm) EV1() {} -func (embedm) EV2() {} -func (embedm) EV3() {} -func (*embedm) EP1() {} -func (*embedm) EP2() {} -func (*embedm) EP3() {} - -type Embedm struct { - A int -} - -func (Embedm) FV() {} -func (*Embedm) FP() {} - -type RepeatEmbedm struct { - Embedm -} - -// new -type SM struct { - embedm2 - embedm3 - Embedm - // i SM.A: changed from int to bool -} - -// c SMa: added -type SMa = SM - -func (SM) V1() {} // OK: same - -// func (SM) V2() {} -// i SM.V2: removed - -// i SM.V3: changed from func() to func(int) -func (SM) V3(int) {} - -// c SM.V5: added -func (SM) V5() {} - -func (SM) v(int) {} // OK: unexported method change -func (SM) v2() {} // OK: unexported method added - -func (*SM) P1() {} // OK: same -//func (*SM) P2() {} -// i (*SM).P2: removed - -// i (*SM).P3: changed from func() to func(int) -func (*SMa) P3(int) {} - -// c (*SM).P5: added -func (*SM) P5() {} - -// func (*SM) p() {} // OK: unexported method removed - -// Changing from a value to a pointer receiver or vice versa -// just looks like adding and removing a method. - -// i SM.V4: removed -// i (*SM).V4: changed from func() to func(int) -func (*SM) V4(int) {} - -// c SM.P4: added -// P4 is not removed from (*SM) because value methods -// are in the pointer method set. -func (SM) P4() {} - -type embedm2 int - -// i embedm.EV1: changed from func() to func(int) -func (embedm2) EV1(int) {} - -// i embedm.EV2, method set of SM: removed -// i embedm.EV2, method set of *SM: removed - -// i (*embedm).EP2, method set of *SM: removed -func (*embedm2) EP1() {} - -type embedm3 int - -func (embedm3) EV3() {} // OK: compatible with old embedm.EV3 -func (*embedm3) EP3() {} // OK: compatible with old (*embedm).EP3 - -type Embedm struct { - // i Embedm.A: changed from int to bool - A bool -} - -// i Embedm.FV: changed from func() to func(int) -func (Embedm) FV(int) {} -func (*Embedm) FP() {} - -type RepeatEmbedm struct { - // i RepeatEmbedm.A: changed from int to bool - Embedm -} - -//////////////// Whole-package interface satisfaction - -// old -type WI1 interface { - M1() - m1() -} - -type WI2 interface { - M2() - m2() -} - -type WS1 int - -func (WS1) M1() {} -func (WS1) m1() {} - -type WS2 int - -func (WS2) M2() {} -func (WS2) m2() {} - -// new -type WI1 interface { - M1() - m() -} - -type WS1 int - -func (WS1) M1() {} - -// i WS1: no longer implements WI1 -//func (WS1) m1() {} - -type WI2 interface { - M2() - m2() - // i WS2: no longer implements WI2 - m3() -} - -type WS2 int - -func (WS2) M2() {} -func (WS2) m2() {} - -//////////////// Miscellany - -// This verifies that the code works even through -// multiple levels of unexported typed. - -// old -var Z w - -type w []x -type x []z -type z int - -// new -var Z w - -type w []x -type x []z - -// i z: changed from int to bool -type z bool - -// old -type H struct{} - -func (H) M() {} - -// new -// i H: changed from struct{} to interface{M()} -type H interface { - M() -} - -//// Splitting types - -//// OK: in both old and new, {J1, K1, L1} name the same type. -// old -type ( - J1 = K1 - K1 = L1 - L1 int -) - -// new -type ( - J1 = K1 - K1 int - L1 = J1 -) - -//// Old has one type, K2; new has J2 and K2. -// both -type K2 int - -// old -type J2 = K2 - -// new -// i K2: changed from K2 to K2 -type J2 K2 // old K2 corresponds with new J2 -// old K2 also corresponds with new K2: problem - -// both -type k3 int - -var Vj3 j3 // expose j3 - -// old -type j3 = k3 - -// new -// OK: k3 isn't exposed -type j3 k3 - -// both -type k4 int - -var Vj4 j4 // expose j4 -var VK4 k4 // expose k4 - -// old -type j4 = k4 - -// new -// i Vj4: changed from k4 to j4 -// e.g. p.Vj4 = p.Vk4 -type j4 k4 From a9bf6fdf9803c2872968caff08802cf427eb875c Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Feb 2025 11:26:00 -0500 Subject: [PATCH 117/309] gopls/internal/analysis/modernize: remove SortStable Remove the modernization from sort.SliceStable to slices.SortStable. There is no slices.SortStable. Change-Id: I55e6c6848aa2708976d35ceabab73e7b55da1d1f Reviewed-on: https://go-review.googlesource.com/c/tools/+/647735 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/analysis/modernize/sortslice.go | 21 +++++++++---------- .../testdata/src/sortslice/sortslice.go | 2 -- .../src/sortslice/sortslice.go.golden | 4 ---- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 7f590eefc32..4f856d39c33 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -9,7 +9,6 @@ import ( "go/ast" "go/token" "go/types" - "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -25,13 +24,15 @@ import ( // sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // => slices.Sort(s) // -// It also supports the SliceStable variant. +// There is no slices.SortStable. // // TODO(adonovan): support // // - sort.Slice(s, func(i, j int) bool { return s[i] ... s[j] }) -// -> slices.SortFunc(s, func(x, y int) bool { return x ... y }) -// iff all uses of i, j can be replaced by s[i], s[j]. +// -> slices.SortFunc(s, func(x, y T) int { return x ... y }) +// iff all uses of i, j can be replaced by s[i], s[j] and "<" can be replaced with cmp.Compare. +// +// - As above for sort.SliceStable -> slices.SortStableFunc. // // - sort.Sort(x) where x has a named slice type whose Less method is the natural order. // -> sort.Slice(x) @@ -43,13 +44,11 @@ func sortslice(pass *analysis.Pass) { info := pass.TypesInfo check := func(file *ast.File, call *ast.CallExpr) { - // call to sort.Slice{,Stable}? + // call to sort.Slice? obj := typeutil.Callee(info, call) - if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable") { + if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice") { return } - stable := cond(strings.HasSuffix(obj.Name(), "Stable"), "Stable", "") - if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 { sig := info.Types[lit.Type].Type.(*types.Signature) @@ -78,15 +77,15 @@ func sortslice(pass *analysis.Pass) { Pos: call.Fun.Pos(), End: call.Fun.End(), Category: "sortslice", - Message: fmt.Sprintf("sort.Slice%[1]s can be modernized using slices.Sort%[1]s", stable), + Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"), SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Replace sort.Slice%[1]s call by slices.Sort%[1]s", stable), + Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"), TextEdits: append(importEdits, []analysis.TextEdit{ { // Replace sort.Slice with slices.Sort. Pos: call.Fun.Pos(), End: call.Fun.End(), - NewText: []byte(slicesName + ".Sort" + stable), + NewText: []byte(slicesName + ".Sort"), }, { // Eliminate FuncLit. diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go index fce3e006328..53d15746839 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go @@ -6,8 +6,6 @@ type myint int func _(s []myint) { sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort" - - sort.SliceStable(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.SliceStable can be modernized using slices.SortStable" } func _(x *struct{ s []int }) { diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden index 176ae66d204..d97636fd311 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden @@ -4,16 +4,12 @@ import "slices" import "slices" -import "slices" - import "sort" type myint int func _(s []myint) { slices.Sort(s) // want "sort.Slice can be modernized using slices.Sort" - - slices.SortStable(s) // want "sort.SliceStable can be modernized using slices.SortStable" } func _(x *struct{ s []int }) { From e65ea150db54e65bce06700111515e5a4598900c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 17 Jan 2025 20:33:22 -0500 Subject: [PATCH 118/309] go/analysis/internal/checker: implement three-way merge This CL adds support for three-way merging to the checker's -fix operation. It consists of three parts: 1. a rewritten applyFix function that applies as many changes as can be cleanly merged; 2. a script-based test framework that allows all existing and new tests to be written as txtar files in testdata instead of ad-hoc Go logic; and 3. a data-driven "marker" analyzer that reports diagnostics containing fixes according to //@f comments in the target Go source files. Also, it adds a -diff flag to the checker tools that causes them to print the computed file changes instead of directly applying them. The new applyFix treats each SuggestedFix as an independent change, analogous to a git commit. Fixes are combined by invoking a three-way merge algorithm, diff.Merge, analogous to git merge, except simpler since it works on the list of []diff.Edit instead of text. If any fix does not apply cleanly, we discard it, and report that we did so, with a hint to run the tool again until a fixed point is reached. (This is just a starting point; a better UX would be for the tool to do this itself.) If a diagnostic has multiple suggested fixes, we select the first one. The old behavior of attempting to apply them all makes no sense. The support for filesystem-level aliases (e.g. symbolic and hard links) previously implemented using FileID has been removed, as its interactions with the new logic were tricky. I ran gopls' modernize singlechecker on k8s/... and it was able to cleanly resolved 142 edits across 53 files; the result builds, and symbolic links were not evidently a problem. Update golang/go#68765 Update golang/go#67049 Change-Id: Id3fb55118b3d0612cafe7e86f52589812bd74a96 Reviewed-on: https://go-review.googlesource.com/c/tools/+/644835 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- go/analysis/analysistest/analysistest.go | 6 + go/analysis/internal/checker/checker.go | 386 ++++++---- go/analysis/internal/checker/checker_test.go | 98 --- go/analysis/internal/checker/fix_test.go | 669 +++++++++++------- .../internal/checker/testdata/conflict.txt | 30 + .../internal/checker/testdata/diff.txt | 36 + .../internal/checker/testdata/fixes.txt | 59 ++ .../internal/checker/testdata/importdup.txt | 29 + .../internal/checker/testdata/importdup2.txt | 60 ++ .../internal/checker/testdata/json.txt | 42 ++ .../internal/checker/testdata/noend.txt | 21 + .../internal/checker/testdata/overlap.txt | 34 + 12 files changed, 973 insertions(+), 497 deletions(-) create mode 100644 go/analysis/internal/checker/testdata/conflict.txt create mode 100644 go/analysis/internal/checker/testdata/diff.txt create mode 100644 go/analysis/internal/checker/testdata/fixes.txt create mode 100644 go/analysis/internal/checker/testdata/importdup.txt create mode 100644 go/analysis/internal/checker/testdata/importdup2.txt create mode 100644 go/analysis/internal/checker/testdata/json.txt create mode 100644 go/analysis/internal/checker/testdata/noend.txt create mode 100644 go/analysis/internal/checker/testdata/overlap.txt diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 4300490a445..8c62c56fa84 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -36,6 +36,12 @@ import ( // and populates it with a GOPATH-style project using filemap (which // maps file names to contents). On success it returns the name of the // directory and a cleanup function to delete it. +// +// TODO(adonovan): provide a newer version that accepts a testing.T, +// calls T.TempDir, and calls T.Fatal on any error, avoiding the need +// to return cleanup or err: +// +// func WriteFilesToTmp(t *testing.T filemap map[string]string) string func WriteFiles(filemap map[string]string) (dir string, cleanup func(), err error) { gopath, err := os.MkdirTemp("", "analysistest") if err != nil { diff --git a/go/analysis/internal/checker/checker.go b/go/analysis/internal/checker/checker.go index a4cddeb2c6e..fb3c47b1625 100644 --- a/go/analysis/internal/checker/checker.go +++ b/go/analysis/internal/checker/checker.go @@ -17,9 +17,8 @@ import ( "flag" "fmt" "go/format" - "go/token" "io" - "io/ioutil" + "maps" "log" "os" @@ -32,10 +31,11 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/checker" + "golang.org/x/tools/go/analysis/internal" "golang.org/x/tools/go/analysis/internal/analysisflags" "golang.org/x/tools/go/packages" + "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/diff" - "golang.org/x/tools/internal/robustio" ) var ( @@ -55,8 +55,12 @@ var ( // IncludeTests indicates whether test files should be analyzed too. IncludeTests = true - // Fix determines whether to apply all suggested fixes. + // Fix determines whether to apply (!Diff) or display (Diff) all suggested fixes. Fix bool + + // Diff causes the file updates to be displayed, but not applied. + // This flag has no effect unless Fix is true. + Diff bool ) // RegisterFlags registers command-line flags used by the analysis driver. @@ -72,6 +76,7 @@ func RegisterFlags() { flag.BoolVar(&IncludeTests, "test", IncludeTests, "indicates whether test files should be analyzed, too") flag.BoolVar(&Fix, "fix", false, "apply all suggested fixes") + flag.BoolVar(&Diff, "diff", false, "with -fix, don't update the files, but print a unified diff") } // Run loads the packages specified by args using go/packages, @@ -170,13 +175,18 @@ func Run(args []string, analyzers []*analysis.Analyzer) int { return 1 } - // Apply all fixes from the root actions. + // Don't print the diagnostics, + // but apply all fixes from the root actions. if Fix { - if err := applyFixes(graph.Roots); err != nil { + if err := applyFixes(graph.Roots, Diff); err != nil { // Fail when applying fixes failed. log.Print(err) return 1 } + // TODO(adonovan): don't proceed to print the text or JSON output + // if we applied fixes; stop here. + // + // return pkgsExitCode } // Print the results. If !RunDespiteErrors and there @@ -265,7 +275,13 @@ func load(patterns []string, allSyntax bool) ([]*packages.Package, error) { } mode |= packages.NeedModule conf := packages.Config{ - Mode: mode, + Mode: mode, + // Ensure that child process inherits correct alias of PWD. + // (See discussion at Dir field of [exec.Command].) + // However, this currently breaks some tests. + // TODO(adonovan): Investigate. + // + // Dir: os.Getenv("PWD"), Tests: IncludeTests, } initial, err := packages.Load(&conf, patterns...) @@ -275,181 +291,257 @@ func load(patterns []string, allSyntax bool) ([]*packages.Package, error) { return initial, err } -// applyFixes applies suggested fixes associated with diagnostics -// reported by the specified actions. It verifies that edits do not -// conflict, even through file-system level aliases such as symbolic -// links, and then edits the files. -func applyFixes(actions []*checker.Action) error { - // Visit all of the actions and accumulate the suggested edits. - paths := make(map[robustio.FileID]string) - editsByAction := make(map[robustio.FileID]map[*checker.Action][]diff.Edit) +// applyFixes attempts to apply the first suggested fix associated +// with each diagnostic reported by the specified actions. +// All fixes must have been validated by [analysisinternal.ValidateFixes]. +// +// Each fix is treated as an independent change; fixes are merged in +// an arbitrary deterministic order as if by a three-way diff tool +// such as the UNIX diff3 command or 'git merge'. Any fix that cannot be +// cleanly merged is discarded, in which case the final summary tells +// the user to re-run the tool. +// TODO(adonovan): make the checker tool re-run the analysis itself. +// +// When the same file is analyzed as a member of both a primary +// package "p" and a test-augmented package "p [p.test]", there may be +// duplicate diagnostics and fixes. One set of fixes will be applied +// and the other will be discarded; but re-running the tool may then +// show zero fixes, which may cause the confused user to wonder what +// happened to the other ones. +// TODO(adonovan): consider pre-filtering completely identical fixes. +// +// A common reason for overlapping fixes is duplicate additions of the +// same import. The merge algorithm may often cleanly resolve such +// fixes, coalescing identical edits, but the merge may sometimes be +// confused by nearby changes. +// +// Even when merging succeeds, there is no guarantee that the +// composition of the two fixes is semantically correct. Coalescing +// identical edits is appropriate for imports, but not for, say, +// increments to a counter variable; the correct resolution in that +// case might be to increment it twice. Or consider two fixes that +// each delete the penultimate reference to an import or local +// variable: each fix is sound individually, and they may be textually +// distant from each other, but when both are applied, the program is +// no longer valid because it has an unreferenced import or local +// variable. +// TODO(adonovan): investigate replacing the final "gofmt" step with a +// formatter that applies the unused-import deletion logic of +// "goimports". +// +// Merging depends on both the order of fixes and they order of edits +// within them. For example, if three fixes add import "a" twice and +// import "b" once, the two imports of "a" may be combined if they +// appear in order [a, a, b], or not if they appear as [a, b, a]. +// TODO(adonovan): investigate an algebraic approach to imports; +// that is, for fixes to Go source files, convert changes within the +// import(...) portion of the file into semantic edits, compose those +// edits algebraically, then convert the result back to edits. +// +// applyFixes returns success if all fixes are valid, could be cleanly +// merged, and the corresponding files were successfully updated. +// +// If showDiff, instead of updating the files it display the final +// patch composed of all the cleanly merged fixes. +// +// TODO(adonovan): handle file-system level aliases such as symbolic +// links using robustio.FileID. +func applyFixes(actions []*checker.Action, showDiff bool) error { + + // Select fixes to apply. + // + // If there are several for a given Diagnostic, choose the first. + // Preserve the order of iteration, for determinism. + type fixact struct { + fix *analysis.SuggestedFix + act *checker.Action + } + var fixes []*fixact for _, act := range actions { - editsForTokenFile := make(map[*token.File][]diff.Edit) for _, diag := range act.Diagnostics { - for _, sf := range diag.SuggestedFixes { - for _, edit := range sf.TextEdits { - // Validate the edit. - // Any error here indicates a bug in the analyzer. - start, end := edit.Pos, edit.End - file := act.Package.Fset.File(start) - if file == nil { - return fmt.Errorf("analysis %q suggests invalid fix: missing file info for pos (%v)", - act.Analyzer.Name, edit.Pos) - } - if !end.IsValid() { - end = start - } - if start > end { - return fmt.Errorf("analysis %q suggests invalid fix: pos (%v) > end (%v)", - act.Analyzer.Name, edit.Pos, edit.End) - } - if eof := token.Pos(file.Base() + file.Size()); end > eof { - return fmt.Errorf("analysis %q suggests invalid fix: end (%v) past end of file (%v)", - act.Analyzer.Name, edit.End, eof) - } - edit := diff.Edit{ - Start: file.Offset(start), - End: file.Offset(end), - New: string(edit.NewText), - } - editsForTokenFile[file] = append(editsForTokenFile[file], edit) + for i := range diag.SuggestedFixes { + fix := &diag.SuggestedFixes[i] + if i == 0 { + fixes = append(fixes, &fixact{fix, act}) + } else { + // TODO(adonovan): abstract the logger. + log.Printf("%s: ignoring alternative fix %q", act, fix.Message) } } } + } - for f, edits := range editsForTokenFile { - id, _, err := robustio.GetFileID(f.Name()) + // Read file content on demand, from the virtual + // file system that fed the analyzer (see #62292). + // + // This cache assumes that all successful reads for the same + // file name return the same content. + // (It is tempting to group fixes by package and do the + // merge/apply/format steps one package at a time, but + // packages are not disjoint, due to test variants, so this + // would not really address the issue.) + baselineContent := make(map[string][]byte) + getBaseline := func(readFile analysisinternal.ReadFileFunc, filename string) ([]byte, error) { + content, ok := baselineContent[filename] + if !ok { + var err error + content, err = readFile(filename) if err != nil { - return err - } - if _, hasId := paths[id]; !hasId { - paths[id] = f.Name() - editsByAction[id] = make(map[*checker.Action][]diff.Edit) + return nil, err } - editsByAction[id][act] = edits + baselineContent[filename] = content } + return content, nil } - // Validate and group the edits to each actual file. - editsByPath := make(map[string][]diff.Edit) - for id, actToEdits := range editsByAction { - path := paths[id] - actions := make([]*checker.Action, 0, len(actToEdits)) - for act := range actToEdits { - actions = append(actions, act) - } + // Apply each fix, updating the current state + // only if the entire fix can be cleanly merged. + accumulatedEdits := make(map[string][]diff.Edit) + goodFixes := 0 +fixloop: + for _, fixact := range fixes { + readFile := internal.Pass(fixact.act).ReadFile + + // Convert analysis.TextEdits to diff.Edits, grouped by file. + // Precondition: a prior call to validateFix succeeded. + fileEdits := make(map[string][]diff.Edit) + fset := fixact.act.Package.Fset + for _, edit := range fixact.fix.TextEdits { + file := fset.File(edit.Pos) + + baseline, err := getBaseline(readFile, file.Name()) + if err != nil { + log.Printf("skipping fix to file %s: %v", file.Name(), err) + continue fixloop + } - // Does any action create conflicting edits? - for _, act := range actions { - edits := actToEdits[act] - if _, invalid := validateEdits(edits); invalid > 0 { - name, x, y := act.Analyzer.Name, edits[invalid-1], edits[invalid] - return diff3Conflict(path, name, name, []diff.Edit{x}, []diff.Edit{y}) + // We choose to treat size mismatch as a serious error, + // as it indicates a concurrent write to at least one file, + // and possibly others (consider a git checkout, for example). + if file.Size() != len(baseline) { + return fmt.Errorf("concurrent file modification detected in file %s (size changed from %d -> %d bytes); aborting fix", + file.Name(), file.Size(), len(baseline)) } + + fileEdits[file.Name()] = append(fileEdits[file.Name()], diff.Edit{ + Start: file.Offset(edit.Pos), + End: file.Offset(edit.End), + New: string(edit.NewText), + }) } - // Does any pair of different actions create edits that conflict? - for j := range actions { - for k := range actions[:j] { - x, y := actions[j], actions[k] - if x.Analyzer.Name > y.Analyzer.Name { - x, y = y, x - } - xedits, yedits := actToEdits[x], actToEdits[y] - combined := append(xedits, yedits...) - if _, invalid := validateEdits(combined); invalid > 0 { - // TODO: consider applying each action's consistent list of edits entirely, - // and then using a three-way merge (such as GNU diff3) on the resulting - // files to report more precisely the parts that actually conflict. - return diff3Conflict(path, x.Analyzer.Name, y.Analyzer.Name, xedits, yedits) + // Apply each set of edits by merging atop + // the previous accumulated state. + after := make(map[string][]diff.Edit) + for file, edits := range fileEdits { + if prev := accumulatedEdits[file]; len(prev) > 0 { + merged, ok := diff.Merge(prev, edits) + if !ok { + // debugging + if false { + log.Printf("%s: fix %s conflicts", fixact.act, fixact.fix.Message) + } + continue fixloop // conflict } + edits = merged } + after[file] = edits } - var edits []diff.Edit - for act := range actToEdits { - edits = append(edits, actToEdits[act]...) + // The entire fix applied cleanly; commit it. + goodFixes++ + maps.Copy(accumulatedEdits, after) + // debugging + if false { + log.Printf("%s: fix %s applied", fixact.act, fixact.fix.Message) } - editsByPath[path], _ = validateEdits(edits) // remove duplicates. already validated. } + badFixes := len(fixes) - goodFixes - // Now we've got a set of valid edits for each file. Apply them. - // TODO(adonovan): don't abort the operation partway just because one file fails. - for path, edits := range editsByPath { - // TODO(adonovan): this should really work on the same - // gulp from the file system that fed the analyzer (see #62292). - contents, err := os.ReadFile(path) - if err != nil { - return err + // Show diff or update files to final state. + var files []string + for file := range accumulatedEdits { + files = append(files, file) + } + sort.Strings(files) // for deterministic -diff + var filesUpdated, totalFiles int + for _, file := range files { + edits := accumulatedEdits[file] + if len(edits) == 0 { + continue // the diffs annihilated (a miracle?) } - out, err := diff.ApplyBytes(contents, edits) + // Apply accumulated fixes. + baseline := baselineContent[file] // (cache hit) + final, err := diff.ApplyBytes(baseline, edits) if err != nil { - return err - } - - // Try to format the file. - if formatted, err := format.Source(out); err == nil { - out = formatted + log.Fatalf("internal error in diff.ApplyBytes: %v", err) } - if err := os.WriteFile(path, out, 0644); err != nil { - return err + // Attempt to format each file. + if formatted, err := format.Source(final); err == nil { + final = formatted } - } - return nil -} -// validateEdits returns a list of edits that is sorted and -// contains no duplicate edits. Returns the index of some -// overlapping adjacent edits if there is one and <0 if the -// edits are valid. -func validateEdits(edits []diff.Edit) ([]diff.Edit, int) { - if len(edits) == 0 { - return nil, -1 - } - equivalent := func(x, y diff.Edit) bool { - return x.Start == y.Start && x.End == y.End && x.New == y.New - } - diff.SortEdits(edits) - unique := []diff.Edit{edits[0]} - invalid := -1 - for i := 1; i < len(edits); i++ { - prev, cur := edits[i-1], edits[i] - // We skip over equivalent edits without considering them - // an error. This handles identical edits coming from the - // multiple ways of loading a package into a - // *go/packages.Packages for testing, e.g. packages "p" and "p [p.test]". - if !equivalent(prev, cur) { - unique = append(unique, cur) - if prev.End > cur.Start { - invalid = i + if showDiff { + // Since we formatted the file, we need to recompute the diff. + unified := diff.Unified(file+" (old)", file+" (new)", string(baseline), string(final)) + // TODO(adonovan): abstract the I/O. + os.Stdout.WriteString(unified) + + } else { + // write + totalFiles++ + // TODO(adonovan): abstract the I/O. + if err := os.WriteFile(file, final, 0644); err != nil { + log.Println(err) + continue } + filesUpdated++ } } - return unique, invalid -} - -// diff3Conflict returns an error describing two conflicting sets of -// edits on a file at path. -func diff3Conflict(path string, xlabel, ylabel string, xedits, yedits []diff.Edit) error { - contents, err := ioutil.ReadFile(path) - if err != nil { - return err - } - oldlabel, old := "base", string(contents) - xdiff, err := diff.ToUnified(oldlabel, xlabel, old, xedits, diff.DefaultContextLines) - if err != nil { - return err - } - ydiff, err := diff.ToUnified(oldlabel, ylabel, old, yedits, diff.DefaultContextLines) - if err != nil { - return err + // TODO(adonovan): consider returning a structured result that + // maps each SuggestedFix to its status: + // - invalid + // - secondary, not selected + // - applied + // - had conflicts. + // and a mapping from each affected file to: + // - its final/original content pair, and + // - whether formatting was successful. + // Then file writes and the UI can be applied by the caller + // in whatever form they like. + + // If victory was incomplete, report an error that indicates partial progress. + // + // badFixes > 0 indicates that we decided not to attempt some + // fixes due to conflicts or failure to read the source; still + // it's a relatively benign situation since the user can + // re-run the tool, and we may still make progress. + // + // filesUpdated < totalFiles indicates that some file updates + // failed. This should be rare, but is a serious error as it + // may apply half a fix, or leave the files in a bad state. + // + // These numbers are potentially misleading: + // The denominator includes duplicate conflicting fixes due to + // common files in packages "p" and "p [p.test]", which may + // have been fixed fixed and won't appear in the re-run. + // TODO(adonovan): eliminate identical fixes as an initial + // filtering step. + // + // TODO(adonovan): should we log that n files were updated in case of total victory? + if badFixes > 0 || filesUpdated < totalFiles { + if showDiff { + return fmt.Errorf("%d of %d fixes skipped (e.g. due to conflicts)", badFixes, len(fixes)) + } else { + return fmt.Errorf("applied %d of %d fixes; %d files updated. (Re-run the command to apply more.)", + goodFixes, len(fixes), filesUpdated) + } } - return fmt.Errorf("conflicting edits from %s and %s on %s\nfirst edits:\n%s\nsecond edits:\n%s", - xlabel, ylabel, path, xdiff, ydiff) + return nil } // needFacts reports whether any analysis required by the specified set diff --git a/go/analysis/internal/checker/checker_test.go b/go/analysis/internal/checker/checker_test.go index 76d45adceef..fcf5f66e03e 100644 --- a/go/analysis/internal/checker/checker_test.go +++ b/go/analysis/internal/checker/checker_test.go @@ -5,8 +5,6 @@ package checker_test import ( - "fmt" - "go/ast" "os" "path/filepath" "reflect" @@ -17,7 +15,6 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/internal/checker" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/internal/testenv" "golang.org/x/tools/internal/testfiles" "golang.org/x/tools/txtar" @@ -68,101 +65,6 @@ func Foo() { defer cleanup() } -var renameAnalyzer = &analysis.Analyzer{ - Name: "rename", - Requires: []*analysis.Analyzer{inspect.Analyzer}, - Run: run, - Doc: "renames symbols named bar to baz", - RunDespiteErrors: true, -} - -var otherAnalyzer = &analysis.Analyzer{ // like analyzer but with a different Name. - Name: "other", - Requires: []*analysis.Analyzer{inspect.Analyzer}, - Run: run, - Doc: "renames symbols named bar to baz only in package 'other'", -} - -func run(pass *analysis.Pass) (interface{}, error) { - // TODO(adonovan): replace this entangled test with something completely data-driven. - const ( - from = "bar" - to = "baz" - conflict = "conflict" // add conflicting edits to package conflict. - duplicate = "duplicate" // add duplicate edits to package conflict. - other = "other" // add conflicting edits to package other from different analyzers. - ) - - if pass.Analyzer.Name == other { - if pass.Pkg.Name() != other { - return nil, nil // only apply Analyzer other to packages named other - } - } - - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - nodeFilter := []ast.Node{(*ast.Ident)(nil)} - inspect.Preorder(nodeFilter, func(n ast.Node) { - ident := n.(*ast.Ident) - if ident.Name == from { - msg := fmt.Sprintf("renaming %q to %q", from, to) - edits := []analysis.TextEdit{ - {Pos: ident.Pos(), End: ident.End(), NewText: []byte(to)}, - } - switch pass.Pkg.Name() { - case conflict: - // Conflicting edits are legal, so long as they appear in different fixes. - pass.Report(analysis.Diagnostic{ - Pos: ident.Pos(), - End: ident.End(), - Message: msg, - SuggestedFixes: []analysis.SuggestedFix{{ - Message: msg, TextEdits: []analysis.TextEdit{ - {Pos: ident.Pos() - 1, End: ident.End(), NewText: []byte(to)}, - }, - }}, - }) - pass.Report(analysis.Diagnostic{ - Pos: ident.Pos(), - End: ident.End(), - Message: msg, - SuggestedFixes: []analysis.SuggestedFix{{ - Message: msg, TextEdits: []analysis.TextEdit{ - {Pos: ident.Pos(), End: ident.End() - 1, NewText: []byte(to)}, - }, - }}, - }) - pass.Report(analysis.Diagnostic{ - Pos: ident.Pos(), - End: ident.End(), - Message: msg, - SuggestedFixes: []analysis.SuggestedFix{{ - Message: msg, TextEdits: []analysis.TextEdit{ - {Pos: ident.Pos(), End: ident.End(), NewText: []byte("lorem ipsum")}, - }, - }}, - }) - return - - case duplicate: - // Duplicate (non-insertion) edits are disallowed, - // so this is a buggy analyzer, and validatedFixes should reject it. - edits = append(edits, edits...) - case other: - if pass.Analyzer.Name == other { - edits[0].Pos++ // shift by one to mismatch analyzer and other - } - } - pass.Report(analysis.Diagnostic{ - Pos: ident.Pos(), - End: ident.End(), - Message: msg, - SuggestedFixes: []analysis.SuggestedFix{{Message: msg, TextEdits: edits}}}) - } - }) - - return nil, nil -} - func TestRunDespiteErrors(t *testing.T) { testenv.NeedsGoPackages(t) testenv.RedirectStderr(t) // associate checker.Run output with this test diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index 4063aed35cd..8fb7506ac70 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -5,47 +5,44 @@ package checker_test import ( + "bytes" "flag" "fmt" + "go/ast" "go/token" "log" "os" "os/exec" - "path" + "path/filepath" "regexp" "runtime" + "slices" "strings" "testing" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/checker" "golang.org/x/tools/go/analysis/multichecker" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/expect" "golang.org/x/tools/go/packages" + "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/testenv" + "golang.org/x/tools/internal/testfiles" + "golang.org/x/tools/txtar" ) -// These are the analyzers available to the multichecker. -// (Tests may add more in init functions as needed.) -var candidates = map[string]*analysis.Analyzer{ - renameAnalyzer.Name: renameAnalyzer, - otherAnalyzer.Name: otherAnalyzer, -} - func TestMain(m *testing.M) { - // If the ANALYZERS=a,..,z environment is set, then this - // process should behave like a multichecker with the - // named analyzers. - if s, ok := os.LookupEnv("ANALYZERS"); ok { - var analyzers []*analysis.Analyzer - for _, name := range strings.Split(s, ",") { - a := candidates[name] - if a == nil { - log.Fatalf("no such analyzer: %q", name) - } - analyzers = append(analyzers, a) - } - multichecker.Main(analyzers...) + // If the CHECKER_TEST_CHILD environment variable is set, + // this process should behave like a multichecker. + // Analyzers are selected by flags. + if _, ok := os.LookupEnv("CHECKER_TEST_CHILD"); ok { + multichecker.Main( + markerAnalyzer, + noendAnalyzer, + renameAnalyzer, + ) panic("unreachable") } @@ -60,126 +57,6 @@ const ( exitCodeDiagnostics = 3 // diagnostics were reported ) -// fix runs a multichecker subprocess with -fix in the specified -// directory, applying the comma-separated list of named analyzers to -// the packages matching the patterns. It returns the CombinedOutput. -func fix(t *testing.T, dir, analyzers string, wantExit int, patterns ...string) string { - testenv.NeedsExec(t) - testenv.NeedsTool(t, "go") - - cmd := exec.Command(os.Args[0], "-fix") - cmd.Args = append(cmd.Args, patterns...) - cmd.Env = append(os.Environ(), - "ANALYZERS="+analyzers, - "GOPATH="+dir, - "GO111MODULE=off", - "GOPROXY=off") - - clean := func(s string) string { - return strings.ReplaceAll(s, os.TempDir(), "os.TempDir/") - } - outBytes, err := cmd.CombinedOutput() - switch err := err.(type) { - case nil: - // success - case *exec.ExitError: - if code := err.ExitCode(); code != wantExit { - // plan9 ExitCode() currently only returns 0 for success or 1 for failure - if !(runtime.GOOS == "plan9" && wantExit != exitCodeSuccess && code != exitCodeSuccess) { - t.Errorf("exit code was %d, want %d", code, wantExit) - } - } - default: - t.Fatalf("failed to execute multichecker: %v", err) - } - - out := clean(string(outBytes)) - t.Logf("$ %s\n%s", clean(fmt.Sprint(cmd)), out) - - return out -} - -// TestFixes ensures that checker.Run applies fixes correctly. -// This test fork/execs the main function above. -func TestFixes(t *testing.T) { - files := map[string]string{ - "rename/foo.go": `package rename - -func Foo() { - bar := 12 - _ = bar -} - -// the end -`, - "rename/intestfile_test.go": `package rename - -func InTestFile() { - bar := 13 - _ = bar -} - -// the end -`, - "rename/foo_test.go": `package rename_test - -func Foo() { - bar := 14 - _ = bar -} - -// the end -`, - } - fixed := map[string]string{ - "rename/foo.go": `package rename - -func Foo() { - baz := 12 - _ = baz -} - -// the end -`, - "rename/intestfile_test.go": `package rename - -func InTestFile() { - baz := 13 - _ = baz -} - -// the end -`, - "rename/foo_test.go": `package rename_test - -func Foo() { - baz := 14 - _ = baz -} - -// the end -`, - } - dir, cleanup, err := analysistest.WriteFiles(files) - if err != nil { - t.Fatalf("Creating test files failed with %s", err) - } - defer cleanup() - - fix(t, dir, "rename,other", exitCodeDiagnostics, "rename") - - for name, want := range fixed { - path := path.Join(dir, "src", name) - contents, err := os.ReadFile(path) - if err != nil { - t.Errorf("error reading %s: %v", path, err) - } - if got := string(contents); got != want { - t.Errorf("contents of %s file did not match expectations. got=%s, want=%s", path, got, want) - } - } -} - // TestReportInvalidDiagnostic tests that a call to pass.Report with // certain kind of invalid diagnostic (e.g. conflicting fixes) // promptly results in a panic. @@ -291,142 +168,420 @@ func TestReportInvalidDiagnostic(t *testing.T) { } } -// TestConflict ensures that checker.Run detects conflicts correctly. -// This test fork/execs the main function above. -func TestConflict(t *testing.T) { - files := map[string]string{ - "conflict/foo.go": `package conflict - -func Foo() { - bar := 12 - _ = bar -} +// TestScript runs script-driven tests in testdata/*.txt. +// Each file is a txtar archive, expanded to a temporary directory. +// +// The comment section of the archive is a script, with the following +// commands: +// +// # comment +// ignored +// blank line +// ignored +// skip k=v... +// Skip the test if any k=v string is a substring of the string +// "GOOS=darwin GOARCH=arm64" appropriate to the current build. +// checker args... +// Run the checker command with the specified space-separated +// arguments; this fork+execs the [TestMain] function above. +// If the archive has a "stdout" section, its contents must +// match the stdout output of the checker command. +// Do NOT use this for testing -diff: tests should not +// rely on the particulars of the diff algorithm. +// exit int +// Assert that previous checker command had this exit code. +// stderr regexp +// Assert that stderr output from previous checker run matches this pattern. +// +// The script must include at least one 'checker' command. +func TestScript(t *testing.T) { + testenv.NeedsExec(t) + testenv.NeedsGoPackages(t) -// the end -`, - } - dir, cleanup, err := analysistest.WriteFiles(files) + txtfiles, err := filepath.Glob("testdata/*.txt") if err != nil { - t.Fatalf("Creating test files failed with %s", err) + t.Fatal(err) } - defer cleanup() - - out := fix(t, dir, "rename,other", exitCodeFailed, "conflict") + for _, txtfile := range txtfiles { + t.Run(txtfile, func(t *testing.T) { + t.Parallel() + + // Expand archive into tmp tree. + ar, err := txtar.ParseFile(txtfile) + if err != nil { + t.Fatal(err) + } + fs, err := txtar.FS(ar) + if err != nil { + t.Fatal(err) + } + dir := testfiles.CopyToTmp(t, fs) + + // Parse txtar comment as a script. + const noExitCode = -999 + var ( + // state variables operated on by script + lastExitCode = noExitCode + lastStderr string + ) + for i, line := range strings.Split(string(ar.Comment), "\n") { + line = strings.TrimSpace(line) + if line == "" || line[0] == '#' { + continue // skip blanks and comments + } - pattern := `conflicting edits from rename and rename on .*foo.go` - matched, err := regexp.MatchString(pattern, out) - if err != nil { - t.Errorf("error matching pattern %s: %v", pattern, err) - } else if !matched { - t.Errorf("output did not match pattern: %s", pattern) + command, rest, _ := strings.Cut(line, " ") + prefix := fmt.Sprintf("%s:%d: %s", txtfile, i+1, command) // for error messages + switch command { + case "checker": + cmd := exec.Command(os.Args[0], strings.Fields(rest)...) + cmd.Dir = dir + cmd.Stdout = new(strings.Builder) + cmd.Stderr = new(strings.Builder) + cmd.Env = append(os.Environ(), "CHECKER_TEST_CHILD=1", "GOPROXY=off") + if err := cmd.Run(); err != nil { + if err, ok := err.(*exec.ExitError); ok { + lastExitCode = err.ExitCode() + // fall through + } else { + t.Fatalf("%s: failed to execute checker: %v (%s)", prefix, err, cmd) + } + } else { + lastExitCode = 0 // success + } + + // Eliminate nondeterministic strings from the output. + clean := func(x any) string { + s := fmt.Sprint(x) + pwd, _ := os.Getwd() + if realDir, err := filepath.EvalSymlinks(dir); err == nil { + // Work around checker's packages.Load failing to + // set Config.Dir to dir, causing the filenames + // of loaded packages not to be a subdir of dir. + s = strings.ReplaceAll(s, realDir, dir) + } + s = strings.ReplaceAll(s, dir, string(os.PathSeparator)+"TMP") + s = strings.ReplaceAll(s, pwd, string(os.PathSeparator)+"PWD") + s = strings.ReplaceAll(s, cmd.Path, filepath.Base(cmd.Path)) + return s + } + + lastStderr = clean(cmd.Stderr) + stdout := clean(cmd.Stdout) + + // Detect bad markers out of band: + // though they cause a non-zero exit, + // that may be expected. + if strings.Contains(lastStderr, badMarker) { + t.Errorf("marker analyzer encountered errors; stderr=%s", lastStderr) + } + + // debugging + if false { + t.Logf("%s: $ %s\nstdout:\n%s\nstderr:\n%s", prefix, clean(cmd), stdout, lastStderr) + } + + unified := func(xlabel, ylabel string, x, y []byte) string { + x = append(slices.Clip(bytes.TrimSpace(x)), '\n') + y = append(slices.Clip(bytes.TrimSpace(y)), '\n') + return diff.Unified(xlabel, ylabel, string(x), string(y)) + } + + // Check stdout, if there's a section of that name. + // + // Do not use this for testing -diff! It exposes tests to the + // internals of our (often suboptimal) diff algorithm. + // Instead, use the want/ mechanism. + if f := section(ar, "stdout"); f != nil { + got, want := []byte(stdout), f.Data + if diff := unified("got", "want", got, want); diff != "" { + t.Errorf("%s: unexpected stdout: -- got --\n%s-- want --\n%s-- diff --\n%s", + prefix, + got, want, diff) + } + } + + for _, f := range ar.Files { + // For each file named want/X, assert that the + // current content of X now equals want/X. + if filename, ok := strings.CutPrefix(f.Name, "want/"); ok { + fixed, err := os.ReadFile(filepath.Join(dir, filename)) + if err != nil { + t.Errorf("reading %s: %v", filename, err) + continue + } + var original []byte + if f := section(ar, filename); f != nil { + original = f.Data + } + want := f.Data + if diff := unified(filename+" (fixed)", filename+" (want)", fixed, want); diff != "" { + t.Errorf("%s: unexpected %s content:\n"+ + "-- original --\n%s\n"+ + "-- fixed --\n%s\n"+ + "-- want --\n%s\n"+ + "-- diff original fixed --\n%s\n"+ + "-- diff fixed want --\n%s", + prefix, filename, + original, + fixed, + want, + unified(filename+" (original)", filename+" (fixed)", original, fixed), + diff) + } + } + } + + case "skip": + config := fmt.Sprintf("GOOS=%s GOARCH=%s", runtime.GOOS, runtime.GOARCH) + for _, word := range strings.Fields(rest) { + if strings.Contains(config, word) { + t.Skip(word) + } + } + + case "exit": + if lastExitCode == noExitCode { + t.Fatalf("%s: no prior 'checker' command", prefix) + } + var want int + if _, err := fmt.Sscanf(rest, "%d", &want); err != nil { + t.Fatalf("%s: requires one numeric operand", prefix) + } + if want != lastExitCode { + // plan9 ExitCode() currently only returns 0 for success or 1 for failure + if !(runtime.GOOS == "plan9" && want != exitCodeSuccess && lastExitCode != exitCodeSuccess) { + t.Errorf("%s: exit code was %d, want %d", prefix, lastExitCode, want) + } + } + + case "stderr": + if lastExitCode == noExitCode { + t.Fatalf("%s: no prior 'checker' command", prefix) + } + if matched, err := regexp.MatchString(rest, lastStderr); err != nil { + t.Fatalf("%s: invalid regexp: %v", prefix, err) + } else if !matched { + t.Errorf("%s: output didn't match pattern %q:\n%s", prefix, rest, lastStderr) + } + + default: + t.Errorf("%s: unknown command", prefix) + } + } + if lastExitCode == noExitCode { + t.Errorf("test script contains no 'checker' command") + } + }) } +} - // No files updated - for name, want := range files { - path := path.Join(dir, "src", name) - contents, err := os.ReadFile(path) - if err != nil { - t.Errorf("error reading %s: %v", path, err) +const badMarker = "[bad marker]" + +// The marker analyzer generates fixes from @marker annotations in the +// source. Each marker is of the form: +// +// @message("pattern", "replacement) +// +// The "message" is used for both the Diagnostic.Message and +// SuggestedFix.Message field. Multiple markers with the same +// message form a single diagnostic and fix with a list of textedits. +// +// The "pattern" is a regular expression that must match on the +// current line (though it may extend beyond if the pattern starts +// with "(?s)"), and whose extent forms the TextEdit.{Pos,End} +// deletion. If the pattern contains one subgroup, its range will be +// used; this allows contextual matching. +// +// The "replacement" is a literal string that forms the +// TextEdit.NewText. +// +// Fixes are applied in the order they are first mentioned in the +// source. +var markerAnalyzer = &analysis.Analyzer{ + Name: "marker", + Doc: "doc", + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Run: func(pass *analysis.Pass) (_ any, err error) { + // Errors returned by this analyzer cause the + // checker command to exit non-zero, but that + // may be the expected outcome for other reasons + // (e.g. there were diagnostics). + // + // So, we report these errors out of band by logging + // them with a special badMarker string that the + // TestScript harness looks for, to ensure that the + // test fails in that case. + defer func() { + if err != nil { + log.Printf("%s: %v", badMarker, err) + } + }() + + // Parse all notes in the files. + var keys []string + edits := make(map[string][]analysis.TextEdit) + for _, file := range pass.Files { + tokFile := pass.Fset.File(file.FileStart) + content, err := pass.ReadFile(tokFile.Name()) + if err != nil { + return nil, err + } + notes, err := expect.ExtractGo(pass.Fset, file) + if err != nil { + return nil, err + } + for _, note := range notes { + edit, err := markerEdit(tokFile, content, note) + if err != nil { + return nil, fmt.Errorf("%s: %v", tokFile.Position(note.Pos), err) + } + // Preserve note order as it determines fix order. + if edits[note.Name] == nil { + keys = append(keys, note.Name) + } + edits[note.Name] = append(edits[note.Name], edit) + } } - if got := string(contents); got != want { - t.Errorf("contents of %s file updated. got=%s, want=%s", path, got, want) + + // Report each fix in its own Diagnostic. + for _, key := range keys { + edits := edits[key] + // debugging + if false { + log.Printf("%s: marker: @%s: %+v", pass.Fset.Position(edits[0].Pos), key, edits) + } + pass.Report(analysis.Diagnostic{ + Pos: edits[0].Pos, + End: edits[0].Pos, + Message: key, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: key, + TextEdits: edits, + }}, + }) } - } + return nil, nil + }, } -// TestOther ensures that checker.Run reports conflicts from -// distinct actions correctly. -// This test fork/execs the main function above. -func TestOther(t *testing.T) { - files := map[string]string{ - "other/foo.go": `package other - -func Foo() { - bar := 12 - _ = bar -} +// markerEdit returns the TextEdit denoted by note. +func markerEdit(tokFile *token.File, content []byte, note *expect.Note) (analysis.TextEdit, error) { + if len(note.Args) != 2 { + return analysis.TextEdit{}, fmt.Errorf("got %d args, want @%s(pattern, replacement)", len(note.Args), note.Name) + } -// the end -`, + pattern, ok := note.Args[0].(string) + if !ok { + return analysis.TextEdit{}, fmt.Errorf("got %T for pattern, want string", note.Args[0]) } - dir, cleanup, err := analysistest.WriteFiles(files) + rx, err := regexp.Compile(pattern) if err != nil { - t.Fatalf("Creating test files failed with %s", err) + return analysis.TextEdit{}, fmt.Errorf("invalid pattern regexp: %v", err) } - defer cleanup() - - // The 'rename' and 'other' analyzers suggest conflicting fixes. - out := fix(t, dir, "rename,other", exitCodeFailed, "other") - pattern := `.*conflicting edits from other and rename on .*foo.go` - matched, err := regexp.MatchString(pattern, out) - if err != nil { - t.Errorf("error matching pattern %s: %v", pattern, err) - } else if !matched { - t.Errorf("output did not match pattern: %s", pattern) + // Match the pattern against the current line. + lineStart := tokFile.LineStart(tokFile.Position(note.Pos).Line) + lineStartOff := tokFile.Offset(lineStart) + lineEndOff := tokFile.Offset(note.Pos) + matches := rx.FindSubmatchIndex(content[lineStartOff:]) + if len(matches) == 0 { + return analysis.TextEdit{}, fmt.Errorf("no match for regexp %q", rx) } - - // No files updated - for name, want := range files { - path := path.Join(dir, "src", name) - contents, err := os.ReadFile(path) - if err != nil { - t.Errorf("error reading %s: %v", path, err) - } - if got := string(contents); got != want { - t.Errorf("contents of %s file updated. got=%s, want=%s", path, got, want) - } + var start, end int // line-relative offset + switch len(matches) { + case 2: + // no subgroups: return the range of the regexp expression + start, end = matches[0], matches[1] + case 4: + // one subgroup: return its range + start, end = matches[2], matches[3] + default: + return analysis.TextEdit{}, fmt.Errorf("invalid location regexp %q: expect either 0 or 1 subgroups, got %d", rx, len(matches)/2-1) + } + if start > lineEndOff-lineStartOff { + // The start of the match must be between the start of the line and the + // marker position (inclusive). + return analysis.TextEdit{}, fmt.Errorf("no matching range found starting on the current line") } -} -// TestNoEnd tests that a missing SuggestedFix.End position is -// correctly interpreted as if equal to SuggestedFix.Pos (see issue #64199). -func TestNoEnd(t *testing.T) { - files := map[string]string{ - "a/a.go": "package a\n\nfunc F() {}", + replacement, ok := note.Args[1].(string) + if !ok { + return analysis.TextEdit{}, fmt.Errorf("second argument must be pattern, got %T", note.Args[1]) } - dir, cleanup, err := analysistest.WriteFiles(files) - if err != nil { - t.Fatalf("Creating test files failed with %s", err) + + // debugging: show matched portion + if false { + log.Printf("%s: %s: r%q (%q) -> %q", + tokFile.Position(note.Pos), + note.Name, + pattern, + content[lineStartOff+start:lineStartOff+end], + replacement) } - defer cleanup() - fix(t, dir, "noend", exitCodeDiagnostics, "a") + return analysis.TextEdit{ + Pos: lineStart + token.Pos(start), + End: lineStart + token.Pos(end), + NewText: []byte(replacement), + }, nil +} - got, err := os.ReadFile(path.Join(dir, "src/a/a.go")) - if err != nil { - t.Fatal(err) - } - const want = "package a\n\n/*hello*/\nfunc F() {}\n" - if string(got) != want { - t.Errorf("new file contents were <<%s>>, want <<%s>>", got, want) - } +var renameAnalyzer = &analysis.Analyzer{ + Name: "rename", + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Doc: "renames symbols named bar to baz", + RunDespiteErrors: true, + Run: func(pass *analysis.Pass) (any, error) { + const ( + from = "bar" + to = "baz" + ) + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + nodeFilter := []ast.Node{(*ast.Ident)(nil)} + inspect.Preorder(nodeFilter, func(n ast.Node) { + ident := n.(*ast.Ident) + if ident.Name == from { + msg := fmt.Sprintf("renaming %q to %q", from, to) + pass.Report(analysis.Diagnostic{ + Pos: ident.Pos(), + End: ident.End(), + Message: msg, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: msg, + TextEdits: []analysis.TextEdit{{ + Pos: ident.Pos(), + End: ident.End(), + NewText: []byte(to), + }}, + }}, + }) + } + }) + return nil, nil + }, } -func init() { - candidates["noend"] = &analysis.Analyzer{ - Name: "noend", - Doc: "inserts /*hello*/ before first decl", - Run: func(pass *analysis.Pass) (any, error) { - decl := pass.Files[0].Decls[0] - pass.Report(analysis.Diagnostic{ - Pos: decl.Pos(), - End: token.NoPos, +var noendAnalyzer = &analysis.Analyzer{ + Name: "noend", + Doc: "inserts /*hello*/ before first decl", + Run: func(pass *analysis.Pass) (any, error) { + decl := pass.Files[0].Decls[0] + pass.Report(analysis.Diagnostic{ + Pos: decl.Pos(), + End: token.NoPos, + Message: "say hello", + SuggestedFixes: []analysis.SuggestedFix{{ Message: "say hello", - SuggestedFixes: []analysis.SuggestedFix{{ - Message: "say hello", - TextEdits: []analysis.TextEdit{ - { - Pos: decl.Pos(), - End: token.NoPos, - NewText: []byte("/*hello*/"), - }, - }, + TextEdits: []analysis.TextEdit{{ + Pos: decl.Pos(), + End: token.NoPos, + NewText: []byte("/*hello*/"), }}, - }) - return nil, nil - }, - } + }}, + }) + return nil, nil + }, } // panics asserts that f() panics with with a value whose printed form matches the regexp want. @@ -442,3 +597,13 @@ func panics(t *testing.T, want string, f func()) { }() f() } + +// section returns the named archive section, or nil. +func section(ar *txtar.Archive, name string) *txtar.File { + for i, f := range ar.Files { + if f.Name == name { + return &ar.Files[i] + } + } + return nil +} diff --git a/go/analysis/internal/checker/testdata/conflict.txt b/go/analysis/internal/checker/testdata/conflict.txt new file mode 100644 index 00000000000..c4a4b13b9ab --- /dev/null +++ b/go/analysis/internal/checker/testdata/conflict.txt @@ -0,0 +1,30 @@ +# Conflicting edits are legal, so long as they appear in different fixes. +# The driver will apply them in some order, and discard those that conflict. +# +# fix1 appears first, so is applied first; it succeeds. +# fix2 and fix3 conflict with it and are rejected. + +checker -marker -fix example.com/a +exit 1 +stderr applied 1 of 3 fixes; 1 files updated...Re-run + +-- go.mod -- +module example.com + +go 1.22 + +-- a/a.go -- +package a + +func f() { + bar := 12 //@ fix1("\tbar", "baz"), fix2("ar ", "baz"), fix3("bar", "lorem ipsum") + _ = bar //@ fix1(" bar", "baz") +} + +-- want/a/a.go -- +package a + +func f() { + baz := 12 //@ fix1("\tbar", "baz"), fix2("ar ", "baz"), fix3("bar", "lorem ipsum") + _ = baz //@ fix1(" bar", "baz") +} diff --git a/go/analysis/internal/checker/testdata/diff.txt b/go/analysis/internal/checker/testdata/diff.txt new file mode 100644 index 00000000000..5a0c9c2a3b2 --- /dev/null +++ b/go/analysis/internal/checker/testdata/diff.txt @@ -0,0 +1,36 @@ +# Basic test of -diff: ensure that stdout contains a diff, +# and the file system is unchanged. +# +# (Most tests of fixes should use want/* not -diff + stdout +# to avoid dependency on the diff algorithm.) +# +# File slashes assume non-Windows. + +skip GOOS=windows +checker -rename -fix -diff example.com/p +exit 3 +stderr renaming "bar" to "baz" + +-- go.mod -- +module example.com +go 1.22 + +-- p/p.go -- +package p + +var bar int + +-- want/p/p.go -- +package p + +var bar int + +-- stdout -- +--- /TMP/p/p.go (old) ++++ /TMP/p/p.go (new) +@@ -1,4 +1,3 @@ + package p + +-var bar int +- ++var baz int diff --git a/go/analysis/internal/checker/testdata/fixes.txt b/go/analysis/internal/checker/testdata/fixes.txt new file mode 100644 index 00000000000..89f245f9ace --- /dev/null +++ b/go/analysis/internal/checker/testdata/fixes.txt @@ -0,0 +1,59 @@ +# Ensure that fixes are applied correctly, in +# particular when processing duplicate fixes for overlapping packages +# in the same directory ("p", "p [p.test]", "p_test [p.test]"). + +checker -rename -fix example.com/p +exit 3 +stderr renaming "bar" to "baz" + +-- go.mod -- +module example.com +go 1.22 + +-- p/p.go -- +package p + +func Foo() { + bar := 12 + _ = bar +} + +-- p/p_test.go -- +package p + +func InTestFile() { + bar := 13 + _ = bar +} + +-- p/p_x_test.go -- +package p_test + +func Foo() { + bar := 14 + _ = bar +} + +-- want/p/p.go -- +package p + +func Foo() { + baz := 12 + _ = baz +} + +-- want/p/p_test.go -- +package p + +func InTestFile() { + baz := 13 + _ = baz +} + +-- want/p/p_x_test.go -- +package p_test + +func Foo() { + baz := 14 + _ = baz +} diff --git a/go/analysis/internal/checker/testdata/importdup.txt b/go/analysis/internal/checker/testdata/importdup.txt new file mode 100644 index 00000000000..e1783777858 --- /dev/null +++ b/go/analysis/internal/checker/testdata/importdup.txt @@ -0,0 +1,29 @@ +# Test that duplicate imports--and, more generally, duplicate +# identical insertions--are coalesced. + +checker -marker -fix example.com/a +exit 3 + +-- go.mod -- +module example.com +go 1.22 + +-- a/a.go -- +package a + +import ( + _ "errors" + //@ fix1("()//", `"foo"`), fix2("()//", `"foo"`) +) + +func f() {} //@ fix1("()}", "n++"), fix2("()}", "n++") + +-- want/a/a.go -- +package a + +import ( + _ "errors" + "foo" //@ fix1("()//", `"foo"`), fix2("()//", `"foo"`) +) + +func f() { n++ } //@ fix1("()}", "n++"), fix2("()}", "n++") diff --git a/go/analysis/internal/checker/testdata/importdup2.txt b/go/analysis/internal/checker/testdata/importdup2.txt new file mode 100644 index 00000000000..118fdc0184b --- /dev/null +++ b/go/analysis/internal/checker/testdata/importdup2.txt @@ -0,0 +1,60 @@ +# Test of import de-duplication behavior. +# +# In packages a and b, there are three fixes, +# each adding one of two imports, but in different order. +# +# In package a, the fixes are [foo, foo, bar], +# and they are resolved as follows: +# - foo is applied -> [foo] +# - foo is coalesced -> [foo] +# - bar is applied -> [foo bar] +# The result is then formatted to [bar foo]. +# +# In package b, the fixes are [foo, bar, foo]: +# - foo is applied -> [foo] +# - bar is applied -> [foo bar] +# - foo is coalesced -> [foo bar] +# The same result is again formatted to [bar foo]. +# +# In more complex examples, the result +# may be more subtly order-dependent. + +checker -marker -fix example.com/a example.com/b +exit 3 + +-- go.mod -- +module example.com +go 1.22 + +-- a/a.go -- +package a + +import ( + //@ fix1("()//", "\"foo\"\n"), fix2("()//", "\"foo\"\n"), fix3("()//", "\"bar\"\n") +) + +-- want/a/a.go -- +package a + +import ( + "bar" + "foo" + // @ fix1("()//", "\"foo\"\n"), fix2("()//", "\"foo\"\n"), fix3("()//", "\"bar\"\n") +) + +-- b/b.go -- +package b + +import ( + //@ fix1("()//", "\"foo\"\n"), fix2("()//", "\"bar\"\n"), fix3("()//", "\"foo\"\n") +) + +-- want/b/b.go -- +package b + +import ( + "bar" + "foo" + // @ fix1("()//", "\"foo\"\n"), fix2("()//", "\"bar\"\n"), fix3("()//", "\"foo\"\n") +) + diff --git a/go/analysis/internal/checker/testdata/json.txt b/go/analysis/internal/checker/testdata/json.txt new file mode 100644 index 00000000000..8e6091aebbc --- /dev/null +++ b/go/analysis/internal/checker/testdata/json.txt @@ -0,0 +1,42 @@ +# Test basic JSON output. +# +# File slashes assume non-Windows. + +skip GOOS=windows +checker -rename -json example.com/p +exit 0 + +-- go.mod -- +module example.com +go 1.22 + +-- p/p.go -- +package p + +func f(bar int) {} + +-- stdout -- +{ + "example.com/p": { + "rename": [ + { + "posn": "/TMP/p/p.go:3:8", + "message": "renaming \"bar\" to \"baz\"", + "suggested_fixes": [ + { + "message": "renaming \"bar\" to \"baz\"", + "edits": [ + { + "filename": "/TMP/p/p.go", + "start": 18, + "end": 21, + "new": "baz" + } + ] + } + ] + } + ] + } +} + diff --git a/go/analysis/internal/checker/testdata/noend.txt b/go/analysis/internal/checker/testdata/noend.txt new file mode 100644 index 00000000000..2d6be074565 --- /dev/null +++ b/go/analysis/internal/checker/testdata/noend.txt @@ -0,0 +1,21 @@ +# Test that a missing SuggestedFix.End position is correctly +# interpreted as if equal to SuggestedFix.Pos (see issue #64199). + +checker -noend -fix example.com/a +exit 3 +stderr say hello + +-- go.mod -- +module example.com +go 1.22 + +-- a/a.go -- +package a + +func f() {} + +-- want/a/a.go -- +package a + +/*hello*/ +func f() {} diff --git a/go/analysis/internal/checker/testdata/overlap.txt b/go/analysis/internal/checker/testdata/overlap.txt new file mode 100644 index 00000000000..f556ef308b9 --- /dev/null +++ b/go/analysis/internal/checker/testdata/overlap.txt @@ -0,0 +1,34 @@ +# This test exercises an edge case of merging. +# +# Two analyzers generate overlapping fixes for this package: +# - 'rename' changes "bar" to "baz" +# - 'marker' changes "ar" to "baz" +# Historically this used to cause a conflict, but as it happens, +# the new merge algorithm splits the rename fix, since it overlaps +# the marker fix, into two subedits: +# - a deletion of "b" and +# - an edit from "ar" to "baz". +# The deletion is of course nonoverlapping, and the edit, +# by happy chance, is identical to the marker fix, so the two +# are coalesced. +# +# (This is a pretty unlikely situation, but it corresponds +# to a historical test, TestOther, that used to check for +# a conflict, and it seemed wrong to delete it without explanation.) + +checker -rename -marker -fix example.com/a +exit 3 + +-- go.mod -- +module example.com +go 1.22 + +-- a/a.go -- +package a + +func f(bar int) {} //@ fix("ar", "baz") + +-- want/a/a.go -- +package a + +func f(baz int) {} //@ fix("ar", "baz") From 82317cea8a3807ada2a9b6a794188f227b55595f Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Feb 2025 12:38:02 -0500 Subject: [PATCH 119/309] gopls/internal/analysis/modernize: slices.Delete: import slices We forgot to add a call to AddImport: yet more evidence that our test framework needs to assert that fixes preserve well-typedness. RunWithSuggestedFix does a poor job of merging imports, so there are many duplicates in the golden file, but I will port the recent work in internal/checker to it. Change-Id: I976b52242772c2796b0cd54aab98d0710dbc2de9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647697 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam --- .../internal/analysis/modernize/slicesdelete.go | 13 ++++++++----- .../src/slicesdelete/slicesdelete.go.golden | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/gopls/internal/analysis/modernize/slicesdelete.go b/gopls/internal/analysis/modernize/slicesdelete.go index f1f96c7d5fc..c9e2da0eb60 100644 --- a/gopls/internal/analysis/modernize/slicesdelete.go +++ b/gopls/internal/analysis/modernize/slicesdelete.go @@ -13,6 +13,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/analysisinternal" ) // The slicesdelete pass attempts to replace instances of append(s[:i], s[i+k:]...) @@ -22,7 +23,8 @@ import ( func slicesdelete(pass *analysis.Pass) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) info := pass.TypesInfo - report := func(call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) { + report := func(file *ast.File, call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) { + slicesName, edits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices") pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), @@ -30,12 +32,12 @@ func slicesdelete(pass *analysis.Pass) { Message: "Replace append with slices.Delete", SuggestedFixes: []analysis.SuggestedFix{{ Message: "Replace append with slices.Delete", - TextEdits: []analysis.TextEdit{ + TextEdits: append(edits, []analysis.TextEdit{ // Change name of called function. { Pos: call.Fun.Pos(), End: call.Fun.End(), - NewText: []byte("slices.Delete"), + NewText: []byte(slicesName + ".Delete"), }, // Delete ellipsis. { @@ -69,11 +71,12 @@ func slicesdelete(pass *analysis.Pass) { Pos: slice2.Low.End(), End: slice2.Rbrack + 1, }, - }, + }...), }}, }) } for curFile := range filesUsing(inspect, info, "go1.21") { + file := curFile.Node().(*ast.File) for curCall := range curFile.Preorder((*ast.CallExpr)(nil)) { call := curCall.Node().(*ast.CallExpr) if id, ok := call.Fun.(*ast.Ident); ok && len(call.Args) == 2 { @@ -88,7 +91,7 @@ func slicesdelete(pass *analysis.Pass) { equalSyntax(slice1.X, slice2.X) && increasingSliceIndices(info, slice1.High, slice2.Low) { // Have append(s[:a], s[b:]...) where we can verify a < b. - report(call, slice1, slice2) + report(file, call, slice1, slice2) } } } diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden index 8c2f21a2782..9b2ba9a0b80 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden @@ -1,5 +1,21 @@ package slicesdelete +import "slices" + +import "slices" + +import "slices" + +import "slices" + +import "slices" + +import "slices" + +import "slices" + +import "slices" + var g struct{ f []int } func slicesdelete(test, other []byte, i int) { From 9c087d9bfa108039bfcaa605c16fe467d8c47940 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Feb 2025 12:20:05 -0500 Subject: [PATCH 120/309] internal/analysis/gofix: change "forward" back to "inline" For golang/go#32816. Change-Id: I02605efe2ca4db4fbef68ae26a57cb793ad5bf56 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647736 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/analyzers.md | 3 +- gopls/doc/release/v0.18.0.md | 9 +- gopls/internal/analysis/gofix/doc.go | 22 +++-- gopls/internal/analysis/gofix/gofix.go | 88 ++++++++----------- .../analysis/gofix/testdata/src/a/a.go | 56 ++++++------ .../analysis/gofix/testdata/src/a/a.go.golden | 56 ++++++------ .../analysis/gofix/testdata/src/b/b.go | 10 +-- .../analysis/gofix/testdata/src/b/b.go.golden | 10 +-- gopls/internal/doc/api.json | 4 +- 9 files changed, 112 insertions(+), 146 deletions(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 8764791561d..06ac853800f 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -294,8 +294,7 @@ Package documentation: [framepointer](https://pkg.go.dev/golang.org/x/tools/go/a ## `gofix`: apply fixes based on go:fix comment directives -The gofix analyzer inlines functions that are marked for inlining -and forwards constants that are marked for forwarding. +The gofix analyzer inlines functions and constants that are marked for inlining. Default: on. diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 9df179390f7..8d641a2104f 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -73,9 +73,8 @@ instead. ## New `gofix` analyzer -Gopls now reports when a function call should be inlined or a use of a constant -should be forwarded. -These diagnostics and the associated code actions are triggered by "//go:fix" +Gopls now reports when a function call or a use of a constant should be inlined. +These diagnostics and the associated code actions are triggered by "//go:fix inline" directives at the function and constant definitions. (See [the go:fix proposal](https://go.dev/issue/32816).) @@ -90,10 +89,10 @@ func Square(x int) int { return Pow(x, 2) } If gopls sees a call to `intmath.Square` in your code, it will suggest inlining it, and will offer a code action to do so. -The same feature works for constants, only the directive is "//go:fix forward". +The same feature works for constants. With a constant definition like this: ``` -//go:fix forward +//go:fix inline const Ptr = Pointer ``` gopls will suggest replacing `Ptr` in your code with `Pointer`. diff --git a/gopls/internal/analysis/gofix/doc.go b/gopls/internal/analysis/gofix/doc.go index c3c453f841b..a0c6a08ded9 100644 --- a/gopls/internal/analysis/gofix/doc.go +++ b/gopls/internal/analysis/gofix/doc.go @@ -4,16 +4,14 @@ /* Package gofix defines an Analyzer that inlines calls to functions -marked with a "//go:fix inline" doc comment, -and forwards uses of constants -marked with a "//go:fix forward" doc comment. +and uses of constants +marked with a "//go:fix inline" doc comment. # Analyzer gofix gofix: apply fixes based on go:fix comment directives -The gofix analyzer inlines functions that are marked for inlining -and forwards constants that are marked for forwarding. +The gofix analyzer inlines functions and constants that are marked for inlining. # Functions @@ -48,31 +46,31 @@ to enable automatic migration. # Constants -Given a constant that is marked for forwarding, like this one: +Given a constant that is marked for inlining, like this one: - //go:fix forward + //go:fix inline const Ptr = Pointer this analyzer will recommend that uses of Ptr should be replaced with Pointer. -As with inlining, forwarding can be used to replace deprecated constants and +As with functions, inlining can be used to replace deprecated constants and constants in obsolete packages. -A constant definition can be marked for forwarding only if it refers to another +A constant definition can be marked for inlining only if it refers to another named constant. -The "//go:fix forward" comment must appear before a single const declaration on its own, +The "//go:fix inline" comment must appear before a single const declaration on its own, as above; before a const declaration that is part of a group, as in this case: const ( C = 1 - //go:fix forward + //go:fix inline Ptr = Pointer ) or before a group, applying to every constant in the group: - //go:fix forward + //go:fix inline const ( Ptr = Pointer Val = Value diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 7021d5092e7..b7d80f9f4a5 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -33,7 +33,7 @@ var Analyzer = &analysis.Analyzer{ Doc: analysisinternal.MustExtractDoc(doc, "gofix"), URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", Run: run, - FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixForwardConstFact)}, + FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixInlineConstFact)}, Requires: []*analysis.Analyzer{inspect.Analyzer}, } @@ -64,19 +64,14 @@ func run(pass *analysis.Pass) (any, error) { // comment (the syntax proposed by #32816), // and export a fact for each one. inlinableFuncs := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) - forwardableConsts := make(map[*types.Const]*goFixForwardConstFact) + inlinableConsts := make(map[*types.Const]*goFixInlineConstFact) inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)} inspect.Preorder(nodeFilter, func(n ast.Node) { switch decl := n.(type) { case *ast.FuncDecl: - hasInline, hasForward := fixDirectives(decl.Doc) - if hasForward { - pass.Reportf(decl.Doc.Pos(), "use //go:fix inline for functions") - return - } - if !hasInline { + if !hasFixInline(decl.Doc) { return } content, err := readFile(decl) @@ -97,20 +92,12 @@ func run(pass *analysis.Pass) (any, error) { if decl.Tok != token.CONST { return } - declInline, declForward := fixDirectives(decl.Doc) - if declInline { - pass.Reportf(decl.Doc.Pos(), "use //go:fix forward for constants") - return - } - // Accept forward directives on the entire decl as well as individual specs. + declInline := hasFixInline(decl.Doc) + // Accept inline directives on the entire decl as well as individual specs. for _, spec := range decl.Specs { spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST - specInline, specForward := fixDirectives(spec.Doc) - if specInline { - pass.Reportf(spec.Doc.Pos(), "use //go:fix forward for constants") - return - } - if declForward || specForward { + specInline := hasFixInline(spec.Doc) + if declInline || specInline { for i, name := range spec.Names { if i >= len(spec.Values) { // Possible following an iota. @@ -120,21 +107,21 @@ func run(pass *analysis.Pass) (any, error) { var rhsID *ast.Ident switch e := val.(type) { case *ast.Ident: - // Constants defined with the predeclared iota cannot be forwarded. + // Constants defined with the predeclared iota cannot be inlined. if pass.TypesInfo.Uses[e] == builtinIota { - pass.Reportf(val.Pos(), "invalid //go:fix forward directive: const value is iota") + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") continue } rhsID = e case *ast.SelectorExpr: rhsID = e.Sel default: - pass.Reportf(val.Pos(), "invalid //go:fix forward directive: const value is not the name of another constant") + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") continue } lhs := pass.TypesInfo.Defs[name].(*types.Const) rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program - con := &goFixForwardConstFact{ + con := &goFixInlineConstFact{ RHSName: rhs.Name(), RHSPkgName: rhs.Pkg().Name(), RHSPkgPath: rhs.Pkg().Path(), @@ -142,7 +129,7 @@ func run(pass *analysis.Pass) (any, error) { if rhs.Pkg() == pass.Pkg { con.rhsObj = rhs } - forwardableConsts[lhs] = con + inlinableConsts[lhs] = con // Create a fact only if the LHS is exported and defined at top level. // We create a fact even if the RHS is non-exported, // so we can warn uses in other packages. @@ -155,8 +142,8 @@ func run(pass *analysis.Pass) (any, error) { } }) - // Pass 2. Inline each static call to an inlinable function, - // and forward each reference to a forwardable constant. + // Pass 2. Inline each static call to an inlinable function + // and each reference to an inlinable constant. // // TODO(adonovan): handle multiple diffs that each add the same import. for cur := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { @@ -231,14 +218,14 @@ func run(pass *analysis.Pass) (any, error) { } case *ast.Ident: - // If the identifier is a use of a forwardable constant, suggest forwarding it. + // If the identifier is a use of an inlinable constant, suggest inlining it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { - fcon, ok := forwardableConsts[con] + fcon, ok := inlinableConsts[con] if !ok { - var fact goFixForwardConstFact + var fact goFixInlineConstFact if pass.ImportObjectFact(con, &fact) { fcon = &fact - forwardableConsts[con] = fcon + inlinableConsts[con] = fcon } } if fcon == nil { @@ -253,7 +240,7 @@ func run(pass *analysis.Pass) (any, error) { curFile := currentFile(cur) // We have an identifier A here (n), possibly qualified by a package identifier (sel.X), - // and a forwardable "const A = B" elsewhere (fcon). + // and an inlinable "const A = B" elsewhere (fcon). // Consider replacing A with B. // Check that the expression we are inlining (B) means the same thing @@ -268,10 +255,10 @@ func run(pass *analysis.Pass) (any, error) { if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. - panic(fmt.Sprintf("no object for forwardable const %s RHS %s", n.Name, fcon.RHSName)) + panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, fcon.RHSName)) } if obj != fcon.rhsObj { - // "B" means something different here than at the forwardable const's scope. + // "B" means something different here than at the inlinable const's scope. continue } } @@ -304,9 +291,9 @@ func run(pass *analysis.Pass) (any, error) { pass.Report(analysis.Diagnostic{ Pos: pos, End: end, - Message: fmt.Sprintf("Constant %s should be forwarded", name), + Message: fmt.Sprintf("Constant %s should be inlined", name), SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Forward constant %s", name), + Message: fmt.Sprintf("Inline constant %s", name), TextEdits: edits, }}, }) @@ -317,20 +304,15 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } -// fixDirectives reports the presence of "//go:fix inline" and "//go:fix forward" -// directives in the comments. -func fixDirectives(cg *ast.CommentGroup) (inline, forward bool) { +// hasFixInline reports the presence of a "//go:fix inline" directive +// in the comments. +func hasFixInline(cg *ast.CommentGroup) bool { for _, d := range directives(cg) { - if d.Tool == "go" && d.Name == "fix" { - switch d.Args { - case "inline": - inline = true - case "forward": - forward = true - } + if d.Tool == "go" && d.Name == "fix" && d.Args == "inline" { + return true } } - return + return false } // A goFixInlineFuncFact is exported for each function marked "//go:fix inline". @@ -340,9 +322,9 @@ type goFixInlineFuncFact struct{ Callee *inline.Callee } func (f *goFixInlineFuncFact) String() string { return "goFixInline " + f.Callee.String() } func (*goFixInlineFuncFact) AFact() {} -// A goFixForwardConstFact is exported for each constant marked "//go:fix forward". -// It holds information about a forwardable constant. Gob-serializable. -type goFixForwardConstFact struct { +// A goFixInlineConstFact is exported for each constant marked "//go:fix inline". +// It holds information about an inlinable constant. Gob-serializable. +type goFixInlineConstFact struct { // Information about "const LHSName = RHSName". RHSName string RHSPkgPath string @@ -350,11 +332,11 @@ type goFixForwardConstFact struct { rhsObj types.Object // for current package } -func (c *goFixForwardConstFact) String() string { - return fmt.Sprintf("goFixForward const %q.%s", c.RHSPkgPath, c.RHSName) +func (c *goFixInlineConstFact) String() string { + return fmt.Sprintf("goFixInline const %q.%s", c.RHSPkgPath, c.RHSName) } -func (*goFixForwardConstFact) AFact() {} +func (*goFixInlineConstFact) AFact() {} func discard(string, ...any) {} diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index 009afd5c7af..ae486746e5b 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -18,87 +18,81 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` -//go:fix forward // want `use //go:fix inline for functions` -func Three() {} - // Constants. const Uno = 1 -//go:fix forward -const In1 = Uno // want In1: `goFixForward const "a".Uno` +//go:fix inline +const In1 = Uno // want In1: `goFixInline const "a".Uno` const ( no1 = one - //go:fix forward - In2 = one // want In2: `goFixForward const "a".one` + //go:fix inline + In2 = one // want In2: `goFixInline const "a".one` ) -//go:fix forward +//go:fix inline const ( in3 = one in4 = one - bad1 = 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` ) -//go:fix forward +//go:fix inline const in5, in6, bad2 = one, one, - one + 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` // Make sure we don't crash on iota consts, but still process the whole decl. // -//go:fix forward +//go:fix inline const ( - a = iota // want `invalid //go:fix forward directive: const value is iota` + a = iota // want `invalid //go:fix inline directive: const value is iota` b in7 = one ) func _() { - x := In1 // want `Constant In1 should be forwarded` - x = In2 // want `Constant In2 should be forwarded` - x = in3 // want `Constant in3 should be forwarded` - x = in4 // want `Constant in4 should be forwarded` - x = in5 // want `Constant in5 should be forwarded` - x = in6 // want `Constant in6 should be forwarded` - x = in7 // want `Constant in7 should be forwarded` + x := In1 // want `Constant In1 should be inlined` + x = In2 // want `Constant In2 should be inlined` + x = in3 // want `Constant in3 should be inlined` + x = in4 // want `Constant in4 should be inlined` + x = in5 // want `Constant in5 should be inlined` + x = in6 // want `Constant in6 should be inlined` + x = in7 // want `Constant in7 should be inlined` x = no1 _ = x - in1 := 1 // don't forward lvalues + in1 := 1 // don't inline lvalues _ = in1 } const ( x = 1 - //go:fix forward + //go:fix inline in8 = x ) func shadow() { var x int // shadows x at package scope - //go:fix forward - const a = iota // want `invalid //go:fix forward directive: const value is iota` + //go:fix inline + const a = iota // want `invalid //go:fix inline directive: const value is iota` const iota = 2 // Below this point, iota is an ordinary constant. - //go:fix forward + //go:fix inline const b = iota - x = a // a is defined with the predeclared iota, so it cannot be forwarded - x = b // want `Constant b should be forwarded` + x = a // a is defined with the predeclared iota, so it cannot be inlined + x = b // want `Constant b should be inlined` - // Don't offer to forward in8, because the result, "x", would mean something different + // Don't offer to inline in8, because the result, "x", would mean something different // in this scope than it does in the scope where in8 is defined. x = in8 _ = x } - -//go:fix inline // want `use //go:fix forward for constants` -const In9 = x diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index decbcdd561f..7d75a598fb7 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -18,87 +18,81 @@ const one = 1 //go:fix inline func (T) Two() int { return 2 } // want Two:`goFixInline \(a.T\).Two` -//go:fix forward // want `use //go:fix inline for functions` -func Three() {} - // Constants. const Uno = 1 -//go:fix forward -const In1 = Uno // want In1: `goFixForward const "a".Uno` +//go:fix inline +const In1 = Uno // want In1: `goFixInline const "a".Uno` const ( no1 = one - //go:fix forward - In2 = one // want In2: `goFixForward const "a".one` + //go:fix inline + In2 = one // want In2: `goFixInline const "a".one` ) -//go:fix forward +//go:fix inline const ( in3 = one in4 = one - bad1 = 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` + bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` ) -//go:fix forward +//go:fix inline const in5, in6, bad2 = one, one, - one + 1 // want `invalid //go:fix forward directive: const value is not the name of another constant` + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` // Make sure we don't crash on iota consts, but still process the whole decl. // -//go:fix forward +//go:fix inline const ( - a = iota // want `invalid //go:fix forward directive: const value is iota` + a = iota // want `invalid //go:fix inline directive: const value is iota` b in7 = one ) func _() { - x := Uno // want `Constant In1 should be forwarded` - x = one // want `Constant In2 should be forwarded` - x = one // want `Constant in3 should be forwarded` - x = one // want `Constant in4 should be forwarded` - x = one // want `Constant in5 should be forwarded` - x = one // want `Constant in6 should be forwarded` - x = one // want `Constant in7 should be forwarded` + x := Uno // want `Constant In1 should be inlined` + x = one // want `Constant In2 should be inlined` + x = one // want `Constant in3 should be inlined` + x = one // want `Constant in4 should be inlined` + x = one // want `Constant in5 should be inlined` + x = one // want `Constant in6 should be inlined` + x = one // want `Constant in7 should be inlined` x = no1 _ = x - in1 := 1 // don't forward lvalues + in1 := 1 // don't inline lvalues _ = in1 } const ( x = 1 - //go:fix forward + //go:fix inline in8 = x ) func shadow() { var x int // shadows x at package scope - //go:fix forward - const a = iota // want `invalid //go:fix forward directive: const value is iota` + //go:fix inline + const a = iota // want `invalid //go:fix inline directive: const value is iota` const iota = 2 // Below this point, iota is an ordinary constant. - //go:fix forward + //go:fix inline const b = iota - x = a // a is defined with the predeclared iota, so it cannot be forwarded - x = iota // want `Constant b should be forwarded` + x = a // a is defined with the predeclared iota, so it cannot be inlined + x = iota // want `Constant b should be inlined` - // Don't offer to forward in8, because the result, "x", would mean something different + // Don't offer to inline in8, because the result, "x", would mean something different // in this scope than it does in the scope where in8 is defined. x = in8 _ = x } - -//go:fix inline // want `use //go:fix forward for constants` -const In9 = x diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go b/gopls/internal/analysis/gofix/testdata/src/b/b.go index 72d4748a8d9..4bf9f0dc650 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go @@ -9,21 +9,21 @@ func f() { new(a.T).Two() // want `Call of \(a.T\).Two should be inlined` } -//go:fix forward +//go:fix inline const in2 = a.Uno -//go:fix forward +//go:fix inline const in3 = C // c.C, by dot import func g() { - x := a.In1 // want `Constant a\.In1 should be forwarded` + x := a.In1 // want `Constant a\.In1 should be inlined` a := 1 // Although the package identifier "a" is shadowed here, // a second import of "a" will be added with a new package identifer. - x = in2 // want `Constant in2 should be forwarded` + x = in2 // want `Constant in2 should be inlined` - x = in3 // want `Constant in3 should be forwarded` + x = in3 // want `Constant in3 should be inlined` _ = a _ = x diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index fdc83c5199c..dae869e52f3 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -15,21 +15,21 @@ func f() { _ = 2 // want `Call of \(a.T\).Two should be inlined` } -//go:fix forward +//go:fix inline const in2 = a.Uno -//go:fix forward +//go:fix inline const in3 = C // c.C, by dot import func g() { - x := a.Uno // want `Constant a\.In1 should be forwarded` + x := a.Uno // want `Constant a\.In1 should be inlined` a := 1 // Although the package identifier "a" is shadowed here, // a second import of "a" will be added with a new package identifer. - x = a0.Uno // want `Constant in2 should be forwarded` + x = a0.Uno // want `Constant in2 should be inlined` - x = c.C // want `Constant in3 should be forwarded` + x = c.C // want `Constant in3 should be inlined` _ = a _ = x diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 5079edc10a6..b83dfe4bde0 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -475,7 +475,7 @@ }, { "Name": "\"gofix\"", - "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", + "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions and constants that are marked for inlining.", "Default": "true" }, { @@ -1147,7 +1147,7 @@ }, { "Name": "gofix", - "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions that are marked for inlining\nand forwards constants that are marked for forwarding.", + "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions and constants that are marked for inlining.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", "Default": true }, From a1eb5fda89ed74fd8906e27269623f679dbd0ac1 Mon Sep 17 00:00:00 2001 From: Nick Ripley Date: Fri, 7 Feb 2025 12:43:19 -0500 Subject: [PATCH 121/309] go/analysis/passes/framepointer: support arm64 Add arm64 support to the framepointer vet check. Essentially use the same logic as the amd64 check: in instruction order, look at functions without frames, and fail if the functions write to the frame pointer register before reading it. Stop looking at a function on the first branch instruction. For golang/go#69838 Change-Id: If69be8a6eb5f9275df602c2c2ff644c338deaef2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/635338 Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee --- .../passes/framepointer/framepointer.go | 108 +++++++++++++++--- .../framepointer/testdata/src/a/asm_arm64.s | 42 +++++++ 2 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 go/analysis/passes/framepointer/testdata/src/a/asm_arm64.s diff --git a/go/analysis/passes/framepointer/framepointer.go b/go/analysis/passes/framepointer/framepointer.go index 6eff3a20fea..8012de99daa 100644 --- a/go/analysis/passes/framepointer/framepointer.go +++ b/go/analysis/passes/framepointer/framepointer.go @@ -10,6 +10,7 @@ import ( "go/build" "regexp" "strings" + "unicode" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" @@ -24,15 +25,97 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -var ( - re = regexp.MustCompile - asmWriteBP = re(`,\s*BP$`) // TODO: can have false positive, e.g. for TESTQ BP,BP. Seems unlikely. - asmMentionBP = re(`\bBP\b`) - asmControlFlow = re(`^(J|RET)`) -) +// Per-architecture checks for instructions. +// Assume comments, leading and trailing spaces are removed. +type arch struct { + isFPWrite func(string) bool + isFPRead func(string) bool + isBranch func(string) bool +} + +var re = regexp.MustCompile + +func hasAnyPrefix(s string, prefixes ...string) bool { + for _, p := range prefixes { + if strings.HasPrefix(s, p) { + return true + } + } + return false +} + +var arches = map[string]arch{ + "amd64": { + isFPWrite: re(`,\s*BP$`).MatchString, // TODO: can have false positive, e.g. for TESTQ BP,BP. Seems unlikely. + isFPRead: re(`\bBP\b`).MatchString, + isBranch: func(s string) bool { + return hasAnyPrefix(s, "J", "RET") + }, + }, + "arm64": { + isFPWrite: func(s string) bool { + if i := strings.LastIndex(s, ","); i > 0 && strings.HasSuffix(s[i:], "R29") { + return true + } + if hasAnyPrefix(s, "LDP", "LDAXP", "LDXP", "CASP") { + // Instructions which write to a pair of registers, e.g. + // LDP 8(R0), (R26, R29) + // CASPD (R2, R3), (R2), (R26, R29) + lp := strings.LastIndex(s, "(") + rp := strings.LastIndex(s, ")") + if lp > -1 && lp < rp { + return strings.Contains(s[lp:rp], ",") && strings.Contains(s[lp:rp], "R29") + } + } + return false + }, + isFPRead: re(`\bR29\b`).MatchString, + isBranch: func(s string) bool { + // Get just the instruction + if i := strings.IndexFunc(s, unicode.IsSpace); i > 0 { + s = s[:i] + } + return arm64Branch[s] + }, + }, +} + +// arm64 has many control flow instructions. +// ^(B|RET) isn't sufficient or correct (e.g. BIC, BFI aren't control flow.) +// It's easier to explicitly enumerate them in a map than to write a regex. +// Borrowed from Go tree, cmd/asm/internal/arch/arm64.go +var arm64Branch = map[string]bool{ + "B": true, + "BL": true, + "BEQ": true, + "BNE": true, + "BCS": true, + "BHS": true, + "BCC": true, + "BLO": true, + "BMI": true, + "BPL": true, + "BVS": true, + "BVC": true, + "BHI": true, + "BLS": true, + "BGE": true, + "BLT": true, + "BGT": true, + "BLE": true, + "CBZ": true, + "CBZW": true, + "CBNZ": true, + "CBNZW": true, + "JMP": true, + "TBNZ": true, + "TBZ": true, + "RET": true, +} func run(pass *analysis.Pass) (interface{}, error) { - if build.Default.GOARCH != "amd64" { // TODO: arm64 also? + arch, ok := arches[build.Default.GOARCH] + if !ok { return nil, nil } if build.Default.GOOS != "linux" && build.Default.GOOS != "darwin" { @@ -63,6 +146,9 @@ func run(pass *analysis.Pass) (interface{}, error) { line = line[:i] } line = strings.TrimSpace(line) + if line == "" { + continue + } // We start checking code at a TEXT line for a frameless function. if strings.HasPrefix(line, "TEXT") && strings.Contains(line, "(SB)") && strings.Contains(line, "$0") { @@ -73,16 +159,12 @@ func run(pass *analysis.Pass) (interface{}, error) { continue } - if asmWriteBP.MatchString(line) { // clobber of BP, function is not OK + if arch.isFPWrite(line) { pass.Reportf(analysisutil.LineStart(tf, lineno), "frame pointer is clobbered before saving") active = false continue } - if asmMentionBP.MatchString(line) { // any other use of BP might be a read, so function is OK - active = false - continue - } - if asmControlFlow.MatchString(line) { // give up after any branch instruction + if arch.isFPRead(line) || arch.isBranch(line) { active = false continue } diff --git a/go/analysis/passes/framepointer/testdata/src/a/asm_arm64.s b/go/analysis/passes/framepointer/testdata/src/a/asm_arm64.s new file mode 100644 index 00000000000..f2be7bdb9e9 --- /dev/null +++ b/go/analysis/passes/framepointer/testdata/src/a/asm_arm64.s @@ -0,0 +1,42 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +TEXT ·bad1(SB), 0, $0 + MOVD $0, R29 // want `frame pointer is clobbered before saving` + RET +TEXT ·bad2(SB), 0, $0 + MOVD R1, R29 // want `frame pointer is clobbered before saving` + RET +TEXT ·bad3(SB), 0, $0 + MOVD 6(R2), R29 // want `frame pointer is clobbered before saving` + RET +TEXT ·bad4(SB), 0, $0 + LDP 0(R1), (R26, R29) // want `frame pointer is clobbered before saving` + RET +TEXT ·bad5(SB), 0, $0 + AND $0x1, R3, R29 // want `frame pointer is clobbered before saving` + RET +TEXT ·good1(SB), 0, $0 + STPW (R29, R30), -32(RSP) + MOVD $0, R29 // this is ok + LDPW 32(RSP), (R29, R30) + RET +TEXT ·good2(SB), 0, $0 + MOVD R29, R1 + MOVD $0, R29 // this is ok + MOVD R1, R29 + RET +TEXT ·good3(SB), 0, $0 + CMP R1, R2 + BEQ skip + MOVD $0, R29 // this is ok +skip: + RET +TEXT ·good4(SB), 0, $0 + RET + MOVD $0, R29 // this is ok + RET +TEXT ·good5(SB), 0, $8 + MOVD $0, R29 // this is ok + RET From 5f9967d63b2b964daae36c6f0fa3e1eecdd8eb06 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 6 Feb 2025 15:08:45 -0500 Subject: [PATCH 122/309] gopls/internal/analysis/modernize: strings.Split -> SplitSeq This CL defines a modernizer for calls to strings.Split and bytes.Split, that offers a fix to instead use go1.24's SplitSeq, which avoids allocating an array. The fix is offered only if Split is used as the operand of a range statement, either directly, or indirectly via a variable whose sole use is the range statement. + tests Change-Id: I7c6c128d21ccf7f8b3c7745538177d2d162f62de Reviewed-on: https://go-review.googlesource.com/c/tools/+/647438 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- gopls/doc/analyzers.md | 2 + gopls/internal/analysis/modernize/doc.go | 2 + .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + gopls/internal/analysis/modernize/splitseq.go | 112 ++++++++++++++++++ .../testdata/src/splitseq/splitseq.go | 42 +++++++ .../testdata/src/splitseq/splitseq.go.golden | 42 +++++++ .../testdata/src/splitseq/splitseq_go123.go | 1 + gopls/internal/doc/api.json | 4 +- 9 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 gopls/internal/analysis/modernize/splitseq.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq_go123.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 06ac853800f..68465f9809d 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -497,6 +497,8 @@ existing code by using more modern features of Go, such as: added in go1.21 - replacing a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22; + - replacing Split in "for range strings.Split(...)" by go1.24's + more efficient SplitSeq; Default: on. diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 6a247feccf4..15aeab64d8d 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -30,4 +30,6 @@ // added in go1.21 // - replacing a 3-clause for i := 0; i < n; i++ {} loop by // for i := range n {}, added in go1.22; +// - replacing Split in "for range strings.Split(...)" by go1.24's +// more efficient SplitSeq; package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 96ab3131833..861194e6242 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -70,6 +70,7 @@ func run(pass *analysis.Pass) (any, error) { rangeint(pass) slicescontains(pass) slicesdelete(pass) + splitseq(pass) sortslice(pass) testingContext(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 7e375c1c24c..6662914b28d 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -23,6 +23,7 @@ func Test(t *testing.T) { "rangeint", "slicescontains", "slicesdelete", + "splitseq", "sortslice", "testingcontext", ) diff --git a/gopls/internal/analysis/modernize/splitseq.go b/gopls/internal/analysis/modernize/splitseq.go new file mode 100644 index 00000000000..1f3da859e9b --- /dev/null +++ b/gopls/internal/analysis/modernize/splitseq.go @@ -0,0 +1,112 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "go/ast" + "go/token" + "go/types" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/edge" +) + +// splitseq offers a fix to replace a call to strings.Split with +// SplitSeq when it is the operand of a range loop, either directly: +// +// for _, line := range strings.Split() {...} +// +// or indirectly, if the variable's sole use is the range statement: +// +// lines := strings.Split() +// for _, line := range lines {...} +// +// Variants: +// - bytes.SplitSeq +func splitseq(pass *analysis.Pass) { + if !analysisinternal.Imports(pass.Pkg, "strings") && + !analysisinternal.Imports(pass.Pkg, "bytes") { + return + } + info := pass.TypesInfo + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.24") { + for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) { + rng := curRange.Node().(*ast.RangeStmt) + + // Reject "for i, line := ..." since SplitSeq is not an iter.Seq2. + // (We require that i is blank.) + if id, ok := rng.Key.(*ast.Ident); ok && id.Name != "_" { + continue + } + + // Find the call operand of the range statement, + // whether direct or indirect. + call, ok := rng.X.(*ast.CallExpr) + if !ok { + if id, ok := rng.X.(*ast.Ident); ok { + if v, ok := info.Uses[id].(*types.Var); ok { + if ek, idx := curRange.Edge(); ek == edge.BlockStmt_List && idx > 0 { + curPrev, _ := curRange.PrevSibling() + if assign, ok := curPrev.Node().(*ast.AssignStmt); ok && + assign.Tok == token.DEFINE && + len(assign.Lhs) == 1 && + len(assign.Rhs) == 1 && + info.Defs[assign.Lhs[0].(*ast.Ident)] == v && + soleUse(info, v) == id { + // Have: + // lines := ... + // for _, line := range lines {...} + // and no other uses of lines. + call, _ = assign.Rhs[0].(*ast.CallExpr) + } + } + } + } + } + + if call != nil { + var edits []analysis.TextEdit + if rng.Key != nil { + // Delete (blank) RangeStmt.Key: + // for _, line := -> for line := + // for _, _ := -> for + // for _ := -> for + end := rng.Range + if rng.Value != nil { + end = rng.Value.Pos() + } + edits = append(edits, analysis.TextEdit{ + Pos: rng.Key.Pos(), + End: end, + }) + } + + if sel, ok := call.Fun.(*ast.SelectorExpr); ok && + (analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "strings", "Split") || + analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "bytes", "Split")) { + pass.Report(analysis.Diagnostic{ + Pos: sel.Pos(), + End: sel.End(), + Category: "splitseq", + Message: "Ranging over SplitSeq is more efficient", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Replace Split with SplitSeq", + TextEdits: append(edits, analysis.TextEdit{ + // Split -> SplitSeq + Pos: sel.Sel.Pos(), + End: sel.Sel.End(), + NewText: []byte("SplitSeq")}), + }}, + }) + } + } + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go new file mode 100644 index 00000000000..4f533ed22bc --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go @@ -0,0 +1,42 @@ +//go:build go1.24 + +package splitseq + +import ( + "bytes" + "strings" +) + +func _() { + for _, line := range strings.Split("", "") { // want "Ranging over SplitSeq is more efficient" + println(line) + } + for i, line := range strings.Split("", "") { // nope: uses index var + println(i, line) + } + for i, _ := range strings.Split("", "") { // nope: uses index var + println(i) + } + for i := range strings.Split("", "") { // nope: uses index var + println(i) + } + for _ = range strings.Split("", "") { // want "Ranging over SplitSeq is more efficient" + } + for range strings.Split("", "") { // want "Ranging over SplitSeq is more efficient" + } + for range bytes.Split(nil, nil) { // want "Ranging over SplitSeq is more efficient" + } + { + lines := strings.Split("", "") // want "Ranging over SplitSeq is more efficient" + for _, line := range lines { + println(line) + } + } + { + lines := strings.Split("", "") // nope: lines is used not just by range + for _, line := range lines { + println(line) + } + println(lines) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go.golden b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go.golden new file mode 100644 index 00000000000..d10e0e8e564 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq.go.golden @@ -0,0 +1,42 @@ +//go:build go1.24 + +package splitseq + +import ( + "bytes" + "strings" +) + +func _() { + for line := range strings.SplitSeq("", "") { // want "Ranging over SplitSeq is more efficient" + println(line) + } + for i, line := range strings.Split("", "") { // nope: uses index var + println(i, line) + } + for i, _ := range strings.Split("", "") { // nope: uses index var + println(i) + } + for i := range strings.Split("", "") { // nope: uses index var + println(i) + } + for range strings.SplitSeq("", "") { // want "Ranging over SplitSeq is more efficient" + } + for range strings.SplitSeq("", "") { // want "Ranging over SplitSeq is more efficient" + } + for range bytes.SplitSeq(nil, nil) { // want "Ranging over SplitSeq is more efficient" + } + { + lines := strings.SplitSeq("", "") // want "Ranging over SplitSeq is more efficient" + for line := range lines { + println(line) + } + } + { + lines := strings.Split("", "") // nope: lines is used not just by range + for _, line := range lines { + println(line) + } + println(lines) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq_go123.go b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq_go123.go new file mode 100644 index 00000000000..c3e86bb2ed9 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/splitseq/splitseq_go123.go @@ -0,0 +1 @@ +package splitseq diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b83dfe4bde0..8f101079a9c 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -510,7 +510,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;", "Default": "true" }, { @@ -1189,7 +1189,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From 94c3c49c41819ed247e0423acff990ab4ed12cf9 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Feb 2025 12:55:32 -0500 Subject: [PATCH 123/309] go/analysis/analysistest: RunWithSuggestedFix: assume valid fixes Recent work has caused internal/checker to validate fixes at the moment they are reported, panicking if invalid, so we can simplify the logic here. Later we'll support three-way merging of fixes. Change-Id: I10cc582afbeb62308252979e6db37b7ed10ddddc Reviewed-on: https://go-review.googlesource.com/c/tools/+/647699 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- go/analysis/analysistest/analysistest.go | 56 +++++------------------- 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 8c62c56fa84..775fd20094d 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -174,62 +174,27 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns act := result.Action // file -> message -> edits - // TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers. + // TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers, + // whereas they are only unique within a given Diagnostic. fileEdits := make(map[*token.File]map[string][]diff.Edit) - fileContents := make(map[*token.File][]byte) - // Validate edits, prepare the fileEdits map and read the file contents. + // We may assume that fixes are validated upon creation in Pass.Report. + // Group fixes by file and message. for _, diag := range act.Diagnostics { for _, fix := range diag.SuggestedFixes { - // Assert that lazy fixes have a Category (#65578, #65087). if inTools && len(fix.TextEdits) == 0 && diag.Category == "" { t.Errorf("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command") } - // TODO(adonovan): factor in common with go/analysis/internal/checker.validateEdits. - for _, edit := range fix.TextEdits { - start, end := edit.Pos, edit.End - if !end.IsValid() { - end = start - } - // Validate the edit. - if start > end { - t.Errorf( - "diagnostic for analysis %v contains Suggested Fix with malformed edit: pos (%v) > end (%v)", - act.Analyzer.Name, start, end) - continue - } - file := act.Package.Fset.File(start) - if file == nil { - t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed start position %v", act.Analyzer.Name, start) - continue - } - endFile := act.Package.Fset.File(end) - if endFile == nil { - t.Errorf("diagnostic for analysis %v contains Suggested Fix with malformed end position %v", act.Analyzer.Name, end) - continue - } - if file != endFile { - t.Errorf( - "diagnostic for analysis %v contains Suggested Fix with malformed spanning files %v and %v", - act.Analyzer.Name, file.Name(), endFile.Name()) - continue - } - if _, ok := fileContents[file]; !ok { - contents, err := os.ReadFile(file.Name()) - if err != nil { - t.Errorf("error reading %s: %v", file.Name(), err) - } - fileContents[file] = contents - } + file := act.Package.Fset.File(edit.Pos) if _, ok := fileEdits[file]; !ok { fileEdits[file] = make(map[string][]diff.Edit) } fileEdits[file][fix.Message] = append(fileEdits[file][fix.Message], diff.Edit{ - Start: file.Offset(start), - End: file.Offset(end), + Start: file.Offset(edit.Pos), + End: file.Offset(edit.End), New: string(edit.NewText), }) } @@ -238,9 +203,10 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns for file, fixes := range fileEdits { // Get the original file contents. - orig, ok := fileContents[file] - if !ok { - t.Errorf("could not find file contents for %s", file.Name()) + // TODO(adonovan): plumb pass.ReadFile. + orig, err := os.ReadFile(file.Name()) + if err != nil { + t.Errorf("error reading %s: %v", file.Name(), err) continue } From a886a1c2ed0dee2af11f465e11bf48b11dda984c Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 6 Feb 2025 18:26:36 -0500 Subject: [PATCH 124/309] internal/analysisinternal: AddImport handles dot imports If AddImport finds that an existing dot import suffices to refer to an name, it returns that information by means of a first return value of ".", and does not add a new import. For this to work, AddImport must know the name for which an import is needed, so it can determine whether it is shadowed. Change-Id: Ie4c9edf78fb89fc1b64f344517627173a253b999 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647437 LUCI-TryBot-Result: Go LUCI Auto-Submit: Jonathan Amsterdam Reviewed-by: Alan Donovan --- go/analysis/passes/stringintconv/string.go | 6 +-- .../stringintconv/testdata/src/fix/fixdot.go | 7 +++ .../testdata/src/fix/fixdot.go.golden | 18 +++++++ gopls/internal/analysis/gofix/gofix.go | 14 +++--- .../analysis/gofix/testdata/src/b/b.go.golden | 4 +- gopls/internal/analysis/modernize/maps.go | 23 +++++---- gopls/internal/analysis/modernize/slices.go | 8 ++-- .../analysis/modernize/slicescontains.go | 6 +-- .../analysis/modernize/slicesdelete.go | 4 +- .../internal/analysis/modernize/sortslice.go | 5 +- .../testdata/src/mapsloop/mapsloop_dot.go | 23 +++++++++ .../src/mapsloop/mapsloop_dot.go.golden | 19 ++++++++ .../testdata/src/sortslice/sortslice_dot.go | 26 ++++++++++ .../src/sortslice/sortslice_dot.go.golden | 26 ++++++++++ internal/analysisinternal/addimport_test.go | 48 ++++++++++++++++++- internal/analysisinternal/analysis.go | 23 +++++++-- 16 files changed, 220 insertions(+), 40 deletions(-) create mode 100644 go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go create mode 100644 go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go.golden diff --git a/go/analysis/passes/stringintconv/string.go b/go/analysis/passes/stringintconv/string.go index 108600a2baf..f56e6ecaa29 100644 --- a/go/analysis/passes/stringintconv/string.go +++ b/go/analysis/passes/stringintconv/string.go @@ -198,14 +198,14 @@ func run(pass *analysis.Pass) (interface{}, error) { // the type has methods, as some {String,GoString,Format} // may change the behavior of fmt.Sprint. if len(ttypes) == 1 && len(vtypes) == 1 && types.NewMethodSet(V0).Len() == 0 { - fmtName, importEdits := analysisinternal.AddImport(pass.TypesInfo, file, arg.Pos(), "fmt", "fmt") + _, prefix, importEdits := analysisinternal.AddImport(pass.TypesInfo, file, "fmt", "fmt", "Sprint", arg.Pos()) if types.Identical(T0, types.Typ[types.String]) { // string(x) -> fmt.Sprint(x) addFix("Format the number as a decimal", append(importEdits, analysis.TextEdit{ Pos: call.Fun.Pos(), End: call.Fun.End(), - NewText: []byte(fmtName + ".Sprint"), + NewText: []byte(prefix + "Sprint"), }), ) } else { @@ -214,7 +214,7 @@ func run(pass *analysis.Pass) (interface{}, error) { analysis.TextEdit{ Pos: call.Lparen + 1, End: call.Lparen + 1, - NewText: []byte(fmtName + ".Sprint("), + NewText: []byte(prefix + "Sprint("), }, analysis.TextEdit{ Pos: call.Rparen, diff --git a/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go b/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go new file mode 100644 index 00000000000..d89ca94af82 --- /dev/null +++ b/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go @@ -0,0 +1,7 @@ +package fix + +import . "fmt" + +func _(x uint64) { + Println(string(x)) // want `conversion from uint64 to string yields...` +} diff --git a/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go.golden b/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go.golden new file mode 100644 index 00000000000..18aec2d027a --- /dev/null +++ b/go/analysis/passes/stringintconv/testdata/src/fix/fixdot.go.golden @@ -0,0 +1,18 @@ +-- Format the number as a decimal -- +package fix + +import . "fmt" + +func _(x uint64) { + Println(Sprint(x)) // want `conversion from uint64 to string yields...` +} + +-- Convert a single rune to a string -- +package fix + +import . "fmt" + +func _(x uint64) { + Println(string(rune(x))) // want `conversion from uint64 to string yields...` +} + diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index b7d80f9f4a5..101924366d6 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -262,15 +262,13 @@ func run(pass *analysis.Pass) (any, error) { continue } } - importPrefix := "" - var edits []analysis.TextEdit + var ( + importPrefix string + edits []analysis.TextEdit + ) if fcon.RHSPkgPath != pass.Pkg.Path() { - // TODO(jba): fix AddImport so that it returns "." if an existing dot import will work. - // We will need to tell AddImport the name of the identifier we want to qualify (fcon.RHSName here). - importID, eds := analysisinternal.AddImport( - pass.TypesInfo, curFile, n.Pos(), fcon.RHSPkgPath, fcon.RHSPkgName) - importPrefix = importID + "." - edits = eds + _, importPrefix, edits = analysisinternal.AddImport( + pass.TypesInfo, curFile, fcon.RHSPkgName, fcon.RHSPkgPath, fcon.RHSName, n.Pos()) } var ( pos = n.Pos() diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index dae869e52f3..b26a05c3046 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -2,8 +2,6 @@ package b import a0 "a" -import "c" - import ( "a" . "c" @@ -29,7 +27,7 @@ func g() { // a second import of "a" will be added with a new package identifer. x = a0.Uno // want `Constant in2 should be inlined` - x = c.C // want `Constant in3 should be inlined` + x = C // want `Constant in3 should be inlined` _ = a _ = x diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 7950b546683..c93899621ef 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -126,28 +126,33 @@ func mapsloop(pass *analysis.Pass) { } } - // Choose function, report diagnostic, and suggest fix. + // Choose function. + var funcName string + if mrhs != nil { + funcName = cond(xmap, "Clone", "Collect") + } else { + funcName = cond(xmap, "Copy", "Insert") + } + + // Report diagnostic, and suggest fix. rng := curRange.Node() - mapsName, importEdits := analysisinternal.AddImport(info, file, rng.Pos(), "maps", "maps") + _, prefix, importEdits := analysisinternal.AddImport(info, file, "maps", "maps", funcName, rng.Pos()) var ( - funcName string newText []byte start, end token.Pos ) if mrhs != nil { // Replace RHS of preceding m=... assignment (and loop) with expression. start, end = mrhs.Pos(), rng.End() - funcName = cond(xmap, "Clone", "Collect") - newText = fmt.Appendf(nil, "%s.%s(%s)", - mapsName, + newText = fmt.Appendf(nil, "%s%s(%s)", + prefix, funcName, analysisinternal.Format(pass.Fset, x)) } else { // Replace loop with call statement. start, end = rng.Pos(), rng.End() - funcName = cond(xmap, "Copy", "Insert") - newText = fmt.Appendf(nil, "%s.%s(%s, %s)", - mapsName, + newText = fmt.Appendf(nil, "%s%s(%s, %s)", + prefix, funcName, analysisinternal.Format(pass.Fset, m), analysisinternal.Format(pass.Fset, x)) diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index cb73f7e30cd..aada97df802 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -92,7 +92,7 @@ func appendclipped(pass *analysis.Pass) { } // append(zerocap, s...) -> slices.Clone(s) - slicesName, importEdits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices") + _, prefix, importEdits := analysisinternal.AddImport(info, file, "slices", "slices", "Clone", call.Pos()) pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), @@ -103,7 +103,7 @@ func appendclipped(pass *analysis.Pass) { TextEdits: append(importEdits, []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), - NewText: fmt.Appendf(nil, "%s.Clone(%s)", slicesName, analysisinternal.Format(pass.Fset, s)), + NewText: fmt.Appendf(nil, "%sClone(%s)", prefix, analysisinternal.Format(pass.Fset, s)), }}...), }}, }) @@ -116,7 +116,7 @@ func appendclipped(pass *analysis.Pass) { // - slices.Clone(s) -> s // - s[:len(s):len(s)] -> s // - slices.Clip(s) -> s - slicesName, importEdits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices") + _, prefix, importEdits := analysisinternal.AddImport(info, file, "slices", "slices", "Concat", call.Pos()) pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), @@ -127,7 +127,7 @@ func appendclipped(pass *analysis.Pass) { TextEdits: append(importEdits, []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), - NewText: fmt.Appendf(nil, "%s.Concat(%s)", slicesName, formatExprs(pass.Fset, sliceArgs)), + NewText: fmt.Appendf(nil, "%sConcat(%s)", prefix, formatExprs(pass.Fset, sliceArgs)), }}...), }}, }) diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index d860d642743..09642448bb5 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -158,9 +158,9 @@ func slicescontains(pass *analysis.Pass) { } // Prepare slices.Contains{,Func} call. - slicesName, importEdits := analysisinternal.AddImport(info, file, rng.Pos(), "slices", "slices") - contains := fmt.Sprintf("%s.%s(%s, %s)", - slicesName, + _, prefix, importEdits := analysisinternal.AddImport(info, file, "slices", "slices", funcName, rng.Pos()) + contains := fmt.Sprintf("%s%s(%s, %s)", + prefix, funcName, analysisinternal.Format(pass.Fset, rng.X), analysisinternal.Format(pass.Fset, arg2)) diff --git a/gopls/internal/analysis/modernize/slicesdelete.go b/gopls/internal/analysis/modernize/slicesdelete.go index c9e2da0eb60..24b2182ca6a 100644 --- a/gopls/internal/analysis/modernize/slicesdelete.go +++ b/gopls/internal/analysis/modernize/slicesdelete.go @@ -24,7 +24,7 @@ func slicesdelete(pass *analysis.Pass) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) info := pass.TypesInfo report := func(file *ast.File, call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) { - slicesName, edits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices") + _, prefix, edits := analysisinternal.AddImport(info, file, "slices", "slices", "Delete", call.Pos()) pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), @@ -37,7 +37,7 @@ func slicesdelete(pass *analysis.Pass) { { Pos: call.Fun.Pos(), End: call.Fun.End(), - NewText: []byte(slicesName + ".Delete"), + NewText: []byte(prefix + "Delete"), }, // Delete ellipsis. { diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 4f856d39c33..7f695d76495 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -70,7 +70,8 @@ func sortslice(pass *analysis.Pass) { if isIndex(compare.X, i) && isIndex(compare.Y, j) { // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) - slicesName, importEdits := analysisinternal.AddImport(info, file, call.Pos(), "slices", "slices") + _, prefix, importEdits := analysisinternal.AddImport( + info, file, "slices", "slices", "Sort", call.Pos()) pass.Report(analysis.Diagnostic{ // Highlight "sort.Slice". @@ -85,7 +86,7 @@ func sortslice(pass *analysis.Pass) { // Replace sort.Slice with slices.Sort. Pos: call.Fun.Pos(), End: call.Fun.End(), - NewText: []byte(slicesName + ".Sort"), + NewText: []byte(prefix + "Sort"), }, { // Eliminate FuncLit. diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go new file mode 100644 index 00000000000..c33d43e23ad --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go @@ -0,0 +1,23 @@ +//go:build go1.23 + +package mapsloop + +import . "maps" + +var _ = Clone[M] // force "maps" import so that each diagnostic doesn't add one + +func useCopyDot(dst, src map[int]string) { + // Replace loop by maps.Copy. + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" + } +} + +func useCloneDot(src map[int]string) { + // Replace make(...) by maps.Clone. + dst := make(map[int]string, len(src)) + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + } + println(dst) +} diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden new file mode 100644 index 00000000000..d6a30537645 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden @@ -0,0 +1,19 @@ +//go:build go1.23 + +package mapsloop + +import . "maps" + +var _ = Clone[M] // force "maps" import so that each diagnostic doesn't add one + +func useCopyDot(dst, src map[int]string) { + // Replace loop by maps.Copy. + Copy(dst, src) +} + +func useCloneDot(src map[int]string) { + // Replace make(...) by maps.Clone. + dst := Clone(src) + println(dst) +} + diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go new file mode 100644 index 00000000000..8502718c1a5 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go @@ -0,0 +1,26 @@ +package sortslice + +import . "slices" +import "sort" + +func _(s []myint) { + sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort" +} + +func _(x *struct{ s []int }) { + sort.Slice(x.s, func(first, second int) bool { return x.s[first] < x.s[second] }) // want "sort.Slice can be modernized using slices.Sort" +} + +func _(s []int) { + sort.Slice(s, func(i, j int) bool { return s[i] > s[j] }) // nope: wrong comparison operator +} + +func _(s []int) { + sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var +} + +func _(s2 []struct{ x int }) { + sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation +} + +func _() { Clip([]int{}) } diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go.golden new file mode 100644 index 00000000000..45c056d24fb --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice_dot.go.golden @@ -0,0 +1,26 @@ +package sortslice + +import . "slices" +import "sort" + +func _(s []myint) { + Sort(s) // want "sort.Slice can be modernized using slices.Sort" +} + +func _(x *struct{ s []int }) { + Sort(x.s) // want "sort.Slice can be modernized using slices.Sort" +} + +func _(s []int) { + sort.Slice(s, func(i, j int) bool { return s[i] > s[j] }) // nope: wrong comparison operator +} + +func _(s []int) { + sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var +} + +func _(s2 []struct{ x int }) { + sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation +} + +func _() { Clip([]int{}) } diff --git a/internal/analysisinternal/addimport_test.go b/internal/analysisinternal/addimport_test.go index f361bde82f8..145d5861b8f 100644 --- a/internal/analysisinternal/addimport_test.go +++ b/internal/analysisinternal/addimport_test.go @@ -183,6 +183,42 @@ import foo "encoding/json" func _() { foo +}`, + }, + { + descr: descr("dot import unshadowed"), + src: `package a + +import . "fmt" + +func _() { + «. fmt» +}`, + want: `package a + +import . "fmt" + +func _() { + . +}`, + }, + { + descr: descr("dot import shadowed"), + src: `package a + +import . "fmt" + +func _(Print fmt.Stringer) { + «fmt fmt» +}`, + want: `package a + +import "fmt" + +import . "fmt" + +func _(Print fmt.Stringer) { + fmt }`, }, } { @@ -218,7 +254,8 @@ func _() { conf.Check(f.Name.Name, fset, []*ast.File{f}, info) // add import - name, edits := analysisinternal.AddImport(info, f, pos, path, name) + // The "Print" argument is only relevant for dot-import tests. + name, prefix, edits := analysisinternal.AddImport(info, f, name, path, "Print", pos) var edit analysis.TextEdit switch len(edits) { @@ -229,6 +266,15 @@ func _() { t.Fatalf("expected at most one edit, got %d", len(edits)) } + // prefix is a simple function of name. + wantPrefix := name + "." + if name == "." { + wantPrefix = "" + } + if prefix != wantPrefix { + t.Errorf("got prefix %q, want %q", prefix, wantPrefix) + } + // apply patch start := fset.Position(edit.Pos) end := fset.Position(edit.End) diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 7608692b750..abf708111bf 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -211,10 +211,17 @@ func CheckReadable(pass *analysis.Pass, filename string) error { // that import is in scope at pos. If so, it returns the name under // which it was imported and a zero edit. Otherwise, it adds a new // import of pkgpath, using a name derived from the preferred name, -// and returns the chosen name along with the edit for the new import. +// and returns the chosen name, a prefix to be concatenated with member +// to form a qualified name, and the edit for the new import. +// +// In the special case that pkgpath is dot-imported then member, the +// identifer for which the import is being added, is consulted. If +// member is not shadowed at pos, AddImport returns (".", "", nil). +// (AddImport accepts the caller's implicit claim that the imported +// package declares member.) // // It does not mutate its arguments. -func AddImport(info *types.Info, file *ast.File, pos token.Pos, pkgpath, preferredName string) (name string, newImport []analysis.TextEdit) { +func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member string, pos token.Pos) (name, prefix string, newImport []analysis.TextEdit) { // Find innermost enclosing lexical block. scope := info.Scopes[file].Innermost(pos) if scope == nil { @@ -226,8 +233,14 @@ func AddImport(info *types.Info, file *ast.File, pos token.Pos, pkgpath, preferr for _, spec := range file.Imports { pkgname := info.PkgNameOf(spec) if pkgname != nil && pkgname.Imported().Path() == pkgpath { - if _, obj := scope.LookupParent(pkgname.Name(), pos); obj == pkgname { - return pkgname.Name(), nil + name = pkgname.Name() + if name == "." { + // The scope of ident must be the file scope. + if s, _ := scope.LookupParent(member, pos); s == info.Scopes[file] { + return name, "", nil + } + } else if _, obj := scope.LookupParent(name, pos); obj == pkgname { + return name, name + ".", nil } } } @@ -265,7 +278,7 @@ func AddImport(info *types.Info, file *ast.File, pos token.Pos, pkgpath, preferr before = decl0.Doc } } - return newName, []analysis.TextEdit{{ + return newName, newName + ".", []analysis.TextEdit{{ Pos: before.Pos(), End: before.Pos(), NewText: []byte(newText), From dc9353b60ee791ae31f3be19b8ae7ea5450e5e62 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Feb 2025 16:24:30 -0500 Subject: [PATCH 125/309] gopls/internal/analysis/modernize: appendclipped: unclip The appendclipped pass must ascertain that the first argument to append(x, y...) is clipped, so that we don't eliminate possible intended side effects on x, but in some cases: - append(x[:len(x):len(x)], y...) - append(slices.Clip(x), y...) we can further simplify the first argument to its unclipped version (just x in both cases), so that the result is: slices.Concat(x, y) + test Fixes golang/go#71296 Change-Id: I89cc4350b5dbd57c88c35c0b4459b23347814441 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647796 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- .../internal/analysis/modernize/modernize.go | 1 + gopls/internal/analysis/modernize/slices.go | 53 +++++++++++-------- .../src/appendclipped/appendclipped.go.golden | 4 +- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 861194e6242..0f7b58eed37 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -130,6 +130,7 @@ var ( builtinAppend = types.Universe.Lookup("append") builtinBool = types.Universe.Lookup("bool") builtinFalse = types.Universe.Lookup("false") + builtinLen = types.Universe.Lookup("len") builtinMake = types.Universe.Lookup("make") builtinNil = types.Universe.Lookup("nil") builtinTrue = types.Universe.Lookup("true") diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index aada97df802..bdab9dea649 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -52,18 +52,21 @@ func appendclipped(pass *analysis.Pass) { // Only appends whose base is a clipped slice can be simplified: // We must conservatively assume an append to an unclipped slice // such as append(y[:0], x...) is intended to have effects on y. - clipped, empty := isClippedSlice(info, base) - if !clipped { + clipped, empty := clippedSlice(info, base) + if clipped == nil { return } // If the (clipped) base is empty, it may be safely ignored. - // Otherwise treat it as just another arg (the first) to Concat. + // Otherwise treat it (or its unclipped subexpression, if possible) + // as just another arg (the first) to Concat. if !empty { - sliceArgs = append(sliceArgs, base) + sliceArgs = append(sliceArgs, clipped) } slices.Reverse(sliceArgs) + // TODO(adonovan): simplify sliceArgs[0] further: slices.Clone(s) -> s + // Concat of a single (non-trivial) slice degenerates to Clone. if len(sliceArgs) == 1 { s := sliceArgs[0] @@ -111,11 +114,6 @@ func appendclipped(pass *analysis.Pass) { } // append(append(append(base, a...), b..., c...) -> slices.Concat(base, a, b, c) - // - // TODO(adonovan): simplify sliceArgs[0] further: - // - slices.Clone(s) -> s - // - s[:len(s):len(s)] -> s - // - slices.Clip(s) -> s _, prefix, importEdits := analysisinternal.AddImport(info, file, "slices", "slices", "Concat", call.Pos()) pass.Report(analysis.Diagnostic{ Pos: call.Pos(), @@ -172,25 +170,36 @@ func appendclipped(pass *analysis.Pass) { } } -// isClippedSlice reports whether e denotes a slice that is definitely -// clipped, that is, its len(s)==cap(s). +// clippedSlice returns res != nil if e denotes a slice that is +// definitely clipped, that is, its len(s)==cap(s). +// +// The value of res is either the same as e or is a subexpression of e +// that denotes the same slice but without the clipping operation. // -// In addition, it reports whether the slice is definitely empty. +// In addition, it reports whether the slice is definitely empty, // // Examples of clipped slices: // // x[:0:0] (empty) // []T(nil) (empty) // Slice{} (empty) -// x[:len(x):len(x)] (nonempty) +// x[:len(x):len(x)] (nonempty) res=x // x[:k:k] (nonempty) -// slices.Clip(x) (nonempty) -func isClippedSlice(info *types.Info, e ast.Expr) (clipped, empty bool) { +// slices.Clip(x) (nonempty) res=x +func clippedSlice(info *types.Info, e ast.Expr) (res ast.Expr, empty bool) { switch e := e.(type) { case *ast.SliceExpr: - // x[:0:0], x[:len(x):len(x)], x[:k:k], x[:0] - clipped = e.Slice3 && e.High != nil && e.Max != nil && equalSyntax(e.High, e.Max) // x[:k:k] - empty = e.High != nil && isZeroLiteral(e.High) // x[:0:*] + // x[:0:0], x[:len(x):len(x)], x[:k:k] + if e.Slice3 && e.High != nil && e.Max != nil && equalSyntax(e.High, e.Max) { // x[:k:k] + res = e + empty = isZeroLiteral(e.High) // x[:0:0] + if call, ok := e.High.(*ast.CallExpr); ok && + typeutil.Callee(info, call) == builtinLen && + equalSyntax(call.Args[0], e.X) { + res = e.X // x[:len(x):len(x)] -> x + } + return + } return case *ast.CallExpr: @@ -198,20 +207,20 @@ func isClippedSlice(info *types.Info, e ast.Expr) (clipped, empty bool) { if info.Types[e.Fun].IsType() && is[*ast.Ident](e.Args[0]) && info.Uses[e.Args[0].(*ast.Ident)] == builtinNil { - return true, true + return e, true } // slices.Clip(x)? obj := typeutil.Callee(info, e) if analysisinternal.IsFunctionNamed(obj, "slices", "Clip") { - return true, false + return e.Args[0], false // slices.Clip(x) -> x } case *ast.CompositeLit: // Slice{}? if len(e.Elts) == 0 { - return true, true + return e, true } } - return false, false + return nil, false } diff --git a/gopls/internal/analysis/modernize/testdata/src/appendclipped/appendclipped.go.golden b/gopls/internal/analysis/modernize/testdata/src/appendclipped/appendclipped.go.golden index 5d6761b5371..6352d525b34 100644 --- a/gopls/internal/analysis/modernize/testdata/src/appendclipped/appendclipped.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/appendclipped/appendclipped.go.golden @@ -20,7 +20,7 @@ func _(s, other []string) { print(slices.Concat(Bytes{1, 2, 3}, Bytes{4, 5, 6})) // want "Replace append with slices.Concat" print(slices.Concat(s, other, other)) // want "Replace append with slices.Concat" print(slices.Concat(os.Environ(), other, other)) // want "Replace append with slices.Concat" - print(slices.Concat(other[:len(other):len(other)], s, other)) // want "Replace append with slices.Concat" - print(slices.Concat(slices.Clip(other), s, other)) // want "Replace append with slices.Concat" + print(slices.Concat(other, s, other)) // want "Replace append with slices.Concat" + print(slices.Concat(other, s, other)) // want "Replace append with slices.Concat" print(append(append(append(other[:0], s...), other...), other...)) // nope: intent may be to mutate other } From 09747cdf594a7924dcecb506312be3bd6e437962 Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Mon, 10 Feb 2025 08:34:47 -0800 Subject: [PATCH 126/309] go.mod: update golang.org/x dependencies Update golang.org/x dependencies to their latest tagged versions. Change-Id: Ie986bf7db1326094ae979a6c659790a6a4e6dcfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/648078 LUCI-TryBot-Result: Go LUCI Auto-Submit: Gopher Robot Reviewed-by: David Chase Reviewed-by: Dmitri Shuralyov --- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- gopls/go.mod | 8 ++++---- gopls/go.sum | 22 +++++++++++----------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 0f49047782e..8cea866daf8 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ go 1.22.0 // => default GODEBUG has gotypesalias=0 require ( github.com/google/go-cmp v0.6.0 github.com/yuin/goldmark v1.4.13 - golang.org/x/mod v0.22.0 - golang.org/x/net v0.34.0 - golang.org/x/sync v0.10.0 + golang.org/x/mod v0.23.0 + golang.org/x/net v0.35.0 + golang.org/x/sync v0.11.0 golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 ) -require golang.org/x/sys v0.29.0 // indirect +require golang.org/x/sys v0.30.0 // indirect diff --git a/go.sum b/go.sum index c788c5fbdc3..2d11b060c08 100644 --- a/go.sum +++ b/go.sum @@ -2,13 +2,13 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= -golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= -golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= -golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= -golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= -golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= +golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= +golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= +golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMePxjXAJctxKmyjKUsjA11Uzuk= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= diff --git a/gopls/go.mod b/gopls/go.mod index 173614714cc..83620720ae6 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -7,11 +7,11 @@ go 1.23.4 require ( github.com/google/go-cmp v0.6.0 github.com/jba/templatecheck v0.7.1 - golang.org/x/mod v0.22.0 - golang.org/x/sync v0.10.0 - golang.org/x/sys v0.29.0 + golang.org/x/mod v0.23.0 + golang.org/x/sync v0.11.0 + golang.org/x/sys v0.30.0 golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9 - golang.org/x/text v0.21.0 + golang.org/x/text v0.22.0 golang.org/x/tools v0.28.0 golang.org/x/vuln v1.1.3 gopkg.in/yaml.v3 v3.0.1 diff --git a/gopls/go.sum b/gopls/go.sum index bba08403559..b2b3d925a78 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -16,36 +16,36 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= golang.org/x/exp/typeparams v0.0.0-20241210194714-1829a127f884 h1:1xaZTydL5Gsg78QharTwKfA9FY9CZ1VQj6D/AZEvHR0= golang.org/x/exp/typeparams v0.0.0-20241210194714-1829a127f884/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= -golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= +golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= +golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= -golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9 h1:L2k9GUV2TpQKVRGMjN94qfUMgUwOFimSQ6gipyJIjKw= golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9/go.mod h1:8h4Hgq+jcTvCDv2+i7NrfWwpYHcESleo2nGHxLbFLJ4= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= +golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= golang.org/x/vuln v1.1.3 h1:NPGnvPOTgnjBc9HTaUx+nj+EaUYxl5SJOWqaDYGaFYw= golang.org/x/vuln v1.1.3/go.mod h1:7Le6Fadm5FOqE9C926BCD0g12NWyhg7cxV4BwcPFuNY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 94c41d32ced998662addd81b01a9edd1e33e0346 Mon Sep 17 00:00:00 2001 From: Madeline Kalilh Date: Mon, 10 Feb 2025 11:31:51 -0500 Subject: [PATCH 127/309] gopls/internal/golang: add comment about SymbolKind Change-Id: I99bdf283f0ebd63ab1de044ca54fc8ce65136e65 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648096 Reviewed-by: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI --- gopls/internal/protocol/command/interface.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gopls/internal/protocol/command/interface.go b/gopls/internal/protocol/command/interface.go index 32e03dd388a..34adf59b38e 100644 --- a/gopls/internal/protocol/command/interface.go +++ b/gopls/internal/protocol/command/interface.go @@ -814,6 +814,9 @@ type PackageSymbol struct { Detail string `json:"detail,omitempty"` + // protocol.SymbolKind maps an integer to an enum: + // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolKind + // i.e. File = 1 Kind protocol.SymbolKind `json:"kind"` Tags []protocol.SymbolTag `json:"tags,omitempty"` From 91bac86b5c14ba7d5ed6033fe1b85d9c2aed8215 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 10 Feb 2025 16:32:24 -0500 Subject: [PATCH 128/309] internal/analysisinternal: add CanImport Add a function that reports whether one package can import another, respecting the Go toolchain's interpretation of path segments named "internal". Change-Id: I66dc90453a178d0e626117f4e3cadf30e61912dc Reviewed-on: https://go-review.googlesource.com/c/tools/+/648255 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/analysisinternal/analysis.go | 27 +++++++++++++++++ internal/analysisinternal/analysis_test.go | 34 ++++++++++++++++++++++ internal/refactor/inline/inline.go | 27 ++--------------- 3 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 internal/analysisinternal/analysis_test.go diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index abf708111bf..a1edabbe84d 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -449,3 +449,30 @@ func validateFix(fset *token.FileSet, fix *analysis.SuggestedFix) error { return nil } + +// CanImport reports whether one package is allowed to import another. +// +// TODO(adonovan): allow customization of the accessibility relation +// (e.g. for Bazel). +func CanImport(from, to string) bool { + // TODO(adonovan): better segment hygiene. + if to == "internal" || strings.HasPrefix(to, "internal/") { + // Special case: only std packages may import internal/... + // We can't reliably know whether we're in std, so we + // use a heuristic on the first segment. + first, _, _ := strings.Cut(from, "/") + if strings.Contains(first, ".") { + return false // example.com/foo ∉ std + } + if first == "testdata" { + return false // testdata/foo ∉ std + } + } + if strings.HasSuffix(to, "/internal") { + return strings.HasPrefix(from, to[:len(to)-len("/internal")]) + } + if i := strings.LastIndex(to, "/internal/"); i >= 0 { + return strings.HasPrefix(from, to[:i]) + } + return true +} diff --git a/internal/analysisinternal/analysis_test.go b/internal/analysisinternal/analysis_test.go new file mode 100644 index 00000000000..0b21876d386 --- /dev/null +++ b/internal/analysisinternal/analysis_test.go @@ -0,0 +1,34 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package analysisinternal + +import "testing" + +func TestCanImport(t *testing.T) { + for _, tt := range []struct { + from string + to string + want bool + }{ + {"fmt", "internal", true}, + {"fmt", "internal/foo", true}, + {"a.com/b", "internal", false}, + {"a.com/b", "xinternal", true}, + {"a.com/b", "internal/foo", false}, + {"a.com/b", "xinternal/foo", true}, + {"a.com/b", "a.com/internal", true}, + {"a.com/b", "a.com/b/internal", true}, + {"a.com/b", "a.com/b/internal/foo", true}, + {"a.com/b", "a.com/c/internal", false}, + {"a.com/b", "a.com/c/xinternal", true}, + {"a.com/b", "a.com/c/internal/foo", false}, + {"a.com/b", "a.com/c/xinternal/foo", true}, + } { + got := CanImport(tt.from, tt.to) + if got != tt.want { + t.Errorf("CanImport(%q, %q) = %v, want %v", tt.from, tt.to, got, tt.want) + } + } +} diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 2c897c24954..96fbb8f8706 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -23,6 +23,7 @@ import ( "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/imports" + "golang.org/x/tools/internal/analysisinternal" internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/typesinternal" @@ -331,7 +332,7 @@ func (st *state) inline() (*Result, error) { for _, imp := range res.newImports { // Check that the new imports are accessible. path, _ := strconv.Unquote(imp.spec.Path.Value) - if !canImport(caller.Types.Path(), path) { + if !analysisinternal.CanImport(caller.Types.Path(), path) { return nil, fmt.Errorf("can't inline function %v as its body refers to inaccessible package %q", callee, path) } importDecl.Specs = append(importDecl.Specs, imp.spec) @@ -3196,30 +3197,6 @@ func last[T any](slice []T) T { return *new(T) } -// canImport reports whether one package is allowed to import another. -// -// TODO(adonovan): allow customization of the accessibility relation -// (e.g. for Bazel). -func canImport(from, to string) bool { - // TODO(adonovan): better segment hygiene. - if strings.HasPrefix(to, "internal/") { - // Special case: only std packages may import internal/... - // We can't reliably know whether we're in std, so we - // use a heuristic on the first segment. - first, _, _ := strings.Cut(from, "/") - if strings.Contains(first, ".") { - return false // example.com/foo ∉ std - } - if first == "testdata" { - return false // testdata/foo ∉ std - } - } - if i := strings.LastIndex(to, "/internal/"); i >= 0 { - return strings.HasPrefix(from, to[:i]) - } - return true -} - // consistentOffsets reports whether the portion of caller.Content // that corresponds to caller.Call can be parsed as a call expression. // If not, the client has provided inconsistent information, possibly From f61b225cbfefb205c92c7ebcd3e25c20c0f3c090 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 10 Feb 2025 15:54:38 -0500 Subject: [PATCH 129/309] internal/analysisinternal: AddImport puts new import in a group If AddImport needs to add a new import, and the file's first declaration is a grouped import, then add it to that import. This is one step towards a full implementation of the issue below, and perhaps is good enough. For golang/go#71647. Change-Id: I8327b07c21c3efbd189c519e51c339b7aa4751d8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648136 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Jonathan Amsterdam --- internal/analysisinternal/addimport_test.go | 22 +++++++++++++++++++++ internal/analysisinternal/analysis.go | 22 ++++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/internal/analysisinternal/addimport_test.go b/internal/analysisinternal/addimport_test.go index 145d5861b8f..12423b7c061 100644 --- a/internal/analysisinternal/addimport_test.go +++ b/internal/analysisinternal/addimport_test.go @@ -219,6 +219,28 @@ import . "fmt" func _(Print fmt.Stringer) { fmt +}`, + }, + { + descr: descr("add import to group"), + src: `package a + +import ( + "io" +) + +func _(io.Reader) { + «fmt fmt» +}`, + want: `package a + +import ( + "io" + "fmt" +) + +func _(io.Reader) { + fmt }`, }, } { diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index a1edabbe84d..d96d22982c5 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -255,16 +255,16 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member newName = fmt.Sprintf("%s%d", preferredName, i) } - // For now, keep it real simple: create a new import - // declaration before the first existing declaration (which - // must exist), including its comments, and let goimports tidy it up. + // Create a new import declaration either before the first existing + // declaration (which must exist), including its comments; or + // inside the declaration, if it is an import group. // // Use a renaming import whenever the preferred name is not // available, or the chosen name does not match the last // segment of its path. - newText := fmt.Sprintf("import %q\n\n", pkgpath) + newText := fmt.Sprintf("%q", pkgpath) if newName != preferredName || newName != pathpkg.Base(pkgpath) { - newText = fmt.Sprintf("import %s %q\n\n", newName, pkgpath) + newText = fmt.Sprintf("%s %q", newName, pkgpath) } decl0 := file.Decls[0] var before ast.Node = decl0 @@ -278,9 +278,17 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member before = decl0.Doc } } + // If the first decl is an import group, add this new import at the end. + if gd, ok := before.(*ast.GenDecl); ok && gd.Tok == token.IMPORT && gd.Rparen.IsValid() { + pos = gd.Rparen + newText = "\t" + newText + "\n" + } else { + pos = before.Pos() + newText = "import " + newText + "\n\n" + } return newName, newName + ".", []analysis.TextEdit{{ - Pos: before.Pos(), - End: before.Pos(), + Pos: pos, + End: pos, NewText: []byte(newText), }} } From 027eab55ae11cdaf85b7e426cd74249b206070a3 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Feb 2025 17:34:36 -0500 Subject: [PATCH 130/309] go/analysis/analysistest: RunWithSuggestedFix: 3-way merge This CL adds support for three-way merging to RunWithSuggestedFix, similar to the recent changes in go/analysis/internal/checker's -fix logic. Although unresolved conflicts are still considered a test failure, the diff algorithm may successfully merge identical edits, in particular redundant imports of the same package. NOTE: This does mean that existing tests whose golden files expect redundant imports will fail, and need updating. The test failure messages are improved, again following internal/checker. Fixes golang/go#67049 Fixes golang/go#68765 Change-Id: I8ace0ff0cd0b147696ec4dc742b0d63d0d713155 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647798 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- go/analysis/analysistest/analysistest.go | 284 ++++++++++-------- go/analysis/internal/checker/fix_test.go | 3 + .../src/slicesdelete/slicesdelete.go.golden | 14 - .../src/sortslice/sortslice.go.golden | 2 - 4 files changed, 164 insertions(+), 139 deletions(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 775fd20094d..08981776478 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -7,6 +7,7 @@ package analysistest import ( "bytes" + "cmp" "fmt" "go/format" "go/token" @@ -78,16 +79,24 @@ type Testing interface { Errorf(format string, args ...interface{}) } -// RunWithSuggestedFixes behaves like Run, but additionally verifies suggested fixes. -// It uses golden files placed alongside the source code under analysis: -// suggested fixes for code in example.go will be compared against example.go.golden. +// RunWithSuggestedFixes behaves like Run, but additionally applies +// suggested fixes and verifies their output. // -// Golden files can be formatted in one of two ways: as plain Go source code, or as txtar archives. -// In the first case, all suggested fixes will be applied to the original source, which will then be compared against the golden file. -// In the second case, suggested fixes will be grouped by their messages, and each set of fixes will be applied and tested separately. -// Each section in the archive corresponds to a single message. +// It uses golden files, placed alongside each source file, to express +// the desired output: the expected transformation of file example.go +// is specified in file example.go.golden. // -// A golden file using txtar may look like this: +// Golden files may be of two forms: a plain Go source file, or a +// txtar archive. +// +// A plain Go source file indicates the expected result of applying +// all suggested fixes to the original file. +// +// A txtar archive specifies, in each section, the expected result of +// applying all suggested fixes of a given message to the original +// file; the name of the archive section is the fix's message. In this +// way, the various alternative fixes offered by a single diagnostic +// can be tested independently. Here's an example: // // -- turn into single negation -- // package pkg @@ -109,41 +118,28 @@ type Testing interface { // // # Conflicts // -// A single analysis pass may offer two or more suggested fixes that -// (1) conflict but are nonetheless logically composable, (e.g. -// because both update the import declaration), or (2) are -// fundamentally incompatible (e.g. alternative fixes to the same -// statement). -// -// It is up to the driver to decide how to apply such fixes. A -// sophisticated driver could attempt to resolve conflicts of the -// first kind, but this test driver simply reports the fact of the -// conflict with the expectation that the user will split their tests -// into nonconflicting parts. +// Regardless of the form of the golden file, it is possible for +// multiple fixes to conflict, either because they overlap, or are +// close enough together that the particular diff algorithm cannot +// separate them. // -// Conflicts of the second kind can be avoided by giving the -// alternative fixes different names (SuggestedFix.Message) and -// defining the .golden file as a multi-section txtar file with a -// named section for each alternative fix, as shown above. +// RunWithSuggestedFixes uses a simple three-way merge to accumulate +// fixes, similar to a git merge. The merge algorithm may be able to +// coalesce identical edits, for example duplicate imports of the same +// package. (Bear in mind that this is an editorial decision. In +// general, coalescing identical edits may not be correct: consider +// two statements that increment the same counter.) // -// Analyzers that compute fixes from a textual diff of the -// before/after file contents (instead of directly from syntax tree -// positions) may produce fixes that, although logically -// non-conflicting, nonetheless conflict due to the particulars of the -// diff algorithm. In such cases it may suffice to introduce -// sufficient separation of the statements in the test input so that -// the computed diffs do not overlap. If that fails, break the test -// into smaller parts. +// If there are conflicts, the test fails. In any case, the +// non-conflicting edits will be compared against the expected output. +// In this situation, we recommend that you increase the textual +// separation between conflicting parts or, if that fails, split +// your tests into smaller parts. // -// TODO(adonovan): the behavior of RunWithSuggestedFixes as documented -// above is impractical for tests that report multiple diagnostics and -// offer multiple alternative fixes for the same diagnostic, and it is -// inconsistent with the interpretation of multiple diagnostics -// described at Diagnostic.SuggestedFixes. -// We need to rethink the analyzer testing API to better support such -// cases. In the meantime, users of RunWithSuggestedFixes testing -// analyzers that offer alternative fixes are advised to put each fix -// in a separate .go file in the testdata. +// If a diagnostic offers multiple fixes for the same problem, they +// are almost certain to conflict, so in this case you should define +// the expected output using a multi-section txtar file as described +// above. func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result { results := Run(t, dir, a, patterns...) @@ -173,133 +169,165 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns for _, result := range results { act := result.Action - // file -> message -> edits - // TODO(adonovan): this mapping assumes fix.Messages are unique across analyzers, - // whereas they are only unique within a given Diagnostic. - fileEdits := make(map[*token.File]map[string][]diff.Edit) - - // We may assume that fixes are validated upon creation in Pass.Report. - // Group fixes by file and message. + // For each fix, split its edits by file and convert to diff form. + var ( + // fixEdits: message -> fixes -> filename -> edits + // + // TODO(adonovan): this mapping assumes fix.Messages + // are unique across analyzers, whereas they are only + // unique within a given Diagnostic. + fixEdits = make(map[string][]map[string][]diff.Edit) + allFilenames = make(map[string]bool) + ) for _, diag := range act.Diagnostics { + // Fixes are validated upon creation in Pass.Report. for _, fix := range diag.SuggestedFixes { // Assert that lazy fixes have a Category (#65578, #65087). if inTools && len(fix.TextEdits) == 0 && diag.Category == "" { t.Errorf("missing Diagnostic.Category for SuggestedFix without TextEdits (gopls requires the category for the name of the fix command") } + // Convert edits to diff form. + // Group fixes by message and file. + edits := make(map[string][]diff.Edit) for _, edit := range fix.TextEdits { file := act.Package.Fset.File(edit.Pos) - if _, ok := fileEdits[file]; !ok { - fileEdits[file] = make(map[string][]diff.Edit) - } - fileEdits[file][fix.Message] = append(fileEdits[file][fix.Message], diff.Edit{ + allFilenames[file.Name()] = true + edits[file.Name()] = append(edits[file.Name()], diff.Edit{ Start: file.Offset(edit.Pos), End: file.Offset(edit.End), New: string(edit.NewText), }) } + fixEdits[fix.Message] = append(fixEdits[fix.Message], edits) } } - for file, fixes := range fileEdits { - // Get the original file contents. - // TODO(adonovan): plumb pass.ReadFile. - orig, err := os.ReadFile(file.Name()) + merge := func(file, message string, x, y []diff.Edit) []diff.Edit { + z, ok := diff.Merge(x, y) + if !ok { + t.Errorf("in file %s, conflict applying fix %q", file, message) + return x // discard y + } + return z + } + + // Because the checking is driven by original + // filenames, there is no way to express that a fix + // (e.g. extract declaration) creates a new file. + for _, filename := range sortedKeys(allFilenames) { + // Read the original file. + content, err := os.ReadFile(filename) if err != nil { - t.Errorf("error reading %s: %v", file.Name(), err) + t.Errorf("error reading %s: %v", filename, err) continue } - // Get the golden file and read the contents. - ar, err := txtar.ParseFile(file.Name() + ".golden") + // check checks that the accumulated edits applied + // to the original content yield the wanted content. + check := func(prefix string, accumulated []diff.Edit, want []byte) { + if err := applyDiffsAndCompare(filename, content, want, accumulated); err != nil { + t.Errorf("%s: %s", prefix, err) + } + } + + // Read the golden file. It may have one of two forms: + // (1) A txtar archive with one section per fix title, + // including all fixes of just that title. + // (2) The expected output for file.Name after all (?) fixes are applied. + // This form requires that no diagnostic has multiple fixes. + ar, err := txtar.ParseFile(filename + ".golden") if err != nil { - t.Errorf("error reading %s.golden: %v", file.Name(), err) + t.Errorf("error reading %s.golden: %v", filename, err) continue } - if len(ar.Files) > 0 { - // one virtual file per kind of suggested fix - - if len(ar.Comment) != 0 { - // we allow either just the comment, or just virtual - // files, not both. it is not clear how "both" should - // behave. - t.Errorf("%s.golden has leading comment; we don't know what to do with it", file.Name()) + // Form #1: one archive section per kind of suggested fix. + if len(ar.Comment) > 0 { + // Disallow the combination of comment and archive sections. + t.Errorf("%s.golden has leading comment; we don't know what to do with it", filename) continue } - // Sort map keys for determinism in tests. - // TODO(jba): replace with slices.Sorted(maps.Keys(fixes)) when go.mod >= 1.23. - var keys []string - for k := range fixes { - keys = append(keys, k) - } - slices.Sort(keys) - for _, sf := range keys { - edits := fixes[sf] - found := false - for _, vf := range ar.Files { - if vf.Name == sf { - found = true - // the file may contain multiple trailing - // newlines if the user places empty lines - // between files in the archive. normalize - // this to a single newline. - golden := append(bytes.TrimRight(vf.Data, "\n"), '\n') - - if err := applyDiffsAndCompare(orig, golden, edits, file.Name()); err != nil { - t.Errorf("%s", err) - } - break - } - } - if !found { - t.Errorf("no section for suggested fix %q in %s.golden", sf, file.Name()) + + // Each archive section is named for a fix.Message. + // Accumulate the parts of the fix that apply to the current file, + // using a simple three-way merge, discarding conflicts, + // then apply the merged edits and compare to the archive section. + for _, section := range ar.Files { + message, want := section.Name, section.Data + var accumulated []diff.Edit + for _, fix := range fixEdits[message] { + accumulated = merge(filename, message, accumulated, fix[filename]) } - } - } else { - // all suggested fixes are represented by a single file - // TODO(adonovan): fix: this makes no sense if len(fixes) > 1. - // Sort map keys for determinism in tests. - // TODO(jba): replace with slices.Sorted(maps.Keys(fixes)) when go.mod >= 1.23. - var keys []string - for k := range fixes { - keys = append(keys, k) - } - slices.Sort(keys) - var catchallEdits []diff.Edit - for _, k := range keys { - catchallEdits = append(catchallEdits, fixes[k]...) + check(fmt.Sprintf("all fixes of message %q", message), accumulated, want) } - if err := applyDiffsAndCompare(orig, ar.Comment, catchallEdits, file.Name()); err != nil { - t.Errorf("%s", err) + } else { + // Form #2: all suggested fixes are represented by a single file. + want := ar.Comment + var accumulated []diff.Edit + for _, message := range sortedKeys(fixEdits) { + for _, fix := range fixEdits[message] { + accumulated = merge(filename, message, accumulated, fix[filename]) + } } + check("all fixes", accumulated, want) } } } + return results } -// applyDiffsAndCompare applies edits to src and compares the results against -// golden after formatting both. fileName is use solely for error reporting. -func applyDiffsAndCompare(src, golden []byte, edits []diff.Edit, fileName string) error { - out, err := diff.ApplyBytes(src, edits) +// applyDiffsAndCompare applies edits to original and compares the results against +// want after formatting both. fileName is use solely for error reporting. +func applyDiffsAndCompare(filename string, original, want []byte, edits []diff.Edit) error { + // Relativize filename, for tidier errors. + if cwd, err := os.Getwd(); err == nil { + if rel, err := filepath.Rel(cwd, filename); err == nil { + filename = rel + } + } + + if len(edits) == 0 { + return fmt.Errorf("%s: no edits", filename) + } + fixedBytes, err := diff.ApplyBytes(original, edits) if err != nil { - return fmt.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", fileName, err) + return fmt.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", filename, err) } - wantRaw, err := format.Source(golden) + fixed, err := format.Source(fixedBytes) if err != nil { - return fmt.Errorf("%s.golden: error formatting golden file: %v\n%s", fileName, err, out) + return fmt.Errorf("%s: error formatting resulting source: %v\n%s", filename, err, fixed) } - want := string(wantRaw) - formatted, err := format.Source(out) + want, err = format.Source(want) if err != nil { - return fmt.Errorf("%s: error formatting resulting source: %v\n%s", fileName, err, out) + return fmt.Errorf("%s.golden: error formatting golden file: %v\n%s", filename, err, fixed) + } + + // Keep error reporting logic below consistent with + // TestScript in ../internal/checker/fix_test.go! + + unified := func(xlabel, ylabel string, x, y []byte) string { + x = append(slices.Clip(bytes.TrimSpace(x)), '\n') + y = append(slices.Clip(bytes.TrimSpace(y)), '\n') + return diff.Unified(xlabel, ylabel, string(x), string(y)) } - if got := string(formatted); got != want { - unified := diff.Unified(fileName+".golden", "actual", want, got) - return fmt.Errorf("suggested fixes failed for %s:\n%s", fileName, unified) + + if diff := unified(filename+" (fixed)", filename+" (want)", fixed, want); diff != "" { + return fmt.Errorf("unexpected %s content:\n"+ + "-- original --\n%s\n"+ + "-- fixed --\n%s\n"+ + "-- want --\n%s\n"+ + "-- diff original fixed --\n%s\n"+ + "-- diff fixed want --\n%s", + filename, + original, + fixed, + want, + unified(filename+" (original)", filename+" (fixed)", original, fixed), + diff) } return nil } @@ -740,3 +768,13 @@ func sanitize(gopath, filename string) string { prefix := gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) return filepath.ToSlash(strings.TrimPrefix(filename, prefix)) } + +// TODO(adonovan): use better stuff from go1.23. +func sortedKeys[K cmp.Ordered, V any](m map[K]V) []K { + keys := make([]K, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + slices.Sort(keys) + return keys +} diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index 8fb7506ac70..8f4e7a3f6a9 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -281,6 +281,9 @@ func TestScript(t *testing.T) { t.Logf("%s: $ %s\nstdout:\n%s\nstderr:\n%s", prefix, clean(cmd), stdout, lastStderr) } + // Keep error reporting logic below consistent with + // applyDiffsAndCompare in ../../analysistest/analysistest.go! + unified := func(xlabel, ylabel string, x, y []byte) string { x = append(slices.Clip(bytes.TrimSpace(x)), '\n') y = append(slices.Clip(bytes.TrimSpace(y)), '\n') diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden index 9b2ba9a0b80..2d9447af3a3 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden @@ -2,20 +2,6 @@ package slicesdelete import "slices" -import "slices" - -import "slices" - -import "slices" - -import "slices" - -import "slices" - -import "slices" - -import "slices" - var g struct{ f []int } func slicesdelete(test, other []byte, i int) { diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden index d97636fd311..34af5aad60b 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden @@ -2,8 +2,6 @@ package sortslice import "slices" -import "slices" - import "sort" type myint int From 0d16805d3d4f589b6910ab64a4c8e18dc5f02f16 Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Tue, 11 Feb 2025 10:28:37 -0800 Subject: [PATCH 131/309] internal/stdlib: update stdlib index for Go 1.24.0 For golang/go#38706. Change-Id: Iaa6281ad4c18906c8dc1733df29ccc6b78130fb4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648556 Reviewed-by: Cherry Mui Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Gopher Robot --- internal/stdlib/manifest.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/internal/stdlib/manifest.go b/internal/stdlib/manifest.go index 9f0b871ff6b..e7d0aee2186 100644 --- a/internal/stdlib/manifest.go +++ b/internal/stdlib/manifest.go @@ -2151,6 +2151,8 @@ var PackageSymbols = map[string][]Symbol{ {"(Type).String", Method, 0}, {"(Version).GoString", Method, 0}, {"(Version).String", Method, 0}, + {"(VersionIndex).Index", Method, 24}, + {"(VersionIndex).IsHidden", Method, 24}, {"ARM_MAGIC_TRAMP_NUMBER", Const, 0}, {"COMPRESS_HIOS", Const, 6}, {"COMPRESS_HIPROC", Const, 6}, @@ -3834,6 +3836,7 @@ var PackageSymbols = map[string][]Symbol{ {"SymType", Type, 0}, {"SymVis", Type, 0}, {"Symbol", Type, 0}, + {"Symbol.HasVersion", Field, 24}, {"Symbol.Info", Field, 0}, {"Symbol.Library", Field, 13}, {"Symbol.Name", Field, 0}, @@ -3843,18 +3846,12 @@ var PackageSymbols = map[string][]Symbol{ {"Symbol.Value", Field, 0}, {"Symbol.Version", Field, 13}, {"Symbol.VersionIndex", Field, 24}, - {"Symbol.VersionScope", Field, 24}, - {"SymbolVersionScope", Type, 24}, {"Type", Type, 0}, {"VER_FLG_BASE", Const, 24}, {"VER_FLG_INFO", Const, 24}, {"VER_FLG_WEAK", Const, 24}, {"Version", Type, 0}, - {"VersionScopeGlobal", Const, 24}, - {"VersionScopeHidden", Const, 24}, - {"VersionScopeLocal", Const, 24}, - {"VersionScopeNone", Const, 24}, - {"VersionScopeSpecific", Const, 24}, + {"VersionIndex", Type, 24}, }, "debug/gosym": { {"(*DecodingError).Error", Method, 0}, From d2585c467c83030b6ee984e63dce55e799ff4741 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 31 Jan 2025 11:39:18 -0500 Subject: [PATCH 132/309] gopls/internal/golang: folding range: remove FoldingRangeInfo Another unnecessary data type. Updates golang/go#71489 Change-Id: I374f677afe44abf818a35741202579abfed4aeb3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645855 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/golang/folding_range.go | 64 +++++++++++++------------- gopls/internal/server/folding_range.go | 21 +-------- 2 files changed, 34 insertions(+), 51 deletions(-) diff --git a/gopls/internal/golang/folding_range.go b/gopls/internal/golang/folding_range.go index 9d80cc8de29..4352da28151 100644 --- a/gopls/internal/golang/folding_range.go +++ b/gopls/internal/golang/folding_range.go @@ -6,6 +6,7 @@ package golang import ( "bytes" + "cmp" "context" "go/ast" "go/token" @@ -20,14 +21,8 @@ import ( "golang.org/x/tools/gopls/internal/util/safetoken" ) -// FoldingRangeInfo holds range and kind info of folding for an ast.Node -type FoldingRangeInfo struct { - Range protocol.Range - Kind protocol.FoldingRangeKind -} - // FoldingRange gets all of the folding range for f. -func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lineFoldingOnly bool) (ranges []*FoldingRangeInfo, err error) { +func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lineFoldingOnly bool) ([]protocol.FoldingRange, error) { // TODO(suzmue): consider limiting the number of folding ranges returned, and // implement a way to prioritize folding ranges in that case. pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) @@ -48,27 +43,29 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, } // Get folding ranges for comments separately as they are not walked by ast.Inspect. - ranges = append(ranges, commentsFoldingRange(pgf)...) + ranges := commentsFoldingRange(pgf) - visit := func(n ast.Node) bool { - rng := foldingRangeFunc(pgf, n, lineFoldingOnly) - if rng != nil { + // Walk the ast and collect folding ranges. + ast.Inspect(pgf.File, func(n ast.Node) bool { + if rng, ok := foldingRangeFunc(pgf, n, lineFoldingOnly); ok { ranges = append(ranges, rng) } return true - } - // Walk the ast and collect folding ranges. - ast.Inspect(pgf.File, visit) + }) - slices.SortFunc(ranges, func(x, y *FoldingRangeInfo) int { - return protocol.CompareRange(x.Range, y.Range) + // Sort by start position. + slices.SortFunc(ranges, func(x, y protocol.FoldingRange) int { + if d := cmp.Compare(x.StartLine, y.StartLine); d != 0 { + return d + } + return cmp.Compare(x.StartCharacter, y.StartCharacter) }) return ranges, nil } // foldingRangeFunc calculates the line folding range for ast.Node n -func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *FoldingRangeInfo { +func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) (protocol.FoldingRange, bool) { // TODO(suzmue): include trailing empty lines before the closing // parenthesis/brace. var kind protocol.FoldingRangeKind @@ -109,25 +106,22 @@ func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *Fold // Check that folding positions are valid. if !start.IsValid() || !end.IsValid() { - return nil + return protocol.FoldingRange{}, false } if start == end { // Nothing to fold. - return nil + return protocol.FoldingRange{}, false } // in line folding mode, do not fold if the start and end lines are the same. if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) { - return nil + return protocol.FoldingRange{}, false } rng, err := pgf.PosRange(start, end) if err != nil { bug.Reportf("failed to create range: %s", err) // can't happen - return nil - } - return &FoldingRangeInfo{ - Range: rng, - Kind: kind, + return protocol.FoldingRange{}, false } + return foldingRange(kind, rng), true } // getLineFoldingRange returns the folding range for nodes with parentheses/braces/brackets @@ -196,7 +190,7 @@ func getLineFoldingRange(pgf *parsego.File, open, close token.Pos, lineFoldingOn // commentsFoldingRange returns the folding ranges for all comment blocks in file. // The folding range starts at the end of the first line of the comment block, and ends at the end of the // comment block and has kind protocol.Comment. -func commentsFoldingRange(pgf *parsego.File) (comments []*FoldingRangeInfo) { +func commentsFoldingRange(pgf *parsego.File) (comments []protocol.FoldingRange) { tokFile := pgf.Tok for _, commentGrp := range pgf.File.Comments { startGrpLine, endGrpLine := safetoken.Line(tokFile, commentGrp.Pos()), safetoken.Line(tokFile, commentGrp.End()) @@ -218,11 +212,19 @@ func commentsFoldingRange(pgf *parsego.File) (comments []*FoldingRangeInfo) { bug.Reportf("failed to create mapped range: %s", err) // can't happen continue } - comments = append(comments, &FoldingRangeInfo{ - // Fold from the end of the first line comment to the end of the comment block. - Range: rng, - Kind: protocol.Comment, - }) + // Fold from the end of the first line comment to the end of the comment block. + comments = append(comments, foldingRange(protocol.Comment, rng)) } return comments } + +func foldingRange(kind protocol.FoldingRangeKind, rng protocol.Range) protocol.FoldingRange { + return protocol.FoldingRange{ + // I have no idea why LSP doesn't use a protocol.Range here. + StartLine: rng.Start.Line, + StartCharacter: rng.Start.Character, + EndLine: rng.End.Line, + EndCharacter: rng.End.Character, + Kind: string(kind), + } +} diff --git a/gopls/internal/server/folding_range.go b/gopls/internal/server/folding_range.go index 95b2ffc0744..b05d5302f10 100644 --- a/gopls/internal/server/folding_range.go +++ b/gopls/internal/server/folding_range.go @@ -26,24 +26,5 @@ func (s *server) FoldingRange(ctx context.Context, params *protocol.FoldingRange if snapshot.FileKind(fh) != file.Go { return nil, nil // empty result } - ranges, err := golang.FoldingRange(ctx, snapshot, fh, snapshot.Options().LineFoldingOnly) - if err != nil { - return nil, err - } - return toProtocolFoldingRanges(ranges) -} - -func toProtocolFoldingRanges(ranges []*golang.FoldingRangeInfo) ([]protocol.FoldingRange, error) { - result := make([]protocol.FoldingRange, 0, len(ranges)) - for _, info := range ranges { - rng := info.Range - result = append(result, protocol.FoldingRange{ - StartLine: rng.Start.Line, - StartCharacter: rng.Start.Character, - EndLine: rng.End.Line, - EndCharacter: rng.End.Character, - Kind: string(info.Kind), - }) - } - return result, nil + return golang.FoldingRange(ctx, snapshot, fh, snapshot.Options().LineFoldingOnly) } From b3c5d108cdc6a215e5a4169c71a1c4dedbc69a83 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 11 Feb 2025 20:58:26 +0000 Subject: [PATCH 133/309] gopls: record telemetry counters for settings that are used Instrument telemetry recording which settings are passed to gopls. In some cases, this merely records whether settings were set. In others, it records buckets for the setting value. Fixes golang/go#71285 Change-Id: I820318fe9cf1b05accb3105e5e2d6ddc3c5e768f Reviewed-on: https://go-review.googlesource.com/c/tools/+/648416 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- gopls/internal/cache/session_test.go | 3 +- gopls/internal/server/general.go | 17 +- gopls/internal/settings/settings.go | 201 ++++++++++++------- gopls/internal/settings/settings_test.go | 2 +- gopls/internal/telemetry/counterpath.go | 30 +++ gopls/internal/telemetry/counterpath_test.go | 47 +++++ gopls/internal/telemetry/telemetry_test.go | 44 ++++ 7 files changed, 262 insertions(+), 82 deletions(-) create mode 100644 gopls/internal/telemetry/counterpath.go create mode 100644 gopls/internal/telemetry/counterpath_test.go diff --git a/gopls/internal/cache/session_test.go b/gopls/internal/cache/session_test.go index 5f9a59a4945..1b7472af605 100644 --- a/gopls/internal/cache/session_test.go +++ b/gopls/internal/cache/session_test.go @@ -337,7 +337,8 @@ replace ( for _, f := range test.folders { opts := settings.DefaultOptions() if f.options != nil { - for _, err := range opts.Set(f.options(dir)) { + _, errs := opts.Set(f.options(dir)) + for _, err := range errs { t.Fatal(err) } } diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go index 35614945f9d..de6b764c79f 100644 --- a/gopls/internal/server/general.go +++ b/gopls/internal/server/general.go @@ -28,6 +28,7 @@ import ( "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/protocol/semtok" "golang.org/x/tools/gopls/internal/settings" + "golang.org/x/tools/gopls/internal/telemetry" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/goversion" "golang.org/x/tools/gopls/internal/util/moremaps" @@ -74,7 +75,11 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ // TODO(rfindley): eliminate this defer. defer func() { s.SetOptions(options) }() - s.handleOptionErrors(ctx, options.Set(params.InitializationOptions)) + // Process initialization options. + { + res, errs := options.Set(params.InitializationOptions) + s.handleOptionResult(ctx, res, errs) + } options.ForClientCapabilities(params.ClientInfo, params.Capabilities) if options.ShowBugReports { @@ -541,7 +546,8 @@ func (s *server) fetchFolderOptions(ctx context.Context, folder protocol.Documen opts = opts.Clone() for _, config := range configs { - s.handleOptionErrors(ctx, opts.Set(config)) + res, errs := opts.Set(config) + s.handleOptionResult(ctx, res, errs) } return opts, nil } @@ -555,7 +561,12 @@ func (s *server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMe s.notifications = append(s.notifications, msg) } -func (s *server) handleOptionErrors(ctx context.Context, optionErrors []error) { +func (s *server) handleOptionResult(ctx context.Context, applied []telemetry.CounterPath, optionErrors []error) { + for _, path := range applied { + path = append(settings.CounterPath{"gopls", "setting"}, path...) + counter.Inc(path.FullName()) + } + var warnings, errs []string for _, err := range optionErrors { if err == nil { diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 8f33bdae96b..7d64cbef459 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/protocol/semtok" + "golang.org/x/tools/gopls/internal/telemetry" "golang.org/x/tools/gopls/internal/util/frob" ) @@ -822,10 +823,18 @@ const ( // TODO: support "Manual"? ) -// Set updates *options based on the provided JSON value: +type CounterPath = telemetry.CounterPath + +// Set updates *Options based on the provided JSON value: // null, bool, string, number, array, or object. +// +// The applied result describes settings that were applied. Each CounterPath +// contains at least the name of the setting, but may also include sub-setting +// names for settings that are themselves maps, and/or a non-empty bucket name +// when bucketing is desirable. +// // On failure, it returns one or more non-nil errors. -func (o *Options) Set(value any) (errors []error) { +func (o *Options) Set(value any) (applied []CounterPath, errs []error) { switch value := value.(type) { case nil: case map[string]any: @@ -840,19 +849,32 @@ func (o *Options) Set(value any) (errors []error) { name = split[len(split)-1] if _, ok := seen[name]; ok { - errors = append(errors, fmt.Errorf("duplicate value for %s", name)) + errs = append(errs, fmt.Errorf("duplicate value for %s", name)) } seen[name] = struct{}{} - if err := o.setOne(name, value); err != nil { + paths, err := o.setOne(name, value) + if err != nil { err := fmt.Errorf("setting option %q: %w", name, err) - errors = append(errors, err) + errs = append(errs, err) + } + _, soft := err.(*SoftError) + if err == nil || soft { + if len(paths) == 0 { + path := CounterPath{name, ""} + applied = append(applied, path) + } else { + for _, subpath := range paths { + path := append(CounterPath{name}, subpath...) + applied = append(applied, path) + } + } } } default: - errors = append(errors, fmt.Errorf("invalid options type %T (want JSON null or object)", value)) + errs = append(errs, fmt.Errorf("invalid options type %T (want JSON null or object)", value)) } - return errors + return applied, errs } func (o *Options) ForClientCapabilities(clientInfo *protocol.ClientInfo, caps protocol.ClientCapabilities) { @@ -955,14 +977,26 @@ func validateDirectoryFilter(ifilter string) (string, error) { } // setOne updates a field of o based on the name and value. +// +// The applied result describes the counter values to be updated as a result of +// the applied setting. If the result is nil, the default counter for this +// setting should be updated. +// +// For example, if the setting name is "foo", +// - If applied is nil, update the count for "foo". +// - If applied is []CounterPath{{"bucket"}}, update the count for +// foo:bucket. +// - If applied is []CounterPath{{"a","b"}, {"c","d"}}, update foo/a:b and +// foo/c:d. +// // It returns an error if the value was invalid or duplicate. // It is the caller's responsibility to augment the error with 'name'. -func (o *Options) setOne(name string, value any) error { +func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error) { switch name { case "env": env, ok := value.(map[string]any) if !ok { - return fmt.Errorf("invalid type %T (want JSON object)", value) + return nil, fmt.Errorf("invalid type %T (want JSON object)", value) } if o.Env == nil { o.Env = make(map[string]string) @@ -973,30 +1007,32 @@ func (o *Options) setOne(name string, value any) error { case string, int: o.Env[k] = fmt.Sprint(v) default: - return fmt.Errorf("invalid map value %T (want string)", v) + return nil, fmt.Errorf("invalid map value %T (want string)", v) } } + return nil, nil case "buildFlags": - return setStringSlice(&o.BuildFlags, value) + return nil, setStringSlice(&o.BuildFlags, value) case "directoryFilters": filterStrings, err := asStringSlice(value) if err != nil { - return err + return nil, err } var filters []string for _, filterStr := range filterStrings { filter, err := validateDirectoryFilter(filterStr) if err != nil { - return err + return nil, err } filters = append(filters, strings.TrimRight(filepath.FromSlash(filter), "/")) } o.DirectoryFilters = filters + return nil, nil case "workspaceFiles": - return setStringSlice(&o.WorkspaceFiles, value) + return nil, setStringSlice(&o.WorkspaceFiles, value) case "completionDocumentation": return setBool(&o.CompletionDocumentation, value) case "usePlaceholders": @@ -1006,7 +1042,7 @@ func (o *Options) setOne(name string, value any) error { case "completeUnimported": return setBool(&o.CompleteUnimported, value) case "completionBudget": - return setDuration(&o.CompletionBudget, value) + return nil, setDuration(&o.CompletionBudget, value) case "importsSource": return setEnum(&o.ImportsSource, value, ImportsSourceOff, @@ -1038,7 +1074,7 @@ func (o *Options) setOne(name string, value any) error { case "hoverKind": if s, ok := value.(string); ok && strings.EqualFold(s, "structured") { - return deprecatedError("the experimental hoverKind='structured' setting was removed in gopls/v0.18.0 (https://go.dev/issue/70233)") + return nil, deprecatedError("the experimental hoverKind='structured' setting was removed in gopls/v0.18.0 (https://go.dev/issue/70233)") } return setEnum(&o.HoverKind, value, NoDocumentation, @@ -1047,7 +1083,7 @@ func (o *Options) setOne(name string, value any) error { FullDocumentation) case "linkTarget": - return setString(&o.LinkTarget, value) + return nil, setString(&o.LinkTarget, value) case "linksInHover": switch value { @@ -1058,9 +1094,9 @@ func (o *Options) setOne(name string, value any) error { case "gopls": o.LinksInHover = LinksInHover_Gopls default: - return fmt.Errorf(`invalid value %s; expect false, true, or "gopls"`, - value) + return nil, fmt.Errorf(`invalid value %s; expect false, true, or "gopls"`, value) } + return nil, nil case "importShortcut": return setEnum(&o.ImportShortcut, value, @@ -1069,18 +1105,20 @@ func (o *Options) setOne(name string, value any) error { DefinitionShortcut) case "analyses": - if err := setBoolMap(&o.Analyses, value); err != nil { - return err + counts, err := setBoolMap(&o.Analyses, value) + if err != nil { + return nil, err } if o.Analyses["fieldalignment"] { - return deprecatedError("the 'fieldalignment' analyzer was removed in gopls/v0.17.0; instead, hover over struct fields to see size/offset information (https://go.dev/issue/66861)") + return counts, deprecatedError("the 'fieldalignment' analyzer was removed in gopls/v0.17.0; instead, hover over struct fields to see size/offset information (https://go.dev/issue/66861)") } + return counts, nil case "hints": return setBoolMap(&o.Hints, value) case "annotations": - return deprecatedError("the 'annotations' setting was removed in gopls/v0.18.0; all compiler optimization details are now shown") + return nil, deprecatedError("the 'annotations' setting was removed in gopls/v0.18.0; all compiler optimization details are now shown") case "vulncheck": return setEnum(&o.Vulncheck, value, @@ -1090,7 +1128,7 @@ func (o *Options) setOne(name string, value any) error { case "codelenses", "codelens": lensOverrides, err := asBoolMap[CodeLensSource](value) if err != nil { - return err + return nil, err } if o.Codelenses == nil { o.Codelenses = make(map[CodeLensSource]bool) @@ -1098,15 +1136,21 @@ func (o *Options) setOne(name string, value any) error { o.Codelenses = maps.Clone(o.Codelenses) maps.Copy(o.Codelenses, lensOverrides) + var counts []CounterPath + for k, v := range lensOverrides { + counts = append(counts, CounterPath{string(k), fmt.Sprint(v)}) + } + if name == "codelens" { - return deprecatedError("codelenses") + return counts, deprecatedError("codelenses") } + return counts, nil case "staticcheck": return setBool(&o.Staticcheck, value) case "local": - return setString(&o.Local, value) + return nil, setString(&o.Local, value) case "verboseOutput": return setBool(&o.VerboseOutput, value) @@ -1128,16 +1172,18 @@ func (o *Options) setOne(name string, value any) error { // TODO(hxjiang): deprecate noSemanticString and noSemanticNumber. case "noSemanticString": - if err := setBool(&o.NoSemanticString, value); err != nil { - return err + counts, err := setBool(&o.NoSemanticString, value) + if err != nil { + return nil, err } - return &SoftError{fmt.Sprintf("noSemanticString setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being).")} + return counts, &SoftError{"noSemanticString setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being)."} case "noSemanticNumber": - if err := setBool(&o.NoSemanticNumber, value); err != nil { - return nil + counts, err := setBool(&o.NoSemanticNumber, value) + if err != nil { + return nil, err } - return &SoftError{fmt.Sprintf("noSemanticNumber setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being).")} + return counts, &SoftError{"noSemanticNumber setting is deprecated, use semanticTokenTypes instead (though you can continue to apply them for the time being)."} case "semanticTokenTypes": return setBoolMap(&o.SemanticTokenTypes, value) @@ -1157,15 +1203,16 @@ func (o *Options) setOne(name string, value any) error { case "templateExtensions": switch value := value.(type) { case []any: - return setStringSlice(&o.TemplateExtensions, value) + return nil, setStringSlice(&o.TemplateExtensions, value) case nil: o.TemplateExtensions = nil default: - return fmt.Errorf("unexpected type %T (want JSON array of string)", value) + return nil, fmt.Errorf("unexpected type %T (want JSON array of string)", value) } + return nil, nil case "diagnosticsDelay": - return setDuration(&o.DiagnosticsDelay, value) + return nil, setDuration(&o.DiagnosticsDelay, value) case "diagnosticsTrigger": return setEnum(&o.DiagnosticsTrigger, value, @@ -1175,11 +1222,8 @@ func (o *Options) setOne(name string, value any) error { case "analysisProgressReporting": return setBool(&o.AnalysisProgressReporting, value) - case "allowImplicitNetworkAccess": - return deprecatedError("") - case "standaloneTags": - return setStringSlice(&o.StandaloneTags, value) + return nil, setStringSlice(&o.StandaloneTags, value) case "subdirWatchPatterns": return setEnum(&o.SubdirWatchPatterns, value, @@ -1188,7 +1232,7 @@ func (o *Options) setOne(name string, value any) error { SubdirWatchPatternsAuto) case "reportAnalysisProgressAfter": - return setDuration(&o.ReportAnalysisProgressAfter, value) + return nil, setDuration(&o.ReportAnalysisProgressAfter, value) case "telemetryPrompt": return setBool(&o.TelemetryPrompt, value) @@ -1213,50 +1257,54 @@ func (o *Options) setOne(name string, value any) error { // renamed case "experimentalDisabledAnalyses": - return deprecatedError("analyses") + return nil, deprecatedError("analyses") case "disableDeepCompletion": - return deprecatedError("deepCompletion") + return nil, deprecatedError("deepCompletion") case "disableFuzzyMatching": - return deprecatedError("fuzzyMatching") + return nil, deprecatedError("fuzzyMatching") case "wantCompletionDocumentation": - return deprecatedError("completionDocumentation") + return nil, deprecatedError("completionDocumentation") case "wantUnimportedCompletions": - return deprecatedError("completeUnimported") + return nil, deprecatedError("completeUnimported") case "fuzzyMatching": - return deprecatedError("matcher") + return nil, deprecatedError("matcher") case "caseSensitiveCompletion": - return deprecatedError("matcher") + return nil, deprecatedError("matcher") case "experimentalDiagnosticsDelay": - return deprecatedError("diagnosticsDelay") + return nil, deprecatedError("diagnosticsDelay") // deprecated + + case "allowImplicitNetworkAccess": + return nil, deprecatedError("") + case "memoryMode": - return deprecatedError("") + return nil, deprecatedError("") case "tempModFile": - return deprecatedError("") + return nil, deprecatedError("") case "experimentalWorkspaceModule": - return deprecatedError("") + return nil, deprecatedError("") case "experimentalTemplateSupport": - return deprecatedError("") + return nil, deprecatedError("") case "experimentalWatchedFileDelay": - return deprecatedError("") + return nil, deprecatedError("") case "experimentalPackageCacheKey": - return deprecatedError("") + return nil, deprecatedError("") case "allowModfileModifications": - return deprecatedError("") + return nil, deprecatedError("") case "allExperiments": // golang/go#65548: this setting is a no-op, but we fail don't report it as @@ -1265,29 +1313,29 @@ func (o *Options) setOne(name string, value any) error { // If, in the future, VS Code stops injecting this, we could theoretically // report an error here, but it also seems harmless to keep ignoring this // setting forever. + return nil, nil case "experimentalUseInvalidMetadata": - return deprecatedError("") + return nil, deprecatedError("") case "newDiff": - return deprecatedError("") + return nil, deprecatedError("") case "wantSuggestedFixes": - return deprecatedError("") + return nil, deprecatedError("") case "noIncrementalSync": - return deprecatedError("") + return nil, deprecatedError("") case "watchFileChanges": - return deprecatedError("") + return nil, deprecatedError("") case "go-diff": - return deprecatedError("") + return nil, deprecatedError("") default: - return fmt.Errorf("unexpected setting") + return nil, fmt.Errorf("unexpected setting") } - return nil } // EnabledSemanticTokenModifiers returns a map of modifiers to boolean. @@ -1323,11 +1371,6 @@ func (e *SoftError) Error() string { return e.msg } -// softErrorf reports a soft error related to the current option. -func softErrorf(format string, args ...any) error { - return &SoftError{fmt.Sprintf(format, args...)} -} - // deprecatedError reports the current setting as deprecated. // The optional replacement is suggested to the user. func deprecatedError(replacement string) error { @@ -1341,13 +1384,13 @@ func deprecatedError(replacement string) error { // setT() and asT() helpers: the setT forms write to the 'dest *T' // variable only on success, to reduce boilerplate in Option.set. -func setBool(dest *bool, value any) error { +func setBool(dest *bool, value any) ([]CounterPath, error) { b, err := asBool(value) if err != nil { - return err + return nil, err } *dest = b - return nil + return []CounterPath{{fmt.Sprint(b)}}, nil } func asBool(value any) (bool, error) { @@ -1371,13 +1414,17 @@ func setDuration(dest *time.Duration, value any) error { return nil } -func setBoolMap[K ~string](dest *map[K]bool, value any) error { +func setBoolMap[K ~string](dest *map[K]bool, value any) ([]CounterPath, error) { m, err := asBoolMap[K](value) if err != nil { - return err + return nil, err } *dest = m - return nil + var counts []CounterPath + for k, v := range m { + counts = append(counts, CounterPath{string(k), fmt.Sprint(v)}) + } + return counts, nil } func asBoolMap[K ~string](value any) (map[K]bool, error) { @@ -1438,13 +1485,13 @@ func asStringSlice(value any) ([]string, error) { return slice, nil } -func setEnum[S ~string](dest *S, value any, options ...S) error { +func setEnum[S ~string](dest *S, value any, options ...S) ([]CounterPath, error) { enum, err := asEnum(value, options...) if err != nil { - return err + return nil, err } *dest = enum - return nil + return []CounterPath{{string(enum)}}, nil } func asEnum[S ~string](value any, options ...S) (S, error) { diff --git a/gopls/internal/settings/settings_test.go b/gopls/internal/settings/settings_test.go index 63b4aded8bd..05afa8ecac3 100644 --- a/gopls/internal/settings/settings_test.go +++ b/gopls/internal/settings/settings_test.go @@ -206,7 +206,7 @@ func TestOptions_Set(t *testing.T) { for _, test := range tests { var opts Options - err := opts.Set(map[string]any{test.name: test.value}) + _, err := opts.Set(map[string]any{test.name: test.value}) if err != nil { if !test.wantError { t.Errorf("Options.set(%q, %v) failed: %v", diff --git a/gopls/internal/telemetry/counterpath.go b/gopls/internal/telemetry/counterpath.go new file mode 100644 index 00000000000..e6d9d84b531 --- /dev/null +++ b/gopls/internal/telemetry/counterpath.go @@ -0,0 +1,30 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package telemetry + +import "strings" + +// A CounterPath represents the components of a telemetry counter name. +// +// By convention, counter names follow the format path/to/counter:bucket. The +// CounterPath holds the '/'-separated components of this path, along with a +// final element representing the bucket. +// +// CounterPaths may be used to build up counters incrementally, such as when a +// set of observed counters shared a common prefix, to be controlled by the +// caller. +type CounterPath []string + +// FullName returns the counter name for the receiver. +func (p CounterPath) FullName() string { + if len(p) == 0 { + return "" + } + name := strings.Join([]string(p[:len(p)-1]), "/") + if bucket := p[len(p)-1]; bucket != "" { + name += ":" + bucket + } + return name +} diff --git a/gopls/internal/telemetry/counterpath_test.go b/gopls/internal/telemetry/counterpath_test.go new file mode 100644 index 00000000000..b6ac7478b72 --- /dev/null +++ b/gopls/internal/telemetry/counterpath_test.go @@ -0,0 +1,47 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package telemetry + +import ( + "testing" +) + +// TestCounterPath tests the formatting of various counter paths. +func TestCounterPath(t *testing.T) { + tests := []struct { + path CounterPath + want string + }{ + { + path: CounterPath{}, + want: "", + }, + { + path: CounterPath{"counter"}, + want: ":counter", + }, + { + path: CounterPath{"counter", "bucket"}, + want: "counter:bucket", + }, + { + path: CounterPath{"path", "to", "counter"}, + want: "path/to:counter", + }, + { + path: CounterPath{"multi", "component", "path", "bucket"}, + want: "multi/component/path:bucket", + }, + { + path: CounterPath{"path", ""}, + want: "path", + }, + } + for _, tt := range tests { + if got := tt.path.FullName(); got != tt.want { + t.Errorf("CounterPath(%v).FullName() = %v, want %v", tt.path, got, tt.want) + } + } +} diff --git a/gopls/internal/telemetry/telemetry_test.go b/gopls/internal/telemetry/telemetry_test.go index 7aaca41ab55..4c41cc40dc9 100644 --- a/gopls/internal/telemetry/telemetry_test.go +++ b/gopls/internal/telemetry/telemetry_test.go @@ -119,6 +119,50 @@ func TestTelemetry(t *testing.T) { } } +func TestSettingTelemetry(t *testing.T) { + // counters that should be incremented by each session + sessionCounters := []*counter.Counter{ + counter.New("gopls/setting/diagnosticsDelay"), + counter.New("gopls/setting/staticcheck:true"), + counter.New("gopls/setting/noSemanticString:true"), + counter.New("gopls/setting/analyses/deprecated:false"), + } + + initialCounts := make([]uint64, len(sessionCounters)) + for i, c := range sessionCounters { + count, err := countertest.ReadCounter(c) + if err != nil { + continue // counter db not open, or counter not found + } + initialCounts[i] = count + } + + // Run gopls. + WithOptions( + Modes(Default), + Settings{ + "staticcheck": true, + "analyses": map[string]bool{ + "deprecated": false, + }, + "diagnosticsDelay": "0s", + "noSemanticString": true, + }, + ).Run(t, "", func(_ *testing.T, env *Env) { + }) + + for i, c := range sessionCounters { + count, err := countertest.ReadCounter(c) + if err != nil { + t.Errorf("ReadCounter(%q) failed: %v", c.Name(), err) + continue + } + if count <= initialCounts[i] { + t.Errorf("ReadCounter(%q) = %d, want > %d", c.Name(), count, initialCounts[i]) + } + } +} + func addForwardedCounters(env *Env, names []string, values []int64) { args, err := command.MarshalArgs(command.AddTelemetryCountersArgs{ Names: names, Values: values, From 25932623b63eed2010348abe2dc5ff3e1fe6f86d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 11 Feb 2025 16:04:17 -0500 Subject: [PATCH 134/309] gopls/internal/telemetry/cmd/stacks: remove leading \b match A directory separator / does not create word boundaries, so dir/file will not match \bfile. This CL removes the leading word-boundary match from the interpretation of string literals in stacks' claim expression language, which was causing spurious duplicate issues. + test Change-Id: Ie02be3591096ddf1d38b10873bed02449e35bd56 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648579 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam --- gopls/internal/telemetry/cmd/stacks/stacks.go | 19 +++++++++++++++---- .../telemetry/cmd/stacks/stacks_test.go | 10 ++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 7cb20012657..36a675d0eb0 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -479,11 +479,20 @@ func parsePredicate(s string) (func(string) bool, error) { if err != nil { return err } - // The literal should match complete words. It may match multiple words, - // if it contains non-word runes like whitespace; but it must match word - // boundaries at each end. + // The end of the literal (usually "symbol", + // "pkg.symbol", or "pkg.symbol:+1") must + // match a word boundary. However, the start + // of the literal need not: an input line such + // as "domain.name/dir/pkg.symbol:+1" should + // match literal "pkg.symbol", but the slash + // is not a word boundary (witness: + // https://go.dev/play/p/w-8ev_VUBSq). + // + // It may match multiple words if it contains + // non-word runes like whitespace. + // // The constructed regular expression is always valid. - literalRegexps[e] = regexp.MustCompile(`\b` + regexp.QuoteMeta(lit) + `\b`) + literalRegexps[e] = regexp.MustCompile(regexp.QuoteMeta(lit) + `\b`) default: return fmt.Errorf("syntax error (%T)", e) @@ -1084,6 +1093,8 @@ type Issue struct { newStacks []string // new stacks to add to existing issue (comments and IDs) } +func (issue *Issue) String() string { return fmt.Sprintf("#%d", issue.Number) } + type User struct { Login string HTMLURL string `json:"html_url"` diff --git a/gopls/internal/telemetry/cmd/stacks/stacks_test.go b/gopls/internal/telemetry/cmd/stacks/stacks_test.go index 452113a1581..9f798aa43a3 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks_test.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks_test.go @@ -85,13 +85,15 @@ func TestParsePredicate(t *testing.T) { want bool }{ {`"x"`, `"x"`, true}, - {`"x"`, `"axe"`, false}, // literals match whole words + {`"x"`, `"axe"`, false}, // literals must match word ends + {`"xe"`, `"axe"`, true}, {`"x"`, "val:x+5", true}, {`"fu+12"`, "x:fu+12,", true}, - {`"fu+12"`, "snafu+12,", false}, + {`"fu+12"`, "snafu+12,", true}, // literals needn't match word start {`"fu+12"`, "x:fu+123,", false}, - {`"a.*b"`, "a.*b", true}, // regexp metachars are escaped - {`"a.*b"`, "axxb", false}, // ditto + {`"foo:+12"`, "dir/foo:+12,", true}, // literals needn't match word start + {`"a.*b"`, "a.*b", true}, // regexp metachars are escaped + {`"a.*b"`, "axxb", false}, // ditto {`"x"`, `"y"`, false}, {`!"x"`, "x", false}, {`!"x"`, "y", true}, From d98774edc040d4c944774f1b6777522d4d921b54 Mon Sep 17 00:00:00 2001 From: Sean Liao Date: Sun, 9 Feb 2025 13:15:11 +0000 Subject: [PATCH 135/309] cmd/signature-fuzzer/internal/fuzz-generator: update to math/rand/v2 Fixes golang/go#71613 Change-Id: Id69044282568b3564aee82dfe4c1b98c41d16d0f Reviewed-on: https://go-review.googlesource.com/c/tools/+/647896 Reviewed-by: Than McIntosh Reviewed-by: Dmitri Shuralyov Auto-Submit: Dmitri Shuralyov Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- .../internal/fuzz-generator/wraprand.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/signature-fuzzer/internal/fuzz-generator/wraprand.go b/cmd/signature-fuzzer/internal/fuzz-generator/wraprand.go index bba178dc317..f83a5f22e27 100644 --- a/cmd/signature-fuzzer/internal/fuzz-generator/wraprand.go +++ b/cmd/signature-fuzzer/internal/fuzz-generator/wraprand.go @@ -6,7 +6,7 @@ package generator import ( "fmt" - "math/rand" + "math/rand/v2" "os" "runtime" "strings" @@ -20,8 +20,7 @@ const ( ) func NewWrapRand(seed int64, ctl int) *wraprand { - rand.Seed(seed) - return &wraprand{seed: seed, ctl: ctl} + return &wraprand{seed: seed, ctl: ctl, rand: rand.New(rand.NewPCG(0, uint64(seed)))} } type wraprand struct { @@ -32,6 +31,7 @@ type wraprand struct { tag string calls []string ctl int + rand *rand.Rand } func (w *wraprand) captureCall(tag string, val string) { @@ -59,7 +59,7 @@ func (w *wraprand) captureCall(tag string, val string) { func (w *wraprand) Intn(n int64) int64 { w.intncalls++ - rv := rand.Int63n(n) + rv := w.rand.Int64N(n) if w.ctl&RandCtlCapture != 0 { w.captureCall("Intn", fmt.Sprintf("%d", rv)) } @@ -68,7 +68,7 @@ func (w *wraprand) Intn(n int64) int64 { func (w *wraprand) Float32() float32 { w.f32calls++ - rv := rand.Float32() + rv := w.rand.Float32() if w.ctl&RandCtlCapture != 0 { w.captureCall("Float32", fmt.Sprintf("%f", rv)) } @@ -77,7 +77,7 @@ func (w *wraprand) Float32() float32 { func (w *wraprand) NormFloat64() float64 { w.f64calls++ - rv := rand.NormFloat64() + rv := w.rand.NormFloat64() if w.ctl&RandCtlCapture != 0 { w.captureCall("NormFloat64", fmt.Sprintf("%f", rv)) } @@ -85,7 +85,7 @@ func (w *wraprand) NormFloat64() float64 { } func (w *wraprand) emitCalls(fn string) { - outf, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + outf, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) if err != nil { panic(err) } From b752317a21a68f705b3f8845fb2696d6f977cf4e Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 11 Feb 2025 17:01:42 -0500 Subject: [PATCH 136/309] internal/analysisinternal: disable AddImport test without go command The AddImport test uses the default importer, which calls go list. This fails in environments that can't call the go command, like js/wasm. Add a predicate to testenv that asserts the need for the default importer, and call it from TestAddImport. A subtlety: although this bug manifested itself only for the dot-import cases, in fact all the test cases failed type checking on js/wasm for this reason. But a successful type-check is not a precondition for the test (see the new comment in TestAddImport). What caused the particular test case to fail was a bad diff resulting from how the edit was applied in the presence of that failure. Fixes golang/go#71645. Change-Id: Ib04afad108c323999bb67f329cf8d9cf329fead1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648580 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/analysisinternal/addimport_test.go | 6 ++++++ internal/testenv/testenv.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/internal/analysisinternal/addimport_test.go b/internal/analysisinternal/addimport_test.go index 12423b7c061..da7c7f90114 100644 --- a/internal/analysisinternal/addimport_test.go +++ b/internal/analysisinternal/addimport_test.go @@ -18,9 +18,12 @@ import ( "github.com/google/go-cmp/cmp" "golang.org/x/tools/go/analysis" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/testenv" ) func TestAddImport(t *testing.T) { + testenv.NeedsDefaultImporter(t) + descr := func(s string) string { if _, _, line, ok := runtime.Caller(1); ok { return fmt.Sprintf("L%d %s", line, s) @@ -270,6 +273,9 @@ func _(io.Reader) { Implicits: make(map[ast.Node]types.Object), } conf := &types.Config{ + // We don't want to fail if there is an error during type checking: + // the error may be because we're missing an import, and adding imports + // is the whole point of AddImport. Error: func(err error) { t.Log(err) }, Importer: importer.Default(), } diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 144f4f8fd64..5c541b7b19b 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -278,6 +278,16 @@ func NeedsGoBuild(t testing.TB) { NeedsTool(t, "go") } +// NeedsDefaultImporter skips t if the test uses the default importer, +// returned by [go/importer.Default]. +func NeedsDefaultImporter(t testing.TB) { + t.Helper() + // The default importer may call `go list` + // (in src/internal/exportdata/exportdata.go:lookupGorootExport), + // so check for the go tool. + NeedsTool(t, "go") +} + // ExitIfSmallMachine emits a helpful diagnostic and calls os.Exit(0) if the // current machine is a builder known to have scarce resources. // From b5a64bbcfd2247d59f40403a28ca8e3a9a417a24 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Feb 2025 13:18:50 -0500 Subject: [PATCH 137/309] go/analysis/internal/checker: be silent with -fix Just apply the fixes without listing the diagnostics. Also: be verbose with -v. The -v flag exists for historical reasons to do with the vet CLI, but it had no effect. This change makes it effectively an alias for -debug=v, which no-one can remember how to spell. Also, list the number of fixes and fixes updated when -fix and -v. Change-Id: Ic2342ad4868b6c8649077bf13c48e8727dbeba37 Reviewed-on: https://go-review.googlesource.com/c/tools/+/647698 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- go/analysis/internal/checker/checker.go | 65 ++++++++++++++----- go/analysis/internal/checker/checker_test.go | 32 +++++---- go/analysis/internal/checker/fix_test.go | 4 +- go/analysis/internal/checker/start_test.go | 1 + .../internal/checker/testdata/diff.txt | 3 +- .../internal/checker/testdata/fixes.txt | 6 +- .../internal/checker/testdata/importdup.txt | 5 +- .../internal/checker/testdata/importdup2.txt | 5 +- .../internal/checker/testdata/noend.txt | 3 +- .../internal/checker/testdata/overlap.txt | 7 +- 10 files changed, 85 insertions(+), 46 deletions(-) diff --git a/go/analysis/internal/checker/checker.go b/go/analysis/internal/checker/checker.go index fb3c47b1625..2a9ff2931b3 100644 --- a/go/analysis/internal/checker/checker.go +++ b/go/analysis/internal/checker/checker.go @@ -86,7 +86,36 @@ func RegisterFlags() { // It provides most of the logic for the main functions of both the // singlechecker and the multi-analysis commands. // It returns the appropriate exit code. -func Run(args []string, analyzers []*analysis.Analyzer) int { +// +// TODO(adonovan): tests should not call this function directly; +// fiddling with global variables (flags) is error-prone and hostile +// to parallelism. Instead, use unit tests of the actual units (e.g. +// checker.Analyze) and integration tests (e.g. TestScript) of whole +// executables. +func Run(args []string, analyzers []*analysis.Analyzer) (exitcode int) { + // Instead of returning a code directly, + // call this function to monotonically increase the exit code. + // This allows us to keep going in the face of some errors + // without having to remember what code to return. + // + // TODO(adonovan): interpreting exit codes is like reading tea-leaves. + // Insted of wasting effort trying to encode a multidimensional result + // into 7 bits we should just emit structured JSON output, and + // an exit code of 0 or 1 for success or failure. + exitAtLeast := func(code int) { + exitcode = max(code, exitcode) + } + + // When analysisflags is linked in (for {single,multi}checker), + // then the -v flag is registered for complex legacy reasons + // related to cmd/vet CLI. + // Treat it as an undocumented alias for -debug=v. + if v := flag.CommandLine.Lookup("v"); v != nil && + v.Value.(flag.Getter).Get() == true && + !strings.Contains(Debug, "v") { + Debug += "v" + } + if CPUProfile != "" { f, err := os.Create(CPUProfile) if err != nil { @@ -142,17 +171,14 @@ func Run(args []string, analyzers []*analysis.Analyzer) int { initial, err := load(args, allSyntax) if err != nil { log.Print(err) - return 1 + exitAtLeast(1) + return } - // TODO(adonovan): simplify exit code logic by using a single - // exit code variable and applying "code = max(code, X)" each - // time an error of code X occurs. - pkgsExitCode := 0 // Print package and module errors regardless of RunDespiteErrors. // Do not exit if there are errors, yet. if n := packages.PrintErrors(initial); n > 0 { - pkgsExitCode = 1 + exitAtLeast(1) } var factLog io.Writer @@ -172,7 +198,8 @@ func Run(args []string, analyzers []*analysis.Analyzer) int { graph, err := checker.Analyze(analyzers, initial, opts) if err != nil { log.Print(err) - return 1 + exitAtLeast(1) + return } // Don't print the diagnostics, @@ -181,22 +208,22 @@ func Run(args []string, analyzers []*analysis.Analyzer) int { if err := applyFixes(graph.Roots, Diff); err != nil { // Fail when applying fixes failed. log.Print(err) - return 1 + exitAtLeast(1) + return } - // TODO(adonovan): don't proceed to print the text or JSON output - // if we applied fixes; stop here. - // - // return pkgsExitCode + // Don't proceed to print text/JSON, + // and don't report an error + // just because there were diagnostics. + return } // Print the results. If !RunDespiteErrors and there // are errors in the packages, this will have 0 exit // code. Otherwise, we prefer to return exit code // indicating diagnostics. - if diagExitCode := printDiagnostics(graph); diagExitCode != 0 { - return diagExitCode // there were diagnostics - } - return pkgsExitCode // package errors but no diagnostics + exitAtLeast(printDiagnostics(graph)) + + return } // printDiagnostics prints diagnostics in text or JSON form @@ -541,6 +568,10 @@ fixloop: } } + if dbg('v') { + log.Printf("applied %d fixes, updated %d files", len(fixes), filesUpdated) + } + return nil } diff --git a/go/analysis/internal/checker/checker_test.go b/go/analysis/internal/checker/checker_test.go index fcf5f66e03e..9ec6e61cd73 100644 --- a/go/analysis/internal/checker/checker_test.go +++ b/go/analysis/internal/checker/checker_test.go @@ -49,8 +49,10 @@ func Foo() { t.Fatal(err) } path := filepath.Join(testdata, "src/rename/test.go") + checker.Fix = true checker.Run([]string{"file=" + path}, []*analysis.Analyzer{renameAnalyzer}) + checker.Fix = false contents, err := os.ReadFile(path) if err != nil { @@ -138,31 +140,33 @@ func NewT1() *T1 { return &T1{T} } // package from source. For the rest, it asks 'go list' for export data, // which fails because the compiler encounters the type error. Since the // errors come from 'go list', the driver doesn't run the analyzer. - {name: "despite-error", pattern: []string{rderrFile}, analyzers: []*analysis.Analyzer{noop}, code: 1}, + {name: "despite-error", pattern: []string{rderrFile}, analyzers: []*analysis.Analyzer{noop}, code: exitCodeFailed}, // The noopfact analyzer does use facts, so the driver loads source for // all dependencies, does type checking itself, recognizes the error as a // type error, and runs the analyzer. - {name: "despite-error-fact", pattern: []string{rderrFile}, analyzers: []*analysis.Analyzer{noopWithFact}, code: 1}, + {name: "despite-error-fact", pattern: []string{rderrFile}, analyzers: []*analysis.Analyzer{noopWithFact}, code: exitCodeFailed}, // combination of parse/type errors and no errors - {name: "despite-error-and-no-error", pattern: []string{rderrFile, "sort"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: 1}, + {name: "despite-error-and-no-error", pattern: []string{rderrFile, "sort"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: exitCodeFailed}, // non-existing package error - {name: "no-package", pattern: []string{"xyz"}, analyzers: []*analysis.Analyzer{renameAnalyzer}, code: 1}, - {name: "no-package-despite-error", pattern: []string{"abc"}, analyzers: []*analysis.Analyzer{noop}, code: 1}, - {name: "no-multi-package-despite-error", pattern: []string{"xyz", "abc"}, analyzers: []*analysis.Analyzer{noop}, code: 1}, + {name: "no-package", pattern: []string{"xyz"}, analyzers: []*analysis.Analyzer{renameAnalyzer}, code: exitCodeFailed}, + {name: "no-package-despite-error", pattern: []string{"abc"}, analyzers: []*analysis.Analyzer{noop}, code: exitCodeFailed}, + {name: "no-multi-package-despite-error", pattern: []string{"xyz", "abc"}, analyzers: []*analysis.Analyzer{noop}, code: exitCodeFailed}, // combination of type/parsing and different errors - {name: "different-errors", pattern: []string{rderrFile, "xyz"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: 1}, + {name: "different-errors", pattern: []string{rderrFile, "xyz"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: exitCodeFailed}, // non existing dir error - {name: "no-match-dir", pattern: []string{"file=non/existing/dir"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: 1}, + {name: "no-match-dir", pattern: []string{"file=non/existing/dir"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: exitCodeFailed}, // no errors - {name: "no-errors", pattern: []string{"sort"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: 0}, + {name: "no-errors", pattern: []string{"sort"}, analyzers: []*analysis.Analyzer{renameAnalyzer, noop}, code: exitCodeSuccess}, // duplicate list error with no findings - {name: "list-error", pattern: []string{cperrFile}, analyzers: []*analysis.Analyzer{noop}, code: 1}, + {name: "list-error", pattern: []string{cperrFile}, analyzers: []*analysis.Analyzer{noop}, code: exitCodeFailed}, // duplicate list errors with findings (issue #67790) - {name: "list-error-findings", pattern: []string{cperrFile}, analyzers: []*analysis.Analyzer{renameAnalyzer}, code: 3}, + {name: "list-error-findings", pattern: []string{cperrFile}, analyzers: []*analysis.Analyzer{renameAnalyzer}, code: exitCodeDiagnostics}, } { - if got := checker.Run(test.pattern, test.analyzers); got != test.code { - t.Errorf("got incorrect exit code %d for test %s; want %d", got, test.name, test.code) - } + t.Run(test.name, func(t *testing.T) { + if got := checker.Run(test.pattern, test.analyzers); got != test.code { + t.Errorf("got incorrect exit code %d for test %s; want %d", got, test.name, test.code) + } + }) } } diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index 8f4e7a3f6a9..68d965d08d6 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -52,9 +52,9 @@ func TestMain(m *testing.M) { } const ( - exitCodeSuccess = 0 // success (no diagnostics) + exitCodeSuccess = 0 // success (no diagnostics, or successful -fix) exitCodeFailed = 1 // analysis failed to run - exitCodeDiagnostics = 3 // diagnostics were reported + exitCodeDiagnostics = 3 // diagnostics were reported (and no -fix) ) // TestReportInvalidDiagnostic tests that a call to pass.Report with diff --git a/go/analysis/internal/checker/start_test.go b/go/analysis/internal/checker/start_test.go index 618ccd09b93..c78829a5adf 100644 --- a/go/analysis/internal/checker/start_test.go +++ b/go/analysis/internal/checker/start_test.go @@ -40,6 +40,7 @@ package comment path := filepath.Join(testdata, "src/comment/doc.go") checker.Fix = true checker.Run([]string{"file=" + path}, []*analysis.Analyzer{commentAnalyzer}) + checker.Fix = false contents, err := os.ReadFile(path) if err != nil { diff --git a/go/analysis/internal/checker/testdata/diff.txt b/go/analysis/internal/checker/testdata/diff.txt index 5a0c9c2a3b2..f11f01ad1e4 100644 --- a/go/analysis/internal/checker/testdata/diff.txt +++ b/go/analysis/internal/checker/testdata/diff.txt @@ -8,8 +8,7 @@ skip GOOS=windows checker -rename -fix -diff example.com/p -exit 3 -stderr renaming "bar" to "baz" +exit 0 -- go.mod -- module example.com diff --git a/go/analysis/internal/checker/testdata/fixes.txt b/go/analysis/internal/checker/testdata/fixes.txt index 89f245f9ace..4d906ca3f54 100644 --- a/go/analysis/internal/checker/testdata/fixes.txt +++ b/go/analysis/internal/checker/testdata/fixes.txt @@ -2,9 +2,9 @@ # particular when processing duplicate fixes for overlapping packages # in the same directory ("p", "p [p.test]", "p_test [p.test]"). -checker -rename -fix example.com/p -exit 3 -stderr renaming "bar" to "baz" +checker -rename -fix -v example.com/p +stderr applied 8 fixes, updated 3 files +exit 0 -- go.mod -- module example.com diff --git a/go/analysis/internal/checker/testdata/importdup.txt b/go/analysis/internal/checker/testdata/importdup.txt index e1783777858..4c144a61221 100644 --- a/go/analysis/internal/checker/testdata/importdup.txt +++ b/go/analysis/internal/checker/testdata/importdup.txt @@ -1,8 +1,9 @@ # Test that duplicate imports--and, more generally, duplicate # identical insertions--are coalesced. -checker -marker -fix example.com/a -exit 3 +checker -marker -fix -v example.com/a +stderr applied 2 fixes, updated 1 files +exit 0 -- go.mod -- module example.com diff --git a/go/analysis/internal/checker/testdata/importdup2.txt b/go/analysis/internal/checker/testdata/importdup2.txt index 118fdc0184b..c2da0f33195 100644 --- a/go/analysis/internal/checker/testdata/importdup2.txt +++ b/go/analysis/internal/checker/testdata/importdup2.txt @@ -19,8 +19,9 @@ # In more complex examples, the result # may be more subtly order-dependent. -checker -marker -fix example.com/a example.com/b -exit 3 +checker -marker -fix -v example.com/a example.com/b +stderr applied 6 fixes, updated 2 files +exit 0 -- go.mod -- module example.com diff --git a/go/analysis/internal/checker/testdata/noend.txt b/go/analysis/internal/checker/testdata/noend.txt index 2d6be074565..5ebc5e011ba 100644 --- a/go/analysis/internal/checker/testdata/noend.txt +++ b/go/analysis/internal/checker/testdata/noend.txt @@ -2,8 +2,7 @@ # interpreted as if equal to SuggestedFix.Pos (see issue #64199). checker -noend -fix example.com/a -exit 3 -stderr say hello +exit 0 -- go.mod -- module example.com diff --git a/go/analysis/internal/checker/testdata/overlap.txt b/go/analysis/internal/checker/testdata/overlap.txt index f556ef308b9..581f2e18950 100644 --- a/go/analysis/internal/checker/testdata/overlap.txt +++ b/go/analysis/internal/checker/testdata/overlap.txt @@ -15,9 +15,12 @@ # (This is a pretty unlikely situation, but it corresponds # to a historical test, TestOther, that used to check for # a conflict, and it seemed wrong to delete it without explanation.) +# +# The fixes are silently and successfully applied. -checker -rename -marker -fix example.com/a -exit 3 +checker -rename -marker -fix -v example.com/a +stderr applied 2 fixes, updated 1 file +exit 0 -- go.mod -- module example.com From f9aad7054b5ff7461d687469b3329b583093e72e Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Wed, 12 Feb 2025 10:43:03 -0500 Subject: [PATCH 138/309] go/types/typeutil: avoid shifting uintptr by 32 on 32-bit archs Shifting by 32 on an uintptr causes vet's check for shifts that equal or exceed the width of the integer to trigger on 32-bit architectures. For example, on 386: $ GOARCH=386 GOOS=linux go vet golang.org/x/tools/go/types/typeutil # golang.org/x/tools/go/types/typeutil go/types/typeutil/map.go:393:24: hash (32 bits) too small for shift of 32 Because this package is vendored into the main Go tree, and go test has a special case to turn on all vet checks there, the finding is promoted to an error (even if the code is otherwise harmless). Fix it. For golang/go#69407. For golang/go#36905. Change-Id: Ib8bf981bbe338db4ba8e9b7add0b373acae7338d Cq-Include-Trybots: luci.golang.try:x_tools-gotip-linux-386-longtest,x_tools-gotip-linux-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/tools/+/648895 LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Auto-Submit: Dmitri Shuralyov Reviewed-by: Alan Donovan --- go/types/typeutil/map.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/go/types/typeutil/map.go b/go/types/typeutil/map.go index 43261147c05..b6d542c64ee 100644 --- a/go/types/typeutil/map.go +++ b/go/types/typeutil/map.go @@ -389,8 +389,13 @@ func (hasher) hashTypeName(tname *types.TypeName) uint32 { // path, and whether or not it is a package-level typename. It // is rare for a package to define multiple local types with // the same name.) - hash := uintptr(unsafe.Pointer(tname)) - return uint32(hash ^ (hash >> 32)) + ptr := uintptr(unsafe.Pointer(tname)) + if unsafe.Sizeof(ptr) == 8 { + hash := uint64(ptr) + return uint32(hash ^ (hash >> 32)) + } else { + return uint32(ptr) + } } // shallowHash computes a hash of t without looking at any of its From 57629448da517f7c9b04e039d70bf2aa06d02ee6 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 11 Feb 2025 18:49:54 -0500 Subject: [PATCH 139/309] gopls/internal/analysis/gofix: check package visibility If the RHS of an inlinable constant is in a package that is not visible from the current package, do not report that it can be inlined. For golang/go#32816. Change-Id: Iff9e18f844aa898beb9f1f8df01142057b341c39 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648581 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 3 +++ gopls/internal/analysis/gofix/testdata/src/a/a.go | 5 +++++ gopls/internal/analysis/gofix/testdata/src/a/a.go.golden | 5 +++++ gopls/internal/analysis/gofix/testdata/src/a/internal/d.go | 5 +++++ gopls/internal/analysis/gofix/testdata/src/b/b.go | 2 ++ gopls/internal/analysis/gofix/testdata/src/b/b.go.golden | 2 ++ 6 files changed, 22 insertions(+) create mode 100644 gopls/internal/analysis/gofix/testdata/src/a/internal/d.go diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 101924366d6..8460286bbe3 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -261,6 +261,9 @@ func run(pass *analysis.Pass) (any, error) { // "B" means something different here than at the inlinable const's scope. continue } + } else if !analysisinternal.CanImport(pass.Pkg.Path(), fcon.RHSPkgPath) { + // If this package can't see the RHS's package, we can't inline. + continue } var ( importPrefix string diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index ae486746e5b..4f41b9a8c5d 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -1,5 +1,7 @@ package a +import "a/internal" + // Functions. func f() { @@ -75,6 +77,9 @@ const ( in8 = x ) +//go:fix inline +const D = internal.D // want D: `goFixInline const "a/internal".D` + func shadow() { var x int // shadows x at package scope diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index 7d75a598fb7..9e9cc25996f 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -1,5 +1,7 @@ package a +import "a/internal" + // Functions. func f() { @@ -75,6 +77,9 @@ const ( in8 = x ) +//go:fix inline +const D = internal.D // want D: `goFixInline const "a/internal".D` + func shadow() { var x int // shadows x at package scope diff --git a/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go b/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go new file mode 100644 index 00000000000..3211d7ae3cc --- /dev/null +++ b/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go @@ -0,0 +1,5 @@ +// According to the go toolchain's rule about internal packages, +// this package is visible to package a, but not package b. +package internal + +const D = 1 diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go b/gopls/internal/analysis/gofix/testdata/src/b/b.go index 4bf9f0dc650..74876738bea 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go @@ -28,3 +28,5 @@ func g() { _ = a _ = x } + +const d = a.D // nope: a.D refers to a constant in a package that is not visible here. diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index b26a05c3046..b3608d6793e 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -32,3 +32,5 @@ func g() { _ = a _ = x } + +const d = a.D // nope: a.D refers to a constant in a package that is not visible here. From 86f13a91fb506bb6aee3a8a398f8a639c5212425 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 11 Feb 2025 18:58:26 -0500 Subject: [PATCH 140/309] gopls/internal/analysis/gofix: rename local Trivial: rename a local from fcon (forwardable const) to incon (inlinable const) to match terminology. For golang/go#32816. Change-Id: I7d61f055c7057c30b240c076b8710f47f2bf86d1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648715 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 8460286bbe3..8ec31bd4736 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -220,15 +220,15 @@ func run(pass *analysis.Pass) (any, error) { case *ast.Ident: // If the identifier is a use of an inlinable constant, suggest inlining it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { - fcon, ok := inlinableConsts[con] + incon, ok := inlinableConsts[con] if !ok { var fact goFixInlineConstFact if pass.ImportObjectFact(con, &fact) { - fcon = &fact - inlinableConsts[con] = fcon + incon = &fact + inlinableConsts[con] = incon } } - if fcon == nil { + if incon == nil { continue // nope } @@ -248,20 +248,20 @@ func run(pass *analysis.Pass) (any, error) { // If the RHS is not in the current package, AddImport will handle // shadowing, so we only need to worry about when both expressions // are in the current package. - if pass.Pkg.Path() == fcon.RHSPkgPath { + if pass.Pkg.Path() == incon.RHSPkgPath { // fcon.rhsObj is the object referred to by B in the definition of A. scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(fcon.RHSName, n.Pos()) // what "B" means in n's scope + _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. - panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, fcon.RHSName)) + panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName)) } - if obj != fcon.rhsObj { + if obj != incon.rhsObj { // "B" means something different here than at the inlinable const's scope. continue } - } else if !analysisinternal.CanImport(pass.Pkg.Path(), fcon.RHSPkgPath) { + } else if !analysisinternal.CanImport(pass.Pkg.Path(), incon.RHSPkgPath) { // If this package can't see the RHS's package, we can't inline. continue } @@ -269,9 +269,9 @@ func run(pass *analysis.Pass) (any, error) { importPrefix string edits []analysis.TextEdit ) - if fcon.RHSPkgPath != pass.Pkg.Path() { + if incon.RHSPkgPath != pass.Pkg.Path() { _, importPrefix, edits = analysisinternal.AddImport( - pass.TypesInfo, curFile, fcon.RHSPkgName, fcon.RHSPkgPath, fcon.RHSName, n.Pos()) + pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) } var ( pos = n.Pos() @@ -287,7 +287,7 @@ func run(pass *analysis.Pass) (any, error) { edits = append(edits, analysis.TextEdit{ Pos: pos, End: end, - NewText: []byte(importPrefix + fcon.RHSName), + NewText: []byte(importPrefix + incon.RHSName), }) pass.Report(analysis.Diagnostic{ Pos: pos, From 2f1b076c4ab654a507fef278b9a1d4a6bae56f04 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 12 Feb 2025 17:24:58 -0500 Subject: [PATCH 141/309] x/tools: add //go:fix inline This CL adds //go:fix inline annotations to some deprecated functions that may be inlined. Updates golang/go#32816 Change-Id: I2e8e82bee054721f266506af24ea39cf2e8b7983 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649056 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- go/ast/astutil/util.go | 2 ++ go/packages/packages.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/go/ast/astutil/util.go b/go/ast/astutil/util.go index ca71e3e1055..c820b208499 100644 --- a/go/ast/astutil/util.go +++ b/go/ast/astutil/util.go @@ -8,4 +8,6 @@ import "go/ast" // Unparen returns e with any enclosing parentheses stripped. // Deprecated: use [ast.Unparen]. +// +//go:fix inline func Unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } diff --git a/go/packages/packages.go b/go/packages/packages.go index c3a59b8ebf4..342f019a0f9 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -141,6 +141,8 @@ const ( LoadAllSyntax = LoadSyntax | NeedDeps // Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile. + // + //go:fix inline NeedExportsFile = NeedExportFile ) From d0d86e40a80dcab58f5cd2fa5f81e650d0777817 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 12 Feb 2025 17:26:02 -0500 Subject: [PATCH 142/309] x/tools: run gopls/internal/analysis/gofix/main.go -fix This inlines calls to a number of deprecated functions in both std and x/tools. Updates golang/go#32816 Change-Id: Id7f89983b1428fd3c042947dbecf07349f0bc134 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649057 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- go/analysis/checker/checker.go | 2 +- go/analysis/passes/copylock/copylock.go | 2 +- go/analysis/passes/unusedresult/unusedresult.go | 4 +--- go/analysis/validate.go | 2 +- go/ast/astutil/rewrite.go | 2 +- go/callgraph/vta/propagation_test.go | 2 +- go/internal/gccgoimporter/parser.go | 4 ++-- go/ssa/interp/reflect.go | 4 ++-- go/ssa/util.go | 2 +- go/ssa/wrappers.go | 6 ++---- go/types/objectpath/objectpath_test.go | 2 +- internal/astutil/clone.go | 2 +- internal/gcimporter/ureader_yes.go | 2 +- internal/refactor/inline/inline.go | 2 +- internal/refactor/inline/inline_test.go | 2 +- internal/tool/tool.go | 4 ++-- refactor/eg/match.go | 4 +--- refactor/eg/rewrite.go | 2 +- refactor/rename/util.go | 4 +--- 19 files changed, 23 insertions(+), 31 deletions(-) diff --git a/go/analysis/checker/checker.go b/go/analysis/checker/checker.go index 502ec922179..94808733b9d 100644 --- a/go/analysis/checker/checker.go +++ b/go/analysis/checker/checker.go @@ -594,7 +594,7 @@ func (act *Action) exportPackageFact(fact analysis.Fact) { func factType(fact analysis.Fact) reflect.Type { t := reflect.TypeOf(fact) - if t.Kind() != reflect.Ptr { + if t.Kind() != reflect.Pointer { log.Fatalf("invalid Fact type: got %T, want pointer", fact) } return t diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go index a9f02ac62e6..8a215677165 100644 --- a/go/analysis/passes/copylock/copylock.go +++ b/go/analysis/passes/copylock/copylock.go @@ -378,7 +378,7 @@ var lockerType *types.Interface // Construct a sync.Locker interface type. func init() { - nullary := types.NewSignature(nil, nil, nil, false) // func() + nullary := types.NewSignatureType(nil, nil, nil, nil, nil, false) // func() methods := []*types.Func{ types.NewFunc(token.NoPos, nil, "Lock", nullary), types.NewFunc(token.NoPos, nil, "Unlock", nullary), diff --git a/go/analysis/passes/unusedresult/unusedresult.go b/go/analysis/passes/unusedresult/unusedresult.go index d7cc1e6ae2c..e298f644277 100644 --- a/go/analysis/passes/unusedresult/unusedresult.go +++ b/go/analysis/passes/unusedresult/unusedresult.go @@ -130,9 +130,7 @@ func run(pass *analysis.Pass) (interface{}, error) { } // func() string -var sigNoArgsStringResult = types.NewSignature(nil, nil, - types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])), - false) +var sigNoArgsStringResult = types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])), false) type stringSetFlag map[string]bool diff --git a/go/analysis/validate.go b/go/analysis/validate.go index 4f2c4045622..14539392116 100644 --- a/go/analysis/validate.go +++ b/go/analysis/validate.go @@ -63,7 +63,7 @@ func Validate(analyzers []*Analyzer) error { return fmt.Errorf("fact type %s registered by two analyzers: %v, %v", t, a, prev) } - if t.Kind() != reflect.Ptr { + if t.Kind() != reflect.Pointer { return fmt.Errorf("%s: fact type %s is not a pointer", a, t) } factTypes[t] = a diff --git a/go/ast/astutil/rewrite.go b/go/ast/astutil/rewrite.go index 58934f76633..5c8dbbb7a35 100644 --- a/go/ast/astutil/rewrite.go +++ b/go/ast/astutil/rewrite.go @@ -183,7 +183,7 @@ type application struct { func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.Node) { // convert typed nil into untyped nil - if v := reflect.ValueOf(n); v.Kind() == reflect.Ptr && v.IsNil() { + if v := reflect.ValueOf(n); v.Kind() == reflect.Pointer && v.IsNil() { n = nil } diff --git a/go/callgraph/vta/propagation_test.go b/go/callgraph/vta/propagation_test.go index 492258f81e3..3885ef201cb 100644 --- a/go/callgraph/vta/propagation_test.go +++ b/go/callgraph/vta/propagation_test.go @@ -203,7 +203,7 @@ func testSuite() map[string]*vtaGraph { a := newNamedType("A") b := newNamedType("B") c := newNamedType("C") - sig := types.NewSignature(nil, types.NewTuple(), types.NewTuple(), false) + sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false) f1 := &ssa.Function{Signature: sig} setName(f1, "F1") diff --git a/go/internal/gccgoimporter/parser.go b/go/internal/gccgoimporter/parser.go index f315ec41004..f70946edbe4 100644 --- a/go/internal/gccgoimporter/parser.go +++ b/go/internal/gccgoimporter/parser.go @@ -619,7 +619,7 @@ func (p *parser) parseNamedType(nlist []interface{}) types.Type { p.skipInlineBody() p.expectEOL() - sig := types.NewSignature(receiver, params, results, isVariadic) + sig := types.NewSignatureType(receiver, nil, nil, params, results, isVariadic) nt.AddMethod(types.NewFunc(token.NoPos, pkg, name, sig)) } } @@ -800,7 +800,7 @@ func (p *parser) parseFunctionType(pkg *types.Package, nlist []interface{}) *typ params, isVariadic := p.parseParamList(pkg) results := p.parseResultList(pkg) - *t = *types.NewSignature(nil, params, results, isVariadic) + *t = *types.NewSignatureType(nil, nil, nil, params, results, isVariadic) return t } diff --git a/go/ssa/interp/reflect.go b/go/ssa/interp/reflect.go index 8259e56d860..22f8cde89c0 100644 --- a/go/ssa/interp/reflect.go +++ b/go/ssa/interp/reflect.go @@ -231,7 +231,7 @@ func reflectKind(t types.Type) reflect.Kind { case *types.Map: return reflect.Map case *types.Pointer: - return reflect.Ptr + return reflect.Pointer case *types.Slice: return reflect.Slice case *types.Struct: @@ -510,7 +510,7 @@ func newMethod(pkg *ssa.Package, recvType types.Type, name string) *ssa.Function // that is needed is the "pointerness" of Recv.Type, and for // now, we'll set it to always be false since we're only // concerned with rtype. Encapsulate this better. - sig := types.NewSignature(types.NewParam(token.NoPos, nil, "recv", recvType), nil, nil, false) + sig := types.NewSignatureType(types.NewParam(token.NoPos, nil, "recv", recvType), nil, nil, nil, nil, false) fn := pkg.Prog.NewFunction(name, sig, "fake reflect method") fn.Pkg = pkg return fn diff --git a/go/ssa/util.go b/go/ssa/util.go index 4a056cbe0bd..56638129602 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -195,7 +195,7 @@ func makeLen(T types.Type) *Builtin { lenParams := types.NewTuple(anonVar(T)) return &Builtin{ name: "len", - sig: types.NewSignature(nil, lenParams, lenResults, false), + sig: types.NewSignatureType(nil, nil, nil, lenParams, lenResults, false), } } diff --git a/go/ssa/wrappers.go b/go/ssa/wrappers.go index d09b4f250ee..aeb160eff23 100644 --- a/go/ssa/wrappers.go +++ b/go/ssa/wrappers.go @@ -106,9 +106,7 @@ func (b *builder) buildWrapper(fn *Function) { var c Call c.Call.Value = &Builtin{ name: "ssa:wrapnilchk", - sig: types.NewSignature(nil, - types.NewTuple(anonVar(fn.method.recv), anonVar(tString), anonVar(tString)), - types.NewTuple(anonVar(fn.method.recv)), false), + sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(anonVar(fn.method.recv), anonVar(tString), anonVar(tString)), types.NewTuple(anonVar(fn.method.recv)), false), } c.Call.Args = []Value{ v, @@ -262,7 +260,7 @@ func createThunk(prog *Program, sel *selection) *Function { } func changeRecv(s *types.Signature, recv *types.Var) *types.Signature { - return types.NewSignature(recv, s.Params(), s.Results(), s.Variadic()) + return types.NewSignatureType(recv, nil, nil, s.Params(), s.Results(), s.Variadic()) } // A local version of *types.Selection. diff --git a/go/types/objectpath/objectpath_test.go b/go/types/objectpath/objectpath_test.go index 0805c9d919a..642d6da4926 100644 --- a/go/types/objectpath/objectpath_test.go +++ b/go/types/objectpath/objectpath_test.go @@ -308,7 +308,7 @@ func (unreachable) F() {} // not reachable in export data if err != nil { t.Fatal(err) } - conf := types.Config{Importer: importer.For("source", nil)} + conf := types.Config{Importer: importer.ForCompiler(token.NewFileSet(), "source", nil)} info := &types.Info{ Defs: make(map[*ast.Ident]types.Object), } diff --git a/internal/astutil/clone.go b/internal/astutil/clone.go index d5ee82c58b2..2c9b6bb4841 100644 --- a/internal/astutil/clone.go +++ b/internal/astutil/clone.go @@ -25,7 +25,7 @@ func cloneNode(n ast.Node) ast.Node { } clone = func(x reflect.Value) reflect.Value { switch x.Kind() { - case reflect.Ptr: + case reflect.Pointer: if x.IsNil() { return x } diff --git a/internal/gcimporter/ureader_yes.go b/internal/gcimporter/ureader_yes.go index 522287d18d6..37b4a39e9e1 100644 --- a/internal/gcimporter/ureader_yes.go +++ b/internal/gcimporter/ureader_yes.go @@ -574,7 +574,7 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) { recv := types.NewVar(fn.Pos(), fn.Pkg(), "", named) typesinternal.SetVarKind(recv, typesinternal.RecvVar) - methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignature(recv, sig.Params(), sig.Results(), sig.Variadic())) + methods[i] = types.NewFunc(fn.Pos(), fn.Pkg(), fn.Name(), types.NewSignatureType(recv, nil, nil, sig.Params(), sig.Results(), sig.Variadic())) } embeds := make([]types.Type, iface.NumEmbeddeds()) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 96fbb8f8706..54308243e1c 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -2981,7 +2981,7 @@ func replaceNode(root ast.Node, from, to ast.Node) { var visit func(reflect.Value) visit = func(v reflect.Value) { switch v.Kind() { - case reflect.Ptr: + case reflect.Pointer: if v.Interface() == from { found = true diff --git a/internal/refactor/inline/inline_test.go b/internal/refactor/inline/inline_test.go index 03fb5ccdb17..3be37d5ecde 100644 --- a/internal/refactor/inline/inline_test.go +++ b/internal/refactor/inline/inline_test.go @@ -1977,7 +1977,7 @@ func deepHash(n ast.Node) any { var visit func(reflect.Value) visit = func(v reflect.Value) { switch v.Kind() { - case reflect.Ptr: + case reflect.Pointer: ptr := v.UnsafePointer() writeUint64(uint64(uintptr(ptr))) if !v.IsNil() { diff --git a/internal/tool/tool.go b/internal/tool/tool.go index 46f5b87fa35..fe2b1c289b8 100644 --- a/internal/tool/tool.go +++ b/internal/tool/tool.go @@ -250,7 +250,7 @@ func addFlags(f *flag.FlagSet, field reflect.StructField, value reflect.Value) * child := value.Type().Field(i) v := value.Field(i) // make sure we have a pointer - if v.Kind() != reflect.Ptr { + if v.Kind() != reflect.Pointer { v = v.Addr() } // check if that field is a flag or contains flags @@ -289,7 +289,7 @@ func addFlag(f *flag.FlagSet, value reflect.Value, flagName string, help string) func resolve(v reflect.Value) reflect.Value { for { switch v.Kind() { - case reflect.Interface, reflect.Ptr: + case reflect.Interface, reflect.Pointer: v = v.Elem() default: return v diff --git a/refactor/eg/match.go b/refactor/eg/match.go index 31f8af28f23..0a109210bc4 100644 --- a/refactor/eg/match.go +++ b/refactor/eg/match.go @@ -13,8 +13,6 @@ import ( "log" "os" "reflect" - - "golang.org/x/tools/go/ast/astutil" ) // matchExpr reports whether pattern x matches y. @@ -229,7 +227,7 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool { // -- utilities -------------------------------------------------------- -func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } +func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } // isRef returns the object referred to by this (possibly qualified) // identifier, or nil if the node is not a referring identifier. diff --git a/refactor/eg/rewrite.go b/refactor/eg/rewrite.go index 3f71c53b7bb..6fb1e44ef30 100644 --- a/refactor/eg/rewrite.go +++ b/refactor/eg/rewrite.go @@ -338,7 +338,7 @@ func (tr *Transformer) subst(env map[string]ast.Expr, pattern, pos reflect.Value } return v - case reflect.Ptr: + case reflect.Pointer: v := reflect.New(p.Type()).Elem() if elem := p.Elem(); elem.IsValid() { v.Set(tr.subst(env, elem, pos).Addr()) diff --git a/refactor/rename/util.go b/refactor/rename/util.go index 7c1a634e4ed..a3d998f90e0 100644 --- a/refactor/rename/util.go +++ b/refactor/rename/util.go @@ -14,8 +14,6 @@ import ( "runtime" "strings" "unicode" - - "golang.org/x/tools/go/ast/astutil" ) func objectKind(obj types.Object) string { @@ -93,7 +91,7 @@ func sameFile(x, y string) bool { return false } -func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } +func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } func is[T any](x any) bool { _, ok := x.(T) From 44b61a1d174cc06329b20f5de60c2b0c800741a4 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 12 Feb 2025 17:33:27 -0500 Subject: [PATCH 143/309] x/tools: eliminate various unparen (et al) helpers This was achieved by annotiating them with //go:fix inline, running gopls/internal/analysis/gofix/main.go -fix ./... then deleting them. Update golang/go#32816 Change-Id: If65dbf8bfcad796ef274d80804daa135e8ccabf9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/648976 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan --- go/ssa/builder.go | 20 +++++++-------- go/ssa/emit.go | 2 +- go/ssa/source.go | 2 +- go/ssa/util.go | 2 -- gopls/internal/golang/extract.go | 2 +- gopls/internal/golang/freesymbols.go | 2 +- refactor/eg/match.go | 6 ++--- refactor/rename/spec.go | 4 +-- refactor/rename/util.go | 3 --- refactor/satisfy/find.go | 38 ++++++++++++---------------- 10 files changed, 34 insertions(+), 47 deletions(-) diff --git a/go/ssa/builder.go b/go/ssa/builder.go index 4cd71260b61..1761dcc3068 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -559,7 +559,7 @@ func (sb *storebuf) emit(fn *Function) { // literal that may reference parts of the LHS. func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *storebuf) { // Can we initialize it in place? - if e, ok := unparen(e).(*ast.CompositeLit); ok { + if e, ok := ast.Unparen(e).(*ast.CompositeLit); ok { // A CompositeLit never evaluates to a pointer, // so if the type of the location is a pointer, // an &-operation is implied. @@ -614,7 +614,7 @@ func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb * // expr lowers a single-result expression e to SSA form, emitting code // to fn and returning the Value defined by the expression. func (b *builder) expr(fn *Function, e ast.Expr) Value { - e = unparen(e) + e = ast.Unparen(e) tv := fn.info.Types[e] @@ -704,7 +704,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value { return y } // Call to "intrinsic" built-ins, e.g. new, make, panic. - if id, ok := unparen(e.Fun).(*ast.Ident); ok { + if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok { if obj, ok := fn.info.Uses[id].(*types.Builtin); ok { if v := b.builtin(fn, obj, e.Args, fn.typ(tv.Type), e.Lparen); v != nil { return v @@ -721,7 +721,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value { switch e.Op { case token.AND: // &X --- potentially escaping. addr := b.addr(fn, e.X, true) - if _, ok := unparen(e.X).(*ast.StarExpr); ok { + if _, ok := ast.Unparen(e.X).(*ast.StarExpr); ok { // &*p must panic if p is nil (http://golang.org/s/go12nil). // For simplicity, we'll just (suboptimally) rely // on the side effects of a load. @@ -1002,7 +1002,7 @@ func (b *builder) setCallFunc(fn *Function, e *ast.CallExpr, c *CallCommon) { c.pos = e.Lparen // Is this a method call? - if selector, ok := unparen(e.Fun).(*ast.SelectorExpr); ok { + if selector, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok { sel := fn.selection(selector) if sel != nil && sel.kind == types.MethodVal { obj := sel.obj.(*types.Func) @@ -1372,7 +1372,7 @@ func (b *builder) compLit(fn *Function, addr Value, e *ast.CompositeLit, isZero // An &-operation may be implied: // map[*struct{}]bool{&struct{}{}: true} wantAddr := false - if _, ok := unparen(e.Key).(*ast.CompositeLit); ok { + if _, ok := ast.Unparen(e.Key).(*ast.CompositeLit); ok { wantAddr = isPointerCore(t.Key()) } @@ -1547,9 +1547,9 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl var x Value switch ass := s.Assign.(type) { case *ast.ExprStmt: // x.(type) - x = b.expr(fn, unparen(ass.X).(*ast.TypeAssertExpr).X) + x = b.expr(fn, ast.Unparen(ass.X).(*ast.TypeAssertExpr).X) case *ast.AssignStmt: // y := x.(type) - x = b.expr(fn, unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) + x = b.expr(fn, ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) } done := fn.newBasicBlock("typeswitch.done") @@ -1667,7 +1667,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) { } case *ast.AssignStmt: // x := <-ch - recv := unparen(comm.Rhs[0]).(*ast.UnaryExpr) + recv := ast.Unparen(comm.Rhs[0]).(*ast.UnaryExpr) st = &SelectState{ Dir: types.RecvOnly, Chan: b.expr(fn, recv.X), @@ -1678,7 +1678,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) { } case *ast.ExprStmt: // <-ch - recv := unparen(comm.X).(*ast.UnaryExpr) + recv := ast.Unparen(comm.X).(*ast.UnaryExpr) st = &SelectState{ Dir: types.RecvOnly, Chan: b.expr(fn, recv.X), diff --git a/go/ssa/emit.go b/go/ssa/emit.go index a3d41ad95a4..bca79adc4e1 100644 --- a/go/ssa/emit.go +++ b/go/ssa/emit.go @@ -81,7 +81,7 @@ func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) { panic("nil") } var obj types.Object - e = unparen(e) + e = ast.Unparen(e) if id, ok := e.(*ast.Ident); ok { if isBlankIdent(id) { return diff --git a/go/ssa/source.go b/go/ssa/source.go index 055a6b1ef5f..d0cc1f4861a 100644 --- a/go/ssa/source.go +++ b/go/ssa/source.go @@ -153,7 +153,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function { // the ssa.Value.) func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) { if f.debugInfo() { // (opt) - e = unparen(e) + e = ast.Unparen(e) for _, b := range f.Blocks { for _, instr := range b.Instrs { if ref, ok := instr.(*DebugRef); ok { diff --git a/go/ssa/util.go b/go/ssa/util.go index 56638129602..2a9c9b9d318 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -35,8 +35,6 @@ func assert(p bool, msg string) { //// AST utilities -func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } - // isBlankIdent returns true iff e is an Ident with name "_". // They have no associated types.Object, and thus no type. func isBlankIdent(e ast.Expr) bool { diff --git a/gopls/internal/golang/extract.go b/gopls/internal/golang/extract.go index 2ce89795a06..8c8758d9f0a 100644 --- a/gopls/internal/golang/extract.go +++ b/gopls/internal/golang/extract.go @@ -1229,7 +1229,7 @@ func collectFreeVars(info *types.Info, file *ast.File, start, end token.Pos, nod // return value acts as an indicator for where it was defined. var sel func(n *ast.SelectorExpr) (types.Object, bool) sel = func(n *ast.SelectorExpr) (types.Object, bool) { - switch x := astutil.Unparen(n.X).(type) { + switch x := ast.Unparen(n.X).(type) { case *ast.SelectorExpr: return sel(x) case *ast.Ident: diff --git a/gopls/internal/golang/freesymbols.go b/gopls/internal/golang/freesymbols.go index 2c9e25165f6..336025367f5 100644 --- a/gopls/internal/golang/freesymbols.go +++ b/gopls/internal/golang/freesymbols.go @@ -342,7 +342,7 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t for { suffix = append(suffix, info.Uses[sel.Sel]) - switch x := astutil.Unparen(sel.X).(type) { + switch x := ast.Unparen(sel.X).(type) { case *ast.Ident: return id(x, suffix) default: diff --git a/refactor/eg/match.go b/refactor/eg/match.go index 0a109210bc4..d85a473b978 100644 --- a/refactor/eg/match.go +++ b/refactor/eg/match.go @@ -32,8 +32,8 @@ func (tr *Transformer) matchExpr(x, y ast.Expr) bool { if x == nil || y == nil { return false } - x = unparen(x) - y = unparen(y) + x = ast.Unparen(x) + y = ast.Unparen(y) // Is x a wildcard? (a reference to a 'before' parameter) if xobj, ok := tr.wildcardObj(x); ok { @@ -227,8 +227,6 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool { // -- utilities -------------------------------------------------------- -func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } - // isRef returns the object referred to by this (possibly qualified) // identifier, or nil if the node is not a referring identifier. func isRef(n ast.Node, info *types.Info) types.Object { diff --git a/refactor/rename/spec.go b/refactor/rename/spec.go index 1d8c32c9dc3..99068c13358 100644 --- a/refactor/rename/spec.go +++ b/refactor/rename/spec.go @@ -155,7 +155,7 @@ func parseObjectSpec(spec *spec, main string) error { } if e, ok := e.(*ast.SelectorExpr); ok { - x := unparen(e.X) + x := ast.Unparen(e.X) // Strip off star constructor, if any. if star, ok := x.(*ast.StarExpr); ok { @@ -172,7 +172,7 @@ func parseObjectSpec(spec *spec, main string) error { if x, ok := x.(*ast.SelectorExpr); ok { // field/method of type e.g. ("encoding/json".Decoder).Decode - y := unparen(x.X) + y := ast.Unparen(x.X) if pkg := parseImportPath(y); pkg != "" { spec.pkg = pkg // e.g. "encoding/json" spec.pkgMember = x.Sel.Name // e.g. "Decoder" diff --git a/refactor/rename/util.go b/refactor/rename/util.go index a3d998f90e0..cb7cea3a86e 100644 --- a/refactor/rename/util.go +++ b/refactor/rename/util.go @@ -5,7 +5,6 @@ package rename import ( - "go/ast" "go/token" "go/types" "os" @@ -91,8 +90,6 @@ func sameFile(x, y string) bool { return false } -func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } - func is[T any](x any) bool { _, ok := x.(T) return ok diff --git a/refactor/satisfy/find.go b/refactor/satisfy/find.go index 3d693aa04ab..a897c3c2fd4 100644 --- a/refactor/satisfy/find.go +++ b/refactor/satisfy/find.go @@ -126,13 +126,13 @@ func (f *Finder) exprN(e ast.Expr) types.Type { case *ast.CallExpr: // x, err := f(args) - sig := coreType(f.expr(e.Fun)).(*types.Signature) + sig := typeparams.CoreType(f.expr(e.Fun)).(*types.Signature) f.call(sig, e.Args) case *ast.IndexExpr: // y, ok := x[i] x := f.expr(e.X) - f.assign(f.expr(e.Index), coreType(x).(*types.Map).Key()) + f.assign(f.expr(e.Index), typeparams.CoreType(x).(*types.Map).Key()) case *ast.TypeAssertExpr: // y, ok := x.(T) @@ -213,7 +213,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex f.expr(args[1]) } else { // append(x, y, z) - tElem := coreType(s).(*types.Slice).Elem() + tElem := typeparams.CoreType(s).(*types.Slice).Elem() for _, arg := range args[1:] { f.assign(tElem, f.expr(arg)) } @@ -222,7 +222,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex case "delete": m := f.expr(args[0]) k := f.expr(args[1]) - f.assign(coreType(m).(*types.Map).Key(), k) + f.assign(typeparams.CoreType(m).(*types.Map).Key(), k) default: // ordinary call @@ -273,7 +273,7 @@ func (f *Finder) assign(lhs, rhs types.Type) { if types.Identical(lhs, rhs) { return } - if !isInterface(lhs) { + if !types.IsInterface(lhs) { return } @@ -354,7 +354,7 @@ func (f *Finder) expr(e ast.Expr) types.Type { f.sig = saved case *ast.CompositeLit: - switch T := coreType(typeparams.Deref(tv.Type)).(type) { + switch T := typeparams.CoreType(typeparams.Deref(tv.Type)).(type) { case *types.Struct: for i, elem := range e.Elts { if kv, ok := elem.(*ast.KeyValueExpr); ok { @@ -405,7 +405,7 @@ func (f *Finder) expr(e ast.Expr) types.Type { // x[i] or m[k] -- index or lookup operation x := f.expr(e.X) i := f.expr(e.Index) - if ux, ok := coreType(x).(*types.Map); ok { + if ux, ok := typeparams.CoreType(x).(*types.Map); ok { f.assign(ux.Key(), i) } } @@ -440,7 +440,7 @@ func (f *Finder) expr(e ast.Expr) types.Type { // unsafe call. Treat calls to functions in unsafe like ordinary calls, // except that their signature cannot be determined by their func obj. // Without this special handling, f.expr(e.Fun) would fail below. - if s, ok := unparen(e.Fun).(*ast.SelectorExpr); ok { + if s, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok { if obj, ok := f.info.Uses[s.Sel].(*types.Builtin); ok && obj.Pkg().Path() == "unsafe" { sig := f.info.Types[e.Fun].Type.(*types.Signature) f.call(sig, e.Args) @@ -449,7 +449,7 @@ func (f *Finder) expr(e ast.Expr) types.Type { } // builtin call - if id, ok := unparen(e.Fun).(*ast.Ident); ok { + if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok { if obj, ok := f.info.Uses[id].(*types.Builtin); ok { sig := f.info.Types[id].Type.(*types.Signature) f.builtin(obj, sig, e.Args) @@ -458,7 +458,7 @@ func (f *Finder) expr(e ast.Expr) types.Type { } // ordinary call - f.call(coreType(f.expr(e.Fun)).(*types.Signature), e.Args) + f.call(typeparams.CoreType(f.expr(e.Fun)).(*types.Signature), e.Args) } case *ast.StarExpr: @@ -518,7 +518,7 @@ func (f *Finder) stmt(s ast.Stmt) { case *ast.SendStmt: ch := f.expr(s.Chan) val := f.expr(s.Value) - f.assign(coreType(ch).(*types.Chan).Elem(), val) + f.assign(typeparams.CoreType(ch).(*types.Chan).Elem(), val) case *ast.IncDecStmt: f.expr(s.X) @@ -622,9 +622,9 @@ func (f *Finder) stmt(s ast.Stmt) { var I types.Type switch ass := s.Assign.(type) { case *ast.ExprStmt: // x.(type) - I = f.expr(unparen(ass.X).(*ast.TypeAssertExpr).X) + I = f.expr(ast.Unparen(ass.X).(*ast.TypeAssertExpr).X) case *ast.AssignStmt: // y := x.(type) - I = f.expr(unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) + I = f.expr(ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) } for _, cc := range s.Body.List { cc := cc.(*ast.CaseClause) @@ -668,7 +668,7 @@ func (f *Finder) stmt(s ast.Stmt) { var xelem types.Type // Keys of array, *array, slice, string aren't interesting // since the RHS key type is just an int. - switch ux := coreType(x).(type) { + switch ux := typeparams.CoreType(x).(type) { case *types.Chan: xelem = ux.Elem() case *types.Map: @@ -683,13 +683,13 @@ func (f *Finder) stmt(s ast.Stmt) { var xelem types.Type // Values of type strings aren't interesting because // the RHS value type is just a rune. - switch ux := coreType(x).(type) { + switch ux := typeparams.CoreType(x).(type) { case *types.Array: xelem = ux.Elem() case *types.Map: xelem = ux.Elem() case *types.Pointer: // *array - xelem = coreType(typeparams.Deref(ux)).(*types.Array).Elem() + xelem = typeparams.CoreType(typeparams.Deref(ux)).(*types.Array).Elem() case *types.Slice: xelem = ux.Elem() } @@ -707,12 +707,6 @@ func (f *Finder) stmt(s ast.Stmt) { // -- Plundered from golang.org/x/tools/go/ssa ----------------- -func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } - -func isInterface(T types.Type) bool { return types.IsInterface(T) } - -func coreType(T types.Type) types.Type { return typeparams.CoreType(T) } - func instance(info *types.Info, expr ast.Expr) bool { var id *ast.Ident switch x := expr.(type) { From ddd4bde5a0f514414daf47ff0232b601ba01d18f Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 13 Feb 2025 18:02:22 +0000 Subject: [PATCH 144/309] gopls/internal/golang: avoid PackageSymbols errors with missing packages As reported in golang/vscode-go#3681, spurious errors from gopls.package_symbols can cause very distracting popups in VS Code. For now, err on the side of silence. In the future, we may want to revisit this behavior. For golang/vscode-go#3681 Change-Id: I67f8b8e1e299ef88dabbb284a151aada131652f8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649257 Reviewed-by: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/symbols.go | 32 +++--- gopls/internal/server/command.go | 4 + .../integration/misc/package_symbols_test.go | 101 ++++++++++-------- 3 files changed, 80 insertions(+), 57 deletions(-) diff --git a/gopls/internal/golang/symbols.go b/gopls/internal/golang/symbols.go index 14f2703441c..db31baa69f2 100644 --- a/gopls/internal/golang/symbols.go +++ b/gopls/internal/golang/symbols.go @@ -86,17 +86,22 @@ func PackageSymbols(ctx context.Context, snapshot *cache.Snapshot, uri protocol. ctx, done := event.Start(ctx, "source.PackageSymbols") defer done() - mp, err := NarrowestMetadataForFile(ctx, snapshot, uri) - if err != nil { - return command.PackageSymbolsResult{}, err + pkgFiles := []protocol.DocumentURI{uri} + + // golang/vscode-go#3681: do our best if the file is not in a package. + // TODO(rfindley): revisit this in the future once there is more graceful + // handling in VS Code. + if mp, err := NarrowestMetadataForFile(ctx, snapshot, uri); err == nil { + pkgFiles = mp.CompiledGoFiles } - pkgfiles := mp.CompiledGoFiles - // Maps receiver name to the methods that use it - receiverToMethods := make(map[string][]command.PackageSymbol) - // Maps type symbol name to its index in symbols - typeSymbolToIdx := make(map[string]int) - var symbols []command.PackageSymbol - for fidx, f := range pkgfiles { + + var ( + pkgName string + symbols []command.PackageSymbol + receiverToMethods = make(map[string][]command.PackageSymbol) // receiver name -> methods + typeSymbolToIdx = make(map[string]int) // type name -> index in symbols + ) + for fidx, f := range pkgFiles { fh, err := snapshot.ReadFile(ctx, f) if err != nil { return command.PackageSymbolsResult{}, err @@ -105,6 +110,9 @@ func PackageSymbols(ctx context.Context, snapshot *cache.Snapshot, uri protocol. if err != nil { return command.PackageSymbolsResult{}, err } + if pkgName == "" && pgf.File != nil && pgf.File.Name != nil { + pkgName = pgf.File.Name.Name + } for _, decl := range pgf.File.Decls { switch decl := decl.(type) { case *ast.FuncDecl: @@ -154,8 +162,8 @@ func PackageSymbols(ctx context.Context, snapshot *cache.Snapshot, uri protocol. } } return command.PackageSymbolsResult{ - PackageName: string(mp.Name), - Files: pkgfiles, + PackageName: pkgName, + Files: pkgFiles, Symbols: symbols, }, nil diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 2b5c282a28f..007b8d5218f 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -1741,6 +1741,10 @@ func (c *commandHandler) PackageSymbols(ctx context.Context, args command.Packag err := c.run(ctx, commandConfig{ forURI: args.URI, }, func(ctx context.Context, deps commandDeps) error { + if deps.snapshot.FileKind(deps.fh) != file.Go { + // golang/vscode-go#3681: fail silently, to avoid spurious error popups. + return nil + } res, err := golang.PackageSymbols(ctx, deps.snapshot, args.URI) if err != nil { return err diff --git a/gopls/internal/test/integration/misc/package_symbols_test.go b/gopls/internal/test/integration/misc/package_symbols_test.go index 860264f2bb0..1e06a655935 100644 --- a/gopls/internal/test/integration/misc/package_symbols_test.go +++ b/gopls/internal/test/integration/misc/package_symbols_test.go @@ -16,6 +16,11 @@ import ( func TestPackageSymbols(t *testing.T) { const files = ` +-- go.mod -- +module example.com + +go 1.20 + -- a.go -- package a @@ -33,68 +38,74 @@ func (s *S) M2() {} func (s *S) M3() {} func F() {} +-- unloaded.go -- +//go:build unloaded + +package a + +var Unloaded int ` integration.Run(t, files, func(t *testing.T, env *integration.Env) { - a_uri := env.Sandbox.Workdir.URI("a.go") - b_uri := env.Sandbox.Workdir.URI("b.go") + aURI := env.Sandbox.Workdir.URI("a.go") + bURI := env.Sandbox.Workdir.URI("b.go") args, err := command.MarshalArgs(command.PackageSymbolsArgs{ - URI: a_uri, + URI: aURI, }) if err != nil { - t.Fatalf("failed to MarshalArgs: %v", err) + t.Fatal(err) } var res command.PackageSymbolsResult env.ExecuteCommand(&protocol.ExecuteCommandParams{ - Command: "gopls.package_symbols", + Command: command.PackageSymbols.String(), Arguments: args, }, &res) want := command.PackageSymbolsResult{ PackageName: "a", - Files: []protocol.DocumentURI{a_uri, b_uri}, + Files: []protocol.DocumentURI{aURI, bURI}, Symbols: []command.PackageSymbol{ - { - Name: "A", - Kind: protocol.Variable, - File: 0, - }, - { - Name: "F", - Kind: protocol.Function, - File: 1, - }, - { - Name: "S", - Kind: protocol.Struct, - File: 0, - Children: []command.PackageSymbol{ - { - Name: "M1", - Kind: protocol.Method, - File: 0, - }, - { - Name: "M2", - Kind: protocol.Method, - File: 1, - }, - { - Name: "M3", - Kind: protocol.Method, - File: 1, - }, - }, - }, - { - Name: "b", - Kind: protocol.Variable, - File: 1, - }, + {Name: "A", Kind: protocol.Variable, File: 0}, + {Name: "F", Kind: protocol.Function, File: 1}, + {Name: "S", Kind: protocol.Struct, File: 0, Children: []command.PackageSymbol{ + {Name: "M1", Kind: protocol.Method, File: 0}, + {Name: "M2", Kind: protocol.Method, File: 1}, + {Name: "M3", Kind: protocol.Method, File: 1}, + }}, + {Name: "b", Kind: protocol.Variable, File: 1}, }, } - if diff := cmp.Diff(want, res, cmpopts.IgnoreFields(command.PackageSymbol{}, "Range", "SelectionRange", "Detail")); diff != "" { - t.Errorf("gopls.package_symbols returned unexpected diff (-want +got):\n%s", diff) + ignore := cmpopts.IgnoreFields(command.PackageSymbol{}, "Range", "SelectionRange", "Detail") + if diff := cmp.Diff(want, res, ignore); diff != "" { + t.Errorf("package_symbols returned unexpected diff (-want +got):\n%s", diff) + } + + for file, want := range map[string]command.PackageSymbolsResult{ + "go.mod": {}, + "unloaded.go": { + PackageName: "a", + Files: []protocol.DocumentURI{env.Sandbox.Workdir.URI("unloaded.go")}, + Symbols: []command.PackageSymbol{ + {Name: "Unloaded", Kind: protocol.Variable, File: 0}, + }, + }, + } { + uri := env.Sandbox.Workdir.URI(file) + args, err := command.MarshalArgs(command.PackageSymbolsArgs{ + URI: uri, + }) + if err != nil { + t.Fatal(err) + } + var res command.PackageSymbolsResult + env.ExecuteCommand(&protocol.ExecuteCommandParams{ + Command: command.PackageSymbols.String(), + Arguments: args, + }, &res) + + if diff := cmp.Diff(want, res, ignore); diff != "" { + t.Errorf("package_symbols returned unexpected diff (-want +got):\n%s", diff) + } } }) } From ab04c1963f5c2e5425c494f549e018313bdaa817 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 13 Feb 2025 14:37:56 -0500 Subject: [PATCH 145/309] gopls/internal/analysis/modernize: improve rangeint transformation for i := 0; i < len(slice); i++ {} is currently reduced to for i := range len(slice) {} but this CL delivers the better style of: for i := range slice {} Fixes golang/go#71725 Change-Id: Idb025315047c3be992267f6c1783757798e0c840 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649356 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/rangeint.go | 10 ++++++++++ .../modernize/testdata/src/rangeint/rangeint.go | 5 ++++- .../modernize/testdata/src/rangeint/rangeint.go.golden | 5 ++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index c36203cef06..2d25d6a0a06 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -8,10 +8,12 @@ import ( "fmt" "go/ast" "go/token" + "go/types" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" @@ -98,6 +100,14 @@ func rangeint(pass *analysis.Pass) { }) } + // If limit is len(slice), + // simplify "range len(slice)" to "range slice". + if call, ok := limit.(*ast.CallExpr); ok && + typeutil.Callee(info, call) == builtinLen && + is[*types.Slice](info.TypeOf(call.Args[0]).Underlying()) { + limit = call.Args[0] + } + pass.Report(analysis.Diagnostic{ Pos: init.Pos(), End: inc.End(), diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index e17dccac9d0..a60bd5eac37 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -1,6 +1,6 @@ package rangeint -func _(i int, s struct{ i int }) { +func _(i int, s struct{ i int }, slice []int) { for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" println(i) } @@ -9,6 +9,9 @@ func _(i int, s struct{ i int }) { for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" // i unused within loop } + for i := 0; i < len(slice); i++ { // want "for loop can be modernized using range over int" + println(slice[i]) + } // nope for i := 0; i < 10; { // nope: missing increment diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 5a76229c858..348f77508ac 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -1,6 +1,6 @@ package rangeint -func _(i int, s struct{ i int }) { +func _(i int, s struct{ i int }, slice []int) { for i := range 10 { // want "for loop can be modernized using range over int" println(i) } @@ -9,6 +9,9 @@ func _(i int, s struct{ i int }) { for range 10 { // want "for loop can be modernized using range over int" // i unused within loop } + for i := range slice { // want "for loop can be modernized using range over int" + println(slice[i]) + } // nope for i := 0; i < 10; { // nope: missing increment From 809cde44e486bf9b083f7c68d47e09fc20662b4f Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 13 Feb 2025 13:37:07 -0500 Subject: [PATCH 146/309] gopls/internal/analysis/modernize: fix bug in minmax Wrong operator. D'oh. + test Fixes golang/go#71721 Change-Id: Ia7fe314df07afa9a9de63c2b6031e678755e9d56 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649357 Reviewed-by: Jonathan Amsterdam Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/minmax.go | 2 +- .../analysis/modernize/testdata/src/minmax/minmax.go | 11 +++++++++++ .../modernize/testdata/src/minmax/minmax.go.golden | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index 26b12341cad..1466e767fc7 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -57,7 +57,7 @@ func minmax(pass *analysis.Pass) { if equalSyntax(lhs, lhs2) { if equalSyntax(rhs, a) && equalSyntax(rhs2, b) { sign = +sign - } else if equalSyntax(rhs2, a) || equalSyntax(rhs, b) { + } else if equalSyntax(rhs2, a) && equalSyntax(rhs, b) { sign = -sign } else { return diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index c73bd30139b..8fdc3bc2106 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -92,3 +92,14 @@ func nopeAssignHasIncrementOperator() { } print(y) } + +// Regression test for https://github.com/golang/go/issues/71721. +func nopeNotAMinimum(x, y int) int { + // A value of -1 or 0 will use a default value (30). + if x <= 0 { + y = 30 + } else { + y = x + } + return y +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index 11eac2c1418..48e154729e7 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -69,3 +69,14 @@ func nopeAssignHasIncrementOperator() { } print(y) } + +// Regression test for https://github.com/golang/go/issues/71721. +func nopeNotAMinimum(x, y int) int { + // A value of -1 or 0 will use a default value (30). + if x <= 0 { + y = 30 + } else { + y = x + } + return y +} From 85a3833c521302254b403f7606939863e21f736a Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 12 Feb 2025 11:18:11 -0500 Subject: [PATCH 147/309] internal/analysis/gofix: simple type aliases Offer to replace inlinable type aliases whose RHS is a named type. Despite the amount of new code, there is little to test, because most of it duplicates constants. When there is a chain of inlinable type aliases, as in: type A = T type AA = A var v AA we don't follow the chain to the end. Instead, the first set of fixes produces: type A = T type AA = T var v A That is, the replacements happen "in parallel." A second round of fixes would rewrite var v A to var v T This case is rare enough that it's not worth doing better. For golang/go#32816. Change-Id: Ib6854d60b26a273b592a297cb9a650a31e094392 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649055 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 264 +++++++++++++----- .../analysis/gofix/testdata/src/a/a.go | 15 + .../analysis/gofix/testdata/src/a/a.go.golden | 15 + .../analysis/gofix/testdata/src/b/b.go | 2 + .../analysis/gofix/testdata/src/b/b.go.golden | 2 + 5 files changed, 225 insertions(+), 73 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 8ec31bd4736..147399d315d 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -63,8 +63,11 @@ func run(pass *analysis.Pass) (any, error) { // Pass 1: find functions and constants annotated with an appropriate "//go:fix" // comment (the syntax proposed by #32816), // and export a fact for each one. - inlinableFuncs := make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) - inlinableConsts := make(map[*types.Const]*goFixInlineConstFact) + var ( + inlinableFuncs = make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) + inlinableConsts = make(map[*types.Const]*goFixInlineConstFact) + inlinableAliases = make(map[*types.TypeName]*goFixInlineAliasFact) + ) inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)} @@ -89,52 +92,96 @@ func run(pass *analysis.Pass) (any, error) { inlinableFuncs[fn] = callee case *ast.GenDecl: - if decl.Tok != token.CONST { + if decl.Tok != token.CONST && decl.Tok != token.TYPE { return } declInline := hasFixInline(decl.Doc) // Accept inline directives on the entire decl as well as individual specs. for _, spec := range decl.Specs { - spec := spec.(*ast.ValueSpec) // guaranteed by Tok == CONST - specInline := hasFixInline(spec.Doc) - if declInline || specInline { - for i, name := range spec.Names { - if i >= len(spec.Values) { - // Possible following an iota. - break - } - val := spec.Values[i] - var rhsID *ast.Ident - switch e := val.(type) { - case *ast.Ident: - // Constants defined with the predeclared iota cannot be inlined. - if pass.TypesInfo.Uses[e] == builtinIota { - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") + switch spec := spec.(type) { + case *ast.TypeSpec: // Tok == TYPE + if !declInline && !hasFixInline(spec.Doc) { + continue + } + if !spec.Assign.IsValid() { + pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") + continue + } + if spec.TypeParams != nil { + // TODO(jba): handle generic aliases + continue + } + // The alias must refer to another named type. + // TODO(jba): generalize to more type expressions. + var rhsID *ast.Ident + switch e := ast.Unparen(spec.Type).(type) { + case *ast.Ident: + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + continue + } + lhs := pass.TypesInfo.Defs[spec.Name].(*types.TypeName) + // more (jba): test one alias pointing to another alias + rhs := pass.TypesInfo.Uses[rhsID].(*types.TypeName) + typ := &goFixInlineAliasFact{ + RHSName: rhs.Name(), + RHSPkgName: rhs.Pkg().Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + if rhs.Pkg() == pass.Pkg { + typ.rhsObj = rhs + } + inlinableAliases[lhs] = typ + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn about uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + pass.ExportObjectFact(lhs, typ) + } + + case *ast.ValueSpec: // Tok == CONST + specInline := hasFixInline(spec.Doc) + if declInline || specInline { + for i, name := range spec.Names { + if i >= len(spec.Values) { + // Possible following an iota. + break + } + val := spec.Values[i] + var rhsID *ast.Ident + switch e := val.(type) { + case *ast.Ident: + // Constants defined with the predeclared iota cannot be inlined. + if pass.TypesInfo.Uses[e] == builtinIota { + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") + continue + } + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") continue } - rhsID = e - case *ast.SelectorExpr: - rhsID = e.Sel - default: - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") - continue - } - lhs := pass.TypesInfo.Defs[name].(*types.Const) - rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program - con := &goFixInlineConstFact{ - RHSName: rhs.Name(), - RHSPkgName: rhs.Pkg().Name(), - RHSPkgPath: rhs.Pkg().Path(), - } - if rhs.Pkg() == pass.Pkg { - con.rhsObj = rhs - } - inlinableConsts[lhs] = con - // Create a fact only if the LHS is exported and defined at top level. - // We create a fact even if the RHS is non-exported, - // so we can warn uses in other packages. - if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - pass.ExportObjectFact(lhs, con) + lhs := pass.TypesInfo.Defs[name].(*types.Const) + rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program + con := &goFixInlineConstFact{ + RHSName: rhs.Name(), + RHSPkgName: rhs.Pkg().Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + if rhs.Pkg() == pass.Pkg { + con.rhsObj = rhs + } + inlinableConsts[lhs] = con + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn about uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + pass.ExportObjectFact(lhs, con) + } } } } @@ -143,7 +190,7 @@ func run(pass *analysis.Pass) (any, error) { }) // Pass 2. Inline each static call to an inlinable function - // and each reference to an inlinable constant. + // and each reference to an inlinable constant or type alias. // // TODO(adonovan): handle multiple diffs that each add the same import. for cur := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { @@ -218,6 +265,65 @@ func run(pass *analysis.Pass) (any, error) { } case *ast.Ident: + // If the identifier is a use of an inlinable type alias, suggest inlining it. + // TODO(jba): much of this code is shared with the constant case, below. + // Try to factor more of it out, unless it will change anyway when we move beyond simple RHS's. + if ali, ok := pass.TypesInfo.Uses[n].(*types.TypeName); ok { + inalias, ok := inlinableAliases[ali] + if !ok { + var fact goFixInlineAliasFact + if pass.ImportObjectFact(ali, &fact) { + inalias = &fact + inlinableAliases[ali] = inalias + } + } + if inalias == nil { + continue // nope + } + curFile := currentFile(cur) + + // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, + // where sel is the parent of X), // and an inlinable "type A = B" elsewhere (inali). + // Consider replacing A with B. + + // Check that the expression we are inlining (B) means the same thing + // (refers to the same object) in n's scope as it does in A's scope. + // If the RHS is not in the current package, AddImport will handle + // shadowing, so we only need to worry about when both expressions + // are in the current package. + if pass.Pkg.Path() == inalias.RHSPkgPath { + // fcon.rhsObj is the object referred to by B in the definition of A. + scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope + if obj == nil { + // Should be impossible: if code at n can refer to the LHS, + // it can refer to the RHS. + panic(fmt.Sprintf("no object for inlinable alias %s RHS %s", n.Name, inalias.RHSName)) + } + if obj != inalias.rhsObj { + // "B" means something different here than at the inlinable const's scope. + continue + } + } else if !analysisinternal.CanImport(pass.Pkg.Path(), inalias.RHSPkgPath) { + // If this package can't see the RHS's package, we can't inline. + continue + } + var ( + importPrefix string + edits []analysis.TextEdit + ) + if inalias.RHSPkgPath != pass.Pkg.Path() { + _, importPrefix, edits = analysisinternal.AddImport( + pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) + } + // If n is qualified by a package identifier, we'll need the full selector expression. + var expr ast.Expr = n + if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + expr = cur.Parent().Node().(ast.Expr) + } + reportInline(pass, "type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) + continue + } // If the identifier is a use of an inlinable constant, suggest inlining it. if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { incon, ok := inlinableConsts[con] @@ -233,14 +339,10 @@ func run(pass *analysis.Pass) (any, error) { } // If n is qualified by a package identifier, we'll need the full selector expression. - var sel *ast.SelectorExpr - if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { - sel = cur.Parent().Node().(*ast.SelectorExpr) - } curFile := currentFile(cur) - // We have an identifier A here (n), possibly qualified by a package identifier (sel.X), - // and an inlinable "const A = B" elsewhere (fcon). + // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, + // where sel is the parent of n), // and an inlinable "const A = B" elsewhere (incon). // Consider replacing A with B. // Check that the expression we are inlining (B) means the same thing @@ -249,7 +351,7 @@ func run(pass *analysis.Pass) (any, error) { // shadowing, so we only need to worry about when both expressions // are in the current package. if pass.Pkg.Path() == incon.RHSPkgPath { - // fcon.rhsObj is the object referred to by B in the definition of A. + // incon.rhsObj is the object referred to by B in the definition of A. scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { @@ -273,31 +375,12 @@ func run(pass *analysis.Pass) (any, error) { _, importPrefix, edits = analysisinternal.AddImport( pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) } - var ( - pos = n.Pos() - end = n.End() - name = n.Name - ) - // Replace the entire SelectorExpr if there is one. - if sel != nil { - pos = sel.Pos() - end = sel.End() - name = sel.X.(*ast.Ident).Name + "." + n.Name + // If n is qualified by a package identifier, we'll need the full selector expression. + var expr ast.Expr = n + if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + expr = cur.Parent().Node().(ast.Expr) } - edits = append(edits, analysis.TextEdit{ - Pos: pos, - End: end, - NewText: []byte(importPrefix + incon.RHSName), - }) - pass.Report(analysis.Diagnostic{ - Pos: pos, - End: end, - Message: fmt.Sprintf("Constant %s should be inlined", name), - SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Inline constant %s", name), - TextEdits: edits, - }}, - }) + reportInline(pass, "constant", "Constant", expr, edits, importPrefix+incon.RHSName) } } } @@ -305,6 +388,25 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } +// reportInline reports a diagnostic for fixing an inlinable name. +func reportInline(pass *analysis.Pass, kind, capKind string, ident ast.Expr, edits []analysis.TextEdit, newText string) { + edits = append(edits, analysis.TextEdit{ + Pos: ident.Pos(), + End: ident.End(), + NewText: []byte(newText), + }) + name := analysisinternal.Format(pass.Fset, ident) + pass.Report(analysis.Diagnostic{ + Pos: ident.Pos(), + End: ident.End(), + Message: fmt.Sprintf("%s %s should be inlined", capKind, name), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Inline %s %s", kind, name), + TextEdits: edits, + }}, + }) +} + // hasFixInline reports the presence of a "//go:fix inline" directive // in the comments. func hasFixInline(cg *ast.CommentGroup) bool { @@ -339,6 +441,22 @@ func (c *goFixInlineConstFact) String() string { func (*goFixInlineConstFact) AFact() {} +// A goFixInlineAliasFact is exported for each type alias marked "//go:fix inline". +// It holds information about an inlinable type alias. Gob-serializable. +type goFixInlineAliasFact struct { + // Information about "type LHSName = RHSName". + RHSName string + RHSPkgPath string + RHSPkgName string + rhsObj types.Object // for current package +} + +func (c *goFixInlineAliasFact) String() string { + return fmt.Sprintf("goFixInline alias %q.%s", c.RHSPkgPath, c.RHSName) +} + +func (*goFixInlineAliasFact) AFact() {} + func discard(string, ...any) {} var builtinIota = types.Universe.Lookup("iota") diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index 4f41b9a8c5d..fb4d8b92172 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -101,3 +101,18 @@ func shadow() { _ = x } + +// Type aliases + +//go:fix inline +type A = T // want A: `goFixInline alias "a".T` + +var _ A // want `Type alias A should be inlined` + +type B = []T // nope: only named RHSs + +//go:fix inline +type AA = // want AA: `goFixInline alias "a".A` +A // want `Type alias A should be inlined` + +var _ AA // want `Type alias AA should be inlined` diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index 9e9cc25996f..9ab1bcbc652 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -101,3 +101,18 @@ func shadow() { _ = x } + +// Type aliases + +//go:fix inline +type A = T // want A: `goFixInline alias "a".T` + +var _ T // want `Type alias A should be inlined` + +type B = []T // nope: only named RHSs + +//go:fix inline +type AA = // want AA: `goFixInline alias "a".A` +T // want `Type alias A should be inlined` + +var _ A // want `Type alias AA should be inlined` diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go b/gopls/internal/analysis/gofix/testdata/src/b/b.go index 74876738bea..d52fd514024 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go @@ -30,3 +30,5 @@ func g() { } const d = a.D // nope: a.D refers to a constant in a package that is not visible here. + +var _ a.A // want `Type alias a\.A should be inlined` diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index b3608d6793e..4228ffeb489 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -34,3 +34,5 @@ func g() { } const d = a.D // nope: a.D refers to a constant in a package that is not visible here. + +var _ a.T // want `Type alias a\.A should be inlined` From c0dbb60e4ba78287d76e623a94ae61616ff3c74c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 13 Feb 2025 16:29:14 -0500 Subject: [PATCH 148/309] gopls: tweak release notes Also, move gofix command into a package so that it can be "go run" from the release branch. Change-Id: I0a75c1ec4b00d22eef6c13c5162dd02ed9ef272f Reviewed-on: https://go-review.googlesource.com/c/tools/+/649318 Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- gopls/doc/analyzers.md | 38 ++++++++++++++++--- gopls/doc/release/v0.18.0.md | 18 +++++++-- .../analysis/gofix/{ => cmd/gofix}/main.go | 5 +-- gopls/internal/analysis/gofix/doc.go | 4 ++ gopls/internal/analysis/modernize/doc.go | 9 +++++ gopls/internal/analysis/unusedfunc/doc.go | 29 +++++++++++--- gopls/internal/doc/api.json | 8 ++-- 7 files changed, 88 insertions(+), 23 deletions(-) rename gopls/internal/analysis/gofix/{ => cmd/gofix}/main.go (83%) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 68465f9809d..dde95591718 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -500,6 +500,15 @@ existing code by using more modern features of Go, such as: - replacing Split in "for range strings.Split(...)" by go1.24's more efficient SplitSeq; +To apply all modernization fixes en masse, you can use the +following command: + + $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./... + +If the tool warns of conflicting fixes, you may need to run it more +than once until it has applied all fixes cleanly. This command is +not an officially supported interface and may change in the future. + Default: on. Package documentation: [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) @@ -962,12 +971,29 @@ A method is considered unused if it is unexported, not referenced that of any method of an interface type declared within the same package. -The tool may report a false positive for a declaration of an -unexported function that is referenced from another package using -the go:linkname mechanism, if the declaration's doc comment does -not also have a go:linkname comment. (Such code is in any case -strongly discouraged: linkname annotations, if they must be used at -all, should be used on both the declaration and the alias.) +The tool may report false positives in some situations, for +example: + + - For a declaration of an unexported function that is referenced + from another package using the go:linkname mechanism, if the + declaration's doc comment does not also have a go:linkname + comment. + + (Such code is in any case strongly discouraged: linkname + annotations, if they must be used at all, should be used on both + the declaration and the alias.) + + - For compiler intrinsics in the "runtime" package that, though + never referenced, are known to the compiler and are called + indirectly by compiled object code. + + - For functions called only from assembly. + + - For functions called only from files whose build tags are not + selected in the current build configuration. + +See https://github.com/golang/go/issues/71686 for discussion of +these limitations. The unusedfunc algorithm is not as precise as the golang.org/x/tools/cmd/deadcode tool, but it has the advantage that diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index 8d641a2104f..ba2c0184307 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -37,16 +37,22 @@ details to be reported as diagnostics. For example, it indicates which variables escape to the heap, and which array accesses require bounds checks. +TODO: add links to the complete manual for each item. + ## New `modernize` analyzer Gopls now reports when code could be simplified or clarified by using more modern features of Go, and provides a quick fix to apply the change. -Examples: +For example, a conditional assignment using an if/else statement may +be replaced by a call to the `min` or `max` built-in functions added +in Go 1.18. -- replacement of conditional assignment using an if/else statement by - a call to the `min` or `max` built-in functions added in Go 1.18; +Use this command to apply modernization fixes en masse: +``` +$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./... +``` ## New `unusedfunc` analyzer @@ -97,6 +103,12 @@ const Ptr = Pointer ``` gopls will suggest replacing `Ptr` in your code with `Pointer`. +Use this command to apply such fixes en masse: + +``` +$ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./... +``` + ## "Implementations" supports generics At long last, the "Go to Implementations" feature now fully supports diff --git a/gopls/internal/analysis/gofix/main.go b/gopls/internal/analysis/gofix/cmd/gofix/main.go similarity index 83% rename from gopls/internal/analysis/gofix/main.go rename to gopls/internal/analysis/gofix/cmd/gofix/main.go index fde633f2f62..d75978f6e59 100644 --- a/gopls/internal/analysis/gofix/main.go +++ b/gopls/internal/analysis/gofix/cmd/gofix/main.go @@ -1,10 +1,7 @@ -// Copyright 2023 The Go Authors. All rights reserved. +// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build ignore -// +build ignore - // The inline command applies the inliner to the specified packages of // Go source code. Run with: // diff --git a/gopls/internal/analysis/gofix/doc.go b/gopls/internal/analysis/gofix/doc.go index a0c6a08ded9..ad8b067daa4 100644 --- a/gopls/internal/analysis/gofix/doc.go +++ b/gopls/internal/analysis/gofix/doc.go @@ -77,5 +77,9 @@ or before a group, applying to every constant in the group: ) The proposal https://go.dev/issue/32816 introduces the "//go:fix" directives. + +You can use this (officially unsupported) command to apply gofix fixes en masse: + + $ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./... */ package gofix diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 15aeab64d8d..3759fdb10c5 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -32,4 +32,13 @@ // for i := range n {}, added in go1.22; // - replacing Split in "for range strings.Split(...)" by go1.24's // more efficient SplitSeq; +// +// To apply all modernization fixes en masse, you can use the +// following command: +// +// $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./... +// +// If the tool warns of conflicting fixes, you may need to run it more +// than once until it has applied all fixes cleanly. This command is +// not an officially supported interface and may change in the future. package modernize diff --git a/gopls/internal/analysis/unusedfunc/doc.go b/gopls/internal/analysis/unusedfunc/doc.go index 5946ed897bb..9e2fc8145c8 100644 --- a/gopls/internal/analysis/unusedfunc/doc.go +++ b/gopls/internal/analysis/unusedfunc/doc.go @@ -20,12 +20,29 @@ // that of any method of an interface type declared within the same // package. // -// The tool may report a false positive for a declaration of an -// unexported function that is referenced from another package using -// the go:linkname mechanism, if the declaration's doc comment does -// not also have a go:linkname comment. (Such code is in any case -// strongly discouraged: linkname annotations, if they must be used at -// all, should be used on both the declaration and the alias.) +// The tool may report false positives in some situations, for +// example: +// +// - For a declaration of an unexported function that is referenced +// from another package using the go:linkname mechanism, if the +// declaration's doc comment does not also have a go:linkname +// comment. +// +// (Such code is in any case strongly discouraged: linkname +// annotations, if they must be used at all, should be used on both +// the declaration and the alias.) +// +// - For compiler intrinsics in the "runtime" package that, though +// never referenced, are known to the compiler and are called +// indirectly by compiled object code. +// +// - For functions called only from assembly. +// +// - For functions called only from files whose build tags are not +// selected in the current build configuration. +// +// See https://github.com/golang/go/issues/71686 for discussion of +// these limitations. // // The unusedfunc algorithm is not as precise as the // golang.org/x/tools/cmd/deadcode tool, but it has the advantage that diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 8f101079a9c..629e45ff766 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -510,7 +510,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", "Default": "true" }, { @@ -630,7 +630,7 @@ }, { "Name": "\"unusedfunc\"", - "Doc": "check for unused functions and methods\n\nThe unusedfunc analyzer reports functions and methods that are\nnever referenced outside of their own declaration.\n\nA function is considered unused if it is unexported and not\nreferenced (except within its own declaration).\n\nA method is considered unused if it is unexported, not referenced\n(except within its own declaration), and its name does not match\nthat of any method of an interface type declared within the same\npackage.\n\nThe tool may report a false positive for a declaration of an\nunexported function that is referenced from another package using\nthe go:linkname mechanism, if the declaration's doc comment does\nnot also have a go:linkname comment. (Such code is in any case\nstrongly discouraged: linkname annotations, if they must be used at\nall, should be used on both the declaration and the alias.)\n\nThe unusedfunc algorithm is not as precise as the\ngolang.org/x/tools/cmd/deadcode tool, but it has the advantage that\nit runs within the modular analysis framework, enabling near\nreal-time feedback within gopls.", + "Doc": "check for unused functions and methods\n\nThe unusedfunc analyzer reports functions and methods that are\nnever referenced outside of their own declaration.\n\nA function is considered unused if it is unexported and not\nreferenced (except within its own declaration).\n\nA method is considered unused if it is unexported, not referenced\n(except within its own declaration), and its name does not match\nthat of any method of an interface type declared within the same\npackage.\n\nThe tool may report false positives in some situations, for\nexample:\n\n - For a declaration of an unexported function that is referenced\n from another package using the go:linkname mechanism, if the\n declaration's doc comment does not also have a go:linkname\n comment.\n\n (Such code is in any case strongly discouraged: linkname\n annotations, if they must be used at all, should be used on both\n the declaration and the alias.)\n\n - For compiler intrinsics in the \"runtime\" package that, though\n never referenced, are known to the compiler and are called\n indirectly by compiled object code.\n\n - For functions called only from assembly.\n\n - For functions called only from files whose build tags are not\n selected in the current build configuration.\n\nSee https://github.com/golang/go/issues/71686 for discussion of\nthese limitations.\n\nThe unusedfunc algorithm is not as precise as the\ngolang.org/x/tools/cmd/deadcode tool, but it has the advantage that\nit runs within the modular analysis framework, enabling near\nreal-time feedback within gopls.", "Default": "true" }, { @@ -1189,7 +1189,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, @@ -1333,7 +1333,7 @@ }, { "Name": "unusedfunc", - "Doc": "check for unused functions and methods\n\nThe unusedfunc analyzer reports functions and methods that are\nnever referenced outside of their own declaration.\n\nA function is considered unused if it is unexported and not\nreferenced (except within its own declaration).\n\nA method is considered unused if it is unexported, not referenced\n(except within its own declaration), and its name does not match\nthat of any method of an interface type declared within the same\npackage.\n\nThe tool may report a false positive for a declaration of an\nunexported function that is referenced from another package using\nthe go:linkname mechanism, if the declaration's doc comment does\nnot also have a go:linkname comment. (Such code is in any case\nstrongly discouraged: linkname annotations, if they must be used at\nall, should be used on both the declaration and the alias.)\n\nThe unusedfunc algorithm is not as precise as the\ngolang.org/x/tools/cmd/deadcode tool, but it has the advantage that\nit runs within the modular analysis framework, enabling near\nreal-time feedback within gopls.", + "Doc": "check for unused functions and methods\n\nThe unusedfunc analyzer reports functions and methods that are\nnever referenced outside of their own declaration.\n\nA function is considered unused if it is unexported and not\nreferenced (except within its own declaration).\n\nA method is considered unused if it is unexported, not referenced\n(except within its own declaration), and its name does not match\nthat of any method of an interface type declared within the same\npackage.\n\nThe tool may report false positives in some situations, for\nexample:\n\n - For a declaration of an unexported function that is referenced\n from another package using the go:linkname mechanism, if the\n declaration's doc comment does not also have a go:linkname\n comment.\n\n (Such code is in any case strongly discouraged: linkname\n annotations, if they must be used at all, should be used on both\n the declaration and the alias.)\n\n - For compiler intrinsics in the \"runtime\" package that, though\n never referenced, are known to the compiler and are called\n indirectly by compiled object code.\n\n - For functions called only from assembly.\n\n - For functions called only from files whose build tags are not\n selected in the current build configuration.\n\nSee https://github.com/golang/go/issues/71686 for discussion of\nthese limitations.\n\nThe unusedfunc algorithm is not as precise as the\ngolang.org/x/tools/cmd/deadcode tool, but it has the advantage that\nit runs within the modular analysis framework, enabling near\nreal-time feedback within gopls.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedfunc", "Default": true }, From 8807101233fe3a3ccf31dca77298fe538436ee20 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 13 Feb 2025 17:48:26 -0500 Subject: [PATCH 149/309] gopls/internal/analysis/gofix: one function per pass Split the two passes into two separate functions. Declare a type to hold common state. No behavior changes. For golang/go#32816. Change-Id: I571956859b12687d824f36c80f75deb96db38d92 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649476 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 224 +++++++++++++------------ 1 file changed, 120 insertions(+), 104 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 147399d315d..35d21c0e05a 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -37,63 +37,60 @@ var Analyzer = &analysis.Analyzer{ Requires: []*analysis.Analyzer{inspect.Analyzer}, } -func run(pass *analysis.Pass) (any, error) { - // Memoize repeated calls for same file. - fileContent := make(map[string][]byte) - readFile := func(node ast.Node) ([]byte, error) { - filename := pass.Fset.File(node.Pos()).Name() - content, ok := fileContent[filename] - if !ok { - var err error - content, err = pass.ReadFile(filename) - if err != nil { - return nil, err - } - fileContent[filename] = content - } - return content, nil - } +// analyzer holds the state for this analysis. +type analyzer struct { + pass *analysis.Pass + root cursor.Cursor + // memoization of repeated calls for same file. + fileContent map[string][]byte + // memoization of fact imports (nil => no fact) + inlinableFuncs map[*types.Func]*inline.Callee + inlinableConsts map[*types.Const]*goFixInlineConstFact + inlinableAliases map[*types.TypeName]*goFixInlineAliasFact +} - // Return the unique ast.File for a cursor. - currentFile := func(c cursor.Cursor) *ast.File { - cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) - return cf.Node().(*ast.File) +func run(pass *analysis.Pass) (any, error) { + a := &analyzer{ + pass: pass, + root: cursor.Root(pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)), + fileContent: make(map[string][]byte), + inlinableFuncs: make(map[*types.Func]*inline.Callee), + inlinableConsts: make(map[*types.Const]*goFixInlineConstFact), + inlinableAliases: make(map[*types.TypeName]*goFixInlineAliasFact), } + a.find() + a.inline() + return nil, nil +} - // Pass 1: find functions and constants annotated with an appropriate "//go:fix" - // comment (the syntax proposed by #32816), - // and export a fact for each one. - var ( - inlinableFuncs = make(map[*types.Func]*inline.Callee) // memoization of fact import (nil => no fact) - inlinableConsts = make(map[*types.Const]*goFixInlineConstFact) - inlinableAliases = make(map[*types.TypeName]*goFixInlineAliasFact) - ) - - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)} - inspect.Preorder(nodeFilter, func(n ast.Node) { - switch decl := n.(type) { +// find finds functions and constants annotated with an appropriate "//go:fix" +// comment (the syntax proposed by #32816), +// and exports a fact for each one. +func (a *analyzer) find() { + info := a.pass.TypesInfo + for cur := range a.root.Preorder((*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)) { + switch decl := cur.Node().(type) { case *ast.FuncDecl: if !hasFixInline(decl.Doc) { - return + continue } - content, err := readFile(decl) + content, err := a.readFile(decl) if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) - return + a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + continue } - callee, err := inline.AnalyzeCallee(discard, pass.Fset, pass.Pkg, pass.TypesInfo, decl, content) + callee, err := inline.AnalyzeCallee(discard, a.pass.Fset, a.pass.Pkg, info, decl, content) if err != nil { - pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) - return + a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) + continue } - fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) - pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) - inlinableFuncs[fn] = callee + fn := info.Defs[decl.Name].(*types.Func) + a.pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) + a.inlinableFuncs[fn] = callee case *ast.GenDecl: if decl.Tok != token.CONST && decl.Tok != token.TYPE { - return + continue } declInline := hasFixInline(decl.Doc) // Accept inline directives on the entire decl as well as individual specs. @@ -104,7 +101,7 @@ func run(pass *analysis.Pass) (any, error) { continue } if !spec.Assign.IsValid() { - pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") + a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") continue } if spec.TypeParams != nil { @@ -122,23 +119,23 @@ func run(pass *analysis.Pass) (any, error) { default: continue } - lhs := pass.TypesInfo.Defs[spec.Name].(*types.TypeName) + lhs := info.Defs[spec.Name].(*types.TypeName) // more (jba): test one alias pointing to another alias - rhs := pass.TypesInfo.Uses[rhsID].(*types.TypeName) + rhs := info.Uses[rhsID].(*types.TypeName) typ := &goFixInlineAliasFact{ RHSName: rhs.Name(), RHSPkgName: rhs.Pkg().Name(), RHSPkgPath: rhs.Pkg().Path(), } - if rhs.Pkg() == pass.Pkg { + if rhs.Pkg() == a.pass.Pkg { typ.rhsObj = rhs } - inlinableAliases[lhs] = typ + a.inlinableAliases[lhs] = typ // Create a fact only if the LHS is exported and defined at top level. // We create a fact even if the RHS is non-exported, // so we can warn about uses in other packages. if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - pass.ExportObjectFact(lhs, typ) + a.pass.ExportObjectFact(lhs, typ) } case *ast.ValueSpec: // Tok == CONST @@ -154,58 +151,65 @@ func run(pass *analysis.Pass) (any, error) { switch e := val.(type) { case *ast.Ident: // Constants defined with the predeclared iota cannot be inlined. - if pass.TypesInfo.Uses[e] == builtinIota { - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") + if info.Uses[e] == builtinIota { + a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") continue } rhsID = e case *ast.SelectorExpr: rhsID = e.Sel default: - pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") continue } - lhs := pass.TypesInfo.Defs[name].(*types.Const) - rhs := pass.TypesInfo.Uses[rhsID].(*types.Const) // must be so in a well-typed program + lhs := info.Defs[name].(*types.Const) + rhs := info.Uses[rhsID].(*types.Const) // must be so in a well-typed program con := &goFixInlineConstFact{ RHSName: rhs.Name(), RHSPkgName: rhs.Pkg().Name(), RHSPkgPath: rhs.Pkg().Path(), } - if rhs.Pkg() == pass.Pkg { + if rhs.Pkg() == a.pass.Pkg { con.rhsObj = rhs } - inlinableConsts[lhs] = con + a.inlinableConsts[lhs] = con // Create a fact only if the LHS is exported and defined at top level. // We create a fact even if the RHS is non-exported, // so we can warn about uses in other packages. if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - pass.ExportObjectFact(lhs, con) + a.pass.ExportObjectFact(lhs, con) } } } } } } - }) + } +} + +// inline inlines each static call to an inlinable function +// and each reference to an inlinable constant or type alias. +// +// TODO(adonovan): handle multiple diffs that each add the same import. +func (a *analyzer) inline() { + // Return the unique ast.File for a cursor. + currentFile := func(c cursor.Cursor) *ast.File { + cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) + return cf.Node().(*ast.File) + } - // Pass 2. Inline each static call to an inlinable function - // and each reference to an inlinable constant or type alias. - // - // TODO(adonovan): handle multiple diffs that each add the same import. - for cur := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { - n := cur.Node() - switch n := n.(type) { + for cur := range a.root.Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { + switch n := cur.Node().(type) { case *ast.CallExpr: call := n - if fn := typeutil.StaticCallee(pass.TypesInfo, call); fn != nil { + if fn := typeutil.StaticCallee(a.pass.TypesInfo, call); fn != nil { // Inlinable? - callee, ok := inlinableFuncs[fn] + callee, ok := a.inlinableFuncs[fn] if !ok { var fact goFixInlineFuncFact - if pass.ImportObjectFact(fn, &fact) { + if a.pass.ImportObjectFact(fn, &fact) { callee = fact.Callee - inlinableFuncs[fn] = callee + a.inlinableFuncs[fn] = callee } } if callee == nil { @@ -213,23 +217,23 @@ func run(pass *analysis.Pass) (any, error) { } // Inline the call. - content, err := readFile(call) + content, err := a.readFile(call) if err != nil { - pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) + a.pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) continue } curFile := currentFile(cur) caller := &inline.Caller{ - Fset: pass.Fset, - Types: pass.Pkg, - Info: pass.TypesInfo, + Fset: a.pass.Fset, + Types: a.pass.Pkg, + Info: a.pass.TypesInfo, File: curFile, Call: call, Content: content, } res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) if err != nil { - pass.Reportf(call.Lparen, "%v", err) + a.pass.Reportf(call.Lparen, "%v", err) continue } if res.Literalized { @@ -253,7 +257,7 @@ func run(pass *analysis.Pass) (any, error) { NewText: []byte(edit.New), }) } - pass.Report(analysis.Diagnostic{ + a.pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), Message: fmt.Sprintf("Call of %v should be inlined", callee), @@ -268,13 +272,13 @@ func run(pass *analysis.Pass) (any, error) { // If the identifier is a use of an inlinable type alias, suggest inlining it. // TODO(jba): much of this code is shared with the constant case, below. // Try to factor more of it out, unless it will change anyway when we move beyond simple RHS's. - if ali, ok := pass.TypesInfo.Uses[n].(*types.TypeName); ok { - inalias, ok := inlinableAliases[ali] + if ali, ok := a.pass.TypesInfo.Uses[n].(*types.TypeName); ok { + inalias, ok := a.inlinableAliases[ali] if !ok { var fact goFixInlineAliasFact - if pass.ImportObjectFact(ali, &fact) { + if a.pass.ImportObjectFact(ali, &fact) { inalias = &fact - inlinableAliases[ali] = inalias + a.inlinableAliases[ali] = inalias } } if inalias == nil { @@ -291,10 +295,10 @@ func run(pass *analysis.Pass) (any, error) { // If the RHS is not in the current package, AddImport will handle // shadowing, so we only need to worry about when both expressions // are in the current package. - if pass.Pkg.Path() == inalias.RHSPkgPath { + if a.pass.Pkg.Path() == inalias.RHSPkgPath { // fcon.rhsObj is the object referred to by B in the definition of A. - scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. @@ -304,7 +308,7 @@ func run(pass *analysis.Pass) (any, error) { // "B" means something different here than at the inlinable const's scope. continue } - } else if !analysisinternal.CanImport(pass.Pkg.Path(), inalias.RHSPkgPath) { + } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), inalias.RHSPkgPath) { // If this package can't see the RHS's package, we can't inline. continue } @@ -312,26 +316,26 @@ func run(pass *analysis.Pass) (any, error) { importPrefix string edits []analysis.TextEdit ) - if inalias.RHSPkgPath != pass.Pkg.Path() { + if inalias.RHSPkgPath != a.pass.Pkg.Path() { _, importPrefix, edits = analysisinternal.AddImport( - pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) + a.pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) } // If n is qualified by a package identifier, we'll need the full selector expression. var expr ast.Expr = n if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { expr = cur.Parent().Node().(ast.Expr) } - reportInline(pass, "type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) + a.reportInline("type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) continue } // If the identifier is a use of an inlinable constant, suggest inlining it. - if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok { - incon, ok := inlinableConsts[con] + if con, ok := a.pass.TypesInfo.Uses[n].(*types.Const); ok { + incon, ok := a.inlinableConsts[con] if !ok { var fact goFixInlineConstFact - if pass.ImportObjectFact(con, &fact) { + if a.pass.ImportObjectFact(con, &fact) { incon = &fact - inlinableConsts[con] = incon + a.inlinableConsts[con] = incon } } if incon == nil { @@ -350,10 +354,10 @@ func run(pass *analysis.Pass) (any, error) { // If the RHS is not in the current package, AddImport will handle // shadowing, so we only need to worry about when both expressions // are in the current package. - if pass.Pkg.Path() == incon.RHSPkgPath { + if a.pass.Pkg.Path() == incon.RHSPkgPath { // incon.rhsObj is the object referred to by B in the definition of A. - scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope if obj == nil { // Should be impossible: if code at n can refer to the LHS, // it can refer to the RHS. @@ -363,7 +367,7 @@ func run(pass *analysis.Pass) (any, error) { // "B" means something different here than at the inlinable const's scope. continue } - } else if !analysisinternal.CanImport(pass.Pkg.Path(), incon.RHSPkgPath) { + } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), incon.RHSPkgPath) { // If this package can't see the RHS's package, we can't inline. continue } @@ -371,32 +375,30 @@ func run(pass *analysis.Pass) (any, error) { importPrefix string edits []analysis.TextEdit ) - if incon.RHSPkgPath != pass.Pkg.Path() { + if incon.RHSPkgPath != a.pass.Pkg.Path() { _, importPrefix, edits = analysisinternal.AddImport( - pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) + a.pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) } // If n is qualified by a package identifier, we'll need the full selector expression. var expr ast.Expr = n if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { expr = cur.Parent().Node().(ast.Expr) } - reportInline(pass, "constant", "Constant", expr, edits, importPrefix+incon.RHSName) + a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName) } } } - - return nil, nil } // reportInline reports a diagnostic for fixing an inlinable name. -func reportInline(pass *analysis.Pass, kind, capKind string, ident ast.Expr, edits []analysis.TextEdit, newText string) { +func (a *analyzer) reportInline(kind, capKind string, ident ast.Expr, edits []analysis.TextEdit, newText string) { edits = append(edits, analysis.TextEdit{ Pos: ident.Pos(), End: ident.End(), NewText: []byte(newText), }) - name := analysisinternal.Format(pass.Fset, ident) - pass.Report(analysis.Diagnostic{ + name := analysisinternal.Format(a.pass.Fset, ident) + a.pass.Report(analysis.Diagnostic{ Pos: ident.Pos(), End: ident.End(), Message: fmt.Sprintf("%s %s should be inlined", capKind, name), @@ -407,6 +409,20 @@ func reportInline(pass *analysis.Pass, kind, capKind string, ident ast.Expr, edi }) } +func (a *analyzer) readFile(node ast.Node) ([]byte, error) { + filename := a.pass.Fset.File(node.Pos()).Name() + content, ok := a.fileContent[filename] + if !ok { + var err error + content, err = a.pass.ReadFile(filename) + if err != nil { + return nil, err + } + a.fileContent[filename] = content + } + return content, nil +} + // hasFixInline reports the presence of a "//go:fix inline" directive // in the comments. func hasFixInline(cg *ast.CommentGroup) bool { From 2880aae7521d3d90acfcef88c2efd17fc8f10369 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Thu, 13 Feb 2025 17:16:56 -0500 Subject: [PATCH 150/309] gopls/internal/protocol: Avoid omitempty for integer fields The existing code adds the json tag 'omitempty' to optional protocol fields, but for some integer-valued fields 0 and empty have different semantics. This CL changes the behavior so that optional integer-valued fields are not omitted if they are zero. FIxes golang.go/go#71489 Change-Id: I1f2cd6c6b0d6b7495adb1d8bc0b404ee9ea895f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649455 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/protocol/generate/generate.go | 35 +++++++++++--------- gopls/internal/protocol/generate/output.go | 13 ++++++-- gopls/internal/protocol/tsprotocol.go | 22 ++++++------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/gopls/internal/protocol/generate/generate.go b/gopls/internal/protocol/generate/generate.go index 2bb14790940..9c7009113ab 100644 --- a/gopls/internal/protocol/generate/generate.go +++ b/gopls/internal/protocol/generate/generate.go @@ -54,39 +54,44 @@ func generateDoc(out *bytes.Buffer, doc string) { // decide if a property is optional, and if it needs a * // return ",omitempty" if it is optional, and "*" if it needs a pointer -func propStar(name string, t NameType, gotype string) (string, string) { - var opt, star string +func propStar(name string, t NameType, gotype string) (omitempty, indirect bool) { if t.Optional { - star = "*" - opt = ",omitempty" + switch gotype { + case "uint32", "int32": + // in FoldingRange.endLine, 0 and empty have different semantics + // There seem to be no other cases. + default: + indirect = true + omitempty = true + } } if strings.HasPrefix(gotype, "[]") || strings.HasPrefix(gotype, "map[") { - star = "" // passed by reference, so no need for * + indirect = false // passed by reference, so no need for * } else { switch gotype { - case "bool", "uint32", "int32", "string", "interface{}", "any": - star = "" // gopls compatibility if t.Optional + case "bool", "string", "interface{}", "any": + indirect = false // gopls compatibility if t.Optional } } - ostar, oopt := star, opt + oind, oomit := indirect, omitempty if newStar, ok := goplsStar[prop{name, t.Name}]; ok { switch newStar { case nothing: - star, opt = "", "" + indirect, omitempty = false, false case wantStar: - star, opt = "*", "" + indirect, omitempty = false, false case wantOpt: - star, opt = "", ",omitempty" + indirect, omitempty = false, true case wantOptStar: - star, opt = "*", ",omitempty" + indirect, omitempty = true, true } - if star == ostar && opt == oopt { // no change - log.Printf("goplsStar[ {%q, %q} ](%d) useless %s/%s %s/%s", name, t.Name, t.Line, ostar, star, oopt, opt) + if indirect == oind && omitempty == oomit { // no change + log.Printf("goplsStar[ {%q, %q} ](%d) useless %v/%v %v/%v", name, t.Name, t.Line, oind, indirect, oomit, omitempty) } usedGoplsStar[prop{name, t.Name}] = true } - return opt, star + return } func goName(s string) string { diff --git a/gopls/internal/protocol/generate/output.go b/gopls/internal/protocol/generate/output.go index ba9d0cb909f..5eaa0cba969 100644 --- a/gopls/internal/protocol/generate/output.go +++ b/gopls/internal/protocol/generate/output.go @@ -273,10 +273,17 @@ func genProps(out *bytes.Buffer, props []NameType, name string) { tp = newNm } // it's a pointer if it is optional, or for gopls compatibility - opt, star := propStar(name, p, tp) - json := fmt.Sprintf(" `json:\"%s%s\"`", p.Name, opt) + omit, star := propStar(name, p, tp) + json := fmt.Sprintf(" `json:\"%s\"`", p.Name) + if omit { + json = fmt.Sprintf(" `json:\"%s,omitempty\"`", p.Name) + } generateDoc(out, p.Documentation) - fmt.Fprintf(out, "\t%s %s%s %s\n", goName(p.Name), star, tp, json) + if star { + fmt.Fprintf(out, "\t%s *%s %s\n", goName(p.Name), tp, json) + } else { + fmt.Fprintf(out, "\t%s %s %s\n", goName(p.Name), tp, json) + } } } diff --git a/gopls/internal/protocol/tsprotocol.go b/gopls/internal/protocol/tsprotocol.go index 444e51e0717..7306f62a7ad 100644 --- a/gopls/internal/protocol/tsprotocol.go +++ b/gopls/internal/protocol/tsprotocol.go @@ -55,7 +55,7 @@ type ApplyWorkspaceEditResult struct { // Depending on the client's failure handling strategy `failedChange` might // contain the index of the change that failed. This property is only available // if the client signals a `failureHandlingStrategy` in its client capabilities. - FailedChange uint32 `json:"failedChange,omitempty"` + FailedChange uint32 `json:"failedChange"` } // A base for all symbol information. @@ -2377,12 +2377,12 @@ type FoldingRange struct { // To be valid, the end must be zero or larger and smaller than the number of lines in the document. StartLine uint32 `json:"startLine"` // The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. - StartCharacter uint32 `json:"startCharacter,omitempty"` + StartCharacter uint32 `json:"startCharacter"` // The zero-based end line of the range to fold. The folded area ends with the line's last character. // To be valid, the end must be zero or larger and smaller than the number of lines in the document. EndLine uint32 `json:"endLine"` // The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. - EndCharacter uint32 `json:"endCharacter,omitempty"` + EndCharacter uint32 `json:"endCharacter"` // Describes the kind of the folding range such as 'comment' or 'region'. The kind // is used to categorize folding ranges and used by commands like 'Fold all comments'. // See {@link FoldingRangeKind} for an enumeration of standardized kinds. @@ -2405,7 +2405,7 @@ type FoldingRangeClientCapabilities struct { // The maximum number of folding ranges that the client prefers to receive // per document. The value serves as a hint, servers are free to follow the // limit. - RangeLimit uint32 `json:"rangeLimit,omitempty"` + RangeLimit uint32 `json:"rangeLimit"` // If set, the client signals that it only supports folding complete lines. // If set, client will ignore specified `startCharacter` and `endCharacter` // properties in a FoldingRange. @@ -4148,7 +4148,7 @@ type PublishDiagnosticsParams struct { // Optional the version number of the document the diagnostics are published for. // // @since 3.15.0 - Version int32 `json:"version,omitempty"` + Version int32 `json:"version"` // An array of diagnostic information items. Diagnostics []Diagnostic `json:"diagnostics"` } @@ -4907,7 +4907,7 @@ type SignatureHelp struct { // // In future version of the protocol this property might become // mandatory to better express this. - ActiveSignature uint32 `json:"activeSignature,omitempty"` + ActiveSignature uint32 `json:"activeSignature"` // The active parameter of the active signature. // // If `null`, no parameter of the signature is active (for example a named @@ -4924,7 +4924,7 @@ type SignatureHelp struct { // In future version of the protocol this property might become // mandatory (but still nullable) to better express the active parameter if // the active signature does have any. - ActiveParameter uint32 `json:"activeParameter,omitempty"` + ActiveParameter uint32 `json:"activeParameter"` } // Client Capabilities for a {@link SignatureHelpRequest}. @@ -5036,7 +5036,7 @@ type SignatureInformation struct { // `SignatureHelp.activeParameter`. // // @since 3.16.0 - ActiveParameter uint32 `json:"activeParameter,omitempty"` + ActiveParameter uint32 `json:"activeParameter"` } // An interactive text edit. @@ -5261,7 +5261,7 @@ type TextDocumentContentChangePartial struct { // The optional length of the range that got replaced. // // @deprecated use range instead. - RangeLength uint32 `json:"rangeLength,omitempty"` + RangeLength uint32 `json:"rangeLength"` // The new text for the provided range. Text string `json:"text"` } @@ -5764,7 +5764,7 @@ type WorkDoneProgressBegin struct { // // The value should be steadily rising. Clients are free to ignore values // that are not following this rule. The value range is [0, 100]. - Percentage uint32 `json:"percentage,omitempty"` + Percentage uint32 `json:"percentage"` } // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workDoneProgressCancelParams @@ -5824,7 +5824,7 @@ type WorkDoneProgressReport struct { // // The value should be steadily rising. Clients are free to ignore values // that are not following this rule. The value range is [0, 100] - Percentage uint32 `json:"percentage,omitempty"` + Percentage uint32 `json:"percentage"` } // Workspace specific client capabilities. From 32ffaa3103522a0b05609e241d1b03b3b1abb9a6 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 14 Feb 2025 08:03:39 -0500 Subject: [PATCH 151/309] gopls/internal/analysis/gofix: one function per kind Refactor so that each kind of inlinable (function, type alias, constant) has its own function for each pass. Refactoring only; no behavior changes. For golang/go#32816. Change-Id: I2f09b4020bcf03409664cee3b8379417d8717fa6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649456 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/gofix/gofix.go | 593 +++++++++++++------------ 1 file changed, 308 insertions(+), 285 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 35d21c0e05a..ffc64be755b 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -67,26 +67,10 @@ func run(pass *analysis.Pass) (any, error) { // comment (the syntax proposed by #32816), // and exports a fact for each one. func (a *analyzer) find() { - info := a.pass.TypesInfo for cur := range a.root.Preorder((*ast.FuncDecl)(nil), (*ast.GenDecl)(nil)) { switch decl := cur.Node().(type) { case *ast.FuncDecl: - if !hasFixInline(decl.Doc) { - continue - } - content, err := a.readFile(decl) - if err != nil { - a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) - continue - } - callee, err := inline.AnalyzeCallee(discard, a.pass.Fset, a.pass.Pkg, info, decl, content) - if err != nil { - a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) - continue - } - fn := info.Defs[decl.Name].(*types.Func) - a.pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) - a.inlinableFuncs[fn] = callee + a.findFunc(decl) case *ast.GenDecl: if decl.Tok != token.CONST && decl.Tok != token.TYPE { @@ -97,91 +81,119 @@ func (a *analyzer) find() { for _, spec := range decl.Specs { switch spec := spec.(type) { case *ast.TypeSpec: // Tok == TYPE - if !declInline && !hasFixInline(spec.Doc) { - continue - } - if !spec.Assign.IsValid() { - a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") - continue - } - if spec.TypeParams != nil { - // TODO(jba): handle generic aliases - continue - } - // The alias must refer to another named type. - // TODO(jba): generalize to more type expressions. - var rhsID *ast.Ident - switch e := ast.Unparen(spec.Type).(type) { - case *ast.Ident: - rhsID = e - case *ast.SelectorExpr: - rhsID = e.Sel - default: - continue - } - lhs := info.Defs[spec.Name].(*types.TypeName) - // more (jba): test one alias pointing to another alias - rhs := info.Uses[rhsID].(*types.TypeName) - typ := &goFixInlineAliasFact{ - RHSName: rhs.Name(), - RHSPkgName: rhs.Pkg().Name(), - RHSPkgPath: rhs.Pkg().Path(), - } - if rhs.Pkg() == a.pass.Pkg { - typ.rhsObj = rhs - } - a.inlinableAliases[lhs] = typ - // Create a fact only if the LHS is exported and defined at top level. - // We create a fact even if the RHS is non-exported, - // so we can warn about uses in other packages. - if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - a.pass.ExportObjectFact(lhs, typ) - } + a.findAlias(spec, declInline) case *ast.ValueSpec: // Tok == CONST - specInline := hasFixInline(spec.Doc) - if declInline || specInline { - for i, name := range spec.Names { - if i >= len(spec.Values) { - // Possible following an iota. - break - } - val := spec.Values[i] - var rhsID *ast.Ident - switch e := val.(type) { - case *ast.Ident: - // Constants defined with the predeclared iota cannot be inlined. - if info.Uses[e] == builtinIota { - a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") - continue - } - rhsID = e - case *ast.SelectorExpr: - rhsID = e.Sel - default: - a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") - continue - } - lhs := info.Defs[name].(*types.Const) - rhs := info.Uses[rhsID].(*types.Const) // must be so in a well-typed program - con := &goFixInlineConstFact{ - RHSName: rhs.Name(), - RHSPkgName: rhs.Pkg().Name(), - RHSPkgPath: rhs.Pkg().Path(), - } - if rhs.Pkg() == a.pass.Pkg { - con.rhsObj = rhs - } - a.inlinableConsts[lhs] = con - // Create a fact only if the LHS is exported and defined at top level. - // We create a fact even if the RHS is non-exported, - // so we can warn about uses in other packages. - if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { - a.pass.ExportObjectFact(lhs, con) - } - } - } + a.findConst(spec, declInline) + } + } + } + } +} + +func (a *analyzer) findFunc(decl *ast.FuncDecl) { + if !hasFixInline(decl.Doc) { + return + } + content, err := a.readFile(decl) + if err != nil { + a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: cannot read source file: %v", err) + return + } + callee, err := inline.AnalyzeCallee(discard, a.pass.Fset, a.pass.Pkg, a.pass.TypesInfo, decl, content) + if err != nil { + a.pass.Reportf(decl.Doc.Pos(), "invalid inlining candidate: %v", err) + return + } + fn := a.pass.TypesInfo.Defs[decl.Name].(*types.Func) + a.pass.ExportObjectFact(fn, &goFixInlineFuncFact{callee}) + a.inlinableFuncs[fn] = callee +} + +func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) { + if !declInline && !hasFixInline(spec.Doc) { + return + } + if !spec.Assign.IsValid() { + a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") + return + } + if spec.TypeParams != nil { + // TODO(jba): handle generic aliases + return + } + // The alias must refer to another named type. + // TODO(jba): generalize to more type expressions. + var rhsID *ast.Ident + switch e := ast.Unparen(spec.Type).(type) { + case *ast.Ident: + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + return + } + lhs := a.pass.TypesInfo.Defs[spec.Name].(*types.TypeName) + // more (jba): test one alias pointing to another alias + rhs := a.pass.TypesInfo.Uses[rhsID].(*types.TypeName) + typ := &goFixInlineAliasFact{ + RHSName: rhs.Name(), + RHSPkgName: rhs.Pkg().Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + if rhs.Pkg() == a.pass.Pkg { + typ.rhsObj = rhs + } + a.inlinableAliases[lhs] = typ + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn about uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + a.pass.ExportObjectFact(lhs, typ) + } +} + +func (a *analyzer) findConst(spec *ast.ValueSpec, declInline bool) { + info := a.pass.TypesInfo + specInline := hasFixInline(spec.Doc) + if declInline || specInline { + for i, name := range spec.Names { + if i >= len(spec.Values) { + // Possible following an iota. + break + } + val := spec.Values[i] + var rhsID *ast.Ident + switch e := val.(type) { + case *ast.Ident: + // Constants defined with the predeclared iota cannot be inlined. + if info.Uses[e] == builtinIota { + a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is iota") + return } + rhsID = e + case *ast.SelectorExpr: + rhsID = e.Sel + default: + a.pass.Reportf(val.Pos(), "invalid //go:fix inline directive: const value is not the name of another constant") + return + } + lhs := info.Defs[name].(*types.Const) + rhs := info.Uses[rhsID].(*types.Const) // must be so in a well-typed program + con := &goFixInlineConstFact{ + RHSName: rhs.Name(), + RHSPkgName: rhs.Pkg().Name(), + RHSPkgPath: rhs.Pkg().Path(), + } + if rhs.Pkg() == a.pass.Pkg { + con.rhsObj = rhs + } + a.inlinableConsts[lhs] = con + // Create a fact only if the LHS is exported and defined at top level. + // We create a fact even if the RHS is non-exported, + // so we can warn about uses in other packages. + if lhs.Exported() && typesinternal.IsPackageLevel(lhs) { + a.pass.ExportObjectFact(lhs, con) } } } @@ -192,204 +204,209 @@ func (a *analyzer) find() { // // TODO(adonovan): handle multiple diffs that each add the same import. func (a *analyzer) inline() { - // Return the unique ast.File for a cursor. - currentFile := func(c cursor.Cursor) *ast.File { - cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) - return cf.Node().(*ast.File) - } - for cur := range a.root.Preorder((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { switch n := cur.Node().(type) { case *ast.CallExpr: - call := n - if fn := typeutil.StaticCallee(a.pass.TypesInfo, call); fn != nil { - // Inlinable? - callee, ok := a.inlinableFuncs[fn] - if !ok { - var fact goFixInlineFuncFact - if a.pass.ImportObjectFact(fn, &fact) { - callee = fact.Callee - a.inlinableFuncs[fn] = callee - } - } - if callee == nil { - continue // nope - } - - // Inline the call. - content, err := a.readFile(call) - if err != nil { - a.pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) - continue - } - curFile := currentFile(cur) - caller := &inline.Caller{ - Fset: a.pass.Fset, - Types: a.pass.Pkg, - Info: a.pass.TypesInfo, - File: curFile, - Call: call, - Content: content, - } - res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) - if err != nil { - a.pass.Reportf(call.Lparen, "%v", err) - continue - } - if res.Literalized { - // Users are not fond of inlinings that literalize - // f(x) to func() { ... }(), so avoid them. - // - // (Unfortunately the inliner is very timid, - // and often literalizes when it cannot prove that - // reducing the call is safe; the user of this tool - // has no indication of what the problem is.) - continue - } - got := res.Content - - // Suggest the "fix". - var textEdits []analysis.TextEdit - for _, edit := range diff.Bytes(content, got) { - textEdits = append(textEdits, analysis.TextEdit{ - Pos: curFile.FileStart + token.Pos(edit.Start), - End: curFile.FileStart + token.Pos(edit.End), - NewText: []byte(edit.New), - }) - } - a.pass.Report(analysis.Diagnostic{ - Pos: call.Pos(), - End: call.End(), - Message: fmt.Sprintf("Call of %v should be inlined", callee), - SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Inline call of %v", callee), - TextEdits: textEdits, - }}, - }) - } + a.inlineCall(n, cur) case *ast.Ident: - // If the identifier is a use of an inlinable type alias, suggest inlining it. - // TODO(jba): much of this code is shared with the constant case, below. - // Try to factor more of it out, unless it will change anyway when we move beyond simple RHS's. - if ali, ok := a.pass.TypesInfo.Uses[n].(*types.TypeName); ok { - inalias, ok := a.inlinableAliases[ali] - if !ok { - var fact goFixInlineAliasFact - if a.pass.ImportObjectFact(ali, &fact) { - inalias = &fact - a.inlinableAliases[ali] = inalias - } - } - if inalias == nil { - continue // nope - } - curFile := currentFile(cur) - - // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, - // where sel is the parent of X), // and an inlinable "type A = B" elsewhere (inali). - // Consider replacing A with B. - - // Check that the expression we are inlining (B) means the same thing - // (refers to the same object) in n's scope as it does in A's scope. - // If the RHS is not in the current package, AddImport will handle - // shadowing, so we only need to worry about when both expressions - // are in the current package. - if a.pass.Pkg.Path() == inalias.RHSPkgPath { - // fcon.rhsObj is the object referred to by B in the definition of A. - scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope - if obj == nil { - // Should be impossible: if code at n can refer to the LHS, - // it can refer to the RHS. - panic(fmt.Sprintf("no object for inlinable alias %s RHS %s", n.Name, inalias.RHSName)) - } - if obj != inalias.rhsObj { - // "B" means something different here than at the inlinable const's scope. - continue - } - } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), inalias.RHSPkgPath) { - // If this package can't see the RHS's package, we can't inline. - continue - } - var ( - importPrefix string - edits []analysis.TextEdit - ) - if inalias.RHSPkgPath != a.pass.Pkg.Path() { - _, importPrefix, edits = analysisinternal.AddImport( - a.pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) - } - // If n is qualified by a package identifier, we'll need the full selector expression. - var expr ast.Expr = n - if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { - expr = cur.Parent().Node().(ast.Expr) - } - a.reportInline("type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) - continue + switch t := a.pass.TypesInfo.Uses[n].(type) { + case *types.TypeName: + a.inlineAlias(t, cur) + case *types.Const: + a.inlineConst(t, cur) } - // If the identifier is a use of an inlinable constant, suggest inlining it. - if con, ok := a.pass.TypesInfo.Uses[n].(*types.Const); ok { - incon, ok := a.inlinableConsts[con] - if !ok { - var fact goFixInlineConstFact - if a.pass.ImportObjectFact(con, &fact) { - incon = &fact - a.inlinableConsts[con] = incon - } - } - if incon == nil { - continue // nope - } + } + } +} - // If n is qualified by a package identifier, we'll need the full selector expression. - curFile := currentFile(cur) - - // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, - // where sel is the parent of n), // and an inlinable "const A = B" elsewhere (incon). - // Consider replacing A with B. - - // Check that the expression we are inlining (B) means the same thing - // (refers to the same object) in n's scope as it does in A's scope. - // If the RHS is not in the current package, AddImport will handle - // shadowing, so we only need to worry about when both expressions - // are in the current package. - if a.pass.Pkg.Path() == incon.RHSPkgPath { - // incon.rhsObj is the object referred to by B in the definition of A. - scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope - if obj == nil { - // Should be impossible: if code at n can refer to the LHS, - // it can refer to the RHS. - panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName)) - } - if obj != incon.rhsObj { - // "B" means something different here than at the inlinable const's scope. - continue - } - } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), incon.RHSPkgPath) { - // If this package can't see the RHS's package, we can't inline. - continue - } - var ( - importPrefix string - edits []analysis.TextEdit - ) - if incon.RHSPkgPath != a.pass.Pkg.Path() { - _, importPrefix, edits = analysisinternal.AddImport( - a.pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) - } - // If n is qualified by a package identifier, we'll need the full selector expression. - var expr ast.Expr = n - if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { - expr = cur.Parent().Node().(ast.Expr) - } - a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName) +// If call is a call to an inlinable func, suggest inlining its use at cur. +func (a *analyzer) inlineCall(call *ast.CallExpr, cur cursor.Cursor) { + if fn := typeutil.StaticCallee(a.pass.TypesInfo, call); fn != nil { + // Inlinable? + callee, ok := a.inlinableFuncs[fn] + if !ok { + var fact goFixInlineFuncFact + if a.pass.ImportObjectFact(fn, &fact) { + callee = fact.Callee + a.inlinableFuncs[fn] = callee } } + if callee == nil { + return // nope + } + + // Inline the call. + content, err := a.readFile(call) + if err != nil { + a.pass.Reportf(call.Lparen, "invalid inlining candidate: cannot read source file: %v", err) + return + } + curFile := currentFile(cur) + caller := &inline.Caller{ + Fset: a.pass.Fset, + Types: a.pass.Pkg, + Info: a.pass.TypesInfo, + File: curFile, + Call: call, + Content: content, + } + res, err := inline.Inline(caller, callee, &inline.Options{Logf: discard}) + if err != nil { + a.pass.Reportf(call.Lparen, "%v", err) + return + } + if res.Literalized { + // Users are not fond of inlinings that literalize + // f(x) to func() { ... }(), so avoid them. + // + // (Unfortunately the inliner is very timid, + // and often literalizes when it cannot prove that + // reducing the call is safe; the user of this tool + // has no indication of what the problem is.) + return + } + got := res.Content + + // Suggest the "fix". + var textEdits []analysis.TextEdit + for _, edit := range diff.Bytes(content, got) { + textEdits = append(textEdits, analysis.TextEdit{ + Pos: curFile.FileStart + token.Pos(edit.Start), + End: curFile.FileStart + token.Pos(edit.End), + NewText: []byte(edit.New), + }) + } + a.pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Message: fmt.Sprintf("Call of %v should be inlined", callee), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Inline call of %v", callee), + TextEdits: textEdits, + }}, + }) } } +// If tn is the TypeName of an inlinable alias, suggest inlining its use at cur. +func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { + inalias, ok := a.inlinableAliases[tn] + if !ok { + var fact goFixInlineAliasFact + if a.pass.ImportObjectFact(tn, &fact) { + inalias = &fact + a.inlinableAliases[tn] = inalias + } + } + if inalias == nil { + return // nope + } + curFile := currentFile(cur) + + // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, + // where sel is the parent of X), // and an inlinable "type A = B" elsewhere (inali). + // Consider replacing A with B. + + // Check that the expression we are inlining (B) means the same thing + // (refers to the same object) in n's scope as it does in A's scope. + // If the RHS is not in the current package, AddImport will handle + // shadowing, so we only need to worry about when both expressions + // are in the current package. + n := cur.Node().(*ast.Ident) + if a.pass.Pkg.Path() == inalias.RHSPkgPath { + // fcon.rhsObj is the object referred to by B in the definition of A. + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope + if obj == nil { + // Should be impossible: if code at n can refer to the LHS, + // it can refer to the RHS. + panic(fmt.Sprintf("no object for inlinable alias %s RHS %s", n.Name, inalias.RHSName)) + } + if obj != inalias.rhsObj { + // "B" means something different here than at the inlinable const's scope. + return + } + } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), inalias.RHSPkgPath) { + // If this package can't see the RHS's package, we can't inline. + return + } + var ( + importPrefix string + edits []analysis.TextEdit + ) + if inalias.RHSPkgPath != a.pass.Pkg.Path() { + _, importPrefix, edits = analysisinternal.AddImport( + a.pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) + } + // If n is qualified by a package identifier, we'll need the full selector expression. + var expr ast.Expr = n + if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + expr = cur.Parent().Node().(ast.Expr) + } + a.reportInline("type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) +} + +// If con is an inlinable constant, suggest inlining its use at cur. +func (a *analyzer) inlineConst(con *types.Const, cur cursor.Cursor) { + incon, ok := a.inlinableConsts[con] + if !ok { + var fact goFixInlineConstFact + if a.pass.ImportObjectFact(con, &fact) { + incon = &fact + a.inlinableConsts[con] = incon + } + } + if incon == nil { + return // nope + } + + // If n is qualified by a package identifier, we'll need the full selector expression. + curFile := currentFile(cur) + n := cur.Node().(*ast.Ident) + + // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, + // where sel is the parent of n), // and an inlinable "const A = B" elsewhere (incon). + // Consider replacing A with B. + + // Check that the expression we are inlining (B) means the same thing + // (refers to the same object) in n's scope as it does in A's scope. + // If the RHS is not in the current package, AddImport will handle + // shadowing, so we only need to worry about when both expressions + // are in the current package. + if a.pass.Pkg.Path() == incon.RHSPkgPath { + // incon.rhsObj is the object referred to by B in the definition of A. + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope + if obj == nil { + // Should be impossible: if code at n can refer to the LHS, + // it can refer to the RHS. + panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName)) + } + if obj != incon.rhsObj { + // "B" means something different here than at the inlinable const's scope. + return + } + } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), incon.RHSPkgPath) { + // If this package can't see the RHS's package, we can't inline. + return + } + var ( + importPrefix string + edits []analysis.TextEdit + ) + if incon.RHSPkgPath != a.pass.Pkg.Path() { + _, importPrefix, edits = analysisinternal.AddImport( + a.pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos()) + } + // If n is qualified by a package identifier, we'll need the full selector expression. + var expr ast.Expr = n + if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + expr = cur.Parent().Node().(ast.Expr) + } + a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName) +} + // reportInline reports a diagnostic for fixing an inlinable name. func (a *analyzer) reportInline(kind, capKind string, ident ast.Expr, edits []analysis.TextEdit, newText string) { edits = append(edits, analysis.TextEdit{ @@ -423,6 +440,12 @@ func (a *analyzer) readFile(node ast.Node) ([]byte, error) { return content, nil } +// currentFile returns the unique ast.File for a cursor. +func currentFile(c cursor.Cursor) *ast.File { + cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) + return cf.Node().(*ast.File) +} + // hasFixInline reports the presence of a "//go:fix inline" directive // in the comments. func hasFixInline(cg *ast.CommentGroup) bool { From ead62e94e2a9fcb4562a875cad4b3308b2d616ac Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 15 Feb 2025 08:59:08 -0500 Subject: [PATCH 152/309] gopls/internal/analysis/modernize: handle parens In the maps modernizer, consider the possibility of parentheses surrounding some bits of syntax. Change-Id: I395de81b99f2e9b47dca7f4bbfbed66c0772b6f6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649975 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/modernize/maps.go | 4 ++-- .../testdata/src/mapsloop/mapsloop.go | 19 +++++++++++++++++++ .../testdata/src/mapsloop/mapsloop.go.golden | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index c93899621ef..91de659d107 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -87,9 +87,9 @@ func mapsloop(pass *analysis.Pass) { // Have: m = rhs; for k, v := range x { m[k] = v } var newMap bool rhs := assign.Rhs[0] - switch rhs := rhs.(type) { + switch rhs := ast.Unparen(rhs).(type) { case *ast.CallExpr: - if id, ok := rhs.Fun.(*ast.Ident); ok && + if id, ok := ast.Unparen(rhs.Fun).(*ast.Ident); ok && info.Uses[id] == builtinMake { // Have: m = make(...) newMap = true diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go index 769b4c84f60..e4e6963dbae 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go @@ -33,6 +33,25 @@ func useClone(src map[int]string) { for key, value := range src { dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" } + + dst = map[int]string{} + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + } + println(dst) +} + +func useCloneParen(src map[int]string) { + // Replace (make)(...) by maps.Clone. + dst := (make)(map[int]string, len(src)) + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + } + + dst = (map[int]string{}) + for key, value := range src { + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + } println(dst) } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden index b9aa39021e8..70b9a28ed5b 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden @@ -26,6 +26,16 @@ func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { func useClone(src map[int]string) { // Replace make(...) by maps.Clone. dst := maps.Clone(src) + + dst = maps.Clone(src) + println(dst) +} + +func useCloneParen(src map[int]string) { + // Replace (make)(...) by maps.Clone. + dst := maps.Clone(src) + + dst = maps.Clone(src) println(dst) } From 94db7107945332a789135d458a0f0de1d7c00ddb Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Fri, 14 Feb 2025 21:17:29 +0000 Subject: [PATCH 153/309] all: upgrade go directive to at least 1.23.0 [generated] By now Go 1.24.0 has been released, and Go 1.22 is no longer supported per the Go Release Policy (https://go.dev/doc/devel/release#policy). For golang/go#69095. [git-generate] (cd . && go get go@1.23.0 && go mod tidy && go fix ./... && go mod edit -toolchain=none) (cd gopls/doc/assets && go get go@1.23.0 && go mod tidy && go fix ./... && go mod edit -toolchain=none) (cd gopls && echo 'skipping because it already has go1.23.4 >= go1.23.0, nothing to do') Change-Id: I37dad9abd1457a8a8aa940809b7ee6664fba006d Reviewed-on: https://go-review.googlesource.com/c/tools/+/649321 Auto-Submit: Gopher Robot LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: Cherry Mui Reviewed-by: Robert Findley --- cmd/callgraph/main_test.go | 1 - cmd/fiximports/main_test.go | 1 - cmd/godex/isAlias18.go | 1 - cmd/godex/isAlias19.go | 1 - cmd/goimports/goimports_gc.go | 1 - cmd/goimports/goimports_not_gc.go | 1 - cmd/gotype/sizesFor18.go | 1 - cmd/gotype/sizesFor19.go | 1 - cmd/splitdwarf/splitdwarf.go | 1 - cmd/stress/stress.go | 1 - go.mod | 2 +- go/analysis/multichecker/multichecker_test.go | 1 - go/analysis/passes/errorsas/errorsas_test.go | 1 - go/analysis/passes/stdversion/main.go | 1 - go/analysis/unitchecker/main.go | 1 - go/buildutil/allpackages_test.go | 1 - go/callgraph/cha/cha_test.go | 1 - go/callgraph/rta/rta_test.go | 1 - go/callgraph/vta/internal/trie/bits_test.go | 1 - go/callgraph/vta/internal/trie/trie_test.go | 1 - go/gcexportdata/example_test.go | 5 ----- go/gcexportdata/main.go | 1 - go/internal/gccgoimporter/newInterface10.go | 1 - go/internal/gccgoimporter/newInterface11.go | 1 - go/loader/loader_test.go | 1 - go/ssa/example_test.go | 3 --- go/ssa/ssautil/switch_test.go | 1 - go/ssa/stdlib_test.go | 1 - godoc/godoc17_test.go | 1 - godoc/static/makestatic.go | 1 - godoc/vfs/fs.go | 1 - gopls/doc/assets/go.mod | 2 +- internal/gcimporter/israce_test.go | 1 - internal/imports/mkindex.go | 1 - internal/jsonrpc2_v2/serve_go116.go | 1 - internal/jsonrpc2_v2/serve_pre116.go | 1 - internal/pprof/main.go | 1 - internal/robustio/copyfiles.go | 1 - internal/robustio/robustio_flaky.go | 1 - internal/robustio/robustio_other.go | 1 - internal/robustio/robustio_plan9.go | 1 - internal/robustio/robustio_posix.go | 1 - internal/stdlib/generate.go | 1 - internal/testenv/testenv_notunix.go | 1 - internal/testenv/testenv_unix.go | 1 - internal/typeparams/copytermlist.go | 1 - playground/socket/socket.go | 1 - refactor/eg/eg_test.go | 1 - refactor/importgraph/graph_test.go | 1 - 49 files changed, 2 insertions(+), 55 deletions(-) diff --git a/cmd/callgraph/main_test.go b/cmd/callgraph/main_test.go index ce634139e68..3b56cd7ffef 100644 --- a/cmd/callgraph/main_test.go +++ b/cmd/callgraph/main_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android && go1.11 -// +build !android,go1.11 package main diff --git a/cmd/fiximports/main_test.go b/cmd/fiximports/main_test.go index ebbd7520d2e..69f8726f135 100644 --- a/cmd/fiximports/main_test.go +++ b/cmd/fiximports/main_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package main diff --git a/cmd/godex/isAlias18.go b/cmd/godex/isAlias18.go index 431602b2243..f1f78731d4c 100644 --- a/cmd/godex/isAlias18.go +++ b/cmd/godex/isAlias18.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !go1.9 -// +build !go1.9 package main diff --git a/cmd/godex/isAlias19.go b/cmd/godex/isAlias19.go index e5889119fa1..db29555fd8c 100644 --- a/cmd/godex/isAlias19.go +++ b/cmd/godex/isAlias19.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.9 -// +build go1.9 package main diff --git a/cmd/goimports/goimports_gc.go b/cmd/goimports/goimports_gc.go index 3326646d035..3a88482fe8d 100644 --- a/cmd/goimports/goimports_gc.go +++ b/cmd/goimports/goimports_gc.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build gc -// +build gc package main diff --git a/cmd/goimports/goimports_not_gc.go b/cmd/goimports/goimports_not_gc.go index 344fe7576b0..21dc77920be 100644 --- a/cmd/goimports/goimports_not_gc.go +++ b/cmd/goimports/goimports_not_gc.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !gc -// +build !gc package main diff --git a/cmd/gotype/sizesFor18.go b/cmd/gotype/sizesFor18.go index 39e3d9f047e..15d2355ca42 100644 --- a/cmd/gotype/sizesFor18.go +++ b/cmd/gotype/sizesFor18.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !go1.9 -// +build !go1.9 // This file contains a copy of the implementation of types.SizesFor // since this function is not available in go/types before Go 1.9. diff --git a/cmd/gotype/sizesFor19.go b/cmd/gotype/sizesFor19.go index 34181c8d04d..c46bb777024 100644 --- a/cmd/gotype/sizesFor19.go +++ b/cmd/gotype/sizesFor19.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.9 -// +build go1.9 package main diff --git a/cmd/splitdwarf/splitdwarf.go b/cmd/splitdwarf/splitdwarf.go index 90ff10b6a05..24aa239226c 100644 --- a/cmd/splitdwarf/splitdwarf.go +++ b/cmd/splitdwarf/splitdwarf.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd -// +build aix darwin dragonfly freebsd linux netbsd openbsd /* Splitdwarf uncompresses and copies the DWARF segment of a Mach-O diff --git a/cmd/stress/stress.go b/cmd/stress/stress.go index 6472064f933..e8b8641b387 100644 --- a/cmd/stress/stress.go +++ b/cmd/stress/stress.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || windows -// +build unix aix darwin dragonfly freebsd linux netbsd openbsd solaris windows // The stress utility is intended for catching sporadic failures. // It runs a given process in parallel in a loop and collects any failures. diff --git a/go.mod b/go.mod index 8cea866daf8..9edfc58936d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module golang.org/x/tools -go 1.22.0 // => default GODEBUG has gotypesalias=0 +go 1.23.0 // => default GODEBUG has gotypesalias=0 require ( github.com/google/go-cmp v0.6.0 diff --git a/go/analysis/multichecker/multichecker_test.go b/go/analysis/multichecker/multichecker_test.go index 07bf977369b..94a280564ce 100644 --- a/go/analysis/multichecker/multichecker_test.go +++ b/go/analysis/multichecker/multichecker_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.12 -// +build go1.12 package multichecker_test diff --git a/go/analysis/passes/errorsas/errorsas_test.go b/go/analysis/passes/errorsas/errorsas_test.go index 6689d8114a7..5414f9e8b6d 100644 --- a/go/analysis/passes/errorsas/errorsas_test.go +++ b/go/analysis/passes/errorsas/errorsas_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.13 -// +build go1.13 package errorsas_test diff --git a/go/analysis/passes/stdversion/main.go b/go/analysis/passes/stdversion/main.go index 2156d41e4a9..bf1c3a0b31f 100644 --- a/go/analysis/passes/stdversion/main.go +++ b/go/analysis/passes/stdversion/main.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore package main diff --git a/go/analysis/unitchecker/main.go b/go/analysis/unitchecker/main.go index 4374e7bf945..246be909249 100644 --- a/go/analysis/unitchecker/main.go +++ b/go/analysis/unitchecker/main.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // This file provides an example command for static checkers // conforming to the golang.org/x/tools/go/analysis API. diff --git a/go/buildutil/allpackages_test.go b/go/buildutil/allpackages_test.go index 6af86771104..2df5f27e223 100644 --- a/go/buildutil/allpackages_test.go +++ b/go/buildutil/allpackages_test.go @@ -5,7 +5,6 @@ // Incomplete source tree on Android. //go:build !android -// +build !android package buildutil_test diff --git a/go/callgraph/cha/cha_test.go b/go/callgraph/cha/cha_test.go index 5ac64e17244..7795cb44de0 100644 --- a/go/callgraph/cha/cha_test.go +++ b/go/callgraph/cha/cha_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package cha_test diff --git a/go/callgraph/rta/rta_test.go b/go/callgraph/rta/rta_test.go index 74e77b01291..dcec98d7c5d 100644 --- a/go/callgraph/rta/rta_test.go +++ b/go/callgraph/rta/rta_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package rta_test diff --git a/go/callgraph/vta/internal/trie/bits_test.go b/go/callgraph/vta/internal/trie/bits_test.go index 07784cdffac..f6e510eccd0 100644 --- a/go/callgraph/vta/internal/trie/bits_test.go +++ b/go/callgraph/vta/internal/trie/bits_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.13 -// +build go1.13 package trie diff --git a/go/callgraph/vta/internal/trie/trie_test.go b/go/callgraph/vta/internal/trie/trie_test.go index c0651b0ef86..71fd398f12c 100644 --- a/go/callgraph/vta/internal/trie/trie_test.go +++ b/go/callgraph/vta/internal/trie/trie_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.13 -// +build go1.13 package trie diff --git a/go/gcexportdata/example_test.go b/go/gcexportdata/example_test.go index 9574f30d32b..852ba5a597c 100644 --- a/go/gcexportdata/example_test.go +++ b/go/gcexportdata/example_test.go @@ -3,11 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.7 && gc && !android && !ios && (unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || plan9 || windows) -// +build go1.7 -// +build gc -// +build !android -// +build !ios -// +build unix aix darwin dragonfly freebsd linux netbsd openbsd solaris plan9 windows package gcexportdata_test diff --git a/go/gcexportdata/main.go b/go/gcexportdata/main.go index e9df4e9a9a5..0b267e33867 100644 --- a/go/gcexportdata/main.go +++ b/go/gcexportdata/main.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // The gcexportdata command is a diagnostic tool that displays the // contents of gc export data files. diff --git a/go/internal/gccgoimporter/newInterface10.go b/go/internal/gccgoimporter/newInterface10.go index 1b449ef9886..f49c9b067dd 100644 --- a/go/internal/gccgoimporter/newInterface10.go +++ b/go/internal/gccgoimporter/newInterface10.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !go1.11 -// +build !go1.11 package gccgoimporter diff --git a/go/internal/gccgoimporter/newInterface11.go b/go/internal/gccgoimporter/newInterface11.go index 631546ec66f..c7d5edb4858 100644 --- a/go/internal/gccgoimporter/newInterface11.go +++ b/go/internal/gccgoimporter/newInterface11.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.11 -// +build go1.11 package gccgoimporter diff --git a/go/loader/loader_test.go b/go/loader/loader_test.go index 2276b49ad6f..eb9feb221f0 100644 --- a/go/loader/loader_test.go +++ b/go/loader/loader_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package loader_test diff --git a/go/ssa/example_test.go b/go/ssa/example_test.go index e0fba0be681..03775414df2 100644 --- a/go/ssa/example_test.go +++ b/go/ssa/example_test.go @@ -3,9 +3,6 @@ // license that can be found in the LICENSE file. //go:build !android && !ios && (unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || plan9 || windows) -// +build !android -// +build !ios -// +build unix aix darwin dragonfly freebsd linux netbsd openbsd solaris plan9 windows package ssa_test diff --git a/go/ssa/ssautil/switch_test.go b/go/ssa/ssautil/switch_test.go index 081b09010ee..6ff5c9b92c3 100644 --- a/go/ssa/ssautil/switch_test.go +++ b/go/ssa/ssautil/switch_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package ssautil_test diff --git a/go/ssa/stdlib_test.go b/go/ssa/stdlib_test.go index 9b78cfbf839..08df50b9eeb 100644 --- a/go/ssa/stdlib_test.go +++ b/go/ssa/stdlib_test.go @@ -5,7 +5,6 @@ // Incomplete source tree on Android. //go:build !android -// +build !android package ssa_test diff --git a/godoc/godoc17_test.go b/godoc/godoc17_test.go index 82e23e64775..c8bf2d96d42 100644 --- a/godoc/godoc17_test.go +++ b/godoc/godoc17_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.7 -// +build go1.7 package godoc diff --git a/godoc/static/makestatic.go b/godoc/static/makestatic.go index a8a652f8ed5..5a7337290ff 100644 --- a/godoc/static/makestatic.go +++ b/godoc/static/makestatic.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // Command makestatic writes the generated file buffer to "static.go". // It is intended to be invoked via "go generate" (directive in "gen.go"). diff --git a/godoc/vfs/fs.go b/godoc/vfs/fs.go index f12d653fef2..2bec5886052 100644 --- a/godoc/vfs/fs.go +++ b/godoc/vfs/fs.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.16 -// +build go1.16 package vfs diff --git a/gopls/doc/assets/go.mod b/gopls/doc/assets/go.mod index 73f49695726..9b417f19ed8 100644 --- a/gopls/doc/assets/go.mod +++ b/gopls/doc/assets/go.mod @@ -4,4 +4,4 @@ module golang.org/x/tools/gopls/doc/assets -go 1.19 +go 1.23.0 diff --git a/internal/gcimporter/israce_test.go b/internal/gcimporter/israce_test.go index 885ba1c01c5..c75a16b7a1b 100644 --- a/internal/gcimporter/israce_test.go +++ b/internal/gcimporter/israce_test.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build race -// +build race package gcimporter_test diff --git a/internal/imports/mkindex.go b/internal/imports/mkindex.go index ff006b0cd2e..10e8da5243d 100644 --- a/internal/imports/mkindex.go +++ b/internal/imports/mkindex.go @@ -1,5 +1,4 @@ //go:build ignore -// +build ignore // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/internal/jsonrpc2_v2/serve_go116.go b/internal/jsonrpc2_v2/serve_go116.go index 2dac7413f31..19114502d1c 100644 --- a/internal/jsonrpc2_v2/serve_go116.go +++ b/internal/jsonrpc2_v2/serve_go116.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build go1.16 -// +build go1.16 package jsonrpc2 diff --git a/internal/jsonrpc2_v2/serve_pre116.go b/internal/jsonrpc2_v2/serve_pre116.go index ef5477fecb9..9e8ece2ea7b 100644 --- a/internal/jsonrpc2_v2/serve_pre116.go +++ b/internal/jsonrpc2_v2/serve_pre116.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !go1.16 -// +build !go1.16 package jsonrpc2 diff --git a/internal/pprof/main.go b/internal/pprof/main.go index 5e1ae633b4d..42aa187a6a7 100644 --- a/internal/pprof/main.go +++ b/internal/pprof/main.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // The pprof command prints the total time in a pprof profile provided // through the standard input. diff --git a/internal/robustio/copyfiles.go b/internal/robustio/copyfiles.go index 8c93fcd7163..8aace49da8b 100644 --- a/internal/robustio/copyfiles.go +++ b/internal/robustio/copyfiles.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // The copyfiles script copies the contents of the internal cmd/go robustio // package to the current directory, with adjustments to make it build. diff --git a/internal/robustio/robustio_flaky.go b/internal/robustio/robustio_flaky.go index d5c241857b4..c56e36ca624 100644 --- a/internal/robustio/robustio_flaky.go +++ b/internal/robustio/robustio_flaky.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build windows || darwin -// +build windows darwin package robustio diff --git a/internal/robustio/robustio_other.go b/internal/robustio/robustio_other.go index 3a20cac6cf8..da9a46e4fac 100644 --- a/internal/robustio/robustio_other.go +++ b/internal/robustio/robustio_other.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !windows && !darwin -// +build !windows,!darwin package robustio diff --git a/internal/robustio/robustio_plan9.go b/internal/robustio/robustio_plan9.go index 9fa4cacb5a3..3026b9f6321 100644 --- a/internal/robustio/robustio_plan9.go +++ b/internal/robustio/robustio_plan9.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build plan9 -// +build plan9 package robustio diff --git a/internal/robustio/robustio_posix.go b/internal/robustio/robustio_posix.go index cf74865d0b5..6b4beec96fc 100644 --- a/internal/robustio/robustio_posix.go +++ b/internal/robustio/robustio_posix.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !windows && !plan9 -// +build !windows,!plan9 package robustio diff --git a/internal/stdlib/generate.go b/internal/stdlib/generate.go index d4964f60955..1192885405c 100644 --- a/internal/stdlib/generate.go +++ b/internal/stdlib/generate.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // The generate command reads all the GOROOT/api/go1.*.txt files and // generates a single combined manifest.go file containing the Go diff --git a/internal/testenv/testenv_notunix.go b/internal/testenv/testenv_notunix.go index e9ce0d3649d..85b3820e3fb 100644 --- a/internal/testenv/testenv_notunix.go +++ b/internal/testenv/testenv_notunix.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !(unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris) -// +build !unix,!aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris package testenv diff --git a/internal/testenv/testenv_unix.go b/internal/testenv/testenv_unix.go index bc6af1ff81d..d635b96b31b 100644 --- a/internal/testenv/testenv_unix.go +++ b/internal/testenv/testenv_unix.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris -// +build unix aix darwin dragonfly freebsd linux netbsd openbsd solaris package testenv diff --git a/internal/typeparams/copytermlist.go b/internal/typeparams/copytermlist.go index 5357f9d2fd7..1edaaa01c9a 100644 --- a/internal/typeparams/copytermlist.go +++ b/internal/typeparams/copytermlist.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build ignore -// +build ignore // copytermlist.go copies the term list algorithm from GOROOT/src/go/types. diff --git a/playground/socket/socket.go b/playground/socket/socket.go index 9e5b4a954d2..378edd4c3a5 100644 --- a/playground/socket/socket.go +++ b/playground/socket/socket.go @@ -3,7 +3,6 @@ // license that can be found in the LICENSE file. //go:build !appengine -// +build !appengine // Package socket implements a WebSocket-based playground backend. // Clients connect to a websocket handler and send run/kill commands, and diff --git a/refactor/eg/eg_test.go b/refactor/eg/eg_test.go index eb54f0b3f95..4dc24f53358 100644 --- a/refactor/eg/eg_test.go +++ b/refactor/eg/eg_test.go @@ -5,7 +5,6 @@ // No testdata on Android. //go:build !android -// +build !android package eg_test diff --git a/refactor/importgraph/graph_test.go b/refactor/importgraph/graph_test.go index f3378a41e86..a07cc633454 100644 --- a/refactor/importgraph/graph_test.go +++ b/refactor/importgraph/graph_test.go @@ -5,7 +5,6 @@ // Incomplete std lib sources on Android. //go:build !android -// +build !android package importgraph_test From c18bffa1b03c9346f0dd1af830b09979e63c7b5f Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sun, 16 Feb 2025 02:16:29 -0500 Subject: [PATCH 154/309] all: delete redundant //go:debug gotypesalias=1 directives [generated] The module's go directive is >= 1.23 now, so these files no longer have any effect. Also drop the outdated comment in go.mod. For golang/go#69772. [git-generate] find . -type f -name gotypesalias.go -exec rm {} + sed -i '' 's| // => default GODEBUG has gotypesalias=0||' go.mod Change-Id: Id0fa54f89695991de6c8721aadecfd587826d158 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650095 Reviewed-by: Cherry Mui Auto-Submit: Dmitri Shuralyov Reviewed-by: Funda Secgin Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- cmd/bundle/gotypesalias.go | 12 ------------ cmd/callgraph/gotypesalias.go | 12 ------------ cmd/deadcode/gotypesalias.go | 12 ------------ cmd/eg/gotypesalias.go | 12 ------------ cmd/godex/gotypesalias.go | 12 ------------ cmd/godoc/gotypesalias.go | 12 ------------ cmd/goimports/gotypesalias.go | 12 ------------ cmd/gomvpkg/gotypesalias.go | 12 ------------ cmd/gotype/gotypesalias.go | 12 ------------ cmd/ssadump/gotypesalias.go | 12 ------------ cmd/stringer/gotypesalias.go | 12 ------------ go.mod | 2 +- go/analysis/passes/defers/cmd/defers/gotypesalias.go | 12 ------------ .../cmd/fieldalignment/gotypesalias.go | 12 ------------ .../passes/findcall/cmd/findcall/gotypesalias.go | 12 ------------ .../passes/httpmux/cmd/httpmux/gotypesalias.go | 12 ------------ .../ifaceassert/cmd/ifaceassert/gotypesalias.go | 12 ------------ .../passes/lostcancel/cmd/lostcancel/gotypesalias.go | 12 ------------ .../passes/nilness/cmd/nilness/gotypesalias.go | 12 ------------ go/analysis/passes/shadow/cmd/shadow/gotypesalias.go | 12 ------------ .../stringintconv/cmd/stringintconv/gotypesalias.go | 12 ------------ .../passes/unmarshal/cmd/unmarshal/gotypesalias.go | 12 ------------ .../unusedresult/cmd/unusedresult/gotypesalias.go | 12 ------------ go/packages/gopackages/gotypesalias.go | 12 ------------ go/packages/internal/nodecount/gotypesalias.go | 12 ------------ go/types/internal/play/gotypesalias.go | 12 ------------ 26 files changed, 1 insertion(+), 301 deletions(-) delete mode 100644 cmd/bundle/gotypesalias.go delete mode 100644 cmd/callgraph/gotypesalias.go delete mode 100644 cmd/deadcode/gotypesalias.go delete mode 100644 cmd/eg/gotypesalias.go delete mode 100644 cmd/godex/gotypesalias.go delete mode 100644 cmd/godoc/gotypesalias.go delete mode 100644 cmd/goimports/gotypesalias.go delete mode 100644 cmd/gomvpkg/gotypesalias.go delete mode 100644 cmd/gotype/gotypesalias.go delete mode 100644 cmd/ssadump/gotypesalias.go delete mode 100644 cmd/stringer/gotypesalias.go delete mode 100644 go/analysis/passes/defers/cmd/defers/gotypesalias.go delete mode 100644 go/analysis/passes/fieldalignment/cmd/fieldalignment/gotypesalias.go delete mode 100644 go/analysis/passes/findcall/cmd/findcall/gotypesalias.go delete mode 100644 go/analysis/passes/httpmux/cmd/httpmux/gotypesalias.go delete mode 100644 go/analysis/passes/ifaceassert/cmd/ifaceassert/gotypesalias.go delete mode 100644 go/analysis/passes/lostcancel/cmd/lostcancel/gotypesalias.go delete mode 100644 go/analysis/passes/nilness/cmd/nilness/gotypesalias.go delete mode 100644 go/analysis/passes/shadow/cmd/shadow/gotypesalias.go delete mode 100644 go/analysis/passes/stringintconv/cmd/stringintconv/gotypesalias.go delete mode 100644 go/analysis/passes/unmarshal/cmd/unmarshal/gotypesalias.go delete mode 100644 go/analysis/passes/unusedresult/cmd/unusedresult/gotypesalias.go delete mode 100644 go/packages/gopackages/gotypesalias.go delete mode 100644 go/packages/internal/nodecount/gotypesalias.go delete mode 100644 go/types/internal/play/gotypesalias.go diff --git a/cmd/bundle/gotypesalias.go b/cmd/bundle/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/bundle/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/callgraph/gotypesalias.go b/cmd/callgraph/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/callgraph/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/deadcode/gotypesalias.go b/cmd/deadcode/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/deadcode/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/eg/gotypesalias.go b/cmd/eg/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/eg/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/godex/gotypesalias.go b/cmd/godex/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/godex/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/godoc/gotypesalias.go b/cmd/godoc/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/godoc/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/goimports/gotypesalias.go b/cmd/goimports/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/goimports/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/gomvpkg/gotypesalias.go b/cmd/gomvpkg/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/gomvpkg/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/gotype/gotypesalias.go b/cmd/gotype/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/gotype/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/ssadump/gotypesalias.go b/cmd/ssadump/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/ssadump/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/cmd/stringer/gotypesalias.go b/cmd/stringer/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/cmd/stringer/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go.mod b/go.mod index 9edfc58936d..bc7636b4cf8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module golang.org/x/tools -go 1.23.0 // => default GODEBUG has gotypesalias=0 +go 1.23.0 require ( github.com/google/go-cmp v0.6.0 diff --git a/go/analysis/passes/defers/cmd/defers/gotypesalias.go b/go/analysis/passes/defers/cmd/defers/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/defers/cmd/defers/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/fieldalignment/cmd/fieldalignment/gotypesalias.go b/go/analysis/passes/fieldalignment/cmd/fieldalignment/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/fieldalignment/cmd/fieldalignment/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/findcall/cmd/findcall/gotypesalias.go b/go/analysis/passes/findcall/cmd/findcall/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/findcall/cmd/findcall/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/httpmux/cmd/httpmux/gotypesalias.go b/go/analysis/passes/httpmux/cmd/httpmux/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/httpmux/cmd/httpmux/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/ifaceassert/cmd/ifaceassert/gotypesalias.go b/go/analysis/passes/ifaceassert/cmd/ifaceassert/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/ifaceassert/cmd/ifaceassert/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/lostcancel/cmd/lostcancel/gotypesalias.go b/go/analysis/passes/lostcancel/cmd/lostcancel/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/lostcancel/cmd/lostcancel/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/nilness/cmd/nilness/gotypesalias.go b/go/analysis/passes/nilness/cmd/nilness/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/nilness/cmd/nilness/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/shadow/cmd/shadow/gotypesalias.go b/go/analysis/passes/shadow/cmd/shadow/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/shadow/cmd/shadow/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/stringintconv/cmd/stringintconv/gotypesalias.go b/go/analysis/passes/stringintconv/cmd/stringintconv/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/stringintconv/cmd/stringintconv/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/unmarshal/cmd/unmarshal/gotypesalias.go b/go/analysis/passes/unmarshal/cmd/unmarshal/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/unmarshal/cmd/unmarshal/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/analysis/passes/unusedresult/cmd/unusedresult/gotypesalias.go b/go/analysis/passes/unusedresult/cmd/unusedresult/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/analysis/passes/unusedresult/cmd/unusedresult/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/packages/gopackages/gotypesalias.go b/go/packages/gopackages/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/packages/gopackages/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/packages/internal/nodecount/gotypesalias.go b/go/packages/internal/nodecount/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/packages/internal/nodecount/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). diff --git a/go/types/internal/play/gotypesalias.go b/go/types/internal/play/gotypesalias.go deleted file mode 100644 index 288c10c2d0a..00000000000 --- a/go/types/internal/play/gotypesalias.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.23 - -//go:debug gotypesalias=1 - -package main - -// Materialize aliases whenever the go toolchain version is after 1.23 (#69772). -// Remove this file after go.mod >= 1.23 (which implies gotypesalias=1). From d115b345e2022d300181300e01d379c4f65da9f6 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 17 Feb 2025 14:24:33 -0500 Subject: [PATCH 155/309] gopls/internal/analysis: simplify type-error analyzers with Cursor This CL makes a number of simplifications to the type-error analyzers: - Nodes are found using Cursor.FindPos from the error position, which is very fast; - Error position information is read from types.Error instead of formatting the ast.File (!) then invoking the dubious heuristics of analysisinternal.TypeErrorEndPos, which scans the text (!!) assuming well-formattedness (!!!). - plus various minor cleanups. - rename typesinternal.ReadGo116ErrorData to ErrorCodeStartEnd. Updates golang/go#65966 Updates golang/go#71803 Change-Id: Ie4561144040b001b957ef6a3c3631328297d5001 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650217 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan --- .../analysis/fillreturns/fillreturns.go | 147 +++++++----------- .../internal/analysis/nonewvars/nonewvars.go | 81 +++++----- .../analysis/noresultvalues/noresultvalues.go | 61 +++----- gopls/internal/cache/check.go | 5 +- gopls/internal/golang/codeaction.go | 2 +- .../marker/testdata/highlight/controlflow.txt | 3 +- internal/analysisinternal/analysis.go | 2 + internal/typesinternal/types.go | 6 +- 8 files changed, 130 insertions(+), 177 deletions(-) diff --git a/gopls/internal/analysis/fillreturns/fillreturns.go b/gopls/internal/analysis/fillreturns/fillreturns.go index 8a602dc2eef..b6bcc1f24dc 100644 --- a/gopls/internal/analysis/fillreturns/fillreturns.go +++ b/gopls/internal/analysis/fillreturns/fillreturns.go @@ -15,9 +15,12 @@ import ( "strings" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/gopls/internal/fuzzy" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) @@ -27,105 +30,41 @@ var doc string var Analyzer = &analysis.Analyzer{ Name: "fillreturns", Doc: analysisinternal.MustExtractDoc(doc, "fillreturns"), + Requires: []*analysis.Analyzer{inspect.Analyzer}, Run: run, RunDespiteErrors: true, URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/fillreturns", } func run(pass *analysis.Pass) (interface{}, error) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) info := pass.TypesInfo - if info == nil { - return nil, fmt.Errorf("nil TypeInfo") - } outer: for _, typeErr := range pass.TypeErrors { - // Filter out the errors that are not relevant to this analyzer. - if !FixesError(typeErr) { - continue - } - var file *ast.File - for _, f := range pass.Files { - if f.FileStart <= typeErr.Pos && typeErr.Pos <= f.FileEnd { - file = f - break - } - } - if file == nil { - continue - } - - // Get the end position of the error. - // (This heuristic assumes that the buffer is formatted, - // at least up to the end position of the error.) - var buf bytes.Buffer - if err := format.Node(&buf, pass.Fset, file); err != nil { - continue + if !fixesError(typeErr) { + continue // irrelevant type error } - typeErrEndPos := analysisinternal.TypeErrorEndPos(pass.Fset, buf.Bytes(), typeErr.Pos) - - // TODO(rfindley): much of the error handling code below returns, when it - // should probably continue. - - // Get the path for the relevant range. - path, _ := astutil.PathEnclosingInterval(file, typeErr.Pos, typeErrEndPos) - if len(path) == 0 { - return nil, nil - } - - // Find the enclosing return statement. - var ret *ast.ReturnStmt - var retIdx int - for i, n := range path { - if r, ok := n.(*ast.ReturnStmt); ok { - ret = r - retIdx = i - break - } + _, start, end, ok := typesinternal.ErrorCodeStartEnd(typeErr) + if !ok { + continue // no position information } - if ret == nil { - return nil, nil + curErr, ok := cursor.Root(inspect).FindPos(start, end) + if !ok { + continue // can't find node } - // Get the function type that encloses the ReturnStmt. - var enclosingFunc *ast.FuncType - for _, n := range path[retIdx+1:] { - switch node := n.(type) { - case *ast.FuncLit: - enclosingFunc = node.Type - case *ast.FuncDecl: - enclosingFunc = node.Type - } - if enclosingFunc != nil { - break - } - } - if enclosingFunc == nil || enclosingFunc.Results == nil { - continue - } - - // Skip any generic enclosing functions, since type parameters don't - // have 0 values. - // TODO(rfindley): We should be able to handle this if the return - // values are all concrete types. - if tparams := enclosingFunc.TypeParams; tparams != nil && tparams.NumFields() > 0 { - return nil, nil - } - - // Find the function declaration that encloses the ReturnStmt. - var outer *ast.FuncDecl - for _, p := range path { - if p, ok := p.(*ast.FuncDecl); ok { - outer = p - break + // Find cursor for enclosing return statement (which may be curErr itself). + curRet := curErr + if _, ok := curRet.Node().(*ast.ReturnStmt); !ok { + curRet, ok = moreiters.First(curErr.Ancestors((*ast.ReturnStmt)(nil))) + if !ok { + continue // no enclosing return } } - if outer == nil { - return nil, nil - } + ret := curRet.Node().(*ast.ReturnStmt) - // Skip any return statements that contain function calls with multiple - // return values. + // Skip if any return argument is a tuple-valued function call. for _, expr := range ret.Results { e, ok := expr.(*ast.CallExpr) if !ok { @@ -136,24 +75,47 @@ outer: } } + // Get type of innermost enclosing function. + var funcType *ast.FuncType + curFunc, _ := enclosingFunc(curRet) // can't fail + switch fn := curFunc.Node().(type) { + case *ast.FuncLit: + funcType = fn.Type + case *ast.FuncDecl: + funcType = fn.Type + + // Skip generic functions since type parameters don't have zero values. + // TODO(rfindley): We should be able to handle this if the return + // values are all concrete types. + if funcType.TypeParams.NumFields() > 0 { + continue + } + } + if funcType.Results == nil { + continue + } + // Duplicate the return values to track which values have been matched. remaining := make([]ast.Expr, len(ret.Results)) copy(remaining, ret.Results) - fixed := make([]ast.Expr, len(enclosingFunc.Results.List)) + fixed := make([]ast.Expr, len(funcType.Results.List)) // For each value in the return function declaration, find the leftmost element // in the return statement that has the desired type. If no such element exists, // fill in the missing value with the appropriate "zero" value. // Beware that type information may be incomplete. var retTyps []types.Type - for _, ret := range enclosingFunc.Results.List { + for _, ret := range funcType.Results.List { retTyp := info.TypeOf(ret.Type) if retTyp == nil { return nil, nil } retTyps = append(retTyps, retTyp) } + + curFile, _ := moreiters.First(curRet.Ancestors((*ast.File)(nil))) + file := curFile.Node().(*ast.File) matches := analysisinternal.MatchingIdents(retTyps, file, ret.Pos(), info, pass.Pkg) qual := typesinternal.FileQualifier(file, pass.Pkg) for i, retTyp := range retTyps { @@ -215,8 +177,8 @@ outer: } pass.Report(analysis.Diagnostic{ - Pos: typeErr.Pos, - End: typeErrEndPos, + Pos: start, + End: end, Message: typeErr.Msg, SuggestedFixes: []analysis.SuggestedFix{{ Message: "Fill in return values", @@ -255,7 +217,7 @@ var wrongReturnNumRegexes = []*regexp.Regexp{ regexp.MustCompile(`not enough return values`), } -func FixesError(err types.Error) bool { +func fixesError(err types.Error) bool { msg := strings.TrimSpace(err.Msg) for _, rx := range wrongReturnNumRegexes { if rx.MatchString(msg) { @@ -264,3 +226,12 @@ func FixesError(err types.Error) bool { } return false } + +// enclosingFunc returns the cursor for the innermost Func{Decl,Lit} +// that encloses c, if any. +func enclosingFunc(c cursor.Cursor) (cursor.Cursor, bool) { + for curAncestor := range c.Ancestors((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { + return curAncestor, true + } + return cursor.Cursor{}, false +} diff --git a/gopls/internal/analysis/nonewvars/nonewvars.go b/gopls/internal/analysis/nonewvars/nonewvars.go index 9e5d79df02c..8a3bf502c51 100644 --- a/gopls/internal/analysis/nonewvars/nonewvars.go +++ b/gopls/internal/analysis/nonewvars/nonewvars.go @@ -7,16 +7,17 @@ package nonewvars import ( - "bytes" _ "embed" "go/ast" - "go/format" "go/token" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typesinternal" ) //go:embed doc.go @@ -33,57 +34,45 @@ var Analyzer = &analysis.Analyzer{ func run(pass *analysis.Pass) (interface{}, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - if len(pass.TypeErrors) == 0 { - return nil, nil - } - nodeFilter := []ast.Node{(*ast.AssignStmt)(nil)} - inspect.Preorder(nodeFilter, func(n ast.Node) { - assignStmt, _ := n.(*ast.AssignStmt) - // We only care about ":=". - if assignStmt.Tok != token.DEFINE { - return + for _, typeErr := range pass.TypeErrors { + if typeErr.Msg != "no new variables on left side of :=" { + continue // irrelevant error + } + _, start, end, ok := typesinternal.ErrorCodeStartEnd(typeErr) + if !ok { + continue // can't get position info + } + curErr, ok := cursor.Root(inspect).FindPos(start, end) + if !ok { + continue // can't find errant node } - var file *ast.File - for _, f := range pass.Files { - if f.FileStart <= assignStmt.Pos() && assignStmt.Pos() < f.FileEnd { - file = f - break + // Find enclosing assignment (which may be curErr itself). + assign, ok := curErr.Node().(*ast.AssignStmt) + if !ok { + cur, ok := moreiters.First(curErr.Ancestors((*ast.AssignStmt)(nil))) + if !ok { + continue // no enclosing assignment } + assign = cur.Node().(*ast.AssignStmt) } - if file == nil { - return + if assign.Tok != token.DEFINE { + continue // not a := statement } - for _, err := range pass.TypeErrors { - if !FixesError(err.Msg) { - continue - } - if assignStmt.Pos() > err.Pos || err.Pos >= assignStmt.End() { - continue - } - var buf bytes.Buffer - if err := format.Node(&buf, pass.Fset, file); err != nil { - continue - } - pass.Report(analysis.Diagnostic{ - Pos: err.Pos, - End: analysisinternal.TypeErrorEndPos(pass.Fset, buf.Bytes(), err.Pos), - Message: err.Msg, - SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Change ':=' to '='", - TextEdits: []analysis.TextEdit{{ - Pos: err.Pos, - End: err.Pos + 1, - }}, + pass.Report(analysis.Diagnostic{ + Pos: assign.TokPos, + End: assign.TokPos + token.Pos(len(":=")), + Message: typeErr.Msg, + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Change ':=' to '='", + TextEdits: []analysis.TextEdit{{ + Pos: assign.TokPos, + End: assign.TokPos + token.Pos(len(":")), }}, - }) - } - }) + }}, + }) + } return nil, nil } - -func FixesError(msg string) bool { - return msg == "no new variables on left side of :=" -} diff --git a/gopls/internal/analysis/noresultvalues/noresultvalues.go b/gopls/internal/analysis/noresultvalues/noresultvalues.go index 118beb4568b..fe979f52aac 100644 --- a/gopls/internal/analysis/noresultvalues/noresultvalues.go +++ b/gopls/internal/analysis/noresultvalues/noresultvalues.go @@ -5,9 +5,8 @@ package noresultvalues import ( - "bytes" "go/ast" - "go/format" + "go/token" "strings" _ "embed" @@ -15,7 +14,10 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typesinternal" ) //go:embed doc.go @@ -32,55 +34,40 @@ var Analyzer = &analysis.Analyzer{ func run(pass *analysis.Pass) (interface{}, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - if len(pass.TypeErrors) == 0 { - return nil, nil - } - - nodeFilter := []ast.Node{(*ast.ReturnStmt)(nil)} - inspect.Preorder(nodeFilter, func(n ast.Node) { - retStmt, _ := n.(*ast.ReturnStmt) - var file *ast.File - for _, f := range pass.Files { - if f.FileStart <= retStmt.Pos() && retStmt.Pos() < f.FileEnd { - file = f - break - } + for _, typErr := range pass.TypeErrors { + if !fixesError(typErr.Msg) { + continue // irrelevant error } - if file == nil { - return + _, start, end, ok := typesinternal.ErrorCodeStartEnd(typErr) + if !ok { + continue // can't get position info } - - for _, err := range pass.TypeErrors { - if !FixesError(err.Msg) { - continue - } - if retStmt.Pos() >= err.Pos || err.Pos >= retStmt.End() { - continue - } - var buf bytes.Buffer - if err := format.Node(&buf, pass.Fset, file); err != nil { - continue - } + curErr, ok := cursor.Root(inspect).FindPos(start, end) + if !ok { + continue // can't find errant node + } + // Find first enclosing return statement, if any. + if curRet, ok := moreiters.First(curErr.Ancestors((*ast.ReturnStmt)(nil))); ok { + ret := curRet.Node() pass.Report(analysis.Diagnostic{ - Pos: err.Pos, - End: analysisinternal.TypeErrorEndPos(pass.Fset, buf.Bytes(), err.Pos), - Message: err.Msg, + Pos: start, + End: end, + Message: typErr.Msg, SuggestedFixes: []analysis.SuggestedFix{{ Message: "Delete return values", TextEdits: []analysis.TextEdit{{ - Pos: retStmt.Pos(), - End: retStmt.End(), - NewText: []byte("return"), + Pos: ret.Pos() + token.Pos(len("return")), + End: ret.End(), }}, }}, }) } - }) + } return nil, nil } -func FixesError(msg string) bool { +func fixesError(msg string) bool { return msg == "no result values expected" || strings.HasPrefix(msg, "too many return values") && strings.Contains(msg, "want ()") } diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index d094c535d7a..a3aff5e5475 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -2001,7 +2001,7 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, inputs *typeCheckInputs, errs [ batch := func(related []types.Error) { var diags []*Diagnostic for i, e := range related { - code, start, end, ok := typesinternal.ReadGo116ErrorData(e) + code, start, end, ok := typesinternal.ErrorCodeStartEnd(e) if !ok || !start.IsValid() || !end.IsValid() { start, end = e.Pos, e.Pos code = 0 @@ -2075,6 +2075,9 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, inputs *typeCheckInputs, errs [ if end == start { // Expand the end position to a more meaningful span. + // + // TODO(adonovan): It is the type checker's responsibility + // to ensure that (start, end) are meaningful; see #71803. end = analysisinternal.TypeErrorEndPos(e.Fset, pgf.Src, start) // debugging golang/go#65960 diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 34ac7426019..f82c32d6a9c 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -309,7 +309,7 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error { for _, typeError := range req.pkg.TypeErrors() { // Does type error overlap with CodeAction range? start, end := typeError.Pos, typeError.Pos - if _, _, endPos, ok := typesinternal.ReadGo116ErrorData(typeError); ok { + if _, _, endPos, ok := typesinternal.ErrorCodeStartEnd(typeError); ok { end = endPos } typeErrorRange, err := req.pgf.PosRange(start, end) diff --git a/gopls/internal/test/marker/testdata/highlight/controlflow.txt b/gopls/internal/test/marker/testdata/highlight/controlflow.txt index c09f748a553..46ec48d030d 100644 --- a/gopls/internal/test/marker/testdata/highlight/controlflow.txt +++ b/gopls/internal/test/marker/testdata/highlight/controlflow.txt @@ -68,7 +68,6 @@ func _() { } func _() () { - // TODO(golang/go#65966): fix the triplicate diagnostics here. - return 0 //@hiloc(ret2, "0", text), diag("0", re"too many return"), diag("0", re"too many return"), diag("0", re"too many return") + return 0 //@hiloc(ret2, "0", text), diag("0", re"too many return") //@highlight(ret2, ret2) } diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index d96d22982c5..aba435fa404 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -23,6 +23,8 @@ import ( "golang.org/x/tools/internal/typesinternal" ) +// Deprecated: this heuristic is ill-defined. +// TODO(adonovan): move to sole use in gopls/internal/cache. func TypeErrorEndPos(fset *token.FileSet, src []byte, start token.Pos) token.Pos { // Get the end position for the type error. file := fset.File(start) diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index 34534879630..edf0347ec3b 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -32,12 +32,14 @@ func SetUsesCgo(conf *types.Config) bool { return true } -// ReadGo116ErrorData extracts additional information from types.Error values +// ErrorCodeStartEnd extracts additional information from types.Error values // generated by Go version 1.16 and later: the error code, start position, and // end position. If all positions are valid, start <= err.Pos <= end. // // If the data could not be read, the final result parameter will be false. -func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos, ok bool) { +// +// TODO(adonovan): eliminate start/end when proposal #71803 is accepted. +func ErrorCodeStartEnd(err types.Error) (code ErrorCode, start, end token.Pos, ok bool) { var data [3]int // By coincidence all of these fields are ints, which simplifies things. v := reflect.ValueOf(err) From fe883a85d1827c1acaed7273c9a10ff0660d9a0d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 18 Feb 2025 13:06:42 -0500 Subject: [PATCH 156/309] gopls/internal/analysis/unusedvariable: refine bug.Report golang/go#71812 This CL adds assertions to refine the bug reported in golang/go#71812, in which the analyzer reports an invalid SuggestedFix. Updates golang/go#71812 Change-Id: Ie4a9aac9ba3d16974320d7cd4b48bc4cc76fc3c4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650395 Commit-Queue: Alan Donovan Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../analysis/unusedvariable/unusedvariable.go | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/gopls/internal/analysis/unusedvariable/unusedvariable.go b/gopls/internal/analysis/unusedvariable/unusedvariable.go index 15bcd43d873..5f1c188eb6a 100644 --- a/gopls/internal/analysis/unusedvariable/unusedvariable.go +++ b/gopls/internal/analysis/unusedvariable/unusedvariable.go @@ -13,10 +13,12 @@ import ( "go/token" "go/types" "regexp" + "slices" "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" ) @@ -165,16 +167,13 @@ func removeVariableFromSpec(pass *analysis.Pass, path []ast.Node, stmt *ast.Valu // Find parent DeclStmt and delete it for _, node := range path { if declStmt, ok := node.(*ast.DeclStmt); ok { - edits := deleteStmtFromBlock(pass.Fset, path, declStmt) - if len(edits) == 0 { - return nil // can this happen? - } - return []analysis.SuggestedFix{ - { + if edits := deleteStmtFromBlock(pass.Fset, path, declStmt); len(edits) > 0 { + return []analysis.SuggestedFix{{ Message: suggestedFixMessage(ident.Name), TextEdits: edits, - }, + }} } + return nil } } } @@ -222,16 +221,13 @@ func removeVariableFromAssignment(fset *token.FileSet, path []ast.Node, stmt *as } // RHS does not have any side effects, delete the whole statement - edits := deleteStmtFromBlock(fset, path, stmt) - if len(edits) == 0 { - return nil // can this happen? - } - return []analysis.SuggestedFix{ - { + if edits := deleteStmtFromBlock(fset, path, stmt); len(edits) > 0 { + return []analysis.SuggestedFix{{ Message: suggestedFixMessage(ident.Name), TextEdits: edits, - }, + }} } + return nil } // Otherwise replace ident with `_` @@ -253,34 +249,48 @@ func suggestedFixMessage(name string) string { return fmt.Sprintf("Remove variable %s", name) } +// deleteStmtFromBlock returns the edits to remove stmt if its parent is a BlockStmt. +// (stmt is not necessarily the leaf, path[0].) +// +// It returns nil if the parent is not a block, as in these examples: +// +// switch STMT; {} +// switch { default: STMT } +// select { default: STMT } +// +// TODO(adonovan): handle these cases too. func deleteStmtFromBlock(fset *token.FileSet, path []ast.Node, stmt ast.Stmt) []analysis.TextEdit { - // Find innermost enclosing BlockStmt. - var block *ast.BlockStmt - for i := range path { - if blockStmt, ok := path[i].(*ast.BlockStmt); ok { - block = blockStmt - break - } + // TODO(adonovan): simplify using Cursor API. + i := slices.Index(path, ast.Node(stmt)) // must be present + block, ok := path[i+1].(*ast.BlockStmt) + if !ok { + return nil // parent is not a BlockStmt } - nodeIndex := -1 - for i, blockStmt := range block.List { - if blockStmt == stmt { - nodeIndex = i - break - } + nodeIndex := slices.Index(block.List, stmt) + if nodeIndex == -1 { + bug.Reportf("%s: Stmt not found in BlockStmt.List", safetoken.StartPosition(fset, stmt.Pos())) // refine #71812 + return nil } - // The statement we need to delete was not found in BlockStmt - if nodeIndex == -1 { + if !stmt.Pos().IsValid() { + bug.Reportf("%s: invalid Stmt.Pos", safetoken.StartPosition(fset, stmt.Pos())) // refine #71812 return nil } // Delete until the end of the block unless there is another statement after // the one we are trying to delete end := block.Rbrace + if !end.IsValid() { + bug.Reportf("%s: BlockStmt has no Rbrace", safetoken.StartPosition(fset, block.Pos())) // refine #71812 + return nil + } if nodeIndex < len(block.List)-1 { end = block.List[nodeIndex+1].Pos() + if end < stmt.Pos() { + bug.Reportf("%s: BlockStmt.List[last].Pos > BlockStmt.Rbrace", safetoken.StartPosition(fset, block.Pos())) // refine #71812 + return nil + } } // Account for comments within the block containing the statement @@ -298,7 +308,7 @@ outer: // If a comment exists within the current block, after the unused variable statement, // and before the next statement, we shouldn't delete it. if coLine > stmtEndLine { - end = co.Pos() + end = co.Pos() // preserves invariant stmt.Pos <= end (#71812) break outer } if co.Pos() > end { @@ -308,12 +318,11 @@ outer: } } - return []analysis.TextEdit{ - { - Pos: stmt.Pos(), - End: end, - }, - } + // Delete statement and optional following comment. + return []analysis.TextEdit{{ + Pos: stmt.Pos(), + End: end, + }} } // exprMayHaveSideEffects reports whether the expression may have side effects From 4b3fdfd83f78b8336aef4160183bf5b27febc5b9 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 16 Feb 2025 22:19:08 -0500 Subject: [PATCH 157/309] go/analysis/passes/printf: suppress diagnostic for Println("...%XX...") A common form of literal string is a URL containing URL-escaped characters. This CL causes the printf checker not to report a "Println call has possible Printf formatting directive" diagnostic for it. + test Fixes golang/go#29854 Change-Id: Ib1dcc44dd8185da17f61296632ad030cb1e58420 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650175 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan Reviewed-by: Jonathan Amsterdam --- go/analysis/passes/printf/printf.go | 18 +++++++++++++++--- go/analysis/passes/printf/testdata/src/a/a.go | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/go/analysis/passes/printf/printf.go b/go/analysis/passes/printf/printf.go index 81600a283aa..a28ed365d1e 100644 --- a/go/analysis/passes/printf/printf.go +++ b/go/analysis/passes/printf/printf.go @@ -924,9 +924,14 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, name string) { // The % in "abc 0.0%" couldn't be a formatting directive. s = strings.TrimSuffix(s, "%") if strings.Contains(s, "%") { - m := printFormatRE.FindStringSubmatch(s) - if m != nil { - pass.ReportRangef(call, "%s call has possible Printf formatting directive %s", name, m[0]) + for _, m := range printFormatRE.FindAllString(s, -1) { + // Allow %XX where XX are hex digits, + // as this is common in URLs. + if len(m) >= 3 && isHex(m[1]) && isHex(m[2]) { + continue + } + pass.ReportRangef(call, "%s call has possible Printf formatting directive %s", name, m) + break // report only the first one } } } @@ -992,3 +997,10 @@ func (ss stringSet) Set(flag string) error { // // Remove this after the 1.24 release. var suppressNonconstants bool + +// isHex reports whether b is a hex digit. +func isHex(b byte) bool { + return '0' <= b && b <= '9' || + 'A' <= b && b <= 'F' || + 'a' <= b && b <= 'f' +} diff --git a/go/analysis/passes/printf/testdata/src/a/a.go b/go/analysis/passes/printf/testdata/src/a/a.go index 02ce425f8a3..da48f98f0a8 100644 --- a/go/analysis/passes/printf/testdata/src/a/a.go +++ b/go/analysis/passes/printf/testdata/src/a/a.go @@ -154,6 +154,8 @@ func PrintfTests() { fmt.Println("%v", "hi") // want "fmt.Println call has possible Printf formatting directive %v" fmt.Println("%T", "hi") // want "fmt.Println call has possible Printf formatting directive %T" fmt.Println("%s"+" there", "hi") // want "fmt.Println call has possible Printf formatting directive %s" + fmt.Println("http://foo.com?q%2Fabc") // no diagnostic: %XX is excepted + fmt.Println("http://foo.com?q%2Fabc-%s") // want"fmt.Println call has possible Printf formatting directive %s" fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive) fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args" _ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args" From ad5dd9875168fe5cb7c43643a34c8cc26411b2f9 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 18 Feb 2025 20:49:25 +0000 Subject: [PATCH 158/309] gopls: fix a few bugs related to the new modcache imports source Fix the following bugs related to the new "gopls" imports source and module cache index: - Only construct the modcacheState if the imports source is "gopls", which is not yet the default. This was causing memory regressions, as the modcache table is non-trivial. - Add missing error handling. - Don't call modcacheState.stopTimer if the modcacheState is nil, which may already have been the case with "importsSource": "off". For golang/go#71607 Change-Id: I33c90ee4b97c8675b342cb0c045eef183a1ef365 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650397 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/session.go | 7 ++++++- gopls/internal/cache/view.go | 9 +++++++-- gopls/internal/golang/format.go | 6 ++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index a7fb618f679..5ae753eb91c 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -238,7 +238,12 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, * viewDefinition: def, importsState: newImportsState(backgroundCtx, s.cache.modCache, pe), } - if def.folder.Options.ImportsSource != settings.ImportsSourceOff { + + // Keep this in sync with golang.computeImportEdits. + // + // TODO(rfindley): encapsulate the imports state logic so that the handling + // for Options.ImportsSource is in a single location. + if def.folder.Options.ImportsSource == settings.ImportsSourceGopls { v.modcacheState = newModcacheState(def.folder.Env.GOMODCACHE) } diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index 26f0de86125..6ebf6837ef2 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -109,7 +109,10 @@ type View struct { // importsState is for the old imports code importsState *importsState - // maintain the current module cache index + // modcacheState is the replacement for importsState, to be used for + // goimports operations when the imports source is "gopls". + // + // It may be nil, if the imports source is not "gopls". modcacheState *modcacheState // pkgIndex is an index of package IDs, for efficient storage of typerefs. @@ -492,7 +495,9 @@ func (v *View) shutdown() { // Cancel the initial workspace load if it is still running. v.cancelInitialWorkspaceLoad() v.importsState.stopTimer() - v.modcacheState.stopTimer() + if v.modcacheState != nil { + v.modcacheState.stopTimer() + } v.snapshotMu.Lock() if v.snapshot != nil { diff --git a/gopls/internal/golang/format.go b/gopls/internal/golang/format.go index de4ec3a642c..acc619eba0c 100644 --- a/gopls/internal/golang/format.go +++ b/gopls/internal/golang/format.go @@ -137,7 +137,13 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, snapshot *cache. // Build up basic information about the original file. isource, err := imports.NewProcessEnvSource(options.Env, filename, pgf.File.Name.Name) + if err != nil { + return nil, nil, err + } var source imports.Source + + // Keep this in sync with [cache.Session.createView] (see the TODO there: we + // should factor out the handling of the ImportsSource setting). switch snapshot.Options().ImportsSource { case settings.ImportsSourceGopls: source = snapshot.NewGoplsSource(isource) From df7baa073c7b4850753e4f6b6084402fd9cb573b Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 13 Feb 2025 13:55:48 -0500 Subject: [PATCH 159/309] gopls/internal/analysis/simplifyrange: more precise fix This CL reduces the size of the fix offered by simplifyrange, which makes the cursor jump less. It's also simpler, and handles a missing case. Change-Id: I8dbff96158e442b2073e86694b61ea1f0b1ea704 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649355 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- .../analysis/simplifyrange/simplifyrange.go | 93 +++++++------------ .../simplifyrange/simplifyrange_test.go | 11 +-- .../simplifyrange/testdata/src/a/a.go | 7 ++ .../simplifyrange/testdata/src/a/a.go.golden | 7 ++ 4 files changed, 51 insertions(+), 67 deletions(-) diff --git a/gopls/internal/analysis/simplifyrange/simplifyrange.go b/gopls/internal/analysis/simplifyrange/simplifyrange.go index 6d079059eb1..fd685ba2c5b 100644 --- a/gopls/internal/analysis/simplifyrange/simplifyrange.go +++ b/gopls/internal/analysis/simplifyrange/simplifyrange.go @@ -5,10 +5,8 @@ package simplifyrange import ( - "bytes" _ "embed" "go/ast" - "go/printer" "go/token" "golang.org/x/tools/go/analysis" @@ -42,73 +40,48 @@ func run(pass *analysis.Pass) (interface{}, error) { (*ast.RangeStmt)(nil), } inspect.Preorder(nodeFilter, func(n ast.Node) { - if _, ok := generated[pass.Fset.File(n.Pos())]; ok { - return // skip checking if it's generated code - } + rng := n.(*ast.RangeStmt) - var copy *ast.RangeStmt // shallow-copy the AST before modifying - { - x := *n.(*ast.RangeStmt) - copy = &x - } - end := newlineIndex(pass.Fset, copy) + kblank := isBlank(rng.Key) + vblank := isBlank(rng.Value) + var start, end token.Pos + switch { + case kblank && (rng.Value == nil || vblank): + // for _ = range x {} + // for _, _ = range x {} + // ^^^^^^^ + start, end = rng.Key.Pos(), rng.Range - // Range statements of the form: for i, _ := range x {} - var old ast.Expr - if isBlank(copy.Value) { - old = copy.Value - copy.Value = nil - } - // Range statements of the form: for _ := range x {} - if isBlank(copy.Key) && copy.Value == nil { - old = copy.Key - copy.Key = nil + case vblank: + // for k, _ := range x {} + // ^^^ + start, end = rng.Key.End(), rng.Value.End() + + default: + return } - // Return early if neither if condition is met. - if old == nil { + + if generated[pass.Fset.File(n.Pos())] { return } + pass.Report(analysis.Diagnostic{ - Pos: old.Pos(), - End: old.End(), - Message: "simplify range expression", - SuggestedFixes: suggestedFixes(pass.Fset, copy, end), + Pos: start, + End: end, + Message: "simplify range expression", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Remove empty value", + TextEdits: []analysis.TextEdit{{ + Pos: start, + End: end, + }}, + }}, }) }) return nil, nil } -func suggestedFixes(fset *token.FileSet, rng *ast.RangeStmt, end token.Pos) []analysis.SuggestedFix { - var b bytes.Buffer - printer.Fprint(&b, fset, rng) - stmt := b.Bytes() - index := bytes.Index(stmt, []byte("\n")) - // If there is a new line character, then don't replace the body. - if index != -1 { - stmt = stmt[:index] - } - return []analysis.SuggestedFix{{ - Message: "Remove empty value", - TextEdits: []analysis.TextEdit{{ - Pos: rng.Pos(), - End: end, - NewText: stmt[:index], - }}, - }} -} - -func newlineIndex(fset *token.FileSet, rng *ast.RangeStmt) token.Pos { - var b bytes.Buffer - printer.Fprint(&b, fset, rng) - contents := b.Bytes() - index := bytes.Index(contents, []byte("\n")) - if index == -1 { - return rng.End() - } - return rng.Pos() + token.Pos(index) -} - -func isBlank(x ast.Expr) bool { - ident, ok := x.(*ast.Ident) - return ok && ident.Name == "_" +func isBlank(e ast.Expr) bool { + id, ok := e.(*ast.Ident) + return ok && id.Name == "_" } diff --git a/gopls/internal/analysis/simplifyrange/simplifyrange_test.go b/gopls/internal/analysis/simplifyrange/simplifyrange_test.go index 50a600e03bf..089f65df870 100644 --- a/gopls/internal/analysis/simplifyrange/simplifyrange_test.go +++ b/gopls/internal/analysis/simplifyrange/simplifyrange_test.go @@ -5,8 +5,6 @@ package simplifyrange_test import ( - "go/build" - "slices" "testing" "golang.org/x/tools/go/analysis/analysistest" @@ -14,9 +12,8 @@ import ( ) func Test(t *testing.T) { - testdata := analysistest.TestData() - analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a", "generatedcode") - if slices.Contains(build.Default.ReleaseTags, "go1.23") { // uses iter.Seq - analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "rangeoverfunc") - } + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), simplifyrange.Analyzer, + "a", + "generatedcode", + "rangeoverfunc") } diff --git a/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go b/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go index 49face1e968..1d7b1bd58f2 100644 --- a/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go +++ b/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go @@ -13,4 +13,11 @@ func m() { } for _ = range maps { // want "simplify range expression" } + for _, _ = range maps { // want "simplify range expression" + } + for _, v := range maps { // nope + println(v) + } + for range maps { // nope + } } diff --git a/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go.golden b/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go.golden index ec8490ab337..25139bd93f2 100644 --- a/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/simplifyrange/testdata/src/a/a.go.golden @@ -13,4 +13,11 @@ func m() { } for range maps { // want "simplify range expression" } + for range maps { // want "simplify range expression" + } + for _, v := range maps { // nope + println(v) + } + for range maps { // nope + } } From 776604a9ed881ee1274724fb3a5f058f3ebdf0eb Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 17 Feb 2025 12:24:06 -0500 Subject: [PATCH 160/309] gopls/internal/analysis/modernize: sortslice: fix crash The sole statement of a comparison func body is not necessarily a return statement. + Test Fixes golang/go#71786 Change-Id: Ic002035fc9fa303b62ed1828c13f3bdfb8bc6950 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650215 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/analysis/modernize/sortslice.go | 75 ++++++++++--------- .../testdata/src/sortslice/sortslice.go | 12 ++- .../src/sortslice/sortslice.go.golden | 12 ++- 3 files changed, 60 insertions(+), 39 deletions(-) diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 7f695d76495..bbc8befb8ee 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -57,45 +57,46 @@ func sortslice(pass *analysis.Pass) { i := sig.Params().At(0) j := sig.Params().At(1) - ret := lit.Body.List[0].(*ast.ReturnStmt) - if compare, ok := ret.Results[0].(*ast.BinaryExpr); ok && compare.Op == token.LSS { - // isIndex reports whether e is s[v]. - isIndex := func(e ast.Expr, v *types.Var) bool { - index, ok := e.(*ast.IndexExpr) - return ok && - equalSyntax(index.X, s) && - is[*ast.Ident](index.Index) && - info.Uses[index.Index.(*ast.Ident)] == v - } - if isIndex(compare.X, i) && isIndex(compare.Y, j) { - // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) + if ret, ok := lit.Body.List[0].(*ast.ReturnStmt); ok { + if compare, ok := ret.Results[0].(*ast.BinaryExpr); ok && compare.Op == token.LSS { + // isIndex reports whether e is s[v]. + isIndex := func(e ast.Expr, v *types.Var) bool { + index, ok := e.(*ast.IndexExpr) + return ok && + equalSyntax(index.X, s) && + is[*ast.Ident](index.Index) && + info.Uses[index.Index.(*ast.Ident)] == v + } + if isIndex(compare.X, i) && isIndex(compare.Y, j) { + // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) - _, prefix, importEdits := analysisinternal.AddImport( - info, file, "slices", "slices", "Sort", call.Pos()) + _, prefix, importEdits := analysisinternal.AddImport( + info, file, "slices", "slices", "Sort", call.Pos()) - pass.Report(analysis.Diagnostic{ - // Highlight "sort.Slice". - Pos: call.Fun.Pos(), - End: call.Fun.End(), - Category: "sortslice", - Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"), - SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"), - TextEdits: append(importEdits, []analysis.TextEdit{ - { - // Replace sort.Slice with slices.Sort. - Pos: call.Fun.Pos(), - End: call.Fun.End(), - NewText: []byte(prefix + "Sort"), - }, - { - // Eliminate FuncLit. - Pos: call.Args[0].End(), - End: call.Rparen, - }, - }...), - }}, - }) + pass.Report(analysis.Diagnostic{ + // Highlight "sort.Slice". + Pos: call.Fun.Pos(), + End: call.Fun.End(), + Category: "sortslice", + Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"), + TextEdits: append(importEdits, []analysis.TextEdit{ + { + // Replace sort.Slice with slices.Sort. + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: []byte(prefix + "Sort"), + }, + { + // Eliminate FuncLit. + Pos: call.Args[0].End(), + End: call.Rparen, + }, + }...), + }}, + }) + } } } } diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go index 53d15746839..19242065b24 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go @@ -20,6 +20,16 @@ func _(s []int) { sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var } -func _(s2 []struct{ x int }) { +func _(sense bool, s2 []struct{ x int }) { sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation + + // Regression test for a crash: the sole statement of a + // comparison func body is not necessarily a return! + sort.Slice(s2, func(i, j int) bool { + if sense { + return s2[i].x < s2[j].x + } else { + return s2[i].x > s2[j].x + } + }) } diff --git a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden index 34af5aad60b..19149b4480a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden @@ -22,6 +22,16 @@ func _(s []int) { sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var } -func _(s2 []struct{ x int }) { +func _(sense bool, s2 []struct{ x int }) { sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation + + // Regression test for a crash: the sole statement of a + // comparison func body is not necessarily a return! + sort.Slice(s2, func(i, j int) bool { + if sense { + return s2[i].x < s2[j].x + } else { + return s2[i].x > s2[j].x + } + }) } From e6754cedb2986a3026a6de6e1460f6a2edbe01f0 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 18 Feb 2025 14:43:35 -0500 Subject: [PATCH 161/309] gopls/internal/cache/parsego: add File.Cursor, and use it This CL adds a Cursor to the parsego.File. Though the primary motivation is convenience and flexibility, it is expected to be an optimization: though it is computed eagerly, it is retained in the parse cache, and is expected to pay for itself very quickly by allowing us to replace many whole-File ast.Inspect operations with more targeted traversals. The CL replaces all ast.Inspect(file) operations with Cursor, but there remain many more opportunities for using it in narrower traversals, and in places that need to navigate to siblings or ancestors. Also, amend Cursor.FindPos to use the complete range of the File, as CL 637738 recently did for astutil.NodeContains. Also, various clean-ups to InlayHint: - push the traversals down in InlayHint to avoid having to scan a slice for every single node we visit; - simplify the function signature used for each hint algorithm. Change-Id: I64d0c2cae75fd73a4b539ceb81ad9d6f7d80cfb8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650396 Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Reviewed-by: Robert Findley --- gopls/internal/cache/parsego/file.go | 8 + gopls/internal/cache/parsego/parse.go | 8 + gopls/internal/cache/xrefs/xrefs.go | 11 +- gopls/internal/golang/folding_range.go | 142 ++++---- gopls/internal/golang/implementation.go | 20 +- gopls/internal/golang/inlay_hint.go | 414 ++++++++++++------------ gopls/internal/golang/references.go | 12 +- gopls/internal/server/link.go | 16 +- internal/astutil/cursor/cursor.go | 12 +- 9 files changed, 326 insertions(+), 317 deletions(-) diff --git a/gopls/internal/cache/parsego/file.go b/gopls/internal/cache/parsego/file.go index 41fd1937ec1..2be4ed4b2ca 100644 --- a/gopls/internal/cache/parsego/file.go +++ b/gopls/internal/cache/parsego/file.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil/cursor" ) // A File contains the results of parsing a Go file. @@ -32,6 +33,8 @@ type File struct { // actual content of the file if we have fixed the AST. Src []byte + Cursor cursor.Cursor // cursor of *ast.File, sans sibling files + // fixedSrc and fixedAST report on "fixing" that occurred during parsing of // this file. // @@ -71,6 +74,11 @@ func (pgf *File) PositionPos(p protocol.Position) (token.Pos, error) { return safetoken.Pos(pgf.Tok, offset) } +// PosPosition returns a protocol Position for the token.Pos in this file. +func (pgf *File) PosPosition(pos token.Pos) (protocol.Position, error) { + return pgf.Mapper.PosPosition(pgf.Tok, pos) +} + // PosRange returns a protocol Range for the token.Pos interval in this file. func (pgf *File) PosRange(start, end token.Pos) (protocol.Range, error) { return pgf.Mapper.PosRange(pgf.Tok, start, end) diff --git a/gopls/internal/cache/parsego/parse.go b/gopls/internal/cache/parsego/parse.go index df167314b04..db6089d8e6d 100644 --- a/gopls/internal/cache/parsego/parse.go +++ b/gopls/internal/cache/parsego/parse.go @@ -23,11 +23,13 @@ import ( "reflect" "slices" + "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/gopls/internal/label" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/astutil" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/diff" "golang.org/x/tools/internal/event" ) @@ -153,6 +155,11 @@ func Parse(ctx context.Context, fset *token.FileSet, uri protocol.DocumentURI, s } assert(file != nil, "nil *ast.File") + // Provide a cursor for fast and convenient navigation. + inspect := inspector.New([]*ast.File{file}) + curFile, _ := cursor.Root(inspect).FirstChild() + _ = curFile.Node().(*ast.File) + return &File{ URI: uri, Mode: mode, @@ -161,6 +168,7 @@ func Parse(ctx context.Context, fset *token.FileSet, uri protocol.DocumentURI, s fixedAST: fixedAST, File: file, Tok: tok, + Cursor: curFile, Mapper: protocol.NewMapper(uri, src), ParseErr: parseErr, }, fixes diff --git a/gopls/internal/cache/xrefs/xrefs.go b/gopls/internal/cache/xrefs/xrefs.go index 2115322bfdc..d9b7051737a 100644 --- a/gopls/internal/cache/xrefs/xrefs.go +++ b/gopls/internal/cache/xrefs/xrefs.go @@ -44,8 +44,8 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte { objectpathFor := new(objectpath.Encoder).For for fileIndex, pgf := range files { - ast.Inspect(pgf.File, func(n ast.Node) bool { - switch n := n.(type) { + for cur := range pgf.Cursor.Preorder((*ast.Ident)(nil), (*ast.ImportSpec)(nil)) { + switch n := cur.Node().(type) { case *ast.Ident: // Report a reference for each identifier that // uses a symbol exported from another package. @@ -68,7 +68,7 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte { if err != nil { // Capitalized but not exported // (e.g. local const/var/type). - return true + continue } gobObj = &gobObject{Path: path} objects[obj] = gobObj @@ -91,7 +91,7 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte { // string to the imported package. pkgname := info.PkgNameOf(n) if pkgname == nil { - return true // missing import + continue // missing import } objects := getObjects(pkgname.Imported()) gobObj, ok := objects[nil] @@ -109,8 +109,7 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte { bug.Reportf("out of bounds import spec %+v", n.Path) } } - return true - }) + } } // Flatten the maps into slices, and sort for determinism. diff --git a/gopls/internal/golang/folding_range.go b/gopls/internal/golang/folding_range.go index 4352da28151..eed31e92944 100644 --- a/gopls/internal/golang/folding_range.go +++ b/gopls/internal/golang/folding_range.go @@ -46,12 +46,84 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, ranges := commentsFoldingRange(pgf) // Walk the ast and collect folding ranges. - ast.Inspect(pgf.File, func(n ast.Node) bool { - if rng, ok := foldingRangeFunc(pgf, n, lineFoldingOnly); ok { - ranges = append(ranges, rng) + filter := []ast.Node{ + (*ast.BasicLit)(nil), + (*ast.BlockStmt)(nil), + (*ast.CallExpr)(nil), + (*ast.CaseClause)(nil), + (*ast.CommClause)(nil), + (*ast.CompositeLit)(nil), + (*ast.FieldList)(nil), + (*ast.GenDecl)(nil), + } + for cur := range pgf.Cursor.Preorder(filter...) { + // TODO(suzmue): include trailing empty lines before the closing + // parenthesis/brace. + var kind protocol.FoldingRangeKind + // start and end define the range of content to fold away. + var start, end token.Pos + switch n := cur.Node().(type) { + case *ast.BlockStmt: + // Fold between positions of or lines between "{" and "}". + start, end = getLineFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) + + case *ast.CaseClause: + // Fold from position of ":" to end. + start, end = n.Colon+1, n.End() + + case *ast.CommClause: + // Fold from position of ":" to end. + start, end = n.Colon+1, n.End() + + case *ast.CallExpr: + // Fold between positions of or lines between "(" and ")". + start, end = getLineFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) + + case *ast.FieldList: + // Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace. + start, end = getLineFoldingRange(pgf, n.Opening, n.Closing, lineFoldingOnly) + + case *ast.GenDecl: + // If this is an import declaration, set the kind to be protocol.Imports. + if n.Tok == token.IMPORT { + kind = protocol.Imports + } + // Fold between positions of or lines between "(" and ")". + start, end = getLineFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) + + case *ast.BasicLit: + // Fold raw string literals from position of "`" to position of "`". + if n.Kind == token.STRING && len(n.Value) >= 2 && n.Value[0] == '`' && n.Value[len(n.Value)-1] == '`' { + start, end = n.Pos(), n.End() + } + + case *ast.CompositeLit: + // Fold between positions of or lines between "{" and "}". + start, end = getLineFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) + + default: + panic(n) } - return true - }) + + // Check that folding positions are valid. + if !start.IsValid() || !end.IsValid() { + continue + } + if start == end { + // Nothing to fold. + continue + } + // in line folding mode, do not fold if the start and end lines are the same. + if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) { + continue + } + rng, err := pgf.PosRange(start, end) + if err != nil { + bug.Reportf("failed to create range: %s", err) // can't happen + continue + } + ranges = append(ranges, foldingRange(kind, rng)) + } // Sort by start position. slices.SortFunc(ranges, func(x, y protocol.FoldingRange) int { @@ -64,66 +136,6 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, return ranges, nil } -// foldingRangeFunc calculates the line folding range for ast.Node n -func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) (protocol.FoldingRange, bool) { - // TODO(suzmue): include trailing empty lines before the closing - // parenthesis/brace. - var kind protocol.FoldingRangeKind - // start and end define the range of content to fold away. - var start, end token.Pos - switch n := n.(type) { - case *ast.BlockStmt: - // Fold between positions of or lines between "{" and "}". - start, end = getLineFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) - case *ast.CaseClause: - // Fold from position of ":" to end. - start, end = n.Colon+1, n.End() - case *ast.CommClause: - // Fold from position of ":" to end. - start, end = n.Colon+1, n.End() - case *ast.CallExpr: - // Fold between positions of or lines between "(" and ")". - start, end = getLineFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) - case *ast.FieldList: - // Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace. - start, end = getLineFoldingRange(pgf, n.Opening, n.Closing, lineFoldingOnly) - case *ast.GenDecl: - // If this is an import declaration, set the kind to be protocol.Imports. - if n.Tok == token.IMPORT { - kind = protocol.Imports - } - // Fold between positions of or lines between "(" and ")". - start, end = getLineFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) - case *ast.BasicLit: - // Fold raw string literals from position of "`" to position of "`". - if n.Kind == token.STRING && len(n.Value) >= 2 && n.Value[0] == '`' && n.Value[len(n.Value)-1] == '`' { - start, end = n.Pos(), n.End() - } - case *ast.CompositeLit: - // Fold between positions of or lines between "{" and "}". - start, end = getLineFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) - } - - // Check that folding positions are valid. - if !start.IsValid() || !end.IsValid() { - return protocol.FoldingRange{}, false - } - if start == end { - // Nothing to fold. - return protocol.FoldingRange{}, false - } - // in line folding mode, do not fold if the start and end lines are the same. - if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) { - return protocol.FoldingRange{}, false - } - rng, err := pgf.PosRange(start, end) - if err != nil { - bug.Reportf("failed to create range: %s", err) // can't happen - return protocol.FoldingRange{}, false - } - return foldingRange(kind, rng), true -} - // getLineFoldingRange returns the folding range for nodes with parentheses/braces/brackets // that potentially can take up multiple lines. func getLineFoldingRange(pgf *parsego.File, open, close token.Pos, lineFoldingOnly bool) (token.Pos, token.Pos) { diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index fe0a34a1c80..a7a7e663d44 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -352,17 +352,14 @@ func localImplementations(ctx context.Context, snapshot *cache.Snapshot, pkg *ca var locs []protocol.Location var methodLocs []methodsets.Location for _, pgf := range pkg.CompiledGoFiles() { - ast.Inspect(pgf.File, func(n ast.Node) bool { - spec, ok := n.(*ast.TypeSpec) - if !ok { - return true // not a type declaration - } + for cur := range pgf.Cursor.Preorder((*ast.TypeSpec)(nil)) { + spec := cur.Node().(*ast.TypeSpec) def := pkg.TypesInfo().Defs[spec.Name] if def == nil { - return true // "can't happen" for types + continue // "can't happen" for types } if def.(*types.TypeName).IsAlias() { - return true // skip type aliases to avoid duplicate reporting + continue // skip type aliases to avoid duplicate reporting } candidateType := methodsets.EnsurePointer(def.Type()) @@ -373,20 +370,20 @@ func localImplementations(ctx context.Context, snapshot *cache.Snapshot, pkg *ca // TODO(adonovan): UX: report I/I pairs too? // The same question appears in the global algorithm (methodsets). if !concreteImplementsIntf(&msets, candidateType, queryType) { - return true // not assignable + continue // not assignable } // Ignore types with empty method sets. // (No point reporting that every type satisfies 'any'.) mset := msets.MethodSet(candidateType) if mset.Len() == 0 { - return true + continue } if method == nil { // Found matching type. locs = append(locs, mustLocation(pgf, spec.Name)) - return true + continue } // Find corresponding method. @@ -407,8 +404,7 @@ func localImplementations(ctx context.Context, snapshot *cache.Snapshot, pkg *ca break } } - return true - }) + } } // Finally convert method positions to protocol form by reading the files. diff --git a/gopls/internal/golang/inlay_hint.go b/gopls/internal/golang/inlay_hint.go index bc85745cb0b..84b18e06781 100644 --- a/gopls/internal/golang/inlay_hint.go +++ b/gopls/internal/golang/inlay_hint.go @@ -14,9 +14,11 @@ import ( "strings" "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/settings" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/typesinternal" @@ -47,7 +49,7 @@ func InlayHint(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pR } info := pkg.TypesInfo() - q := typesinternal.FileQualifier(pgf.File, pkg.Types()) + qual := typesinternal.FileQualifier(pgf.File, pkg.Types()) // Set the range to the full file if the range is not valid. start, end := pgf.File.FileStart, pgf.File.FileEnd @@ -63,20 +65,16 @@ func InlayHint(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pR } var hints []protocol.InlayHint - ast.Inspect(pgf.File, func(node ast.Node) bool { - // If not in range, we can stop looking. - if node == nil || node.End() < start || node.Pos() > end { - return false - } + if curSubrange, ok := pgf.Cursor.FindPos(start, end); ok { + add := func(hint protocol.InlayHint) { hints = append(hints, hint) } for _, fn := range enabledHints { - hints = append(hints, fn(node, pgf.Mapper, pgf.Tok, info, &q)...) + fn(info, pgf, qual, curSubrange, add) } - return true - }) + } return hints, nil } -type inlayHintFunc func(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, q *types.Qualifier) []protocol.InlayHint +type inlayHintFunc func(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) var allInlayHints = map[settings.InlayHint]inlayHintFunc{ settings.AssignVariableTypes: assignVariableTypes, @@ -88,259 +86,243 @@ var allInlayHints = map[settings.InlayHint]inlayHintFunc{ settings.FunctionTypeParameters: funcTypeParams, } -func parameterNames(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, _ *types.Qualifier) []protocol.InlayHint { - callExpr, ok := node.(*ast.CallExpr) - if !ok { - return nil - } - t := info.TypeOf(callExpr.Fun) - if t == nil { - return nil - } - signature, ok := typeparams.CoreType(t).(*types.Signature) - if !ok { - return nil +func parameterNames(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curCall := range cur.Preorder((*ast.CallExpr)(nil)) { + callExpr := curCall.Node().(*ast.CallExpr) + t := info.TypeOf(callExpr.Fun) + if t == nil { + continue + } + signature, ok := typeparams.CoreType(t).(*types.Signature) + if !ok { + continue + } + + for i, v := range callExpr.Args { + start, err := pgf.PosPosition(v.Pos()) + if err != nil { + continue + } + params := signature.Params() + // When a function has variadic params, we skip args after + // params.Len(). + if i > params.Len()-1 { + break + } + param := params.At(i) + // param.Name is empty for built-ins like append + if param.Name() == "" { + continue + } + // Skip the parameter name hint if the arg matches + // the parameter name. + if i, ok := v.(*ast.Ident); ok && i.Name == param.Name() { + continue + } + + label := param.Name() + if signature.Variadic() && i == params.Len()-1 { + label = label + "..." + } + add(protocol.InlayHint{ + Position: start, + Label: buildLabel(label + ":"), + Kind: protocol.Parameter, + PaddingRight: true, + }) + } } +} - var hints []protocol.InlayHint - for i, v := range callExpr.Args { - start, err := m.PosPosition(tf, v.Pos()) - if err != nil { +func funcTypeParams(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curCall := range cur.Preorder((*ast.CallExpr)(nil)) { + call := curCall.Node().(*ast.CallExpr) + id, ok := call.Fun.(*ast.Ident) + if !ok { continue } - params := signature.Params() - // When a function has variadic params, we skip args after - // params.Len(). - if i > params.Len()-1 { - break - } - param := params.At(i) - // param.Name is empty for built-ins like append - if param.Name() == "" { + inst := info.Instances[id] + if inst.TypeArgs == nil { continue } - // Skip the parameter name hint if the arg matches - // the parameter name. - if i, ok := v.(*ast.Ident); ok && i.Name == param.Name() { + start, err := pgf.PosPosition(id.End()) + if err != nil { continue } - - label := param.Name() - if signature.Variadic() && i == params.Len()-1 { - label = label + "..." + var args []string + for i := 0; i < inst.TypeArgs.Len(); i++ { + args = append(args, inst.TypeArgs.At(i).String()) + } + if len(args) == 0 { + continue } - hints = append(hints, protocol.InlayHint{ - Position: start, - Label: buildLabel(label + ":"), - Kind: protocol.Parameter, - PaddingRight: true, + add(protocol.InlayHint{ + Position: start, + Label: buildLabel("[" + strings.Join(args, ", ") + "]"), + Kind: protocol.Type, }) } - return hints -} - -func funcTypeParams(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, _ *types.Qualifier) []protocol.InlayHint { - ce, ok := node.(*ast.CallExpr) - if !ok { - return nil - } - id, ok := ce.Fun.(*ast.Ident) - if !ok { - return nil - } - inst := info.Instances[id] - if inst.TypeArgs == nil { - return nil - } - start, err := m.PosPosition(tf, id.End()) - if err != nil { - return nil - } - var args []string - for i := 0; i < inst.TypeArgs.Len(); i++ { - args = append(args, inst.TypeArgs.At(i).String()) - } - if len(args) == 0 { - return nil - } - return []protocol.InlayHint{{ - Position: start, - Label: buildLabel("[" + strings.Join(args, ", ") + "]"), - Kind: protocol.Type, - }} } -func assignVariableTypes(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, q *types.Qualifier) []protocol.InlayHint { - stmt, ok := node.(*ast.AssignStmt) - if !ok || stmt.Tok != token.DEFINE { - return nil - } - - var hints []protocol.InlayHint - for _, v := range stmt.Lhs { - if h := variableType(v, m, tf, info, q); h != nil { - hints = append(hints, *h) +func assignVariableTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curAssign := range cur.Preorder((*ast.AssignStmt)(nil)) { + stmt := curAssign.Node().(*ast.AssignStmt) + if stmt.Tok != token.DEFINE { + continue + } + for _, v := range stmt.Lhs { + variableType(info, pgf, qual, v, add) } } - return hints } -func rangeVariableTypes(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, q *types.Qualifier) []protocol.InlayHint { - rStmt, ok := node.(*ast.RangeStmt) - if !ok { - return nil - } - var hints []protocol.InlayHint - if h := variableType(rStmt.Key, m, tf, info, q); h != nil { - hints = append(hints, *h) - } - if h := variableType(rStmt.Value, m, tf, info, q); h != nil { - hints = append(hints, *h) +func rangeVariableTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curRange := range cur.Preorder((*ast.RangeStmt)(nil)) { + rStmt := curRange.Node().(*ast.RangeStmt) + variableType(info, pgf, qual, rStmt.Key, add) + variableType(info, pgf, qual, rStmt.Value, add) } - return hints } -func variableType(e ast.Expr, m *protocol.Mapper, tf *token.File, info *types.Info, q *types.Qualifier) *protocol.InlayHint { +func variableType(info *types.Info, pgf *parsego.File, qual types.Qualifier, e ast.Expr, add func(protocol.InlayHint)) { typ := info.TypeOf(e) if typ == nil { - return nil + return } - end, err := m.PosPosition(tf, e.End()) + end, err := pgf.PosPosition(e.End()) if err != nil { - return nil + return } - return &protocol.InlayHint{ + add(protocol.InlayHint{ Position: end, - Label: buildLabel(types.TypeString(typ, *q)), + Label: buildLabel(types.TypeString(typ, qual)), Kind: protocol.Type, PaddingLeft: true, - } + }) } -func constantValues(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, _ *types.Qualifier) []protocol.InlayHint { - genDecl, ok := node.(*ast.GenDecl) - if !ok || genDecl.Tok != token.CONST { - return nil - } - - var hints []protocol.InlayHint - for _, v := range genDecl.Specs { - spec, ok := v.(*ast.ValueSpec) - if !ok { +func constantValues(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curDecl := range cur.Preorder((*ast.GenDecl)(nil)) { + genDecl := curDecl.Node().(*ast.GenDecl) + if genDecl.Tok != token.CONST { continue } - end, err := m.PosPosition(tf, v.End()) - if err != nil { - continue - } - // Show hints when values are missing or at least one value is not - // a basic literal. - showHints := len(spec.Values) == 0 - checkValues := len(spec.Names) == len(spec.Values) - var values []string - for i, w := range spec.Names { - obj, ok := info.ObjectOf(w).(*types.Const) - if !ok || obj.Val().Kind() == constant.Unknown { - return nil + + for _, v := range genDecl.Specs { + spec, ok := v.(*ast.ValueSpec) + if !ok { + continue + } + end, err := pgf.PosPosition(v.End()) + if err != nil { + continue } - if checkValues { - switch spec.Values[i].(type) { - case *ast.BadExpr: - return nil - case *ast.BasicLit: - default: - if obj.Val().Kind() != constant.Bool { - showHints = true + // Show hints when values are missing or at least one value is not + // a basic literal. + showHints := len(spec.Values) == 0 + checkValues := len(spec.Names) == len(spec.Values) + var values []string + for i, w := range spec.Names { + obj, ok := info.ObjectOf(w).(*types.Const) + if !ok || obj.Val().Kind() == constant.Unknown { + continue + } + if checkValues { + switch spec.Values[i].(type) { + case *ast.BadExpr: + continue + case *ast.BasicLit: + default: + if obj.Val().Kind() != constant.Bool { + showHints = true + } } } + values = append(values, fmt.Sprintf("%v", obj.Val())) } - values = append(values, fmt.Sprintf("%v", obj.Val())) - } - if !showHints || len(values) == 0 { - continue + if !showHints || len(values) == 0 { + continue + } + add(protocol.InlayHint{ + Position: end, + Label: buildLabel("= " + strings.Join(values, ", ")), + PaddingLeft: true, + }) } - hints = append(hints, protocol.InlayHint{ - Position: end, - Label: buildLabel("= " + strings.Join(values, ", ")), - PaddingLeft: true, - }) } - return hints } -func compositeLiteralFields(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, _ *types.Qualifier) []protocol.InlayHint { - compLit, ok := node.(*ast.CompositeLit) - if !ok { - return nil - } - typ := info.TypeOf(compLit) - if typ == nil { - return nil - } - typ = typesinternal.Unpointer(typ) - strct, ok := typeparams.CoreType(typ).(*types.Struct) - if !ok { - return nil - } +func compositeLiteralFields(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curCompLit := range cur.Preorder((*ast.CompositeLit)(nil)) { + compLit, ok := curCompLit.Node().(*ast.CompositeLit) + typ := info.TypeOf(compLit) + if typ == nil { + continue + } + typ = typesinternal.Unpointer(typ) + strct, ok := typeparams.CoreType(typ).(*types.Struct) + if !ok { + continue + } - var hints []protocol.InlayHint - var allEdits []protocol.TextEdit - for i, v := range compLit.Elts { - if _, ok := v.(*ast.KeyValueExpr); !ok { - start, err := m.PosPosition(tf, v.Pos()) - if err != nil { - continue - } - if i > strct.NumFields()-1 { - break + var hints []protocol.InlayHint + var allEdits []protocol.TextEdit + for i, v := range compLit.Elts { + if _, ok := v.(*ast.KeyValueExpr); !ok { + start, err := pgf.PosPosition(v.Pos()) + if err != nil { + continue + } + if i > strct.NumFields()-1 { + break + } + hints = append(hints, protocol.InlayHint{ + Position: start, + Label: buildLabel(strct.Field(i).Name() + ":"), + Kind: protocol.Parameter, + PaddingRight: true, + }) + allEdits = append(allEdits, protocol.TextEdit{ + Range: protocol.Range{Start: start, End: start}, + NewText: strct.Field(i).Name() + ": ", + }) } - hints = append(hints, protocol.InlayHint{ - Position: start, - Label: buildLabel(strct.Field(i).Name() + ":"), - Kind: protocol.Parameter, - PaddingRight: true, - }) - allEdits = append(allEdits, protocol.TextEdit{ - Range: protocol.Range{Start: start, End: start}, - NewText: strct.Field(i).Name() + ": ", - }) + } + // It is not allowed to have a mix of keyed and unkeyed fields, so + // have the text edits add keys to all fields. + for i := range hints { + hints[i].TextEdits = allEdits + add(hints[i]) } } - // It is not allowed to have a mix of keyed and unkeyed fields, so - // have the text edits add keys to all fields. - for i := range hints { - hints[i].TextEdits = allEdits - } - return hints } -func compositeLiteralTypes(node ast.Node, m *protocol.Mapper, tf *token.File, info *types.Info, q *types.Qualifier) []protocol.InlayHint { - compLit, ok := node.(*ast.CompositeLit) - if !ok { - return nil - } - typ := info.TypeOf(compLit) - if typ == nil { - return nil - } - if compLit.Type != nil { - return nil - } - prefix := "" - if t, ok := typeparams.CoreType(typ).(*types.Pointer); ok { - typ = t.Elem() - prefix = "&" - } - // The type for this composite literal is implicit, add an inlay hint. - start, err := m.PosPosition(tf, compLit.Lbrace) - if err != nil { - return nil +func compositeLiteralTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { + for curCompLit := range cur.Preorder((*ast.CompositeLit)(nil)) { + compLit := curCompLit.Node().(*ast.CompositeLit) + typ := info.TypeOf(compLit) + if typ == nil { + continue + } + if compLit.Type != nil { + continue + } + prefix := "" + if t, ok := typeparams.CoreType(typ).(*types.Pointer); ok { + typ = t.Elem() + prefix = "&" + } + // The type for this composite literal is implicit, add an inlay hint. + start, err := pgf.PosPosition(compLit.Lbrace) + if err != nil { + continue + } + add(protocol.InlayHint{ + Position: start, + Label: buildLabel(fmt.Sprintf("%s%s", prefix, types.TypeString(typ, qual))), + Kind: protocol.Type, + }) } - return []protocol.InlayHint{{ - Position: start, - Label: buildLabel(fmt.Sprintf("%s%s", prefix, types.TypeString(typ, *q))), - Kind: protocol.Type, - }} } func buildLabel(s string) []protocol.InlayHintLabelPart { diff --git a/gopls/internal/golang/references.go b/gopls/internal/golang/references.go index 3ecaab6e3e1..12152453dcd 100644 --- a/gopls/internal/golang/references.go +++ b/gopls/internal/golang/references.go @@ -602,14 +602,12 @@ func localReferences(pkg *cache.Package, targets map[types.Object]bool, correspo // Scan through syntax looking for uses of one of the target objects. for _, pgf := range pkg.CompiledGoFiles() { - ast.Inspect(pgf.File, func(n ast.Node) bool { - if id, ok := n.(*ast.Ident); ok { - if obj, ok := pkg.TypesInfo().Uses[id]; ok && matches(obj) { - report(mustLocation(pgf, id), false) - } + for curId := range pgf.Cursor.Preorder((*ast.Ident)(nil)) { + id := curId.Node().(*ast.Ident) + if obj, ok := pkg.TypesInfo().Uses[id]; ok && matches(obj) { + report(mustLocation(pgf, id), false) } - return true - }) + } } return nil } diff --git a/gopls/internal/server/link.go b/gopls/internal/server/link.go index 13097d89887..c888904baab 100644 --- a/gopls/internal/server/link.go +++ b/gopls/internal/server/link.go @@ -164,17 +164,15 @@ func goLinks(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]p // Gather links found in string literals. var str []*ast.BasicLit - ast.Inspect(pgf.File, func(node ast.Node) bool { - switch n := node.(type) { - case *ast.ImportSpec: - return false // don't process import strings again - case *ast.BasicLit: - if n.Kind == token.STRING { - str = append(str, n) + for curLit := range pgf.Cursor.Preorder((*ast.BasicLit)(nil)) { + lit := curLit.Node().(*ast.BasicLit) + if lit.Kind == token.STRING { + if _, ok := curLit.Parent().Node().(*ast.ImportSpec); ok { + continue // ignore import strings } + str = append(str, lit) } - return true - }) + } for _, s := range str { strOffset, err := safetoken.Offset(pgf.Tok, s.Pos()) if err != nil { diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 1052f65acfb..5ed177c9f3d 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -407,6 +407,8 @@ func (c Cursor) FindNode(n ast.Node) (Cursor, bool) { // FindPos returns the cursor for the innermost node n in the tree // rooted at c such that n.Pos() <= start && end <= n.End(). +// (For an *ast.File, it uses the bounds n.FileStart-n.FileEnd.) +// // It returns zero if none is found. // Precondition: start <= end. // @@ -425,10 +427,16 @@ func (c Cursor) FindPos(start, end token.Pos) (Cursor, bool) { for i, limit := c.indices(); i < limit; i++ { ev := events[i] if ev.index > i { // push? - if ev.node.Pos() > start { + n := ev.node + var nodeStart, nodeEnd token.Pos + if file, ok := n.(*ast.File); ok { + nodeStart, nodeEnd = file.FileStart, file.FileEnd + } else { + nodeStart, nodeEnd = n.Pos(), n.End() + } + if nodeStart > start { break // disjoint, after; stop } - nodeEnd := ev.node.End() if end <= nodeEnd { // node fully contains target range best = i From 3c245dad2c55e3b6e3e1635f1d5bc3da5277be83 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 19 Feb 2025 15:19:16 +0700 Subject: [PATCH 162/309] gopls: fix diagnostics integration test CL 649355 improved simplifyrange suggested fix, thus the corresponding diagnostics integration test must be updated, too. Change-Id: Ief33cc4e9ab3d760c1af28c94102638f6d2b69e8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650556 Reviewed-by: Michael Knyszek Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Cuong Manh Le Reviewed-by: Alan Donovan --- gopls/internal/test/integration/diagnostics/diagnostics_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/internal/test/integration/diagnostics/diagnostics_test.go b/gopls/internal/test/integration/diagnostics/diagnostics_test.go index c496f6464a3..a97d249e7b5 100644 --- a/gopls/internal/test/integration/diagnostics/diagnostics_test.go +++ b/gopls/internal/test/integration/diagnostics/diagnostics_test.go @@ -562,7 +562,7 @@ func _() { env.OpenFile("main.go") var d protocol.PublishDiagnosticsParams env.AfterChange( - Diagnostics(AtPosition("main.go", 5, 8)), + Diagnostics(AtPosition("main.go", 5, 6)), ReadDiagnostics("main.go", &d), ) if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 { From 44abb0ac312034f5c655a3d815ec385884038027 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 14 Feb 2025 10:16:33 -0500 Subject: [PATCH 163/309] go/types/internal/play: display type structure Display the complete recursive structure of a types.Type, in the style of ast.Fprint. Change-Id: I408c9b1f1beb214e0184381e97085e606ad8a5a1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649617 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/types/internal/play/play.go | 66 +++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/go/types/internal/play/play.go b/go/types/internal/play/play.go index eb9e5794b94..8d3b9d19346 100644 --- a/go/types/internal/play/play.go +++ b/go/types/internal/play/play.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build go1.23 + // The play program is a playground for go/types: a simple web-based // text editor into which the user can enter a Go program, select a // region, and see type information about it. @@ -35,7 +37,6 @@ import ( // TODO(adonovan): // - show line numbers next to textarea. -// - show a (tree) breakdown of the representation of the expression's type. // - mention this in the go/types tutorial. // - display versions of go/types and go command. @@ -297,6 +298,10 @@ func formatObj(out *strings.Builder, fset *token.FileSet, ref string, obj types. } fmt.Fprintf(out, "\n\n") + fmt.Fprintf(out, "Type:\n") + describeType(out, obj.Type()) + fmt.Fprintf(out, "\n") + // method set if methods := typeutil.IntuitiveMethodSet(obj.Type(), nil); len(methods) > 0 { fmt.Fprintf(out, "Methods:\n") @@ -318,6 +323,65 @@ func formatObj(out *strings.Builder, fset *token.FileSet, ref string, obj types. } } +// describeType formats t to out in a way that makes it clear what methods to call on t to +// get at its parts. +// describeType assumes t was constructed by the type checker, so it doesn't check +// for recursion. The type checker replaces recursive alias types, which are illegal, +// with a BasicType that says as much. Other types that it constructs are recursive +// only via a name, and this function does not traverse names. +func describeType(out *strings.Builder, t types.Type) { + depth := -1 + + var ft func(string, types.Type) + ft = func(prefix string, t types.Type) { + depth++ + defer func() { depth-- }() + + for range depth { + fmt.Fprint(out, ". ") + } + + fmt.Fprintf(out, "%s%T:", prefix, t) + switch t := t.(type) { + case *types.Basic: + fmt.Fprintf(out, " Name: %q\n", t.Name()) + case *types.Pointer: + fmt.Fprintln(out) + ft("Elem: ", t.Elem()) + case *types.Slice: + fmt.Fprintln(out) + ft("Elem: ", t.Elem()) + case *types.Array: + fmt.Fprintf(out, " Len: %d\n", t.Len()) + ft("Elem: ", t.Elem()) + case *types.Map: + fmt.Fprintln(out) + ft("Key: ", t.Key()) + ft("Elem: ", t.Elem()) + case *types.Chan: + fmt.Fprintf(out, " Dir: %s\n", chanDirs[t.Dir()]) + ft("Elem: ", t.Elem()) + case *types.Alias: + fmt.Fprintf(out, " Name: %q\n", t.Obj().Name()) + ft("Rhs: ", t.Rhs()) + default: + // For types we may have missed or which have too much to bother with, + // print their string representation. + // TODO(jba): print more about struct types (their fields) and interface and named + // types (their methods). + fmt.Fprintf(out, " %s\n", t) + } + } + + ft("", t) +} + +var chanDirs = []string{ + "SendRecv", + "SendOnly", + "RecvOnly", +} + func handleRoot(w http.ResponseWriter, req *http.Request) { io.WriteString(w, mainHTML) } func handleJS(w http.ResponseWriter, req *http.Request) { io.WriteString(w, mainJS) } func handleCSS(w http.ResponseWriter, req *http.Request) { io.WriteString(w, mainCSS) } From 877c1d128ba89b146547e8becff924a35cb9b322 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 19 Feb 2025 17:50:27 +0000 Subject: [PATCH 164/309] gopls: address various staticcheck findings Fix several real findings uncovered by running staticcheck ./... from the gopls module. Change-Id: Ieffd38bfd98cac24052c3b408907eb197c9e1cda Reviewed-on: https://go-review.googlesource.com/c/tools/+/650643 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/analysis/modernize/sortslice.go | 5 ++-- .../analysis/unusedvariable/unusedvariable.go | 2 +- gopls/internal/cache/analysis.go | 3 +-- gopls/internal/cache/check.go | 23 +------------------ .../cache/methodsets/fingerprint_test.go | 5 ---- gopls/internal/cache/snapshot.go | 2 +- gopls/internal/cache/source.go | 13 ++++++----- gopls/internal/cache/view.go | 3 --- gopls/internal/cmd/cmd.go | 9 ++------ gopls/internal/golang/extract.go | 2 +- gopls/internal/golang/inlay_hint.go | 3 +++ gopls/internal/golang/semtok.go | 2 +- gopls/internal/golang/workspace_symbol.go | 2 -- gopls/internal/settings/settings.go | 8 +++---- 14 files changed, 24 insertions(+), 58 deletions(-) diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index bbc8befb8ee..a033be7f635 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -5,7 +5,6 @@ package modernize import ( - "fmt" "go/ast" "go/token" "go/types" @@ -78,9 +77,9 @@ func sortslice(pass *analysis.Pass) { Pos: call.Fun.Pos(), End: call.Fun.End(), Category: "sortslice", - Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"), + Message: "sort.Slice can be modernized using slices.Sort", SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"), + Message: "Replace sort.Slice call by slices.Sort", TextEdits: append(importEdits, []analysis.TextEdit{ { // Replace sort.Slice with slices.Sort. diff --git a/gopls/internal/analysis/unusedvariable/unusedvariable.go b/gopls/internal/analysis/unusedvariable/unusedvariable.go index 5f1c188eb6a..3ea1dbe6953 100644 --- a/gopls/internal/analysis/unusedvariable/unusedvariable.go +++ b/gopls/internal/analysis/unusedvariable/unusedvariable.go @@ -47,7 +47,7 @@ func run(pass *analysis.Pass) (any, error) { if len(match) > 0 { varName := match[1] // Beginning in Go 1.23, go/types began quoting vars as `v'. - varName = strings.Trim(varName, "'`'") + varName = strings.Trim(varName, "`'") err := runForError(pass, typeErr, varName) if err != nil { diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go index d570c0a46ae..a0dd322a51e 100644 --- a/gopls/internal/cache/analysis.go +++ b/gopls/internal/cache/analysis.go @@ -822,8 +822,7 @@ func typesLookup(pkg *types.Package) func(string) *types.Package { ) // search scans children the next package in pending, looking for pkgPath. - var search func(pkgPath string) (*types.Package, int) - search = func(pkgPath string) (sought *types.Package, numPending int) { + search := func(pkgPath string) (sought *types.Package, numPending int) { mu.Lock() defer mu.Unlock() diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index a3aff5e5475..aa1537c8705 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -44,11 +44,6 @@ import ( "golang.org/x/tools/internal/versions" ) -// Various optimizations that should not affect correctness. -const ( - preserveImportGraph = true // hold on to the import graph for open packages -) - type unit = struct{} // A typeCheckBatch holds data for a logical type-checking operation, which may @@ -97,21 +92,6 @@ func (b *typeCheckBatch) getHandle(id PackageID) *packageHandle { return b._handles[id] } -// A futurePackage is a future result of type checking or importing a package, -// to be cached in a map. -// -// The goroutine that creates the futurePackage is responsible for evaluating -// its value, and closing the done channel. -type futurePackage struct { - done chan unit - v pkgOrErr -} - -type pkgOrErr struct { - pkg *types.Package - err error -} - // TypeCheck parses and type-checks the specified packages, // and returns them in the same order as the ids. // The resulting packages' types may belong to different importers, @@ -701,8 +681,7 @@ func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath // search scans children the next package in pending, looking for pkgPath. // Invariant: whenever search is called, pkgPath is not yet mapped. - var search func(pkgPath PackagePath) (PackageID, bool) - search = func(pkgPath PackagePath) (id PackageID, found bool) { + search := func(pkgPath PackagePath) (id PackageID, found bool) { pkg := pending[0] pending = pending[1:] for depPath, depID := range pkg.DepsByPkgPath { diff --git a/gopls/internal/cache/methodsets/fingerprint_test.go b/gopls/internal/cache/methodsets/fingerprint_test.go index a9f47c1a2e6..795ddaa965b 100644 --- a/gopls/internal/cache/methodsets/fingerprint_test.go +++ b/gopls/internal/cache/methodsets/fingerprint_test.go @@ -39,11 +39,6 @@ func Test_fingerprint(t *testing.T) { // (Non-tricky types only.) var fingerprints typeutil.Map - type eqclass struct { - class map[types.Type]bool - fp string - } - for _, pkg := range pkgs { switch pkg.Types.Path() { case "unsafe", "builtin": diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index c341ac6e85a..578cea61eb7 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -1301,7 +1301,7 @@ searchOverlays: // where the file is inside a workspace module, but perhaps no packages // were loaded for that module. _, loadedMod := loadedModFiles[goMod] - _, workspaceMod := s.view.viewDefinition.workspaceModFiles[goMod] + _, workspaceMod := s.view.workspaceModFiles[goMod] // If we have a relevant go.mod file, check whether the file is orphaned // due to its go.mod file being inactive. We could also offer a // prescriptive diagnostic in the case that there is no go.mod file, but diff --git a/gopls/internal/cache/source.go b/gopls/internal/cache/source.go index 3e21c641651..fa038ec37a6 100644 --- a/gopls/internal/cache/source.go +++ b/gopls/internal/cache/source.go @@ -61,9 +61,7 @@ func (s *goplsSource) ResolveReferences(ctx context.Context, filename string, mi // collect the ones that are still needed := maps.Clone(missing) for _, a := range fromWS { - if _, ok := needed[a.Package.Name]; ok { - delete(needed, a.Package.Name) - } + delete(needed, a.Package.Name) } // when debug (below) is gone, change this to: if len(needed) == 0 {return fromWS, nil} var fromCache []*result @@ -184,10 +182,13 @@ type found struct { func (s *goplsSource) resolveWorkspaceReferences(filename string, missing imports.References) ([]*imports.Result, error) { uri := protocol.URIFromPath(filename) mypkgs, err := s.S.MetadataForFile(s.ctx, uri) - if len(mypkgs) != 1 { - // what does this mean? can it happen? + if err != nil { + return nil, err + } + if len(mypkgs) == 0 { + return nil, nil } - mypkg := mypkgs[0] + mypkg := mypkgs[0] // narrowest package // search the metadata graph for package ids correstponding to missing g := s.S.MetadataGraph() var ids []metadata.PackageID diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index 6ebf6837ef2..fc1ac5724ed 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -20,7 +20,6 @@ import ( "os/exec" "path" "path/filepath" - "regexp" "slices" "sort" "strings" @@ -1253,8 +1252,6 @@ func globsMatchPath(globs, target string) bool { return false } -var modFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`) - // TODO(rfindley): clean up the redundancy of allFilesExcluded, // pathExcludedByFilterFunc, pathExcludedByFilter, view.filterFunc... func allFilesExcluded(files []string, filterFunc func(protocol.DocumentURI) bool) bool { diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index a647b3198df..f7ba04df6a4 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -310,11 +310,6 @@ func (app *Application) featureCommands() []tool.Application { } } -var ( - internalMu sync.Mutex - internalConnections = make(map[string]*connection) -) - // connect creates and initializes a new in-process gopls session. func (app *Application) connect(ctx context.Context) (*connection, error) { client := newClient(app) @@ -377,10 +372,10 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti params.InitializationOptions = map[string]interface{}{ "symbolMatcher": string(opts.SymbolMatcher), } - if c.initializeResult, err = c.Server.Initialize(ctx, params); err != nil { + if c.initializeResult, err = c.Initialize(ctx, params); err != nil { return err } - if err := c.Server.Initialized(ctx, &protocol.InitializedParams{}); err != nil { + if err := c.Initialized(ctx, &protocol.InitializedParams{}); err != nil { return err } return nil diff --git a/gopls/internal/golang/extract.go b/gopls/internal/golang/extract.go index 8c8758d9f0a..b8219562de5 100644 --- a/gopls/internal/golang/extract.go +++ b/gopls/internal/golang/extract.go @@ -375,7 +375,7 @@ func stmtToInsertVarBefore(path []ast.Node, variables []*variable) (ast.Stmt, er } return parent, nil } - return enclosingStmt.(ast.Stmt), nil + return enclosingStmt, nil } // canExtractVariable reports whether the code in the given range can be diff --git a/gopls/internal/golang/inlay_hint.go b/gopls/internal/golang/inlay_hint.go index 84b18e06781..b49ebd85e21 100644 --- a/gopls/internal/golang/inlay_hint.go +++ b/gopls/internal/golang/inlay_hint.go @@ -255,6 +255,9 @@ func constantValues(info *types.Info, pgf *parsego.File, qual types.Qualifier, c func compositeLiteralFields(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur cursor.Cursor, add func(protocol.InlayHint)) { for curCompLit := range cur.Preorder((*ast.CompositeLit)(nil)) { compLit, ok := curCompLit.Node().(*ast.CompositeLit) + if !ok { + continue + } typ := info.TypeOf(compLit) if typ == nil { continue diff --git a/gopls/internal/golang/semtok.go b/gopls/internal/golang/semtok.go index cb3f2cfd478..121531d8280 100644 --- a/gopls/internal/golang/semtok.go +++ b/gopls/internal/golang/semtok.go @@ -616,7 +616,7 @@ func (tv *tokenVisitor) ident(id *ast.Ident) { obj types.Object ok bool ) - if obj, ok = tv.info.Defs[id]; obj != nil { + if obj, _ = tv.info.Defs[id]; obj != nil { // definition mods = append(mods, semtok.ModDefinition) tok, mods = tv.appendObjectModifiers(mods, obj) diff --git a/gopls/internal/golang/workspace_symbol.go b/gopls/internal/golang/workspace_symbol.go index feba6081515..89c144b9230 100644 --- a/gopls/internal/golang/workspace_symbol.go +++ b/gopls/internal/golang/workspace_symbol.go @@ -293,14 +293,12 @@ func (c comboMatcher) match(chunks []string) (int, float64) { func collectSymbols(ctx context.Context, snapshots []*cache.Snapshot, matcherType settings.SymbolMatcher, symbolizer symbolizer, query string) ([]protocol.SymbolInformation, error) { // Extract symbols from all files. var work []symbolFile - var roots []string seen := make(map[protocol.DocumentURI]*metadata.Package) // only scan each file once for _, snapshot := range snapshots { // Use the root view URIs for determining (lexically) // whether a URI is in any open workspace. folderURI := snapshot.Folder() - roots = append(roots, strings.TrimRight(string(folderURI), "/")) filters := snapshot.Options().DirectoryFilters filterer := cache.NewFilterer(filters) diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 7d64cbef459..393bccac312 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -740,8 +740,8 @@ type ImportsSourceEnum string const ( ImportsSourceOff ImportsSourceEnum = "off" - ImportsSourceGopls = "gopls" - ImportsSourceGoimports = "goimports" + ImportsSourceGopls ImportsSourceEnum = "gopls" + ImportsSourceGoimports ImportsSourceEnum = "goimports" ) type Matcher string @@ -967,7 +967,7 @@ func validateDirectoryFilter(ifilter string) (string, error) { if seg != "**" { for _, op := range unsupportedOps { if strings.Contains(seg, op) { - return "", fmt.Errorf("invalid filter %v, operator %v not supported. If you want to have this operator supported, consider filing an issue.", filter, op) + return "", fmt.Errorf("invalid filter %v, operator %v not supported. If you want to have this operator supported, consider filing an issue", filter, op) } } } @@ -1374,7 +1374,7 @@ func (e *SoftError) Error() string { // deprecatedError reports the current setting as deprecated. // The optional replacement is suggested to the user. func deprecatedError(replacement string) error { - msg := fmt.Sprintf("this setting is deprecated") + msg := "this setting is deprecated" if replacement != "" { msg = fmt.Sprintf("%s, use %q instead", msg, replacement) } From 4991e7daac565a1cb14d843e78a63a1a91f726d4 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 18 Feb 2025 16:04:35 -0500 Subject: [PATCH 165/309] gopls/internal/golang: use pgf.Cursor in CodeAction fix This CL pushes down the pgf.Cursor into internal interfaces so that it is avaiable where needed. The existing implementations have not been updated to use it. As a rule of thumb, any place that calls PathEnclosingInterval would be better off using Cursor; the exception is when there's a public API that accepts a 'path []Node'. Change-Id: I18313809808fa83cc1f2a1d51850a9fdcf43ecdb Reviewed-on: https://go-review.googlesource.com/c/tools/+/650398 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley Commit-Queue: Alan Donovan --- .../analysis/fillstruct/fillstruct.go | 7 +-- gopls/internal/golang/codeaction.go | 20 ++++----- gopls/internal/golang/extract.go | 44 ++++++++++++------- gopls/internal/golang/fix.go | 6 +-- gopls/internal/golang/invertifcondition.go | 9 ++-- gopls/internal/golang/lines.go | 22 ++++++---- gopls/internal/golang/stub.go | 6 +-- .../golang/stubmethods/stubcalledfunc.go | 6 ++- .../golang/stubmethods/stubmethods.go | 9 +++- gopls/internal/golang/undeclared.go | 5 ++- gopls/internal/util/typesutil/typesutil.go | 2 + 11 files changed, 84 insertions(+), 52 deletions(-) diff --git a/gopls/internal/analysis/fillstruct/fillstruct.go b/gopls/internal/analysis/fillstruct/fillstruct.go index a8a861f0651..641b98e6fa7 100644 --- a/gopls/internal/analysis/fillstruct/fillstruct.go +++ b/gopls/internal/analysis/fillstruct/fillstruct.go @@ -28,6 +28,7 @@ import ( "golang.org/x/tools/gopls/internal/fuzzy" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/typesinternal" ) @@ -129,15 +130,15 @@ const FixCategory = "fillstruct" // recognized by gopls ApplyFix // SuggestedFix computes the suggested fix for the kinds of // diagnostics produced by the Analyzer above. -func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { +func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { if info == nil { return nil, nil, fmt.Errorf("nil types.Info") } pos := start // don't use the end - // TODO(rstambler): Using ast.Inspect would probably be more efficient than - // calling PathEnclosingInterval. Switch this approach. + // TODO(adonovan): simplify, using Cursor. + file := curFile.Node().(*ast.File) path, _ := astutil.PathEnclosingInterval(file, pos, pos) if len(path) == 0 { return nil, nil, fmt.Errorf("no enclosing ast.Node") diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index f82c32d6a9c..49a861852ff 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -325,8 +325,7 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error { case strings.Contains(msg, "missing method"), strings.HasPrefix(msg, "cannot convert"), strings.Contains(msg, "not implement"): - path, _ := astutil.PathEnclosingInterval(req.pgf.File, start, end) - si := stubmethods.GetIfaceStubInfo(req.pkg.FileSet(), info, path, start) + si := stubmethods.GetIfaceStubInfo(req.pkg.FileSet(), info, req.pgf, start, end) if si != nil { qual := typesinternal.FileQualifier(req.pgf.File, si.Concrete.Obj().Pkg()) iface := types.TypeString(si.Interface.Type(), qual) @@ -338,8 +337,7 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error { // Offer a "Declare missing method T.f" code action. // See [stubMissingCalledFunctionFixer] for command implementation. case strings.Contains(msg, "has no field or method"): - path, _ := astutil.PathEnclosingInterval(req.pgf.File, start, end) - si := stubmethods.GetCallStubInfo(req.pkg.FileSet(), info, path, start) + si := stubmethods.GetCallStubInfo(req.pkg.FileSet(), info, req.pgf, start, end) if si != nil { msg := fmt.Sprintf("Declare missing method %s.%s", si.Receiver.Obj().Name(), si.MethodName) req.addApplyFixAction(msg, fixMissingCalledFunction, req.loc) @@ -462,7 +460,7 @@ func goDoc(ctx context.Context, req *codeActionsRequest) error { // refactorExtractFunction produces "Extract function" code actions. // See [extractFunction] for command implementation. func refactorExtractFunction(ctx context.Context, req *codeActionsRequest) error { - if _, ok, _, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.File); ok { + if _, ok, _, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.Cursor); ok { req.addApplyFixAction("Extract function", fixExtractFunction, req.loc) } return nil @@ -471,7 +469,7 @@ func refactorExtractFunction(ctx context.Context, req *codeActionsRequest) error // refactorExtractMethod produces "Extract method" code actions. // See [extractMethod] for command implementation. func refactorExtractMethod(ctx context.Context, req *codeActionsRequest) error { - if _, ok, methodOK, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.File); ok && methodOK { + if _, ok, methodOK, _ := canExtractFunction(req.pgf.Tok, req.start, req.end, req.pgf.Src, req.pgf.Cursor); ok && methodOK { req.addApplyFixAction("Extract method", fixExtractMethod, req.loc) } return nil @@ -481,7 +479,7 @@ func refactorExtractMethod(ctx context.Context, req *codeActionsRequest) error { // See [extractVariable] for command implementation. func refactorExtractVariable(ctx context.Context, req *codeActionsRequest) error { info := req.pkg.TypesInfo() - if exprs, err := canExtractVariable(info, req.pgf.File, req.start, req.end, false); err == nil { + if exprs, err := canExtractVariable(info, req.pgf.Cursor, req.start, req.end, false); err == nil { // Offer one of refactor.extract.{constant,variable} // based on the constness of the expression; this is a // limitation of the codeActionProducers mechanism. @@ -507,7 +505,7 @@ func refactorExtractVariableAll(ctx context.Context, req *codeActionsRequest) er info := req.pkg.TypesInfo() // Don't suggest if only one expr is found, // otherwise it will duplicate with [refactorExtractVariable] - if exprs, err := canExtractVariable(info, req.pgf.File, req.start, req.end, true); err == nil && len(exprs) > 1 { + if exprs, err := canExtractVariable(info, req.pgf.Cursor, req.start, req.end, true); err == nil && len(exprs) > 1 { start, end, err := req.pgf.NodeOffsets(exprs[0]) if err != nil { return err @@ -664,7 +662,7 @@ func refactorRewriteChangeQuote(ctx context.Context, req *codeActionsRequest) er // refactorRewriteInvertIf produces "Invert 'if' condition" code actions. // See [invertIfCondition] for command implementation. func refactorRewriteInvertIf(ctx context.Context, req *codeActionsRequest) error { - if _, ok, _ := canInvertIfCondition(req.pgf.File, req.start, req.end); ok { + if _, ok, _ := canInvertIfCondition(req.pgf.Cursor, req.start, req.end); ok { req.addApplyFixAction("Invert 'if' condition", fixInvertIfCondition, req.loc) } return nil @@ -674,7 +672,7 @@ func refactorRewriteInvertIf(ctx context.Context, req *codeActionsRequest) error // See [splitLines] for command implementation. func refactorRewriteSplitLines(ctx context.Context, req *codeActionsRequest) error { // TODO(adonovan): opt: don't set needPkg just for FileSet. - if msg, ok, _ := canSplitLines(req.pgf.File, req.pkg.FileSet(), req.start, req.end); ok { + if msg, ok, _ := canSplitLines(req.pgf.Cursor, req.pkg.FileSet(), req.start, req.end); ok { req.addApplyFixAction(msg, fixSplitLines, req.loc) } return nil @@ -684,7 +682,7 @@ func refactorRewriteSplitLines(ctx context.Context, req *codeActionsRequest) err // See [joinLines] for command implementation. func refactorRewriteJoinLines(ctx context.Context, req *codeActionsRequest) error { // TODO(adonovan): opt: don't set needPkg just for FileSet. - if msg, ok, _ := canJoinLines(req.pgf.File, req.pkg.FileSet(), req.start, req.end); ok { + if msg, ok, _ := canJoinLines(req.pgf.Cursor, req.pkg.FileSet(), req.start, req.end); ok { req.addApplyFixAction(msg, fixJoinLines, req.loc) } return nil diff --git a/gopls/internal/golang/extract.go b/gopls/internal/golang/extract.go index b8219562de5..3d2b880db2d 100644 --- a/gopls/internal/golang/extract.go +++ b/gopls/internal/golang/extract.go @@ -24,17 +24,18 @@ import ( "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) // extractVariable implements the refactor.extract.{variable,constant} CodeAction command. -func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractExprs(fset, start, end, src, file, info, false) +func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractExprs(fset, start, end, src, curFile, info, false) } // extractVariableAll implements the refactor.extract.{variable,constant}-all CodeAction command. -func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractExprs(fset, start, end, src, file, info, true) +func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractExprs(fset, start, end, src, curFile, info, true) } // extractExprs replaces occurrence(s) of a specified expression within the same function @@ -43,9 +44,11 @@ func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, f // // The new variable/constant is declared as close as possible to the first found expression // within the deepest common scope accessible to all candidate occurrences. -func extractExprs(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, info *types.Info, all bool) (*token.FileSet, *analysis.SuggestedFix, error) { +func extractExprs(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, info *types.Info, all bool) (*token.FileSet, *analysis.SuggestedFix, error) { + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. tokFile := fset.File(file.FileStart) - exprs, err := canExtractVariable(info, file, start, end, all) + exprs, err := canExtractVariable(info, curFile, start, end, all) if err != nil { return nil, nil, fmt.Errorf("cannot extract: %v", err) } @@ -381,10 +384,12 @@ func stmtToInsertVarBefore(path []ast.Node, variables []*variable) (ast.Stmt, er // canExtractVariable reports whether the code in the given range can be // extracted to a variable (or constant). It returns the selected expression or, if 'all', // all structurally equivalent expressions within the same function body, in lexical order. -func canExtractVariable(info *types.Info, file *ast.File, start, end token.Pos, all bool) ([]ast.Expr, error) { +func canExtractVariable(info *types.Info, curFile cursor.Cursor, start, end token.Pos, all bool) ([]ast.Expr, error) { if start == end { return nil, fmt.Errorf("empty selection") } + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. path, exact := astutil.PathEnclosingInterval(file, start, end) if !exact { return nil, fmt.Errorf("selection is not an expression") @@ -571,13 +576,13 @@ type returnVariable struct { } // extractMethod refactors the selected block of code into a new method. -func extractMethod(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractFunctionMethod(fset, start, end, src, file, pkg, info, true) +func extractMethod(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractFunctionMethod(fset, start, end, src, curFile, pkg, info, true) } // extractFunction refactors the selected block of code into a new function. -func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractFunctionMethod(fset, start, end, src, file, pkg, info, false) +func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractFunctionMethod(fset, start, end, src, curFile, pkg, info, false) } // extractFunctionMethod refactors the selected block of code into a new function/method. @@ -588,17 +593,19 @@ func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, file // and return values of the extracted function/method. Lastly, we construct the call // of the function/method and insert this call as well as the extracted function/method into // their proper locations. -func extractFunctionMethod(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info, isMethod bool) (*token.FileSet, *analysis.SuggestedFix, error) { +func extractFunctionMethod(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info, isMethod bool) (*token.FileSet, *analysis.SuggestedFix, error) { errorPrefix := "extractFunction" if isMethod { errorPrefix = "extractMethod" } + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. tok := fset.File(file.FileStart) if tok == nil { return nil, nil, bug.Errorf("no file for position") } - p, ok, methodOk, err := canExtractFunction(tok, start, end, src, file) + p, ok, methodOk, err := canExtractFunction(tok, start, end, src, curFile) if (!ok && !isMethod) || (!methodOk && isMethod) { return nil, nil, fmt.Errorf("%s: cannot extract %s: %v", errorPrefix, safetoken.StartPosition(fset, start), err) @@ -1086,7 +1093,10 @@ func moveParamToFrontIfFound(params []ast.Expr, paramTypes []*ast.Field, x, sel // their cursors for whitespace. To support this use case, we must manually adjust the // ranges to match the correct AST node. In this particular example, we would adjust // rng.Start forward to the start of 'if' and rng.End backward to after '}'. -func adjustRangeForCommentsAndWhiteSpace(tok *token.File, start, end token.Pos, content []byte, file *ast.File) (token.Pos, token.Pos, error) { +func adjustRangeForCommentsAndWhiteSpace(tok *token.File, start, end token.Pos, content []byte, curFile cursor.Cursor) (token.Pos, token.Pos, error) { + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. + // Adjust the end of the range to after leading whitespace and comments. prevStart := token.NoPos startComment := sort.Search(len(file.Comments), func(i int) bool { @@ -1410,12 +1420,14 @@ type fnExtractParams struct { // canExtractFunction reports whether the code in the given range can be // extracted to a function. -func canExtractFunction(tok *token.File, start, end token.Pos, src []byte, file *ast.File) (*fnExtractParams, bool, bool, error) { +func canExtractFunction(tok *token.File, start, end token.Pos, src []byte, curFile cursor.Cursor) (*fnExtractParams, bool, bool, error) { if start == end { return nil, false, false, fmt.Errorf("start and end are equal") } var err error - start, end, err = adjustRangeForCommentsAndWhiteSpace(tok, start, end, src, file) + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. + start, end, err = adjustRangeForCommentsAndWhiteSpace(tok, start, end, src, curFile) if err != nil { return nil, false, false, err } diff --git a/gopls/internal/golang/fix.go b/gopls/internal/golang/fix.go index e812c677541..2c14d09c218 100644 --- a/gopls/internal/golang/fix.go +++ b/gopls/internal/golang/fix.go @@ -7,7 +7,6 @@ package golang import ( "context" "fmt" - "go/ast" "go/token" "go/types" @@ -20,6 +19,7 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/imports" ) @@ -47,12 +47,12 @@ type fixer func(ctx context.Context, s *cache.Snapshot, pkg *cache.Package, pgf // TODO(adonovan): move fillstruct and undeclaredname into this // package, so we can remove the import restriction and push // the singleFile wrapper down into each singleFileFixer? -type singleFileFixer func(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) +type singleFileFixer func(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) // singleFile adapts a single-file fixer to a Fixer. func singleFile(fixer1 singleFileFixer) fixer { return func(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { - return fixer1(pkg.FileSet(), start, end, pgf.Src, pgf.File, pkg.Types(), pkg.TypesInfo()) + return fixer1(pkg.FileSet(), start, end, pgf.Src, pgf.Cursor, pkg.Types(), pkg.TypesInfo()) } } diff --git a/gopls/internal/golang/invertifcondition.go b/gopls/internal/golang/invertifcondition.go index 0fb7d1e4d0a..b26618ebf93 100644 --- a/gopls/internal/golang/invertifcondition.go +++ b/gopls/internal/golang/invertifcondition.go @@ -14,11 +14,12 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil/cursor" ) // invertIfCondition is a singleFileFixFunc that inverts an if/else statement -func invertIfCondition(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - ifStatement, _, err := canInvertIfCondition(file, start, end) +func invertIfCondition(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + ifStatement, _, err := canInvertIfCondition(curFile, start, end) if err != nil { return nil, nil, err } @@ -241,7 +242,9 @@ func invertAndOr(fset *token.FileSet, expr *ast.BinaryExpr, src []byte) ([]byte, // canInvertIfCondition reports whether we can do invert-if-condition on the // code in the given range. -func canInvertIfCondition(file *ast.File, start, end token.Pos) (*ast.IfStmt, bool, error) { +func canInvertIfCondition(curFile cursor.Cursor, start, end token.Pos) (*ast.IfStmt, bool, error) { + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. path, _ := astutil.PathEnclosingInterval(file, start, end) for _, node := range path { stmt, isIfStatement := node.(*ast.IfStmt) diff --git a/gopls/internal/golang/lines.go b/gopls/internal/golang/lines.go index b6a9823957d..f208e33e0c3 100644 --- a/gopls/internal/golang/lines.go +++ b/gopls/internal/golang/lines.go @@ -20,12 +20,13 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil/cursor" ) // canSplitLines checks whether we can split lists of elements inside // an enclosing curly bracket/parens into separate lines. -func canSplitLines(file *ast.File, fset *token.FileSet, start, end token.Pos) (string, bool, error) { - itemType, items, comments, _, _, _ := findSplitJoinTarget(fset, file, nil, start, end) +func canSplitLines(curFile cursor.Cursor, fset *token.FileSet, start, end token.Pos) (string, bool, error) { + itemType, items, comments, _, _, _ := findSplitJoinTarget(fset, curFile, nil, start, end) if itemType == "" { return "", false, nil } @@ -47,8 +48,8 @@ func canSplitLines(file *ast.File, fset *token.FileSet, start, end token.Pos) (s // canJoinLines checks whether we can join lists of elements inside an // enclosing curly bracket/parens into a single line. -func canJoinLines(file *ast.File, fset *token.FileSet, start, end token.Pos) (string, bool, error) { - itemType, items, comments, _, _, _ := findSplitJoinTarget(fset, file, nil, start, end) +func canJoinLines(curFile cursor.Cursor, fset *token.FileSet, start, end token.Pos) (string, bool, error) { + itemType, items, comments, _, _, _ := findSplitJoinTarget(fset, curFile, nil, start, end) if itemType == "" { return "", false, nil } @@ -84,8 +85,8 @@ func canSplitJoinLines(items []ast.Node, comments []*ast.CommentGroup) bool { } // splitLines is a singleFile fixer. -func splitLines(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - itemType, items, comments, indent, braceOpen, braceClose := findSplitJoinTarget(fset, file, src, start, end) +func splitLines(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + itemType, items, comments, indent, braceOpen, braceClose := findSplitJoinTarget(fset, curFile, src, start, end) if itemType == "" { return nil, nil, nil // no fix available } @@ -94,8 +95,8 @@ func splitLines(fset *token.FileSet, start, end token.Pos, src []byte, file *ast } // joinLines is a singleFile fixer. -func joinLines(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - itemType, items, comments, _, braceOpen, braceClose := findSplitJoinTarget(fset, file, src, start, end) +func joinLines(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { + itemType, items, comments, _, braceOpen, braceClose := findSplitJoinTarget(fset, curFile, src, start, end) if itemType == "" { return nil, nil, nil // no fix available } @@ -166,11 +167,14 @@ func processLines(fset *token.FileSet, items []ast.Node, comments []*ast.Comment } // findSplitJoinTarget returns the first curly bracket/parens that encloses the current cursor. -func findSplitJoinTarget(fset *token.FileSet, file *ast.File, src []byte, start, end token.Pos) (itemType string, items []ast.Node, comments []*ast.CommentGroup, indent string, open, close token.Pos) { +func findSplitJoinTarget(fset *token.FileSet, curFile cursor.Cursor, src []byte, start, end token.Pos) (itemType string, items []ast.Node, comments []*ast.CommentGroup, indent string, open, close token.Pos) { isCursorInside := func(nodePos, nodeEnd token.Pos) bool { return nodePos < start && end < nodeEnd } + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. + findTarget := func() (targetType string, target ast.Node, open, close token.Pos) { path, _ := astutil.PathEnclosingInterval(file, start, end) for _, node := range path { diff --git a/gopls/internal/golang/stub.go b/gopls/internal/golang/stub.go index a04a82988c5..c85080f8a0c 100644 --- a/gopls/internal/golang/stub.go +++ b/gopls/internal/golang/stub.go @@ -31,8 +31,7 @@ import ( // methods of the concrete type that is assigned to an interface type // at the cursor position. func stubMissingInterfaceMethodsFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { - nodes, _ := astutil.PathEnclosingInterval(pgf.File, start, end) - si := stubmethods.GetIfaceStubInfo(pkg.FileSet(), pkg.TypesInfo(), nodes, start) + si := stubmethods.GetIfaceStubInfo(pkg.FileSet(), pkg.TypesInfo(), pgf, start, end) if si == nil { return nil, nil, fmt.Errorf("nil interface request") } @@ -43,8 +42,7 @@ func stubMissingInterfaceMethodsFixer(ctx context.Context, snapshot *cache.Snaps // method that the user may want to generate based on CallExpr // at the cursor position. func stubMissingCalledFunctionFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { - nodes, _ := astutil.PathEnclosingInterval(pgf.File, start, end) - si := stubmethods.GetCallStubInfo(pkg.FileSet(), pkg.TypesInfo(), nodes, start) + si := stubmethods.GetCallStubInfo(pkg.FileSet(), pkg.TypesInfo(), pgf, start, end) if si == nil { return nil, nil, fmt.Errorf("invalid type request") } diff --git a/gopls/internal/golang/stubmethods/stubcalledfunc.go b/gopls/internal/golang/stubmethods/stubcalledfunc.go index 1b1b6aba7de..b4b59340d83 100644 --- a/gopls/internal/golang/stubmethods/stubcalledfunc.go +++ b/gopls/internal/golang/stubmethods/stubcalledfunc.go @@ -13,6 +13,8 @@ import ( "strings" "unicode" + "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/util/typesutil" "golang.org/x/tools/internal/typesinternal" ) @@ -34,7 +36,9 @@ type CallStubInfo struct { // GetCallStubInfo extracts necessary information to generate a method definition from // a CallExpr. -func GetCallStubInfo(fset *token.FileSet, info *types.Info, path []ast.Node, pos token.Pos) *CallStubInfo { +func GetCallStubInfo(fset *token.FileSet, info *types.Info, pgf *parsego.File, start, end token.Pos) *CallStubInfo { + // TODO(adonovan): simplify, using pgf.Cursor. + path, _ := astutil.PathEnclosingInterval(pgf.File, start, end) for i, n := range path { switch n := n.(type) { case *ast.CallExpr: diff --git a/gopls/internal/golang/stubmethods/stubmethods.go b/gopls/internal/golang/stubmethods/stubmethods.go index f380f5b984d..a060993b1ab 100644 --- a/gopls/internal/golang/stubmethods/stubmethods.go +++ b/gopls/internal/golang/stubmethods/stubmethods.go @@ -15,8 +15,10 @@ import ( "go/types" "strings" + "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/internal/typesinternal" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/typesutil" ) @@ -49,7 +51,12 @@ type IfaceStubInfo struct { // function call. This is essentially what the refactor/satisfy does, // more generally. Refactor to share logic, after auditing 'satisfy' // for safety on ill-typed code. -func GetIfaceStubInfo(fset *token.FileSet, info *types.Info, path []ast.Node, pos token.Pos) *IfaceStubInfo { +func GetIfaceStubInfo(fset *token.FileSet, info *types.Info, pgf *parsego.File, pos, end token.Pos) *IfaceStubInfo { + // TODO(adonovan): simplify, using Cursor: + // curErr, _ := pgf.Cursor.FindPos(pos, end) + // for cur := range curErr.Ancestors() { + // switch n := cur.Node().(type) {... + path, _ := astutil.PathEnclosingInterval(pgf.File, pos, end) for _, n := range path { switch n := n.(type) { case *ast.ValueSpec: diff --git a/gopls/internal/golang/undeclared.go b/gopls/internal/golang/undeclared.go index 0615386e9bf..f9331348f47 100644 --- a/gopls/internal/golang/undeclared.go +++ b/gopls/internal/golang/undeclared.go @@ -17,6 +17,7 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/gopls/internal/util/typesutil" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) @@ -69,8 +70,10 @@ func undeclaredFixTitle(path []ast.Node, errMsg string) string { } // createUndeclared generates a suggested declaration for an undeclared variable or function. -func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { +func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { pos := start // don't use the end + file := curFile.Node().(*ast.File) + // TODO(adonovan): simplify, using Cursor. path, _ := astutil.PathEnclosingInterval(file, pos, pos) if len(path) < 2 { return nil, nil, fmt.Errorf("no expression found") diff --git a/gopls/internal/util/typesutil/typesutil.go b/gopls/internal/util/typesutil/typesutil.go index 79042a24901..4b5c5e7fd4f 100644 --- a/gopls/internal/util/typesutil/typesutil.go +++ b/gopls/internal/util/typesutil/typesutil.go @@ -42,6 +42,8 @@ func FormatTypeParams(tparams *types.TypeParamList) string { // the hole that must be filled by EXPR has type (string, int). // // It returns nil on failure. +// +// TODO(adonovan): simplify using Cursor. func TypesFromContext(info *types.Info, path []ast.Node, pos token.Pos) []types.Type { anyType := types.Universe.Lookup("any").Type() var typs []types.Type From cd01e86527e7f9c4cb689b66c5313bf739674c09 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Feb 2025 12:22:56 -0500 Subject: [PATCH 166/309] gopls/internal/golang: make singleFileFixer like fixer In the past the singleFileFixer has two roles: to simplify the signature for fixers of a single file, and to allow them to be expressed only in terms of go/{ast,types} data types, allowing fixers to be more loosely coupled to gopls. But that latter role seems unimportant now, so this CL simplifies the two functions to make them more alike. Change-Id: I42ee9fd275e344fafee0b27b9861f8f599f89e3e Reviewed-on: https://go-review.googlesource.com/c/tools/+/650641 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../analysis/fillstruct/fillstruct.go | 21 ++++----- gopls/internal/golang/extract.go | 43 ++++++++++++------- gopls/internal/golang/fix.go | 18 +++----- gopls/internal/golang/invertifcondition.go | 12 ++++-- gopls/internal/golang/lines.go | 17 +++++--- gopls/internal/golang/undeclared.go | 19 +++++--- 6 files changed, 76 insertions(+), 54 deletions(-) diff --git a/gopls/internal/analysis/fillstruct/fillstruct.go b/gopls/internal/analysis/fillstruct/fillstruct.go index 641b98e6fa7..62f7d77f58e 100644 --- a/gopls/internal/analysis/fillstruct/fillstruct.go +++ b/gopls/internal/analysis/fillstruct/fillstruct.go @@ -25,10 +25,11 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/fuzzy" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/analysisinternal" - "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/typesinternal" ) @@ -130,15 +131,15 @@ const FixCategory = "fillstruct" // recognized by gopls ApplyFix // SuggestedFix computes the suggested fix for the kinds of // diagnostics produced by the Analyzer above. -func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - if info == nil { - return nil, nil, fmt.Errorf("nil types.Info") - } - - pos := start // don't use the end - +func SuggestedFix(cpkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + var ( + fset = cpkg.FileSet() + pkg = cpkg.Types() + info = cpkg.TypesInfo() + pos = start // don't use end + ) // TODO(adonovan): simplify, using Cursor. - file := curFile.Node().(*ast.File) + file := pgf.Cursor.Node().(*ast.File) path, _ := astutil.PathEnclosingInterval(file, pos, pos) if len(path) == 0 { return nil, nil, fmt.Errorf("no enclosing ast.Node") @@ -235,7 +236,7 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, cur } // Find the line on which the composite literal is declared. - split := bytes.Split(content, []byte("\n")) + split := bytes.Split(pgf.Src, []byte("\n")) lineNumber := safetoken.StartPosition(fset, expr.Lbrace).Line firstLine := split[lineNumber-1] // lines are 1-indexed diff --git a/gopls/internal/golang/extract.go b/gopls/internal/golang/extract.go index 3d2b880db2d..f73e772e676 100644 --- a/gopls/internal/golang/extract.go +++ b/gopls/internal/golang/extract.go @@ -20,6 +20,8 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" goplsastutil "golang.org/x/tools/gopls/internal/util/astutil" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" @@ -29,13 +31,13 @@ import ( ) // extractVariable implements the refactor.extract.{variable,constant} CodeAction command. -func extractVariable(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractExprs(fset, start, end, src, curFile, info, false) +func extractVariable(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractExprs(pkg, pgf, start, end, false) } // extractVariableAll implements the refactor.extract.{variable,constant}-all CodeAction command. -func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractExprs(fset, start, end, src, curFile, info, true) +func extractVariableAll(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractExprs(pkg, pgf, start, end, true) } // extractExprs replaces occurrence(s) of a specified expression within the same function @@ -44,11 +46,15 @@ func extractVariableAll(fset *token.FileSet, start, end token.Pos, src []byte, c // // The new variable/constant is declared as close as possible to the first found expression // within the deepest common scope accessible to all candidate occurrences. -func extractExprs(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, info *types.Info, all bool) (*token.FileSet, *analysis.SuggestedFix, error) { - file := curFile.Node().(*ast.File) +func extractExprs(pkg *cache.Package, pgf *parsego.File, start, end token.Pos, all bool) (*token.FileSet, *analysis.SuggestedFix, error) { + var ( + fset = pkg.FileSet() + info = pkg.TypesInfo() + file = pgf.File + ) // TODO(adonovan): simplify, using Cursor. tokFile := fset.File(file.FileStart) - exprs, err := canExtractVariable(info, curFile, start, end, all) + exprs, err := canExtractVariable(info, pgf.Cursor, start, end, all) if err != nil { return nil, nil, fmt.Errorf("cannot extract: %v", err) } @@ -157,7 +163,7 @@ Outer: return nil, nil, fmt.Errorf("cannot find location to insert extraction: %v", err) } // Within function: compute appropriate statement indentation. - indent, err := calculateIndentation(src, tokFile, before) + indent, err := calculateIndentation(pgf.Src, tokFile, before) if err != nil { return nil, nil, err } @@ -576,13 +582,13 @@ type returnVariable struct { } // extractMethod refactors the selected block of code into a new method. -func extractMethod(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractFunctionMethod(fset, start, end, src, curFile, pkg, info, true) +func extractMethod(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractFunctionMethod(pkg, pgf, start, end, true) } // extractFunction refactors the selected block of code into a new function. -func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - return extractFunctionMethod(fset, start, end, src, curFile, pkg, info, false) +func extractFunction(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + return extractFunctionMethod(pkg, pgf, start, end, false) } // extractFunctionMethod refactors the selected block of code into a new function/method. @@ -593,19 +599,26 @@ func extractFunction(fset *token.FileSet, start, end token.Pos, src []byte, curF // and return values of the extracted function/method. Lastly, we construct the call // of the function/method and insert this call as well as the extracted function/method into // their proper locations. -func extractFunctionMethod(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info, isMethod bool) (*token.FileSet, *analysis.SuggestedFix, error) { +func extractFunctionMethod(cpkg *cache.Package, pgf *parsego.File, start, end token.Pos, isMethod bool) (*token.FileSet, *analysis.SuggestedFix, error) { + var ( + fset = cpkg.FileSet() + pkg = cpkg.Types() + info = cpkg.TypesInfo() + src = pgf.Src + ) + errorPrefix := "extractFunction" if isMethod { errorPrefix = "extractMethod" } - file := curFile.Node().(*ast.File) + file := pgf.Cursor.Node().(*ast.File) // TODO(adonovan): simplify, using Cursor. tok := fset.File(file.FileStart) if tok == nil { return nil, nil, bug.Errorf("no file for position") } - p, ok, methodOk, err := canExtractFunction(tok, start, end, src, curFile) + p, ok, methodOk, err := canExtractFunction(tok, start, end, src, pgf.Cursor) if (!ok && !isMethod) || (!methodOk && isMethod) { return nil, nil, fmt.Errorf("%s: cannot extract %s: %v", errorPrefix, safetoken.StartPosition(fset, start), err) diff --git a/gopls/internal/golang/fix.go b/gopls/internal/golang/fix.go index 2c14d09c218..dbd83ef071f 100644 --- a/gopls/internal/golang/fix.go +++ b/gopls/internal/golang/fix.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "go/token" - "go/types" "golang.org/x/tools/go/analysis" "golang.org/x/tools/gopls/internal/analysis/embeddirective" @@ -19,7 +18,6 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" - "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/imports" ) @@ -41,18 +39,14 @@ import ( // A fixer may return (nil, nil) if no fix is available. type fixer func(ctx context.Context, s *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) -// A singleFileFixer is a Fixer that inspects only a single file, -// and does not depend on data types from the cache package. -// -// TODO(adonovan): move fillstruct and undeclaredname into this -// package, so we can remove the import restriction and push -// the singleFile wrapper down into each singleFileFixer? -type singleFileFixer func(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) +// A singleFileFixer is a [fixer] that inspects only a single file. +type singleFileFixer func(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) -// singleFile adapts a single-file fixer to a Fixer. +// singleFile adapts a [singleFileFixer] to a [fixer] +// by discarding the snapshot and the context it needs. func singleFile(fixer1 singleFileFixer) fixer { - return func(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { - return fixer1(pkg.FileSet(), start, end, pgf.Src, pgf.Cursor, pkg.Types(), pkg.TypesInfo()) + return func(_ context.Context, _ *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + return fixer1(pkg, pgf, start, end) } } diff --git a/gopls/internal/golang/invertifcondition.go b/gopls/internal/golang/invertifcondition.go index b26618ebf93..012278df79e 100644 --- a/gopls/internal/golang/invertifcondition.go +++ b/gopls/internal/golang/invertifcondition.go @@ -8,18 +8,24 @@ import ( "fmt" "go/ast" "go/token" - "go/types" "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/astutil/cursor" ) // invertIfCondition is a singleFileFixFunc that inverts an if/else statement -func invertIfCondition(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - ifStatement, _, err := canInvertIfCondition(curFile, start, end) +func invertIfCondition(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + var ( + fset = pkg.FileSet() + src = pgf.Src + ) + + ifStatement, _, err := canInvertIfCondition(pgf.Cursor, start, end) if err != nil { return nil, nil, err } diff --git a/gopls/internal/golang/lines.go b/gopls/internal/golang/lines.go index f208e33e0c3..cb161671726 100644 --- a/gopls/internal/golang/lines.go +++ b/gopls/internal/golang/lines.go @@ -12,13 +12,14 @@ import ( "bytes" "go/ast" "go/token" - "go/types" "slices" "sort" "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/astutil/cursor" ) @@ -85,23 +86,25 @@ func canSplitJoinLines(items []ast.Node, comments []*ast.CommentGroup) bool { } // splitLines is a singleFile fixer. -func splitLines(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - itemType, items, comments, indent, braceOpen, braceClose := findSplitJoinTarget(fset, curFile, src, start, end) +func splitLines(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + fset := pkg.FileSet() + itemType, items, comments, indent, braceOpen, braceClose := findSplitJoinTarget(fset, pgf.Cursor, pgf.Src, start, end) if itemType == "" { return nil, nil, nil // no fix available } - return fset, processLines(fset, items, comments, src, braceOpen, braceClose, ",\n", "\n", ",\n"+indent, indent+"\t"), nil + return fset, processLines(fset, items, comments, pgf.Src, braceOpen, braceClose, ",\n", "\n", ",\n"+indent, indent+"\t"), nil } // joinLines is a singleFile fixer. -func joinLines(fset *token.FileSet, start, end token.Pos, src []byte, curFile cursor.Cursor, _ *types.Package, _ *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - itemType, items, comments, _, braceOpen, braceClose := findSplitJoinTarget(fset, curFile, src, start, end) +func joinLines(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + fset := pkg.FileSet() + itemType, items, comments, _, braceOpen, braceClose := findSplitJoinTarget(fset, pgf.Cursor, pgf.Src, start, end) if itemType == "" { return nil, nil, nil // no fix available } - return fset, processLines(fset, items, comments, src, braceOpen, braceClose, ", ", "", "", ""), nil + return fset, processLines(fset, items, comments, pgf.Src, braceOpen, braceClose, ", ", "", "", ""), nil } // processLines is the common operation for both split and join lines because this split/join operation is diff --git a/gopls/internal/golang/undeclared.go b/gopls/internal/golang/undeclared.go index f9331348f47..9df8e2bfd2e 100644 --- a/gopls/internal/golang/undeclared.go +++ b/gopls/internal/golang/undeclared.go @@ -16,8 +16,9 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/util/typesutil" - "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) @@ -70,9 +71,13 @@ func undeclaredFixTitle(path []ast.Node, errMsg string) string { } // createUndeclared generates a suggested declaration for an undeclared variable or function. -func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, curFile cursor.Cursor, pkg *types.Package, info *types.Info) (*token.FileSet, *analysis.SuggestedFix, error) { - pos := start // don't use the end - file := curFile.Node().(*ast.File) +func createUndeclared(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { + var ( + fset = pkg.FileSet() + info = pkg.TypesInfo() + file = pgf.File + pos = start // don't use end + ) // TODO(adonovan): simplify, using Cursor. path, _ := astutil.PathEnclosingInterval(file, pos, pos) if len(path) < 2 { @@ -86,7 +91,7 @@ func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, // Check for a possible call expression, in which case we should add a // new function declaration. if isCallPosition(path) { - return newFunctionDeclaration(path, file, pkg, info, fset) + return newFunctionDeclaration(path, file, pkg.Types(), info, fset) } var ( firstRef *ast.Ident // We should insert the new declaration before the first occurrence of the undefined ident. @@ -132,7 +137,7 @@ func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, if err != nil { return nil, nil, fmt.Errorf("could not locate insertion point: %v", err) } - indent, err := calculateIndentation(content, fset.File(file.FileStart), insertBeforeStmt) + indent, err := calculateIndentation(pgf.Src, fset.File(file.FileStart), insertBeforeStmt) if err != nil { return nil, nil, err } @@ -141,7 +146,7 @@ func createUndeclared(fset *token.FileSet, start, end token.Pos, content []byte, // Default to 0. typs = []types.Type{types.Typ[types.Int]} } - expr, _ := typesinternal.ZeroExpr(typs[0], typesinternal.FileQualifier(file, pkg)) + expr, _ := typesinternal.ZeroExpr(typs[0], typesinternal.FileQualifier(file, pkg.Types())) assignStmt := &ast.AssignStmt{ Lhs: []ast.Expr{ast.NewIdent(ident.Name)}, Tok: token.DEFINE, From 0b693ed05c20dd39478c8afb542bb4473dde7ba7 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Feb 2025 14:22:09 -0500 Subject: [PATCH 167/309] internal/astutil/cursor: FindPos: don't assume Files are in Pos order + test Change-Id: I75c4c3b7789feda874da7644bda8ba93f87f5efb Reviewed-on: https://go-review.googlesource.com/c/tools/+/650645 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/astutil/cursor/cursor.go | 18 ++++++++++++------ internal/astutil/cursor/cursor_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 5ed177c9f3d..83a47e09058 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -428,15 +428,21 @@ func (c Cursor) FindPos(start, end token.Pos) (Cursor, bool) { ev := events[i] if ev.index > i { // push? n := ev.node - var nodeStart, nodeEnd token.Pos + var nodeEnd token.Pos if file, ok := n.(*ast.File); ok { - nodeStart, nodeEnd = file.FileStart, file.FileEnd + nodeEnd = file.FileEnd + // Note: files may be out of Pos order. + if file.FileStart > start { + i = ev.index // disjoint, after; skip to next file + continue + } } else { - nodeStart, nodeEnd = n.Pos(), n.End() - } - if nodeStart > start { - break // disjoint, after; stop + nodeEnd = n.End() + if n.Pos() > start { + break // disjoint, after; stop + } } + // Inv: node.{Pos,FileStart} <= start if end <= nodeEnd { // node fully contains target range best = i diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 01f791f2833..67e91544c4d 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -14,6 +14,7 @@ import ( "go/token" "iter" "log" + "math/rand" "path/filepath" "reflect" "slices" @@ -332,8 +333,31 @@ func TestCursor_FindNode(t *testing.T) { } } } +} + +// TestCursor_FindPos_order ensures that FindPos does not assume files are in Pos order. +func TestCursor_FindPos_order(t *testing.T) { + // Pick an arbitrary decl. + target := netFiles[7].Decls[0] + + // Find the target decl by its position. + cur, ok := cursor.Root(netInspect).FindPos(target.Pos(), target.End()) + if !ok || cur.Node() != target { + t.Fatalf("unshuffled: FindPos(%T) = (%v, %t)", target, cur, ok) + } - // TODO(adonovan): FindPos needs a test (not just a benchmark). + // Shuffle the files out of Pos order. + files := slices.Clone(netFiles) + rand.Shuffle(len(files), func(i, j int) { + files[i], files[j] = files[j], files[i] + }) + + // Find it again. + inspect := inspector.New(files) + cur, ok = cursor.Root(inspect).FindPos(target.Pos(), target.End()) + if !ok || cur.Node() != target { + t.Fatalf("shuffled: FindPos(%T) = (%v, %t)", target, cur, ok) + } } func TestCursor_Edge(t *testing.T) { From 107c5b255f0e491b4e9f5ce6d3be554e07a38caa Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Feb 2025 15:28:28 -0500 Subject: [PATCH 168/309] gopls/internal/analysis/modernize: disable unsound maps.Clone fix The fix is sound only if the operand is provably non-nil. My word, these simple cases are subtle. Are we wrong to want to expand access to the framework? Fixes golang/go#71844 Change-Id: I5cf414cae41bf9b6445b7a4a7175dbb9c292e4b8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650756 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/maps.go | 14 ++++++- .../testdata/src/mapsloop/mapsloop.go | 30 ++++++++++----- .../testdata/src/mapsloop/mapsloop.go.golden | 37 ++++++++++++++----- .../testdata/src/mapsloop/mapsloop_dot.go | 6 ++- .../src/mapsloop/mapsloop_dot.go.golden | 8 ++-- 5 files changed, 69 insertions(+), 26 deletions(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 91de659d107..dad329477cd 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -32,7 +32,7 @@ import ( // // maps.Copy(m, x) (x is map) // maps.Insert(m, x) (x is iter.Seq2) -// m = maps.Clone(x) (x is map, m is a new map) +// m = maps.Clone(x) (x is a non-nil map, m is a new map) // m = maps.Collect(x) (x is iter.Seq2, m is a new map) // // A map is newly created if the preceding statement has one of these @@ -77,6 +77,8 @@ func mapsloop(pass *analysis.Pass) { // Is the preceding statement of the form // m = make(M) or M{} // and can we replace its RHS with slices.{Clone,Collect}? + // + // Beware: if x may be nil, we cannot use Clone as it preserves nilness. var mrhs ast.Expr // make(M) or M{}, or nil if curPrev, ok := curRange.PrevSibling(); ok { if assign, ok := curPrev.Node().(*ast.AssignStmt); ok && @@ -122,6 +124,16 @@ func mapsloop(pass *analysis.Pass) { mrhs = rhs } } + + // Temporarily disable the transformation to the + // (nil-preserving) maps.Clone until we can prove + // that x is non-nil. This is rarely possible, + // and may require control flow analysis + // (e.g. a dominating "if len(x)" check). + // See #71844. + if xmap { + mrhs = nil + } } } } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go index e4e6963dbae..68ff9154ffd 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go @@ -27,30 +27,34 @@ func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { } } -func useClone(src map[int]string) { - // Replace make(...) by maps.Clone. +func useCopyNotClone(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + + // Replace make(...) by maps.Copy. dst := make(map[int]string, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } dst = map[int]string{} for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } -func useCloneParen(src map[int]string) { +func useCopyParen(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace (make)(...) by maps.Clone. dst := (make)(map[int]string, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } dst = (map[int]string{}) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } @@ -74,32 +78,38 @@ func useCopy_typesDiffer2(src map[int]string) { } func useClone_typesDiffer3(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) as maps.Clone(src) returns map[int]string // which is assignable to M. var dst M dst = make(M, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } func useClone_typesDiffer4(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) as maps.Clone(src) returns map[int]string // which is assignable to M. var dst M dst = make(M, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } func useClone_generic[Map ~map[K]V, K comparable, V any](src Map) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) by maps.Clone dst := make(Map, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden index 70b9a28ed5b..be189673d9a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden @@ -23,19 +23,27 @@ func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { maps.Copy(dst, src) } -func useClone(src map[int]string) { - // Replace make(...) by maps.Clone. - dst := maps.Clone(src) +func useCopyNotClone(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. - dst = maps.Clone(src) + // Replace make(...) by maps.Copy. + dst := make(map[int]string, len(src)) + maps.Copy(dst, src) + + dst = map[int]string{} + maps.Copy(dst, src) println(dst) } -func useCloneParen(src map[int]string) { +func useCopyParen(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace (make)(...) by maps.Clone. - dst := maps.Clone(src) + dst := (make)(map[int]string, len(src)) + maps.Copy(dst, src) - dst = maps.Clone(src) + dst = (map[int]string{}) + maps.Copy(dst, src) println(dst) } @@ -54,24 +62,33 @@ func useCopy_typesDiffer2(src map[int]string) { } func useClone_typesDiffer3(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) as maps.Clone(src) returns map[int]string // which is assignable to M. var dst M - dst = maps.Clone(src) + dst = make(M, len(src)) + maps.Copy(dst, src) println(dst) } func useClone_typesDiffer4(src map[int]string) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) as maps.Clone(src) returns map[int]string // which is assignable to M. var dst M - dst = maps.Clone(src) + dst = make(M, len(src)) + maps.Copy(dst, src) println(dst) } func useClone_generic[Map ~map[K]V, K comparable, V any](src Map) { + // Clone is tempting but wrong when src may be nil; see #71844. + // Replace loop and make(...) by maps.Clone - dst := maps.Clone(src) + dst := make(Map, len(src)) + maps.Copy(dst, src) println(dst) } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go index c33d43e23ad..ae28f11afda 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go @@ -14,10 +14,12 @@ func useCopyDot(dst, src map[int]string) { } func useCloneDot(src map[int]string) { - // Replace make(...) by maps.Clone. + // Clone is tempting but wrong when src may be nil; see #71844. + + // Replace make(...) by maps.Copy. dst := make(map[int]string, len(src)) for key, value := range src { - dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Clone" + dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden index d6a30537645..e992314cf56 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden @@ -12,8 +12,10 @@ func useCopyDot(dst, src map[int]string) { } func useCloneDot(src map[int]string) { - // Replace make(...) by maps.Clone. - dst := Clone(src) + // Clone is tempting but wrong when src may be nil; see #71844. + + // Replace make(...) by maps.Copy. + dst := make(map[int]string, len(src)) + Copy(dst, src) println(dst) } - From 99337ebe7b90918701a41932abf121600b972e34 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Feb 2025 17:40:54 -0500 Subject: [PATCH 169/309] x/tools: modernize interface{} -> any Produced with a patched version of modernize containing only the efaceany pass: $ go run ./gopls/internal/analysis/modernize/cmd/modernize/main.go -test -fix ./... ./gopls/... This is very safe and forms the bulk of the modernize diff, isolating the other changes for ease of review. Change-Id: Iec9352bac5a639a5c03368427531c7842c6e9ad0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650759 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- blog/blog.go | 2 +- container/intsets/sparse.go | 2 +- go/analysis/analysis.go | 8 +- go/analysis/analysistest/analysistest.go | 2 +- go/analysis/analysistest/analysistest_test.go | 2 +- go/analysis/internal/analysisflags/flags.go | 10 +- go/analysis/internal/checker/checker_test.go | 6 +- go/analysis/internal/checker/start_test.go | 2 +- go/analysis/internal/internal.go | 2 +- .../internal/versiontest/version_test.go | 2 +- go/analysis/multichecker/multichecker_test.go | 2 +- go/analysis/passes/appends/appends.go | 2 +- go/analysis/passes/asmdecl/asmdecl.go | 6 +- go/analysis/passes/buildssa/buildssa.go | 2 +- go/analysis/passes/buildtag/buildtag.go | 2 +- go/analysis/passes/cgocall/cgocall.go | 2 +- go/analysis/passes/composite/composite.go | 2 +- go/analysis/passes/copylock/copylock.go | 2 +- go/analysis/passes/ctrlflow/ctrlflow.go | 2 +- go/analysis/passes/directive/directive.go | 2 +- .../passes/fieldalignment/fieldalignment.go | 2 +- go/analysis/passes/findcall/findcall.go | 2 +- .../passes/framepointer/framepointer.go | 2 +- go/analysis/passes/ifaceassert/ifaceassert.go | 2 +- go/analysis/passes/inspect/inspect.go | 2 +- go/analysis/passes/loopclosure/loopclosure.go | 2 +- go/analysis/passes/lostcancel/lostcancel.go | 2 +- go/analysis/passes/nilfunc/nilfunc.go | 2 +- go/analysis/passes/nilness/nilness.go | 4 +- go/analysis/passes/pkgfact/pkgfact.go | 2 +- .../reflectvaluecompare.go | 2 +- go/analysis/passes/shadow/shadow.go | 2 +- go/analysis/passes/shift/shift.go | 2 +- go/analysis/passes/stdmethods/stdmethods.go | 2 +- go/analysis/passes/stringintconv/string.go | 2 +- go/analysis/passes/structtag/structtag.go | 2 +- .../testinggoroutine/testinggoroutine.go | 2 +- go/analysis/passes/tests/tests.go | 2 +- go/analysis/passes/unreachable/unreachable.go | 2 +- go/analysis/passes/unsafeptr/unsafeptr.go | 2 +- .../passes/unusedresult/unusedresult.go | 2 +- .../passes/usesgenerics/usesgenerics.go | 2 +- go/analysis/unitchecker/unitchecker.go | 4 +- go/analysis/unitchecker/unitchecker_test.go | 2 +- go/analysis/validate_test.go | 4 +- go/buildutil/fakecontext.go | 4 +- go/buildutil/tags.go | 2 +- go/callgraph/rta/rta.go | 4 +- go/callgraph/rta/rta_test.go | 2 +- go/callgraph/vta/internal/trie/builder.go | 20 +- go/callgraph/vta/internal/trie/op_test.go | 48 ++-- go/callgraph/vta/internal/trie/trie.go | 22 +- go/callgraph/vta/internal/trie/trie_test.go | 214 +++++++++--------- go/callgraph/vta/propagation.go | 2 +- go/callgraph/vta/vta_test.go | 2 +- go/expect/expect.go | 8 +- go/expect/expect_test.go | 6 +- go/expect/extract.go | 12 +- go/internal/cgo/cgo.go | 2 +- go/internal/gccgoimporter/parser.go | 32 +-- go/loader/loader.go | 2 +- go/packages/gopackages/main.go | 2 +- go/packages/overlay_test.go | 36 +-- go/packages/packages.go | 10 +- go/packages/packages_test.go | 104 ++++----- go/packages/packagestest/expect.go | 36 +-- go/packages/packagestest/expect_test.go | 2 +- go/packages/packagestest/export.go | 10 +- go/packages/packagestest/export_test.go | 26 +-- go/ssa/const_test.go | 6 +- go/ssa/interp/interp.go | 2 +- go/ssa/interp/map.go | 2 +- go/ssa/interp/value.go | 10 +- go/ssa/mode.go | 2 +- go/ssa/print.go | 2 +- go/ssa/sanity.go | 6 +- go/ssa/ssautil/load_test.go | 2 +- go/ssa/util.go | 2 +- .../analysis/deprecated/deprecated.go | 2 +- .../analysis/embeddirective/embeddirective.go | 2 +- .../analysis/fillreturns/fillreturns.go | 2 +- .../internal/analysis/nonewvars/nonewvars.go | 2 +- .../analysis/noresultvalues/noresultvalues.go | 2 +- .../simplifycompositelit.go | 2 +- .../analysis/simplifyrange/simplifyrange.go | 2 +- .../analysis/simplifyslice/simplifyslice.go | 2 +- gopls/internal/analysis/yield/yield.go | 2 +- gopls/internal/cache/analysis.go | 6 +- gopls/internal/cache/load.go | 2 +- gopls/internal/cache/mod.go | 10 +- gopls/internal/cache/mod_tidy.go | 2 +- gopls/internal/cache/mod_vuln.go | 2 +- gopls/internal/cache/parse_cache.go | 6 +- gopls/internal/cmd/cmd.go | 12 +- gopls/internal/cmd/integration_test.go | 4 +- gopls/internal/cmd/stats.go | 2 +- gopls/internal/cmd/symbols.go | 4 +- gopls/internal/debug/log/log.go | 2 +- gopls/internal/debug/rpc.go | 2 +- gopls/internal/debug/serve.go | 22 +- gopls/internal/debug/template_test.go | 2 +- gopls/internal/debug/trace.go | 2 +- gopls/internal/golang/rename_check.go | 2 +- gopls/internal/lsprpc/binder_test.go | 2 +- .../lsprpc/commandinterceptor_test.go | 10 +- gopls/internal/lsprpc/export_test.go | 4 +- gopls/internal/lsprpc/goenv.go | 2 +- gopls/internal/lsprpc/goenv_test.go | 18 +- gopls/internal/lsprpc/lsprpc.go | 12 +- gopls/internal/lsprpc/lsprpc_test.go | 6 +- gopls/internal/lsprpc/middleware_test.go | 2 +- gopls/internal/server/command.go | 2 +- gopls/internal/server/general.go | 4 +- gopls/internal/server/unimplemented.go | 2 +- gopls/internal/template/parse.go | 2 +- .../test/integration/bench/completion_test.go | 2 +- .../test/integration/bench/didchange_test.go | 2 +- gopls/internal/test/integration/env.go | 2 +- gopls/internal/test/integration/env_test.go | 2 +- .../internal/test/integration/expectation.go | 6 +- .../internal/test/integration/fake/client.go | 6 +- .../test/integration/fake/glob/glob.go | 2 +- gopls/internal/test/integration/options.go | 6 +- gopls/internal/util/bug/bug.go | 4 +- gopls/internal/vulncheck/vulntest/report.go | 2 +- internal/event/export/id.go | 2 +- internal/event/export/metric/exporter.go | 4 +- internal/event/export/ocagent/ocagent.go | 4 +- .../event/export/prometheus/prometheus.go | 2 +- internal/event/keys/keys.go | 6 +- internal/event/label/label.go | 6 +- internal/expect/expect.go | 2 +- internal/expect/expect_test.go | 2 +- internal/expect/extract.go | 4 +- internal/facts/facts.go | 2 +- internal/gcimporter/bimport.go | 2 +- internal/gcimporter/iexport.go | 6 +- internal/gcimporter/iimport.go | 2 +- internal/gopathwalk/walk.go | 4 +- internal/imports/fix_test.go | 2 +- internal/jsonrpc2/conn.go | 10 +- internal/jsonrpc2/handler.go | 8 +- internal/jsonrpc2/jsonrpc2_test.go | 12 +- internal/jsonrpc2/messages.go | 8 +- internal/jsonrpc2_v2/conn.go | 14 +- internal/jsonrpc2_v2/jsonrpc2.go | 16 +- internal/jsonrpc2_v2/jsonrpc2_test.go | 28 +-- internal/jsonrpc2_v2/messages.go | 12 +- internal/jsonrpc2_v2/serve_test.go | 4 +- internal/jsonrpc2_v2/wire.go | 2 +- internal/jsonrpc2_v2/wire_test.go | 8 +- internal/memoize/memoize.go | 20 +- internal/memoize/memoize_test.go | 20 +- internal/packagesinternal/packages.go | 2 +- internal/packagestest/expect.go | 36 +-- internal/packagestest/expect_test.go | 2 +- internal/packagestest/export.go | 10 +- internal/packagestest/export_test.go | 26 +-- internal/tool/tool.go | 2 +- internal/typeparams/normalize.go | 2 +- internal/xcontext/xcontext.go | 8 +- 161 files changed, 630 insertions(+), 630 deletions(-) diff --git a/blog/blog.go b/blog/blog.go index 947c60e95a2..901b53f440e 100644 --- a/blog/blog.go +++ b/blog/blog.go @@ -420,7 +420,7 @@ type rootData struct { BasePath string GodocURL string AnalyticsHTML template.HTML - Data interface{} + Data any } // ServeHTTP serves the front, index, and article pages diff --git a/container/intsets/sparse.go b/container/intsets/sparse.go index c56aacc28bb..b9b4c91ed21 100644 --- a/container/intsets/sparse.go +++ b/container/intsets/sparse.go @@ -267,7 +267,7 @@ func (s *Sparse) init() { // loop. Fail fast before this occurs. // We don't want to call panic here because it prevents the // inlining of this function. - _ = (interface{}(nil)).(to_copy_a_sparse_you_must_call_its_Copy_method) + _ = (any(nil)).(to_copy_a_sparse_you_must_call_its_Copy_method) } } diff --git a/go/analysis/analysis.go b/go/analysis/analysis.go index 3a73084a53c..a7df4d1fe4e 100644 --- a/go/analysis/analysis.go +++ b/go/analysis/analysis.go @@ -45,7 +45,7 @@ type Analyzer struct { // To pass analysis results between packages (and thus // potentially between address spaces), use Facts, which are // serializable. - Run func(*Pass) (interface{}, error) + Run func(*Pass) (any, error) // RunDespiteErrors allows the driver to invoke // the Run method of this analyzer even on a @@ -112,7 +112,7 @@ type Pass struct { // The map keys are the elements of Analysis.Required, // and the type of each corresponding value is the required // analysis's ResultType. - ResultOf map[*Analyzer]interface{} + ResultOf map[*Analyzer]any // ReadFile returns the contents of the named file. // @@ -186,7 +186,7 @@ type ObjectFact struct { // Reportf is a helper function that reports a Diagnostic using the // specified position and formatted error message. -func (pass *Pass) Reportf(pos token.Pos, format string, args ...interface{}) { +func (pass *Pass) Reportf(pos token.Pos, format string, args ...any) { msg := fmt.Sprintf(format, args...) pass.Report(Diagnostic{Pos: pos, Message: msg}) } @@ -201,7 +201,7 @@ type Range interface { // ReportRangef is a helper function that reports a Diagnostic using the // range provided. ast.Node values can be passed in as the range because // they satisfy the Range interface. -func (pass *Pass) ReportRangef(rng Range, format string, args ...interface{}) { +func (pass *Pass) ReportRangef(rng Range, format string, args ...any) { msg := fmt.Sprintf(format, args...) pass.Report(Diagnostic{Pos: rng.Pos(), End: rng.End(), Message: msg}) } diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 08981776478..0b5cfe70bfe 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -76,7 +76,7 @@ var TestData = func() string { // Testing is an abstraction of a *testing.T. type Testing interface { - Errorf(format string, args ...interface{}) + Errorf(format string, args ...any) } // RunWithSuggestedFixes behaves like Run, but additionally applies diff --git a/go/analysis/analysistest/analysistest_test.go b/go/analysis/analysistest/analysistest_test.go index eedbb5c2a90..88cd8f8f1d5 100644 --- a/go/analysis/analysistest/analysistest_test.go +++ b/go/analysis/analysistest/analysistest_test.go @@ -262,6 +262,6 @@ type T string type errorfunc func(string) -func (f errorfunc) Errorf(format string, args ...interface{}) { +func (f errorfunc) Errorf(format string, args ...any) { f(fmt.Sprintf(format, args...)) } diff --git a/go/analysis/internal/analysisflags/flags.go b/go/analysis/internal/analysisflags/flags.go index c2445575cff..6aefef25815 100644 --- a/go/analysis/internal/analysisflags/flags.go +++ b/go/analysis/internal/analysisflags/flags.go @@ -201,7 +201,7 @@ func addVersionFlag() { type versionFlag struct{} func (versionFlag) IsBoolFlag() bool { return true } -func (versionFlag) Get() interface{} { return nil } +func (versionFlag) Get() any { return nil } func (versionFlag) String() string { return "" } func (versionFlag) Set(s string) error { if s != "full" { @@ -252,7 +252,7 @@ const ( // triState implements flag.Value, flag.Getter, and flag.boolFlag. // They work like boolean flags: we can say vet -printf as well as vet -printf=true -func (ts *triState) Get() interface{} { +func (ts *triState) Get() any { return *ts == setTrue } @@ -340,7 +340,7 @@ func PrintPlain(out io.Writer, fset *token.FileSet, contextLines int, diag analy // A JSONTree is a mapping from package ID to analysis name to result. // Each result is either a jsonError or a list of JSONDiagnostic. -type JSONTree map[string]map[string]interface{} +type JSONTree map[string]map[string]any // A TextEdit describes the replacement of a portion of a file. // Start and End are zero-based half-open indices into the original byte @@ -383,7 +383,7 @@ type JSONRelatedInformation struct { // Add adds the result of analysis 'name' on package 'id'. // The result is either a list of diagnostics or an error. func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis.Diagnostic, err error) { - var v interface{} + var v any if err != nil { type jsonError struct { Err string `json:"error"` @@ -429,7 +429,7 @@ func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis. if v != nil { m, ok := tree[id] if !ok { - m = make(map[string]interface{}) + m = make(map[string]any) tree[id] = m } m[name] = v diff --git a/go/analysis/internal/checker/checker_test.go b/go/analysis/internal/checker/checker_test.go index 9ec6e61cd73..7d73aa3c6bb 100644 --- a/go/analysis/internal/checker/checker_test.go +++ b/go/analysis/internal/checker/checker_test.go @@ -107,7 +107,7 @@ func NewT1() *T1 { return &T1{T} } Name: "noop", Doc: "noop", Requires: []*analysis.Analyzer{inspect.Analyzer}, - Run: func(pass *analysis.Pass) (interface{}, error) { + Run: func(pass *analysis.Pass) (any, error) { return nil, nil }, RunDespiteErrors: true, @@ -119,7 +119,7 @@ func NewT1() *T1 { return &T1{T} } Name: "noopfact", Doc: "noopfact", Requires: []*analysis.Analyzer{inspect.Analyzer}, - Run: func(pass *analysis.Pass) (interface{}, error) { + Run: func(pass *analysis.Pass) (any, error) { return nil, nil }, RunDespiteErrors: true, @@ -185,7 +185,7 @@ func TestURL(t *testing.T) { Name: "pkgname", Doc: "trivial analyzer that reports package names", URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/internal/checker", - Run: func(p *analysis.Pass) (interface{}, error) { + Run: func(p *analysis.Pass) (any, error) { for _, f := range p.Files { p.ReportRangef(f.Name, "package name is %s", f.Name.Name) } diff --git a/go/analysis/internal/checker/start_test.go b/go/analysis/internal/checker/start_test.go index c78829a5adf..60ed54464ae 100644 --- a/go/analysis/internal/checker/start_test.go +++ b/go/analysis/internal/checker/start_test.go @@ -62,7 +62,7 @@ var commentAnalyzer = &analysis.Analyzer{ Run: commentRun, } -func commentRun(pass *analysis.Pass) (interface{}, error) { +func commentRun(pass *analysis.Pass) (any, error) { const ( from = "/* Package comment */" to = "// Package comment" diff --git a/go/analysis/internal/internal.go b/go/analysis/internal/internal.go index e7c8247fd33..327c4b50579 100644 --- a/go/analysis/internal/internal.go +++ b/go/analysis/internal/internal.go @@ -9,4 +9,4 @@ import "golang.org/x/tools/go/analysis" // This function is set by the checker package to provide // backdoor access to the private Pass field // of the checker.Action type, for use by analysistest. -var Pass func(interface{}) *analysis.Pass +var Pass func(any) *analysis.Pass diff --git a/go/analysis/internal/versiontest/version_test.go b/go/analysis/internal/versiontest/version_test.go index 43c52f565f7..5bd6d3027dd 100644 --- a/go/analysis/internal/versiontest/version_test.go +++ b/go/analysis/internal/versiontest/version_test.go @@ -26,7 +26,7 @@ import ( var analyzer = &analysis.Analyzer{ Name: "versiontest", Doc: "off", - Run: func(pass *analysis.Pass) (interface{}, error) { + Run: func(pass *analysis.Pass) (any, error) { pass.Reportf(pass.Files[0].Package, "goversion=%s", pass.Pkg.GoVersion()) return nil, nil }, diff --git a/go/analysis/multichecker/multichecker_test.go b/go/analysis/multichecker/multichecker_test.go index 94a280564ce..1491df153b9 100644 --- a/go/analysis/multichecker/multichecker_test.go +++ b/go/analysis/multichecker/multichecker_test.go @@ -23,7 +23,7 @@ func main() { fail := &analysis.Analyzer{ Name: "fail", Doc: "always fail on a package 'sort'", - Run: func(pass *analysis.Pass) (interface{}, error) { + Run: func(pass *analysis.Pass) (any, error) { if pass.Pkg.Path() == "sort" { return nil, fmt.Errorf("failed") } diff --git a/go/analysis/passes/appends/appends.go b/go/analysis/passes/appends/appends.go index 6976f0d9090..e554c3cc903 100644 --- a/go/analysis/passes/appends/appends.go +++ b/go/analysis/passes/appends/appends.go @@ -29,7 +29,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/asmdecl/asmdecl.go b/go/analysis/passes/asmdecl/asmdecl.go index a47ecbae731..436b03cb290 100644 --- a/go/analysis/passes/asmdecl/asmdecl.go +++ b/go/analysis/passes/asmdecl/asmdecl.go @@ -150,7 +150,7 @@ var ( abiSuff = re(`^(.+)<(ABI.+)>$`) ) -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // No work if no assembly files. var sfiles []string for _, fname := range pass.OtherFiles { @@ -226,7 +226,7 @@ Files: for lineno, line := range lines { lineno++ - badf := func(format string, args ...interface{}) { + badf := func(format string, args ...any) { pass.Reportf(analysisutil.LineStart(tf, lineno), "[%s] %s: %s", arch, fnName, fmt.Sprintf(format, args...)) } @@ -646,7 +646,7 @@ func asmParseDecl(pass *analysis.Pass, decl *ast.FuncDecl) map[string]*asmFunc { } // asmCheckVar checks a single variable reference. -func asmCheckVar(badf func(string, ...interface{}), fn *asmFunc, line, expr string, off int, v *asmVar, archDef *asmArch) { +func asmCheckVar(badf func(string, ...any), fn *asmFunc, line, expr string, off int, v *asmVar, archDef *asmArch) { m := asmOpcode.FindStringSubmatch(line) if m == nil { if !strings.HasPrefix(strings.TrimSpace(line), "//") { diff --git a/go/analysis/passes/buildssa/buildssa.go b/go/analysis/passes/buildssa/buildssa.go index f077ea28247..f49fea51762 100644 --- a/go/analysis/passes/buildssa/buildssa.go +++ b/go/analysis/passes/buildssa/buildssa.go @@ -32,7 +32,7 @@ type SSA struct { SrcFuncs []*ssa.Function } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // We must create a new Program for each Package because the // analysis API provides no place to hang a Program shared by // all Packages. Consequently, SSA Packages and Functions do not diff --git a/go/analysis/passes/buildtag/buildtag.go b/go/analysis/passes/buildtag/buildtag.go index e7434e8fed2..6c7a0df585d 100644 --- a/go/analysis/passes/buildtag/buildtag.go +++ b/go/analysis/passes/buildtag/buildtag.go @@ -26,7 +26,7 @@ var Analyzer = &analysis.Analyzer{ Run: runBuildTag, } -func runBuildTag(pass *analysis.Pass) (interface{}, error) { +func runBuildTag(pass *analysis.Pass) (any, error) { for _, f := range pass.Files { checkGoFile(pass, f) } diff --git a/go/analysis/passes/cgocall/cgocall.go b/go/analysis/passes/cgocall/cgocall.go index 4f3bb035d65..d9189b5b696 100644 --- a/go/analysis/passes/cgocall/cgocall.go +++ b/go/analysis/passes/cgocall/cgocall.go @@ -55,7 +55,7 @@ func run(pass *analysis.Pass) (any, error) { return nil, nil } -func checkCgo(fset *token.FileSet, f *ast.File, info *types.Info, reportf func(token.Pos, string, ...interface{})) { +func checkCgo(fset *token.FileSet, f *ast.File, info *types.Info, reportf func(token.Pos, string, ...any)) { ast.Inspect(f, func(n ast.Node) bool { call, ok := n.(*ast.CallExpr) if !ok { diff --git a/go/analysis/passes/composite/composite.go b/go/analysis/passes/composite/composite.go index f56c3e622fb..60c6afe49f0 100644 --- a/go/analysis/passes/composite/composite.go +++ b/go/analysis/passes/composite/composite.go @@ -51,7 +51,7 @@ func init() { // runUnkeyedLiteral checks if a composite literal is a struct literal with // unkeyed fields. -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/copylock/copylock.go b/go/analysis/passes/copylock/copylock.go index 8a215677165..49c14d4980d 100644 --- a/go/analysis/passes/copylock/copylock.go +++ b/go/analysis/passes/copylock/copylock.go @@ -36,7 +36,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) var goversion string // effective file version ("" => unknown) diff --git a/go/analysis/passes/ctrlflow/ctrlflow.go b/go/analysis/passes/ctrlflow/ctrlflow.go index d21adeee900..951aaed00fd 100644 --- a/go/analysis/passes/ctrlflow/ctrlflow.go +++ b/go/analysis/passes/ctrlflow/ctrlflow.go @@ -80,7 +80,7 @@ func (c *CFGs) FuncLit(lit *ast.FuncLit) *cfg.CFG { return c.funcLits[lit].cfg } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) // Because CFG construction consumes and produces noReturn diff --git a/go/analysis/passes/directive/directive.go b/go/analysis/passes/directive/directive.go index b205402388e..bebec891408 100644 --- a/go/analysis/passes/directive/directive.go +++ b/go/analysis/passes/directive/directive.go @@ -40,7 +40,7 @@ var Analyzer = &analysis.Analyzer{ Run: runDirective, } -func runDirective(pass *analysis.Pass) (interface{}, error) { +func runDirective(pass *analysis.Pass) (any, error) { for _, f := range pass.Files { checkGoFile(pass, f) } diff --git a/go/analysis/passes/fieldalignment/fieldalignment.go b/go/analysis/passes/fieldalignment/fieldalignment.go index 93fa39140e6..e2ddc83b604 100644 --- a/go/analysis/passes/fieldalignment/fieldalignment.go +++ b/go/analysis/passes/fieldalignment/fieldalignment.go @@ -65,7 +65,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ (*ast.StructType)(nil), diff --git a/go/analysis/passes/findcall/findcall.go b/go/analysis/passes/findcall/findcall.go index 2671573d1fe..9db4de1c20f 100644 --- a/go/analysis/passes/findcall/findcall.go +++ b/go/analysis/passes/findcall/findcall.go @@ -38,7 +38,7 @@ func init() { Analyzer.Flags.StringVar(&name, "name", name, "name of the function to find") } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { for _, f := range pass.Files { ast.Inspect(f, func(n ast.Node) bool { if call, ok := n.(*ast.CallExpr); ok { diff --git a/go/analysis/passes/framepointer/framepointer.go b/go/analysis/passes/framepointer/framepointer.go index 8012de99daa..ba94fd68ea4 100644 --- a/go/analysis/passes/framepointer/framepointer.go +++ b/go/analysis/passes/framepointer/framepointer.go @@ -113,7 +113,7 @@ var arm64Branch = map[string]bool{ "RET": true, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { arch, ok := arches[build.Default.GOARCH] if !ok { return nil, nil diff --git a/go/analysis/passes/ifaceassert/ifaceassert.go b/go/analysis/passes/ifaceassert/ifaceassert.go index 5f07ed3ffde..4022dbe7c22 100644 --- a/go/analysis/passes/ifaceassert/ifaceassert.go +++ b/go/analysis/passes/ifaceassert/ifaceassert.go @@ -52,7 +52,7 @@ func assertableTo(free *typeparams.Free, v, t types.Type) *types.Func { return nil } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ (*ast.TypeAssertExpr)(nil), diff --git a/go/analysis/passes/inspect/inspect.go b/go/analysis/passes/inspect/inspect.go index 3b121cb0ce7..ee1972f56df 100644 --- a/go/analysis/passes/inspect/inspect.go +++ b/go/analysis/passes/inspect/inspect.go @@ -44,6 +44,6 @@ var Analyzer = &analysis.Analyzer{ ResultType: reflect.TypeOf(new(inspector.Inspector)), } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { return inspector.New(pass.Files), nil } diff --git a/go/analysis/passes/loopclosure/loopclosure.go b/go/analysis/passes/loopclosure/loopclosure.go index d3181242153..64df1b106a1 100644 --- a/go/analysis/passes/loopclosure/loopclosure.go +++ b/go/analysis/passes/loopclosure/loopclosure.go @@ -30,7 +30,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/lostcancel/lostcancel.go b/go/analysis/passes/lostcancel/lostcancel.go index f8a661aa5db..a7fee180925 100644 --- a/go/analysis/passes/lostcancel/lostcancel.go +++ b/go/analysis/passes/lostcancel/lostcancel.go @@ -47,7 +47,7 @@ var contextPackage = "context" // containing the assignment, we assume that other uses exist. // // checkLostCancel analyzes a single named or literal function. -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // Fast path: bypass check if file doesn't use context.WithCancel. if !analysisinternal.Imports(pass.Pkg, contextPackage) { return nil, nil diff --git a/go/analysis/passes/nilfunc/nilfunc.go b/go/analysis/passes/nilfunc/nilfunc.go index 778f7f1f8f9..3ac2dcd4907 100644 --- a/go/analysis/passes/nilfunc/nilfunc.go +++ b/go/analysis/passes/nilfunc/nilfunc.go @@ -30,7 +30,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/nilness/nilness.go b/go/analysis/passes/nilness/nilness.go index faaf12a9385..af61ae6088d 100644 --- a/go/analysis/passes/nilness/nilness.go +++ b/go/analysis/passes/nilness/nilness.go @@ -28,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ Requires: []*analysis.Analyzer{buildssa.Analyzer}, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { ssainput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) for _, fn := range ssainput.SrcFuncs { runFunc(pass, fn) @@ -37,7 +37,7 @@ func run(pass *analysis.Pass) (interface{}, error) { } func runFunc(pass *analysis.Pass, fn *ssa.Function) { - reportf := func(category string, pos token.Pos, format string, args ...interface{}) { + reportf := func(category string, pos token.Pos, format string, args ...any) { // We ignore nil-checking ssa.Instructions // that don't correspond to syntax. if pos.IsValid() { diff --git a/go/analysis/passes/pkgfact/pkgfact.go b/go/analysis/passes/pkgfact/pkgfact.go index 077c8780815..31748795dac 100644 --- a/go/analysis/passes/pkgfact/pkgfact.go +++ b/go/analysis/passes/pkgfact/pkgfact.go @@ -53,7 +53,7 @@ type pairsFact []string func (f *pairsFact) AFact() {} func (f *pairsFact) String() string { return "pairs(" + strings.Join(*f, ", ") + ")" } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { result := make(map[string]string) // At each import, print the fact from the imported diff --git a/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go b/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go index 72435b2fc7a..d0632dbdafe 100644 --- a/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go +++ b/go/analysis/passes/reflectvaluecompare/reflectvaluecompare.go @@ -28,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/shadow/shadow.go b/go/analysis/passes/shadow/shadow.go index 30258c991f3..8f768bb76c5 100644 --- a/go/analysis/passes/shadow/shadow.go +++ b/go/analysis/passes/shadow/shadow.go @@ -36,7 +36,7 @@ func init() { Analyzer.Flags.BoolVar(&strict, "strict", strict, "whether to be strict about shadowing; can be noisy") } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) spans := make(map[types.Object]span) diff --git a/go/analysis/passes/shift/shift.go b/go/analysis/passes/shift/shift.go index 46b5f6d68c6..57987b3d203 100644 --- a/go/analysis/passes/shift/shift.go +++ b/go/analysis/passes/shift/shift.go @@ -34,7 +34,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) // Do a complete pass to compute dead nodes. diff --git a/go/analysis/passes/stdmethods/stdmethods.go b/go/analysis/passes/stdmethods/stdmethods.go index 28f51b1ec9a..a0bdf001abd 100644 --- a/go/analysis/passes/stdmethods/stdmethods.go +++ b/go/analysis/passes/stdmethods/stdmethods.go @@ -66,7 +66,7 @@ var canonicalMethods = map[string]struct{ args, results []string }{ "WriteTo": {[]string{"=io.Writer"}, []string{"int64", "error"}}, // io.WriterTo } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/stringintconv/string.go b/go/analysis/passes/stringintconv/string.go index f56e6ecaa29..a23721cd26f 100644 --- a/go/analysis/passes/stringintconv/string.go +++ b/go/analysis/passes/stringintconv/string.go @@ -70,7 +70,7 @@ func typeName(t types.Type) string { return "" } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ (*ast.File)(nil), diff --git a/go/analysis/passes/structtag/structtag.go b/go/analysis/passes/structtag/structtag.go index 4115ef76943..d926503403d 100644 --- a/go/analysis/passes/structtag/structtag.go +++ b/go/analysis/passes/structtag/structtag.go @@ -34,7 +34,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/testinggoroutine/testinggoroutine.go b/go/analysis/passes/testinggoroutine/testinggoroutine.go index fef5a6014c4..f49ac4eb1a0 100644 --- a/go/analysis/passes/testinggoroutine/testinggoroutine.go +++ b/go/analysis/passes/testinggoroutine/testinggoroutine.go @@ -36,7 +36,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) if !analysisinternal.Imports(pass.Pkg, "testing") { diff --git a/go/analysis/passes/tests/tests.go b/go/analysis/passes/tests/tests.go index 285b34218c3..9f59006ebb2 100644 --- a/go/analysis/passes/tests/tests.go +++ b/go/analysis/passes/tests/tests.go @@ -47,7 +47,7 @@ var acceptedFuzzTypes = []types.Type{ types.NewSlice(types.Universe.Lookup("byte").Type()), } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { for _, f := range pass.Files { if !strings.HasSuffix(pass.Fset.File(f.FileStart).Name(), "_test.go") { continue diff --git a/go/analysis/passes/unreachable/unreachable.go b/go/analysis/passes/unreachable/unreachable.go index b810db7ee95..fcf5fbd9060 100644 --- a/go/analysis/passes/unreachable/unreachable.go +++ b/go/analysis/passes/unreachable/unreachable.go @@ -30,7 +30,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/unsafeptr/unsafeptr.go b/go/analysis/passes/unsafeptr/unsafeptr.go index fb5b944faad..57c6da64ff3 100644 --- a/go/analysis/passes/unsafeptr/unsafeptr.go +++ b/go/analysis/passes/unsafeptr/unsafeptr.go @@ -30,7 +30,7 @@ var Analyzer = &analysis.Analyzer{ Run: run, } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) nodeFilter := []ast.Node{ diff --git a/go/analysis/passes/unusedresult/unusedresult.go b/go/analysis/passes/unusedresult/unusedresult.go index e298f644277..932f1347e56 100644 --- a/go/analysis/passes/unusedresult/unusedresult.go +++ b/go/analysis/passes/unusedresult/unusedresult.go @@ -85,7 +85,7 @@ func init() { "comma-separated list of names of methods of type func() string whose results must be used") } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) // Split functions into (pkg, name) pairs to save allocation later. diff --git a/go/analysis/passes/usesgenerics/usesgenerics.go b/go/analysis/passes/usesgenerics/usesgenerics.go index 5c5df3a79a0..b7ff3ad6877 100644 --- a/go/analysis/passes/usesgenerics/usesgenerics.go +++ b/go/analysis/passes/usesgenerics/usesgenerics.go @@ -53,7 +53,7 @@ type featuresFact struct { func (f *featuresFact) AFact() {} func (f *featuresFact) String() string { return f.Features.String() } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) direct := genericfeatures.ForPackage(inspect, pass.TypesInfo) diff --git a/go/analysis/unitchecker/unitchecker.go b/go/analysis/unitchecker/unitchecker.go index 82c3db6a39d..a1ee80388b6 100644 --- a/go/analysis/unitchecker/unitchecker.go +++ b/go/analysis/unitchecker/unitchecker.go @@ -287,7 +287,7 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re // Also build a map to hold working state and result. type action struct { once sync.Once - result interface{} + result any err error usesFacts bool // (transitively uses) diagnostics []analysis.Diagnostic @@ -337,7 +337,7 @@ func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]re // The inputs to this analysis are the // results of its prerequisites. - inputs := make(map[*analysis.Analyzer]interface{}) + inputs := make(map[*analysis.Analyzer]any) var failed []string for _, req := range a.Requires { reqact := exec(req) diff --git a/go/analysis/unitchecker/unitchecker_test.go b/go/analysis/unitchecker/unitchecker_test.go index 173d76348f7..6c3bba6793e 100644 --- a/go/analysis/unitchecker/unitchecker_test.go +++ b/go/analysis/unitchecker/unitchecker_test.go @@ -59,7 +59,7 @@ func testIntegration(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a func _() { diff --git a/go/analysis/validate_test.go b/go/analysis/validate_test.go index 7f4ee2c05b9..b192ef0a3c0 100644 --- a/go/analysis/validate_test.go +++ b/go/analysis/validate_test.go @@ -11,7 +11,7 @@ import ( func TestValidate(t *testing.T) { var ( - run = func(p *Pass) (interface{}, error) { + run = func(p *Pass) (any, error) { return nil, nil } dependsOnSelf = &Analyzer{ @@ -130,7 +130,7 @@ func TestCycleInRequiresGraphErrorMessage(t *testing.T) { func TestValidateEmptyDoc(t *testing.T) { withoutDoc := &Analyzer{ Name: "withoutDoc", - Run: func(p *Pass) (interface{}, error) { + Run: func(p *Pass) (any, error) { return nil, nil }, } diff --git a/go/buildutil/fakecontext.go b/go/buildutil/fakecontext.go index 763d18809b4..1f75141d504 100644 --- a/go/buildutil/fakecontext.go +++ b/go/buildutil/fakecontext.go @@ -95,7 +95,7 @@ func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() } type fakeFileInfo string func (fi fakeFileInfo) Name() string { return string(fi) } -func (fakeFileInfo) Sys() interface{} { return nil } +func (fakeFileInfo) Sys() any { return nil } func (fakeFileInfo) ModTime() time.Time { return time.Time{} } func (fakeFileInfo) IsDir() bool { return false } func (fakeFileInfo) Size() int64 { return 0 } @@ -104,7 +104,7 @@ func (fakeFileInfo) Mode() os.FileMode { return 0644 } type fakeDirInfo string func (fd fakeDirInfo) Name() string { return string(fd) } -func (fakeDirInfo) Sys() interface{} { return nil } +func (fakeDirInfo) Sys() any { return nil } func (fakeDirInfo) ModTime() time.Time { return time.Time{} } func (fakeDirInfo) IsDir() bool { return true } func (fakeDirInfo) Size() int64 { return 0 } diff --git a/go/buildutil/tags.go b/go/buildutil/tags.go index 32c8d1424d2..410c8e72d48 100644 --- a/go/buildutil/tags.go +++ b/go/buildutil/tags.go @@ -51,7 +51,7 @@ func (v *TagsFlag) Set(s string) error { return nil } -func (v *TagsFlag) Get() interface{} { return *v } +func (v *TagsFlag) Get() any { return *v } func splitQuotedFields(s string) ([]string, error) { // See $GOROOT/src/cmd/internal/quoted/quoted.go (Split) diff --git a/go/callgraph/rta/rta.go b/go/callgraph/rta/rta.go index b489b0178c8..224c0b96ce0 100644 --- a/go/callgraph/rta/rta.go +++ b/go/callgraph/rta/rta.go @@ -371,7 +371,7 @@ func (r *rta) interfaces(C types.Type) []*types.Interface { // Ascertain set of interfaces C implements // and update the 'implements' relation. - r.interfaceTypes.Iterate(func(I types.Type, v interface{}) { + r.interfaceTypes.Iterate(func(I types.Type, v any) { iinfo := v.(*interfaceTypeInfo) if I := types.Unalias(I).(*types.Interface); implements(cinfo, iinfo) { iinfo.implementations = append(iinfo.implementations, C) @@ -400,7 +400,7 @@ func (r *rta) implementations(I *types.Interface) []types.Type { // Ascertain set of concrete types that implement I // and update the 'implements' relation. - r.concreteTypes.Iterate(func(C types.Type, v interface{}) { + r.concreteTypes.Iterate(func(C types.Type, v any) { cinfo := v.(*concreteTypeInfo) if implements(cinfo, iinfo) { cinfo.implements = append(cinfo.implements, I) diff --git a/go/callgraph/rta/rta_test.go b/go/callgraph/rta/rta_test.go index dcec98d7c5d..6b16484245b 100644 --- a/go/callgraph/rta/rta_test.go +++ b/go/callgraph/rta/rta_test.go @@ -220,7 +220,7 @@ func check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) { // Check runtime types. { got := make(stringset) - res.RuntimeTypes.Iterate(func(key types.Type, value interface{}) { + res.RuntimeTypes.Iterate(func(key types.Type, value any) { if !value.(bool) { // accessible to reflection typ := types.TypeString(types.Unalias(key), types.RelativeTo(pkg.Pkg)) got[typ] = true diff --git a/go/callgraph/vta/internal/trie/builder.go b/go/callgraph/vta/internal/trie/builder.go index c814c039f72..bdd39397ec6 100644 --- a/go/callgraph/vta/internal/trie/builder.go +++ b/go/callgraph/vta/internal/trie/builder.go @@ -14,13 +14,13 @@ package trie // // Collisions functions may be applied whenever a value is inserted // or two maps are merged, or intersected. -type Collision func(lhs interface{}, rhs interface{}) interface{} +type Collision func(lhs any, rhs any) any // TakeLhs always returns the left value in a collision. -func TakeLhs(lhs, rhs interface{}) interface{} { return lhs } +func TakeLhs(lhs, rhs any) any { return lhs } // TakeRhs always returns the right hand side in a collision. -func TakeRhs(lhs, rhs interface{}) interface{} { return rhs } +func TakeRhs(lhs, rhs any) any { return rhs } // Builder creates new Map. Each Builder has a unique Scope. // @@ -78,7 +78,7 @@ func (b *Builder) Empty() Map { return Map{b.Scope(), b.empty} } // if _, ok := m[k]; ok { m[k] = c(m[k], v} else { m[k] = v} // // An insertion or update happened whenever Insert(m, ...) != m . -func (b *Builder) InsertWith(c Collision, m Map, k uint64, v interface{}) Map { +func (b *Builder) InsertWith(c Collision, m Map, k uint64, v any) Map { m = b.Clone(m) return Map{b.Scope(), b.insert(c, m.n, b.mkLeaf(key(k), v), false)} } @@ -92,7 +92,7 @@ func (b *Builder) InsertWith(c Collision, m Map, k uint64, v interface{}) Map { // if _, ok := m[k]; ok { m[k] = val } // // This is equivalent to b.Merge(m, b.Create({k: v})). -func (b *Builder) Insert(m Map, k uint64, v interface{}) Map { +func (b *Builder) Insert(m Map, k uint64, v any) Map { return b.InsertWith(TakeLhs, m, k, v) } @@ -100,7 +100,7 @@ func (b *Builder) Insert(m Map, k uint64, v interface{}) Map { // updating a map[uint64]interface{} by: // // m[key] = val -func (b *Builder) Update(m Map, key uint64, val interface{}) Map { +func (b *Builder) Update(m Map, key uint64, val any) Map { return b.InsertWith(TakeRhs, m, key, val) } @@ -185,14 +185,14 @@ func (b *Builder) MutEmpty() MutMap { // Insert an element into the map using the collision function for the builder. // Returns true if the element was inserted. -func (mm *MutMap) Insert(k uint64, v interface{}) bool { +func (mm *MutMap) Insert(k uint64, v any) bool { old := mm.M mm.M = mm.B.Insert(old, k, v) return old != mm.M } // Updates an element in the map. Returns true if the map was updated. -func (mm *MutMap) Update(k uint64, v interface{}) bool { +func (mm *MutMap) Update(k uint64, v any) bool { old := mm.M mm.M = mm.B.Update(old, k, v) return old != mm.M @@ -221,7 +221,7 @@ func (mm *MutMap) Intersect(other Map) bool { return old != mm.M } -func (b *Builder) Create(m map[uint64]interface{}) Map { +func (b *Builder) Create(m map[uint64]any) Map { var leaves []*leaf for k, v := range m { leaves = append(leaves, b.mkLeaf(key(k), v)) @@ -259,7 +259,7 @@ func (b *Builder) create(leaves []*leaf) node { } // mkLeaf returns the hash-consed representative of (k, v) in the current scope. -func (b *Builder) mkLeaf(k key, v interface{}) *leaf { +func (b *Builder) mkLeaf(k key, v any) *leaf { rep, ok := b.leaves[leaf{k, v}] if !ok { rep = &leaf{k, v} // heap-allocated copy diff --git a/go/callgraph/vta/internal/trie/op_test.go b/go/callgraph/vta/internal/trie/op_test.go index ba0d5be71a9..b4610d55c22 100644 --- a/go/callgraph/vta/internal/trie/op_test.go +++ b/go/callgraph/vta/internal/trie/op_test.go @@ -21,13 +21,13 @@ import ( // mapCollection is effectively a []map[uint64]interface{}. // These support operations being applied to the i'th maps. type mapCollection interface { - Elements() []map[uint64]interface{} + Elements() []map[uint64]any DeepEqual(l, r int) bool - Lookup(id int, k uint64) (interface{}, bool) + Lookup(id int, k uint64) (any, bool) - Insert(id int, k uint64, v interface{}) - Update(id int, k uint64, v interface{}) + Insert(id int, k uint64, v any) + Update(id int, k uint64, v any) Remove(id int, k uint64) Intersect(l int, r int) Merge(l int, r int) @@ -86,19 +86,19 @@ type trieCollection struct { tries []trie.MutMap } -func (c *trieCollection) Elements() []map[uint64]interface{} { - var maps []map[uint64]interface{} +func (c *trieCollection) Elements() []map[uint64]any { + var maps []map[uint64]any for _, m := range c.tries { maps = append(maps, trie.Elems(m.M)) } return maps } -func (c *trieCollection) Eq(id int, m map[uint64]interface{}) bool { +func (c *trieCollection) Eq(id int, m map[uint64]any) bool { elems := trie.Elems(c.tries[id].M) return !reflect.DeepEqual(elems, m) } -func (c *trieCollection) Lookup(id int, k uint64) (interface{}, bool) { +func (c *trieCollection) Lookup(id int, k uint64) (any, bool) { return c.tries[id].M.Lookup(k) } func (c *trieCollection) DeepEqual(l, r int) bool { @@ -109,11 +109,11 @@ func (c *trieCollection) Add() { c.tries = append(c.tries, c.b.MutEmpty()) } -func (c *trieCollection) Insert(id int, k uint64, v interface{}) { +func (c *trieCollection) Insert(id int, k uint64, v any) { c.tries[id].Insert(k, v) } -func (c *trieCollection) Update(id int, k uint64, v interface{}) { +func (c *trieCollection) Update(id int, k uint64, v any) { c.tries[id].Update(k, v) } @@ -140,7 +140,7 @@ func (c *trieCollection) Assign(l, r int) { c.tries[l] = c.tries[r] } -func average(x interface{}, y interface{}) interface{} { +func average(x any, y any) any { if x, ok := x.(float32); ok { if y, ok := y.(float32); ok { return (x + y) / 2.0 @@ -149,13 +149,13 @@ func average(x interface{}, y interface{}) interface{} { return x } -type builtinCollection []map[uint64]interface{} +type builtinCollection []map[uint64]any -func (c builtinCollection) Elements() []map[uint64]interface{} { +func (c builtinCollection) Elements() []map[uint64]any { return c } -func (c builtinCollection) Lookup(id int, k uint64) (interface{}, bool) { +func (c builtinCollection) Lookup(id int, k uint64) (any, bool) { v, ok := c[id][k] return v, ok } @@ -163,13 +163,13 @@ func (c builtinCollection) DeepEqual(l, r int) bool { return reflect.DeepEqual(c[l], c[r]) } -func (c builtinCollection) Insert(id int, k uint64, v interface{}) { +func (c builtinCollection) Insert(id int, k uint64, v any) { if _, ok := c[id][k]; !ok { c[id][k] = v } } -func (c builtinCollection) Update(id int, k uint64, v interface{}) { +func (c builtinCollection) Update(id int, k uint64, v any) { c[id][k] = v } @@ -178,7 +178,7 @@ func (c builtinCollection) Remove(id int, k uint64) { } func (c builtinCollection) Intersect(l int, r int) { - result := map[uint64]interface{}{} + result := map[uint64]any{} for k, v := range c[l] { if _, ok := c[r][k]; ok { result[k] = v @@ -188,7 +188,7 @@ func (c builtinCollection) Intersect(l int, r int) { } func (c builtinCollection) Merge(l int, r int) { - result := map[uint64]interface{}{} + result := map[uint64]any{} for k, v := range c[r] { result[k] = v } @@ -199,7 +199,7 @@ func (c builtinCollection) Merge(l int, r int) { } func (c builtinCollection) Average(l int, r int) { - avg := map[uint64]interface{}{} + avg := map[uint64]any{} for k, lv := range c[l] { if rv, ok := c[r][k]; ok { avg[k] = average(lv, rv) @@ -216,7 +216,7 @@ func (c builtinCollection) Average(l int, r int) { } func (c builtinCollection) Assign(l, r int) { - m := map[uint64]interface{}{} + m := map[uint64]any{} for k, v := range c[r] { m[k] = v } @@ -224,7 +224,7 @@ func (c builtinCollection) Assign(l, r int) { } func (c builtinCollection) Clear(id int) { - c[id] = map[uint64]interface{}{} + c[id] = map[uint64]any{} } func newTriesCollection(size int) *trieCollection { @@ -241,7 +241,7 @@ func newTriesCollection(size int) *trieCollection { func newMapsCollection(size int) *builtinCollection { maps := make(builtinCollection, size) for i := 0; i < size; i++ { - maps[i] = map[uint64]interface{}{} + maps[i] = map[uint64]any{} } return &maps } @@ -255,9 +255,9 @@ type operation struct { } // Apply the operation to maps. -func (op operation) Apply(maps mapCollection) interface{} { +func (op operation) Apply(maps mapCollection) any { type lookupresult struct { - v interface{} + v any ok bool } switch op.code { diff --git a/go/callgraph/vta/internal/trie/trie.go b/go/callgraph/vta/internal/trie/trie.go index 511fde51565..a8480192556 100644 --- a/go/callgraph/vta/internal/trie/trie.go +++ b/go/callgraph/vta/internal/trie/trie.go @@ -55,7 +55,7 @@ func (m Map) Size() int { } return m.n.size() } -func (m Map) Lookup(k uint64) (interface{}, bool) { +func (m Map) Lookup(k uint64) (any, bool) { if m.n != nil { if leaf := m.n.find(key(k)); leaf != nil { return leaf.v, true @@ -68,7 +68,7 @@ func (m Map) Lookup(k uint64) (interface{}, bool) { // %s string conversion for . func (m Map) String() string { var kvs []string - m.Range(func(u uint64, i interface{}) bool { + m.Range(func(u uint64, i any) bool { kvs = append(kvs, fmt.Sprintf("%d: %s", u, i)) return true }) @@ -78,7 +78,7 @@ func (m Map) String() string { // Range over the leaf (key, value) pairs in the map in order and // applies cb(key, value) to each. Stops early if cb returns false. // Returns true if all elements were visited without stopping early. -func (m Map) Range(cb func(uint64, interface{}) bool) bool { +func (m Map) Range(cb func(uint64, any) bool) bool { if m.n != nil { return m.n.visit(cb) } @@ -100,9 +100,9 @@ func (m Map) DeepEqual(other Map) bool { } // Elems are the (k,v) elements in the Map as a map[uint64]interface{} -func Elems(m Map) map[uint64]interface{} { - dest := make(map[uint64]interface{}, m.Size()) - m.Range(func(k uint64, v interface{}) bool { +func Elems(m Map) map[uint64]any { + dest := make(map[uint64]any, m.Size()) + m.Range(func(k uint64, v any) bool { dest[k] = v return true }) @@ -117,7 +117,7 @@ type node interface { // visit the leaves (key, value) pairs in the map in order and // applies cb(key, value) to each. Stops early if cb returns false. // Returns true if all elements were visited without stopping early. - visit(cb func(uint64, interface{}) bool) bool + visit(cb func(uint64, any) bool) bool // Two nodes contain the same elements regardless of scope. deepEqual(node) bool @@ -139,7 +139,7 @@ type empty struct { // leaf represents a single pair. type leaf struct { k key - v interface{} + v any } // branch represents a tree node within the Patricia trie. @@ -215,13 +215,13 @@ func (br *branch) deepEqual(m node) bool { return false } -func (*empty) visit(cb func(uint64, interface{}) bool) bool { +func (*empty) visit(cb func(uint64, any) bool) bool { return true } -func (l *leaf) visit(cb func(uint64, interface{}) bool) bool { +func (l *leaf) visit(cb func(uint64, any) bool) bool { return cb(uint64(l.k), l.v) } -func (br *branch) visit(cb func(uint64, interface{}) bool) bool { +func (br *branch) visit(cb func(uint64, any) bool) bool { if !br.left.visit(cb) { return false } diff --git a/go/callgraph/vta/internal/trie/trie_test.go b/go/callgraph/vta/internal/trie/trie_test.go index 71fd398f12c..817cb5c5e28 100644 --- a/go/callgraph/vta/internal/trie/trie_test.go +++ b/go/callgraph/vta/internal/trie/trie_test.go @@ -34,8 +34,8 @@ func TestScope(t *testing.T) { } func TestCollision(t *testing.T) { - var x interface{} = 1 - var y interface{} = 2 + var x any = 1 + var y any = 2 if v := TakeLhs(x, y); v != x { t.Errorf("TakeLhs(%s, %s) got %s. want %s", x, y, v, x) @@ -57,7 +57,7 @@ func TestDefault(t *testing.T) { if v, ok := def.Lookup(123); !(v == nil && !ok) { t.Errorf("Scope{}.Lookup() = (%s, %v) not (nil, false)", v, ok) } - if !def.Range(func(k uint64, v interface{}) bool { + if !def.Range(func(k uint64, v any) bool { t.Errorf("Scope{}.Range() called it callback on %d:%s", k, v) return true }) { @@ -114,7 +114,7 @@ func TestEmpty(t *testing.T) { if l := e.n.find(123); l != nil { t.Errorf("empty.find(123) got %v. want nil", l) } - e.Range(func(k uint64, v interface{}) bool { + e.Range(func(k uint64, v any) bool { t.Errorf("empty.Range() called it callback on %d:%s", k, v) return true }) @@ -129,23 +129,23 @@ func TestCreate(t *testing.T) { // The node orders are printed in lexicographic little-endian. b := NewBuilder() for _, c := range []struct { - m map[uint64]interface{} + m map[uint64]any want string }{ { - map[uint64]interface{}{}, + map[uint64]any{}, "{}", }, { - map[uint64]interface{}{1: "a"}, + map[uint64]any{1: "a"}, "{1: a}", }, { - map[uint64]interface{}{2: "b", 1: "a"}, + map[uint64]any{2: "b", 1: "a"}, "{1: a, 2: b}", }, { - map[uint64]interface{}{1: "x", 4: "y", 5: "z"}, + map[uint64]any{1: "x", 4: "y", 5: "z"}, "{1: x, 4: y, 5: z}", }, } { @@ -158,7 +158,7 @@ func TestCreate(t *testing.T) { func TestElems(t *testing.T) { b := NewBuilder() - for _, orig := range []map[uint64]interface{}{ + for _, orig := range []map[uint64]any{ {}, {1: "a"}, {1: "a", 2: "b"}, @@ -174,10 +174,10 @@ func TestElems(t *testing.T) { func TestRange(t *testing.T) { b := NewBuilder() - m := b.Create(map[uint64]interface{}{1: "x", 3: "y", 5: "z", 6: "stop", 8: "a"}) + m := b.Create(map[uint64]any{1: "x", 3: "y", 5: "z", 6: "stop", 8: "a"}) calls := 0 - cb := func(k uint64, v interface{}) bool { + cb := func(k uint64, v any) bool { t.Logf("visiting (%d, %v)", k, v) calls++ return k%2 != 0 // stop after the first even number. @@ -195,7 +195,7 @@ func TestRange(t *testing.T) { } func TestDeepEqual(t *testing.T) { - for _, m := range []map[uint64]interface{}{ + for _, m := range []map[uint64]any{ {}, {1: "x"}, {1: "x", 2: "y"}, @@ -210,32 +210,32 @@ func TestDeepEqual(t *testing.T) { func TestNotDeepEqual(t *testing.T) { for _, c := range []struct { - left map[uint64]interface{} - right map[uint64]interface{} + left map[uint64]any + right map[uint64]any }{ { - map[uint64]interface{}{1: "x"}, - map[uint64]interface{}{}, + map[uint64]any{1: "x"}, + map[uint64]any{}, }, { - map[uint64]interface{}{}, - map[uint64]interface{}{1: "y"}, + map[uint64]any{}, + map[uint64]any{1: "y"}, }, { - map[uint64]interface{}{1: "x"}, - map[uint64]interface{}{1: "y"}, + map[uint64]any{1: "x"}, + map[uint64]any{1: "y"}, }, { - map[uint64]interface{}{1: "x"}, - map[uint64]interface{}{1: "x", 2: "Y"}, + map[uint64]any{1: "x"}, + map[uint64]any{1: "x", 2: "Y"}, }, { - map[uint64]interface{}{1: "x", 2: "Y"}, - map[uint64]interface{}{1: "x"}, + map[uint64]any{1: "x", 2: "Y"}, + map[uint64]any{1: "x"}, }, { - map[uint64]interface{}{1: "x", 2: "y"}, - map[uint64]interface{}{1: "x", 2: "Y"}, + map[uint64]any{1: "x", 2: "y"}, + map[uint64]any{1: "x", 2: "Y"}, }, } { l := NewBuilder().Create(c.left) @@ -249,97 +249,97 @@ func TestNotDeepEqual(t *testing.T) { func TestMerge(t *testing.T) { b := NewBuilder() for _, c := range []struct { - left map[uint64]interface{} - right map[uint64]interface{} + left map[uint64]any + right map[uint64]any want string }{ { - map[uint64]interface{}{}, - map[uint64]interface{}{}, + map[uint64]any{}, + map[uint64]any{}, "{}", }, { - map[uint64]interface{}{}, - map[uint64]interface{}{1: "a"}, + map[uint64]any{}, + map[uint64]any{1: "a"}, "{1: a}", }, { - map[uint64]interface{}{1: "a"}, - map[uint64]interface{}{}, + map[uint64]any{1: "a"}, + map[uint64]any{}, "{1: a}", }, { - map[uint64]interface{}{1: "a", 2: "b"}, - map[uint64]interface{}{}, + map[uint64]any{1: "a", 2: "b"}, + map[uint64]any{}, "{1: a, 2: b}", }, { - map[uint64]interface{}{1: "x"}, - map[uint64]interface{}{1: "y"}, + map[uint64]any{1: "x"}, + map[uint64]any{1: "y"}, "{1: x}", // default collision is left }, { - map[uint64]interface{}{1: "x"}, - map[uint64]interface{}{2: "y"}, + map[uint64]any{1: "x"}, + map[uint64]any{2: "y"}, "{1: x, 2: y}", }, { - map[uint64]interface{}{4: "y", 5: "z"}, - map[uint64]interface{}{1: "x"}, + map[uint64]any{4: "y", 5: "z"}, + map[uint64]any{1: "x"}, "{1: x, 4: y, 5: z}", }, { - map[uint64]interface{}{1: "x", 5: "z"}, - map[uint64]interface{}{4: "y"}, + map[uint64]any{1: "x", 5: "z"}, + map[uint64]any{4: "y"}, "{1: x, 4: y, 5: z}", }, { - map[uint64]interface{}{1: "x", 4: "y"}, - map[uint64]interface{}{5: "z"}, + map[uint64]any{1: "x", 4: "y"}, + map[uint64]any{5: "z"}, "{1: x, 4: y, 5: z}", }, { - map[uint64]interface{}{1: "a", 4: "c"}, - map[uint64]interface{}{2: "b", 5: "d"}, + map[uint64]any{1: "a", 4: "c"}, + map[uint64]any{2: "b", 5: "d"}, "{1: a, 2: b, 4: c, 5: d}", }, { - map[uint64]interface{}{1: "a", 4: "c"}, - map[uint64]interface{}{2: "b", 5 + 8: "d"}, + map[uint64]any{1: "a", 4: "c"}, + map[uint64]any{2: "b", 5 + 8: "d"}, "{1: a, 2: b, 4: c, 13: d}", }, { - map[uint64]interface{}{2: "b", 5 + 8: "d"}, - map[uint64]interface{}{1: "a", 4: "c"}, + map[uint64]any{2: "b", 5 + 8: "d"}, + map[uint64]any{1: "a", 4: "c"}, "{1: a, 2: b, 4: c, 13: d}", }, { - map[uint64]interface{}{1: "a", 4: "c"}, - map[uint64]interface{}{2: "b", 5 + 8: "d"}, + map[uint64]any{1: "a", 4: "c"}, + map[uint64]any{2: "b", 5 + 8: "d"}, "{1: a, 2: b, 4: c, 13: d}", }, { - map[uint64]interface{}{2: "b", 5 + 8: "d"}, - map[uint64]interface{}{1: "a", 4: "c"}, + map[uint64]any{2: "b", 5 + 8: "d"}, + map[uint64]any{1: "a", 4: "c"}, "{1: a, 2: b, 4: c, 13: d}", }, { - map[uint64]interface{}{2: "b", 5 + 8: "d"}, - map[uint64]interface{}{2: "", 3: "a"}, + map[uint64]any{2: "b", 5 + 8: "d"}, + map[uint64]any{2: "", 3: "a"}, "{2: b, 3: a, 13: d}", }, { // crafted for `!prefixesOverlap(p, m, q, n)` - left: map[uint64]interface{}{1: "a", 2 + 1: "b"}, - right: map[uint64]interface{}{4 + 1: "c", 4 + 2: "d"}, + left: map[uint64]any{1: "a", 2 + 1: "b"}, + right: map[uint64]any{4 + 1: "c", 4 + 2: "d"}, // p: 5, m: 2 q: 1, n: 2 want: "{1: a, 3: b, 5: c, 6: d}", }, { // crafted for `ord(m, n) && !zeroBit(q, m)` - left: map[uint64]interface{}{8 + 2 + 1: "a", 16 + 4: "b"}, - right: map[uint64]interface{}{16 + 8 + 2 + 1: "c", 16 + 8 + 4 + 2 + 1: "d"}, + left: map[uint64]any{8 + 2 + 1: "a", 16 + 4: "b"}, + right: map[uint64]any{16 + 8 + 2 + 1: "c", 16 + 8 + 4 + 2 + 1: "d"}, // left: p: 15, m: 16 // right: q: 27, n: 4 want: "{11: a, 20: b, 27: c, 31: d}", @@ -347,8 +347,8 @@ func TestMerge(t *testing.T) { { // crafted for `ord(n, m) && !zeroBit(p, n)` // p: 6, m: 1 q: 5, n: 2 - left: map[uint64]interface{}{4 + 2: "b", 4 + 2 + 1: "c"}, - right: map[uint64]interface{}{4: "a", 4 + 2 + 1: "dropped"}, + left: map[uint64]any{4 + 2: "b", 4 + 2 + 1: "c"}, + right: map[uint64]any{4: "a", 4 + 2 + 1: "dropped"}, want: "{4: a, 6: b, 7: c}", }, } { @@ -364,65 +364,65 @@ func TestIntersect(t *testing.T) { // Most of the test cases go after specific branches of intersect. b := NewBuilder() for _, c := range []struct { - left map[uint64]interface{} - right map[uint64]interface{} + left map[uint64]any + right map[uint64]any want string }{ { - left: map[uint64]interface{}{10: "a", 39: "b"}, - right: map[uint64]interface{}{10: "A", 39: "B", 75: "C"}, + left: map[uint64]any{10: "a", 39: "b"}, + right: map[uint64]any{10: "A", 39: "B", 75: "C"}, want: "{10: a, 39: b}", }, { - left: map[uint64]interface{}{10: "a", 39: "b"}, - right: map[uint64]interface{}{}, + left: map[uint64]any{10: "a", 39: "b"}, + right: map[uint64]any{}, want: "{}", }, { - left: map[uint64]interface{}{}, - right: map[uint64]interface{}{10: "A", 39: "B", 75: "C"}, + left: map[uint64]any{}, + right: map[uint64]any{10: "A", 39: "B", 75: "C"}, want: "{}", }, { // m == n && p == q && left.(*empty) case - left: map[uint64]interface{}{4: 1, 6: 3, 10: 8, 15: "on left"}, - right: map[uint64]interface{}{0: 8, 7: 6, 11: 0, 15: "on right"}, + left: map[uint64]any{4: 1, 6: 3, 10: 8, 15: "on left"}, + right: map[uint64]any{0: 8, 7: 6, 11: 0, 15: "on right"}, want: "{15: on left}", }, { // m == n && p == q && right.(*empty) case - left: map[uint64]interface{}{0: "on left", 1: 2, 2: 3, 3: 1, 7: 3}, - right: map[uint64]interface{}{0: "on right", 5: 1, 6: 8}, + left: map[uint64]any{0: "on left", 1: 2, 2: 3, 3: 1, 7: 3}, + right: map[uint64]any{0: "on right", 5: 1, 6: 8}, want: "{0: on left}", }, { // m == n && p == q && both left and right are not empty - left: map[uint64]interface{}{1: "a", 2: "b", 3: "c"}, - right: map[uint64]interface{}{0: "A", 1: "B", 2: "C"}, + left: map[uint64]any{1: "a", 2: "b", 3: "c"}, + right: map[uint64]any{0: "A", 1: "B", 2: "C"}, want: "{1: a, 2: b}", }, { // m == n && p == q && both left and right are not empty - left: map[uint64]interface{}{1: "a", 2: "b", 3: "c"}, - right: map[uint64]interface{}{0: "A", 1: "B", 2: "C"}, + left: map[uint64]any{1: "a", 2: "b", 3: "c"}, + right: map[uint64]any{0: "A", 1: "B", 2: "C"}, want: "{1: a, 2: b}", }, { // !prefixesOverlap(p, m, q, n) // p = 1, m = 2, q = 5, n = 2 - left: map[uint64]interface{}{0b001: 1, 0b011: 3}, - right: map[uint64]interface{}{0b100: 4, 0b111: 7}, + left: map[uint64]any{0b001: 1, 0b011: 3}, + right: map[uint64]any{0b100: 4, 0b111: 7}, want: "{}", }, { // ord(m, n) && zeroBit(q, m) // p = 3, m = 4, q = 0, n = 1 - left: map[uint64]interface{}{0b010: 2, 0b101: 5}, - right: map[uint64]interface{}{0b000: 0, 0b001: 1}, + left: map[uint64]any{0b010: 2, 0b101: 5}, + right: map[uint64]any{0b000: 0, 0b001: 1}, want: "{}", }, { // ord(m, n) && !zeroBit(q, m) // p = 29, m = 2, q = 30, n = 1 - left: map[uint64]interface{}{ + left: map[uint64]any{ 0b11101: "29", 0b11110: "30", }, - right: map[uint64]interface{}{ + right: map[uint64]any{ 0b11110: "30 on right", 0b11111: "31", }, @@ -430,14 +430,14 @@ func TestIntersect(t *testing.T) { }, { // ord(n, m) && zeroBit(p, n) // p = 5, m = 2, q = 3, n = 4 - left: map[uint64]interface{}{0b000: 0, 0b001: 1}, - right: map[uint64]interface{}{0b010: 2, 0b101: 5}, + left: map[uint64]any{0b000: 0, 0b001: 1}, + right: map[uint64]any{0b010: 2, 0b101: 5}, want: "{}", }, { // default case // p = 5, m = 2, q = 3, n = 4 - left: map[uint64]interface{}{0b100: 1, 0b110: 3}, - right: map[uint64]interface{}{0b000: 8, 0b111: 6}, + left: map[uint64]any{0b100: 1, 0b110: 3}, + right: map[uint64]any{0b000: 8, 0b111: 6}, want: "{}", }, } { @@ -451,10 +451,10 @@ func TestIntersect(t *testing.T) { func TestIntersectWith(t *testing.T) { b := NewBuilder() - l := b.Create(map[uint64]interface{}{10: 2.0, 39: 32.0}) - r := b.Create(map[uint64]interface{}{10: 6.0, 39: 10.0, 75: 1.0}) + l := b.Create(map[uint64]any{10: 2.0, 39: 32.0}) + r := b.Create(map[uint64]any{10: 6.0, 39: 10.0, 75: 1.0}) - prodIfDifferent := func(x interface{}, y interface{}) interface{} { + prodIfDifferent := func(x any, y any) any { if x, ok := x.(float64); ok { if y, ok := y.(float64); ok { if x == y { @@ -478,24 +478,24 @@ func TestRemove(t *testing.T) { // Most of the test cases go after specific branches of intersect. b := NewBuilder() for _, c := range []struct { - m map[uint64]interface{} + m map[uint64]any key uint64 want string }{ - {map[uint64]interface{}{}, 10, "{}"}, - {map[uint64]interface{}{10: "a"}, 10, "{}"}, - {map[uint64]interface{}{39: "b"}, 10, "{39: b}"}, + {map[uint64]any{}, 10, "{}"}, + {map[uint64]any{10: "a"}, 10, "{}"}, + {map[uint64]any{39: "b"}, 10, "{39: b}"}, // Branch cases: // !matchPrefix(kp, br.prefix, br.branching) - {map[uint64]interface{}{10: "a", 39: "b"}, 128, "{10: a, 39: b}"}, + {map[uint64]any{10: "a", 39: "b"}, 128, "{10: a, 39: b}"}, // case: left == br.left && right == br.right - {map[uint64]interface{}{10: "a", 39: "b"}, 16, "{10: a, 39: b}"}, + {map[uint64]any{10: "a", 39: "b"}, 16, "{10: a, 39: b}"}, // left updated and is empty. - {map[uint64]interface{}{10: "a", 39: "b"}, 10, "{39: b}"}, + {map[uint64]any{10: "a", 39: "b"}, 10, "{39: b}"}, // right updated and is empty. - {map[uint64]interface{}{10: "a", 39: "b"}, 39, "{10: a}"}, + {map[uint64]any{10: "a", 39: "b"}, 39, "{10: a}"}, // final b.mkBranch(...) case. - {map[uint64]interface{}{10: "a", 39: "b", 128: "c"}, 39, "{10: a, 128: c}"}, + {map[uint64]any{10: "a", 39: "b", 128: "c"}, 39, "{10: a, 128: c}"}, } { pre := b.Create(c.m) post := b.Remove(pre, c.key) @@ -507,8 +507,8 @@ func TestRemove(t *testing.T) { func TestRescope(t *testing.T) { b := NewBuilder() - l := b.Create(map[uint64]interface{}{10: "a", 39: "b"}) - r := b.Create(map[uint64]interface{}{10: "A", 39: "B", 75: "C"}) + l := b.Create(map[uint64]any{10: "a", 39: "b"}) + r := b.Create(map[uint64]any{10: "A", 39: "B", 75: "C"}) b.Rescope() @@ -526,8 +526,8 @@ func TestRescope(t *testing.T) { func TestSharing(t *testing.T) { b := NewBuilder() - l := b.Create(map[uint64]interface{}{0: "a", 1: "b"}) - r := b.Create(map[uint64]interface{}{1: "B", 2: "C"}) + l := b.Create(map[uint64]any{0: "a", 1: "b"}) + r := b.Create(map[uint64]any{1: "B", 2: "C"}) rleftold := r.n.(*branch).left diff --git a/go/callgraph/vta/propagation.go b/go/callgraph/vta/propagation.go index f448cde1135..6ce1ca9e322 100644 --- a/go/callgraph/vta/propagation.go +++ b/go/callgraph/vta/propagation.go @@ -120,7 +120,7 @@ func (ptm propTypeMap) propTypes(n node) func(yield func(propType) bool) { // (https://go.dev/issue/65237). return func(yield func(propType) bool) { if types := ptm[n]; types != nil { - types.M.Range(func(_ uint64, elem interface{}) bool { + types.M.Range(func(_ uint64, elem any) bool { return yield(elem.(propType)) }) } diff --git a/go/callgraph/vta/vta_test.go b/go/callgraph/vta/vta_test.go index ea7d584d2d9..42610abb139 100644 --- a/go/callgraph/vta/vta_test.go +++ b/go/callgraph/vta/vta_test.go @@ -118,7 +118,7 @@ func TestVTAProgVsFuncSet(t *testing.T) { // available, which can happen when using analysis package. A successful // test simply does not panic. func TestVTAPanicMissingDefinitions(t *testing.T) { - run := func(pass *analysis.Pass) (interface{}, error) { + run := func(pass *analysis.Pass) (any, error) { s := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) CallGraph(ssautil.AllFunctions(s.Pkg.Prog), cha.CallGraph(s.Pkg.Prog)) return nil, nil diff --git a/go/expect/expect.go b/go/expect/expect.go index be0e1dd23e6..1c002d91b60 100644 --- a/go/expect/expect.go +++ b/go/expect/expect.go @@ -66,9 +66,9 @@ import ( // It knows the position of the start of the comment, and the name and // arguments that make up the note. type Note struct { - Pos token.Pos // The position at which the note identifier appears - Name string // the name associated with the note - Args []interface{} // the arguments for the note + Pos token.Pos // The position at which the note identifier appears + Name string // the name associated with the note + Args []any // the arguments for the note } // ReadFile is the type of a function that can provide file contents for a @@ -85,7 +85,7 @@ type ReadFile func(filename string) ([]byte, error) // MatchBefore returns the range of the line that matched the pattern, and // invalid positions if there was no match, or an error if the line could not be // found. -func MatchBefore(fset *token.FileSet, readFile ReadFile, end token.Pos, pattern interface{}) (token.Pos, token.Pos, error) { +func MatchBefore(fset *token.FileSet, readFile ReadFile, end token.Pos, pattern any) (token.Pos, token.Pos, error) { f := fset.File(end) content, err := readFile(f.Name()) if err != nil { diff --git a/go/expect/expect_test.go b/go/expect/expect_test.go index cc585418d1b..d1ce96b868e 100644 --- a/go/expect/expect_test.go +++ b/go/expect/expect_test.go @@ -18,7 +18,7 @@ func TestMarker(t *testing.T) { filename string expectNotes int expectMarkers map[string]string - expectChecks map[string][]interface{} + expectChecks map[string][]any }{ { filename: "testdata/test.go", @@ -36,7 +36,7 @@ func TestMarker(t *testing.T) { "NonIdentifier": "+", "StringMarker": "\"hello\"", }, - expectChecks: map[string][]interface{}{ + expectChecks: map[string][]any{ "αSimpleMarker": nil, "StringAndInt": {"Number %d", int64(12)}, "Bool": {true}, @@ -140,7 +140,7 @@ func TestMarker(t *testing.T) { } } -func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern interface{}) { +func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern any) { start, end, err := expect.MatchBefore(fset, readFile, pos, pattern) if err != nil { t.Errorf("%v: MatchBefore failed: %v", fset.Position(pos), err) diff --git a/go/expect/extract.go b/go/expect/extract.go index 902b1e806e4..9cc5c8171fd 100644 --- a/go/expect/extract.go +++ b/go/expect/extract.go @@ -32,7 +32,7 @@ type Identifier string // See the package documentation for details about the syntax of those // notes. func Parse(fset *token.FileSet, filename string, content []byte) ([]*Note, error) { - var src interface{} + var src any if content != nil { src = content } @@ -220,7 +220,7 @@ func (t *tokens) Pos() token.Pos { return t.base + token.Pos(t.scanner.Position.Offset) } -func (t *tokens) Errorf(msg string, args ...interface{}) { +func (t *tokens) Errorf(msg string, args ...any) { if t.err != nil { return } @@ -280,9 +280,9 @@ func parseNote(t *tokens) *Note { } } -func parseArgumentList(t *tokens) []interface{} { - args := []interface{}{} // @name() is represented by a non-nil empty slice. - t.Consume() // '(' +func parseArgumentList(t *tokens) []any { + args := []any{} // @name() is represented by a non-nil empty slice. + t.Consume() // '(' t.Skip('\n') for t.Token() != ')' { args = append(args, parseArgument(t)) @@ -300,7 +300,7 @@ func parseArgumentList(t *tokens) []interface{} { return args } -func parseArgument(t *tokens) interface{} { +func parseArgument(t *tokens) any { switch t.Token() { case scanner.Ident: v := t.Consume() diff --git a/go/internal/cgo/cgo.go b/go/internal/cgo/cgo.go index 697974bb9b2..735efeb531d 100644 --- a/go/internal/cgo/cgo.go +++ b/go/internal/cgo/cgo.go @@ -203,7 +203,7 @@ func envList(key, def string) []string { // stringList's arguments should be a sequence of string or []string values. // stringList flattens them into a single []string. -func stringList(args ...interface{}) []string { +func stringList(args ...any) []string { var x []string for _, arg := range args { switch arg := arg.(type) { diff --git a/go/internal/gccgoimporter/parser.go b/go/internal/gccgoimporter/parser.go index f70946edbe4..7b0702892c4 100644 --- a/go/internal/gccgoimporter/parser.go +++ b/go/internal/gccgoimporter/parser.go @@ -86,7 +86,7 @@ func (e importError) Error() string { return fmt.Sprintf("import error %s (byte offset = %d): %s", e.pos, e.pos.Offset, e.err) } -func (p *parser) error(err interface{}) { +func (p *parser) error(err any) { if s, ok := err.(string); ok { err = errors.New(s) } @@ -94,7 +94,7 @@ func (p *parser) error(err interface{}) { panic(importError{p.scanner.Pos(), err.(error)}) } -func (p *parser) errorf(format string, args ...interface{}) { +func (p *parser) errorf(format string, args ...any) { p.error(fmt.Errorf(format, args...)) } @@ -492,7 +492,7 @@ func (p *parser) reserve(n int) { // used to resolve named types, or it can be a *types.Pointer, // used to resolve pointers to named types in case they are referenced // by embedded fields. -func (p *parser) update(t types.Type, nlist []interface{}) { +func (p *parser) update(t types.Type, nlist []any) { if t == reserved { p.errorf("internal error: update(%v) invoked on reserved", nlist) } @@ -529,7 +529,7 @@ func (p *parser) update(t types.Type, nlist []interface{}) { // NamedType = TypeName [ "=" ] Type { Method } . // TypeName = ExportedName . // Method = "func" "(" Param ")" Name ParamList ResultList [InlineBody] ";" . -func (p *parser) parseNamedType(nlist []interface{}) types.Type { +func (p *parser) parseNamedType(nlist []any) types.Type { pkg, name := p.parseExportedName() scope := pkg.Scope() obj := scope.Lookup(name) @@ -648,7 +648,7 @@ func (p *parser) parseInt() int { // parseArrayOrSliceType parses an ArrayOrSliceType: // // ArrayOrSliceType = "[" [ int ] "]" Type . -func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []any) types.Type { p.expect('[') if p.tok == ']' { p.next() @@ -673,7 +673,7 @@ func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []interface{}) // parseMapType parses a MapType: // // MapType = "map" "[" Type "]" Type . -func (p *parser) parseMapType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseMapType(pkg *types.Package, nlist []any) types.Type { p.expectKeyword("map") t := new(types.Map) @@ -691,7 +691,7 @@ func (p *parser) parseMapType(pkg *types.Package, nlist []interface{}) types.Typ // parseChanType parses a ChanType: // // ChanType = "chan" ["<-" | "-<"] Type . -func (p *parser) parseChanType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseChanType(pkg *types.Package, nlist []any) types.Type { p.expectKeyword("chan") t := new(types.Chan) @@ -720,7 +720,7 @@ func (p *parser) parseChanType(pkg *types.Package, nlist []interface{}) types.Ty // parseStructType parses a StructType: // // StructType = "struct" "{" { Field } "}" . -func (p *parser) parseStructType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseStructType(pkg *types.Package, nlist []any) types.Type { p.expectKeyword("struct") t := new(types.Struct) @@ -793,7 +793,7 @@ func (p *parser) parseResultList(pkg *types.Package) *types.Tuple { // parseFunctionType parses a FunctionType: // // FunctionType = ParamList ResultList . -func (p *parser) parseFunctionType(pkg *types.Package, nlist []interface{}) *types.Signature { +func (p *parser) parseFunctionType(pkg *types.Package, nlist []any) *types.Signature { t := new(types.Signature) p.update(t, nlist) @@ -837,7 +837,7 @@ func (p *parser) parseFunc(pkg *types.Package) *types.Func { // parseInterfaceType parses an InterfaceType: // // InterfaceType = "interface" "{" { ("?" Type | Func) ";" } "}" . -func (p *parser) parseInterfaceType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseInterfaceType(pkg *types.Package, nlist []any) types.Type { p.expectKeyword("interface") t := new(types.Interface) @@ -868,7 +868,7 @@ func (p *parser) parseInterfaceType(pkg *types.Package, nlist []interface{}) typ // parsePointerType parses a PointerType: // // PointerType = "*" ("any" | Type) . -func (p *parser) parsePointerType(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parsePointerType(pkg *types.Package, nlist []any) types.Type { p.expect('*') if p.tok == scanner.Ident { p.expectKeyword("any") @@ -888,7 +888,7 @@ func (p *parser) parsePointerType(pkg *types.Package, nlist []interface{}) types // parseTypeSpec parses a TypeSpec: // // TypeSpec = NamedType | MapType | ChanType | StructType | InterfaceType | PointerType | ArrayOrSliceType | FunctionType . -func (p *parser) parseTypeSpec(pkg *types.Package, nlist []interface{}) types.Type { +func (p *parser) parseTypeSpec(pkg *types.Package, nlist []any) types.Type { switch p.tok { case scanner.String: return p.parseNamedType(nlist) @@ -980,14 +980,14 @@ func lookupBuiltinType(typ int) types.Type { // Type = "<" "type" ( "-" int | int [ TypeSpec ] ) ">" . // // parseType updates the type map to t for all type numbers n. -func (p *parser) parseType(pkg *types.Package, n ...interface{}) types.Type { +func (p *parser) parseType(pkg *types.Package, n ...any) types.Type { p.expect('<') t, _ := p.parseTypeAfterAngle(pkg, n...) return t } // (*parser).Type after reading the "<". -func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...interface{}) (t types.Type, n1 int) { +func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...any) (t types.Type, n1 int) { p.expectKeyword("type") n1 = 0 @@ -1030,7 +1030,7 @@ func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...interface{}) (t ty // parseTypeExtended is identical to parseType, but if the type in // question is a saved type, returns the index as well as the type // pointer (index returned is zero if we parsed a builtin). -func (p *parser) parseTypeExtended(pkg *types.Package, n ...interface{}) (t types.Type, n1 int) { +func (p *parser) parseTypeExtended(pkg *types.Package, n ...any) (t types.Type, n1 int) { p.expect('<') t, n1 = p.parseTypeAfterAngle(pkg, n...) return @@ -1119,7 +1119,7 @@ func (p *parser) parseTypes(pkg *types.Package) { } // parseSavedType parses one saved type definition. -func (p *parser) parseSavedType(pkg *types.Package, i int, nlist []interface{}) { +func (p *parser) parseSavedType(pkg *types.Package, i int, nlist []any) { defer func(s *scanner.Scanner, tok rune, lit string) { p.scanner = s p.tok = tok diff --git a/go/loader/loader.go b/go/loader/loader.go index 2d4865f664f..d06f95ad76c 100644 --- a/go/loader/loader.go +++ b/go/loader/loader.go @@ -215,7 +215,7 @@ func (conf *Config) fset() *token.FileSet { // src specifies the parser input as a string, []byte, or io.Reader, and // filename is its apparent name. If src is nil, the contents of // filename are read from the file system. -func (conf *Config) ParseFile(filename string, src interface{}) (*ast.File, error) { +func (conf *Config) ParseFile(filename string, src any) (*ast.File, error) { // TODO(adonovan): use conf.build() etc like parseFiles does. return parser.ParseFile(conf.fset(), filename, src, conf.ParserMode) } diff --git a/go/packages/gopackages/main.go b/go/packages/gopackages/main.go index 3841ac3410b..7ec0bdc7bdd 100644 --- a/go/packages/gopackages/main.go +++ b/go/packages/gopackages/main.go @@ -248,7 +248,7 @@ func (app *application) print(lpkg *packages.Package) { // e.g. --flag=one --flag=two would produce []string{"one", "two"}. type stringListValue []string -func (ss *stringListValue) Get() interface{} { return []string(*ss) } +func (ss *stringListValue) Get() any { return []string(*ss) } func (ss *stringListValue) String() string { return fmt.Sprintf("%q", *ss) } diff --git a/go/packages/overlay_test.go b/go/packages/overlay_test.go index 9edd0d646ed..1108461926f 100644 --- a/go/packages/overlay_test.go +++ b/go/packages/overlay_test.go @@ -32,7 +32,7 @@ func testOverlayChangesPackageName(t *testing.T, exporter packagestest.Exporter) log.SetFlags(log.Lshortfile) exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a.go": "package foo\nfunc f(){}\n", }, Overlay: map[string][]byte{ @@ -62,7 +62,7 @@ func testOverlayChangesBothPackageNames(t *testing.T, exporter packagestest.Expo log.SetFlags(log.Lshortfile) exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a.go": "package foo\nfunc g(){}\n", "a_test.go": "package foo\nfunc f(){}\n", }, @@ -110,7 +110,7 @@ func TestOverlayChangesTestPackageName(t *testing.T) { func testOverlayChangesTestPackageName(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a_test.go": "package foo\nfunc f(){}\n", }, Overlay: map[string][]byte{ @@ -194,7 +194,7 @@ func TestHello(t *testing.T) { // First, get the source of truth by loading the package, all on disk. onDisk := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": aFile, "a/a_test.go": aTestVariant, "a/a_x_test.go": aXTest, @@ -213,7 +213,7 @@ func TestHello(t *testing.T) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": aFile, "a/a_test.go": aTestVariant, "a/a_x_test.go": ``, // empty x test on disk @@ -248,7 +248,7 @@ func TestOverlay(t *testing.T) { testAllOrModulesParallel(t, testOverlay) } func testOverlay(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; const A = "a" + b.B`, "b/b.go": `package b; import "golang.org/fake/c"; const B = "b" + c.C`, "c/c.go": `package c; const C = "c"`, @@ -316,7 +316,7 @@ func TestOverlayDeps(t *testing.T) { testAllOrModulesParallel(t, testOverlayDeps func testOverlayDeps(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "c/c.go": `package c; const C = "c"`, "c/c_test.go": `package c; import "testing"; func TestC(t *testing.T) {}`, }, @@ -366,7 +366,7 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; const A = "a" + b.B`, "b/b.go": `package b; import "golang.org/fake/c"; const B = "b" + c.C`, "c/c.go": `package c; const C = "c"`, @@ -375,7 +375,7 @@ func testNewPackagesInOverlay(t *testing.T, exporter packagestest.Exporter) { }, { Name: "example.com/extramodule", - Files: map[string]interface{}{ + Files: map[string]any{ "pkg/x.go": "package pkg\n", }, }, @@ -471,7 +471,7 @@ func testOverlayNewPackageAndTest(t *testing.T, exporter packagestest.Exporter) exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "foo.txt": "placeholder", }, }, @@ -623,7 +623,7 @@ func TestOverlayGOPATHVendoring(t *testing.T) { exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "vendor/vendor.com/foo/foo.go": `package foo; const X = "hi"`, "user/user.go": `package user`, }, @@ -652,7 +652,7 @@ func TestContainsOverlay(t *testing.T) { testAllOrModulesParallel(t, testContain func testContainsOverlay(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"`, "b/b.go": `package b; import "golang.org/fake/c"`, "c/c.go": `package c`, @@ -681,7 +681,7 @@ func TestContainsOverlayXTest(t *testing.T) { testAllOrModulesParallel(t, testCo func testContainsOverlayXTest(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"`, "b/b.go": `package b; import "golang.org/fake/c"`, "c/c.go": `package c`, @@ -717,7 +717,7 @@ func testInvalidFilesBeforeOverlay(t *testing.T, exporter packagestest.Exporter) exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "d/d.go": ``, "main.go": ``, }, @@ -754,7 +754,7 @@ func testInvalidFilesBeforeOverlayContains(t *testing.T, exporter packagestest.E exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "d/d.go": `package d; import "net/http"; const Get = http.MethodGet; const Hello = "hello";`, "d/util.go": ``, "d/d_test.go": ``, @@ -861,7 +861,7 @@ func testInvalidXTestInGOPATH(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "x/x.go": `package x`, "x/x_test.go": ``, }, @@ -892,7 +892,7 @@ func testAddImportInOverlay(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a import ( @@ -961,7 +961,7 @@ func testLoadDifferentPatterns(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "foo.txt": "placeholder", "b/b.go": `package b import "golang.org/fake/a" diff --git a/go/packages/packages.go b/go/packages/packages.go index 342f019a0f9..6665a04c173 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -163,7 +163,7 @@ type Config struct { // If the user provides a logger, debug logging is enabled. // If the GOPACKAGESDEBUG environment variable is set to true, // but the logger is nil, default to log.Printf. - Logf func(format string, args ...interface{}) + Logf func(format string, args ...any) // Dir is the directory in which to run the build system's query tool // that provides information about the packages. @@ -566,13 +566,13 @@ type ModuleError struct { } func init() { - packagesinternal.GetDepsErrors = func(p interface{}) []*packagesinternal.PackageError { + packagesinternal.GetDepsErrors = func(p any) []*packagesinternal.PackageError { return p.(*Package).depsErrors } - packagesinternal.SetModFile = func(config interface{}, value string) { + packagesinternal.SetModFile = func(config any, value string) { config.(*Config).modFile = value } - packagesinternal.SetModFlag = func(config interface{}, value string) { + packagesinternal.SetModFlag = func(config any, value string) { config.(*Config).modFlag = value } packagesinternal.TypecheckCgo = int(typecheckCgo) @@ -741,7 +741,7 @@ func newLoader(cfg *Config) *loader { if debug { ld.Config.Logf = log.Printf } else { - ld.Config.Logf = func(format string, args ...interface{}) {} + ld.Config.Logf = func(format string, args ...any) {} } } if ld.Config.Mode == 0 { diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 06fa488d1ed..5678b265561 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -129,7 +129,7 @@ func TestLoadImportsGraph(t *testing.T) { testAllOrModulesParallel(t, testLoadIm func testLoadImportsGraph(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const A = 1`, "b/b.go": `package b; import ("golang.org/fake/a"; _ "container/list"); var B = a.A`, "c/c.go": `package c; import (_ "golang.org/fake/b"; _ "unsafe")`, @@ -305,7 +305,7 @@ func TestLoadImportsTestVariants(t *testing.T) { func testLoadImportsTestVariants(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package b`, "b/b_test.go": `package b`, @@ -346,11 +346,11 @@ func TestLoadAbsolutePath(t *testing.T) { exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/gopatha", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a`, }}, { Name: "golang.org/gopathb", - Files: map[string]interface{}{ + Files: map[string]any{ "b/b.go": `package b`, }}}) defer exported.Cleanup() @@ -381,7 +381,7 @@ func TestLoadArgumentListIsNotTooLong(t *testing.T) { argMax := 1_000_000 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/mod", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": `package main"`, }}}) defer exported.Cleanup() @@ -402,7 +402,7 @@ func TestVendorImports(t *testing.T) { exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "b"; import _ "golang.org/fake/c";`, "a/vendor/b/b.go": `package b; import _ "golang.org/fake/c"`, "c/c.go": `package c; import _ "b"`, @@ -463,7 +463,7 @@ func TestConfigDir(t *testing.T) { testAllOrModulesParallel(t, testConfigDir) } func testConfigDir(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const Name = "a" `, "a/b/b.go": `package b; const Name = "a/b"`, "b/b.go": `package b; const Name = "b"`, @@ -522,7 +522,7 @@ func testConfigFlags(t *testing.T, exporter packagestest.Exporter) { // Test satisfying +build line tags, with -tags flag. exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ // package a "a/a.go": `package a; import _ "golang.org/fake/a/b"`, "a/b.go": `// +build tag @@ -587,7 +587,7 @@ func testLoadTypes(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; import "golang.org/fake/c"; const A = "a" + b.B + c.C`, "b/b.go": `package b; const B = "b"`, "c/c.go": `package c; const C = "c" + 1`, @@ -640,7 +640,7 @@ func TestLoadTypesBits(t *testing.T) { testAllOrModulesParallel(t, testLoadTypes func testLoadTypesBits(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; const A = "a" + b.B`, "b/b.go": `package b; import "golang.org/fake/c"; const B = "b" + c.C`, "c/c.go": `package c; import "golang.org/fake/d"; const C = "c" + d.D`, @@ -716,7 +716,7 @@ func TestLoadSyntaxOK(t *testing.T) { testAllOrModulesParallel(t, testLoadSyntax func testLoadSyntaxOK(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; const A = "a" + b.B`, "b/b.go": `package b; import "golang.org/fake/c"; const B = "b" + c.C`, "c/c.go": `package c; import "golang.org/fake/d"; const C = "c" + d.D`, @@ -807,7 +807,7 @@ func testLoadDiamondTypes(t *testing.T, exporter packagestest.Exporter) { // We make a diamond dependency and check the type d.D is the same through both paths exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import ("golang.org/fake/b"; "golang.org/fake/c"); var _ = b.B == c.C`, "b/b.go": `package b; import "golang.org/fake/d"; var B d.D`, "c/c.go": `package c; import "golang.org/fake/d"; var C d.D`, @@ -850,7 +850,7 @@ func testLoadSyntaxError(t *testing.T, exporter packagestest.Exporter) { // should be IllTyped. exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"; const A = "a" + b.B`, "b/b.go": `package b; import "golang.org/fake/c"; const B = "b" + c.C`, "c/c.go": `package c; import "golang.org/fake/d"; const C = "c" + d.D`, @@ -922,7 +922,7 @@ func TestParseFileModifyAST(t *testing.T) { testAllOrModulesParallel(t, testPars func testParseFileModifyAST(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const A = "a" `, }}}) defer exported.Cleanup() @@ -1010,7 +1010,7 @@ func testLoadAllSyntaxImportErrors(t *testing.T, exporter packagestest.Exporter) exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "unicycle/unicycle.go": `package unicycle; import _ "unicycle"`, "bicycle1/bicycle1.go": `package bicycle1; import _ "bicycle2"`, "bicycle2/bicycle2.go": `package bicycle2; import _ "bicycle1"`, @@ -1090,7 +1090,7 @@ func TestAbsoluteFilenames(t *testing.T) { testAllOrModulesParallel(t, testAbsol func testAbsoluteFilenames(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const A = 1`, "b/b.go": `package b; import ("golang.org/fake/a"; _ "errors"); var B = a.A`, "b/vendor/a/a.go": `package a; const A = 1`, @@ -1180,7 +1180,7 @@ func TestContains(t *testing.T) { testAllOrModulesParallel(t, testContains) } func testContains(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"`, "b/b.go": `package b; import "golang.org/fake/c"`, "c/c.go": `package c`, @@ -1219,7 +1219,7 @@ func testSizes(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "unsafe"; const WordSize = 8*unsafe.Sizeof(int(0))`, }}}) defer exported.Cleanup() @@ -1257,7 +1257,7 @@ func TestNeedTypeSizesWithBadGOARCH(t *testing.T) { testAllOrModulesParallel(t, func(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "testdata", - Files: map[string]interface{}{"a/a.go": `package a`}}}) + Files: map[string]any{"a/a.go": `package a`}}}) defer exported.Cleanup() exported.Config.Mode = packages.NeedTypesSizes // or {,Info,Sizes} @@ -1280,7 +1280,7 @@ func TestContainsFallbackSticks(t *testing.T) { func testContainsFallbackSticks(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import "golang.org/fake/b"`, "b/b.go": `package b; import "golang.org/fake/c"`, "c/c.go": `package c`, @@ -1313,7 +1313,7 @@ func TestNoPatterns(t *testing.T) { testAllOrModulesParallel(t, testNoPatterns) func testNoPatterns(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a;`, "a/b/b.go": `package b;`, }}}) @@ -1336,7 +1336,7 @@ func testJSON(t *testing.T, exporter packagestest.Exporter) { // TODO: add in some errors exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; const A = 1`, "b/b.go": `package b; import "golang.org/fake/a"; var B = a.A`, "c/c.go": `package c; import "golang.org/fake/b" ; var C = b.B`, @@ -1503,7 +1503,7 @@ func TestPatternPassthrough(t *testing.T) { testAllOrModulesParallel(t, testPatt func testPatternPassthrough(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a;`, }}}) defer exported.Cleanup() @@ -1563,7 +1563,7 @@ EOF } exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "bin/gopackagesdriver": driverScript, "golist/golist.go": "package golist", }}}) @@ -1639,7 +1639,7 @@ func TestBasicXTest(t *testing.T) { testAllOrModulesParallel(t, testBasicXTest) func testBasicXTest(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a;`, "a/a_test.go": `package a_test;`, }}}) @@ -1657,7 +1657,7 @@ func TestErrorMissingFile(t *testing.T) { testAllOrModulesParallel(t, testErrorM func testErrorMissingFile(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a_test.go": `package a;`, }}}) defer exported.Cleanup() @@ -1685,11 +1685,11 @@ func TestReturnErrorWhenUsingNonGoFiles(t *testing.T) { func testReturnErrorWhenUsingNonGoFiles(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/gopatha", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a`, }}, { Name: "golang.org/gopathb", - Files: map[string]interface{}{ + Files: map[string]any{ "b/b.c": `package b`, }}}) defer exported.Cleanup() @@ -1713,7 +1713,7 @@ func TestReturnErrorWhenUsingGoFilesInMultipleDirectories(t *testing.T) { func testReturnErrorWhenUsingGoFilesInMultipleDirectories(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/gopatha", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a`, "b/b.go": `package b`, }}}) @@ -1745,7 +1745,7 @@ func TestReturnErrorForUnexpectedDirectoryLayout(t *testing.T) { func testReturnErrorForUnexpectedDirectoryLayout(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/gopatha", - Files: map[string]interface{}{ + Files: map[string]any{ "a/testdata/a.go": `package a; import _ "b"`, "a/vendor/b/b.go": `package b; import _ "fmt"`, }}}) @@ -1774,7 +1774,7 @@ func TestMissingDependency(t *testing.T) { testAllOrModulesParallel(t, testMissi func testMissingDependency(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "this/package/doesnt/exist"`, }}}) defer exported.Cleanup() @@ -1796,7 +1796,7 @@ func TestAdHocContains(t *testing.T) { testAllOrModulesParallel(t, testAdHocCont func testAdHocContains(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a;`, }}}) defer exported.Cleanup() @@ -1839,7 +1839,7 @@ func testCgoNoCcompiler(t *testing.T, exporter packagestest.Exporter) { testenv.NeedsTool(t, "cgo") exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a import "net/http" const A = http.MethodGet @@ -1873,7 +1873,7 @@ func testCgoMissingFile(t *testing.T, exporter packagestest.Exporter) { testenv.NeedsTool(t, "cgo") exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a // #include "foo.h" @@ -1962,7 +1962,7 @@ func testCgoNoSyntax(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "c/c.go": `package c; import "C"`, }, }}) @@ -2005,7 +2005,7 @@ func testCgoBadPkgConfig(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "c/c.go": `package c // #cgo pkg-config: --cflags -- foo @@ -2074,7 +2074,7 @@ func TestIssue32814(t *testing.T) { testAllOrModulesParallel(t, testIssue32814) func testIssue32814(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{}}}) + Files: map[string]any{}}}) defer exported.Cleanup() exported.Config.Mode = packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedTypesSizes @@ -2103,7 +2103,7 @@ func TestLoadTypesInfoWithoutNeedDeps(t *testing.T) { func testLoadTypesInfoWithoutNeedDeps(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package b`, }}}) @@ -2130,7 +2130,7 @@ func TestLoadWithNeedDeps(t *testing.T) { func testLoadWithNeedDeps(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package b; import _ "golang.org/fake/c"`, "c/c.go": `package c`, @@ -2174,7 +2174,7 @@ func TestImpliedLoadMode(t *testing.T) { func testImpliedLoadMode(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package b`, }}}) @@ -2243,7 +2243,7 @@ func TestMultiplePackageVersionsIssue36188(t *testing.T) { func testMultiplePackageVersionsIssue36188(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package main`, }}}) @@ -2363,7 +2363,7 @@ func TestCycleImportStack(t *testing.T) { func testCycleImportStack(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; import _ "golang.org/fake/b"`, "b/b.go": `package b; import _ "golang.org/fake/a"`, }}}) @@ -2393,7 +2393,7 @@ func TestForTestField(t *testing.T) { func testForTestField(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; func hello() {};`, "a/a_test.go": `package a; import "testing"; func TestA1(t *testing.T) {};`, "a/x_test.go": `package a_test; import "testing"; func TestA2(t *testing.T) {};`, @@ -2499,7 +2499,7 @@ func testIssue37098(t *testing.T, exporter packagestest.Exporter) { // file. exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ // The "package" statement must be included for SWIG sources to // be generated. "a/a.go": "package a", @@ -2550,7 +2550,7 @@ func TestIssue56632(t *testing.T) { exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/issue56632", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a`, "a/a_cgo.go": `package a @@ -2593,7 +2593,7 @@ func testInvalidFilesInXTest(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "d/d.go": `package d; import "net/http"; const d = http.MethodGet; func Get() string { return d; }`, "d/d2.go": ``, // invalid file "d/d_test.go": `package d_test; import "testing"; import "golang.org/fake/d"; func TestD(t *testing.T) { d.Get(); }`, @@ -2628,7 +2628,7 @@ func testTypecheckCgo(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "cgo/cgo.go": cgo, }, }, @@ -2662,7 +2662,7 @@ func testIssue48226(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{ { Name: "golang.org/fake/syntax", - Files: map[string]interface{}{ + Files: map[string]any{ "syntax.go": `package test`, }, }, @@ -2697,7 +2697,7 @@ func TestModule(t *testing.T) { func testModule(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{"a/a.go": `package a`}}}) + Files: map[string]any{"a/a.go": `package a`}}}) exported.Config.Mode = packages.NeedModule rootDir := filepath.Dir(filepath.Dir(exported.File("golang.org/fake", "a/a.go"))) @@ -2746,7 +2746,7 @@ func testExternal_NotHandled(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a`, "empty_driver/main.go": `package main @@ -2825,7 +2825,7 @@ func TestInvalidPackageName(t *testing.T) { func testInvalidPackageName(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": `package default func main() { @@ -3206,7 +3206,7 @@ func TestLoadTypesInfoWithoutSyntaxOrTypes(t *testing.T) { func testLoadTypesInfoWithoutSyntaxOrTypes(t *testing.T, exporter packagestest.Exporter) { exported := packagestest.Export(t, exporter, []packagestest.Module{{ Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "a/a.go": `package a; func foo() int { diff --git a/go/packages/packagestest/expect.go b/go/packages/packagestest/expect.go index dc41894a6ed..4be34191e62 100644 --- a/go/packages/packagestest/expect.go +++ b/go/packages/packagestest/expect.go @@ -72,7 +72,7 @@ const ( // // It is safe to call this repeatedly with different method sets, but it is // not safe to call it concurrently. -func (e *Exported) Expect(methods map[string]interface{}) error { +func (e *Exported) Expect(methods map[string]any) error { if err := e.getNotes(); err != nil { return err } @@ -98,7 +98,7 @@ func (e *Exported) Expect(methods map[string]interface{}) error { n = &expect.Note{ Pos: n.Pos, Name: markMethod, - Args: []interface{}{n.Name, n.Name}, + Args: []any{n.Name, n.Name}, } } mi, ok := ms[n.Name] @@ -222,7 +222,7 @@ func (e *Exported) getMarkers() error { } // set markers early so that we don't call getMarkers again from Expect e.markers = make(map[string]Range) - return e.Expect(map[string]interface{}{ + return e.Expect(map[string]any{ markMethod: e.Mark, }) } @@ -243,7 +243,7 @@ var ( // It takes the args remaining, and returns the args it did not consume. // This allows a converter to consume 0 args for well known types, or multiple // args for compound types. -type converter func(*expect.Note, []interface{}) (reflect.Value, []interface{}, error) +type converter func(*expect.Note, []any) (reflect.Value, []any, error) // method is used to track information about Invoke methods that is expensive to // calculate so that we can work it out once rather than per marker. @@ -259,19 +259,19 @@ type method struct { func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { switch { case pt == noteType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(n), args, nil }, nil case pt == fsetType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(e.ExpectFileSet), args, nil }, nil case pt == exportedType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(e), args, nil }, nil case pt == posType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -279,7 +279,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(r.Start), remains, nil }, nil case pt == positionType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -287,7 +287,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(e.ExpectFileSet.Position(r.Start)), remains, nil }, nil case pt == rangeType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -295,7 +295,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(r), remains, nil }, nil case pt == identifierType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -310,7 +310,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil case pt == regexType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -323,7 +323,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil case pt.Kind() == reflect.String: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -339,7 +339,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } }, nil case pt.Kind() == reflect.Int64: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -353,7 +353,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } }, nil case pt.Kind() == reflect.Bool: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -366,7 +366,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(b), args, nil }, nil case pt.Kind() == reflect.Slice: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { converter, err := e.buildConverter(pt.Elem()) if err != nil { return reflect.Value{}, nil, err @@ -384,7 +384,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil default: if pt.Kind() == reflect.Interface && pt.NumMethod() == 0 { - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -395,7 +395,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } } -func (e *Exported) rangeConverter(n *expect.Note, args []interface{}) (Range, []interface{}, error) { +func (e *Exported) rangeConverter(n *expect.Note, args []any) (Range, []any, error) { tokFile := e.ExpectFileSet.File(n.Pos) if len(args) < 1 { return Range{}, nil, fmt.Errorf("missing argument") diff --git a/go/packages/packagestest/expect_test.go b/go/packages/packagestest/expect_test.go index 46d96d61fb9..70ff6656012 100644 --- a/go/packages/packagestest/expect_test.go +++ b/go/packages/packagestest/expect_test.go @@ -19,7 +19,7 @@ func TestExpect(t *testing.T) { }}) defer exported.Cleanup() checkCount := 0 - if err := exported.Expect(map[string]interface{}{ + if err := exported.Expect(map[string]any{ "check": func(src, target token.Position) { checkCount++ }, diff --git a/go/packages/packagestest/export.go b/go/packages/packagestest/export.go index 47e6d11b94b..4ac4967b46b 100644 --- a/go/packages/packagestest/export.go +++ b/go/packages/packagestest/export.go @@ -101,7 +101,7 @@ type Module struct { // The keys are the file fragment that follows the module name, the value can // be a string or byte slice, in which case it is the contents of the // file, otherwise it must be a Writer function. - Files map[string]interface{} + Files map[string]any // Overlay is the set of source file overlays for the module. // The keys are the file fragment as in the Files configuration. @@ -483,7 +483,7 @@ func GroupFilesByModules(root string) ([]Module, error) { primarymod := &Module{ Name: root, - Files: make(map[string]interface{}), + Files: make(map[string]any), Overlay: make(map[string][]byte), } mods := map[string]*Module{ @@ -573,7 +573,7 @@ func GroupFilesByModules(root string) ([]Module, error) { } mods[path] = &Module{ Name: filepath.ToSlash(module), - Files: make(map[string]interface{}), + Files: make(map[string]any), Overlay: make(map[string][]byte), } currentModule = path @@ -591,8 +591,8 @@ func GroupFilesByModules(root string) ([]Module, error) { // This is to enable the common case in tests where you have a full copy of the // package in your testdata. // This will panic if there is any kind of error trying to walk the file tree. -func MustCopyFileTree(root string) map[string]interface{} { - result := map[string]interface{}{} +func MustCopyFileTree(root string) map[string]any { + result := map[string]any{} if err := filepath.Walk(filepath.FromSlash(root), func(path string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/go/packages/packagestest/export_test.go b/go/packages/packagestest/export_test.go index eb13f560916..e3e4658efb6 100644 --- a/go/packages/packagestest/export_test.go +++ b/go/packages/packagestest/export_test.go @@ -16,7 +16,7 @@ import ( var testdata = []packagestest.Module{{ Name: "golang.org/fake1", - Files: map[string]interface{}{ + Files: map[string]any{ "a.go": packagestest.Symlink("testdata/a.go"), // broken symlink "b.go": "invalid file contents", }, @@ -26,22 +26,22 @@ var testdata = []packagestest.Module{{ }, }, { Name: "golang.org/fake2", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake2", }, }, { Name: "golang.org/fake2/v2", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake2", }, }, { Name: "golang.org/fake3@v1.0.0", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake3", }, }, { Name: "golang.org/fake3@v1.1.0", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake3", }, }} @@ -97,13 +97,13 @@ func TestGroupFilesByModules(t *testing.T) { want: []packagestest.Module{ { Name: "testdata/groups/one", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/extra", - Files: map[string]interface{}{ + Files: map[string]any{ "help.go": true, }, }, @@ -114,7 +114,7 @@ func TestGroupFilesByModules(t *testing.T) { want: []packagestest.Module{ { Name: "testdata/groups/two", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, "expect/yo.go": true, "expect/yo_test.go": true, @@ -122,33 +122,33 @@ func TestGroupFilesByModules(t *testing.T) { }, { Name: "example.com/extra", - Files: map[string]interface{}{ + Files: map[string]any{ "yo.go": true, "geez/help.go": true, }, }, { Name: "example.com/extra/v2", - Files: map[string]interface{}{ + Files: map[string]any{ "me.go": true, "geez/help.go": true, }, }, { Name: "example.com/tempmod", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/what@v1.0.0", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/what@v1.1.0", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, diff --git a/go/ssa/const_test.go b/go/ssa/const_test.go index 6738f07b2ef..6097bd93757 100644 --- a/go/ssa/const_test.go +++ b/go/ssa/const_test.go @@ -39,9 +39,9 @@ func TestConstString(t *testing.T) { } for _, test := range []struct { - expr string // type expression - constant interface{} // constant value - want string // expected String() value + expr string // type expression + constant any // constant value + want string // expected String() value }{ {"int", int64(0), "0:int"}, {"int64", int64(0), "0:int64"}, diff --git a/go/ssa/interp/interp.go b/go/ssa/interp/interp.go index f80db0676c7..7bd06120f6c 100644 --- a/go/ssa/interp/interp.go +++ b/go/ssa/interp/interp.go @@ -109,7 +109,7 @@ type frame struct { defers *deferred result value panicking bool - panic interface{} + panic any phitemps []value // temporaries for parallel phi assignment } diff --git a/go/ssa/interp/map.go b/go/ssa/interp/map.go index f5d5f230b73..e96e44df2b9 100644 --- a/go/ssa/interp/map.go +++ b/go/ssa/interp/map.go @@ -17,7 +17,7 @@ import ( type hashable interface { hash(t types.Type) int - eq(t types.Type, x interface{}) bool + eq(t types.Type, x any) bool } type entry struct { diff --git a/go/ssa/interp/value.go b/go/ssa/interp/value.go index bd681cb6152..4d65aa6c83e 100644 --- a/go/ssa/interp/value.go +++ b/go/ssa/interp/value.go @@ -48,7 +48,7 @@ import ( "golang.org/x/tools/go/types/typeutil" ) -type value interface{} +type value any type tuple []value @@ -123,7 +123,7 @@ func usesBuiltinMap(t types.Type) bool { panic(fmt.Sprintf("invalid map key type: %T", t)) } -func (x array) eq(t types.Type, _y interface{}) bool { +func (x array) eq(t types.Type, _y any) bool { y := _y.(array) tElt := t.Underlying().(*types.Array).Elem() for i, xi := range x { @@ -143,7 +143,7 @@ func (x array) hash(t types.Type) int { return h } -func (x structure) eq(t types.Type, _y interface{}) bool { +func (x structure) eq(t types.Type, _y any) bool { y := _y.(structure) tStruct := t.Underlying().(*types.Struct) for i, n := 0, tStruct.NumFields(); i < n; i++ { @@ -175,7 +175,7 @@ func sameType(x, y types.Type) bool { return y != nil && types.Identical(x, y) } -func (x iface) eq(t types.Type, _y interface{}) bool { +func (x iface) eq(t types.Type, _y any) bool { y := _y.(iface) return sameType(x.t, y.t) && (x.t == nil || equals(x.t, x.v, y.v)) } @@ -188,7 +188,7 @@ func (x rtype) hash(_ types.Type) int { return hashType(x.t) } -func (x rtype) eq(_ types.Type, y interface{}) bool { +func (x rtype) eq(_ types.Type, y any) bool { return types.Identical(x.t, y.(rtype).t) } diff --git a/go/ssa/mode.go b/go/ssa/mode.go index 8381639a585..61c91452ce2 100644 --- a/go/ssa/mode.go +++ b/go/ssa/mode.go @@ -108,4 +108,4 @@ func (m *BuilderMode) Set(s string) error { } // Get returns m. -func (m BuilderMode) Get() interface{} { return m } +func (m BuilderMode) Get() any { return m } diff --git a/go/ssa/print.go b/go/ssa/print.go index 432c4b05b6d..8b92d08463a 100644 --- a/go/ssa/print.go +++ b/go/ssa/print.go @@ -387,7 +387,7 @@ func (s *MapUpdate) String() string { func (s *DebugRef) String() string { p := s.Parent().Prog.Fset.Position(s.Pos()) - var descr interface{} + var descr any if s.object != nil { descr = s.object // e.g. "var x int" } else { diff --git a/go/ssa/sanity.go b/go/ssa/sanity.go index e35e4d79357..97ef886e3cf 100644 --- a/go/ssa/sanity.go +++ b/go/ssa/sanity.go @@ -48,7 +48,7 @@ func mustSanityCheck(fn *Function, reporter io.Writer) { } } -func (s *sanity) diagnostic(prefix, format string, args ...interface{}) { +func (s *sanity) diagnostic(prefix, format string, args ...any) { fmt.Fprintf(s.reporter, "%s: function %s", prefix, s.fn) if s.block != nil { fmt.Fprintf(s.reporter, ", block %s", s.block) @@ -58,12 +58,12 @@ func (s *sanity) diagnostic(prefix, format string, args ...interface{}) { io.WriteString(s.reporter, "\n") } -func (s *sanity) errorf(format string, args ...interface{}) { +func (s *sanity) errorf(format string, args ...any) { s.insane = true s.diagnostic("Error", format, args...) } -func (s *sanity) warnf(format string, args ...interface{}) { +func (s *sanity) warnf(format string, args ...any) { s.diagnostic("Warning", format, args...) } diff --git a/go/ssa/ssautil/load_test.go b/go/ssa/ssautil/load_test.go index 10375a3227f..cf157fe4401 100644 --- a/go/ssa/ssautil/load_test.go +++ b/go/ssa/ssautil/load_test.go @@ -154,7 +154,7 @@ func TestIssue53604(t *testing.T) { e := packagestest.Export(t, packagestest.Modules, []packagestest.Module{ { Name: "golang.org/fake", - Files: map[string]interface{}{ + Files: map[string]any{ "x/x.go": `package x; import "golang.org/fake/y"; var V = y.F()`, "y/y.go": `package y; import "golang.org/fake/z"; var F = func () *int { return &z.Z } `, "z/z.go": `package z; var Z int`, diff --git a/go/ssa/util.go b/go/ssa/util.go index 2a9c9b9d318..9a73984a6a0 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -166,7 +166,7 @@ func declaredWithin(obj types.Object, fn *types.Func) bool { // returns a closure that prints the corresponding "end" message. // Call using 'defer logStack(...)()' to show builder stack on panic. // Don't forget trailing parens! -func logStack(format string, args ...interface{}) func() { +func logStack(format string, args ...any) func() { msg := fmt.Sprintf(format, args...) io.WriteString(os.Stderr, msg) io.WriteString(os.Stderr, "\n") diff --git a/gopls/internal/analysis/deprecated/deprecated.go b/gopls/internal/analysis/deprecated/deprecated.go index c6df00b4f50..400041ba088 100644 --- a/gopls/internal/analysis/deprecated/deprecated.go +++ b/gopls/internal/analysis/deprecated/deprecated.go @@ -36,7 +36,7 @@ var Analyzer = &analysis.Analyzer{ } // checkDeprecated is a simplified copy of staticcheck.CheckDeprecated. -func checkDeprecated(pass *analysis.Pass) (interface{}, error) { +func checkDeprecated(pass *analysis.Pass) (any, error) { inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) deprs, err := collectDeprecatedNames(pass, inspector) diff --git a/gopls/internal/analysis/embeddirective/embeddirective.go b/gopls/internal/analysis/embeddirective/embeddirective.go index e623587cc68..7590cba9ad8 100644 --- a/gopls/internal/analysis/embeddirective/embeddirective.go +++ b/gopls/internal/analysis/embeddirective/embeddirective.go @@ -28,7 +28,7 @@ var Analyzer = &analysis.Analyzer{ const FixCategory = "addembedimport" // recognized by gopls ApplyFix -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { for _, f := range pass.Files { comments := embedDirectiveComments(f) if len(comments) == 0 { diff --git a/gopls/internal/analysis/fillreturns/fillreturns.go b/gopls/internal/analysis/fillreturns/fillreturns.go index b6bcc1f24dc..184aac5ea1f 100644 --- a/gopls/internal/analysis/fillreturns/fillreturns.go +++ b/gopls/internal/analysis/fillreturns/fillreturns.go @@ -36,7 +36,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/fillreturns", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) info := pass.TypesInfo diff --git a/gopls/internal/analysis/nonewvars/nonewvars.go b/gopls/internal/analysis/nonewvars/nonewvars.go index 8a3bf502c51..b7f861ba7f1 100644 --- a/gopls/internal/analysis/nonewvars/nonewvars.go +++ b/gopls/internal/analysis/nonewvars/nonewvars.go @@ -32,7 +32,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/nonewvars", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for _, typeErr := range pass.TypeErrors { diff --git a/gopls/internal/analysis/noresultvalues/noresultvalues.go b/gopls/internal/analysis/noresultvalues/noresultvalues.go index fe979f52aac..6b8f9d895e4 100644 --- a/gopls/internal/analysis/noresultvalues/noresultvalues.go +++ b/gopls/internal/analysis/noresultvalues/noresultvalues.go @@ -32,7 +32,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/noresultvalues", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for _, typErr := range pass.TypeErrors { diff --git a/gopls/internal/analysis/simplifycompositelit/simplifycompositelit.go b/gopls/internal/analysis/simplifycompositelit/simplifycompositelit.go index 15176cef1c8..b38ccf4d5ed 100644 --- a/gopls/internal/analysis/simplifycompositelit/simplifycompositelit.go +++ b/gopls/internal/analysis/simplifycompositelit/simplifycompositelit.go @@ -33,7 +33,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifycompositelit", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // Gather information whether file is generated or not generated := make(map[*token.File]bool) for _, file := range pass.Files { diff --git a/gopls/internal/analysis/simplifyrange/simplifyrange.go b/gopls/internal/analysis/simplifyrange/simplifyrange.go index fd685ba2c5b..594ebd1f55a 100644 --- a/gopls/internal/analysis/simplifyrange/simplifyrange.go +++ b/gopls/internal/analysis/simplifyrange/simplifyrange.go @@ -26,7 +26,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifyrange", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // Gather information whether file is generated or not generated := make(map[*token.File]bool) for _, file := range pass.Files { diff --git a/gopls/internal/analysis/simplifyslice/simplifyslice.go b/gopls/internal/analysis/simplifyslice/simplifyslice.go index 6755187afe5..28cc266d713 100644 --- a/gopls/internal/analysis/simplifyslice/simplifyslice.go +++ b/gopls/internal/analysis/simplifyslice/simplifyslice.go @@ -37,7 +37,7 @@ var Analyzer = &analysis.Analyzer{ // An example where it does not: // x, y := b[:n], b[n:] -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { // Gather information whether file is generated or not generated := make(map[*token.File]bool) for _, file := range pass.Files { diff --git a/gopls/internal/analysis/yield/yield.go b/gopls/internal/analysis/yield/yield.go index ccd30045f97..354cf372186 100644 --- a/gopls/internal/analysis/yield/yield.go +++ b/gopls/internal/analysis/yield/yield.go @@ -44,7 +44,7 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/yield", } -func run(pass *analysis.Pass) (interface{}, error) { +func run(pass *analysis.Pass) (any, error) { inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) // Find all calls to yield of the right type. diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go index a0dd322a51e..4083f49d2d6 100644 --- a/gopls/internal/cache/analysis.go +++ b/gopls/internal/cache/analysis.go @@ -885,7 +885,7 @@ type action struct { vdeps map[PackageID]*analysisNode // vertical dependencies // results of action.exec(): - result interface{} // result of Run function, of type a.ResultType + result any // result of Run function, of type a.ResultType summary *actionSummary err error } @@ -964,7 +964,7 @@ func (act *action) exec(ctx context.Context) (any, *actionSummary, error) { } // Gather analysis Result values from horizontal dependencies. - inputs := make(map[*analysis.Analyzer]interface{}) + inputs := make(map[*analysis.Analyzer]any) for _, dep := range act.hdeps { inputs[dep.a] = dep.result } @@ -1178,7 +1178,7 @@ func (act *action) exec(ctx context.Context) (any, *actionSummary, error) { // Recover from panics (only) within the analyzer logic. // (Use an anonymous function to limit the recover scope.) - var result interface{} + var result any func() { start := time.Now() defer func() { diff --git a/gopls/internal/cache/load.go b/gopls/internal/cache/load.go index 140cbc45490..e15e0cef0b6 100644 --- a/gopls/internal/cache/load.go +++ b/gopls/internal/cache/load.go @@ -365,7 +365,7 @@ func (s *Snapshot) config(ctx context.Context, allowNetwork AllowNetwork) *packa packages.NeedForTest, Fset: nil, // we do our own parsing Overlay: s.buildOverlays(), - Logf: func(format string, args ...interface{}) { + Logf: func(format string, args ...any) { if s.view.folder.Options.VerboseOutput { event.Log(ctx, fmt.Sprintf(format, args...)) } diff --git a/gopls/internal/cache/mod.go b/gopls/internal/cache/mod.go index f16cfbfe1af..f6dd22754cc 100644 --- a/gopls/internal/cache/mod.go +++ b/gopls/internal/cache/mod.go @@ -45,14 +45,14 @@ func (s *Snapshot) ParseMod(ctx context.Context, fh file.Handle) (*ParsedModule, // cache miss? if !hit { - promise, release := s.store.Promise(parseModKey(fh.Identity()), func(ctx context.Context, _ interface{}) interface{} { + promise, release := s.store.Promise(parseModKey(fh.Identity()), func(ctx context.Context, _ any) any { parsed, err := parseModImpl(ctx, fh) return parseModResult{parsed, err} }) entry = promise s.mu.Lock() - s.parseModHandles.Set(uri, entry, func(_, _ interface{}) { release() }) + s.parseModHandles.Set(uri, entry, func(_, _ any) { release() }) s.mu.Unlock() } @@ -131,14 +131,14 @@ func (s *Snapshot) ParseWork(ctx context.Context, fh file.Handle) (*ParsedWorkFi // cache miss? if !hit { - handle, release := s.store.Promise(parseWorkKey(fh.Identity()), func(ctx context.Context, _ interface{}) interface{} { + handle, release := s.store.Promise(parseWorkKey(fh.Identity()), func(ctx context.Context, _ any) any { parsed, err := parseWorkImpl(ctx, fh) return parseWorkResult{parsed, err} }) entry = handle s.mu.Lock() - s.parseWorkHandles.Set(uri, entry, func(_, _ interface{}) { release() }) + s.parseWorkHandles.Set(uri, entry, func(_, _ any) { release() }) s.mu.Unlock() } @@ -212,7 +212,7 @@ func (s *Snapshot) ModWhy(ctx context.Context, fh file.Handle) (map[string]strin // cache miss? if !hit { - handle := memoize.NewPromise("modWhy", func(ctx context.Context, arg interface{}) interface{} { + handle := memoize.NewPromise("modWhy", func(ctx context.Context, arg any) any { why, err := modWhyImpl(ctx, arg.(*Snapshot), fh) return modWhyResult{why, err} }) diff --git a/gopls/internal/cache/mod_tidy.go b/gopls/internal/cache/mod_tidy.go index 4d473d39b12..6d9a3e56b81 100644 --- a/gopls/internal/cache/mod_tidy.go +++ b/gopls/internal/cache/mod_tidy.go @@ -76,7 +76,7 @@ func (s *Snapshot) ModTidy(ctx context.Context, pm *ParsedModule) (*TidiedModule return nil, err } - handle := memoize.NewPromise("modTidy", func(ctx context.Context, arg interface{}) interface{} { + handle := memoize.NewPromise("modTidy", func(ctx context.Context, arg any) any { tidied, err := modTidyImpl(ctx, arg.(*Snapshot), pm) return modTidyResult{tidied, err} }) diff --git a/gopls/internal/cache/mod_vuln.go b/gopls/internal/cache/mod_vuln.go index a92f5b5abe1..a48b18e4ba4 100644 --- a/gopls/internal/cache/mod_vuln.go +++ b/gopls/internal/cache/mod_vuln.go @@ -40,7 +40,7 @@ func (s *Snapshot) ModVuln(ctx context.Context, modURI protocol.DocumentURI) (*v // Cache miss? if !hit { - handle := memoize.NewPromise("modVuln", func(ctx context.Context, arg interface{}) interface{} { + handle := memoize.NewPromise("modVuln", func(ctx context.Context, arg any) any { result, err := modVulnImpl(ctx, arg.(*Snapshot)) return modVuln{result, err} }) diff --git a/gopls/internal/cache/parse_cache.go b/gopls/internal/cache/parse_cache.go index 8586f655d28..015510b881d 100644 --- a/gopls/internal/cache/parse_cache.go +++ b/gopls/internal/cache/parse_cache.go @@ -195,7 +195,7 @@ func (c *parseCache) startParse(mode parser.Mode, purgeFuncBodies bool, fhs ...f } uri := fh.URI() - promise := memoize.NewPromise("parseCache.parse", func(ctx context.Context, _ interface{}) interface{} { + promise := memoize.NewPromise("parseCache.parse", func(ctx context.Context, _ any) any { // Allocate 2*len(content)+parsePadding to allow for re-parsing once // inside of parseGoSrc without exceeding the allocated space. base, nextBase := c.allocateSpace(2*len(content) + parsePadding) @@ -404,13 +404,13 @@ func (q queue) Swap(i, j int) { q[j].lruIndex = j } -func (q *queue) Push(x interface{}) { +func (q *queue) Push(x any) { e := x.(*parseCacheEntry) e.lruIndex = len(*q) *q = append(*q, e) } -func (q *queue) Pop() interface{} { +func (q *queue) Pop() any { last := len(*q) - 1 e := (*q)[last] (*q)[last] = nil // aid GC diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index f7ba04df6a4..2a161ad0fc8 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -369,7 +369,7 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti } params.Capabilities.Window.WorkDoneProgress = true - params.InitializationOptions = map[string]interface{}{ + params.InitializationOptions = map[string]any{ "symbolMatcher": string(opts.SymbolMatcher), } if c.initializeResult, err = c.Initialize(ctx, params); err != nil { @@ -468,7 +468,7 @@ func (c *cmdClient) LogMessage(ctx context.Context, p *protocol.LogMessageParams return nil } -func (c *cmdClient) Event(ctx context.Context, t *interface{}) error { return nil } +func (c *cmdClient) Event(ctx context.Context, t *any) error { return nil } func (c *cmdClient) RegisterCapability(ctx context.Context, p *protocol.RegistrationParams) error { return nil @@ -482,13 +482,13 @@ func (c *cmdClient) WorkspaceFolders(ctx context.Context) ([]protocol.WorkspaceF return nil, nil } -func (c *cmdClient) Configuration(ctx context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) { - results := make([]interface{}, len(p.Items)) +func (c *cmdClient) Configuration(ctx context.Context, p *protocol.ParamConfiguration) ([]any, error) { + results := make([]any, len(p.Items)) for i, item := range p.Items { if item.Section != "gopls" { continue } - m := map[string]interface{}{ + m := map[string]any{ "analyses": map[string]any{ "fillreturns": true, "nonewvars": true, @@ -658,7 +658,7 @@ func (c *cmdClient) PublishDiagnostics(ctx context.Context, p *protocol.PublishD // TODO(golang/go#60122): replace the gopls.diagnose_files // command with support for textDocument/diagnostic, // so that we don't need to do this de-duplication. - type key [6]interface{} + type key [6]any seen := make(map[key]bool) out := file.diagnostics[:0] for _, d := range file.diagnostics { diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index 42812a870a4..986453253f8 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -930,7 +930,7 @@ package foo res3 := goplsWithEnv(t, tree, []string{"GOPACKAGESDRIVER=off"}, "stats", "-anon") res3.checkExit(true) - var statsAsMap3 map[string]interface{} + var statsAsMap3 map[string]any if err := json.Unmarshal([]byte(res3.stdout), &statsAsMap3); err != nil { t.Fatalf("failed to unmarshal JSON output of stats command: %v", err) } @@ -1212,7 +1212,7 @@ func (res *result) checkOutput(pattern, name, content string) { } // toJSON decodes res.stdout as JSON into to *ptr and reports its success. -func (res *result) toJSON(ptr interface{}) bool { +func (res *result) toJSON(ptr any) bool { if err := json.Unmarshal([]byte(res.stdout), ptr); err != nil { res.t.Errorf("invalid JSON %v", err) return false diff --git a/gopls/internal/cmd/stats.go b/gopls/internal/cmd/stats.go index cc19a94fb84..1ba43ccee83 100644 --- a/gopls/internal/cmd/stats.go +++ b/gopls/internal/cmd/stats.go @@ -164,7 +164,7 @@ func (s *stats) Run(ctx context.Context, args ...string) error { } // Filter JSON output to fields that are consistent with s.Anon. - okFields := make(map[string]interface{}) + okFields := make(map[string]any) { v := reflect.ValueOf(stats) t := v.Type() diff --git a/gopls/internal/cmd/symbols.go b/gopls/internal/cmd/symbols.go index 663a08f4be1..15c593b0e74 100644 --- a/gopls/internal/cmd/symbols.go +++ b/gopls/internal/cmd/symbols.go @@ -53,7 +53,7 @@ func (r *symbols) Run(ctx context.Context, args ...string) error { return err } for _, s := range symbols { - if m, ok := s.(map[string]interface{}); ok { + if m, ok := s.(map[string]any); ok { s, err = mapToSymbol(m) if err != nil { return err @@ -69,7 +69,7 @@ func (r *symbols) Run(ctx context.Context, args ...string) error { return nil } -func mapToSymbol(m map[string]interface{}) (interface{}, error) { +func mapToSymbol(m map[string]any) (any, error) { b, err := json.Marshal(m) if err != nil { return nil, err diff --git a/gopls/internal/debug/log/log.go b/gopls/internal/debug/log/log.go index d015f9bfdd3..9e7efa7bf17 100644 --- a/gopls/internal/debug/log/log.go +++ b/gopls/internal/debug/log/log.go @@ -33,7 +33,7 @@ func (l Level) Log(ctx context.Context, msg string) { } // Logf formats and exports a log event labeled with level l. -func (l Level) Logf(ctx context.Context, format string, args ...interface{}) { +func (l Level) Logf(ctx context.Context, format string, args ...any) { l.Log(ctx, fmt.Sprintf(format, args...)) } diff --git a/gopls/internal/debug/rpc.go b/gopls/internal/debug/rpc.go index 8a696f848d0..5b8e1dbbbd0 100644 --- a/gopls/internal/debug/rpc.go +++ b/gopls/internal/debug/rpc.go @@ -209,7 +209,7 @@ func getStatusCode(span *export.Span) string { return "" } -func (r *Rpcs) getData(req *http.Request) interface{} { +func (r *Rpcs) getData(req *http.Request) any { return r } diff --git a/gopls/internal/debug/serve.go b/gopls/internal/debug/serve.go index 058254b755b..c471f488cd1 100644 --- a/gopls/internal/debug/serve.go +++ b/gopls/internal/debug/serve.go @@ -280,23 +280,23 @@ func cmdline(w http.ResponseWriter, r *http.Request) { pprof.Cmdline(fake, r) } -func (i *Instance) getCache(r *http.Request) interface{} { +func (i *Instance) getCache(r *http.Request) any { return i.State.Cache(path.Base(r.URL.Path)) } -func (i *Instance) getAnalysis(r *http.Request) interface{} { +func (i *Instance) getAnalysis(r *http.Request) any { return i.State.Analysis() } -func (i *Instance) getSession(r *http.Request) interface{} { +func (i *Instance) getSession(r *http.Request) any { return i.State.Session(path.Base(r.URL.Path)) } -func (i *Instance) getClient(r *http.Request) interface{} { +func (i *Instance) getClient(r *http.Request) any { return i.State.Client(path.Base(r.URL.Path)) } -func (i *Instance) getServer(r *http.Request) interface{} { +func (i *Instance) getServer(r *http.Request) any { i.State.mu.Lock() defer i.State.mu.Unlock() id := path.Base(r.URL.Path) @@ -308,7 +308,7 @@ func (i *Instance) getServer(r *http.Request) interface{} { return nil } -func (i *Instance) getFile(r *http.Request) interface{} { +func (i *Instance) getFile(r *http.Request) any { identifier := path.Base(r.URL.Path) sid := path.Base(path.Dir(r.URL.Path)) s := i.State.Session(sid) @@ -324,7 +324,7 @@ func (i *Instance) getFile(r *http.Request) interface{} { return nil } -func (i *Instance) getInfo(r *http.Request) interface{} { +func (i *Instance) getInfo(r *http.Request) any { buf := &bytes.Buffer{} i.PrintServerInfo(r.Context(), buf) return template.HTML(buf.String()) @@ -340,7 +340,7 @@ func (i *Instance) AddService(s protocol.Server, session *cache.Session) { stdlog.Printf("unable to find a Client to add the protocol.Server to") } -func getMemory(_ *http.Request) interface{} { +func getMemory(_ *http.Request) any { var m runtime.MemStats runtime.ReadMemStats(&m) return m @@ -439,7 +439,7 @@ func (i *Instance) Serve(ctx context.Context, addr string) (string, error) { event.Log(ctx, "Debug serving", label1.Port.Of(port)) go func() { mux := http.NewServeMux() - mux.HandleFunc("/", render(MainTmpl, func(*http.Request) interface{} { return i })) + mux.HandleFunc("/", render(MainTmpl, func(*http.Request) any { return i })) mux.HandleFunc("/debug/", render(DebugTmpl, nil)) mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", cmdline) @@ -594,11 +594,11 @@ func makeInstanceExporter(i *Instance) event.Exporter { return exporter } -type dataFunc func(*http.Request) interface{} +type dataFunc func(*http.Request) any func render(tmpl *template.Template, fun dataFunc) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - var data interface{} + var data any if fun != nil { data = fun(r) } diff --git a/gopls/internal/debug/template_test.go b/gopls/internal/debug/template_test.go index d4d9071c140..52c60244776 100644 --- a/gopls/internal/debug/template_test.go +++ b/gopls/internal/debug/template_test.go @@ -29,7 +29,7 @@ import ( var templates = map[string]struct { tmpl *template.Template - data interface{} // a value of the needed type + data any // a value of the needed type }{ "MainTmpl": {debug.MainTmpl, &debug.Instance{}}, "DebugTmpl": {debug.DebugTmpl, nil}, diff --git a/gopls/internal/debug/trace.go b/gopls/internal/debug/trace.go index 9314a04d241..e6ff9697b67 100644 --- a/gopls/internal/debug/trace.go +++ b/gopls/internal/debug/trace.go @@ -277,7 +277,7 @@ func (t *traces) addRecentLocked(span *traceSpan, start bool) { } // getData returns the TraceResults rendered by TraceTmpl for the /trace[/name] endpoint. -func (t *traces) getData(req *http.Request) interface{} { +func (t *traces) getData(req *http.Request) any { // TODO(adonovan): the HTTP request doesn't acquire the mutex // for t or for each span! Audit and fix. diff --git a/gopls/internal/golang/rename_check.go b/gopls/internal/golang/rename_check.go index 280795abe5e..97423fe87a7 100644 --- a/gopls/internal/golang/rename_check.go +++ b/gopls/internal/golang/rename_check.go @@ -51,7 +51,7 @@ import ( ) // errorf reports an error (e.g. conflict) and prevents file modification. -func (r *renamer) errorf(pos token.Pos, format string, args ...interface{}) { +func (r *renamer) errorf(pos token.Pos, format string, args ...any) { // Conflict error messages in the old gorename tool (whence this // logic originated) contain rich information associated with // multiple source lines, such as: diff --git a/gopls/internal/lsprpc/binder_test.go b/gopls/internal/lsprpc/binder_test.go index 042056e7777..07a8b2cdf99 100644 --- a/gopls/internal/lsprpc/binder_test.go +++ b/gopls/internal/lsprpc/binder_test.go @@ -56,7 +56,7 @@ func (b *ServerBinder) Bind(ctx context.Context, conn *jsonrpc2_v2.Connection) j serverHandler := protocol.ServerHandlerV2(server) // Wrap the server handler to inject the client into each request context, so // that log events are reflected back to the client. - wrapped := jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + wrapped := jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { ctx = protocol.WithClient(ctx, client) return serverHandler.Handle(ctx, req) }) diff --git a/gopls/internal/lsprpc/commandinterceptor_test.go b/gopls/internal/lsprpc/commandinterceptor_test.go index 7c83ef993f0..3cfa2e35a7f 100644 --- a/gopls/internal/lsprpc/commandinterceptor_test.go +++ b/gopls/internal/lsprpc/commandinterceptor_test.go @@ -15,9 +15,9 @@ import ( . "golang.org/x/tools/gopls/internal/lsprpc" ) -func CommandInterceptor(command string, run func(*protocol.ExecuteCommandParams) (interface{}, error)) Middleware { +func CommandInterceptor(command string, run func(*protocol.ExecuteCommandParams) (any, error)) Middleware { return BindHandler(func(delegate jsonrpc2_v2.Handler) jsonrpc2_v2.Handler { - return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if req.Method == "workspace/executeCommand" { var params protocol.ExecuteCommandParams if err := json.Unmarshal(req.Params, ¶ms); err == nil { @@ -35,9 +35,9 @@ func CommandInterceptor(command string, run func(*protocol.ExecuteCommandParams) func TestCommandInterceptor(t *testing.T) { const command = "foo" caught := false - intercept := func(_ *protocol.ExecuteCommandParams) (interface{}, error) { + intercept := func(_ *protocol.ExecuteCommandParams) (any, error) { caught = true - return map[string]interface{}{}, nil + return map[string]any{}, nil } ctx := context.Background() @@ -50,7 +50,7 @@ func TestCommandInterceptor(t *testing.T) { params := &protocol.ExecuteCommandParams{ Command: command, } - var res interface{} + var res any err := conn.Call(ctx, "workspace/executeCommand", params).Await(ctx, &res) if err != nil { t.Fatal(err) diff --git a/gopls/internal/lsprpc/export_test.go b/gopls/internal/lsprpc/export_test.go index 509129870dc..8cbdecc98a2 100644 --- a/gopls/internal/lsprpc/export_test.go +++ b/gopls/internal/lsprpc/export_test.go @@ -26,7 +26,7 @@ type Canceler struct { Conn *jsonrpc2_v2.Connection } -func (c *Canceler) Preempt(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { +func (c *Canceler) Preempt(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if req.Method != "$/cancelRequest" { return nil, jsonrpc2_v2.ErrNotHandled } @@ -65,7 +65,7 @@ func (b *ForwardBinder) Bind(ctx context.Context, conn *jsonrpc2_v2.Connection) serverConn, err := jsonrpc2_v2.Dial(context.Background(), b.dialer, clientBinder) if err != nil { return jsonrpc2_v2.ConnectionOptions{ - Handler: jsonrpc2_v2.HandlerFunc(func(context.Context, *jsonrpc2_v2.Request) (interface{}, error) { + Handler: jsonrpc2_v2.HandlerFunc(func(context.Context, *jsonrpc2_v2.Request) (any, error) { return nil, fmt.Errorf("%w: %v", jsonrpc2_v2.ErrInternal, err) }), } diff --git a/gopls/internal/lsprpc/goenv.go b/gopls/internal/lsprpc/goenv.go index 52ec08ff7eb..2b8b94345ca 100644 --- a/gopls/internal/lsprpc/goenv.go +++ b/gopls/internal/lsprpc/goenv.go @@ -12,7 +12,7 @@ import ( "golang.org/x/tools/internal/gocommand" ) -func getGoEnv(ctx context.Context, env map[string]interface{}) (map[string]string, error) { +func getGoEnv(ctx context.Context, env map[string]any) (map[string]string, error) { var runEnv []string for k, v := range env { runEnv = append(runEnv, fmt.Sprintf("%s=%s", k, v)) diff --git a/gopls/internal/lsprpc/goenv_test.go b/gopls/internal/lsprpc/goenv_test.go index 6c41540fafb..bc39228c614 100644 --- a/gopls/internal/lsprpc/goenv_test.go +++ b/gopls/internal/lsprpc/goenv_test.go @@ -21,7 +21,7 @@ import ( func GoEnvMiddleware() (Middleware, error) { return BindHandler(func(delegate jsonrpc2_v2.Handler) jsonrpc2_v2.Handler { - return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if req.Method == "initialize" { if err := addGoEnvToInitializeRequestV2(ctx, req); err != nil { event.Error(ctx, "adding go env to initialize", err) @@ -39,20 +39,20 @@ func addGoEnvToInitializeRequestV2(ctx context.Context, req *jsonrpc2_v2.Request if err := json.Unmarshal(req.Params, ¶ms); err != nil { return err } - var opts map[string]interface{} + var opts map[string]any switch v := params.InitializationOptions.(type) { case nil: - opts = make(map[string]interface{}) - case map[string]interface{}: + opts = make(map[string]any) + case map[string]any: opts = v default: return fmt.Errorf("unexpected type for InitializationOptions: %T", v) } envOpt, ok := opts["env"] if !ok { - envOpt = make(map[string]interface{}) + envOpt = make(map[string]any) } - env, ok := envOpt.(map[string]interface{}) + env, ok := envOpt.(map[string]any) if !ok { return fmt.Errorf("env option is %T, expected a map", envOpt) } @@ -108,8 +108,8 @@ func TestGoEnvMiddleware(t *testing.T) { conn := env.dial(ctx, t, l.Dialer(), noopBinder, true) dispatch := protocol.ServerDispatcherV2(conn) initParams := &protocol.ParamInitialize{} - initParams.InitializationOptions = map[string]interface{}{ - "env": map[string]interface{}{ + initParams.InitializationOptions = map[string]any{ + "env": map[string]any{ "GONOPROXY": "example.com", }, } @@ -120,7 +120,7 @@ func TestGoEnvMiddleware(t *testing.T) { if server.params == nil { t.Fatalf("initialize params are unset") } - envOpts := server.params.InitializationOptions.(map[string]interface{})["env"].(map[string]interface{}) + envOpts := server.params.InitializationOptions.(map[string]any)["env"].(map[string]any) // Check for an arbitrary Go variable. It should be set. if _, ok := envOpts["GOPRIVATE"]; !ok { diff --git a/gopls/internal/lsprpc/lsprpc.go b/gopls/internal/lsprpc/lsprpc.go index b77557c9a4b..9255f9176bc 100644 --- a/gopls/internal/lsprpc/lsprpc.go +++ b/gopls/internal/lsprpc/lsprpc.go @@ -323,20 +323,20 @@ func addGoEnvToInitializeRequest(ctx context.Context, r jsonrpc2.Request) (jsonr if err := json.Unmarshal(r.Params(), ¶ms); err != nil { return nil, err } - var opts map[string]interface{} + var opts map[string]any switch v := params.InitializationOptions.(type) { case nil: - opts = make(map[string]interface{}) - case map[string]interface{}: + opts = make(map[string]any) + case map[string]any: opts = v default: return nil, fmt.Errorf("unexpected type for InitializationOptions: %T", v) } envOpt, ok := opts["env"] if !ok { - envOpt = make(map[string]interface{}) + envOpt = make(map[string]any) } - env, ok := envOpt.(map[string]interface{}) + env, ok := envOpt.(map[string]any) if !ok { return nil, fmt.Errorf(`env option is %T, expected a map`, envOpt) } @@ -368,7 +368,7 @@ func (f *forwarder) replyWithDebugAddress(outerCtx context.Context, r jsonrpc2.R event.Log(outerCtx, "no debug instance to start") return r } - return func(ctx context.Context, result interface{}, outerErr error) error { + return func(ctx context.Context, result any, outerErr error) error { if outerErr != nil { return r(ctx, result, outerErr) } diff --git a/gopls/internal/lsprpc/lsprpc_test.go b/gopls/internal/lsprpc/lsprpc_test.go index c4ccab71a3e..1a259bbd646 100644 --- a/gopls/internal/lsprpc/lsprpc_test.go +++ b/gopls/internal/lsprpc/lsprpc_test.go @@ -302,8 +302,8 @@ func TestEnvForwarding(t *testing.T) { conn.Go(ctx, jsonrpc2.MethodNotFound) dispatch := protocol.ServerDispatcher(conn) initParams := &protocol.ParamInitialize{} - initParams.InitializationOptions = map[string]interface{}{ - "env": map[string]interface{}{ + initParams.InitializationOptions = map[string]any{ + "env": map[string]any{ "GONOPROXY": "example.com", }, } @@ -314,7 +314,7 @@ func TestEnvForwarding(t *testing.T) { if server.params == nil { t.Fatalf("initialize params are unset") } - env := server.params.InitializationOptions.(map[string]interface{})["env"].(map[string]interface{}) + env := server.params.InitializationOptions.(map[string]any)["env"].(map[string]any) // Check for an arbitrary Go variable. It should be set. if _, ok := env["GOPRIVATE"]; !ok { diff --git a/gopls/internal/lsprpc/middleware_test.go b/gopls/internal/lsprpc/middleware_test.go index 526c7343b78..afa6ae78d2f 100644 --- a/gopls/internal/lsprpc/middleware_test.go +++ b/gopls/internal/lsprpc/middleware_test.go @@ -154,7 +154,7 @@ func (h *Handshaker) Middleware(inner jsonrpc2_v2.Binder) jsonrpc2_v2.Binder { // Wrap the delegated handler to accept the handshake. delegate := opts.Handler - opts.Handler = jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) { + opts.Handler = jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { if req.Method == HandshakeMethod { var peerInfo PeerInfo if err := json.Unmarshal(req.Params, &peerInfo); err != nil { diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 007b8d5218f..0142de532c3 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -46,7 +46,7 @@ import ( "golang.org/x/tools/internal/xcontext" ) -func (s *server) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) { +func (s *server) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (any, error) { ctx, done := event.Start(ctx, "lsp.Server.executeCommand") defer done() diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go index de6b764c79f..b7b69931103 100644 --- a/gopls/internal/server/general.go +++ b/gopls/internal/server/general.go @@ -104,7 +104,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ } s.pendingFolders = append(s.pendingFolders, folders...) - var codeActionProvider interface{} = true + var codeActionProvider any = true if ca := params.Capabilities.TextDocument.CodeAction; len(ca.CodeActionLiteralSupport.CodeActionKind.ValueSet) > 0 { // If the client has specified CodeActionLiteralSupport, // send the code actions we support. @@ -126,7 +126,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ } } - var renameOpts interface{} = true + var renameOpts any = true if r := params.Capabilities.TextDocument.Rename; r != nil && r.PrepareSupport { renameOpts = protocol.RenameOptions{ PrepareProvider: r.PrepareSupport, diff --git a/gopls/internal/server/unimplemented.go b/gopls/internal/server/unimplemented.go index 470a7cbb0ee..7375dc4bb1b 100644 --- a/gopls/internal/server/unimplemented.go +++ b/gopls/internal/server/unimplemented.go @@ -114,7 +114,7 @@ func (s *server) ResolveWorkspaceSymbol(context.Context, *protocol.WorkspaceSymb return nil, notImplemented("ResolveWorkspaceSymbol") } -func (s *server) SemanticTokensFullDelta(context.Context, *protocol.SemanticTokensDeltaParams) (interface{}, error) { +func (s *server) SemanticTokensFullDelta(context.Context, *protocol.SemanticTokensDeltaParams) (any, error) { return nil, notImplemented("SemanticTokensFullDelta") } diff --git a/gopls/internal/template/parse.go b/gopls/internal/template/parse.go index 448a5ab51e8..f1b26bbb14f 100644 --- a/gopls/internal/template/parse.go +++ b/gopls/internal/template/parse.go @@ -114,7 +114,7 @@ func parseBuffer(buf []byte) *Parsed { matches := parseErrR.FindStringSubmatch(err.Error()) if len(matches) == 2 { // suppress the error by giving it a function with the right name - funcs[matches[1]] = func() interface{} { return nil } + funcs[matches[1]] = func() any { return nil } t, err = template.New("").Funcs(funcs).Parse(string(ans.buf)) continue } diff --git a/gopls/internal/test/integration/bench/completion_test.go b/gopls/internal/test/integration/bench/completion_test.go index bbbba0e3fd1..d84512d1f8f 100644 --- a/gopls/internal/test/integration/bench/completion_test.go +++ b/gopls/internal/test/integration/bench/completion_test.go @@ -282,7 +282,7 @@ func runCompletion(b *testing.B, test completionTest, followingEdit, completeUni env := repo.newEnv(b, fake.EditorConfig{ Env: envvars, - Settings: map[string]interface{}{ + Settings: map[string]any{ "completeUnimported": completeUnimported, "completionBudget": budget, }, diff --git a/gopls/internal/test/integration/bench/didchange_test.go b/gopls/internal/test/integration/bench/didchange_test.go index 57ed01bbcd6..b1613bb1b03 100644 --- a/gopls/internal/test/integration/bench/didchange_test.go +++ b/gopls/internal/test/integration/bench/didchange_test.go @@ -118,7 +118,7 @@ func runChangeDiagnosticsBenchmark(b *testing.B, test changeTest, save bool, ope Env: map[string]string{ "GOPATH": sharedEnv.Sandbox.GOPATH(), }, - Settings: map[string]interface{}{ + Settings: map[string]any{ "diagnosticsDelay": "0s", }, } diff --git a/gopls/internal/test/integration/env.go b/gopls/internal/test/integration/env.go index 64344d0d146..c8a1b5043aa 100644 --- a/gopls/internal/test/integration/env.go +++ b/gopls/internal/test/integration/env.go @@ -282,7 +282,7 @@ func (a *Awaiter) onProgress(_ context.Context, m *protocol.ProgressParams) erro if !ok { panic(fmt.Sprintf("got progress report for unknown report %v: %v", m.Token, m)) } - v := m.Value.(map[string]interface{}) + v := m.Value.(map[string]any) switch kind := v["kind"]; kind { case "begin": work.title = v["title"].(string) diff --git a/gopls/internal/test/integration/env_test.go b/gopls/internal/test/integration/env_test.go index 32203f7cb83..1fa68676b5c 100644 --- a/gopls/internal/test/integration/env_test.go +++ b/gopls/internal/test/integration/env_test.go @@ -33,7 +33,7 @@ func TestProgressUpdating(t *testing.T) { } updates := []struct { token string - value interface{} + value any }{ {"foo", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "foo work"}}, {"bar", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "bar work"}}, diff --git a/gopls/internal/test/integration/expectation.go b/gopls/internal/test/integration/expectation.go index ad41423d098..fdfca90796e 100644 --- a/gopls/internal/test/integration/expectation.go +++ b/gopls/internal/test/integration/expectation.go @@ -677,7 +677,7 @@ func checkFileWatch(re string, onMatch, onNoMatch Verdict) func(State) Verdict { rec := regexp.MustCompile(re) return func(s State) Verdict { r := s.registeredCapabilities["workspace/didChangeWatchedFiles"] - watchers := jsonProperty(r.RegisterOptions, "watchers").([]interface{}) + watchers := jsonProperty(r.RegisterOptions, "watchers").([]any) for _, watcher := range watchers { pattern := jsonProperty(watcher, "globPattern").(string) if rec.MatchString(pattern) { @@ -699,11 +699,11 @@ func checkFileWatch(re string, onMatch, onNoMatch Verdict) func(State) Verdict { // } // // Then jsonProperty(obj, "foo", "bar") will be 3. -func jsonProperty(obj interface{}, path ...string) interface{} { +func jsonProperty(obj any, path ...string) any { if len(path) == 0 || obj == nil { return obj } - m := obj.(map[string]interface{}) + m := obj.(map[string]any) return jsonProperty(m[path[0]], path[1:]...) } diff --git a/gopls/internal/test/integration/fake/client.go b/gopls/internal/test/integration/fake/client.go index 93eeab4a8af..aee6c1cfc3e 100644 --- a/gopls/internal/test/integration/fake/client.go +++ b/gopls/internal/test/integration/fake/client.go @@ -103,7 +103,7 @@ func (c *Client) LogMessage(ctx context.Context, params *protocol.LogMessagePara return nil } -func (c *Client) Event(ctx context.Context, event *interface{}) error { +func (c *Client) Event(ctx context.Context, event *any) error { return nil } @@ -118,8 +118,8 @@ func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, return []protocol.WorkspaceFolder{}, nil } -func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) { - results := make([]interface{}, len(p.Items)) +func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]any, error) { + results := make([]any, len(p.Items)) for i, item := range p.Items { if item.ScopeURI != nil && *item.ScopeURI == "" { return nil, fmt.Errorf(`malformed ScopeURI ""`) diff --git a/gopls/internal/test/integration/fake/glob/glob.go b/gopls/internal/test/integration/fake/glob/glob.go index a540ebefac5..3bda93bee6d 100644 --- a/gopls/internal/test/integration/fake/glob/glob.go +++ b/gopls/internal/test/integration/fake/glob/glob.go @@ -217,7 +217,7 @@ func (g *Glob) Match(input string) bool { } func match(elems []element, input string) (ok bool) { - var elem interface{} + var elem any for len(elems) > 0 { elem, elems = elems[0], elems[1:] switch elem := elem.(type) { diff --git a/gopls/internal/test/integration/options.go b/gopls/internal/test/integration/options.go index 8090388e17d..11824aa7c16 100644 --- a/gopls/internal/test/integration/options.go +++ b/gopls/internal/test/integration/options.go @@ -25,7 +25,7 @@ type runConfig struct { func defaultConfig() runConfig { return runConfig{ editor: fake.EditorConfig{ - Settings: map[string]interface{}{ + Settings: map[string]any{ // Shorten the diagnostic delay to speed up test execution (else we'd add // the default delay to each assertion about diagnostics) "diagnosticsDelay": "10ms", @@ -109,11 +109,11 @@ func CapabilitiesJSON(capabilities []byte) RunOption { // // As a special case, the env setting must not be provided via Settings: use // EnvVars instead. -type Settings map[string]interface{} +type Settings map[string]any func (s Settings) set(opts *runConfig) { if opts.editor.Settings == nil { - opts.editor.Settings = make(map[string]interface{}) + opts.editor.Settings = make(map[string]any) } for k, v := range s { opts.editor.Settings[k] = v diff --git a/gopls/internal/util/bug/bug.go b/gopls/internal/util/bug/bug.go index dcd242d4856..265ec9dac10 100644 --- a/gopls/internal/util/bug/bug.go +++ b/gopls/internal/util/bug/bug.go @@ -50,13 +50,13 @@ type Bug struct { } // Reportf reports a formatted bug message. -func Reportf(format string, args ...interface{}) { +func Reportf(format string, args ...any) { report(fmt.Sprintf(format, args...)) } // Errorf calls fmt.Errorf for the given arguments, and reports the resulting // error message as a bug. -func Errorf(format string, args ...interface{}) error { +func Errorf(format string, args ...any) error { err := fmt.Errorf(format, args...) report(err.Error()) return err diff --git a/gopls/internal/vulncheck/vulntest/report.go b/gopls/internal/vulncheck/vulntest/report.go index 6aa87221866..3b1bfcc5c96 100644 --- a/gopls/internal/vulncheck/vulntest/report.go +++ b/gopls/internal/vulncheck/vulntest/report.go @@ -134,7 +134,7 @@ func (v Version) Canonical() string { // single-element mapping of type to URL. type Reference osv.Reference -func (r *Reference) MarshalYAML() (interface{}, error) { +func (r *Reference) MarshalYAML() (any, error) { return map[string]string{ strings.ToLower(string(r.Type)): r.URL, }, nil diff --git a/internal/event/export/id.go b/internal/event/export/id.go index bf9938b38c1..fb6026462c1 100644 --- a/internal/event/export/id.go +++ b/internal/event/export/id.go @@ -39,7 +39,7 @@ var ( func initGenerator() { var rngSeed int64 - for _, p := range []interface{}{ + for _, p := range []any{ &rngSeed, &traceIDAdd, &nextSpanID, &spanIDInc, } { binary.Read(crand.Reader, binary.LittleEndian, p) diff --git a/internal/event/export/metric/exporter.go b/internal/event/export/metric/exporter.go index 4cafaa52928..588b8a108c7 100644 --- a/internal/event/export/metric/exporter.go +++ b/internal/event/export/metric/exporter.go @@ -19,14 +19,14 @@ import ( var Entries = keys.New("metric_entries", "The set of metrics calculated for an event") type Config struct { - subscribers map[interface{}][]subscriber + subscribers map[any][]subscriber } type subscriber func(time.Time, label.Map, label.Label) Data func (e *Config) subscribe(key label.Key, s subscriber) { if e.subscribers == nil { - e.subscribers = make(map[interface{}][]subscriber) + e.subscribers = make(map[any][]subscriber) } e.subscribers[key] = append(e.subscribers[key], s) } diff --git a/internal/event/export/ocagent/ocagent.go b/internal/event/export/ocagent/ocagent.go index 722a7446939..d86c4aed0cf 100644 --- a/internal/event/export/ocagent/ocagent.go +++ b/internal/event/export/ocagent/ocagent.go @@ -167,7 +167,7 @@ func (cfg *Config) buildNode() *wire.Node { } } -func (e *Exporter) send(endpoint string, message interface{}) { +func (e *Exporter) send(endpoint string, message any) { blob, err := json.Marshal(message) if err != nil { errorInExport("ocagent failed to marshal message for %v: %v", endpoint, err) @@ -190,7 +190,7 @@ func (e *Exporter) send(endpoint string, message interface{}) { } } -func errorInExport(message string, args ...interface{}) { +func errorInExport(message string, args ...any) { // This function is useful when debugging the exporter, but in general we // want to just drop any export } diff --git a/internal/event/export/prometheus/prometheus.go b/internal/event/export/prometheus/prometheus.go index 0281f60a35f..82bb6c15dfc 100644 --- a/internal/event/export/prometheus/prometheus.go +++ b/internal/event/export/prometheus/prometheus.go @@ -66,7 +66,7 @@ func (e *Exporter) header(w http.ResponseWriter, name, description string, isGau fmt.Fprintf(w, "# TYPE %s %s\n", name, kind) } -func (e *Exporter) row(w http.ResponseWriter, name string, group []label.Label, extra string, value interface{}) { +func (e *Exporter) row(w http.ResponseWriter, name string, group []label.Label, extra string, value any) { fmt.Fprint(w, name) buf := &bytes.Buffer{} fmt.Fprint(buf, group) diff --git a/internal/event/keys/keys.go b/internal/event/keys/keys.go index a02206e3015..4cfa51b6123 100644 --- a/internal/event/keys/keys.go +++ b/internal/event/keys/keys.go @@ -32,7 +32,7 @@ func (k *Value) Format(w io.Writer, buf []byte, l label.Label) { } // Get can be used to get a label for the key from a label.Map. -func (k *Value) Get(lm label.Map) interface{} { +func (k *Value) Get(lm label.Map) any { if t := lm.Find(k); t.Valid() { return k.From(t) } @@ -40,10 +40,10 @@ func (k *Value) Get(lm label.Map) interface{} { } // From can be used to get a value from a Label. -func (k *Value) From(t label.Label) interface{} { return t.UnpackValue() } +func (k *Value) From(t label.Label) any { return t.UnpackValue() } // Of creates a new Label with this key and the supplied value. -func (k *Value) Of(value interface{}) label.Label { return label.OfValue(k, value) } +func (k *Value) Of(value any) label.Label { return label.OfValue(k, value) } // Tag represents a key for tagging labels that have no value. // These are used when the existence of the label is the entire information it diff --git a/internal/event/label/label.go b/internal/event/label/label.go index 0f526e1f9ab..7c00ca2a6da 100644 --- a/internal/event/label/label.go +++ b/internal/event/label/label.go @@ -32,7 +32,7 @@ type Key interface { type Label struct { key Key packed uint64 - untyped interface{} + untyped any } // Map is the interface to a collection of Labels indexed by key. @@ -76,13 +76,13 @@ type mapChain struct { // OfValue creates a new label from the key and value. // This method is for implementing new key types, label creation should // normally be done with the Of method of the key. -func OfValue(k Key, value interface{}) Label { return Label{key: k, untyped: value} } +func OfValue(k Key, value any) Label { return Label{key: k, untyped: value} } // UnpackValue assumes the label was built using LabelOfValue and returns the value // that was passed to that constructor. // This method is for implementing new key types, for type safety normal // access should be done with the From method of the key. -func (t Label) UnpackValue() interface{} { return t.untyped } +func (t Label) UnpackValue() any { return t.untyped } // Of64 creates a new label from a key and a uint64. This is often // used for non uint64 values that can be packed into a uint64. diff --git a/internal/expect/expect.go b/internal/expect/expect.go index d977ea4e262..69875cd6585 100644 --- a/internal/expect/expect.go +++ b/internal/expect/expect.go @@ -86,7 +86,7 @@ type ReadFile func(filename string) ([]byte, error) // MatchBefore returns the range of the line that matched the pattern, and // invalid positions if there was no match, or an error if the line could not be // found. -func MatchBefore(fset *token.FileSet, readFile ReadFile, end token.Pos, pattern interface{}) (token.Pos, token.Pos, error) { +func MatchBefore(fset *token.FileSet, readFile ReadFile, end token.Pos, pattern any) (token.Pos, token.Pos, error) { f := fset.File(end) content, err := readFile(f.Name()) if err != nil { diff --git a/internal/expect/expect_test.go b/internal/expect/expect_test.go index 3ad8d1a74fa..e8f8b6a7a07 100644 --- a/internal/expect/expect_test.go +++ b/internal/expect/expect_test.go @@ -155,7 +155,7 @@ func TestMarker(t *testing.T) { } } -func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern interface{}) { +func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern any) { start, end, err := expect.MatchBefore(fset, readFile, pos, pattern) if err != nil { t.Errorf("%v: MatchBefore failed: %v", fset.Position(pos), err) diff --git a/internal/expect/extract.go b/internal/expect/extract.go index 1fb4349c48e..150a2afbbf6 100644 --- a/internal/expect/extract.go +++ b/internal/expect/extract.go @@ -32,7 +32,7 @@ type Identifier string // See the package documentation for details about the syntax of those // notes. func Parse(fset *token.FileSet, filename string, content []byte) ([]*Note, error) { - var src interface{} + var src any if content != nil { src = content } @@ -220,7 +220,7 @@ func (t *tokens) Pos() token.Pos { return t.base + token.Pos(t.scanner.Position.Offset) } -func (t *tokens) Errorf(msg string, args ...interface{}) { +func (t *tokens) Errorf(msg string, args ...any) { if t.err != nil { return } diff --git a/internal/facts/facts.go b/internal/facts/facts.go index e1c18d373c3..8e2997e6def 100644 --- a/internal/facts/facts.go +++ b/internal/facts/facts.go @@ -209,7 +209,7 @@ func (d *Decoder) Decode(read func(pkgPath string) ([]byte, error)) (*Set, error // Facts may describe indirectly imported packages, or their objects. m := make(map[key]analysis.Fact) // one big bucket for _, imp := range d.pkg.Imports() { - logf := func(format string, args ...interface{}) { + logf := func(format string, args ...any) { if debug { prefix := fmt.Sprintf("in %s, importing %s: ", d.pkg.Path(), imp.Path()) diff --git a/internal/gcimporter/bimport.go b/internal/gcimporter/bimport.go index d79a605ed13..734c46198df 100644 --- a/internal/gcimporter/bimport.go +++ b/internal/gcimporter/bimport.go @@ -14,7 +14,7 @@ import ( "sync" ) -func errorf(format string, args ...interface{}) { +func errorf(format string, args ...any) { panic(fmt.Sprintf(format, args...)) } diff --git a/internal/gcimporter/iexport.go b/internal/gcimporter/iexport.go index 7dfc31a37d7..253d6493c21 100644 --- a/internal/gcimporter/iexport.go +++ b/internal/gcimporter/iexport.go @@ -310,7 +310,7 @@ func IImportShallow(fset *token.FileSet, getPackages GetPackagesFunc, data []byt } // ReportFunc is the type of a function used to report formatted bugs. -type ReportFunc = func(string, ...interface{}) +type ReportFunc = func(string, ...any) // Current bundled export format version. Increase with each format change. // 0: initial implementation @@ -597,7 +597,7 @@ type filePositions struct { needed []uint64 // unordered list of needed file offsets } -func (p *iexporter) trace(format string, args ...interface{}) { +func (p *iexporter) trace(format string, args ...any) { if !trace { // Call sites should also be guarded, but having this check here allows // easily enabling/disabling debug trace statements. @@ -1583,6 +1583,6 @@ func (e internalError) Error() string { return "gcimporter: " + string(e) } // "internalErrorf" as the former is used for bugs, whose cause is // internal inconsistency, whereas the latter is used for ordinary // situations like bad input, whose cause is external. -func internalErrorf(format string, args ...interface{}) error { +func internalErrorf(format string, args ...any) error { return internalError(fmt.Sprintf(format, args...)) } diff --git a/internal/gcimporter/iimport.go b/internal/gcimporter/iimport.go index 12943927159..bc6c9741e7d 100644 --- a/internal/gcimporter/iimport.go +++ b/internal/gcimporter/iimport.go @@ -400,7 +400,7 @@ type iimporter struct { indent int // for tracing support } -func (p *iimporter) trace(format string, args ...interface{}) { +func (p *iimporter) trace(format string, args ...any) { if !trace { // Call sites should also be guarded, but having this check here allows // easily enabling/disabling debug trace statements. diff --git a/internal/gopathwalk/walk.go b/internal/gopathwalk/walk.go index 8361515519f..984b79c2a07 100644 --- a/internal/gopathwalk/walk.go +++ b/internal/gopathwalk/walk.go @@ -22,7 +22,7 @@ import ( // Options controls the behavior of a Walk call. type Options struct { // If Logf is non-nil, debug logging is enabled through this function. - Logf func(format string, args ...interface{}) + Logf func(format string, args ...any) // Search module caches. Also disables legacy goimports ignore rules. ModulesEnabled bool @@ -81,7 +81,7 @@ func WalkSkip(roots []Root, add func(root Root, dir string), skip func(root Root // walkDir creates a walker and starts fastwalk with this walker. func walkDir(root Root, add func(Root, string), skip func(root Root, dir string) bool, opts Options) { if opts.Logf == nil { - opts.Logf = func(format string, args ...interface{}) {} + opts.Logf = func(format string, args ...any) {} } if _, err := os.Stat(root.Path); os.IsNotExist(err) { opts.Logf("skipping nonexistent directory: %v", root.Path) diff --git a/internal/imports/fix_test.go b/internal/imports/fix_test.go index 02ddd480dfd..478313aec7f 100644 --- a/internal/imports/fix_test.go +++ b/internal/imports/fix_test.go @@ -1680,7 +1680,7 @@ type testConfig struct { } // fm is the type for a packagestest.Module's Files, abbreviated for shorter lines. -type fm map[string]interface{} +type fm map[string]any func (c testConfig) test(t *testing.T, fn func(*goimportTest)) { t.Helper() diff --git a/internal/jsonrpc2/conn.go b/internal/jsonrpc2/conn.go index 1d76ef9726b..6e8625208d9 100644 --- a/internal/jsonrpc2/conn.go +++ b/internal/jsonrpc2/conn.go @@ -25,12 +25,12 @@ type Conn interface { // The response will be unmarshaled from JSON into the result. // The id returned will be unique from this connection, and can be used for // logging or tracking. - Call(ctx context.Context, method string, params, result interface{}) (ID, error) + Call(ctx context.Context, method string, params, result any) (ID, error) // Notify invokes the target method but does not wait for a response. // The params will be marshaled to JSON before sending over the wire, and will // be handed to the method invoked. - Notify(ctx context.Context, method string, params interface{}) error + Notify(ctx context.Context, method string, params any) error // Go starts a goroutine to handle the connection. // It must be called exactly once for each Conn. @@ -76,7 +76,7 @@ func NewConn(s Stream) Conn { return conn } -func (c *conn) Notify(ctx context.Context, method string, params interface{}) (err error) { +func (c *conn) Notify(ctx context.Context, method string, params any) (err error) { notify, err := NewNotification(method, params) if err != nil { return fmt.Errorf("marshaling notify parameters: %v", err) @@ -96,7 +96,7 @@ func (c *conn) Notify(ctx context.Context, method string, params interface{}) (e return err } -func (c *conn) Call(ctx context.Context, method string, params, result interface{}) (_ ID, err error) { +func (c *conn) Call(ctx context.Context, method string, params, result any) (_ ID, err error) { // generate a new request identifier id := ID{number: atomic.AddInt64(&c.seq, 1)} call, err := NewCall(id, method, params) @@ -153,7 +153,7 @@ func (c *conn) Call(ctx context.Context, method string, params, result interface } func (c *conn) replier(req Request, spanDone func()) Replier { - return func(ctx context.Context, result interface{}, err error) error { + return func(ctx context.Context, result any, err error) error { defer func() { recordStatus(ctx, err) spanDone() diff --git a/internal/jsonrpc2/handler.go b/internal/jsonrpc2/handler.go index 27cb108922a..317b94f8ac1 100644 --- a/internal/jsonrpc2/handler.go +++ b/internal/jsonrpc2/handler.go @@ -18,7 +18,7 @@ type Handler func(ctx context.Context, reply Replier, req Request) error // Replier is passed to handlers to allow them to reply to the request. // If err is set then result will be ignored. -type Replier func(ctx context.Context, result interface{}, err error) error +type Replier func(ctx context.Context, result any, err error) error // MethodNotFound is a Handler that replies to all call requests with the // standard method not found response. @@ -32,7 +32,7 @@ func MethodNotFound(ctx context.Context, reply Replier, req Request) error { func MustReplyHandler(handler Handler) Handler { return func(ctx context.Context, reply Replier, req Request) error { called := false - err := handler(ctx, func(ctx context.Context, result interface{}, err error) error { + err := handler(ctx, func(ctx context.Context, result any, err error) error { if called { panic(fmt.Errorf("request %q replied to more than once", req.Method())) } @@ -59,7 +59,7 @@ func CancelHandler(handler Handler) (Handler, func(id ID)) { handling[call.ID()] = cancel mu.Unlock() innerReply := reply - reply = func(ctx context.Context, result interface{}, err error) error { + reply = func(ctx context.Context, result any, err error) error { mu.Lock() delete(handling, call.ID()) mu.Unlock() @@ -92,7 +92,7 @@ func AsyncHandler(handler Handler) Handler { nextRequest = make(chan struct{}) releaser := &releaser{ch: nextRequest} innerReply := reply - reply = func(ctx context.Context, result interface{}, err error) error { + reply = func(ctx context.Context, result any, err error) error { releaser.release(true) return innerReply(ctx, result, err) } diff --git a/internal/jsonrpc2/jsonrpc2_test.go b/internal/jsonrpc2/jsonrpc2_test.go index f62977edfce..b7688bc2334 100644 --- a/internal/jsonrpc2/jsonrpc2_test.go +++ b/internal/jsonrpc2/jsonrpc2_test.go @@ -23,8 +23,8 @@ var logRPC = flag.Bool("logrpc", false, "Enable jsonrpc2 communication logging") type callTest struct { method string - params interface{} - expect interface{} + params any + expect any } var callTests = []callTest{ @@ -35,10 +35,10 @@ var callTests = []callTest{ //TODO: expand the test cases } -func (test *callTest) newResults() interface{} { +func (test *callTest) newResults() any { switch e := test.expect.(type) { - case []interface{}: - var r []interface{} + case []any: + var r []any for _, v := range e { r = append(r, reflect.New(reflect.TypeOf(v)).Interface()) } @@ -50,7 +50,7 @@ func (test *callTest) newResults() interface{} { } } -func (test *callTest) verifyResults(t *testing.T, results interface{}) { +func (test *callTest) verifyResults(t *testing.T, results any) { if results == nil { return } diff --git a/internal/jsonrpc2/messages.go b/internal/jsonrpc2/messages.go index e87d772f398..5078b88f4ae 100644 --- a/internal/jsonrpc2/messages.go +++ b/internal/jsonrpc2/messages.go @@ -65,7 +65,7 @@ type Response struct { // NewNotification constructs a new Notification message for the supplied // method and parameters. -func NewNotification(method string, params interface{}) (*Notification, error) { +func NewNotification(method string, params any) (*Notification, error) { p, merr := marshalToRaw(params) return &Notification{method: method, params: p}, merr } @@ -98,7 +98,7 @@ func (n *Notification) UnmarshalJSON(data []byte) error { // NewCall constructs a new Call message for the supplied ID, method and // parameters. -func NewCall(id ID, method string, params interface{}) (*Call, error) { +func NewCall(id ID, method string, params any) (*Call, error) { p, merr := marshalToRaw(params) return &Call{id: id, method: method, params: p}, merr } @@ -135,7 +135,7 @@ func (c *Call) UnmarshalJSON(data []byte) error { // NewResponse constructs a new Response message that is a reply to the // supplied. If err is set result may be ignored. -func NewResponse(id ID, result interface{}, err error) (*Response, error) { +func NewResponse(id ID, result any, err error) (*Response, error) { r, merr := marshalToRaw(result) return &Response{id: id, result: r, err: err}, merr } @@ -229,7 +229,7 @@ func DecodeMessage(data []byte) (Message, error) { return call, nil } -func marshalToRaw(obj interface{}) (json.RawMessage, error) { +func marshalToRaw(obj any) (json.RawMessage, error) { data, err := json.Marshal(obj) if err != nil { return json.RawMessage{}, err diff --git a/internal/jsonrpc2_v2/conn.go b/internal/jsonrpc2_v2/conn.go index df885bfa4c3..4c52a1fd34b 100644 --- a/internal/jsonrpc2_v2/conn.go +++ b/internal/jsonrpc2_v2/conn.go @@ -260,7 +260,7 @@ func newConnection(bindCtx context.Context, rwc io.ReadWriteCloser, binder Binde // Notify invokes the target method but does not wait for a response. // The params will be marshaled to JSON before sending over the wire, and will // be handed to the method invoked. -func (c *Connection) Notify(ctx context.Context, method string, params interface{}) (err error) { +func (c *Connection) Notify(ctx context.Context, method string, params any) (err error) { ctx, done := event.Start(ctx, method, jsonrpc2.Method.Of(method), jsonrpc2.RPCDirection.Of(jsonrpc2.Outbound), @@ -309,7 +309,7 @@ func (c *Connection) Notify(ctx context.Context, method string, params interface // be handed to the method invoked. // You do not have to wait for the response, it can just be ignored if not needed. // If sending the call failed, the response will be ready and have the error in it. -func (c *Connection) Call(ctx context.Context, method string, params interface{}) *AsyncCall { +func (c *Connection) Call(ctx context.Context, method string, params any) *AsyncCall { // Generate a new request identifier. id := Int64ID(atomic.AddInt64(&c.seq, 1)) ctx, endSpan := event.Start(ctx, method, @@ -410,7 +410,7 @@ func (ac *AsyncCall) retire(response *Response) { // Await waits for (and decodes) the results of a Call. // The response will be unmarshaled from JSON into the result. -func (ac *AsyncCall) Await(ctx context.Context, result interface{}) error { +func (ac *AsyncCall) Await(ctx context.Context, result any) error { select { case <-ctx.Done(): return ctx.Err() @@ -429,7 +429,7 @@ func (ac *AsyncCall) Await(ctx context.Context, result interface{}) error { // // Respond must be called exactly once for any message for which a handler // returns ErrAsyncResponse. It must not be called for any other message. -func (c *Connection) Respond(id ID, result interface{}, err error) error { +func (c *Connection) Respond(id ID, result any, err error) error { var req *incomingRequest c.updateInFlight(func(s *inFlightState) { req = s.incomingByID[id] @@ -678,7 +678,7 @@ func (c *Connection) handleAsync() { } // processResult processes the result of a request and, if appropriate, sends a response. -func (c *Connection) processResult(from interface{}, req *incomingRequest, result interface{}, err error) error { +func (c *Connection) processResult(from any, req *incomingRequest, result any, err error) error { switch err { case ErrAsyncResponse: if !req.IsCall() { @@ -781,7 +781,7 @@ func (c *Connection) write(ctx context.Context, msg Message) error { // internalErrorf reports an internal error. By default it panics, but if // c.onInternalError is non-nil it instead calls that and returns an error // wrapping ErrInternal. -func (c *Connection) internalErrorf(format string, args ...interface{}) error { +func (c *Connection) internalErrorf(format string, args ...any) error { err := fmt.Errorf(format, args...) if c.onInternalError == nil { panic("jsonrpc2: " + err.Error()) @@ -803,7 +803,7 @@ func labelStatus(ctx context.Context, err error) { // notDone is a context.Context wrapper that returns a nil Done channel. type notDone struct{ ctx context.Context } -func (ic notDone) Value(key interface{}) interface{} { +func (ic notDone) Value(key any) any { return ic.ctx.Value(key) } diff --git a/internal/jsonrpc2_v2/jsonrpc2.go b/internal/jsonrpc2_v2/jsonrpc2.go index 9d775de0603..270f4f341d8 100644 --- a/internal/jsonrpc2_v2/jsonrpc2.go +++ b/internal/jsonrpc2_v2/jsonrpc2.go @@ -44,13 +44,13 @@ type Preempter interface { // Otherwise, the result and error are processed as if returned by Handle. // // Preempt must not block. (The Context passed to it is for Values only.) - Preempt(ctx context.Context, req *Request) (result interface{}, err error) + Preempt(ctx context.Context, req *Request) (result any, err error) } // A PreempterFunc implements the Preempter interface for a standalone Preempt function. -type PreempterFunc func(ctx context.Context, req *Request) (interface{}, error) +type PreempterFunc func(ctx context.Context, req *Request) (any, error) -func (f PreempterFunc) Preempt(ctx context.Context, req *Request) (interface{}, error) { +func (f PreempterFunc) Preempt(ctx context.Context, req *Request) (any, error) { return f(ctx, req) } @@ -71,23 +71,23 @@ type Handler interface { // connection is broken or the request is canceled or completed. // (If Handle returns ErrAsyncResponse, ctx will remain uncanceled // until either Cancel or Respond is called for the request's ID.) - Handle(ctx context.Context, req *Request) (result interface{}, err error) + Handle(ctx context.Context, req *Request) (result any, err error) } type defaultHandler struct{} -func (defaultHandler) Preempt(context.Context, *Request) (interface{}, error) { +func (defaultHandler) Preempt(context.Context, *Request) (any, error) { return nil, ErrNotHandled } -func (defaultHandler) Handle(context.Context, *Request) (interface{}, error) { +func (defaultHandler) Handle(context.Context, *Request) (any, error) { return nil, ErrNotHandled } // A HandlerFunc implements the Handler interface for a standalone Handle function. -type HandlerFunc func(ctx context.Context, req *Request) (interface{}, error) +type HandlerFunc func(ctx context.Context, req *Request) (any, error) -func (f HandlerFunc) Handle(ctx context.Context, req *Request) (interface{}, error) { +func (f HandlerFunc) Handle(ctx context.Context, req *Request) (any, error) { return f(ctx, req) } diff --git a/internal/jsonrpc2_v2/jsonrpc2_test.go b/internal/jsonrpc2_v2/jsonrpc2_test.go index d75a20739e8..e42f63736c0 100644 --- a/internal/jsonrpc2_v2/jsonrpc2_test.go +++ b/internal/jsonrpc2_v2/jsonrpc2_test.go @@ -87,24 +87,24 @@ type invoker interface { type notify struct { method string - params interface{} + params any } type call struct { method string - params interface{} - expect interface{} + params any + expect any } type async struct { name string method string - params interface{} + params any } type collect struct { name string - expect interface{} + expect any fails bool } @@ -180,7 +180,7 @@ func (test call) Invoke(t *testing.T, ctx context.Context, h *handler) { func (test echo) Invoke(t *testing.T, ctx context.Context, h *handler) { results := newResults(test.expect) - if err := h.conn.Call(ctx, "echo", []interface{}{test.method, test.params}).Await(ctx, results); err != nil { + if err := h.conn.Call(ctx, "echo", []any{test.method, test.params}).Await(ctx, results); err != nil { t.Fatalf("%v:Echo failed: %v", test.method, err) } verifyResults(t, test.method, results, test.expect) @@ -221,10 +221,10 @@ func (test sequence) Invoke(t *testing.T, ctx context.Context, h *handler) { } // newResults makes a new empty copy of the expected type to put the results into -func newResults(expect interface{}) interface{} { +func newResults(expect any) any { switch e := expect.(type) { - case []interface{}: - var r []interface{} + case []any: + var r []any for _, v := range e { r = append(r, reflect.New(reflect.TypeOf(v)).Interface()) } @@ -237,7 +237,7 @@ func newResults(expect interface{}) interface{} { } // verifyResults compares the results to the expected values -func verifyResults(t *testing.T, method string, results interface{}, expect interface{}) { +func verifyResults(t *testing.T, method string, results any, expect any) { if expect == nil { if results != nil { t.Errorf("%v:Got results %+v where none expected", method, expect) @@ -278,7 +278,7 @@ func (h *handler) waiter(name string) chan struct{} { return waiter } -func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) { +func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (any, error) { switch req.Method { case "unblock": var name string @@ -304,7 +304,7 @@ func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface } } -func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) { +func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (any, error) { switch req.Method { case "no_args": if len(req.Params) > 0 { @@ -349,11 +349,11 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{ } return path.Join(v...), nil case "echo": - var v []interface{} + var v []any if err := json.Unmarshal(req.Params, &v); err != nil { return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err) } - var result interface{} + var result any err := h.conn.Call(ctx, v[0].(string), v[1]).Await(ctx, &result) return result, err case "wait": diff --git a/internal/jsonrpc2_v2/messages.go b/internal/jsonrpc2_v2/messages.go index f02b879c3f2..9cfe6e70fe5 100644 --- a/internal/jsonrpc2_v2/messages.go +++ b/internal/jsonrpc2_v2/messages.go @@ -12,7 +12,7 @@ import ( // ID is a Request identifier. type ID struct { - value interface{} + value any } // Message is the interface to all jsonrpc2 message types. @@ -59,18 +59,18 @@ func Int64ID(i int64) ID { return ID{value: i} } func (id ID) IsValid() bool { return id.value != nil } // Raw returns the underlying value of the ID. -func (id ID) Raw() interface{} { return id.value } +func (id ID) Raw() any { return id.value } // NewNotification constructs a new Notification message for the supplied // method and parameters. -func NewNotification(method string, params interface{}) (*Request, error) { +func NewNotification(method string, params any) (*Request, error) { p, merr := marshalToRaw(params) return &Request{Method: method, Params: p}, merr } // NewCall constructs a new Call message for the supplied ID, method and // parameters. -func NewCall(id ID, method string, params interface{}) (*Request, error) { +func NewCall(id ID, method string, params any) (*Request, error) { p, merr := marshalToRaw(params) return &Request{ID: id, Method: method, Params: p}, merr } @@ -85,7 +85,7 @@ func (msg *Request) marshal(to *wireCombined) { // NewResponse constructs a new Response message that is a reply to the // supplied. If err is set result may be ignored. -func NewResponse(id ID, result interface{}, rerr error) (*Response, error) { +func NewResponse(id ID, result any, rerr error) (*Response, error) { r, merr := marshalToRaw(result) return &Response{ID: id, Result: r, Error: rerr}, merr } @@ -169,7 +169,7 @@ func DecodeMessage(data []byte) (Message, error) { return resp, nil } -func marshalToRaw(obj interface{}) (json.RawMessage, error) { +func marshalToRaw(obj any) (json.RawMessage, error) { if obj == nil { return nil, nil } diff --git a/internal/jsonrpc2_v2/serve_test.go b/internal/jsonrpc2_v2/serve_test.go index c5c41e201cd..8eb572c9d01 100644 --- a/internal/jsonrpc2_v2/serve_test.go +++ b/internal/jsonrpc2_v2/serve_test.go @@ -148,7 +148,7 @@ type msg struct { type fakeHandler struct{} -func (fakeHandler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) { +func (fakeHandler) Handle(ctx context.Context, req *jsonrpc2.Request) (any, error) { switch req.Method { case "ping": return &msg{"pong"}, nil @@ -296,7 +296,7 @@ func TestCloseCallRace(t *testing.T) { pokec := make(chan *jsonrpc2.AsyncCall, 1) s := jsonrpc2.NewServer(ctx, listener, jsonrpc2.BinderFunc(func(_ context.Context, srvConn *jsonrpc2.Connection) jsonrpc2.ConnectionOptions { - h := jsonrpc2.HandlerFunc(func(ctx context.Context, _ *jsonrpc2.Request) (interface{}, error) { + h := jsonrpc2.HandlerFunc(func(ctx context.Context, _ *jsonrpc2.Request) (any, error) { // Start a concurrent call from the server to the client. // The point of this test is to ensure this doesn't deadlock // if the client shuts down the connection concurrently. diff --git a/internal/jsonrpc2_v2/wire.go b/internal/jsonrpc2_v2/wire.go index 8f60fc62766..bc56951b5c3 100644 --- a/internal/jsonrpc2_v2/wire.go +++ b/internal/jsonrpc2_v2/wire.go @@ -45,7 +45,7 @@ const wireVersion = "2.0" // We can decode this and then work out which it is. type wireCombined struct { VersionTag string `json:"jsonrpc"` - ID interface{} `json:"id,omitempty"` + ID any `json:"id,omitempty"` Method string `json:"method,omitempty"` Params json.RawMessage `json:"params,omitempty"` Result json.RawMessage `json:"result,omitempty"` diff --git a/internal/jsonrpc2_v2/wire_test.go b/internal/jsonrpc2_v2/wire_test.go index e9337373239..c155c92f287 100644 --- a/internal/jsonrpc2_v2/wire_test.go +++ b/internal/jsonrpc2_v2/wire_test.go @@ -63,7 +63,7 @@ func TestWireMessage(t *testing.T) { } } -func newNotification(method string, params interface{}) jsonrpc2.Message { +func newNotification(method string, params any) jsonrpc2.Message { msg, err := jsonrpc2.NewNotification(method, params) if err != nil { panic(err) @@ -71,7 +71,7 @@ func newNotification(method string, params interface{}) jsonrpc2.Message { return msg } -func newID(id interface{}) jsonrpc2.ID { +func newID(id any) jsonrpc2.ID { switch v := id.(type) { case nil: return jsonrpc2.ID{} @@ -86,7 +86,7 @@ func newID(id interface{}) jsonrpc2.ID { } } -func newCall(id interface{}, method string, params interface{}) jsonrpc2.Message { +func newCall(id any, method string, params any) jsonrpc2.Message { msg, err := jsonrpc2.NewCall(newID(id), method, params) if err != nil { panic(err) @@ -94,7 +94,7 @@ func newCall(id interface{}, method string, params interface{}) jsonrpc2.Message return msg } -func newResponse(id interface{}, result interface{}, rerr error) jsonrpc2.Message { +func newResponse(id any, result any, rerr error) jsonrpc2.Message { msg, err := jsonrpc2.NewResponse(newID(id), result, rerr) if err != nil { panic(err) diff --git a/internal/memoize/memoize.go b/internal/memoize/memoize.go index e56af3bb45b..e49942a8827 100644 --- a/internal/memoize/memoize.go +++ b/internal/memoize/memoize.go @@ -42,7 +42,7 @@ import ( // The main purpose of the argument is to avoid the Function closure // needing to retain large objects (in practice: the snapshot) in // memory that can be supplied at call time by any caller. -type Function func(ctx context.Context, arg interface{}) interface{} +type Function func(ctx context.Context, arg any) any // A RefCounted is a value whose functional lifetime is determined by // reference counting. @@ -94,7 +94,7 @@ type Promise struct { // the function that will be used to populate the value function Function // value is set in completed state. - value interface{} + value any } // NewPromise returns a promise for the future result of calling the @@ -124,7 +124,7 @@ const ( // // It will never cause the value to be generated. // It will return the cached value, if present. -func (p *Promise) Cached() interface{} { +func (p *Promise) Cached() any { p.mu.Lock() defer p.mu.Unlock() if p.state == stateCompleted { @@ -144,7 +144,7 @@ func (p *Promise) Cached() interface{} { // If all concurrent calls to Get are cancelled, the context provided // to the function is cancelled. A later call to Get may attempt to // call the function again. -func (p *Promise) Get(ctx context.Context, arg interface{}) (interface{}, error) { +func (p *Promise) Get(ctx context.Context, arg any) (any, error) { if ctx.Err() != nil { return nil, ctx.Err() } @@ -163,7 +163,7 @@ func (p *Promise) Get(ctx context.Context, arg interface{}) (interface{}, error) } // run starts p.function and returns the result. p.mu must be locked. -func (p *Promise) run(ctx context.Context, arg interface{}) (interface{}, error) { +func (p *Promise) run(ctx context.Context, arg any) (any, error) { childCtx, cancel := context.WithCancel(xcontext.Detach(ctx)) p.cancel = cancel p.state = stateRunning @@ -210,7 +210,7 @@ func (p *Promise) run(ctx context.Context, arg interface{}) (interface{}, error) } // wait waits for the value to be computed, or ctx to be cancelled. p.mu must be locked. -func (p *Promise) wait(ctx context.Context) (interface{}, error) { +func (p *Promise) wait(ctx context.Context) (any, error) { p.waiters++ done := p.done p.mu.Unlock() @@ -258,7 +258,7 @@ type Store struct { evictionPolicy EvictionPolicy promisesMu sync.Mutex - promises map[interface{}]*Promise + promises map[any]*Promise } // NewStore creates a new store with the given eviction policy. @@ -276,13 +276,13 @@ func NewStore(policy EvictionPolicy) *Store { // // Once the last reference has been released, the promise is removed from the // store. -func (store *Store) Promise(key interface{}, function Function) (*Promise, func()) { +func (store *Store) Promise(key any, function Function) (*Promise, func()) { store.promisesMu.Lock() p, ok := store.promises[key] if !ok { p = NewPromise(reflect.TypeOf(key).String(), function) if store.promises == nil { - store.promises = map[interface{}]*Promise{} + store.promises = map[any]*Promise{} } store.promises[key] = p } @@ -323,7 +323,7 @@ func (s *Store) Stats() map[reflect.Type]int { // DebugOnlyIterate iterates through the store and, for each completed // promise, calls f(k, v) for the map key k and function result v. It // should only be used for debugging purposes. -func (s *Store) DebugOnlyIterate(f func(k, v interface{})) { +func (s *Store) DebugOnlyIterate(f func(k, v any)) { s.promisesMu.Lock() defer s.promisesMu.Unlock() diff --git a/internal/memoize/memoize_test.go b/internal/memoize/memoize_test.go index c54572d59ca..08b097eb081 100644 --- a/internal/memoize/memoize_test.go +++ b/internal/memoize/memoize_test.go @@ -18,7 +18,7 @@ func TestGet(t *testing.T) { evaled := 0 - h, release := store.Promise("key", func(context.Context, interface{}) interface{} { + h, release := store.Promise("key", func(context.Context, any) any { evaled++ return "res" }) @@ -30,7 +30,7 @@ func TestGet(t *testing.T) { } } -func expectGet(t *testing.T, h *memoize.Promise, wantV interface{}) { +func expectGet(t *testing.T, h *memoize.Promise, wantV any) { t.Helper() gotV, gotErr := h.Get(context.Background(), nil) if gotV != wantV || gotErr != nil { @@ -40,7 +40,7 @@ func expectGet(t *testing.T, h *memoize.Promise, wantV interface{}) { func TestNewPromise(t *testing.T) { calls := 0 - f := func(context.Context, interface{}) interface{} { + f := func(context.Context, any) any { calls++ return calls } @@ -63,10 +63,10 @@ func TestStoredPromiseRefCounting(t *testing.T) { var store memoize.Store v1 := false v2 := false - p1, release1 := store.Promise("key1", func(context.Context, interface{}) interface{} { + p1, release1 := store.Promise("key1", func(context.Context, any) any { return &v1 }) - p2, release2 := store.Promise("key2", func(context.Context, interface{}) interface{} { + p2, release2 := store.Promise("key2", func(context.Context, any) any { return &v2 }) expectGet(t, p1, &v1) @@ -75,7 +75,7 @@ func TestStoredPromiseRefCounting(t *testing.T) { expectGet(t, p1, &v1) expectGet(t, p2, &v2) - p2Copy, release2Copy := store.Promise("key2", func(context.Context, interface{}) interface{} { + p2Copy, release2Copy := store.Promise("key2", func(context.Context, any) any { return &v1 }) if p2 != p2Copy { @@ -93,7 +93,7 @@ func TestStoredPromiseRefCounting(t *testing.T) { } release1() - p2Copy, release2Copy = store.Promise("key2", func(context.Context, interface{}) interface{} { + p2Copy, release2Copy = store.Promise("key2", func(context.Context, any) any { return &v2 }) if p2 == p2Copy { @@ -109,7 +109,7 @@ func TestPromiseDestroyedWhileRunning(t *testing.T) { c := make(chan int) var v int - h, release := store.Promise("key", func(ctx context.Context, _ interface{}) interface{} { + h, release := store.Promise("key", func(ctx context.Context, _ any) any { <-c <-c if err := ctx.Err(); err != nil { @@ -123,7 +123,7 @@ func TestPromiseDestroyedWhileRunning(t *testing.T) { var wg sync.WaitGroup wg.Add(1) - var got interface{} + var got any var err error go func() { got, err = h.Get(ctx, nil) @@ -146,7 +146,7 @@ func TestPromiseDestroyedWhileRunning(t *testing.T) { func TestDoubleReleasePanics(t *testing.T) { var store memoize.Store - _, release := store.Promise("key", func(ctx context.Context, _ interface{}) interface{} { return 0 }) + _, release := store.Promise("key", func(ctx context.Context, _ any) any { return 0 }) panicked := false diff --git a/internal/packagesinternal/packages.go b/internal/packagesinternal/packages.go index 784605914e0..25ebab663ba 100644 --- a/internal/packagesinternal/packages.go +++ b/internal/packagesinternal/packages.go @@ -17,4 +17,4 @@ var TypecheckCgo int var DepsErrors int // must be set as a LoadMode to call GetDepsErrors var SetModFlag = func(config any, value string) {} -var SetModFile = func(config interface{}, value string) {} +var SetModFile = func(config any, value string) {} diff --git a/internal/packagestest/expect.go b/internal/packagestest/expect.go index e3e3509579d..a5f76f55686 100644 --- a/internal/packagestest/expect.go +++ b/internal/packagestest/expect.go @@ -72,7 +72,7 @@ const ( // // It is safe to call this repeatedly with different method sets, but it is // not safe to call it concurrently. -func (e *Exported) Expect(methods map[string]interface{}) error { +func (e *Exported) Expect(methods map[string]any) error { if err := e.getNotes(); err != nil { return err } @@ -98,7 +98,7 @@ func (e *Exported) Expect(methods map[string]interface{}) error { n = &expect.Note{ Pos: n.Pos, Name: markMethod, - Args: []interface{}{n.Name, n.Name}, + Args: []any{n.Name, n.Name}, } } mi, ok := ms[n.Name] @@ -222,7 +222,7 @@ func (e *Exported) getMarkers() error { } // set markers early so that we don't call getMarkers again from Expect e.markers = make(map[string]Range) - return e.Expect(map[string]interface{}{ + return e.Expect(map[string]any{ markMethod: e.Mark, }) } @@ -243,7 +243,7 @@ var ( // It takes the args remaining, and returns the args it did not consume. // This allows a converter to consume 0 args for well known types, or multiple // args for compound types. -type converter func(*expect.Note, []interface{}) (reflect.Value, []interface{}, error) +type converter func(*expect.Note, []any) (reflect.Value, []any, error) // method is used to track information about Invoke methods that is expensive to // calculate so that we can work it out once rather than per marker. @@ -259,19 +259,19 @@ type method struct { func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { switch { case pt == noteType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(n), args, nil }, nil case pt == fsetType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(e.ExpectFileSet), args, nil }, nil case pt == exportedType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { return reflect.ValueOf(e), args, nil }, nil case pt == posType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -279,7 +279,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(r.Start), remains, nil }, nil case pt == positionType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -287,7 +287,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(e.ExpectFileSet.Position(r.Start)), remains, nil }, nil case pt == rangeType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { r, remains, err := e.rangeConverter(n, args) if err != nil { return reflect.Value{}, nil, err @@ -295,7 +295,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(r), remains, nil }, nil case pt == identifierType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -310,7 +310,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil case pt == regexType: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -323,7 +323,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil case pt.Kind() == reflect.String: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -339,7 +339,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } }, nil case pt.Kind() == reflect.Int64: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -353,7 +353,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } }, nil case pt.Kind() == reflect.Bool: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -366,7 +366,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return reflect.ValueOf(b), args, nil }, nil case pt.Kind() == reflect.Slice: - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { converter, err := e.buildConverter(pt.Elem()) if err != nil { return reflect.Value{}, nil, err @@ -384,7 +384,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { }, nil default: if pt.Kind() == reflect.Interface && pt.NumMethod() == 0 { - return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { + return func(n *expect.Note, args []any) (reflect.Value, []any, error) { if len(args) < 1 { return reflect.Value{}, nil, fmt.Errorf("missing argument") } @@ -395,7 +395,7 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { } } -func (e *Exported) rangeConverter(n *expect.Note, args []interface{}) (Range, []interface{}, error) { +func (e *Exported) rangeConverter(n *expect.Note, args []any) (Range, []any, error) { tokFile := e.ExpectFileSet.File(n.Pos) if len(args) < 1 { return Range{}, nil, fmt.Errorf("missing argument") diff --git a/internal/packagestest/expect_test.go b/internal/packagestest/expect_test.go index d155f5fe9e2..4f148b4183e 100644 --- a/internal/packagestest/expect_test.go +++ b/internal/packagestest/expect_test.go @@ -19,7 +19,7 @@ func TestExpect(t *testing.T) { }}) defer exported.Cleanup() checkCount := 0 - if err := exported.Expect(map[string]interface{}{ + if err := exported.Expect(map[string]any{ "check": func(src, target token.Position) { checkCount++ }, diff --git a/internal/packagestest/export.go b/internal/packagestest/export.go index f8d10718c09..ce992e17a90 100644 --- a/internal/packagestest/export.go +++ b/internal/packagestest/export.go @@ -97,7 +97,7 @@ type Module struct { // The keys are the file fragment that follows the module name, the value can // be a string or byte slice, in which case it is the contents of the // file, otherwise it must be a Writer function. - Files map[string]interface{} + Files map[string]any // Overlay is the set of source file overlays for the module. // The keys are the file fragment as in the Files configuration. @@ -479,7 +479,7 @@ func GroupFilesByModules(root string) ([]Module, error) { primarymod := &Module{ Name: root, - Files: make(map[string]interface{}), + Files: make(map[string]any), Overlay: make(map[string][]byte), } mods := map[string]*Module{ @@ -569,7 +569,7 @@ func GroupFilesByModules(root string) ([]Module, error) { } mods[path] = &Module{ Name: filepath.ToSlash(module), - Files: make(map[string]interface{}), + Files: make(map[string]any), Overlay: make(map[string][]byte), } currentModule = path @@ -587,8 +587,8 @@ func GroupFilesByModules(root string) ([]Module, error) { // This is to enable the common case in tests where you have a full copy of the // package in your testdata. // This will panic if there is any kind of error trying to walk the file tree. -func MustCopyFileTree(root string) map[string]interface{} { - result := map[string]interface{}{} +func MustCopyFileTree(root string) map[string]any { + result := map[string]any{} if err := filepath.Walk(filepath.FromSlash(root), func(path string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/internal/packagestest/export_test.go b/internal/packagestest/export_test.go index 6c074216fbe..fae8bd2d5ba 100644 --- a/internal/packagestest/export_test.go +++ b/internal/packagestest/export_test.go @@ -16,7 +16,7 @@ import ( var testdata = []packagestest.Module{{ Name: "golang.org/fake1", - Files: map[string]interface{}{ + Files: map[string]any{ "a.go": packagestest.Symlink("testdata/a.go"), // broken symlink "b.go": "invalid file contents", }, @@ -26,22 +26,22 @@ var testdata = []packagestest.Module{{ }, }, { Name: "golang.org/fake2", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake2", }, }, { Name: "golang.org/fake2/v2", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake2", }, }, { Name: "golang.org/fake3@v1.0.0", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake3", }, }, { Name: "golang.org/fake3@v1.1.0", - Files: map[string]interface{}{ + Files: map[string]any{ "other/a.go": "package fake3", }, }} @@ -97,13 +97,13 @@ func TestGroupFilesByModules(t *testing.T) { want: []packagestest.Module{ { Name: "testdata/groups/one", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/extra", - Files: map[string]interface{}{ + Files: map[string]any{ "help.go": true, }, }, @@ -114,7 +114,7 @@ func TestGroupFilesByModules(t *testing.T) { want: []packagestest.Module{ { Name: "testdata/groups/two", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, "expect/yo.go": true, "expect/yo_test.go": true, @@ -122,33 +122,33 @@ func TestGroupFilesByModules(t *testing.T) { }, { Name: "example.com/extra", - Files: map[string]interface{}{ + Files: map[string]any{ "yo.go": true, "geez/help.go": true, }, }, { Name: "example.com/extra/v2", - Files: map[string]interface{}{ + Files: map[string]any{ "me.go": true, "geez/help.go": true, }, }, { Name: "example.com/tempmod", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/what@v1.0.0", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, { Name: "example.com/what@v1.1.0", - Files: map[string]interface{}{ + Files: map[string]any{ "main.go": true, }, }, diff --git a/internal/tool/tool.go b/internal/tool/tool.go index fe2b1c289b8..6420c9667d9 100644 --- a/internal/tool/tool.go +++ b/internal/tool/tool.go @@ -81,7 +81,7 @@ func (e commandLineError) Error() string { return string(e) } // CommandLineErrorf is like fmt.Errorf except that it returns a value that // triggers printing of the command line help. // In general you should use this when generating command line validation errors. -func CommandLineErrorf(message string, args ...interface{}) error { +func CommandLineErrorf(message string, args ...any) error { return commandLineError(fmt.Sprintf(message, args...)) } diff --git a/internal/typeparams/normalize.go b/internal/typeparams/normalize.go index 93c80fdc96c..f49802b8ef7 100644 --- a/internal/typeparams/normalize.go +++ b/internal/typeparams/normalize.go @@ -120,7 +120,7 @@ type termSet struct { terms termlist } -func indentf(depth int, format string, args ...interface{}) { +func indentf(depth int, format string, args ...any) { fmt.Fprintf(os.Stderr, strings.Repeat(".", depth)+format+"\n", args...) } diff --git a/internal/xcontext/xcontext.go b/internal/xcontext/xcontext.go index ff8ed4ebb95..641dfe5a102 100644 --- a/internal/xcontext/xcontext.go +++ b/internal/xcontext/xcontext.go @@ -17,7 +17,7 @@ func Detach(ctx context.Context) context.Context { return detachedContext{ctx} } type detachedContext struct{ parent context.Context } -func (v detachedContext) Deadline() (time.Time, bool) { return time.Time{}, false } -func (v detachedContext) Done() <-chan struct{} { return nil } -func (v detachedContext) Err() error { return nil } -func (v detachedContext) Value(key interface{}) interface{} { return v.parent.Value(key) } +func (v detachedContext) Deadline() (time.Time, bool) { return time.Time{}, false } +func (v detachedContext) Done() <-chan struct{} { return nil } +func (v detachedContext) Err() error { return nil } +func (v detachedContext) Value(key any) any { return v.parent.Value(key) } From 8a39d47f70846bf278b4bfb793f04b76e478e37b Mon Sep 17 00:00:00 2001 From: Viktor Blomqvist Date: Mon, 13 Jan 2025 20:25:31 +0100 Subject: [PATCH 170/309] gopls/internal/golang: Add "Eliminate dot import" code action. The code action will qualify identifiers if possible. If there are names in scope which will shadow the package name then the code action fails. Updates golang/go#70319. Change-Id: I7c1ff1c60d592cb6f1093ab653c04a44d7092607 Reviewed-on: https://go-review.googlesource.com/c/tools/+/642016 Auto-Submit: Robert Findley Reviewed-by: Robert Findley Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/features/transformation.md | 9 ++ gopls/doc/release/v0.19.0.md | 7 ++ gopls/internal/golang/codeaction.go | 100 ++++++++++++++++++ gopls/internal/settings/codeactionkind.go | 19 ++-- .../codeaction/eliminate_dot_import.txt | 40 +++++++ 5 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 gopls/doc/release/v0.19.0.md create mode 100644 gopls/internal/test/marker/testdata/codeaction/eliminate_dot_import.txt diff --git a/gopls/doc/features/transformation.md b/gopls/doc/features/transformation.md index caf13221cfa..a72ff676832 100644 --- a/gopls/doc/features/transformation.md +++ b/gopls/doc/features/transformation.md @@ -814,3 +814,12 @@ which HTML documents are composed: ![Before "Add cases for Addr"](../assets/fill-switch-enum-before.png) ![After "Add cases for Addr"](../assets/fill-switch-enum-after.png) + + + +### `refactor.rewrite.eliminateDotImport`: Eliminate dot import + +When the cursor is on a dot import gopls can offer the "Eliminate dot import" +code action, which removes the dot from the import and qualifies uses of the +package throughout the file. This code action is offered only if +each use of the package can be qualified without collisions with existing names. diff --git a/gopls/doc/release/v0.19.0.md b/gopls/doc/release/v0.19.0.md new file mode 100644 index 00000000000..0b3ea64c305 --- /dev/null +++ b/gopls/doc/release/v0.19.0.md @@ -0,0 +1,7 @@ +# New features + +## "Eliminate dot import" code action + +This code action, available on a dotted import, will offer to replace +the import with a regular one and qualify each use of the package +with its name. diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 49a861852ff..587ae3e2de3 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -260,6 +260,7 @@ var codeActionProducers = [...]codeActionProducer{ {kind: settings.RefactorRewriteMoveParamLeft, fn: refactorRewriteMoveParamLeft, needPkg: true}, {kind: settings.RefactorRewriteMoveParamRight, fn: refactorRewriteMoveParamRight, needPkg: true}, {kind: settings.RefactorRewriteSplitLines, fn: refactorRewriteSplitLines, needPkg: true}, + {kind: settings.RefactorRewriteEliminateDotImport, fn: refactorRewriteEliminateDotImport, needPkg: true}, // Note: don't forget to update the allow-list in Server.CodeAction // when adding new query operations like GoTest and GoDoc that @@ -678,6 +679,105 @@ func refactorRewriteSplitLines(ctx context.Context, req *codeActionsRequest) err return nil } +func refactorRewriteEliminateDotImport(ctx context.Context, req *codeActionsRequest) error { + // Figure out if the request is placed over a dot import. + var importSpec *ast.ImportSpec + for _, imp := range req.pgf.File.Imports { + if posRangeContains(imp.Pos(), imp.End(), req.start, req.end) { + importSpec = imp + break + } + } + if importSpec == nil { + return nil + } + if importSpec.Name == nil || importSpec.Name.Name != "." { + return nil + } + + // dotImported package path and its imported name after removing the dot. + imported := req.pkg.TypesInfo().PkgNameOf(importSpec).Imported() + newName := imported.Name() + + rng, err := req.pgf.PosRange(importSpec.Name.Pos(), importSpec.Path.Pos()) + if err != nil { + return err + } + // Delete the '.' part of the import. + edits := []protocol.TextEdit{{ + Range: rng, + }} + + fileScope, ok := req.pkg.TypesInfo().Scopes[req.pgf.File] + if !ok { + return nil + } + + // Go through each use of the dot imported package, checking its scope for + // shadowing and calculating an edit to qualify the identifier. + var stack []ast.Node + ast.Inspect(req.pgf.File, func(n ast.Node) bool { + if n == nil { + stack = stack[:len(stack)-1] // pop + return false + } + stack = append(stack, n) // push + + ident, ok := n.(*ast.Ident) + if !ok { + return true + } + // Only keep identifiers that use a symbol from the + // dot imported package. + use := req.pkg.TypesInfo().Uses[ident] + if use == nil || use.Pkg() == nil { + return true + } + if use.Pkg() != imported { + return true + } + + // Only qualify unqualified identifiers (due to dot imports). + // All other references to a symbol imported from another package + // are nested within a select expression (pkg.Foo, v.Method, v.Field). + if is[*ast.SelectorExpr](stack[len(stack)-2]) { + return true + } + + // Make sure that the package name will not be shadowed by something else in scope. + // If it is then we cannot offer this particular code action. + // + // TODO: If the object found in scope is the package imported without a + // dot, or some builtin not used in the file, the code action could be + // allowed to go through. + sc := fileScope.Innermost(ident.Pos()) + if sc == nil { + return true + } + _, obj := sc.LookupParent(newName, ident.Pos()) + if obj != nil { + return true + } + + rng, err := req.pgf.PosRange(ident.Pos(), ident.Pos()) // sic, zero-width range before ident + if err != nil { + return true + } + edits = append(edits, protocol.TextEdit{ + Range: rng, + NewText: newName + ".", + }) + + return true + }) + + req.addEditAction("Eliminate dot import", nil, protocol.DocumentChangeEdit( + req.fh, + edits, + )) + return nil +} + // refactorRewriteJoinLines produces "Join ITEMS into one line" code actions. // See [joinLines] for command implementation. func refactorRewriteJoinLines(ctx context.Context, req *codeActionsRequest) error { diff --git a/gopls/internal/settings/codeactionkind.go b/gopls/internal/settings/codeactionkind.go index fcce7cd2682..09d9d419567 100644 --- a/gopls/internal/settings/codeactionkind.go +++ b/gopls/internal/settings/codeactionkind.go @@ -86,15 +86,16 @@ const ( GoplsDocFeatures protocol.CodeActionKind = "gopls.doc.features" // refactor.rewrite - RefactorRewriteChangeQuote protocol.CodeActionKind = "refactor.rewrite.changeQuote" - RefactorRewriteFillStruct protocol.CodeActionKind = "refactor.rewrite.fillStruct" - RefactorRewriteFillSwitch protocol.CodeActionKind = "refactor.rewrite.fillSwitch" - RefactorRewriteInvertIf protocol.CodeActionKind = "refactor.rewrite.invertIf" - RefactorRewriteJoinLines protocol.CodeActionKind = "refactor.rewrite.joinLines" - RefactorRewriteRemoveUnusedParam protocol.CodeActionKind = "refactor.rewrite.removeUnusedParam" - RefactorRewriteMoveParamLeft protocol.CodeActionKind = "refactor.rewrite.moveParamLeft" - RefactorRewriteMoveParamRight protocol.CodeActionKind = "refactor.rewrite.moveParamRight" - RefactorRewriteSplitLines protocol.CodeActionKind = "refactor.rewrite.splitLines" + RefactorRewriteChangeQuote protocol.CodeActionKind = "refactor.rewrite.changeQuote" + RefactorRewriteFillStruct protocol.CodeActionKind = "refactor.rewrite.fillStruct" + RefactorRewriteFillSwitch protocol.CodeActionKind = "refactor.rewrite.fillSwitch" + RefactorRewriteInvertIf protocol.CodeActionKind = "refactor.rewrite.invertIf" + RefactorRewriteJoinLines protocol.CodeActionKind = "refactor.rewrite.joinLines" + RefactorRewriteRemoveUnusedParam protocol.CodeActionKind = "refactor.rewrite.removeUnusedParam" + RefactorRewriteMoveParamLeft protocol.CodeActionKind = "refactor.rewrite.moveParamLeft" + RefactorRewriteMoveParamRight protocol.CodeActionKind = "refactor.rewrite.moveParamRight" + RefactorRewriteSplitLines protocol.CodeActionKind = "refactor.rewrite.splitLines" + RefactorRewriteEliminateDotImport protocol.CodeActionKind = "refactor.rewrite.eliminateDotImport" // refactor.inline RefactorInlineCall protocol.CodeActionKind = "refactor.inline.call" diff --git a/gopls/internal/test/marker/testdata/codeaction/eliminate_dot_import.txt b/gopls/internal/test/marker/testdata/codeaction/eliminate_dot_import.txt new file mode 100644 index 00000000000..e72d8bd5417 --- /dev/null +++ b/gopls/internal/test/marker/testdata/codeaction/eliminate_dot_import.txt @@ -0,0 +1,40 @@ +This test checks the behavior of the 'remove dot import' code action. + +-- go.mod -- +module golang.org/lsptests/removedotimport + +go 1.18 + +-- a.go -- +package dotimport + +// Base case: action is OK. + +import ( + . "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1) + . "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2) +) + +var _ = a + +func a() { + Println("hello") + + buf := NewBuffer(nil) + buf.Grow(10) +} + +-- @a1/a.go -- +@@ -6 +6 @@ +- . "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1) ++ "fmt" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a1) +@@ -13 +13 @@ +- Println("hello") ++ fmt.Println("hello") +-- @a2/a.go -- +@@ -7 +7 @@ +- . "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2) ++ "bytes" //@codeaction(`.`, "refactor.rewrite.eliminateDotImport", edit=a2) +@@ -15 +15 @@ +- buf := NewBuffer(nil) ++ buf := bytes.NewBuffer(nil) From 300465cc970af3836a5368d587764267a8f4d77e Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 19 Feb 2025 18:33:23 -0500 Subject: [PATCH 171/309] gopls/internal/analysis/modernize: fix rangeint bug info.Defs[v] is nil if the loop variable is not declared (for i = 0 instead of for i := 0). + test Updates golang/go#71847 Change-Id: I28f82188e813f2d4f1ddc9335f0c13bd90c31ec1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650815 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/rangeint.go | 2 +- .../modernize/testdata/src/rangeint/rangeint.go | 13 +++++++++++++ .../testdata/src/rangeint/rangeint.go.golden | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 2d25d6a0a06..273c13877bd 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -75,7 +75,7 @@ func rangeint(pass *analysis.Pass) { // Have: for i = 0; i < limit; i++ {} // Find references to i within the loop body. - v := info.Defs[index] + v := info.ObjectOf(index) used := false for curId := range curLoop.Child(loop.Body).Preorder((*ast.Ident)(nil)) { id := curId.Node().(*ast.Ident) diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index a60bd5eac37..6c30f183340 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -12,6 +12,9 @@ func _(i int, s struct{ i int }, slice []int) { for i := 0; i < len(slice); i++ { // want "for loop can be modernized using range over int" println(slice[i]) } + for i := 0; i < len(""); i++ { // want "for loop can be modernized using range over int" + // NB: not simplified to range "" + } // nope for i := 0; i < 10; { // nope: missing increment @@ -38,3 +41,13 @@ func _(i int, s struct{ i int }, slice []int) { } func f() int { return 0 } + +// Repro for part of #71847: ("for range n is invalid if the loop body contains i++"): +func _(s string) { + var i int // (this is necessary) + for i = 0; i < len(s); i++ { // nope: loop body increments i + if true { + i++ // nope + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 348f77508ac..52f16347b1e 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -12,6 +12,9 @@ func _(i int, s struct{ i int }, slice []int) { for i := range slice { // want "for loop can be modernized using range over int" println(slice[i]) } + for range len("") { // want "for loop can be modernized using range over int" + // NB: not simplified to range "" + } // nope for i := 0; i < 10; { // nope: missing increment @@ -38,3 +41,13 @@ func _(i int, s struct{ i int }, slice []int) { } func f() int { return 0 } + +// Repro for part of #71847: ("for range n is invalid if the loop body contains i++"): +func _(s string) { + var i int // (this is necessary) + for i = 0; i < len(s); i++ { // nope: loop body increments i + if true { + i++ // nope + } + } +} From f0af81c3ddded0970b2ffe7922f269b53e1a63bb Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 14 Feb 2025 14:24:21 -0500 Subject: [PATCH 172/309] gopls/internal/goasm: support Definition in Go *.s assembly This CL provides a minimal implementation of the Definition query within Go assembly files, plus a test. For now it only works for references to package-level symbols in the same package or a dependency. Details: - add file.Kind Asm and protocol.LanguageKind "go.s". - include .s files in metadata.Graph.IDs mapping. - set LanguageKind correctly in gopls CLI. Also: - add String() method to file.Handle. - add convenient forward deps iterator to Graph. - internal/extract: extract notes from .s files too. Updates golang/go#71754 Change-Id: I0c518c3279f825411221ebe23dc04654e129fc56 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649461 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Robert Findley Commit-Queue: Alan Donovan --- gopls/internal/cache/fs_memoized.go | 2 + gopls/internal/cache/fs_overlay.go | 2 + gopls/internal/cache/metadata/graph.go | 36 +++++ gopls/internal/cache/parse_cache_test.go | 4 + gopls/internal/cache/session.go | 1 + gopls/internal/cache/snapshot.go | 21 +++ gopls/internal/cmd/cmd.go | 17 ++- gopls/internal/cmd/definition.go | 2 +- gopls/internal/file/file.go | 2 + gopls/internal/file/kind.go | 8 +- gopls/internal/goasm/definition.go | 125 ++++++++++++++++++ gopls/internal/golang/snapshot.go | 14 +- gopls/internal/server/definition.go | 3 + .../internal/test/integration/fake/editor.go | 14 +- .../test/marker/testdata/definition/asm.txt | 33 +++++ internal/expect/extract.go | 46 ++++++- 16 files changed, 304 insertions(+), 26 deletions(-) create mode 100644 gopls/internal/goasm/definition.go create mode 100644 gopls/internal/test/marker/testdata/definition/asm.txt diff --git a/gopls/internal/cache/fs_memoized.go b/gopls/internal/cache/fs_memoized.go index 9f156e3e153..a179b0ce7f5 100644 --- a/gopls/internal/cache/fs_memoized.go +++ b/gopls/internal/cache/fs_memoized.go @@ -41,6 +41,8 @@ type diskFile struct { err error } +func (h *diskFile) String() string { return h.uri.Path() } + func (h *diskFile) URI() protocol.DocumentURI { return h.uri } func (h *diskFile) Identity() file.Identity { diff --git a/gopls/internal/cache/fs_overlay.go b/gopls/internal/cache/fs_overlay.go index 265598bb967..b18d6d3f154 100644 --- a/gopls/internal/cache/fs_overlay.go +++ b/gopls/internal/cache/fs_overlay.go @@ -64,6 +64,8 @@ type overlay struct { saved bool } +func (o *overlay) String() string { return o.uri.Path() } + func (o *overlay) URI() protocol.DocumentURI { return o.uri } func (o *overlay) Identity() file.Identity { diff --git a/gopls/internal/cache/metadata/graph.go b/gopls/internal/cache/metadata/graph.go index 4b846df53be..716b767e37b 100644 --- a/gopls/internal/cache/metadata/graph.go +++ b/gopls/internal/cache/metadata/graph.go @@ -5,7 +5,9 @@ package metadata import ( + "iter" "sort" + "strings" "golang.org/x/tools/go/packages" "golang.org/x/tools/gopls/internal/protocol" @@ -99,6 +101,11 @@ func newGraph(pkgs map[PackageID]*Package) *Graph { for _, uri := range mp.GoFiles { uris[uri] = struct{}{} } + for _, uri := range mp.OtherFiles { + if strings.HasSuffix(string(uri), ".s") { // assembly + uris[uri] = struct{}{} + } + } for uri := range uris { uriIDs[uri] = append(uriIDs[uri], id) } @@ -160,6 +167,35 @@ func (g *Graph) ReverseReflexiveTransitiveClosure(ids ...PackageID) map[PackageI return seen } +// ForwardReflexiveTransitiveClosure returns an iterator over the +// specified nodes and all their forward dependencies, in an arbitrary +// topological (dependencies-first) order. The order may vary. +func (g *Graph) ForwardReflexiveTransitiveClosure(ids ...PackageID) iter.Seq[*Package] { + return func(yield func(*Package) bool) { + seen := make(map[PackageID]bool) + var visit func(PackageID) bool + visit = func(id PackageID) bool { + if !seen[id] { + seen[id] = true + if mp := g.Packages[id]; mp != nil { + for _, depID := range mp.DepsByPkgPath { + if !visit(depID) { + return false + } + } + if !yield(mp) { + return false + } + } + } + return true + } + for _, id := range ids { + visit(id) + } + } +} + // breakImportCycles breaks import cycles in the metadata by deleting // Deps* edges. It modifies only metadata present in the 'updates' // subset. This function has an internal test. diff --git a/gopls/internal/cache/parse_cache_test.go b/gopls/internal/cache/parse_cache_test.go index 7aefac77c38..fe0548aa20d 100644 --- a/gopls/internal/cache/parse_cache_test.go +++ b/gopls/internal/cache/parse_cache_test.go @@ -218,6 +218,10 @@ type fakeFileHandle struct { hash file.Hash } +func (h fakeFileHandle) String() string { + return h.uri.Path() +} + func (h fakeFileHandle) URI() protocol.DocumentURI { return h.uri } diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index 5ae753eb91c..c2f57e985f7 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -1084,6 +1084,7 @@ type brokenFile struct { err error } +func (b brokenFile) String() string { return b.uri.Path() } func (b brokenFile) URI() protocol.DocumentURI { return b.uri } func (b brokenFile) Identity() file.Identity { return file.Identity{URI: b.uri} } func (b brokenFile) SameContentsOnDisk() bool { return false } diff --git a/gopls/internal/cache/snapshot.go b/gopls/internal/cache/snapshot.go index 578cea61eb7..754389c7008 100644 --- a/gopls/internal/cache/snapshot.go +++ b/gopls/internal/cache/snapshot.go @@ -323,6 +323,8 @@ func fileKind(fh file.Handle) file.Kind { return file.Sum case ".work": return file.Work + case ".s": + return file.Asm } return file.UnknownKind } @@ -645,6 +647,21 @@ func (s *Snapshot) Tests(ctx context.Context, ids ...PackageID) ([]*testfuncs.In return indexes, s.forEachPackage(ctx, ids, pre, post) } +// NarrowestMetadataForFile returns metadata for the narrowest package +// (the one with the fewest files) that encloses the specified file. +// The result may be a test variant, but never an intermediate test variant. +func (snapshot *Snapshot) NarrowestMetadataForFile(ctx context.Context, uri protocol.DocumentURI) (*metadata.Package, error) { + mps, err := snapshot.MetadataForFile(ctx, uri) + if err != nil { + return nil, err + } + metadata.RemoveIntermediateTestVariants(&mps) + if len(mps) == 0 { + return nil, fmt.Errorf("no package metadata for file %s", uri) + } + return mps[0], nil +} + // MetadataForFile returns a new slice containing metadata for each // package containing the Go file identified by uri, ordered by the // number of CompiledGoFiles (i.e. "narrowest" to "widest" package), @@ -652,6 +669,10 @@ func (s *Snapshot) Tests(ctx context.Context, ids ...PackageID) ([]*testfuncs.In // The result may include tests and intermediate test variants of // importable packages. // It returns an error if the context was cancelled. +// +// TODO(adonovan): in nearly all cases the caller must use +// [metadata.RemoveIntermediateTestVariants]. Make this a parameter to +// force the caller to consider it (and reduce code). func (s *Snapshot) MetadataForFile(ctx context.Context, uri protocol.DocumentURI) ([]*metadata.Package, error) { if s.view.typ == AdHocView { // As described in golang/go#57209, in ad-hoc workspaces (where we load ./ diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index 2a161ad0fc8..8bd7d7b899f 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -773,10 +773,25 @@ func (c *connection) openFile(ctx context.Context, uri protocol.DocumentURI) (*c return nil, file.err } + // Choose language ID from file extension. + var langID protocol.LanguageKind // "" eventually maps to file.UnknownKind + switch filepath.Ext(uri.Path()) { + case ".go": + langID = "go" + case ".mod": + langID = "go.mod" + case ".sum": + langID = "go.sum" + case ".work": + langID = "go.work" + case ".s": + langID = "go.s" + } + p := &protocol.DidOpenTextDocumentParams{ TextDocument: protocol.TextDocumentItem{ URI: uri, - LanguageID: "go", + LanguageID: langID, Version: 1, Text: string(file.mapper.Content), }, diff --git a/gopls/internal/cmd/definition.go b/gopls/internal/cmd/definition.go index d9cd98554e3..71e8b1511bd 100644 --- a/gopls/internal/cmd/definition.go +++ b/gopls/internal/cmd/definition.go @@ -96,7 +96,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error { } if len(locs) == 0 { - return fmt.Errorf("%v: not an identifier", from) + return fmt.Errorf("%v: no definition location (not an identifier?)", from) } file, err = conn.openFile(ctx, locs[0].URI) if err != nil { diff --git a/gopls/internal/file/file.go b/gopls/internal/file/file.go index 5f8be06cf60..b817306aa07 100644 --- a/gopls/internal/file/file.go +++ b/gopls/internal/file/file.go @@ -49,6 +49,8 @@ type Handle interface { // Content returns the contents of a file. // If the file is not available, returns a nil slice and an error. Content() ([]byte, error) + // String returns the file's path. + String() string } // A Source maps URIs to Handles. diff --git a/gopls/internal/file/kind.go b/gopls/internal/file/kind.go index 087a57f32d0..6a0ed009ed5 100644 --- a/gopls/internal/file/kind.go +++ b/gopls/internal/file/kind.go @@ -28,6 +28,8 @@ const ( Tmpl // Work is a go.work file. Work + // Asm is a Go assembly (.s) file. + Asm ) func (k Kind) String() string { @@ -42,13 +44,15 @@ func (k Kind) String() string { return "tmpl" case Work: return "go.work" + case Asm: + return "Go assembly" default: return fmt.Sprintf("internal error: unknown file kind %d", k) } } // KindForLang returns the gopls file [Kind] associated with the given LSP -// LanguageKind string from protocol.TextDocumentItem.LanguageID, +// LanguageKind string from the LanguageID field of [protocol.TextDocumentItem], // or UnknownKind if the language is not one recognized by gopls. func KindForLang(langID protocol.LanguageKind) Kind { switch langID { @@ -62,6 +66,8 @@ func KindForLang(langID protocol.LanguageKind) Kind { return Tmpl case "go.work": return Work + case "go.s": + return Asm default: return UnknownKind } diff --git a/gopls/internal/goasm/definition.go b/gopls/internal/goasm/definition.go new file mode 100644 index 00000000000..4403e7cac7f --- /dev/null +++ b/gopls/internal/goasm/definition.go @@ -0,0 +1,125 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package goasm + +import ( + "bytes" + "context" + "fmt" + "go/token" + "strings" + "unicode" + + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/metadata" + "golang.org/x/tools/gopls/internal/file" + "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/util/morestrings" + "golang.org/x/tools/internal/event" +) + +// Definition handles the textDocument/definition request for Go assembly files. +func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) ([]protocol.Location, error) { + ctx, done := event.Start(ctx, "goasm.Definition") + defer done() + + mp, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI()) + if err != nil { + return nil, err + } + + // Read the file. + content, err := fh.Content() + if err != nil { + return nil, err + } + mapper := protocol.NewMapper(fh.URI(), content) + offset, err := mapper.PositionOffset(position) + if err != nil { + return nil, err + } + + // Figure out the selected symbol. + // For now, just find the identifier around the cursor. + // + // TODO(adonovan): use a real asm parser; see cmd/asm/internal/asm/parse.go. + // Ideally this would just be just another attribute of the + // type-checked cache.Package. + nonIdentRune := func(r rune) bool { return !isIdentRune(r) } + i := bytes.LastIndexFunc(content[:offset], nonIdentRune) + j := bytes.IndexFunc(content[offset:], nonIdentRune) + if j < 0 || j == 0 { + return nil, nil // identifier runs to EOF, or not an identifier + } + sym := string(content[i+1 : offset+j]) + sym = strings.ReplaceAll(sym, "·", ".") // (U+00B7 MIDDLE DOT) + sym = strings.ReplaceAll(sym, "∕", "/") // (U+2215 DIVISION SLASH) + if sym != "" && sym[0] == '.' { + sym = string(mp.PkgPath) + sym + } + + // package-qualified symbol? + if pkgpath, name, ok := morestrings.CutLast(sym, "."); ok { + // Find declaring package among dependencies. + // + // TODO(adonovan): assembly may legally reference + // non-dependencies. For example, sync/atomic calls + // internal/runtime/atomic. Perhaps we should search + // the entire metadata graph, but that's path-dependent. + var declaring *metadata.Package + for pkg := range snapshot.MetadataGraph().ForwardReflexiveTransitiveClosure(mp.ID) { + if pkg.PkgPath == metadata.PackagePath(pkgpath) { + declaring = pkg + break + } + } + if declaring == nil { + return nil, fmt.Errorf("package %q is not a dependency", pkgpath) + } + + pkgs, err := snapshot.TypeCheck(ctx, declaring.ID) + if err != nil { + return nil, err + } + pkg := pkgs[0] + def := pkg.Types().Scope().Lookup(name) + if def == nil { + return nil, fmt.Errorf("no symbol %q in package %q", name, pkgpath) + } + loc, err := mapPosition(ctx, pkg.FileSet(), snapshot, def.Pos(), def.Pos()) + if err == nil { + return []protocol.Location{loc}, nil + } + } + + // TODO(adonovan): support jump to var, block label, and other + // TEXT, DATA, and GLOBAL symbols in the same file. Needs asm parser. + + return nil, nil +} + +// The assembler allows center dot (· U+00B7) and +// division slash (∕ U+2215) to work as identifier characters. +func isIdentRune(r rune) bool { + return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '·' || r == '∕' +} + +// TODO(rfindley): avoid the duplicate column mapping here, by associating a +// column mapper with each file handle. +// TODO(adonovan): plundered from ../golang; factor. +func mapPosition(ctx context.Context, fset *token.FileSet, s file.Source, start, end token.Pos) (protocol.Location, error) { + file := fset.File(start) + uri := protocol.URIFromPath(file.Name()) + fh, err := s.ReadFile(ctx, uri) + if err != nil { + return protocol.Location{}, err + } + content, err := fh.Content() + if err != nil { + return protocol.Location{}, err + } + m := protocol.NewMapper(fh.URI(), content) + return m.PosLocation(file, start, end) +} diff --git a/gopls/internal/golang/snapshot.go b/gopls/internal/golang/snapshot.go index c381c962d08..30199d45463 100644 --- a/gopls/internal/golang/snapshot.go +++ b/gopls/internal/golang/snapshot.go @@ -14,19 +14,9 @@ import ( "golang.org/x/tools/gopls/internal/protocol" ) -// NarrowestMetadataForFile returns metadata for the narrowest package -// (the one with the fewest files) that encloses the specified file. -// The result may be a test variant, but never an intermediate test variant. +//go:fix inline func NarrowestMetadataForFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (*metadata.Package, error) { - mps, err := snapshot.MetadataForFile(ctx, uri) - if err != nil { - return nil, err - } - metadata.RemoveIntermediateTestVariants(&mps) - if len(mps) == 0 { - return nil, fmt.Errorf("no package metadata for file %s", uri) - } - return mps[0], nil + return snapshot.NarrowestMetadataForFile(ctx, uri) } // NarrowestPackageForFile is a convenience function that selects the narrowest diff --git a/gopls/internal/server/definition.go b/gopls/internal/server/definition.go index 7b4df3c7c07..5a9c020cfc5 100644 --- a/gopls/internal/server/definition.go +++ b/gopls/internal/server/definition.go @@ -9,6 +9,7 @@ import ( "fmt" "golang.org/x/tools/gopls/internal/file" + "golang.org/x/tools/gopls/internal/goasm" "golang.org/x/tools/gopls/internal/golang" "golang.org/x/tools/gopls/internal/label" "golang.org/x/tools/gopls/internal/protocol" @@ -37,6 +38,8 @@ func (s *server) Definition(ctx context.Context, params *protocol.DefinitionPara return template.Definition(snapshot, fh, params.Position) case file.Go: return golang.Definition(ctx, snapshot, fh, params.Position) + case file.Asm: + return goasm.Definition(ctx, snapshot, fh, params.Position) default: return nil, fmt.Errorf("can't find definitions for file type %s", kind) } diff --git a/gopls/internal/test/integration/fake/editor.go b/gopls/internal/test/integration/fake/editor.go index adc9df6c17d..170a9823cad 100644 --- a/gopls/internal/test/integration/fake/editor.go +++ b/gopls/internal/test/integration/fake/editor.go @@ -113,11 +113,12 @@ type EditorConfig struct { // Map of language ID -> regexp to match, used to set the file type of new // buffers. Applied as an overlay on top of the following defaults: - // "go" -> ".*\.go" + // "go" -> ".*\.go" // "go.mod" -> "go\.mod" // "go.sum" -> "go\.sum" // "gotmpl" -> ".*tmpl" - FileAssociations map[string]string + // "go.s" -> ".*\.s" + FileAssociations map[protocol.LanguageKind]string // Settings holds user-provided configuration for the LSP server. Settings map[string]any @@ -619,27 +620,28 @@ func (e *Editor) sendDidOpen(ctx context.Context, item protocol.TextDocumentItem return nil } -var defaultFileAssociations = map[string]*regexp.Regexp{ +var defaultFileAssociations = map[protocol.LanguageKind]*regexp.Regexp{ "go": regexp.MustCompile(`^.*\.go$`), // '$' is important: don't match .gotmpl! "go.mod": regexp.MustCompile(`^go\.mod$`), "go.sum": regexp.MustCompile(`^go(\.work)?\.sum$`), "go.work": regexp.MustCompile(`^go\.work$`), "gotmpl": regexp.MustCompile(`^.*tmpl$`), + "go.s": regexp.MustCompile(`\.s$`), } // languageID returns the language identifier for the path p given the user // configured fileAssociations. -func languageID(p string, fileAssociations map[string]string) protocol.LanguageKind { +func languageID(p string, fileAssociations map[protocol.LanguageKind]string) protocol.LanguageKind { base := path.Base(p) for lang, re := range fileAssociations { re := regexp.MustCompile(re) if re.MatchString(base) { - return protocol.LanguageKind(lang) + return lang } } for lang, re := range defaultFileAssociations { if re.MatchString(base) { - return protocol.LanguageKind(lang) + return lang } } return "" diff --git a/gopls/internal/test/marker/testdata/definition/asm.txt b/gopls/internal/test/marker/testdata/definition/asm.txt new file mode 100644 index 00000000000..f0187d7e24a --- /dev/null +++ b/gopls/internal/test/marker/testdata/definition/asm.txt @@ -0,0 +1,33 @@ +This test exercises the Definition request in a Go assembly file. + +For now we support only references to package-level symbols defined in +the same package or a dependency. + +Repeatedly jumping to Definition on ff ping-pongs between the Go and +assembly declarations. + +-- go.mod -- +module example.com +go 1.18 + +-- a/a.go -- +package a + +import _ "fmt" +import _ "example.com/b" + +func ff() //@ loc(ffgo, re"()ff"), def("ff", ffasm) + +var _ = ff // pacify unusedfunc analyzer + +-- a/asm.s -- +// portable assembly + +TEXT ·ff(SB), $16 //@ loc(ffasm, "ff"), def("ff", ffgo) + CALL example·com∕b·B //@ def("com", bB) + JMP ·ff //@ def("ff", ffgo) + +-- b/b.go -- +package b + +func B() {} //@ loc(bB, re"()B") diff --git a/internal/expect/extract.go b/internal/expect/extract.go index 150a2afbbf6..8ad1cb259e5 100644 --- a/internal/expect/extract.go +++ b/internal/expect/extract.go @@ -8,7 +8,9 @@ import ( "fmt" "go/ast" "go/parser" + goscanner "go/scanner" "go/token" + "os" "path/filepath" "regexp" "strconv" @@ -32,21 +34,54 @@ type Identifier string // See the package documentation for details about the syntax of those // notes. func Parse(fset *token.FileSet, filename string, content []byte) ([]*Note, error) { - var src any - if content != nil { - src = content + if content == nil { + data, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + content = data } + switch filepath.Ext(filename) { + case ".s": + // The assembler uses a custom scanner, + // but the go/scanner package is close + // enough: we only want the comments. + file := fset.AddFile(filename, -1, len(content)) + var scan goscanner.Scanner + scan.Init(file, content, nil, goscanner.ScanComments) + + var notes []*Note + for { + pos, tok, lit := scan.Scan() + if tok == token.EOF { + break + } + if tok == token.COMMENT { + text, adjust := getAdjustedNote(lit) + if text == "" { + continue + } + parsed, err := parse(fset, pos+token.Pos(adjust), text) + if err != nil { + return nil, err + } + notes = append(notes, parsed...) + } + } + return notes, nil + case ".go": - // TODO: We should write this in terms of the scanner. + // TODO: We should write this in terms of the scanner, like the .s case above. // there are ways you can break the parser such that it will not add all the // comments to the ast, which may result in files where the tests are silently // not run. - file, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.AllErrors|parser.SkipObjectResolution) + file, err := parser.ParseFile(fset, filename, content, parser.ParseComments|parser.AllErrors|parser.SkipObjectResolution) if file == nil { return nil, err } return ExtractGo(fset, file) + case ".mod": file, err := modfile.Parse(filename, content, nil) if err != nil { @@ -64,6 +99,7 @@ func Parse(fset *token.FileSet, filename string, content []byte) ([]*Note, error note.Pos += token.Pos(f.Base()) } return notes, nil + case ".work": file, err := modfile.ParseWork(filename, content, nil) if err != nil { From 9f7a2b618a10d26b9bc935167355490a7c32a20b Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 20 Feb 2025 16:01:31 -0500 Subject: [PATCH 173/309] gopls/doc/features: tweak markdown Previously the seems to cause underlining of what follows; see https://github.com/golang/tools/blob/master/gopls/doc/features/diagnostics.md. This fix is kind of a stab in the dark. Change-Id: Ic552faae8d03b3d49c1a913ef7e3a145add5cfc4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651096 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/features/diagnostics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/doc/features/diagnostics.md b/gopls/doc/features/diagnostics.md index ceec607c123..6be7a43493a 100644 --- a/gopls/doc/features/diagnostics.md +++ b/gopls/doc/features/diagnostics.md @@ -51,7 +51,7 @@ build`. Gopls doesn't actually run the compiler; that would be too There is an optional third source of diagnostics: - + - **Compiler optimization details** are diagnostics that report details relevant to optimization decisions made by the Go From 33f1ed9242128736ca381ce86d10a5fc479aab4c Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Thu, 20 Feb 2025 10:41:48 -0800 Subject: [PATCH 174/309] gopls/go.mod: update dependencies following the v0.18.0 release This is an automated CL which updates the go.mod and go.sum. For golang/go#71607 Change-Id: Ic4d3e8174be60eca3f4799c0d3a99dd8f9017320 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651116 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Knyszek Auto-Submit: Gopher Robot --- gopls/go.mod | 12 ++++++------ gopls/go.sum | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/gopls/go.mod b/gopls/go.mod index 83620720ae6..f6a2b0a1e9a 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -10,20 +10,20 @@ require ( golang.org/x/mod v0.23.0 golang.org/x/sync v0.11.0 golang.org/x/sys v0.30.0 - golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9 + golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc golang.org/x/text v0.22.0 - golang.org/x/tools v0.28.0 - golang.org/x/vuln v1.1.3 + golang.org/x/tools v0.30.0 + golang.org/x/vuln v1.1.4 gopkg.in/yaml.v3 v3.0.1 - honnef.co/go/tools v0.5.1 + honnef.co/go/tools v0.6.0 mvdan.cc/gofumpt v0.7.0 - mvdan.cc/xurls/v2 v2.5.0 + mvdan.cc/xurls/v2 v2.6.0 ) require ( github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect github.com/google/safehtml v0.1.0 // indirect - golang.org/x/exp/typeparams v0.0.0-20241210194714-1829a127f884 // indirect + golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/gopls/go.sum b/gopls/go.sum index b2b3d925a78..ef93b2c4601 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -12,13 +12,13 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= -github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGKbLGBPtR/8/oO74W6hmz0qE5q0z9aqSAewaaM= +github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= -golang.org/x/exp/typeparams v0.0.0-20241210194714-1829a127f884 h1:1xaZTydL5Gsg78QharTwKfA9FY9CZ1VQj6D/AZEvHR0= -golang.org/x/exp/typeparams v0.0.0-20241210194714-1829a127f884/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= +golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa h1:Br3+0EZZohShrmVVc85znGpxw7Ca8hsUJlrdT/JQGw8= +golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:LKZHyeOpPuZcMgxeHjJp4p5yvxrCX1xDvH10zYHhjjQ= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= @@ -36,8 +36,8 @@ golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= -golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9 h1:L2k9GUV2TpQKVRGMjN94qfUMgUwOFimSQ6gipyJIjKw= -golang.org/x/telemetry v0.0.0-20241220003058-cc96b6e0d3d9/go.mod h1:8h4Hgq+jcTvCDv2+i7NrfWwpYHcESleo2nGHxLbFLJ4= +golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc h1:HS+G1Mhh2dxM8ObutfYKdjfD7zpkyeP/UxeRnJpIZtQ= +golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc/go.mod h1:bDzXkYUaHzz51CtDy5kh/jR4lgPxsdbqC37kp/dzhCc= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= @@ -46,16 +46,16 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= -golang.org/x/vuln v1.1.3 h1:NPGnvPOTgnjBc9HTaUx+nj+EaUYxl5SJOWqaDYGaFYw= -golang.org/x/vuln v1.1.3/go.mod h1:7Le6Fadm5FOqE9C926BCD0g12NWyhg7cxV4BwcPFuNY= +golang.org/x/vuln v1.1.4 h1:Ju8QsuyhX3Hk8ma3CesTbO8vfJD9EvUBgHvkxHBzj0I= +golang.org/x/vuln v1.1.4/go.mod h1:F+45wmU18ym/ca5PLTPLsSzr2KppzswxPP603ldA67s= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I= -honnef.co/go/tools v0.5.1/go.mod h1:e9irvo83WDG9/irijV44wr3tbhcFeRnfpVlRqVwpzMs= +honnef.co/go/tools v0.6.0 h1:TAODvD3knlq75WCp2nyGJtT4LeRV/o7NN9nYPeVJXf8= +honnef.co/go/tools v0.6.0/go.mod h1:3puzxxljPCe8RGJX7BIy1plGbxEOZni5mR2aXe3/uk4= mvdan.cc/gofumpt v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU= mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo= -mvdan.cc/xurls/v2 v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= -mvdan.cc/xurls/v2 v2.5.0/go.mod h1:yQgaGQ1rFtJUzkmKiHYSSfuQxqfYmd//X6PxvholpeE= +mvdan.cc/xurls/v2 v2.6.0 h1:3NTZpeTxYVWNSokW3MKeyVkz/j7uYXYiMtXRUfmjbgI= +mvdan.cc/xurls/v2 v2.6.0/go.mod h1:bCvEZ1XvdA6wDnxY7jPPjEmigDtvtvPXAD/Exa9IMSk= From 1f6c6d67720feea9eeba7e1eb23841e63f3ccc81 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 20 Feb 2025 23:31:50 +0100 Subject: [PATCH 175/309] gopls/doc: adjust nvim-lspconfig link target The file was renamed in the github.com/neovim/nvim-lspconfig repository. Change-Id: I89a8dcbbb31c24d77f0ca00934df1916b338d460 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651195 Reviewed-by: Robert Findley Auto-Submit: Robert Findley Auto-Submit: Tobias Klauser Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/vim.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/doc/vim.md b/gopls/doc/vim.md index e71482115ea..444a7d6ff31 100644 --- a/gopls/doc/vim.md +++ b/gopls/doc/vim.md @@ -230,5 +230,5 @@ require('lspconfig').gopls.setup({ [govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8 [nvim-docs]: https://neovim.io/doc/user/lsp.html [nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim -[nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#gopls +[nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md#gopls [nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115 From 96bfb60194183d530de41f887c48081f8c104a86 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 21 Feb 2025 09:36:24 -0500 Subject: [PATCH 176/309] gopls/internal/analysis/modernize: fix minmax bug The matcher for pattern 2 forgot to check that the IfStmt.Else subtree was nil, leading to unsound fixes. Updates golang/go#71847 Change-Id: I0919076c1af38012cedf3072ef5d1117e96a64b9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651375 Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/minmax.go | 2 +- .../analysis/modernize/testdata/src/minmax/minmax.go | 12 ++++++++++++ .../modernize/testdata/src/minmax/minmax.go.golden | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index 1466e767fc7..8888383afec 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -95,7 +95,7 @@ func minmax(pass *analysis.Pass) { }) } - } else if prev, ok := curIfStmt.PrevSibling(); ok && isSimpleAssign(prev.Node()) { + } else if prev, ok := curIfStmt.PrevSibling(); ok && isSimpleAssign(prev.Node()) && ifStmt.Else == nil { fassign := prev.Node().(*ast.AssignStmt) // Have: lhs0 = rhs0; if a < b { lhs = rhs } diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index 8fdc3bc2106..44ba7c9193a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -103,3 +103,15 @@ func nopeNotAMinimum(x, y int) int { } return y } + +// Regression test for https://github.com/golang/go/issues/71847#issuecomment-2673491596 +func nopeHasElseBlock(x int) int { + y := x + // Before, this was erroneously reduced to y = max(x, 0) + if y < 0 { + y = 0 + } else { + y += 2 + } + return y +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index 48e154729e7..df1d5180f8a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -80,3 +80,15 @@ func nopeNotAMinimum(x, y int) int { } return y } + +// Regression test for https://github.com/golang/go/issues/71847#issuecomment-2673491596 +func nopeHasElseBlock(x int) int { + y := x + // Before, this was erroneously reduced to y = max(x, 0) + if y < 0 { + y = 0 + } else { + y += 2 + } + return y +} From f95771e6301730bd96b5ece4dfb6df630c070e83 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 21 Feb 2025 09:43:42 -0500 Subject: [PATCH 177/309] gopls/go.mod: update to go1.24 No code changes yet. Change-Id: Ibdf2dfab2bf282aea4f1bb7d0787fb60d81ebbdb Reviewed-on: https://go-review.googlesource.com/c/tools/+/651395 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/go.mod | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gopls/go.mod b/gopls/go.mod index f6a2b0a1e9a..210943206b8 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -1,8 +1,6 @@ module golang.org/x/tools/gopls -// go 1.23.1 fixes some bugs in go/types Alias support (golang/go#68894, golang/go#68905). -// go 1.23.4 fixes a miscompilation of range-over-func (golang/go#70035). -go 1.23.4 +go 1.24.0 require ( github.com/google/go-cmp v0.6.0 From 8b85edcc2f1f820a72c251a20869722780356f0a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 21 Feb 2025 10:05:18 -0500 Subject: [PATCH 178/309] gopls/internal: use go1.24-isms This CL intentionally does not include any new API covered by the modernizers, whose fixes will be submitted separately. Surprisingly few changes in all. Change-Id: I0c45ed674fd80234e7c76823a23b8b3af3011835 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651376 Auto-Submit: Alan Donovan Reviewed-by: Robert Findley Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/directive.go | 2 +- gopls/internal/golang/pkgdoc.go | 17 +---------------- .../test/integration/misc/references_test.go | 2 +- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/gopls/internal/analysis/gofix/directive.go b/gopls/internal/analysis/gofix/directive.go index 796feb5189e..20c45313cfb 100644 --- a/gopls/internal/analysis/gofix/directive.go +++ b/gopls/internal/analysis/gofix/directive.go @@ -12,7 +12,7 @@ import ( // -- plundered from the future (CL 605517, issue #68021) -- -// TODO(adonovan): replace with ast.Directive after go1.24 (#68021). +// TODO(adonovan): replace with ast.Directive after go1.25 (#68021). // Beware of our local mods to handle analysistest // "want" comments on the same line. diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index a5f9cc97fa4..2faff1a1526 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -39,7 +39,6 @@ import ( "go/token" "go/types" "html" - "iter" "path/filepath" "slices" "strings" @@ -666,7 +665,7 @@ window.addEventListener('load', function() { cloneTparams(sig.RecvTypeParams()), cloneTparams(sig.TypeParams()), types.NewTuple(append( - slices.Collect(tupleVariables(sig.Params()))[:3], + slices.Collect(sig.Params().Variables())[:3], types.NewParam(0, nil, "", types.Typ[types.Invalid]))...), sig.Results(), false) // any final ...T parameter is truncated @@ -851,17 +850,3 @@ window.addEventListener('load', function() { return buf.Bytes(), nil } - -// tupleVariables returns a go1.23 iterator over the variables of a tuple type. -// -// Example: for v := range tuple.Variables() { ... } -// TODO(adonovan): use t.Variables in go1.24. -func tupleVariables(t *types.Tuple) iter.Seq[*types.Var] { - return func(yield func(v *types.Var) bool) { - for i := range t.Len() { - if !yield(t.At(i)) { - break - } - } - } -} diff --git a/gopls/internal/test/integration/misc/references_test.go b/gopls/internal/test/integration/misc/references_test.go index e84dcd71dc3..58fdb3c5cd8 100644 --- a/gopls/internal/test/integration/misc/references_test.go +++ b/gopls/internal/test/integration/misc/references_test.go @@ -126,7 +126,7 @@ var _ = unsafe.Slice(nil, 0) Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("a.go") - for _, name := range strings.Fields( + for name := range strings.FieldsSeq( "iota error int nil append iota Pointer Sizeof Alignof Add Slice") { loc := env.RegexpSearch("a.go", `\b`+name+`\b`) From 23211ff47d7fe7c3bf662f2a3bf33d9c0ba57f31 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 20 Feb 2025 22:18:10 +0000 Subject: [PATCH 179/309] gopls/internal/test/integration: better expectation failures A particularly tricky-to-diagnose marker test failure finally led me to address several long-standing TODOs: integration test expectations should identify their specific failure reason. Previously, we had been relying on a combination the State.String summary and LSP logs to debug failed expectations, but often it was not obvious from the test failure what condition actually failed. Now, expectations describe their failure, and composite expectations compose their component failures. Change-Id: I2533c8a35b4eb561f505fd3ed95fe55483340773 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651417 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- gopls/internal/test/integration/env.go | 107 ++---- .../internal/test/integration/expectation.go | 307 ++++++++++-------- 2 files changed, 194 insertions(+), 220 deletions(-) diff --git a/gopls/internal/test/integration/env.go b/gopls/internal/test/integration/env.go index c8a1b5043aa..f19a426316d 100644 --- a/gopls/internal/test/integration/env.go +++ b/gopls/internal/test/integration/env.go @@ -114,53 +114,16 @@ type workProgress struct { complete bool // seen 'end' } -// This method, provided for debugging, accesses mutable fields without a lock, -// so it must not be called concurrent with any State mutation. -func (s State) String() string { - var b strings.Builder - b.WriteString("#### log messages (see RPC logs for full text):\n") - for _, msg := range s.logs { - summary := fmt.Sprintf("%v: %q", msg.Type, msg.Message) - if len(summary) > 60 { - summary = summary[:57] + "..." - } - // Some logs are quite long, and since they should be reproduced in the RPC - // logs on any failure we include here just a short summary. - fmt.Fprint(&b, "\t"+summary+"\n") - } - b.WriteString("\n") - b.WriteString("#### diagnostics:\n") - for name, params := range s.diagnostics { - fmt.Fprintf(&b, "\t%s (version %d):\n", name, params.Version) - for _, d := range params.Diagnostics { - fmt.Fprintf(&b, "\t\t%d:%d [%s]: %s\n", d.Range.Start.Line, d.Range.Start.Character, d.Source, d.Message) - } - } - b.WriteString("\n") - b.WriteString("#### outstanding work:\n") - for token, state := range s.work { - if state.complete { - continue - } - name := state.title - if name == "" { - name = fmt.Sprintf("!NO NAME(token: %s)", token) - } - fmt.Fprintf(&b, "\t%s: %.2f\n", name, state.percent) - } - b.WriteString("#### completed work:\n") - for name, count := range s.completedWork { - fmt.Fprintf(&b, "\t%s: %d\n", name, count) - } - return b.String() +type awaitResult struct { + verdict Verdict + reason string } -// A condition is satisfied when all expectations are simultaneously -// met. At that point, the 'met' channel is closed. On any failure, err is set -// and the failed channel is closed. +// A condition is satisfied when its expectation is [Met] or [Unmeetable]. The +// result is sent on the verdict channel. type condition struct { - expectations []Expectation - verdict chan Verdict + expectation Expectation + verdict chan awaitResult } func (a *Awaiter) onDiagnostics(_ context.Context, d *protocol.PublishDiagnosticsParams) error { @@ -334,27 +297,13 @@ func (a *Awaiter) onUnregisterCapability(_ context.Context, m *protocol.Unregist func (a *Awaiter) checkConditionsLocked() { for id, condition := range a.waiters { - if v, _ := checkExpectations(a.state, condition.expectations); v != Unmet { + if v, why := condition.expectation.Check(a.state); v != Unmet { delete(a.waiters, id) - condition.verdict <- v + condition.verdict <- awaitResult{v, why} } } } -// checkExpectations reports whether s meets all expectations. -func checkExpectations(s State, expectations []Expectation) (Verdict, string) { - finalVerdict := Met - var summary strings.Builder - for _, e := range expectations { - v := e.Check(s) - if v > finalVerdict { - finalVerdict = v - } - fmt.Fprintf(&summary, "%v: %s\n", v, e.Description) - } - return finalVerdict, summary.String() -} - // Await blocks until the given expectations are all simultaneously met. // // Generally speaking Await should be avoided because it blocks indefinitely if @@ -363,7 +312,7 @@ func checkExpectations(s State, expectations []Expectation) (Verdict, string) { // waiting. func (e *Env) Await(expectations ...Expectation) { e.T.Helper() - if err := e.Awaiter.Await(e.Ctx, expectations...); err != nil { + if err := e.Awaiter.Await(e.Ctx, AllOf(expectations...)); err != nil { e.T.Fatal(err) } } @@ -371,30 +320,30 @@ func (e *Env) Await(expectations ...Expectation) { // OnceMet blocks until the precondition is met by the state or becomes // unmeetable. If it was met, OnceMet checks that the state meets all // expectations in mustMeets. -func (e *Env) OnceMet(precondition Expectation, mustMeets ...Expectation) { +func (e *Env) OnceMet(pre Expectation, mustMeets ...Expectation) { e.T.Helper() - e.Await(OnceMet(precondition, mustMeets...)) + e.Await(OnceMet(pre, AllOf(mustMeets...))) } // Await waits for all expectations to simultaneously be met. It should only be // called from the main test goroutine. -func (a *Awaiter) Await(ctx context.Context, expectations ...Expectation) error { +func (a *Awaiter) Await(ctx context.Context, expectation Expectation) error { a.mu.Lock() // Before adding the waiter, we check if the condition is currently met or // failed to avoid a race where the condition was realized before Await was // called. - switch verdict, summary := checkExpectations(a.state, expectations); verdict { + switch verdict, why := expectation.Check(a.state); verdict { case Met: a.mu.Unlock() return nil case Unmeetable: - err := fmt.Errorf("unmeetable expectations:\n%s\nstate:\n%v", summary, a.state) + err := fmt.Errorf("unmeetable expectation:\n%s\nreason:\n%s", indent(expectation.Description), indent(why)) a.mu.Unlock() return err } cond := &condition{ - expectations: expectations, - verdict: make(chan Verdict), + expectation: expectation, + verdict: make(chan awaitResult), } a.waiters[nextAwaiterRegistration.Add(1)] = cond a.mu.Unlock() @@ -403,19 +352,17 @@ func (a *Awaiter) Await(ctx context.Context, expectations ...Expectation) error select { case <-ctx.Done(): err = ctx.Err() - case v := <-cond.verdict: - if v != Met { - err = fmt.Errorf("condition has final verdict %v", v) + case res := <-cond.verdict: + if res.verdict != Met { + err = fmt.Errorf("the following condition is %s:\n%s\nreason:\n%s", + res.verdict, indent(expectation.Description), indent(res.reason)) } } - a.mu.Lock() - defer a.mu.Unlock() - _, summary := checkExpectations(a.state, expectations) + return err +} - // Debugging an unmet expectation can be tricky, so we put some effort into - // nicely formatting the failure. - if err != nil { - return fmt.Errorf("waiting on:\n%s\nerr:%v\n\nstate:\n%v", summary, err, a.state) - } - return nil +// indent indents all lines of msg, including the first. +func indent(msg string) string { + const prefix = " " + return prefix + strings.ReplaceAll(msg, "\n", "\n"+prefix) } diff --git a/gopls/internal/test/integration/expectation.go b/gopls/internal/test/integration/expectation.go index fdfca90796e..70a16fd6b3a 100644 --- a/gopls/internal/test/integration/expectation.go +++ b/gopls/internal/test/integration/expectation.go @@ -5,14 +5,17 @@ package integration import ( + "bytes" "fmt" + "maps" "regexp" - "sort" + "slices" "strings" "github.com/google/go-cmp/cmp" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/server" + "golang.org/x/tools/gopls/internal/util/constraints" ) var ( @@ -55,16 +58,11 @@ func (v Verdict) String() string { // // Expectations are combinators. By composing them, tests may express // complex expectations in terms of simpler ones. -// -// TODO(rfindley): as expectations are combined, it becomes harder to identify -// why they failed. A better signature for Check would be -// -// func(State) (Verdict, string) -// -// returning a reason for the verdict that can be composed similarly to -// descriptions. type Expectation struct { - Check func(State) Verdict + // Check returns the verdict of this expectation for the given state. + // If the vertict is not [Met], the second result should return a reason + // that the verdict is not (yet) met. + Check func(State) (Verdict, string) // Description holds a noun-phrase identifying what the expectation checks. // @@ -74,117 +72,117 @@ type Expectation struct { // OnceMet returns an Expectation that, once the precondition is met, asserts // that mustMeet is met. -func OnceMet(precondition Expectation, mustMeets ...Expectation) Expectation { - check := func(s State) Verdict { - switch pre := precondition.Check(s); pre { - case Unmeetable: - return Unmeetable +func OnceMet(pre, post Expectation) Expectation { + check := func(s State) (Verdict, string) { + switch v, why := pre.Check(s); v { + case Unmeetable, Unmet: + return v, fmt.Sprintf("precondition is %s: %s", v, why) case Met: - for _, mustMeet := range mustMeets { - verdict := mustMeet.Check(s) - if verdict != Met { - return Unmeetable - } + v, why := post.Check(s) + if v != Met { + return Unmeetable, fmt.Sprintf("postcondition is not met:\n%s", indent(why)) } - return Met + return Met, "" default: - return Unmet + panic(fmt.Sprintf("unknown precondition verdict %s", v)) } } - description := describeExpectations(mustMeets...) + desc := fmt.Sprintf("once the following is met:\n%s\nmust have:\n%s", + indent(pre.Description), indent(post.Description)) return Expectation{ Check: check, - Description: fmt.Sprintf("once %q is met, must have:\n%s", precondition.Description, description), - } -} - -func describeExpectations(expectations ...Expectation) string { - var descriptions []string - for _, e := range expectations { - descriptions = append(descriptions, e.Description) + Description: desc, } - return strings.Join(descriptions, "\n") } // Not inverts the sense of an expectation: a met expectation is unmet, and an // unmet expectation is met. func Not(e Expectation) Expectation { - check := func(s State) Verdict { - switch v := e.Check(s); v { + check := func(s State) (Verdict, string) { + switch v, _ := e.Check(s); v { case Met: - return Unmet + return Unmet, "condition unexpectedly satisfied" case Unmet, Unmeetable: - return Met + return Met, "" default: panic(fmt.Sprintf("unexpected verdict %v", v)) } } - description := describeExpectations(e) return Expectation{ Check: check, - Description: fmt.Sprintf("not: %s", description), + Description: fmt.Sprintf("not: %s", e.Description), } } // AnyOf returns an expectation that is satisfied when any of the given // expectations is met. func AnyOf(anyOf ...Expectation) Expectation { - check := func(s State) Verdict { + if len(anyOf) == 1 { + return anyOf[0] // avoid unnecessary boilerplate + } + check := func(s State) (Verdict, string) { for _, e := range anyOf { - verdict := e.Check(s) + verdict, _ := e.Check(s) if verdict == Met { - return Met + return Met, "" } } - return Unmet + return Unmet, "none of the expectations were met" } description := describeExpectations(anyOf...) return Expectation{ Check: check, - Description: fmt.Sprintf("Any of:\n%s", description), + Description: fmt.Sprintf("any of:\n%s", description), } } // AllOf expects that all given expectations are met. -// -// TODO(rfindley): the problem with these types of combinators (OnceMet, AnyOf -// and AllOf) is that we lose the information of *why* they failed: the Awaiter -// is not smart enough to look inside. -// -// Refactor the API such that the Check function is responsible for explaining -// why an expectation failed. This should allow us to significantly improve -// test output: we won't need to summarize state at all, as the verdict -// explanation itself should describe clearly why the expectation not met. func AllOf(allOf ...Expectation) Expectation { - check := func(s State) Verdict { - verdict := Met + if len(allOf) == 1 { + return allOf[0] // avoid unnecessary boilerplate + } + check := func(s State) (Verdict, string) { + var ( + verdict = Met + reason string + ) for _, e := range allOf { - if v := e.Check(s); v > verdict { + v, why := e.Check(s) + if v > verdict { verdict = v + reason = why } } - return verdict + return verdict, reason } - description := describeExpectations(allOf...) + desc := describeExpectations(allOf...) return Expectation{ Check: check, - Description: fmt.Sprintf("All of:\n%s", description), + Description: fmt.Sprintf("all of:\n%s", indent(desc)), } } +func describeExpectations(expectations ...Expectation) string { + var descriptions []string + for _, e := range expectations { + descriptions = append(descriptions, e.Description) + } + return strings.Join(descriptions, "\n") +} + // ReadDiagnostics is an Expectation that stores the current diagnostics for // fileName in into, whenever it is evaluated. // // It can be used in combination with OnceMet or AfterChange to capture the // state of diagnostics when other expectations are satisfied. func ReadDiagnostics(fileName string, into *protocol.PublishDiagnosticsParams) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { diags, ok := s.diagnostics[fileName] if !ok { - return Unmeetable + return Unmeetable, fmt.Sprintf("no diagnostics for %q", fileName) } *into = *diags - return Met + return Met, "" } return Expectation{ Check: check, @@ -198,13 +196,10 @@ func ReadDiagnostics(fileName string, into *protocol.PublishDiagnosticsParams) E // It can be used in combination with OnceMet or AfterChange to capture the // state of diagnostics when other expectations are satisfied. func ReadAllDiagnostics(into *map[string]*protocol.PublishDiagnosticsParams) Expectation { - check := func(s State) Verdict { - allDiags := make(map[string]*protocol.PublishDiagnosticsParams) - for name, diags := range s.diagnostics { - allDiags[name] = diags - } + check := func(s State) (Verdict, string) { + allDiags := maps.Clone(s.diagnostics) *into = allDiags - return Met + return Met, "" } return Expectation{ Check: check, @@ -215,13 +210,13 @@ func ReadAllDiagnostics(into *map[string]*protocol.PublishDiagnosticsParams) Exp // ShownDocument asserts that the client has received a // ShowDocumentRequest for the given URI. func ShownDocument(uri protocol.URI) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { for _, params := range s.showDocument { if params.URI == uri { - return Met + return Met, "" } } - return Unmet + return Unmet, fmt.Sprintf("no ShowDocumentRequest received for %s", uri) } return Expectation{ Check: check, @@ -236,9 +231,9 @@ func ShownDocument(uri protocol.URI) Expectation { // capture the set of showDocument requests when other expectations // are satisfied. func ShownDocuments(into *[]*protocol.ShowDocumentParams) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { *into = append(*into, s.showDocument...) - return Met + return Met, "" } return Expectation{ Check: check, @@ -247,31 +242,39 @@ func ShownDocuments(into *[]*protocol.ShowDocumentParams) Expectation { } // NoShownMessage asserts that the editor has not received a ShowMessage. -func NoShownMessage(subString string) Expectation { - check := func(s State) Verdict { +func NoShownMessage(containing string) Expectation { + check := func(s State) (Verdict, string) { for _, m := range s.showMessage { - if strings.Contains(m.Message, subString) { - return Unmeetable + if strings.Contains(m.Message, containing) { + // Format the message (which may contain newlines) as a block quote. + msg := fmt.Sprintf("\"\"\"\n%s\n\"\"\"", strings.TrimSpace(m.Message)) + return Unmeetable, fmt.Sprintf("observed the following message:\n%s", indent(msg)) } } - return Met + return Met, "" + } + var desc string + if containing != "" { + desc = fmt.Sprintf("received no ShowMessage containing %q", containing) + } else { + desc = "received no ShowMessage requests" } return Expectation{ Check: check, - Description: fmt.Sprintf("no ShowMessage received containing %q", subString), + Description: desc, } } // ShownMessage asserts that the editor has received a ShowMessageRequest // containing the given substring. func ShownMessage(containing string) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { for _, m := range s.showMessage { if strings.Contains(m.Message, containing) { - return Met + return Met, "" } } - return Unmet + return Unmet, fmt.Sprintf("no ShowMessage containing %q", containing) } return Expectation{ Check: check, @@ -281,22 +284,22 @@ func ShownMessage(containing string) Expectation { // ShownMessageRequest asserts that the editor has received a // ShowMessageRequest with message matching the given regular expression. -func ShownMessageRequest(messageRegexp string) Expectation { - msgRE := regexp.MustCompile(messageRegexp) - check := func(s State) Verdict { +func ShownMessageRequest(matchingRegexp string) Expectation { + msgRE := regexp.MustCompile(matchingRegexp) + check := func(s State) (Verdict, string) { if len(s.showMessageRequest) == 0 { - return Unmet + return Unmet, "no ShowMessageRequest have been received" } for _, m := range s.showMessageRequest { if msgRE.MatchString(m.Message) { - return Met + return Met, "" } } - return Unmet + return Unmet, fmt.Sprintf("no ShowMessageRequest (out of %d) match %q", len(s.showMessageRequest), matchingRegexp) } return Expectation{ Check: check, - Description: fmt.Sprintf("ShowMessageRequest matching %q", messageRegexp), + Description: fmt.Sprintf("ShowMessageRequest matching %q", matchingRegexp), } } @@ -328,9 +331,7 @@ func (e *Env) DoneDiagnosingChanges() Expectation { } // Sort for stability. - sort.Slice(expected, func(i, j int) bool { - return expected[i] < expected[j] - }) + slices.Sort(expected) var all []Expectation for _, source := range expected { @@ -411,15 +412,16 @@ func (e *Env) DoneWithClose() Expectation { // // See CompletedWork. func StartedWork(title string, atLeast uint64) Expectation { - check := func(s State) Verdict { - if s.startedWork[title] >= atLeast { - return Met + check := func(s State) (Verdict, string) { + started := s.startedWork[title] + if started >= atLeast { + return Met, "" } - return Unmet + return Unmet, fmt.Sprintf("started work %d %s", started, pluralize("time", started)) } return Expectation{ Check: check, - Description: fmt.Sprintf("started work %q at least %d time(s)", title, atLeast), + Description: fmt.Sprintf("started work %q at least %d %s", title, atLeast, pluralize("time", atLeast)), } } @@ -428,16 +430,16 @@ func StartedWork(title string, atLeast uint64) Expectation { // Since the Progress API doesn't include any hidden metadata, we must use the // progress notification title to identify the work we expect to be completed. func CompletedWork(title string, count uint64, atLeast bool) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { completed := s.completedWork[title] if completed == count || atLeast && completed > count { - return Met + return Met, "" } - return Unmet + return Unmet, fmt.Sprintf("completed %d %s", completed, pluralize("time", completed)) } - desc := fmt.Sprintf("completed work %q %v times", title, count) + desc := fmt.Sprintf("completed work %q %v %s", title, count, pluralize("time", count)) if atLeast { - desc = fmt.Sprintf("completed work %q at least %d time(s)", title, count) + desc = fmt.Sprintf("completed work %q at least %d %s", title, count, pluralize("time", count)) } return Expectation{ Check: check, @@ -445,6 +447,14 @@ func CompletedWork(title string, count uint64, atLeast bool) Expectation { } } +// pluralize adds an 's' suffix to name if n > 1. +func pluralize[T constraints.Integer](name string, n T) string { + if n > 1 { + return name + "s" + } + return name +} + type WorkStatus struct { // Last seen message from either `begin` or `report` progress. Msg string @@ -459,24 +469,23 @@ type WorkStatus struct { // If the token is not a progress token that the client has seen, this // expectation is Unmeetable. func CompletedProgressToken(token protocol.ProgressToken, into *WorkStatus) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { work, ok := s.work[token] if !ok { - return Unmeetable // TODO(rfindley): refactor to allow the verdict to explain this result + return Unmeetable, "no matching work items" } if work.complete { if into != nil { into.Msg = work.msg into.EndMsg = work.endMsg } - return Met + return Met, "" } - return Unmet + return Unmet, fmt.Sprintf("work is not complete; last message: %q", work.msg) } - desc := fmt.Sprintf("completed work for token %v", token) return Expectation{ Check: check, - Description: desc, + Description: fmt.Sprintf("completed work for token %v", token), } } @@ -488,28 +497,27 @@ func CompletedProgressToken(token protocol.ProgressToken, into *WorkStatus) Expe // This expectation is a vestige of older workarounds for asynchronous command // execution. func CompletedProgress(title string, into *WorkStatus) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { var work *workProgress for _, w := range s.work { if w.title == title { if work != nil { - // TODO(rfindley): refactor to allow the verdict to explain this result - return Unmeetable // multiple matches + return Unmeetable, "multiple matching work items" } work = w } } if work == nil { - return Unmeetable // zero matches + return Unmeetable, "no matching work items" } if work.complete { if into != nil { into.Msg = work.msg into.EndMsg = work.endMsg } - return Met + return Met, "" } - return Unmet + return Unmet, fmt.Sprintf("work is not complete; last message: %q", work.msg) } desc := fmt.Sprintf("exactly 1 completed workDoneProgress with title %v", title) return Expectation{ @@ -522,16 +530,16 @@ func CompletedProgress(title string, into *WorkStatus) Expectation { // be an exact match, whereas the given msg must only be contained in the work // item's message. func OutstandingWork(title, msg string) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { for _, work := range s.work { if work.complete { continue } if work.title == title && strings.Contains(work.msg, msg) { - return Met + return Met, "" } } - return Unmet + return Unmet, "no matching work" } return Expectation{ Check: check, @@ -548,7 +556,7 @@ func OutstandingWork(title, msg string) Expectation { // TODO(rfindley): consider refactoring to treat outstanding work the same way // we treat diagnostics: with an algebra of filters. func NoOutstandingWork(ignore func(title, msg string) bool) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { for _, w := range s.work { if w.complete { continue @@ -563,9 +571,9 @@ func NoOutstandingWork(ignore func(title, msg string) bool) Expectation { if ignore != nil && ignore(w.title, w.msg) { continue } - return Unmet + return Unmet, fmt.Sprintf("found outstanding work %q: %q", w.title, w.msg) } - return Met + return Met, "" } return Expectation{ Check: check, @@ -600,7 +608,7 @@ func LogMatching(typ protocol.MessageType, re string, count int, atLeast bool) E if err != nil { panic(err) } - check := func(state State) Verdict { + check := func(state State) (Verdict, string) { var found int for _, msg := range state.logs { if msg.Type == typ && rec.Match([]byte(msg.Message)) { @@ -609,14 +617,15 @@ func LogMatching(typ protocol.MessageType, re string, count int, atLeast bool) E } // Check for an exact or "at least" match. if found == count || (found >= count && atLeast) { - return Met + return Met, "" } // If we require an exact count, and have received more than expected, the // expectation can never be met. + verdict := Unmet if found > count && !atLeast { - return Unmeetable + verdict = Unmeetable } - return Unmet + return verdict, fmt.Sprintf("found %d matching logs", found) } desc := fmt.Sprintf("log message matching %q expected %v times", re, count) if atLeast { @@ -640,20 +649,24 @@ func NoLogMatching(typ protocol.MessageType, re string) Expectation { panic(err) } } - check := func(state State) Verdict { + check := func(state State) (Verdict, string) { for _, msg := range state.logs { if msg.Type != typ { continue } if r == nil || r.Match([]byte(msg.Message)) { - return Unmeetable + return Unmeetable, fmt.Sprintf("found matching log %q", msg.Message) } } - return Met + return Met, "" + } + desc := fmt.Sprintf("no %s log messages", typ) + if re != "" { + desc += fmt.Sprintf(" matching %q", re) } return Expectation{ Check: check, - Description: fmt.Sprintf("no log message matching %q", re), + Description: desc, } } @@ -673,18 +686,18 @@ func NoFileWatchMatching(re string) Expectation { } } -func checkFileWatch(re string, onMatch, onNoMatch Verdict) func(State) Verdict { +func checkFileWatch(re string, onMatch, onNoMatch Verdict) func(State) (Verdict, string) { rec := regexp.MustCompile(re) - return func(s State) Verdict { + return func(s State) (Verdict, string) { r := s.registeredCapabilities["workspace/didChangeWatchedFiles"] watchers := jsonProperty(r.RegisterOptions, "watchers").([]any) for _, watcher := range watchers { pattern := jsonProperty(watcher, "globPattern").(string) if rec.MatchString(pattern) { - return onMatch + return onMatch, fmt.Sprintf("matches watcher pattern %q", pattern) } } - return onNoMatch + return onNoMatch, "no matching watchers" } } @@ -707,10 +720,14 @@ func jsonProperty(obj any, path ...string) any { return jsonProperty(m[path[0]], path[1:]...) } +func formatDiagnostic(d protocol.Diagnostic) string { + return fmt.Sprintf("%d:%d [%s]: %s\n", d.Range.Start.Line, d.Range.Start.Character, d.Source, d.Message) +} + // Diagnostics asserts that there is at least one diagnostic matching the given // filters. func Diagnostics(filters ...DiagnosticFilter) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { diags := flattenDiagnostics(s) for _, filter := range filters { var filtered []flatDiagnostic @@ -720,14 +737,22 @@ func Diagnostics(filters ...DiagnosticFilter) Expectation { } } if len(filtered) == 0 { - // TODO(rfindley): if/when expectations describe their own failure, we - // can provide more useful information here as to which filter caused - // the failure. - return Unmet + // Reprinting the description of the filters is too verbose. + // + // We can probably do better here, but for now just format the + // diagnostics. + var b bytes.Buffer + for name, params := range s.diagnostics { + fmt.Fprintf(&b, "\t%s (version %d):\n", name, params.Version) + for _, d := range params.Diagnostics { + fmt.Fprintf(&b, "\t\t%s", formatDiagnostic(d)) + } + } + return Unmet, fmt.Sprintf("diagnostics:\n%s", b.String()) } diags = filtered } - return Met + return Met, "" } var descs []string for _, filter := range filters { @@ -743,7 +768,7 @@ func Diagnostics(filters ...DiagnosticFilter) Expectation { // filters. Notably, if no filters are supplied this assertion checks that // there are no diagnostics at all, for any file. func NoDiagnostics(filters ...DiagnosticFilter) Expectation { - check := func(s State) Verdict { + check := func(s State) (Verdict, string) { diags := flattenDiagnostics(s) for _, filter := range filters { var filtered []flatDiagnostic @@ -755,9 +780,11 @@ func NoDiagnostics(filters ...DiagnosticFilter) Expectation { diags = filtered } if len(diags) > 0 { - return Unmet + d := diags[0] + why := fmt.Sprintf("have diagnostic: %s: %v", d.name, formatDiagnostic(d.diag)) + return Unmet, why } - return Met + return Met, "" } var descs []string for _, filter := range filters { From f2beb33b192b2c3cfca5cc80b88d1d46abc058a7 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 21 Feb 2025 17:47:43 +0000 Subject: [PATCH 180/309] gopls: temporarily reinstate the "Structured" hover kind As described in golang/go#71879, the removal of the experimental "Structured" hover kind unexpectedly broke vim-go. Reinstate support for this setting, with tests, so that we can proceed with its deprecation more cautiously. For golang/go#71879 Change-Id: I6d22852aa10126c84b66f4345fbbdcf4cefbd182 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651238 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- gopls/doc/settings.md | 3 + gopls/internal/doc/api.json | 4 + gopls/internal/golang/hover.go | 136 ++++++++++-------- gopls/internal/settings/settings.go | 14 +- gopls/internal/settings/settings_test.go | 16 +-- .../test/marker/testdata/hover/json.txt | 33 +++++ 6 files changed, 135 insertions(+), 71 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/hover/json.txt diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index d989b2d19b9..7aeab79a575 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -428,6 +428,9 @@ Must be one of: * `"FullDocumentation"` * `"NoDocumentation"` * `"SingleLine"` +* `"Structured"` is a misguided experimental setting that returns a JSON +hover format. This setting should not be used, as it will be removed in a +future release of gopls. * `"SynopsisDocumentation"` Default: `"FullDocumentation"`. diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 629e45ff766..b6e53d18558 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -134,6 +134,10 @@ "Value": "\"SingleLine\"", "Doc": "" }, + { + "Value": "\"Structured\"", + "Doc": "`\"Structured\"` is a misguided experimental setting that returns a JSON\nhover format. This setting should not be used, as it will be removed in a\nfuture release of gopls.\n" + }, { "Value": "\"SynopsisDocumentation\"", "Doc": "" diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index 7fc584f2c1a..cda79dcadb8 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -7,6 +7,7 @@ package golang import ( "bytes" "context" + "encoding/json" "fmt" "go/ast" "go/constant" @@ -48,37 +49,47 @@ import ( // It is formatted in one of several formats as determined by the // HoverKind setting. type hoverResult struct { - // synopsis is a single sentence synopsis of the symbol's documentation. + // The fields below are exported to define the JSON hover format. + // TODO(golang/go#70233): (re)remove support for JSON hover. + + // Synopsis is a single sentence Synopsis of the symbol's documentation. // - // TODO(adonovan): in what syntax? It (usually) comes from doc.synopsis, + // TODO(adonovan): in what syntax? It (usually) comes from doc.Synopsis, // which produces "Text" form, but it may be fed to // DocCommentToMarkdown, which expects doc comment syntax. - synopsis string + Synopsis string - // fullDocumentation is the symbol's full documentation. - fullDocumentation string + // FullDocumentation is the symbol's full documentation. + FullDocumentation string - // signature is the symbol's signature. - signature string + // Signature is the symbol's Signature. + Signature string - // singleLine is a single line describing the symbol. + // SingleLine is a single line describing the symbol. // This is recommended only for use in clients that show a single line for hover. - singleLine string + SingleLine string - // symbolName is the human-readable name to use for the symbol in links. - symbolName string + // SymbolName is the human-readable name to use for the symbol in links. + SymbolName string - // linkPath is the path of the package enclosing the given symbol, + // LinkPath is the path of the package enclosing the given symbol, // with the module portion (if any) replaced by "module@version". // // For example: "github.com/google/go-github/v48@v48.1.0/github". // - // Use LinkTarget + "/" + linkPath + "#" + LinkAnchor to form a pkgsite URL. - linkPath string + // Use LinkTarget + "/" + LinkPath + "#" + LinkAnchor to form a pkgsite URL. + LinkPath string - // linkAnchor is the pkg.go.dev link anchor for the given symbol. + // LinkAnchor is the pkg.go.dev link anchor for the given symbol. // For example, the "Node" part of "pkg.go.dev/go/ast#Node". - linkAnchor string + LinkAnchor string + + // New fields go below, and are unexported. The existing + // exported fields are underspecified and have already + // constrained our movements too much. A detailed JSON + // interface might be nice, but it needs a design and a + // precise specification. + // TODO(golang/go#70233): (re)deprecate the JSON hover output. // typeDecl is the declaration syntax for a type, // or "" for a non-type. @@ -284,9 +295,9 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro typesinternal.SetVarKind(v, typesinternal.LocalVar) signature := types.ObjectString(v, qual) return *hoverRange, &hoverResult{ - signature: signature, - singleLine: signature, - symbolName: v.Name(), + Signature: signature, + SingleLine: signature, + SymbolName: v.Name(), }, nil } @@ -615,13 +626,13 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro } return *hoverRange, &hoverResult{ - synopsis: doc.Synopsis(docText), - fullDocumentation: docText, - singleLine: singleLineSignature, - symbolName: linkName, - signature: signature, - linkPath: linkPath, - linkAnchor: anchor, + Synopsis: doc.Synopsis(docText), + FullDocumentation: docText, + SingleLine: singleLineSignature, + SymbolName: linkName, + Signature: signature, + LinkPath: linkPath, + LinkAnchor: anchor, typeDecl: typeDecl, methods: methods, promotedFields: fields, @@ -638,8 +649,8 @@ func hoverBuiltin(ctx context.Context, snapshot *cache.Snapshot, obj types.Objec if obj.Name() == "Error" { signature := obj.String() return &hoverResult{ - signature: signature, - singleLine: signature, + Signature: signature, + SingleLine: signature, // TODO(rfindley): these are better than the current behavior. // SymbolName: "(error).Error", // LinkPath: "builtin", @@ -682,13 +693,13 @@ func hoverBuiltin(ctx context.Context, snapshot *cache.Snapshot, obj types.Objec docText := comment.Text() return &hoverResult{ - synopsis: doc.Synopsis(docText), - fullDocumentation: docText, - signature: signature, - singleLine: obj.String(), - symbolName: obj.Name(), - linkPath: "builtin", - linkAnchor: obj.Name(), + Synopsis: doc.Synopsis(docText), + FullDocumentation: docText, + Signature: signature, + SingleLine: obj.String(), + SymbolName: obj.Name(), + LinkPath: "builtin", + LinkAnchor: obj.Name(), }, nil } @@ -740,9 +751,9 @@ func hoverImport(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Packa docText := comment.Text() return rng, &hoverResult{ - signature: "package " + string(impMetadata.Name), - synopsis: doc.Synopsis(docText), - fullDocumentation: docText, + Signature: "package " + string(impMetadata.Name), + Synopsis: doc.Synopsis(docText), + FullDocumentation: docText, }, nil } @@ -798,9 +809,9 @@ func hoverPackageName(pkg *cache.Package, pgf *parsego.File) (protocol.Range, *h } return rng, &hoverResult{ - signature: "package " + string(pkg.Metadata().Name), - synopsis: doc.Synopsis(docText), - fullDocumentation: docText, + Signature: "package " + string(pkg.Metadata().Name), + Synopsis: doc.Synopsis(docText), + FullDocumentation: docText, footer: footer, }, nil } @@ -926,8 +937,8 @@ func hoverLit(pgf *parsego.File, lit *ast.BasicLit, pos token.Pos) (protocol.Ran } hover := b.String() return rng, &hoverResult{ - synopsis: hover, - fullDocumentation: hover, + Synopsis: hover, + FullDocumentation: hover, }, nil } @@ -966,7 +977,7 @@ func hoverReturnStatement(pgf *parsego.File, path []ast.Node, ret *ast.ReturnStm } buf.WriteByte(')') return rng, &hoverResult{ - signature: buf.String(), + Signature: buf.String(), }, nil } @@ -1005,9 +1016,9 @@ func hoverEmbed(fh file.Handle, rng protocol.Range, pattern string) (protocol.Ra } res := &hoverResult{ - signature: fmt.Sprintf("Embedding %q", pattern), - synopsis: s.String(), - fullDocumentation: s.String(), + Signature: fmt.Sprintf("Embedding %q", pattern), + Synopsis: s.String(), + FullDocumentation: s.String(), } return rng, res, nil } @@ -1242,10 +1253,17 @@ func formatHover(h *hoverResult, options *settings.Options, pkgURL func(path Pac switch options.HoverKind { case settings.SingleLine: - return h.singleLine, nil + return h.SingleLine, nil case settings.NoDocumentation: - return maybeFenced(h.signature), nil + return maybeFenced(h.Signature), nil + + case settings.Structured: + b, err := json.Marshal(h) + if err != nil { + return "", err + } + return string(b), nil case settings.SynopsisDocumentation, settings.FullDocumentation: var sections [][]string // assembled below @@ -1256,20 +1274,20 @@ func formatHover(h *hoverResult, options *settings.Options, pkgURL func(path Pac // but not Signature, which is redundant (= TypeDecl + "\n" + Methods). // For all other symbols, we display Signature; // TypeDecl and Methods are empty. - // (Now that JSON is no more, we could rationalize this.) + // TODO(golang/go#70233): When JSON is no more, we could rationalize this. if h.typeDecl != "" { sections = append(sections, []string{maybeFenced(h.typeDecl)}) } else { - sections = append(sections, []string{maybeFenced(h.signature)}) + sections = append(sections, []string{maybeFenced(h.Signature)}) } // Doc section. var doc string switch options.HoverKind { case settings.SynopsisDocumentation: - doc = h.synopsis + doc = h.Synopsis case settings.FullDocumentation: - doc = h.fullDocumentation + doc = h.FullDocumentation } if options.PreferredContentFormat == protocol.Markdown { doc = DocCommentToMarkdown(doc, options) @@ -1392,7 +1410,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol { // If pkgURL is non-nil, it should be used to generate doc links. func formatLink(h *hoverResult, options *settings.Options, pkgURL func(path PackagePath, fragment string) protocol.URI) string { - if options.LinksInHover == settings.LinksInHover_None || h.linkPath == "" { + if options.LinksInHover == settings.LinksInHover_None || h.LinkPath == "" { return "" } var url protocol.URI @@ -1400,26 +1418,26 @@ func formatLink(h *hoverResult, options *settings.Options, pkgURL func(path Pack if pkgURL != nil { // LinksInHover == "gopls" // Discard optional module version portion. // (Ideally the hoverResult would retain the structure...) - path := h.linkPath - if module, versionDir, ok := strings.Cut(h.linkPath, "@"); ok { + path := h.LinkPath + if module, versionDir, ok := strings.Cut(h.LinkPath, "@"); ok { // "module@version/dir" path = module if _, dir, ok := strings.Cut(versionDir, "/"); ok { path += "/" + dir } } - url = pkgURL(PackagePath(path), h.linkAnchor) + url = pkgURL(PackagePath(path), h.LinkAnchor) caption = "in gopls doc viewer" } else { if options.LinkTarget == "" { return "" } - url = cache.BuildLink(options.LinkTarget, h.linkPath, h.linkAnchor) + url = cache.BuildLink(options.LinkTarget, h.LinkPath, h.LinkAnchor) caption = "on " + options.LinkTarget } switch options.PreferredContentFormat { case protocol.Markdown: - return fmt.Sprintf("[`%s` %s](%s)", h.symbolName, caption, url) + return fmt.Sprintf("[`%s` %s](%s)", h.SymbolName, caption, url) case protocol.PlainText: return "" default: diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 393bccac312..7b04e6b746b 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -798,6 +798,11 @@ const ( NoDocumentation HoverKind = "NoDocumentation" SynopsisDocumentation HoverKind = "SynopsisDocumentation" FullDocumentation HoverKind = "FullDocumentation" + + // Structured is a misguided experimental setting that returns a JSON + // hover format. This setting should not be used, as it will be removed in a + // future release of gopls. + Structured HoverKind = "Structured" ) type VulncheckMode string @@ -1073,14 +1078,15 @@ func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error AllSymbolScope) case "hoverKind": - if s, ok := value.(string); ok && strings.EqualFold(s, "structured") { - return nil, deprecatedError("the experimental hoverKind='structured' setting was removed in gopls/v0.18.0 (https://go.dev/issue/70233)") - } + // TODO(rfindley): reinstate the deprecation of Structured hover by making + // it a warning in gopls v0.N+1, and removing it in gopls v0.N+2. return setEnum(&o.HoverKind, value, NoDocumentation, SingleLine, SynopsisDocumentation, - FullDocumentation) + FullDocumentation, + Structured, + ) case "linkTarget": return nil, setString(&o.LinkTarget, value) diff --git a/gopls/internal/settings/settings_test.go b/gopls/internal/settings/settings_test.go index 05afa8ecac3..bd9ec110874 100644 --- a/gopls/internal/settings/settings_test.go +++ b/gopls/internal/settings/settings_test.go @@ -91,19 +91,19 @@ func TestOptions_Set(t *testing.T) { }, }, { - name: "hoverKind", - value: "Structured", - wantError: true, + name: "hoverKind", + value: "Structured", + // wantError: true, // TODO(rfindley): reinstate this error check: func(o Options) bool { - return o.HoverKind == FullDocumentation + return o.HoverKind == Structured }, }, { - name: "ui.documentation.hoverKind", - value: "Structured", - wantError: true, + name: "ui.documentation.hoverKind", + value: "Structured", + // wantError: true, // TODO(rfindley): reinstate this error check: func(o Options) bool { - return o.HoverKind == FullDocumentation + return o.HoverKind == Structured }, }, { diff --git a/gopls/internal/test/marker/testdata/hover/json.txt b/gopls/internal/test/marker/testdata/hover/json.txt new file mode 100644 index 00000000000..6c489cb4221 --- /dev/null +++ b/gopls/internal/test/marker/testdata/hover/json.txt @@ -0,0 +1,33 @@ +This test demonstrates support for "hoverKind": "Structured". + +Its size expectations assume a 64-bit machine. + +-- flags -- +-skip_goarch=386,arm + +-- go.mod -- +module example.com/p + +go 1.18 + +-- settings.json -- +{ + "hoverKind": "Structured" +} +-- p.go -- +package p + +// MyType is a type. +type MyType struct { //@ hover("MyType", "MyType", MyType) + F int // a field + S string // a string field +} + +// MyFunc is a function. +func MyFunc(i int) string { //@ hover("MyFunc", "MyFunc", MyFunc) + return "" +} +-- @MyFunc -- +{"Synopsis":"MyFunc is a function.","FullDocumentation":"MyFunc is a function.\n","Signature":"func MyFunc(i int) string","SingleLine":"func MyFunc(i int) string","SymbolName":"p.MyFunc","LinkPath":"example.com/p","LinkAnchor":"MyFunc"} +-- @MyType -- +{"Synopsis":"MyType is a type.","FullDocumentation":"MyType is a type.\n","Signature":"type MyType struct { // size=24 (0x18)\n\tF int // a field\n\tS string // a string field\n}\n","SingleLine":"type MyType struct{F int; S string}","SymbolName":"p.MyType","LinkPath":"example.com/p","LinkAnchor":"MyType"} From 7347766eee58ceaf6dc96b921cbd775f7844f267 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 20 Feb 2025 17:40:52 +0000 Subject: [PATCH 181/309] gopls/internal/test: fix failures when running tests with GOTOOLCHAIN Gopls integration tests want to use the ambient Go toolchain, to test integration with older Go commands. But GOTOOLCHAIN injects the toolchain binary into PATH, so gopls must remove this injected path element before it runs the go command. Unfortunately, if GOTOOLCHAIN=go1.N.P explicitly, those tests will also try to *download* the explicit toolchain and fail because we have set GOPROXY to a file based proxy. Fix this by first adding a check that the initial workspace load did not fail, as well as other related error annotations such that the failure message more accurately identifies the problem. Additionally, the preceding CL improved the integration test framework to better surface such errors. Then, actually fix the problem by setting GOTOOLCHAIN=local in our integration test sandbox. Change-Id: I8c7e9f10d1c17143f10be42476caf29021ab63e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651418 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- .../internal/golang/completion/completion.go | 19 ++++++++++++++----- .../internal/test/integration/fake/sandbox.go | 1 + gopls/internal/test/marker/marker_test.go | 5 ++++- internal/imports/fix.go | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index 4c340055233..a6c0e49c311 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -668,7 +668,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p err = c.collectCompletions(ctx) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to collect completions: %v", err) } // Deep search collected candidates and their members for more candidates. @@ -688,7 +688,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p for _, callback := range c.completionCallbacks { if deadline == nil || time.Now().Before(*deadline) { if err := c.snapshot.RunProcessEnvFunc(ctx, callback); err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to run goimports callback: %v", err) } } } @@ -989,7 +989,10 @@ func (c *completer) populateImportCompletions(searchImport *ast.ImportSpec) erro } c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { - return imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.Types().Name(), opts.Env) + if err := imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.Types().Name(), opts.Env); err != nil { + return fmt.Errorf("getting import paths: %v", err) + } + return nil }) return nil } @@ -1529,7 +1532,10 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error { c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { defer cancel() - return imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.Types().Name(), opts.Env) + if err := imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.Types().Name(), opts.Env); err != nil { + return fmt.Errorf("getting package exports: %v", err) + } + return nil }) return nil } @@ -1916,7 +1922,10 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru } c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error { - return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.Types().Name(), opts.Env) + if err := imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.Types().Name(), opts.Env); err != nil { + return fmt.Errorf("getting completion candidates: %v", err) + } + return nil }) return nil diff --git a/gopls/internal/test/integration/fake/sandbox.go b/gopls/internal/test/integration/fake/sandbox.go index 7adf3e3e4a9..1d8918babd4 100644 --- a/gopls/internal/test/integration/fake/sandbox.go +++ b/gopls/internal/test/integration/fake/sandbox.go @@ -208,6 +208,7 @@ func (sb *Sandbox) GoEnv() map[string]string { "GO111MODULE": "", "GOSUMDB": "off", "GOPACKAGESDRIVER": "off", + "GOTOOLCHAIN": "local", // tests should not download toolchains } if testenv.Go1Point() >= 5 { vars["GOMODCACHE"] = "" diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index 516dfeb3881..d7f91abed46 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -971,7 +971,10 @@ func newEnv(t *testing.T, cache *cache.Cache, files, proxyFiles map[string][]byt sandbox.Close() // ignore error t.Fatal(err) } - if err := awaiter.Await(ctx, integration.InitialWorkspaceLoad); err != nil { + if err := awaiter.Await(ctx, integration.OnceMet( + integration.InitialWorkspaceLoad, + integration.NoShownMessage(""), + )); err != nil { sandbox.Close() // ignore error t.Fatal(err) } diff --git a/internal/imports/fix.go b/internal/imports/fix.go index bf6b0aaddde..ee0efe48a55 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -1030,7 +1030,7 @@ func (e *ProcessEnv) GetResolver() (Resolver, error) { // // For gopls, we can optionally explicitly choose a resolver type, since we // already know the view type. - if len(e.Env["GOMOD"]) == 0 && len(e.Env["GOWORK"]) == 0 { + if e.Env["GOMOD"] == "" && (e.Env["GOWORK"] == "" || e.Env["GOWORK"] == "off") { e.resolver = newGopathResolver(e) e.logf("created gopath resolver") } else if r, err := newModuleResolver(e, e.ModCache); err != nil { From 4e0c888d60c4363071510deedaa07ca8cc9530ae Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Tue, 21 Jan 2025 17:36:24 +0800 Subject: [PATCH 182/309] gopls/internal/hover: show alias rhs type declaration on hover This CL support to find the direct Rhs declaration for an alias type in hover. Fixes golang/go#71286 Change-Id: Ie43a70ec52fe41510e303bb538cc170ff59020c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/644495 Auto-Submit: Robert Findley Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/golang/hover.go | 111 ++++++++++++------ .../test/marker/testdata/definition/embed.txt | 2 + .../marker/testdata/hover/hover_alias.txt | 81 +++++++++++++ 3 files changed, 156 insertions(+), 38 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/hover/hover_alias.txt diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index cda79dcadb8..947595715a7 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -138,6 +138,28 @@ func Hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, positi }, nil } +// findRhsTypeDecl finds an alias's rhs type and returns its declaration. +// The rhs of an alias might be an alias as well, but we feel this is a rare case. +// It returns an empty string if the given obj is not an alias. +func findRhsTypeDecl(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, obj types.Object) (string, error) { + if alias, ok := obj.Type().(*types.Alias); ok { + // we choose Rhs instead of types.Unalias to make the connection between original alias + // and the corresponding aliased type clearer. + // types.Unalias brings confusion because it breaks the connection from A to C given + // the alias chain like 'type ( A = B; B =C ; )' except we show all transitive alias + // from start to the end. As it's rare, we don't do so. + t := alias.Rhs() + switch o := t.(type) { + case *types.Named: + obj = o.Obj() + declPGF1, declPos1, _ := parseFull(ctx, snapshot, pkg.FileSet(), obj.Pos()) + realTypeDecl, _, err := typeDeclContent(declPGF1, declPos1, obj) + return realTypeDecl, err + } + } + return "", nil +} + // hover computes hover information at the given position. If we do not support // hovering at the position, it returns _, nil, nil: an error is only returned // if the position is valid but we fail to compute hover information. @@ -404,46 +426,20 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro _, isTypeName := obj.(*types.TypeName) _, isTypeParam := types.Unalias(obj.Type()).(*types.TypeParam) if isTypeName && !isTypeParam { - spec, ok := spec.(*ast.TypeSpec) - if !ok { - // We cannot find a TypeSpec for this type or alias declaration - // (that is not a type parameter or a built-in). - // This should be impossible even for ill-formed trees; - // we suspect that AST repair may be creating inconsistent - // positions. Don't report a bug in that case. (#64241) - errorf := fmt.Errorf - if !declPGF.Fixed() { - errorf = bug.Errorf - } - return protocol.Range{}, nil, errorf("type name %q without type spec", obj.Name()) + var spec1 *ast.TypeSpec + typeDecl, spec1, err = typeDeclContent(declPGF, declPos, obj) + if err != nil { + return protocol.Range{}, nil, err } - // Format the type's declaration syntax. - { - // Don't duplicate comments. - spec2 := *spec - spec2.Doc = nil - spec2.Comment = nil - - var b strings.Builder - b.WriteString("type ") - fset := tokeninternal.FileSetFor(declPGF.Tok) - // TODO(adonovan): use a smarter formatter that omits - // inaccessible fields (non-exported ones from other packages). - if err := format.Node(&b, fset, &spec2); err != nil { - return protocol.Range{}, nil, err - } - typeDecl = b.String() - - // Splice in size/offset at end of first line. - // "type T struct { // size=..." - if sizeOffset != "" { - nl := strings.IndexByte(typeDecl, '\n') - if nl < 0 { - nl = len(typeDecl) - } - typeDecl = typeDecl[:nl] + " // " + sizeOffset + typeDecl[nl:] + // Splice in size/offset at end of first line. + // "type T struct { // size=..." + if sizeOffset != "" { + nl := strings.IndexByte(typeDecl, '\n') + if nl < 0 { + nl = len(typeDecl) } + typeDecl = typeDecl[:nl] + " // " + sizeOffset + typeDecl[nl:] } // Promoted fields @@ -478,7 +474,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro // already been displayed when the node was formatted // above. Don't list these again. var skip map[string]bool - if iface, ok := spec.Type.(*ast.InterfaceType); ok { + if iface, ok := spec1.Type.(*ast.InterfaceType); ok { if iface.Methods.List != nil { for _, m := range iface.Methods.List { if len(m.Names) == 1 { @@ -520,6 +516,12 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro } } + // realTypeDecl is defined to store the underlying definition of an alias. + realTypeDecl, _ := findRhsTypeDecl(ctx, snapshot, pkg, obj) // tolerate the error + if realTypeDecl != "" { + typeDecl += fmt.Sprintf("\n\n%s", realTypeDecl) + } + // Compute link data (on pkg.go.dev or other documentation host). // // If linkPath is empty, the symbol is not linkable. @@ -640,6 +642,39 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro }, nil } +// typeDeclContent returns a well formatted type definition. +func typeDeclContent(declPGF *parsego.File, declPos token.Pos, obj types.Object) (string, *ast.TypeSpec, error) { + _, spec, _ := findDeclInfo([]*ast.File{declPGF.File}, declPos) // may be nil^3 + // Don't duplicate comments. + spec1, ok := spec.(*ast.TypeSpec) + if !ok { + // We cannot find a TypeSpec for this type or alias declaration + // (that is not a type parameter or a built-in). + // This should be impossible even for ill-formed trees; + // we suspect that AST repair may be creating inconsistent + // positions. Don't report a bug in that case. (#64241) + errorf := fmt.Errorf + if !declPGF.Fixed() { + errorf = bug.Errorf + } + return "", nil, errorf("type name %q without type spec", obj.Name()) + } + spec2 := *spec1 + spec2.Doc = nil + spec2.Comment = nil + + var b strings.Builder + b.WriteString("type ") + fset := tokeninternal.FileSetFor(declPGF.Tok) + // TODO(adonovan): use a smarter formatter that omits + // inaccessible fields (non-exported ones from other packages). + if err := format.Node(&b, fset, &spec2); err != nil { + return "", nil, err + } + typeDecl := b.String() + return typeDecl, spec1, nil +} + // hoverBuiltin computes hover information when hovering over a builtin // identifier. func hoverBuiltin(ctx context.Context, snapshot *cache.Snapshot, obj types.Object) (*hoverResult, error) { diff --git a/gopls/internal/test/marker/testdata/definition/embed.txt b/gopls/internal/test/marker/testdata/definition/embed.txt index 8ff3e37adb3..5a29b31708f 100644 --- a/gopls/internal/test/marker/testdata/definition/embed.txt +++ b/gopls/internal/test/marker/testdata/definition/embed.txt @@ -322,6 +322,8 @@ func (a.A) Hi() -- @aAlias -- ```go type aAlias = a.A // size=16 (0x10) + +type A string ``` --- diff --git a/gopls/internal/test/marker/testdata/hover/hover_alias.txt b/gopls/internal/test/marker/testdata/hover/hover_alias.txt new file mode 100644 index 00000000000..886a175981c --- /dev/null +++ b/gopls/internal/test/marker/testdata/hover/hover_alias.txt @@ -0,0 +1,81 @@ +This test checks gopls behavior when hovering over alias type. + +-- flags -- +-skip_goarch=386,arm + +-- go.mod -- +module mod.com + +-- main.go -- +package main + +import "mod.com/a" +import "mod.com/b" + +type ToTypeDecl = b.RealType //@hover("ToTypeDecl", "ToTypeDecl", ToTypeDecl) + +type ToAlias = a.Alias //@hover("ToAlias", "ToAlias", ToAlias) + +type ToAliasWithComment = a.AliasWithComment //@hover("ToAliasWithComment", "ToAliasWithComment", ToAliasWithComment) + +-- a/a.go -- +package a +import "mod.com/b" + +type Alias = b.RealType + +// AliasWithComment is a type alias with comments. +type AliasWithComment = b.RealType + +-- b/b.go -- +package b +// RealType is a real type rather than an alias type. +type RealType struct { + Name string + Age int +} + +-- @ToTypeDecl -- +```go +type ToTypeDecl = b.RealType // size=24 (0x18) + +type RealType struct { + Name string + Age int +} +``` + +--- + +@hover("ToTypeDecl", "ToTypeDecl", ToTypeDecl) + + +--- + +[`main.ToTypeDecl` on pkg.go.dev](https://pkg.go.dev/mod.com#ToTypeDecl) +-- @ToAlias -- +```go +type ToAlias = a.Alias // size=24 (0x18) +``` + +--- + +@hover("ToAlias", "ToAlias", ToAlias) + + +--- + +[`main.ToAlias` on pkg.go.dev](https://pkg.go.dev/mod.com#ToAlias) +-- @ToAliasWithComment -- +```go +type ToAliasWithComment = a.AliasWithComment // size=24 (0x18) +``` + +--- + +@hover("ToAliasWithComment", "ToAliasWithComment", ToAliasWithComment) + + +--- + +[`main.ToAliasWithComment` on pkg.go.dev](https://pkg.go.dev/mod.com#ToAliasWithComment) From 1c52ccd39b923912d9e4b54944df219a62b60f91 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 14 Feb 2025 08:11:27 -0500 Subject: [PATCH 183/309] gopls/internal/analysis/gofix: inline most aliases Support inlining an alias with an arbitrary right-hand side. The type checker gives us almost everything we need to inline an alias; the only thing missing is the bit that says that a //go:fix directive was present. So the fact is an empty struct. Skip aliases that mention arrays. The array length expression isn't represented, and it may refer to other values, so inlining it would incorrectly decouple the inlined expression from the original. For golang/go#32816. Change-Id: I2e5ff1bd69a0f88cd7cb396dee8d4b426988d1cc Reviewed-on: https://go-review.googlesource.com/c/tools/+/650755 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 220 ++++++++++++------ gopls/internal/analysis/gofix/gofix_test.go | 162 ++++++++++++- .../analysis/gofix/testdata/src/a/a.go | 56 ++++- .../analysis/gofix/testdata/src/a/a.go.golden | 57 ++++- .../gofix/testdata/src/a/internal/d.go | 2 + .../analysis/gofix/testdata/src/b/b.go | 5 + .../analysis/gofix/testdata/src/b/b.go.golden | 9 +- .../analysis/gofix/testdata/src/c/c.go | 5 + 8 files changed, 439 insertions(+), 77 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index ffc64be755b..bb6ce4b43ce 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -9,6 +9,7 @@ import ( "go/ast" "go/token" "go/types" + "strings" _ "embed" @@ -118,32 +119,31 @@ func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) { a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: not a type alias") return } + + // Disallow inlines of type expressions containing array types. + // Given an array type like [N]int where N is a named constant, go/types provides + // only the value of the constant as an int64. So inlining A in this code: + // + // const N = 5 + // type A = [N]int + // + // would result in [5]int, breaking the connection with N. + // TODO(jba): accept type expressions where the array size is a literal integer + for n := range ast.Preorder(spec.Type) { + if ar, ok := n.(*ast.ArrayType); ok && ar.Len != nil { + a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: array types not supported") + return + } + } + if spec.TypeParams != nil { // TODO(jba): handle generic aliases return } - // The alias must refer to another named type. - // TODO(jba): generalize to more type expressions. - var rhsID *ast.Ident - switch e := ast.Unparen(spec.Type).(type) { - case *ast.Ident: - rhsID = e - case *ast.SelectorExpr: - rhsID = e.Sel - default: - return - } + + // Remember that this is an inlinable alias. + typ := &goFixInlineAliasFact{} lhs := a.pass.TypesInfo.Defs[spec.Name].(*types.TypeName) - // more (jba): test one alias pointing to another alias - rhs := a.pass.TypesInfo.Uses[rhsID].(*types.TypeName) - typ := &goFixInlineAliasFact{ - RHSName: rhs.Name(), - RHSPkgName: rhs.Pkg().Name(), - RHSPkgPath: rhs.Pkg().Path(), - } - if rhs.Pkg() == a.pass.Pkg { - typ.rhsObj = rhs - } a.inlinableAliases[lhs] = typ // Create a fact only if the LHS is exported and defined at top level. // We create a fact even if the RHS is non-exported, @@ -302,49 +302,148 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { if inalias == nil { return // nope } - curFile := currentFile(cur) - // We have an identifier A here (n), possibly qualified by a package identifier (sel.X, - // where sel is the parent of X), // and an inlinable "type A = B" elsewhere (inali). - // Consider replacing A with B. + // Get the alias's RHS. It has everything we need to format the replacement text. + rhs := tn.Type().(*types.Alias).Rhs() - // Check that the expression we are inlining (B) means the same thing - // (refers to the same object) in n's scope as it does in A's scope. - // If the RHS is not in the current package, AddImport will handle - // shadowing, so we only need to worry about when both expressions - // are in the current package. + curPath := a.pass.Pkg.Path() + curFile := currentFile(cur) n := cur.Node().(*ast.Ident) - if a.pass.Pkg.Path() == inalias.RHSPkgPath { - // fcon.rhsObj is the object referred to by B in the definition of A. - scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(inalias.RHSName, n.Pos()) // what "B" means in n's scope - if obj == nil { - // Should be impossible: if code at n can refer to the LHS, - // it can refer to the RHS. - panic(fmt.Sprintf("no object for inlinable alias %s RHS %s", n.Name, inalias.RHSName)) + // We have an identifier A here (n), possibly qualified by a package + // identifier (sel.n), and an inlinable "type A = rhs" elsewhere. + // + // We can replace A with rhs if no name in rhs is shadowed at n's position, + // and every package in rhs is importable by the current package. + + var ( + importPrefixes = map[string]string{curPath: ""} // from pkg path to prefix + edits []analysis.TextEdit + ) + for _, tn := range typenames(rhs) { + var pkgPath, pkgName string + if pkg := tn.Pkg(); pkg != nil { + pkgPath = pkg.Path() + pkgName = pkg.Name() } - if obj != inalias.rhsObj { - // "B" means something different here than at the inlinable const's scope. + if pkgPath == "" || pkgPath == curPath { + // The name is in the current package or the universe scope, so no import + // is required. Check that it is not shadowed (that is, that the type + // it refers to in rhs is the same one it refers to at n). + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope + _, obj := scope.LookupParent(tn.Name(), n.Pos()) // what qn.name means in n's scope + if obj != tn { // shadowed + return + } + } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), pkgPath) { + // If this package can't see the package of this part of rhs, we can't inline. return + } else if _, ok := importPrefixes[pkgPath]; !ok { + // Use AddImport to add pkgPath if it's not there already. Associate the prefix it assigns + // with the package path for use by the TypeString qualifier below. + _, prefix, eds := analysisinternal.AddImport( + a.pass.TypesInfo, curFile, pkgName, pkgPath, tn.Name(), n.Pos()) + importPrefixes[pkgPath] = strings.TrimSuffix(prefix, ".") + edits = append(edits, eds...) } - } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), inalias.RHSPkgPath) { - // If this package can't see the RHS's package, we can't inline. - return - } - var ( - importPrefix string - edits []analysis.TextEdit - ) - if inalias.RHSPkgPath != a.pass.Pkg.Path() { - _, importPrefix, edits = analysisinternal.AddImport( - a.pass.TypesInfo, curFile, inalias.RHSPkgName, inalias.RHSPkgPath, inalias.RHSName, n.Pos()) } // If n is qualified by a package identifier, we'll need the full selector expression. var expr ast.Expr = n if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { expr = cur.Parent().Node().(ast.Expr) } - a.reportInline("type alias", "Type alias", expr, edits, importPrefix+inalias.RHSName) + // To get the replacement text, render the alias RHS using the package prefixes + // we assigned above. + newText := types.TypeString(rhs, func(p *types.Package) string { + if p == a.pass.Pkg { + return "" + } + if prefix, ok := importPrefixes[p.Path()]; ok { + return prefix + } + panic(fmt.Sprintf("in %q, package path %q has no import prefix", rhs, p.Path())) + }) + a.reportInline("type alias", "Type alias", expr, edits, newText) +} + +// typenames returns the TypeNames for types within t (including t itself) that have +// them: basic types, named types and alias types. +// The same name may appear more than once. +func typenames(t types.Type) []*types.TypeName { + var tns []*types.TypeName + + var visit func(types.Type) + + // TODO(jba): when typesinternal.NamedOrAlias adds TypeArgs, replace this type literal with it. + namedOrAlias := func(t interface { + TypeArgs() *types.TypeList + Obj() *types.TypeName + }) { + tns = append(tns, t.Obj()) + args := t.TypeArgs() + // TODO(jba): replace with TypeList.Types when this file is at 1.24. + for i := range args.Len() { + visit(args.At(i)) + } + } + + visit = func(t types.Type) { + switch t := t.(type) { + case *types.Basic: + tns = append(tns, types.Universe.Lookup(t.Name()).(*types.TypeName)) + case *types.Named: + namedOrAlias(t) + case *types.Alias: + namedOrAlias(t) + case *types.TypeParam: + tns = append(tns, t.Obj()) + case *types.Pointer: + visit(t.Elem()) + case *types.Slice: + visit(t.Elem()) + case *types.Array: + visit(t.Elem()) + case *types.Chan: + visit(t.Elem()) + case *types.Map: + visit(t.Key()) + visit(t.Elem()) + case *types.Struct: + // TODO(jba): replace with Struct.Fields when this file is at 1.24. + for i := range t.NumFields() { + visit(t.Field(i).Type()) + } + case *types.Signature: + // Ignore the receiver: although it may be present, it has no meaning + // in a type expression. + // Ditto for receiver type params. + // Also, function type params cannot appear in a type expression. + if t.TypeParams() != nil { + panic("Signature.TypeParams in type expression") + } + visit(t.Params()) + visit(t.Results()) + case *types.Interface: + for i := range t.NumEmbeddeds() { + visit(t.EmbeddedType(i)) + } + for i := range t.NumExplicitMethods() { + visit(t.ExplicitMethod(i).Type()) + } + case *types.Tuple: + // TODO(jba): replace with Tuple.Variables when this file is at 1.24. + for i := range t.Len() { + visit(t.At(i).Type()) + } + case *types.Union: + panic("Union in type expression") + default: + panic(fmt.Sprintf("unknown type %T", t)) + } + } + + visit(t) + + return tns } // If con is an inlinable constant, suggest inlining its use at cur. @@ -481,20 +580,11 @@ func (c *goFixInlineConstFact) String() string { func (*goFixInlineConstFact) AFact() {} // A goFixInlineAliasFact is exported for each type alias marked "//go:fix inline". -// It holds information about an inlinable type alias. Gob-serializable. -type goFixInlineAliasFact struct { - // Information about "type LHSName = RHSName". - RHSName string - RHSPkgPath string - RHSPkgName string - rhsObj types.Object // for current package -} - -func (c *goFixInlineAliasFact) String() string { - return fmt.Sprintf("goFixInline alias %q.%s", c.RHSPkgPath, c.RHSName) -} +// It holds no information; its mere existence demonstrates that an alias is inlinable. +type goFixInlineAliasFact struct{} -func (*goFixInlineAliasFact) AFact() {} +func (c *goFixInlineAliasFact) String() string { return "goFixInline alias" } +func (*goFixInlineAliasFact) AFact() {} func discard(string, ...any) {} diff --git a/gopls/internal/analysis/gofix/gofix_test.go b/gopls/internal/analysis/gofix/gofix_test.go index 32bd87b6cd2..dc98ef47181 100644 --- a/gopls/internal/analysis/gofix/gofix_test.go +++ b/gopls/internal/analysis/gofix/gofix_test.go @@ -2,15 +2,171 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package gofix_test +package gofix import ( + "go/ast" + "go/importer" + "go/parser" + "go/token" + "go/types" + "slices" "testing" + gocmp "github.com/google/go-cmp/cmp" "golang.org/x/tools/go/analysis/analysistest" - "golang.org/x/tools/gopls/internal/analysis/gofix" + "golang.org/x/tools/internal/testenv" ) func TestAnalyzer(t *testing.T) { - analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), gofix.Analyzer, "a", "b") + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "a", "b") +} + +func TestTypesWithNames(t *testing.T) { + // Test setup inspired by internal/analysisinternal/addimport_test.go. + testenv.NeedsDefaultImporter(t) + + for _, test := range []struct { + typeExpr string + want []string + }{ + { + "int", + []string{"int"}, + }, + { + "*int", + []string{"int"}, + }, + { + "[]*int", + []string{"int"}, + }, + { + "[2]int", + []string{"int"}, + }, + { + // go/types does not expose the length expression. + "[unsafe.Sizeof(uint(1))]int", + []string{"int"}, + }, + { + "map[string]int", + []string{"int", "string"}, + }, + { + "map[int]struct{x, y int}", + []string{"int"}, + }, + { + "T", + []string{"a.T"}, + }, + { + "iter.Seq[int]", + []string{"int", "iter.Seq"}, + }, + { + "io.Reader", + []string{"io.Reader"}, + }, + { + "map[*io.Writer]map[T]A", + []string{"a.A", "a.T", "io.Writer"}, + }, + { + "func(int, int) (bool, error)", + []string{"bool", "error", "int"}, + }, + { + "func(int, ...string) (T, *T, error)", + []string{"a.T", "error", "int", "string"}, + }, + { + "func(iter.Seq[int])", + []string{"int", "iter.Seq"}, + }, + { + "struct { a int; b bool}", + []string{"bool", "int"}, + }, + { + "struct { io.Reader; a int}", + []string{"int", "io.Reader"}, + }, + { + "map[*string]struct{x chan int; y [2]bool}", + []string{"bool", "int", "string"}, + }, + { + "interface {F(int) bool}", + []string{"bool", "int"}, + }, + { + "interface {io.Reader; F(int) bool}", + []string{"bool", "int", "io.Reader"}, + }, + { + "G", // a type parameter of the function + []string{"a.G"}, + }, + } { + src := ` + package a + import ("io"; "iter"; "unsafe") + func _(io.Reader, iter.Seq[int]) uintptr {return unsafe.Sizeof(1)} + type T int + type A = T + + func F[G any]() { + var V ` + test.typeExpr + ` + _ = V + }` + + // parse + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "a.go", src, 0) + if err != nil { + t.Errorf("%s: %v", test.typeExpr, err) + continue + } + + // type-check + info := &types.Info{ + Types: make(map[ast.Expr]types.TypeAndValue), + Scopes: make(map[ast.Node]*types.Scope), + Defs: make(map[*ast.Ident]types.Object), + Implicits: make(map[ast.Node]types.Object), + } + conf := &types.Config{ + Error: func(err error) { t.Fatalf("%s: %v", test.typeExpr, err) }, + Importer: importer.Default(), + } + pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, info) + if err != nil { + t.Errorf("%s: %v", test.typeExpr, err) + continue + } + + // Look at V's type. + typ := pkg.Scope().Lookup("F").(*types.Func). + Scope().Lookup("V").(*types.Var).Type() + tns := typenames(typ) + // Sort names for comparison. + var got []string + for _, tn := range tns { + var prefix string + if p := tn.Pkg(); p != nil && p.Path() != "" { + prefix = p.Path() + "." + } + got = append(got, prefix+tn.Name()) + } + slices.Sort(got) + got = slices.Compact(got) + + if diff := gocmp.Diff(test.want, got); diff != "" { + t.Errorf("%s: mismatch (-want, +got):\n%s", test.typeExpr, diff) + } + } } diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index fb4d8b92172..49a0587c2b1 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -105,14 +105,62 @@ func shadow() { // Type aliases //go:fix inline -type A = T // want A: `goFixInline alias "a".T` +type A = T // want A: `goFixInline alias` var _ A // want `Type alias A should be inlined` -type B = []T // nope: only named RHSs - //go:fix inline -type AA = // want AA: `goFixInline alias "a".A` +type AA = // want AA: `goFixInline alias` A // want `Type alias A should be inlined` var _ AA // want `Type alias AA should be inlined` + +//go:fix inline +type ( + B = []T // want B: `goFixInline alias` + C = map[*string][]error // want C: `goFixInline alias` +) + +var _ B // want `Type alias B should be inlined` +var _ C // want `Type alias C should be inlined` + +//go:fix inline +type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array types not supported` + +var _ E // nothing should happen here + +//go:fix inline +type F = map[internal.T]T // want F: `goFixInline alias` + +var _ F // want `Type alias F should be inlined` + +//go:fix inline +type G = []chan *internal.T // want G: `goFixInline alias` + +var _ G // want `Type alias G should be inlined` + +// local shadowing +func _() { + type string = int + const T = 1 + + var _ B // nope: B's RHS contains T, which is shadowed + var _ C // nope: C's RHS contains string, which is shadowed +} + +// local inlining +func _[P any]() { + const a = 1 + //go:fix inline + const b = a + + x := b // want `Constant b should be inlined` + + //go:fix inline + type u = []P + + var y u // want `Type alias u should be inlined` + + _ = x + _ = y +} diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index 9ab1bcbc652..9d4c527919e 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -105,14 +105,63 @@ func shadow() { // Type aliases //go:fix inline -type A = T // want A: `goFixInline alias "a".T` +type A = T // want A: `goFixInline alias` var _ T // want `Type alias A should be inlined` -type B = []T // nope: only named RHSs - //go:fix inline -type AA = // want AA: `goFixInline alias "a".A` +type AA = // want AA: `goFixInline alias` T // want `Type alias A should be inlined` var _ A // want `Type alias AA should be inlined` + +//go:fix inline +type ( + B = []T // want B: `goFixInline alias` + C = map[*string][]error // want C: `goFixInline alias` +) + +var _ []T // want `Type alias B should be inlined` +var _ map[*string][]error // want `Type alias C should be inlined` + +//go:fix inline +type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array types not supported` + +var _ E // nothing should happen here + +//go:fix inline +type F = map[internal.T]T // want F: `goFixInline alias` + +var _ map[internal.T]T // want `Type alias F should be inlined` + +//go:fix inline +type G = []chan *internal.T // want G: `goFixInline alias` + +var _ []chan *internal.T // want `Type alias G should be inlined` + +// local shadowing +func _() { + type string = int + const T = 1 + + var _ B // nope: B's RHS contains T, which is shadowed + var _ C // nope: C's RHS contains string, which is shadowed +} + + +// local inlining +func _[P any]() { + const a = 1 + //go:fix inline + const b = a + + x := a // want `Constant b should be inlined` + + //go:fix inline + type u = []P + + var y []P // want `Type alias u should be inlined` + + _ = x + _ = y +} diff --git a/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go b/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go index 3211d7ae3cc..60d0c1ab7e8 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go @@ -3,3 +3,5 @@ package internal const D = 1 + +type T int diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go b/gopls/internal/analysis/gofix/testdata/src/b/b.go index d52fd514024..b358d7b4f67 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go @@ -32,3 +32,8 @@ func g() { const d = a.D // nope: a.D refers to a constant in a package that is not visible here. var _ a.A // want `Type alias a\.A should be inlined` +var _ a.B // want `Type alias a\.B should be inlined` +var _ a.C // want `Type alias a\.C should be inlined` +var _ R // want `Type alias R should be inlined` + +var _ a.G // nope: a.G refers to a type in a package that is not visible here diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index 4228ffeb489..fd8d87a2ef1 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -2,6 +2,8 @@ package b import a0 "a" +import "io" + import ( "a" . "c" @@ -35,4 +37,9 @@ func g() { const d = a.D // nope: a.D refers to a constant in a package that is not visible here. -var _ a.T // want `Type alias a\.A should be inlined` +var _ a.T // want `Type alias a\.A should be inlined` +var _ []a.T // want `Type alias a\.B should be inlined` +var _ map[*string][]error // want `Type alias a\.C should be inlined` +var _ map[io.Reader]io.Reader // want `Type alias R should be inlined` + +var _ a.G // nope: a.G refers to a type in a package that is not visible here diff --git a/gopls/internal/analysis/gofix/testdata/src/c/c.go b/gopls/internal/analysis/gofix/testdata/src/c/c.go index 36504b886a7..7f6a3f26fe2 100644 --- a/gopls/internal/analysis/gofix/testdata/src/c/c.go +++ b/gopls/internal/analysis/gofix/testdata/src/c/c.go @@ -2,4 +2,9 @@ package c // This package is dot-imported by package b. +import "io" + const C = 1 + +//go:fix inline +type R = map[io.Reader]io.Reader From 6e3d8bca20c96bbb8297a834e4421b93e6c3ffa5 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 21 Feb 2025 11:20:11 -0500 Subject: [PATCH 184/309] gopls/internal/analysis/gofix: use 1.24 iterators For golang/go#32816. Change-Id: Icf805984f812af19c720d4f477ed12a97a5dd68d Reviewed-on: https://go-review.googlesource.com/c/tools/+/651615 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/gofix/gofix.go | 35 ++++++++++---------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index bb6ce4b43ce..237e5b0b58a 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -372,28 +372,21 @@ func typenames(t types.Type) []*types.TypeName { var tns []*types.TypeName var visit func(types.Type) - - // TODO(jba): when typesinternal.NamedOrAlias adds TypeArgs, replace this type literal with it. - namedOrAlias := func(t interface { - TypeArgs() *types.TypeList - Obj() *types.TypeName - }) { - tns = append(tns, t.Obj()) - args := t.TypeArgs() - // TODO(jba): replace with TypeList.Types when this file is at 1.24. - for i := range args.Len() { - visit(args.At(i)) - } - } - visit = func(t types.Type) { + if hasName, ok := t.(interface{ Obj() *types.TypeName }); ok { + tns = append(tns, hasName.Obj()) + } switch t := t.(type) { case *types.Basic: tns = append(tns, types.Universe.Lookup(t.Name()).(*types.TypeName)) case *types.Named: - namedOrAlias(t) + for t := range t.TypeArgs().Types() { + visit(t) + } case *types.Alias: - namedOrAlias(t) + for t := range t.TypeArgs().Types() { + visit(t) + } case *types.TypeParam: tns = append(tns, t.Obj()) case *types.Pointer: @@ -408,9 +401,8 @@ func typenames(t types.Type) []*types.TypeName { visit(t.Key()) visit(t.Elem()) case *types.Struct: - // TODO(jba): replace with Struct.Fields when this file is at 1.24. - for i := range t.NumFields() { - visit(t.Field(i).Type()) + for f := range t.Fields() { + visit(f.Type()) } case *types.Signature: // Ignore the receiver: although it may be present, it has no meaning @@ -430,9 +422,8 @@ func typenames(t types.Type) []*types.TypeName { visit(t.ExplicitMethod(i).Type()) } case *types.Tuple: - // TODO(jba): replace with Tuple.Variables when this file is at 1.24. - for i := range t.Len() { - visit(t.At(i).Type()) + for v := range t.Variables() { + visit(v.Type()) } case *types.Union: panic("Union in type expression") From 3d7c2e28a97c1a7e134dba0e3a3a27c560ddaa75 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 21 Feb 2025 21:23:18 +0000 Subject: [PATCH 185/309] gopls/internal/golang: add missing json tags for hoverResult In my haste to partially revert CL 635226 in 651238, I failed to add json struct tags. Add them back. For golang/go#71879 Change-Id: I45190cba5154eeed7b6a49db51d2a2a51999be7a Reviewed-on: https://go-review.googlesource.com/c/tools/+/651618 Auto-Submit: Robert Findley Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/hover.go | 14 +++++++------- gopls/internal/test/marker/testdata/hover/json.txt | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index 947595715a7..74cf5dbb593 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -57,20 +57,20 @@ type hoverResult struct { // TODO(adonovan): in what syntax? It (usually) comes from doc.Synopsis, // which produces "Text" form, but it may be fed to // DocCommentToMarkdown, which expects doc comment syntax. - Synopsis string + Synopsis string `json:"synopsis"` // FullDocumentation is the symbol's full documentation. - FullDocumentation string + FullDocumentation string `json:"fullDocumentation"` // Signature is the symbol's Signature. - Signature string + Signature string `json:"signature"` // SingleLine is a single line describing the symbol. // This is recommended only for use in clients that show a single line for hover. - SingleLine string + SingleLine string `json:"singleLine"` // SymbolName is the human-readable name to use for the symbol in links. - SymbolName string + SymbolName string `json:"symbolName"` // LinkPath is the path of the package enclosing the given symbol, // with the module portion (if any) replaced by "module@version". @@ -78,11 +78,11 @@ type hoverResult struct { // For example: "github.com/google/go-github/v48@v48.1.0/github". // // Use LinkTarget + "/" + LinkPath + "#" + LinkAnchor to form a pkgsite URL. - LinkPath string + LinkPath string `json:"linkPath"` // LinkAnchor is the pkg.go.dev link anchor for the given symbol. // For example, the "Node" part of "pkg.go.dev/go/ast#Node". - LinkAnchor string + LinkAnchor string `json:"linkAnchor"` // New fields go below, and are unexported. The existing // exported fields are underspecified and have already diff --git a/gopls/internal/test/marker/testdata/hover/json.txt b/gopls/internal/test/marker/testdata/hover/json.txt index 6c489cb4221..f3229805cb6 100644 --- a/gopls/internal/test/marker/testdata/hover/json.txt +++ b/gopls/internal/test/marker/testdata/hover/json.txt @@ -28,6 +28,6 @@ func MyFunc(i int) string { //@ hover("MyFunc", "MyFunc", MyFunc) return "" } -- @MyFunc -- -{"Synopsis":"MyFunc is a function.","FullDocumentation":"MyFunc is a function.\n","Signature":"func MyFunc(i int) string","SingleLine":"func MyFunc(i int) string","SymbolName":"p.MyFunc","LinkPath":"example.com/p","LinkAnchor":"MyFunc"} +{"synopsis":"MyFunc is a function.","fullDocumentation":"MyFunc is a function.\n","signature":"func MyFunc(i int) string","singleLine":"func MyFunc(i int) string","symbolName":"p.MyFunc","linkPath":"example.com/p","linkAnchor":"MyFunc"} -- @MyType -- -{"Synopsis":"MyType is a type.","FullDocumentation":"MyType is a type.\n","Signature":"type MyType struct { // size=24 (0x18)\n\tF int // a field\n\tS string // a string field\n}\n","SingleLine":"type MyType struct{F int; S string}","SymbolName":"p.MyType","LinkPath":"example.com/p","LinkAnchor":"MyType"} +{"synopsis":"MyType is a type.","fullDocumentation":"MyType is a type.\n","signature":"type MyType struct { // size=24 (0x18)\n\tF int // a field\n\tS string // a string field\n}\n","singleLine":"type MyType struct{F int; S string}","symbolName":"p.MyType","linkPath":"example.com/p","linkAnchor":"MyType"} From 5299dcb7277190caeca1a827cb7d5c856b22f37f Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 21 Feb 2025 21:57:05 +0000 Subject: [PATCH 186/309] gopls/internal/settings: fix misleading error messages The deprecatedError helper constructs a specifically formatted error string suggesting a replacement. Certain deprecations were misusing the API, resulting in nonsensical error messages. Change-Id: Ic72bf608b5b2e97baf75c192a49fd4181d7800b2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651695 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/settings/settings.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 7b04e6b746b..dd353da64e9 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -1116,7 +1116,7 @@ func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error return nil, err } if o.Analyses["fieldalignment"] { - return counts, deprecatedError("the 'fieldalignment' analyzer was removed in gopls/v0.17.0; instead, hover over struct fields to see size/offset information (https://go.dev/issue/66861)") + return counts, &SoftError{"the 'fieldalignment' analyzer was removed in gopls/v0.17.0; instead, hover over struct fields to see size/offset information (https://go.dev/issue/66861)"} } return counts, nil @@ -1124,7 +1124,7 @@ func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error return setBoolMap(&o.Hints, value) case "annotations": - return nil, deprecatedError("the 'annotations' setting was removed in gopls/v0.18.0; all compiler optimization details are now shown") + return nil, &SoftError{"the 'annotations' setting was removed in gopls/v0.18.0; all compiler optimization details are now shown"} case "vulncheck": return setEnum(&o.Vulncheck, value, From 274b2375098fb4ba49aedcc8b86edcbf2079ba0a Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 21 Feb 2025 20:26:19 +0000 Subject: [PATCH 187/309] gopls: add a -severity flag for gopls check In golang/go#50764, users were reporting having to filter out noisy diagnostics from the output of `gopls check` in CI. This is because there was no differentiation between diagnostics that represent real bugs, and those that are suggestions. By contrast, hint level diagnostics are very unobtrusive in the editor. Add a new -severity flag to control the minimum severity output by gopls check, and set its default to "warning". For golang/go#50764 Change-Id: I48d8bb74371fa6035fef4d2608412b986f509f7b Reviewed-on: https://go-review.googlesource.com/c/tools/+/651616 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Robert Findley --- gopls/doc/release/v0.19.0.md | 7 +++++++ gopls/internal/cmd/check.go | 20 +++++++++++++++++++- gopls/internal/cmd/cmd.go | 2 +- gopls/internal/cmd/integration_test.go | 22 ++++++++++++++++++++++ gopls/internal/cmd/usage/check.hlp | 2 ++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/gopls/doc/release/v0.19.0.md b/gopls/doc/release/v0.19.0.md index 0b3ea64c305..18088732656 100644 --- a/gopls/doc/release/v0.19.0.md +++ b/gopls/doc/release/v0.19.0.md @@ -1,3 +1,10 @@ +# Configuration Changes + +- The `gopls check` subcommant now accepts a `-severity` flag to set a minimum + severity for the diagnostics it reports. By default, the minimum severity + is "warning", so `gopls check` may report fewer diagnostics than before. Set + `-severity=hint` to reproduce the previous behavior. + # New features ## "Eliminate dot import" code action diff --git a/gopls/internal/cmd/check.go b/gopls/internal/cmd/check.go index d256fa9de2a..8c0362b148a 100644 --- a/gopls/internal/cmd/check.go +++ b/gopls/internal/cmd/check.go @@ -16,7 +16,8 @@ import ( // check implements the check verb for gopls. type check struct { - app *Application + app *Application + Severity string `flag:"severity" help:"minimum diagnostic severity (hint, info, warning, or error)"` } func (c *check) Name() string { return "check" } @@ -35,6 +36,20 @@ Example: show the diagnostic results of this file: // Run performs the check on the files specified by args and prints the // results to stdout. func (c *check) Run(ctx context.Context, args ...string) error { + severityCutoff := protocol.SeverityWarning + switch c.Severity { + case "hint": + severityCutoff = protocol.SeverityHint + case "info": + severityCutoff = protocol.SeverityInformation + case "warning": + // default + case "error": + severityCutoff = protocol.SeverityError + default: + return fmt.Errorf("unrecognized -severity value %q", c.Severity) + } + if len(args) == 0 { return nil } @@ -95,6 +110,9 @@ func (c *check) Run(ctx context.Context, args ...string) error { file.diagnosticsMu.Unlock() for _, diag := range diags { + if diag.Severity > severityCutoff { // lower severity value => greater severity, counterintuitively + continue + } if err := print(file.uri, diag.Range, diag.Message); err != nil { return err } diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index 8bd7d7b899f..119577c012b 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -284,7 +284,7 @@ func (app *Application) internalCommands() []tool.Application { func (app *Application) featureCommands() []tool.Application { return []tool.Application{ &callHierarchy{app: app}, - &check{app: app}, + &check{app: app, Severity: "warning"}, &codeaction{app: app}, &codelens{app: app}, &definition{app: app}, diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index 986453253f8..e7ac774f5c0 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -108,6 +108,12 @@ var C int -- c/c2.go -- package c var C int +-- d/d.go -- +package d + +import "io/ioutil" + +var _ = ioutil.ReadFile `) // no files @@ -141,6 +147,22 @@ var C int res.checkStdout(`c2.go:2:5-6: C redeclared in this block`) res.checkStdout(`c.go:2:5-6: - other declaration of C`) } + + // No deprecated (hint) diagnostic without -severity. + { + res := gopls(t, tree, "check", "./d/d.go") + res.checkExit(true) + if len(res.stdout) > 0 { + t.Errorf("check ./d/d.go returned unexpected output:\n%s", res.stdout) + } + } + + // Deprecated (hint) diagnostics with -severity=hint + { + res := gopls(t, tree, "check", "-severity=hint", "./d/d.go") + res.checkExit(true) + res.checkStdout(`ioutil.ReadFile is deprecated`) + } } // TestCallHierarchy tests the 'call_hierarchy' subcommand (call_hierarchy.go). diff --git a/gopls/internal/cmd/usage/check.hlp b/gopls/internal/cmd/usage/check.hlp index eda1a25a191..c387c2cf5d9 100644 --- a/gopls/internal/cmd/usage/check.hlp +++ b/gopls/internal/cmd/usage/check.hlp @@ -6,3 +6,5 @@ Usage: Example: show the diagnostic results of this file: $ gopls check internal/cmd/check.go + -severity=string + minimum diagnostic severity (hint, info, warning, or error) (default "warning") From 739a5af40476496b626dc23e996357a7dff4e3e8 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Sun, 23 Feb 2025 13:08:20 +0000 Subject: [PATCH 188/309] gopls/internal/test/marker: skip on the freebsd race builder The marker tests frequently time out on the freebsd race builder. Skip them to reduce noise. (We don't currently have resources to investigate). Fixes golang/go#71731 Change-Id: I2e27c2e8063b6d5e698eb9d1b5c32d08914fcc77 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651895 Auto-Submit: Robert Findley Reviewed-by: Peter Weinberger LUCI-TryBot-Result: Go LUCI --- gopls/internal/test/marker/marker_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index d7f91abed46..a5e23b928ad 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -96,6 +96,9 @@ func Test(t *testing.T) { if strings.HasPrefix(builder, "darwin-") || strings.Contains(builder, "solaris") { t.Skip("golang/go#64473: skipping with -short: this test is too slow on darwin and solaris builders") } + if strings.HasSuffix(builder, "freebsd-amd64-race") { + t.Skip("golang/go#71731: the marker tests are too slow to run on the amd64-race builder") + } } // The marker tests must be able to run go/packages.Load. testenv.NeedsGoPackages(t) @@ -658,7 +661,7 @@ type stringListValue []string func (l *stringListValue) Set(s string) error { if s != "" { - for _, d := range strings.Split(s, ",") { + for d := range strings.SplitSeq(s, ",") { *l = append(*l, strings.TrimSpace(d)) } } @@ -1838,7 +1841,7 @@ func removeDiagnostic(mark marker, loc protocol.Location, matchEnd bool, re *reg diags := mark.run.diags[key] for i, diag := range diags { if re.MatchString(diag.Message) && (!matchEnd || diag.Range.End == loc.Range.End) { - mark.run.diags[key] = append(diags[:i], diags[i+1:]...) + mark.run.diags[key] = slices.Delete(diags, i, i+1) return diag, true } } From 2b2a44ed6f269fbfd9adfd17139a0485e1b0a144 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Sun, 23 Feb 2025 13:52:01 +0000 Subject: [PATCH 189/309] gopls/internal/test: avoid panic in TestDoubleParamReturnCompletion An invalid assumption in this test led to an out of bounds error, which likely masked a real error or timeout. Update the test to not panic, and factor. Fixes golang/go#71906 Change-Id: Ib01d3b75df8bdb71984457807312cfe1d27ddf73 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651896 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam --- .../integration/completion/completion_test.go | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index 1d293fe9019..0713b1f62b9 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go @@ -1212,25 +1212,21 @@ func TestDoubleParamReturnCompletion(t *testing.T) { Run(t, src, func(t *testing.T, env *Env) { env.OpenFile("a.go") - compl := env.RegexpSearch("a.go", `DoubleWrap\[()\]\(\)`) - result := env.Completion(compl) - - wantLabel := []string{"InterfaceA", "TypeA", "InterfaceB", "TypeB", "TypeC"} - - for i, item := range result.Items[:len(wantLabel)] { - if diff := cmp.Diff(wantLabel[i], item.Label); diff != "" { - t.Errorf("Completion: unexpected label mismatch (-want +got):\n%s", diff) - } + tests := map[string][]string{ + `DoubleWrap\[()\]\(\)`: {"InterfaceA", "TypeA", "InterfaceB", "TypeB", "TypeC"}, + `DoubleWrap\[InterfaceA, (_)\]\(\)`: {"InterfaceB", "TypeB", "TypeX", "InterfaceA", "TypeA"}, } - compl = env.RegexpSearch("a.go", `DoubleWrap\[InterfaceA, (_)\]\(\)`) - result = env.Completion(compl) - - wantLabel = []string{"InterfaceB", "TypeB", "TypeX", "InterfaceA", "TypeA"} - - for i, item := range result.Items[:len(wantLabel)] { - if diff := cmp.Diff(wantLabel[i], item.Label); diff != "" { - t.Errorf("Completion: unexpected label mismatch (-want +got):\n%s", diff) + for re, wantLabels := range tests { + compl := env.RegexpSearch("a.go", re) + result := env.Completion(compl) + if len(result.Items) < len(wantLabels) { + t.Fatalf("Completion(%q) returned mismatching labels: got %v, want at least labels %v", re, result.Items, wantLabels) + } + for i, item := range result.Items[:len(wantLabels)] { + if diff := cmp.Diff(wantLabels[i], item.Label); diff != "" { + t.Errorf("Completion(%q): unexpected label mismatch (-want +got):\n%s", re, diff) + } } } }) From d2fcd360ffaa3fcc4c225918750054b056033d3c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 20 Feb 2025 16:31:14 -0500 Subject: [PATCH 190/309] go/analysis/passes/unreachable/testdata: relax test for CL 638395 This test case is about to become a parse error. To allow us to submit the change to the parser, we must relax this test. Updates golang/go#71659 Updates golang/go#70957 Change-Id: Ic4fbfedb69d152d691dec41a94bb402149463f84 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651155 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/analysis/passes/unreachable/testdata/src/a/a.go | 5 ----- go/analysis/passes/unreachable/testdata/src/a/a.go.golden | 5 ----- 2 files changed, 10 deletions(-) diff --git a/go/analysis/passes/unreachable/testdata/src/a/a.go b/go/analysis/passes/unreachable/testdata/src/a/a.go index b283fd00b9a..136a07caa21 100644 --- a/go/analysis/passes/unreachable/testdata/src/a/a.go +++ b/go/analysis/passes/unreachable/testdata/src/a/a.go @@ -2118,11 +2118,6 @@ var _ = func() int { println() // ok } -var _ = func() { - // goto without label used to panic - goto -} - func _() int { // Empty switch tag with non-bool case value used to panic. switch { diff --git a/go/analysis/passes/unreachable/testdata/src/a/a.go.golden b/go/analysis/passes/unreachable/testdata/src/a/a.go.golden index 40494030423..79cb89d4181 100644 --- a/go/analysis/passes/unreachable/testdata/src/a/a.go.golden +++ b/go/analysis/passes/unreachable/testdata/src/a/a.go.golden @@ -2082,11 +2082,6 @@ var _ = func() int { println() // ok } -var _ = func() { - // goto without label used to panic - goto -} - func _() int { // Empty switch tag with non-bool case value used to panic. switch { From 3e76cae71578160dca62d1cab42a715ef960c892 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 20 Feb 2025 16:37:02 -0500 Subject: [PATCH 191/309] internal/analysisinternal: ValidateFix: more specific errors These details help us diagnose errors in gopls especially relating to synthezized End() positions beyond EOF. Change-Id: Iff36f5c4e01f2256f2cbf8cc03b27d7b3aa74b11 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651097 Reviewed-by: Robert Findley Commit-Queue: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- go/analysis/internal/checker/fix_test.go | 4 ++-- internal/analysisinternal/analysis.go | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/go/analysis/internal/checker/fix_test.go b/go/analysis/internal/checker/fix_test.go index 68d965d08d6..00710cc0e1b 100644 --- a/go/analysis/internal/checker/fix_test.go +++ b/go/analysis/internal/checker/fix_test.go @@ -93,7 +93,7 @@ func TestReportInvalidDiagnostic(t *testing.T) { // TextEdit has invalid Pos. { "bad Pos", - `analyzer "a" suggests invalid fix .*: missing file info for pos`, + `analyzer "a" suggests invalid fix .*: no token.File for TextEdit.Pos .0.`, func(pos token.Pos) analysis.Diagnostic { return analysis.Diagnostic{ Pos: pos, @@ -110,7 +110,7 @@ func TestReportInvalidDiagnostic(t *testing.T) { // TextEdit has invalid End. { "End < Pos", - `analyzer "a" suggests invalid fix .*: pos .* > end`, + `analyzer "a" suggests invalid fix .*: TextEdit.Pos .* > TextEdit.End .*`, func(pos token.Pos) analysis.Diagnostic { return analysis.Diagnostic{ Pos: pos, diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index aba435fa404..5eb7ac5a939 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -419,18 +419,19 @@ func validateFix(fset *token.FileSet, fix *analysis.SuggestedFix) error { start := edit.Pos file := fset.File(start) if file == nil { - return fmt.Errorf("missing file info for pos (%v)", edit.Pos) + return fmt.Errorf("no token.File for TextEdit.Pos (%v)", edit.Pos) } if end := edit.End; end.IsValid() { if end < start { - return fmt.Errorf("pos (%v) > end (%v)", edit.Pos, edit.End) + return fmt.Errorf("TextEdit.Pos (%v) > TextEdit.End (%v)", edit.Pos, edit.End) } endFile := fset.File(end) if endFile == nil { - return fmt.Errorf("malformed end position %v", end) + return fmt.Errorf("no token.File for TextEdit.End (%v; File(start).FileEnd is %d)", end, file.Base()+file.Size()) } if endFile != file { - return fmt.Errorf("edit spans files %v and %v", file.Name(), endFile.Name()) + return fmt.Errorf("edit #%d spans files (%v and %v)", + i, file.Position(edit.Pos), endFile.Position(edit.End)) } } else { edit.End = start // update the SuggestedFix From 851c747e148e33b095d285e569c734385d2b074b Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 24 Feb 2025 15:05:15 +0000 Subject: [PATCH 192/309] gopls/internal/golang: fix crash when hovering over implicit After months of intermittent investigation, I was finally able to reproduce the telemetry crash of golang/go#69362: if the operant of the type switch is undefined, the selectedType will be nil, and therefore logic may proceed to the point where it reaches a nil entry in types.Info.Defs. The fix is of course straightforward, now that we understand it: we cannot rely on types.Info.Defs not containing nil entries. A follow-up CL will introduce an analyzer to detect such problematic uses of the go/types API. Fixes golang/go#69362 Change-Id: I8f75c24710dbb2e78c79d8b9d721f45d9a040cd7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652015 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/hover.go | 13 ++++--------- .../internal/test/marker/testdata/hover/issues.txt | 12 ++++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index 74cf5dbb593..c3fecd1c9d1 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -375,15 +375,10 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro // use the default build config for all other types, even // if they embed platform-variant types. // - var sizeOffset string // optional size/offset description - // debugging #69362: unexpected nil Defs[ident] value (?) - _ = ident.Pos() // (can't be nil due to check after referencedObject) - _ = pkg.TypesInfo() // (can't be nil due to check in call to inferredSignature) - _ = pkg.TypesInfo().Defs // (can't be nil due to nature of cache.Package) - if def, ok := pkg.TypesInfo().Defs[ident]; ok { - _ = def.Pos() // can't be nil due to reasoning in #69362. - } - if def, ok := pkg.TypesInfo().Defs[ident]; ok && ident.Pos() == def.Pos() { + var sizeOffset string + + // As painfully learned in golang/go#69362, Defs can contain nil entries. + if def, _ := pkg.TypesInfo().Defs[ident]; def != nil && ident.Pos() == def.Pos() { // This is the declaring identifier. // (We can't simply use ident.Pos() == obj.Pos() because // referencedObject prefers the TypeName for an embedded field). diff --git a/gopls/internal/test/marker/testdata/hover/issues.txt b/gopls/internal/test/marker/testdata/hover/issues.txt index 6212964dff2..eda0eea3efa 100644 --- a/gopls/internal/test/marker/testdata/hover/issues.txt +++ b/gopls/internal/test/marker/testdata/hover/issues.txt @@ -20,3 +20,15 @@ package issue64237 import "golang.org/lsptests/nonexistant" //@diag("\"golang", re"could not import") var _ = nonexistant.Value //@hovererr("nonexistant", "no package data") + +-- issue69362/p.go -- +package issue69362 + +// golang/go#69362: hover panics over undefined implicits. + +func _() { + switch x := y.(type) { //@diag("y", re"undefined"), hover("x", "x", "") + case int: + _ = x + } +} From bf9e2a812de33f4ff08ed99be3ecfa95d857830e Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Thu, 13 Feb 2025 12:49:36 -0500 Subject: [PATCH 193/309] gopls/internal: test fixes for some imports bugs The CL has tests for two fixed bugs. For the new imports to work the metadata graph has to be current, which in this CL, is accomplished with a call to snapshot.WordspaceMetadata which may wait for changes to be assimilated. Fixes: golang.go/go#44510 Fixes: golang.go/go#67973 Change-Id: Ieb5a9361a75796a172da953cc58853d38f596ebd Reviewed-on: https://go-review.googlesource.com/c/tools/+/649315 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/source.go | 2 +- gopls/internal/golang/format.go | 3 + .../test/integration/misc/imports_test.go | 55 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/gopls/internal/cache/source.go b/gopls/internal/cache/source.go index fa038ec37a6..7946b9746ab 100644 --- a/gopls/internal/cache/source.go +++ b/gopls/internal/cache/source.go @@ -212,7 +212,7 @@ func (s *goplsSource) resolveWorkspaceReferences(filename string, missing import // keep track of used syms and found results by package name // TODO: avoid import cycles (is current package in forward closure) founds := make(map[string][]found) - for i := 0; i < len(ids); i++ { + for i := range len(ids) { nm := string(pkgs[i].Name) if satisfies(syms[i], missing[nm]) { got := &imports.Result{ diff --git a/gopls/internal/golang/format.go b/gopls/internal/golang/format.go index acc619eba0c..b353538d978 100644 --- a/gopls/internal/golang/format.go +++ b/gopls/internal/golang/format.go @@ -152,6 +152,9 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, snapshot *cache. case settings.ImportsSourceGoimports: source = isource } + // imports require a current metadata graph + // TODO(rfindlay) improve the API + snapshot.WorkspaceMetadata(ctx) allFixes, err := imports.FixImports(ctx, filename, pgf.Src, goroot, options.Env.Logf, source) if err != nil { return nil, nil, err diff --git a/gopls/internal/test/integration/misc/imports_test.go b/gopls/internal/test/integration/misc/imports_test.go index 98a70478ecf..bcbfacc967a 100644 --- a/gopls/internal/test/integration/misc/imports_test.go +++ b/gopls/internal/test/integration/misc/imports_test.go @@ -401,6 +401,31 @@ return nil } }) } + +// use the import from a different package in the same module +func Test44510(t *testing.T) { + const files = `-- go.mod -- +module test +go 1.19 +-- foo/foo.go -- +package main +import strs "strings" +var _ = strs.Count +-- bar/bar.go -- +package main +var _ = strs.Builder +` + WithOptions( + WriteGoSum("."), + ).Run(t, files, func(T *testing.T, env *Env) { + env.OpenFile("bar/bar.go") + env.SaveBuffer("bar/bar.go") + buf := env.BufferText("bar/bar.go") + if !strings.Contains(buf, "strs") { + t.Error(buf) + } + }) +} func TestRelativeReplace(t *testing.T) { const files = ` -- go.mod -- @@ -688,3 +713,33 @@ func Test() { } }) } + +// this test replaces 'package bar' with 'package foo' +// saves the file, and then looks for the import in the main package.s +func Test67973(t *testing.T) { + const files = `-- go.mod -- +module hello +go 1.19 +-- hello.go -- +package main +var _ = foo.Bar +-- internal/foo/foo.go -- +package bar +func Bar() {} +` + WithOptions( + Settings{"importsSource": settings.ImportsSourceGopls}, + ).Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("hello.go") + env.AfterChange(env.DoneWithOpen()) + env.SaveBuffer("hello.go") + env.OpenFile("internal/foo/foo.go") + env.RegexpReplace("internal/foo/foo.go", "bar", "foo") + env.SaveBuffer("internal/foo/foo.go") + env.SaveBuffer("hello.go") + buf := env.BufferText("hello.go") + if !strings.Contains(buf, "internal/foo") { + t.Errorf(`expected import "hello/internal/foo" but got %q`, buf) + } + }) +} From 6d4af1e1f521077aa5f196b9a4be4297d95ec1c2 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 24 Feb 2025 17:24:53 -0500 Subject: [PATCH 194/309] gopls/internal/golang: Assembly: update "Compiling" message This CL causes the "Browse assembly" feature to flush the header early, and to update the "Compiling..." message when the report is complete. Change-Id: I96e0c3e1e0949dadbbd058101959f01e38e7596b Reviewed-on: https://go-review.googlesource.com/c/tools/+/652196 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/golang/assembly.go | 60 ++++++++++++++++++++----------- gopls/internal/server/server.go | 7 +--- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/gopls/internal/golang/assembly.go b/gopls/internal/golang/assembly.go index 3b778a54697..a74c48a171d 100644 --- a/gopls/internal/golang/assembly.go +++ b/gopls/internal/golang/assembly.go @@ -16,6 +16,8 @@ import ( "context" "fmt" "html" + "io" + "net/http" "regexp" "strconv" "strings" @@ -26,39 +28,33 @@ import ( // AssemblyHTML returns an HTML document containing an assembly listing of the selected function. // -// TODO(adonovan): -// - display a "Compiling..." message as a cold build can be slow. -// - cross-link jumps and block labels, like github.com/aclements/objbrowse. -func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, symbol string, web Web) ([]byte, error) { - // Compile the package with -S, and capture its stderr stream. +// TODO(adonovan): cross-link jumps and block labels, like github.com/aclements/objbrowse. +func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, w http.ResponseWriter, pkg *cache.Package, symbol string, web Web) { + // Prepare to compile the package with -S, and capture its stderr stream. inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkg.Metadata().CompiledGoFiles[0].DirPath(), "build", []string{"-gcflags=-S", "."}) if err != nil { - return nil, err // e.g. failed to write overlays (rare) + // e.g. failed to write overlays (rare) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } defer cleanupInvocation() - _, stderr, err, _ := snapshot.View().GoCommandRunner().RunRaw(ctx, *inv) - if err != nil { - return nil, err // e.g. won't compile - } - content := stderr.String() escape := html.EscapeString - // Produce the report. + // Emit the start of the report. title := fmt.Sprintf("%s assembly for %s", escape(snapshot.View().GOARCH()), escape(symbol)) - var buf bytes.Buffer - buf.WriteString(` + io.WriteString(w, ` - ` + escape(title) + ` + `+escape(title)+` -

` + title + `

+

`+title+`

A Quick Guide to Go's Assembler

@@ -69,11 +65,23 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Pack Click on a source line marker L1234 to navigate your editor there. (VS Code users: please upvote #208093)

-

- Reload the page to recompile. -

+

Compiling...

 `)
+	if flusher, ok := w.(http.Flusher); ok {
+		flusher.Flush()
+	}
+
+	// Compile the package.
+	_, stderr, err, _ := snapshot.View().GoCommandRunner().RunRaw(ctx, *inv)
+	if err != nil {
+		// e.g. won't compile
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	// Write the rest of the report.
+	content := stderr.String()
 
 	// insnRx matches an assembly instruction line.
 	// Submatch groups are: (offset-hex-dec, file-line-column, instruction).
@@ -88,7 +96,8 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Pack
 	//
 	// Allow matches of symbol, symbol.func1, symbol.deferwrap, etc.
 	on := false
-	for _, line := range strings.Split(content, "\n") {
+	var buf bytes.Buffer
+	for line := range strings.SplitSeq(content, "\n") {
 		// start of function symbol?
 		if strings.Contains(line, " STEXT ") {
 			on = strings.HasPrefix(line, symbol) &&
@@ -116,5 +125,14 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Pack
 		}
 		buf.WriteByte('\n')
 	}
-	return buf.Bytes(), nil
+
+	// Update the "Compiling..." message.
+	buf.WriteString(`
+
+ +`) + + w.Write(buf.Bytes()) } diff --git a/gopls/internal/server/server.go b/gopls/internal/server/server.go index d9090250a66..033295ffb32 100644 --- a/gopls/internal/server/server.go +++ b/gopls/internal/server/server.go @@ -447,12 +447,7 @@ func (s *server) initWeb() (*web, error) { pkg := pkgs[0] // Produce report. - html, err := golang.AssemblyHTML(ctx, snapshot, pkg, symbol, web) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - w.Write(html) + golang.AssemblyHTML(ctx, snapshot, w, pkg, symbol, web) }) return web, nil From e890c1f6805a34b15289937191b324a5172f9c22 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 24 Feb 2025 18:03:04 -0500 Subject: [PATCH 195/309] gopls/internal/golang: Assembly: support package level var and init This CL offers the "Browse Assembly" code action when the selection is within a package-level var initializer, or a source-level init function. + Test Change-Id: Ic0fcf321765027df0c11fb7269a1aedf971814fc Reviewed-on: https://go-review.googlesource.com/c/tools/+/652197 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/golang/assembly.go | 32 ++--- gopls/internal/golang/codeaction.go | 86 +++++++++----- .../test/integration/misc/webserver_test.go | 109 ++++++++++++------ 3 files changed, 144 insertions(+), 83 deletions(-) diff --git a/gopls/internal/golang/assembly.go b/gopls/internal/golang/assembly.go index a74c48a171d..9e673dd9719 100644 --- a/gopls/internal/golang/assembly.go +++ b/gopls/internal/golang/assembly.go @@ -29,6 +29,8 @@ import ( // AssemblyHTML returns an HTML document containing an assembly listing of the selected function. // // TODO(adonovan): cross-link jumps and block labels, like github.com/aclements/objbrowse. +// +// See gopls/internal/test/integration/misc/webserver_test.go for tests. func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, w http.ResponseWriter, pkg *cache.Package, symbol string, web Web) { // Prepare to compile the package with -S, and capture its stderr stream. inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkg.Metadata().CompiledGoFiles[0].DirPath(), "build", []string{"-gcflags=-S", "."}) @@ -72,11 +74,26 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, w http.Response flusher.Flush() } + // At this point errors must be reported by writing HTML. + // To do this, set "status" return early. + + var buf bytes.Buffer + status := "Reload the page to recompile." + defer func() { + // Update the "Compiling..." message. + fmt.Fprintf(&buf, ` + + +`, status) + w.Write(buf.Bytes()) + }() + // Compile the package. _, stderr, err, _ := snapshot.View().GoCommandRunner().RunRaw(ctx, *inv) if err != nil { - // e.g. won't compile - http.Error(w, err.Error(), http.StatusInternalServerError) + status = fmt.Sprintf("compilation failed: %v", err) return } @@ -96,7 +113,6 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, w http.Response // // Allow matches of symbol, symbol.func1, symbol.deferwrap, etc. on := false - var buf bytes.Buffer for line := range strings.SplitSeq(content, "\n") { // start of function symbol? if strings.Contains(line, " STEXT ") { @@ -125,14 +141,4 @@ func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, w http.Response } buf.WriteByte('\n') } - - // Update the "Compiling..." message. - buf.WriteString(` - - -`) - - w.Write(buf.Bytes()) } diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 587ae3e2de3..74f3c2b6085 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -945,44 +945,66 @@ func goAssembly(ctx context.Context, req *codeActionsRequest) error { // directly to (say) a lambda of interest. // Perhaps we could scroll to STEXT for the innermost // enclosing nested function? - path, _ := astutil.PathEnclosingInterval(req.pgf.File, req.start, req.end) - if len(path) >= 2 { // [... FuncDecl File] - if decl, ok := path[len(path)-2].(*ast.FuncDecl); ok { - if fn, ok := req.pkg.TypesInfo().Defs[decl.Name].(*types.Func); ok { - sig := fn.Signature() - - // Compute the linker symbol of the enclosing function. - var sym strings.Builder - if fn.Pkg().Name() == "main" { - sym.WriteString("main") - } else { - sym.WriteString(fn.Pkg().Path()) - } - sym.WriteString(".") - if sig.Recv() != nil { - if isPtr, named := typesinternal.ReceiverNamed(sig.Recv()); named != nil { - if isPtr { - fmt.Fprintf(&sym, "(*%s)", named.Obj().Name()) - } else { - sym.WriteString(named.Obj().Name()) + + // Compute the linker symbol of the enclosing function or var initializer. + var sym strings.Builder + if pkg := req.pkg.Types(); pkg.Name() == "main" { + sym.WriteString("main") + } else { + sym.WriteString(pkg.Path()) + } + sym.WriteString(".") + + curSel, _ := req.pgf.Cursor.FindPos(req.start, req.end) + for cur := range curSel.Ancestors((*ast.FuncDecl)(nil), (*ast.ValueSpec)(nil)) { + var name string // in command title + switch node := cur.Node().(type) { + case *ast.FuncDecl: + // package-level func or method + if fn, ok := req.pkg.TypesInfo().Defs[node.Name].(*types.Func); ok && + fn.Name() != "_" { // blank functions are not compiled + + // Source-level init functions are compiled (along with + // package-level var initializers) in into a single pkg.init + // function, so this falls out of the logic below. + + if sig := fn.Signature(); sig.TypeParams() == nil && sig.RecvTypeParams() == nil { // generic => no assembly + if sig.Recv() != nil { + if isPtr, named := typesinternal.ReceiverNamed(sig.Recv()); named != nil { + if isPtr { + fmt.Fprintf(&sym, "(*%s)", named.Obj().Name()) + } else { + sym.WriteString(named.Obj().Name()) + } + sym.WriteByte('.') } - sym.WriteByte('.') } + sym.WriteString(fn.Name()) + + name = node.Name.Name // success } - sym.WriteString(fn.Name()) - - if fn.Name() != "_" && // blank functions are not compiled - (fn.Name() != "init" || sig.Recv() != nil) && // init functions aren't linker functions - sig.TypeParams() == nil && sig.RecvTypeParams() == nil { // generic => no assembly - cmd := command.NewAssemblyCommand( - fmt.Sprintf("Browse %s assembly for %s", view.GOARCH(), decl.Name), - view.ID(), - string(req.pkg.Metadata().ID), - sym.String()) - req.addCommandAction(cmd, false) + } + + case *ast.ValueSpec: + // package-level var initializer? + if len(node.Names) > 0 && len(node.Values) > 0 { + v := req.pkg.TypesInfo().Defs[node.Names[0]] + if v != nil && typesinternal.IsPackageLevel(v) { + sym.WriteString("init") + name = "package initializer" // success } } } + + if name != "" { + cmd := command.NewAssemblyCommand( + fmt.Sprintf("Browse %s assembly for %s", view.GOARCH(), name), + view.ID(), + string(req.pkg.Metadata().ID), + sym.String()) + req.addCommandAction(cmd, false) + break + } } return nil } diff --git a/gopls/internal/test/integration/misc/webserver_test.go b/gopls/internal/test/integration/misc/webserver_test.go index 2bde7df8aa2..5153289941f 100644 --- a/gopls/internal/test/integration/misc/webserver_test.go +++ b/gopls/internal/test/integration/misc/webserver_test.go @@ -520,43 +520,57 @@ module example.com -- a/a.go -- package a -func f() { +func f(x int) int { println("hello") defer println("world") + return x } func g() { println("goodbye") } + +var v = [...]int{ + f(123), + f(456), +} + +func init() { + f(789) +} ` Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("a/a.go") - // Invoke the "Browse assembly" code action to start the server. - loc := env.RegexpSearch("a/a.go", "println") - actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) - if err != nil { - t.Fatalf("CodeAction: %v", err) - } - action, err := codeActionByKind(actions, settings.GoAssembly) - if err != nil { - t.Fatal(err) - } + asmFor := func(pattern string) []byte { + // Invoke the "Browse assembly" code action to start the server. + loc := env.RegexpSearch("a/a.go", pattern) + actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) + if err != nil { + t.Fatalf("CodeAction: %v", err) + } + action, err := codeActionByKind(actions, settings.GoAssembly) + if err != nil { + t.Fatal(err) + } - // Execute the command. - // Its side effect should be a single showDocument request. - params := &protocol.ExecuteCommandParams{ - Command: action.Command.Command, - Arguments: action.Command.Arguments, - } - var result command.DebuggingResult - collectDocs := env.Awaiter.ListenToShownDocuments() - env.ExecuteCommand(params, &result) - doc := shownDocument(t, collectDocs(), "http:") - if doc == nil { - t.Fatalf("no showDocument call had 'file:' prefix") + // Execute the command. + // Its side effect should be a single showDocument request. + params := &protocol.ExecuteCommandParams{ + Command: action.Command.Command, + Arguments: action.Command.Arguments, + } + var result command.DebuggingResult + collectDocs := env.Awaiter.ListenToShownDocuments() + env.ExecuteCommand(params, &result) + doc := shownDocument(t, collectDocs(), "http:") + if doc == nil { + t.Fatalf("no showDocument call had 'file:' prefix") + } + t.Log("showDocument(package doc) URL:", doc.URI) + + return get(t, doc.URI) } - t.Log("showDocument(package doc) URL:", doc.URI) // Get the report and do some minimal checks for sensible results. // @@ -567,23 +581,42 @@ func g() { // (e.g. uses JAL for CALL, or BL for RET). // We conservatively test only on the two most popular // architectures. - report := get(t, doc.URI) - checkMatch(t, true, report, `TEXT.*example.com/a.f`) - switch runtime.GOARCH { - case "amd64", "arm64": - checkMatch(t, true, report, `CALL runtime.printlock`) - checkMatch(t, true, report, `CALL runtime.printstring`) - checkMatch(t, true, report, `CALL runtime.printunlock`) - checkMatch(t, true, report, `CALL example.com/a.f.deferwrap1`) - checkMatch(t, true, report, `RET`) - checkMatch(t, true, report, `CALL runtime.morestack_noctxt`) + { + report := asmFor("println") + checkMatch(t, true, report, `TEXT.*example.com/a.f`) + switch runtime.GOARCH { + case "amd64", "arm64": + checkMatch(t, true, report, `CALL runtime.printlock`) + checkMatch(t, true, report, `CALL runtime.printstring`) + checkMatch(t, true, report, `CALL runtime.printunlock`) + checkMatch(t, true, report, `CALL example.com/a.f.deferwrap1`) + checkMatch(t, true, report, `RET`) + checkMatch(t, true, report, `CALL runtime.morestack_noctxt`) + } + + // Nested functions are also shown. + checkMatch(t, true, report, `TEXT.*example.com/a.f.deferwrap1`) + + // But other functions are not. + checkMatch(t, false, report, `TEXT.*example.com/a.g`) } - // Nested functions are also shown. - checkMatch(t, true, report, `TEXT.*example.com/a.f.deferwrap1`) + // Check that code in a package-level var initializer is found too. + { + report := asmFor(`f\(123\)`) + checkMatch(t, true, report, `TEXT.*example.com/a.init`) + checkMatch(t, true, report, `MOV. \$123`) + checkMatch(t, true, report, `MOV. \$456`) + checkMatch(t, true, report, `CALL example.com/a.f`) + } - // But other functions are not. - checkMatch(t, false, report, `TEXT.*example.com/a.g`) + // And code in a source-level init function. + { + report := asmFor(`f\(789\)`) + checkMatch(t, true, report, `TEXT.*example.com/a.init`) + checkMatch(t, true, report, `MOV. \$789`) + checkMatch(t, true, report, `CALL example.com/a.f`) + } }) } From 6f7906b2b92af49e85ca2e34f08a7097a19b6b5a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 13 Feb 2025 13:21:46 -0500 Subject: [PATCH 196/309] x/tools: use ast.IsGenerated throughout Note the behavior change: the go/ast implementation checks that the special comment appears before the packge declaration; the ad hoc implementations did not. Change-Id: Ib51c498c1c73fd32c882e25f8228a6076bba7ed7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649316 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam Commit-Queue: Alan Donovan --- cmd/deadcode/deadcode.go | 41 +------------------ copyright/copyright.go | 23 +---------- gopls/internal/golang/format.go | 11 ++--- gopls/internal/golang/util.go | 26 ++---------- .../diagnostics/diagnostics_test.go | 17 +++++--- .../marker/testdata/diagnostics/generated.txt | 4 +- internal/refactor/inline/inline.go | 4 +- refactor/rename/rename.go | 2 +- refactor/rename/spec.go | 25 +---------- 9 files changed, 29 insertions(+), 124 deletions(-) diff --git a/cmd/deadcode/deadcode.go b/cmd/deadcode/deadcode.go index f129102cc4c..0c66d07f79f 100644 --- a/cmd/deadcode/deadcode.go +++ b/cmd/deadcode/deadcode.go @@ -175,7 +175,7 @@ func main() { } } - if isGenerated(file) { + if ast.IsGenerated(file) { generated[p.Fset.File(file.Pos()).Name()] = true } } @@ -414,45 +414,6 @@ func printObjects(format string, objects []any) { } } -// TODO(adonovan): use go1.21's ast.IsGenerated. - -// isGenerated reports whether the file was generated by a program, -// not handwritten, by detecting the special comment described -// at https://go.dev/s/generatedcode. -// -// The syntax tree must have been parsed with the ParseComments flag. -// Example: -// -// f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly) -// if err != nil { ... } -// gen := ast.IsGenerated(f) -func isGenerated(file *ast.File) bool { - _, ok := generator(file) - return ok -} - -func generator(file *ast.File) (string, bool) { - for _, group := range file.Comments { - for _, comment := range group.List { - if comment.Pos() > file.Package { - break // after package declaration - } - // opt: check Contains first to avoid unnecessary array allocation in Split. - const prefix = "// Code generated " - if strings.Contains(comment.Text, prefix) { - for _, line := range strings.Split(comment.Text, "\n") { - if rest, ok := strings.CutPrefix(line, prefix); ok { - if gen, ok := strings.CutSuffix(rest, " DO NOT EDIT."); ok { - return gen, true - } - } - } - } - } - } - return "", false -} - // pathSearch returns the shortest path from one of the roots to one // of the targets (along with the root itself), or zero if no path was found. func pathSearch(roots []*ssa.Function, res *rta.Result, targets map[*ssa.Function]bool) (*callgraph.Node, []*callgraph.Edge) { diff --git a/copyright/copyright.go b/copyright/copyright.go index 54bc8f512a4..4d4ad71fd72 100644 --- a/copyright/copyright.go +++ b/copyright/copyright.go @@ -75,7 +75,7 @@ func checkFile(toolsDir, filename string) (bool, error) { return false, err } // Don't require headers on generated files. - if isGenerated(fset, parsed) { + if ast.IsGenerated(parsed) { return false, nil } shouldAddCopyright := true @@ -91,24 +91,3 @@ func checkFile(toolsDir, filename string) (bool, error) { } return shouldAddCopyright, nil } - -// Copied from golang.org/x/tools/gopls/internal/golang/util.go. -// Matches cgo generated comment as well as the proposed standard: -// -// https://golang.org/s/generatedcode -var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`) - -func isGenerated(fset *token.FileSet, file *ast.File) bool { - for _, commentGroup := range file.Comments { - for _, comment := range commentGroup.List { - if matched := generatedRx.MatchString(comment.Text); !matched { - continue - } - // Check if comment is at the beginning of the line in source. - if pos := fset.Position(comment.Slash); pos.Column == 1 { - return true - } - } - } - return false -} diff --git a/gopls/internal/golang/format.go b/gopls/internal/golang/format.go index b353538d978..ded00deef38 100644 --- a/gopls/internal/golang/format.go +++ b/gopls/internal/golang/format.go @@ -35,15 +35,16 @@ func Format(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]pr ctx, done := event.Start(ctx, "golang.Format") defer done() - // Generated files shouldn't be edited. So, don't format them - if IsGenerated(ctx, snapshot, fh.URI()) { - return nil, fmt.Errorf("can't format %q: file is generated", fh.URI().Path()) - } - pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) if err != nil { return nil, err } + + // Generated files shouldn't be edited. So, don't format them. + if ast.IsGenerated(pgf.File) { + return nil, fmt.Errorf("can't format %q: file is generated", fh.URI().Path()) + } + // Even if this file has parse errors, it might still be possible to format it. // Using format.Node on an AST with errors may result in code being modified. // Attempt to format the source of this file instead. diff --git a/gopls/internal/golang/util.go b/gopls/internal/golang/util.go index 23fd3443fac..a81ff3fbe58 100644 --- a/gopls/internal/golang/util.go +++ b/gopls/internal/golang/util.go @@ -19,16 +19,11 @@ import ( "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" - "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/tokeninternal" ) -// IsGenerated gets and reads the file denoted by uri and reports -// whether it contains a "generated file" comment as described at -// https://golang.org/s/generatedcode. -// -// TODO(adonovan): opt: this function does too much. -// Move snapshot.ReadFile into the caller (most of which have already done it). +// IsGenerated reads and parses the header of the file denoted by uri +// and reports whether it [ast.IsGenerated]. func IsGenerated(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) bool { fh, err := snapshot.ReadFile(ctx, uri) if err != nil { @@ -38,17 +33,7 @@ func IsGenerated(ctx context.Context, snapshot *cache.Snapshot, uri protocol.Doc if err != nil { return false } - for _, commentGroup := range pgf.File.Comments { - for _, comment := range commentGroup.List { - if matched := generatedRx.MatchString(comment.Text); matched { - // Check if comment is at the beginning of the line in source. - if safetoken.Position(pgf.Tok, comment.Slash).Column == 1 { - return true - } - } - } - } - return false + return ast.IsGenerated(pgf.File) } // adjustedObjEnd returns the end position of obj, possibly modified for @@ -76,11 +61,6 @@ func adjustedObjEnd(obj types.Object) token.Pos { return obj.Pos() + token.Pos(nameLen) } -// Matches cgo generated comment as well as the proposed standard: -// -// https://golang.org/s/generatedcode -var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`) - // FormatNode returns the "pretty-print" output for an ast node. func FormatNode(fset *token.FileSet, n ast.Node) string { var buf strings.Builder diff --git a/gopls/internal/test/integration/diagnostics/diagnostics_test.go b/gopls/internal/test/integration/diagnostics/diagnostics_test.go index a97d249e7b5..5ef39a5f0c5 100644 --- a/gopls/internal/test/integration/diagnostics/diagnostics_test.go +++ b/gopls/internal/test/integration/diagnostics/diagnostics_test.go @@ -542,27 +542,34 @@ var X = 0 // Tests golang/go#38467. func TestNoSuggestedFixesForGeneratedFiles_Issue38467(t *testing.T) { + // This test ensures that gopls' CodeAction handler suppresses + // diagnostics in generated code. Beware that many analyzers + // themselves suppress diagnostics in generated files, in + // particular the low-status "simplifiers" (modernize, + // simplify{range,slice,compositelit}), so we use the hostport + // analyzer here. const generated = ` -- go.mod -- module mod.com go 1.12 -- main.go -- +// Code generated by generator.go. DO NOT EDIT. + package main -// Code generated by generator.go. DO NOT EDIT. +import ("fmt"; "net") func _() { - for i, _ := range []string{} { - _ = i - } + addr := fmt.Sprintf("%s:%d", "localhost", 12345) + net.Dial("tcp", addr) } ` Run(t, generated, func(t *testing.T, env *Env) { env.OpenFile("main.go") var d protocol.PublishDiagnosticsParams env.AfterChange( - Diagnostics(AtPosition("main.go", 5, 6)), + Diagnostics(AtPosition("main.go", 7, 21)), ReadDiagnostics("main.go", &d), ) if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 { diff --git a/gopls/internal/test/marker/testdata/diagnostics/generated.txt b/gopls/internal/test/marker/testdata/diagnostics/generated.txt index 123602df3c3..80de61200a3 100644 --- a/gopls/internal/test/marker/testdata/diagnostics/generated.txt +++ b/gopls/internal/test/marker/testdata/diagnostics/generated.txt @@ -10,10 +10,10 @@ module example.com go 1.12 -- generated.go -- -package generated - // Code generated by generator.go. DO NOT EDIT. +package generated + func _() { var y int //@diag("y", re"declared (and|but) not used") } diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 54308243e1c..6f6ed4583a9 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -102,9 +102,9 @@ func (st *state) inline() (*Result, error) { return nil, fmt.Errorf("internal error: caller syntax positions are inconsistent with file content (did you forget to use FileSet.PositionFor when computing the file name?)") } - // TODO(adonovan): use go1.21's ast.IsGenerated. // Break the string literal so we can use inlining in this file. :) - if bytes.Contains(caller.Content, []byte("// Code generated by "+"cmd/cgo; DO NOT EDIT.")) { + if ast.IsGenerated(caller.File) && + bytes.Contains(caller.Content, []byte("// Code generated by "+"cmd/cgo; DO NOT EDIT.")) { return nil, fmt.Errorf("cannot inline calls from files that import \"C\"") } diff --git a/refactor/rename/rename.go b/refactor/rename/rename.go index 3e944b2df38..cb218434e49 100644 --- a/refactor/rename/rename.go +++ b/refactor/rename/rename.go @@ -491,7 +491,7 @@ func (r *renamer) update() error { for _, info := range r.packages { for _, f := range info.Files { tokenFile := r.iprog.Fset.File(f.FileStart) - if filesToUpdate[tokenFile] && generated(f, tokenFile) { + if filesToUpdate[tokenFile] && ast.IsGenerated(f) { generatedFileNames = append(generatedFileNames, tokenFile.Name()) } } diff --git a/refactor/rename/spec.go b/refactor/rename/spec.go index 99068c13358..0a6d7d4346c 100644 --- a/refactor/rename/spec.go +++ b/refactor/rename/spec.go @@ -19,7 +19,6 @@ import ( "log" "os" "path/filepath" - "regexp" "strconv" "strings" @@ -321,7 +320,7 @@ func findFromObjectsInFile(iprog *loader.Program, spec *spec) ([]types.Object, e if spec.offset != 0 { // We cannot refactor generated files since position information is invalidated. - if generated(f, thisFile) { + if ast.IsGenerated(f) { return nil, fmt.Errorf("cannot rename identifiers in generated file containing DO NOT EDIT marker: %s", thisFile.Name()) } @@ -566,25 +565,3 @@ func ambiguityError(fset *token.FileSet, objects []types.Object) error { return fmt.Errorf("ambiguous specifier %s matches %s", objects[0].Name(), buf.String()) } - -// Matches cgo generated comment as well as the proposed standard: -// -// https://golang.org/s/generatedcode -var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`) - -// generated reports whether ast.File is a generated file. -func generated(f *ast.File, tokenFile *token.File) bool { - - // Iterate over the comments in the file - for _, commentGroup := range f.Comments { - for _, comment := range commentGroup.List { - if matched := generatedRx.MatchString(comment.Text); matched { - // Check if comment is at the beginning of the line in source - if pos := tokenFile.Position(comment.Slash); pos.Column == 1 { - return true - } - } - } - } - return false -} From 7fed2a4a04b822b897c3dd789a11e027c9ad1b0c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Feb 2025 16:02:44 -0500 Subject: [PATCH 197/309] gopls/internal/analysis/modernize: fix bug in rangeint for and range loops leave their index variables with a different final value: limit, and limit-1, respectively. Thus we must not offer a fix if the loop variable is used after the loop. + test Fixes golang/go#71952 Change-Id: Iaabd20792724166ace0ed5fd9dd997edaa96a435 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652496 Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/rangeint.go | 12 ++++++++++++ .../modernize/testdata/src/rangeint/rangeint.go | 17 ++++++++++++++++- .../testdata/src/rangeint/rangeint.go.golden | 17 ++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 273c13877bd..b94bff34431 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -100,6 +100,18 @@ func rangeint(pass *analysis.Pass) { }) } + // If i is used after the loop, + // don't offer a fix, as a range loop + // leaves i with a different final value (limit-1). + if init.Tok == token.ASSIGN { + for curId := range curLoop.Parent().Preorder((*ast.Ident)(nil)) { + id := curId.Node().(*ast.Ident) + if id.Pos() > loop.End() && info.Uses[id] == v { + continue nextLoop + } + } + } + // If limit is len(slice), // simplify "range len(slice)" to "range slice". if call, ok := limit.(*ast.CallExpr); ok && diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index 6c30f183340..32628f5fae3 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -4,7 +4,11 @@ func _(i int, s struct{ i int }, slice []int) { for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" println(i) } - for i = 0; i < f(); i++ { // want "for loop can be modernized using range over int" + { + var i int + for i = 0; i < f(); i++ { // want "for loop can be modernized using range over int" + } + // NB: no uses of i after loop. } for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" // i unused within loop @@ -51,3 +55,14 @@ func _(s string) { } } } + +// Repro for #71952: for and range loops have different final values +// on i (n and n-1, respectively) so we can't offer the fix if i is +// used after the loop. +func nopePostconditionDiffers() { + i := 0 + for i = 0; i < 5; i++ { + println(i) + } + println(i) // must print 5, not 4 +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 52f16347b1e..43cf220d699 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -4,7 +4,11 @@ func _(i int, s struct{ i int }, slice []int) { for i := range 10 { // want "for loop can be modernized using range over int" println(i) } - for i = range f() { // want "for loop can be modernized using range over int" + { + var i int + for i = range f() { // want "for loop can be modernized using range over int" + } + // NB: no uses of i after loop. } for range 10 { // want "for loop can be modernized using range over int" // i unused within loop @@ -51,3 +55,14 @@ func _(s string) { } } } + +// Repro for #71952: for and range loops have different final values +// on i (n and n-1, respectively) so we can't offer the fix if i is +// used after the loop. +func nopePostconditionDiffers() { + i := 0 + for i = 0; i < 5; i++ { + println(i) + } + println(i) // must print 5, not 4 +} From 6399d21203019d71e290a17b26c0946f3152cba0 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 14 Feb 2025 09:42:49 -0500 Subject: [PATCH 198/309] go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare: add main.go This makes it easier to play with. Updates golang/go#71732 Change-Id: If5ec810c051b0c12ec30891c9a431cc5ca06dcd9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649615 Reviewed-by: Keith Randall Reviewed-by: Keith Randall Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../cmd/reflectvaluecompare/main.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare/main.go diff --git a/go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare/main.go b/go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare/main.go new file mode 100644 index 00000000000..f3f9e163913 --- /dev/null +++ b/go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare/main.go @@ -0,0 +1,18 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The reflectvaluecompare command applies the reflectvaluecompare +// checker to the specified packages of Go source code. +// +// Run with: +// +// $ go run ./go/analysis/passes/reflectvaluecompare/cmd/reflectvaluecompare -- packages... +package main + +import ( + "golang.org/x/tools/go/analysis/passes/reflectvaluecompare" + "golang.org/x/tools/go/analysis/singlechecker" +) + +func main() { singlechecker.Main(reflectvaluecompare.Analyzer) } From 5dc980c6debffbe1b319cf554f28eaf100b9fc94 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Feb 2025 22:24:09 -0500 Subject: [PATCH 199/309] gopls/internal/test/integration/misc: fix "want" assembly MOVD, MOVL, MOV are all valid. The latter appears in riscv. Fixes golang/go#71956 Change-Id: I74aa3d9a47a20b44d398054e7184e984c6701ca0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652359 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/internal/test/integration/misc/webserver_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gopls/internal/test/integration/misc/webserver_test.go b/gopls/internal/test/integration/misc/webserver_test.go index 5153289941f..4b495dfa07e 100644 --- a/gopls/internal/test/integration/misc/webserver_test.go +++ b/gopls/internal/test/integration/misc/webserver_test.go @@ -605,8 +605,8 @@ func init() { { report := asmFor(`f\(123\)`) checkMatch(t, true, report, `TEXT.*example.com/a.init`) - checkMatch(t, true, report, `MOV. \$123`) - checkMatch(t, true, report, `MOV. \$456`) + checkMatch(t, true, report, `MOV.? \$123`) + checkMatch(t, true, report, `MOV.? \$456`) checkMatch(t, true, report, `CALL example.com/a.f`) } @@ -614,7 +614,7 @@ func init() { { report := asmFor(`f\(789\)`) checkMatch(t, true, report, `TEXT.*example.com/a.init`) - checkMatch(t, true, report, `MOV. \$789`) + checkMatch(t, true, report, `MOV.? \$789`) checkMatch(t, true, report, `CALL example.com/a.f`) } }) From 779331ac58c17baf109674a5754c0f0c630f695a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 26 Feb 2025 07:50:27 -0500 Subject: [PATCH 200/309] gopls/internal/test/integration/misc: only test asm on {arm,amd}64 Fixes golang/go#71956 for real this time Change-Id: I3db07168da163ea6c8fdeefda28f64b94fe2ed57 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652695 Reviewed-by: Robert Findley Commit-Queue: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- .../test/integration/misc/webserver_test.go | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/gopls/internal/test/integration/misc/webserver_test.go b/gopls/internal/test/integration/misc/webserver_test.go index 4b495dfa07e..79a6548ee3e 100644 --- a/gopls/internal/test/integration/misc/webserver_test.go +++ b/gopls/internal/test/integration/misc/webserver_test.go @@ -604,18 +604,24 @@ func init() { // Check that code in a package-level var initializer is found too. { report := asmFor(`f\(123\)`) - checkMatch(t, true, report, `TEXT.*example.com/a.init`) - checkMatch(t, true, report, `MOV.? \$123`) - checkMatch(t, true, report, `MOV.? \$456`) - checkMatch(t, true, report, `CALL example.com/a.f`) + switch runtime.GOARCH { + case "amd64", "arm64": + checkMatch(t, true, report, `TEXT.*example.com/a.init`) + checkMatch(t, true, report, `MOV.? \$123`) + checkMatch(t, true, report, `MOV.? \$456`) + checkMatch(t, true, report, `CALL example.com/a.f`) + } } // And code in a source-level init function. { report := asmFor(`f\(789\)`) - checkMatch(t, true, report, `TEXT.*example.com/a.init`) - checkMatch(t, true, report, `MOV.? \$789`) - checkMatch(t, true, report, `CALL example.com/a.f`) + switch runtime.GOARCH { + case "amd64", "arm64": + checkMatch(t, true, report, `TEXT.*example.com/a.init`) + checkMatch(t, true, report, `MOV.? \$789`) + checkMatch(t, true, report, `CALL example.com/a.f`) + } } }) } From d740adf9c34bc9f6c7944b62fd3fd15851ed8fc0 Mon Sep 17 00:00:00 2001 From: danztran Date: Wed, 26 Feb 2025 06:18:57 +0000 Subject: [PATCH 201/309] gopls/internal/settings: correct SemanticTokenTypes source fix golang/go#71964 Change-Id: I2694023636272ea971880865a4f2cb6d9192d7d5 GitHub-Last-Rev: c81d388cfe13667504cff275c969fc81587c6fc9 GitHub-Pull-Request: golang/tools#564 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652655 Reviewed-by: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Hongxiang Jiang --- gopls/internal/settings/settings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index dd353da64e9..11b06040181 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -1356,7 +1356,7 @@ func (o *Options) EnabledSemanticTokenModifiers() map[semtok.Modifier]bool { // EncodeSemanticTokenTypes returns a map of types to boolean. func (o *Options) EnabledSemanticTokenTypes() map[semtok.Type]bool { copy := make(map[semtok.Type]bool, len(o.SemanticTokenTypes)) - for k, v := range o.SemanticTokenModifiers { + for k, v := range o.SemanticTokenTypes { copy[semtok.Type(k)] = v } if o.NoSemanticString { From 63229bc79404d8cf2fe4e88ad569168fe251d993 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 26 Feb 2025 14:20:40 -0500 Subject: [PATCH 202/309] gopls/internal/analysis/gofix: register "alias" fact type Fixes golang/go#71982 Change-Id: I29535d430e2fb9da0915a1d6ec99d4a3ade8e4e8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652975 Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/gofix/gofix.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 237e5b0b58a..7323028aa31 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -30,12 +30,16 @@ import ( var doc string var Analyzer = &analysis.Analyzer{ - Name: "gofix", - Doc: analysisinternal.MustExtractDoc(doc, "gofix"), - URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", - Run: run, - FactTypes: []analysis.Fact{new(goFixInlineFuncFact), new(goFixInlineConstFact)}, - Requires: []*analysis.Analyzer{inspect.Analyzer}, + Name: "gofix", + Doc: analysisinternal.MustExtractDoc(doc, "gofix"), + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + Run: run, + FactTypes: []analysis.Fact{ + (*goFixInlineFuncFact)(nil), + (*goFixInlineConstFact)(nil), + (*goFixInlineAliasFact)(nil), + }, + Requires: []*analysis.Analyzer{inspect.Analyzer}, } // analyzer holds the state for this analysis. From 57b529ad205da65cbc7429c2cadd5d7c44055981 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 25 Feb 2025 11:10:41 -0500 Subject: [PATCH 203/309] doc/release/v0.18.0.md: add -fix flag Added the -fix flag to the command line for applying go:fix fixes. The given command prints the fixes, but does not apply them. Change-Id: Ia6dc100cf88e293453fbc6649f14aa0046572104 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652355 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/release/v0.18.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/doc/release/v0.18.0.md b/gopls/doc/release/v0.18.0.md index ba2c0184307..9aa0f9c9d07 100644 --- a/gopls/doc/release/v0.18.0.md +++ b/gopls/doc/release/v0.18.0.md @@ -106,7 +106,7 @@ gopls will suggest replacing `Ptr` in your code with `Pointer`. Use this command to apply such fixes en masse: ``` -$ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./... +$ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test -fix ./... ``` ## "Implementations" supports generics From 8f4b8cd6b69a761defc548aa8377b8306a881c20 Mon Sep 17 00:00:00 2001 From: Madeline Kalil Date: Thu, 27 Feb 2025 11:28:35 -0500 Subject: [PATCH 204/309] gopls/internal/golang: add package symbols comment Note and explain why we use only syntax and not type information to parse package symbols. Change-Id: I7498158cf633e82d4149f88fc7e8858babd66559 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653355 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/golang/symbols.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gopls/internal/golang/symbols.go b/gopls/internal/golang/symbols.go index db31baa69f2..53fbb663800 100644 --- a/gopls/internal/golang/symbols.go +++ b/gopls/internal/golang/symbols.go @@ -82,6 +82,9 @@ func DocumentSymbols(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand // The PackageSymbol data type contains the same fields as protocol.DocumentSymbol, with // an additional int field "File" that stores the index of that symbol's file in the // PackageSymbolsResult.Files. +// Symbols are gathered using syntax rather than type information because type checking is +// significantly slower. Syntax information provides enough value to the user without +// causing a lag when loading symbol information across different files. func PackageSymbols(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (command.PackageSymbolsResult, error) { ctx, done := event.Start(ctx, "source.PackageSymbols") defer done() From 1cc80ad525837f752d516a5827e78bce18755cd2 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 26 Feb 2025 13:35:43 -0500 Subject: [PATCH 205/309] internal/event/export/ocagent: delete We never use it, and OpenCensus is officially moribund. Change-Id: I0095f996c58954c238be7875694ee62dd721b3f2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653016 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan --- gopls/internal/cmd/cmd.go | 7 +- gopls/internal/cmd/usage/usage-v.hlp | 2 - gopls/internal/cmd/usage/usage.hlp | 2 - gopls/internal/debug/serve.go | 15 +- gopls/internal/lsprpc/lsprpc_test.go | 8 +- gopls/internal/test/integration/runner.go | 4 +- gopls/internal/test/marker/marker_test.go | 2 +- internal/event/export/ocagent/README.md | 139 ------- internal/event/export/ocagent/metrics.go | 213 ----------- internal/event/export/ocagent/metrics_test.go | 144 ------- internal/event/export/ocagent/ocagent.go | 358 ------------------ internal/event/export/ocagent/ocagent_test.go | 210 ---------- internal/event/export/ocagent/trace_test.go | 158 -------- internal/event/export/ocagent/wire/common.go | 101 ----- internal/event/export/ocagent/wire/core.go | 17 - internal/event/export/ocagent/wire/metrics.go | 204 ---------- .../event/export/ocagent/wire/metrics_test.go | 80 ---- internal/event/export/ocagent/wire/trace.go | 112 ------ 18 files changed, 10 insertions(+), 1766 deletions(-) delete mode 100644 internal/event/export/ocagent/README.md delete mode 100644 internal/event/export/ocagent/metrics.go delete mode 100644 internal/event/export/ocagent/metrics_test.go delete mode 100644 internal/event/export/ocagent/ocagent.go delete mode 100644 internal/event/export/ocagent/ocagent_test.go delete mode 100644 internal/event/export/ocagent/trace_test.go delete mode 100644 internal/event/export/ocagent/wire/common.go delete mode 100644 internal/event/export/ocagent/wire/core.go delete mode 100644 internal/event/export/ocagent/wire/metrics.go delete mode 100644 internal/event/export/ocagent/wire/metrics_test.go delete mode 100644 internal/event/export/ocagent/wire/trace.go diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index 119577c012b..4a00afc4115 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -63,9 +63,6 @@ type Application struct { // VeryVerbose enables a higher level of verbosity in logging output. VeryVerbose bool `flag:"vv,veryverbose" help:"very verbose output"` - // Control ocagent export of telemetry - OCAgent string `flag:"ocagent" help:"the address of the ocagent (e.g. http://localhost:55678), or off"` - // PrepareOptions is called to update the options when a new view is built. // It is primarily to allow the behavior of gopls to be modified by hooks. PrepareOptions func(*settings.Options) @@ -98,8 +95,6 @@ func (app *Application) verbose() bool { // New returns a new Application ready to run. func New() *Application { app := &Application{ - OCAgent: "off", //TODO: Remove this line to default the exporter to on - Serve: Serve{ RemoteListenTimeout: 1 * time.Minute, }, @@ -238,7 +233,7 @@ func (app *Application) Run(ctx context.Context, args ...string) error { // executable, and immediately runs a gc. filecache.Start() - ctx = debug.WithInstance(ctx, app.OCAgent) + ctx = debug.WithInstance(ctx) if len(args) == 0 { s := flag.NewFlagSet(app.Name(), flag.ExitOnError) return tool.Run(ctx, s, &app.Serve, args) diff --git a/gopls/internal/cmd/usage/usage-v.hlp b/gopls/internal/cmd/usage/usage-v.hlp index 64f99a3387e..044d4251e89 100644 --- a/gopls/internal/cmd/usage/usage-v.hlp +++ b/gopls/internal/cmd/usage/usage-v.hlp @@ -61,8 +61,6 @@ flags: filename to log to. if value is "auto", then logging to a default output file is enabled -mode=string no effect - -ocagent=string - the address of the ocagent (e.g. http://localhost:55678), or off (default "off") -port=int port on which to run gopls for debugging purposes -profile.alloc=string diff --git a/gopls/internal/cmd/usage/usage.hlp b/gopls/internal/cmd/usage/usage.hlp index c801a467626..b918b24a411 100644 --- a/gopls/internal/cmd/usage/usage.hlp +++ b/gopls/internal/cmd/usage/usage.hlp @@ -58,8 +58,6 @@ flags: filename to log to. if value is "auto", then logging to a default output file is enabled -mode=string no effect - -ocagent=string - the address of the ocagent (e.g. http://localhost:55678), or off (default "off") -port=int port on which to run gopls for debugging purposes -profile.alloc=string diff --git a/gopls/internal/debug/serve.go b/gopls/internal/debug/serve.go index c471f488cd1..7cfe2b3d23e 100644 --- a/gopls/internal/debug/serve.go +++ b/gopls/internal/debug/serve.go @@ -33,7 +33,6 @@ import ( "golang.org/x/tools/internal/event/core" "golang.org/x/tools/internal/event/export" "golang.org/x/tools/internal/event/export/metric" - "golang.org/x/tools/internal/event/export/ocagent" "golang.org/x/tools/internal/event/export/prometheus" "golang.org/x/tools/internal/event/keys" "golang.org/x/tools/internal/event/label" @@ -51,13 +50,11 @@ type Instance struct { Logfile string StartTime time.Time ServerAddress string - OCAgentConfig string LogWriter io.Writer exporter event.Exporter - ocagent *ocagent.Exporter prometheus *prometheus.Exporter rpcs *Rpcs traces *traces @@ -363,16 +360,11 @@ func GetInstance(ctx context.Context) *Instance { // WithInstance creates debug instance ready for use using the supplied // configuration and stores it in the returned context. -func WithInstance(ctx context.Context, agent string) context.Context { +func WithInstance(ctx context.Context) context.Context { i := &Instance{ - StartTime: time.Now(), - OCAgentConfig: agent, + StartTime: time.Now(), } i.LogWriter = os.Stderr - ocConfig := ocagent.Discover() - //TODO: we should not need to adjust the discovered configuration - ocConfig.Address = i.OCAgentConfig - i.ocagent = ocagent.Connect(ocConfig) i.prometheus = prometheus.New() i.rpcs = &Rpcs{} i.traces = &traces{} @@ -541,9 +533,6 @@ func messageType(l log.Level) protocol.MessageType { func makeInstanceExporter(i *Instance) event.Exporter { exporter := func(ctx context.Context, ev core.Event, lm label.Map) context.Context { - if i.ocagent != nil { - ctx = i.ocagent.ProcessEvent(ctx, ev, lm) - } if i.prometheus != nil { ctx = i.prometheus.ProcessEvent(ctx, ev, lm) } diff --git a/gopls/internal/lsprpc/lsprpc_test.go b/gopls/internal/lsprpc/lsprpc_test.go index 1a259bbd646..eda00b28c7a 100644 --- a/gopls/internal/lsprpc/lsprpc_test.go +++ b/gopls/internal/lsprpc/lsprpc_test.go @@ -58,7 +58,7 @@ func TestClientLogging(t *testing.T) { server := PingServer{} client := FakeClient{Logs: make(chan string, 10)} - ctx = debug.WithInstance(ctx, "") + ctx = debug.WithInstance(ctx) ss := NewStreamServer(cache.New(nil), false, nil).(*StreamServer) ss.serverForTest = server ts := servertest.NewPipeServer(ss, nil) @@ -121,7 +121,7 @@ func checkClose(t *testing.T, closer func() error) { func setupForwarding(ctx context.Context, t *testing.T, s protocol.Server) (direct, forwarded servertest.Connector, cleanup func()) { t.Helper() - serveCtx := debug.WithInstance(ctx, "") + serveCtx := debug.WithInstance(ctx) ss := NewStreamServer(cache.New(nil), false, nil).(*StreamServer) ss.serverForTest = s tsDirect := servertest.NewTCPServer(serveCtx, ss, nil) @@ -214,8 +214,8 @@ func TestDebugInfoLifecycle(t *testing.T) { baseCtx, cancel := context.WithCancel(context.Background()) defer cancel() - clientCtx := debug.WithInstance(baseCtx, "") - serverCtx := debug.WithInstance(baseCtx, "") + clientCtx := debug.WithInstance(baseCtx) + serverCtx := debug.WithInstance(baseCtx) cache := cache.New(nil) ss := NewStreamServer(cache, false, nil) diff --git a/gopls/internal/test/integration/runner.go b/gopls/internal/test/integration/runner.go index 6d10b16cab3..b3e98b859d3 100644 --- a/gopls/internal/test/integration/runner.go +++ b/gopls/internal/test/integration/runner.go @@ -173,7 +173,7 @@ func (r *Runner) Run(t *testing.T, files string, test TestFunc, opts ...RunOptio } // TODO(rfindley): do we need an instance at all? Can it be removed? - ctx = debug.WithInstance(ctx, "off") + ctx = debug.WithInstance(ctx) rootDir := filepath.Join(r.tempDir, filepath.FromSlash(t.Name())) if err := os.MkdirAll(rootDir, 0755); err != nil { @@ -349,7 +349,7 @@ func (r *Runner) defaultServer() jsonrpc2.StreamServer { func (r *Runner) forwardedServer() jsonrpc2.StreamServer { r.tsOnce.Do(func() { ctx := context.Background() - ctx = debug.WithInstance(ctx, "off") + ctx = debug.WithInstance(ctx) ss := lsprpc.NewStreamServer(cache.New(nil), false, nil) r.ts = servertest.NewTCPServer(ctx, ss, nil) }) diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index a5e23b928ad..a3e62d35968 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -964,7 +964,7 @@ func newEnv(t *testing.T, cache *cache.Cache, files, proxyFiles map[string][]byt // Put a debug instance in the context to prevent logging to stderr. // See associated TODO in runner.go: we should revisit this pattern. ctx := context.Background() - ctx = debug.WithInstance(ctx, "off") + ctx = debug.WithInstance(ctx) awaiter := integration.NewAwaiter(sandbox.Workdir) ss := lsprpc.NewStreamServer(cache, false, nil) diff --git a/internal/event/export/ocagent/README.md b/internal/event/export/ocagent/README.md deleted file mode 100644 index 22e8469f06b..00000000000 --- a/internal/event/export/ocagent/README.md +++ /dev/null @@ -1,139 +0,0 @@ -# Exporting Metrics and Traces with OpenCensus, Zipkin, and Prometheus - -This tutorial provides a minimum example to verify that metrics and traces -can be exported to OpenCensus from Go tools. - -## Setting up oragent - -1. Ensure you have [docker](https://www.docker.com/get-started) and [docker-compose](https://docs.docker.com/compose/install/). -2. Clone [oragent](https://github.com/orijtech/oragent). -3. In the oragent directory, start the services: -```bash -docker-compose up -``` -If everything goes well, you should see output resembling the following: -``` -Starting oragent_zipkin_1 ... done -Starting oragent_oragent_1 ... done -Starting oragent_prometheus_1 ... done -... -``` -* You can check the status of the OpenCensus agent using zPages at http://localhost:55679/debug/tracez. -* You can now access the Prometheus UI at http://localhost:9445. -* You can now access the Zipkin UI at http://localhost:9444. -4. To shut down oragent, hit Ctrl+C in the terminal. -5. You can also start oragent in detached mode by running `docker-compose up -d`. To stop oragent while detached, run `docker-compose down`. - -## Exporting Metrics and Traces -1. Clone the [tools](https://golang.org/x/tools) subrepository. -1. Inside `internal`, create a file named `main.go` with the following contents: -```go -package main - -import ( - "context" - "fmt" - "math/rand" - "net/http" - "time" - - "golang.org/x/tools/internal/event" - "golang.org/x/tools/internal/event/export" - "golang.org/x/tools/internal/event/export/metric" - "golang.org/x/tools/internal/event/export/ocagent" -) - -type testExporter struct { - metrics metric.Exporter - ocagent *ocagent.Exporter -} - -func (e *testExporter) ProcessEvent(ctx context.Context, ev event.Event) (context.Context, event.Event) { - ctx, ev = export.Tag(ctx, ev) - ctx, ev = export.ContextSpan(ctx, ev) - ctx, ev = e.metrics.ProcessEvent(ctx, ev) - ctx, ev = e.ocagent.ProcessEvent(ctx, ev) - return ctx, ev -} - -func main() { - exporter := &testExporter{} - - exporter.ocagent = ocagent.Connect(&ocagent.Config{ - Start: time.Now(), - Address: "http://127.0.0.1:55678", - Service: "go-tools-test", - Rate: 5 * time.Second, - Client: &http.Client{}, - }) - event.SetExporter(exporter) - - ctx := context.TODO() - mLatency := event.NewFloat64Key("latency", "the latency in milliseconds") - distribution := metric.HistogramFloat64Data{ - Info: &metric.HistogramFloat64{ - Name: "latencyDistribution", - Description: "the various latencies", - Buckets: []float64{0, 10, 50, 100, 200, 400, 800, 1000, 1400, 2000, 5000, 10000, 15000}, - }, - } - - distribution.Info.Record(&exporter.metrics, mLatency) - - for { - sleep := randomSleep() - _, end := event.StartSpan(ctx, "main.randomSleep()") - time.Sleep(time.Duration(sleep) * time.Millisecond) - end() - event.Record(ctx, mLatency.Of(float64(sleep))) - - fmt.Println("Latency: ", float64(sleep)) - } -} - -func randomSleep() int64 { - var max int64 - switch modulus := time.Now().Unix() % 5; modulus { - case 0: - max = 17001 - case 1: - max = 8007 - case 2: - max = 917 - case 3: - max = 87 - case 4: - max = 1173 - } - return rand.Int63n(max) -} - -``` -3. Run the new file from within the tools repository: -```bash -go run internal/main.go -``` -4. After about 5 seconds, OpenCensus should start receiving your new metrics, which you can see at http://localhost:8844/metrics. This page will look similar to the following: -``` -# HELP promdemo_latencyDistribution the various latencies -# TYPE promdemo_latencyDistribution histogram -promdemo_latencyDistribution_bucket{vendor="otc",le="0"} 0 -promdemo_latencyDistribution_bucket{vendor="otc",le="10"} 2 -promdemo_latencyDistribution_bucket{vendor="otc",le="50"} 9 -promdemo_latencyDistribution_bucket{vendor="otc",le="100"} 22 -promdemo_latencyDistribution_bucket{vendor="otc",le="200"} 35 -promdemo_latencyDistribution_bucket{vendor="otc",le="400"} 49 -promdemo_latencyDistribution_bucket{vendor="otc",le="800"} 63 -promdemo_latencyDistribution_bucket{vendor="otc",le="1000"} 78 -promdemo_latencyDistribution_bucket{vendor="otc",le="1400"} 93 -promdemo_latencyDistribution_bucket{vendor="otc",le="2000"} 108 -promdemo_latencyDistribution_bucket{vendor="otc",le="5000"} 123 -promdemo_latencyDistribution_bucket{vendor="otc",le="10000"} 138 -promdemo_latencyDistribution_bucket{vendor="otc",le="15000"} 153 -promdemo_latencyDistribution_bucket{vendor="otc",le="+Inf"} 15 -promdemo_latencyDistribution_sum{vendor="otc"} 1641 -promdemo_latencyDistribution_count{vendor="otc"} 15 -``` -5. After a few more seconds, Prometheus should start displaying your new metrics. You can view the distribution at http://localhost:9445/graph?g0.range_input=5m&g0.stacked=1&g0.expr=rate(oragent_latencyDistribution_bucket%5B5m%5D)&g0.tab=0. - -6. Zipkin should also start displaying traces. You can view them at http://localhost:9444/zipkin/?limit=10&lookback=300000&serviceName=go-tools-test. \ No newline at end of file diff --git a/internal/event/export/ocagent/metrics.go b/internal/event/export/ocagent/metrics.go deleted file mode 100644 index 78d65994db8..00000000000 --- a/internal/event/export/ocagent/metrics.go +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ocagent - -import ( - "time" - - "golang.org/x/tools/internal/event/export/metric" - "golang.org/x/tools/internal/event/export/ocagent/wire" - "golang.org/x/tools/internal/event/label" -) - -// dataToMetricDescriptor return a *wire.MetricDescriptor based on data. -func dataToMetricDescriptor(data metric.Data) *wire.MetricDescriptor { - if data == nil { - return nil - } - descriptor := &wire.MetricDescriptor{ - Name: data.Handle(), - Description: getDescription(data), - // TODO: Unit? - Type: dataToMetricDescriptorType(data), - LabelKeys: getLabelKeys(data), - } - - return descriptor -} - -// getDescription returns the description of data. -func getDescription(data metric.Data) string { - switch d := data.(type) { - case *metric.Int64Data: - return d.Info.Description - - case *metric.Float64Data: - return d.Info.Description - - case *metric.HistogramInt64Data: - return d.Info.Description - - case *metric.HistogramFloat64Data: - return d.Info.Description - } - - return "" -} - -// getLabelKeys returns a slice of *wire.LabelKeys based on the keys -// in data. -func getLabelKeys(data metric.Data) []*wire.LabelKey { - switch d := data.(type) { - case *metric.Int64Data: - return infoKeysToLabelKeys(d.Info.Keys) - - case *metric.Float64Data: - return infoKeysToLabelKeys(d.Info.Keys) - - case *metric.HistogramInt64Data: - return infoKeysToLabelKeys(d.Info.Keys) - - case *metric.HistogramFloat64Data: - return infoKeysToLabelKeys(d.Info.Keys) - } - - return nil -} - -// dataToMetricDescriptorType returns a wire.MetricDescriptor_Type based on the -// underlying type of data. -func dataToMetricDescriptorType(data metric.Data) wire.MetricDescriptor_Type { - switch d := data.(type) { - case *metric.Int64Data: - if d.IsGauge { - return wire.MetricDescriptor_GAUGE_INT64 - } - return wire.MetricDescriptor_CUMULATIVE_INT64 - - case *metric.Float64Data: - if d.IsGauge { - return wire.MetricDescriptor_GAUGE_DOUBLE - } - return wire.MetricDescriptor_CUMULATIVE_DOUBLE - - case *metric.HistogramInt64Data: - return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION - - case *metric.HistogramFloat64Data: - return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION - } - - return wire.MetricDescriptor_UNSPECIFIED -} - -// dataToTimeseries returns a slice of *wire.TimeSeries based on the -// points in data. -func dataToTimeseries(data metric.Data, start time.Time) []*wire.TimeSeries { - if data == nil { - return nil - } - - numRows := numRows(data) - startTimestamp := convertTimestamp(start) - timeseries := make([]*wire.TimeSeries, 0, numRows) - - for i := 0; i < numRows; i++ { - timeseries = append(timeseries, &wire.TimeSeries{ - StartTimestamp: &startTimestamp, - // TODO: labels? - Points: dataToPoints(data, i), - }) - } - - return timeseries -} - -// numRows returns the number of rows in data. -func numRows(data metric.Data) int { - switch d := data.(type) { - case *metric.Int64Data: - return len(d.Rows) - case *metric.Float64Data: - return len(d.Rows) - case *metric.HistogramInt64Data: - return len(d.Rows) - case *metric.HistogramFloat64Data: - return len(d.Rows) - } - - return 0 -} - -// dataToPoints returns an array of *wire.Points based on the point(s) -// in data at index i. -func dataToPoints(data metric.Data, i int) []*wire.Point { - switch d := data.(type) { - case *metric.Int64Data: - timestamp := convertTimestamp(d.EndTime) - return []*wire.Point{ - { - Value: wire.PointInt64Value{ - Int64Value: d.Rows[i], - }, - Timestamp: ×tamp, - }, - } - case *metric.Float64Data: - timestamp := convertTimestamp(d.EndTime) - return []*wire.Point{ - { - Value: wire.PointDoubleValue{ - DoubleValue: d.Rows[i], - }, - Timestamp: ×tamp, - }, - } - case *metric.HistogramInt64Data: - row := d.Rows[i] - bucketBounds := make([]float64, len(d.Info.Buckets)) - for i, val := range d.Info.Buckets { - bucketBounds[i] = float64(val) - } - return distributionToPoints(row.Values, row.Count, float64(row.Sum), bucketBounds, d.EndTime) - case *metric.HistogramFloat64Data: - row := d.Rows[i] - return distributionToPoints(row.Values, row.Count, row.Sum, d.Info.Buckets, d.EndTime) - } - - return nil -} - -// distributionToPoints returns an array of *wire.Points containing a -// wire.PointDistributionValue representing a distribution with the -// supplied counts, count, and sum. -func distributionToPoints(counts []int64, count int64, sum float64, bucketBounds []float64, end time.Time) []*wire.Point { - buckets := make([]*wire.Bucket, len(counts)) - for i := 0; i < len(counts); i++ { - buckets[i] = &wire.Bucket{ - Count: counts[i], - } - } - timestamp := convertTimestamp(end) - return []*wire.Point{ - { - Value: wire.PointDistributionValue{ - DistributionValue: &wire.DistributionValue{ - Count: count, - Sum: sum, - // TODO: SumOfSquaredDeviation? - Buckets: buckets, - BucketOptions: &wire.BucketOptionsExplicit{ - Bounds: bucketBounds, - }, - }, - }, - Timestamp: ×tamp, - }, - } -} - -// infoKeysToLabelKeys returns an array of *wire.LabelKeys containing the -// string values of the elements of labelKeys. -func infoKeysToLabelKeys(infoKeys []label.Key) []*wire.LabelKey { - labelKeys := make([]*wire.LabelKey, 0, len(infoKeys)) - for _, key := range infoKeys { - labelKeys = append(labelKeys, &wire.LabelKey{ - Key: key.Name(), - }) - } - - return labelKeys -} diff --git a/internal/event/export/ocagent/metrics_test.go b/internal/event/export/ocagent/metrics_test.go deleted file mode 100644 index 001e7f02dbf..00000000000 --- a/internal/event/export/ocagent/metrics_test.go +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright 2020 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ocagent_test - -import ( - "context" - "errors" - "testing" - - "golang.org/x/tools/internal/event" - "golang.org/x/tools/internal/event/keys" -) - -func TestEncodeMetric(t *testing.T) { - exporter := registerExporter() - const prefix = testNodeStr + ` - "metrics":[` - const suffix = `]}` - tests := []struct { - name string - run func(ctx context.Context) - want string - }{ - { - name: "HistogramFloat64, HistogramInt64", - run: func(ctx context.Context) { - ctx = event.Label(ctx, keyMethod.Of("godoc.ServeHTTP")) - event.Metric(ctx, latencyMs.Of(96.58)) - ctx = event.Label(ctx, keys.Err.Of(errors.New("panic: fatal signal"))) - event.Metric(ctx, bytesIn.Of(97e2)) - }, - want: prefix + ` - { - "metric_descriptor": { - "name": "latency_ms", - "description": "The latency of calls in milliseconds", - "type": 6, - "label_keys": [ - { - "key": "method" - }, - { - "key": "route" - } - ] - }, - "timeseries": [ - { - "start_timestamp": "1970-01-01T00:00:00Z", - "points": [ - { - "timestamp": "1970-01-01T00:00:40Z", - "distributionValue": { - "count": 1, - "sum": 96.58, - "bucket_options": { - "explicit": { - "bounds": [ - 0, - 5, - 10, - 25, - 50 - ] - } - }, - "buckets": [ - {}, - {}, - {}, - {}, - {} - ] - } - } - ] - } - ] - }, - { - "metric_descriptor": { - "name": "latency_ms", - "description": "The latency of calls in milliseconds", - "type": 6, - "label_keys": [ - { - "key": "method" - }, - { - "key": "route" - } - ] - }, - "timeseries": [ - { - "start_timestamp": "1970-01-01T00:00:00Z", - "points": [ - { - "timestamp": "1970-01-01T00:00:40Z", - "distributionValue": { - "count": 1, - "sum": 9700, - "bucket_options": { - "explicit": { - "bounds": [ - 0, - 10, - 50, - 100, - 500, - 1000, - 2000 - ] - } - }, - "buckets": [ - {}, - {}, - {}, - {}, - {}, - {}, - {} - ] - } - } - ] - } - ] - }` + suffix, - }, - } - - ctx := context.TODO() - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tt.run(ctx) - got := exporter.Output("/v1/metrics") - checkJSON(t, got, []byte(tt.want)) - }) - } -} diff --git a/internal/event/export/ocagent/ocagent.go b/internal/event/export/ocagent/ocagent.go deleted file mode 100644 index d86c4aed0cf..00000000000 --- a/internal/event/export/ocagent/ocagent.go +++ /dev/null @@ -1,358 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package ocagent adds the ability to export all telemetry to an ocagent. -// This keeps the compile time dependencies to zero and allows the agent to -// have the exporters needed for telemetry aggregation and viewing systems. -package ocagent - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "net/http" - "os" - "path/filepath" - "sync" - "time" - - "golang.org/x/tools/internal/event" - "golang.org/x/tools/internal/event/core" - "golang.org/x/tools/internal/event/export" - "golang.org/x/tools/internal/event/export/metric" - "golang.org/x/tools/internal/event/export/ocagent/wire" - "golang.org/x/tools/internal/event/keys" - "golang.org/x/tools/internal/event/label" -) - -type Config struct { - Start time.Time - Host string - Process uint32 - Client *http.Client - Service string - Address string - Rate time.Duration -} - -var ( - connectMu sync.Mutex - exporters = make(map[Config]*Exporter) -) - -// Discover finds the local agent to export to, it will return nil if there -// is not one running. -// TODO: Actually implement a discovery protocol rather than a hard coded address -func Discover() *Config { - return &Config{ - Address: "http://localhost:55678", - } -} - -type Exporter struct { - mu sync.Mutex - config Config - spans []*export.Span - metrics []metric.Data -} - -// Connect creates a process specific exporter with the specified -// serviceName and the address of the ocagent to which it will upload -// its telemetry. -func Connect(config *Config) *Exporter { - if config == nil || config.Address == "off" { - return nil - } - resolved := *config - if resolved.Host == "" { - hostname, _ := os.Hostname() - resolved.Host = hostname - } - if resolved.Process == 0 { - resolved.Process = uint32(os.Getpid()) - } - if resolved.Client == nil { - resolved.Client = http.DefaultClient - } - if resolved.Service == "" { - resolved.Service = filepath.Base(os.Args[0]) - } - if resolved.Rate == 0 { - resolved.Rate = 2 * time.Second - } - - connectMu.Lock() - defer connectMu.Unlock() - if exporter, found := exporters[resolved]; found { - return exporter - } - exporter := &Exporter{config: resolved} - exporters[resolved] = exporter - if exporter.config.Start.IsZero() { - exporter.config.Start = time.Now() - } - go func() { - for range time.Tick(exporter.config.Rate) { - exporter.Flush() - } - }() - return exporter -} - -func (e *Exporter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) context.Context { - switch { - case event.IsEnd(ev): - e.mu.Lock() - defer e.mu.Unlock() - span := export.GetSpan(ctx) - if span != nil { - e.spans = append(e.spans, span) - } - case event.IsMetric(ev): - e.mu.Lock() - defer e.mu.Unlock() - data := metric.Entries.Get(lm).([]metric.Data) - e.metrics = append(e.metrics, data...) - } - return ctx -} - -func (e *Exporter) Flush() { - e.mu.Lock() - defer e.mu.Unlock() - spans := make([]*wire.Span, len(e.spans)) - for i, s := range e.spans { - spans[i] = convertSpan(s) - } - e.spans = nil - metrics := make([]*wire.Metric, len(e.metrics)) - for i, m := range e.metrics { - metrics[i] = convertMetric(m, e.config.Start) - } - e.metrics = nil - - if len(spans) > 0 { - e.send("/v1/trace", &wire.ExportTraceServiceRequest{ - Node: e.config.buildNode(), - Spans: spans, - //TODO: Resource? - }) - } - if len(metrics) > 0 { - e.send("/v1/metrics", &wire.ExportMetricsServiceRequest{ - Node: e.config.buildNode(), - Metrics: metrics, - //TODO: Resource? - }) - } -} - -func (cfg *Config) buildNode() *wire.Node { - return &wire.Node{ - Identifier: &wire.ProcessIdentifier{ - HostName: cfg.Host, - Pid: cfg.Process, - StartTimestamp: convertTimestamp(cfg.Start), - }, - LibraryInfo: &wire.LibraryInfo{ - Language: wire.LanguageGo, - ExporterVersion: "0.0.1", - CoreLibraryVersion: "x/tools", - }, - ServiceInfo: &wire.ServiceInfo{ - Name: cfg.Service, - }, - } -} - -func (e *Exporter) send(endpoint string, message any) { - blob, err := json.Marshal(message) - if err != nil { - errorInExport("ocagent failed to marshal message for %v: %v", endpoint, err) - return - } - uri := e.config.Address + endpoint - req, err := http.NewRequest("POST", uri, bytes.NewReader(blob)) - if err != nil { - errorInExport("ocagent failed to build request for %v: %v", uri, err) - return - } - req.Header.Set("Content-Type", "application/json") - res, err := e.config.Client.Do(req) - if err != nil { - errorInExport("ocagent failed to send message: %v \n", err) - return - } - if res.Body != nil { - res.Body.Close() - } -} - -func errorInExport(message string, args ...any) { - // This function is useful when debugging the exporter, but in general we - // want to just drop any export -} - -func convertTimestamp(t time.Time) wire.Timestamp { - return t.Format(time.RFC3339Nano) -} - -func toTruncatableString(s string) *wire.TruncatableString { - if s == "" { - return nil - } - return &wire.TruncatableString{Value: s} -} - -func convertSpan(span *export.Span) *wire.Span { - result := &wire.Span{ - TraceID: span.ID.TraceID[:], - SpanID: span.ID.SpanID[:], - TraceState: nil, //TODO? - ParentSpanID: span.ParentID[:], - Name: toTruncatableString(span.Name), - Kind: wire.UnspecifiedSpanKind, - StartTime: convertTimestamp(span.Start().At()), - EndTime: convertTimestamp(span.Finish().At()), - Attributes: convertAttributes(span.Start(), 1), - TimeEvents: convertEvents(span.Events()), - SameProcessAsParentSpan: true, - //TODO: StackTrace? - //TODO: Links? - //TODO: Status? - //TODO: Resource? - } - return result -} - -func convertMetric(data metric.Data, start time.Time) *wire.Metric { - descriptor := dataToMetricDescriptor(data) - timeseries := dataToTimeseries(data, start) - - if descriptor == nil && timeseries == nil { - return nil - } - - // TODO: handle Histogram metrics - return &wire.Metric{ - MetricDescriptor: descriptor, - Timeseries: timeseries, - // TODO: attach Resource? - } -} - -func skipToValidLabel(list label.List, index int) (int, label.Label) { - // skip to the first valid label - for ; list.Valid(index); index++ { - l := list.Label(index) - if !l.Valid() || l.Key() == keys.Label { - continue - } - return index, l - } - return -1, label.Label{} -} - -func convertAttributes(list label.List, index int) *wire.Attributes { - index, l := skipToValidLabel(list, index) - if !l.Valid() { - return nil - } - attributes := make(map[string]wire.Attribute) - for { - if l.Valid() { - attributes[l.Key().Name()] = convertAttribute(l) - } - index++ - if !list.Valid(index) { - return &wire.Attributes{AttributeMap: attributes} - } - l = list.Label(index) - } -} - -func convertAttribute(l label.Label) wire.Attribute { - switch key := l.Key().(type) { - case *keys.Int: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.Int8: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.Int16: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.Int32: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.Int64: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.UInt: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.UInt8: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.UInt16: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.UInt32: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.UInt64: - return wire.IntAttribute{IntValue: int64(key.From(l))} - case *keys.Float32: - return wire.DoubleAttribute{DoubleValue: float64(key.From(l))} - case *keys.Float64: - return wire.DoubleAttribute{DoubleValue: key.From(l)} - case *keys.Boolean: - return wire.BoolAttribute{BoolValue: key.From(l)} - case *keys.String: - return wire.StringAttribute{StringValue: toTruncatableString(key.From(l))} - case *keys.Error: - return wire.StringAttribute{StringValue: toTruncatableString(key.From(l).Error())} - case *keys.Value: - return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(key.From(l)))} - default: - return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprintf("%T", key))} - } -} - -func convertEvents(events []core.Event) *wire.TimeEvents { - //TODO: MessageEvents? - result := make([]wire.TimeEvent, len(events)) - for i, event := range events { - result[i] = convertEvent(event) - } - return &wire.TimeEvents{TimeEvent: result} -} - -func convertEvent(ev core.Event) wire.TimeEvent { - return wire.TimeEvent{ - Time: convertTimestamp(ev.At()), - Annotation: convertAnnotation(ev), - } -} - -func getAnnotationDescription(ev core.Event) (string, int) { - l := ev.Label(0) - if l.Key() != keys.Msg { - return "", 0 - } - if msg := keys.Msg.From(l); msg != "" { - return msg, 1 - } - l = ev.Label(1) - if l.Key() != keys.Err { - return "", 1 - } - if err := keys.Err.From(l); err != nil { - return err.Error(), 2 - } - return "", 2 -} - -func convertAnnotation(ev core.Event) *wire.Annotation { - description, index := getAnnotationDescription(ev) - if _, l := skipToValidLabel(ev, index); !l.Valid() && description == "" { - return nil - } - return &wire.Annotation{ - Description: toTruncatableString(description), - Attributes: convertAttributes(ev, index), - } -} diff --git a/internal/event/export/ocagent/ocagent_test.go b/internal/event/export/ocagent/ocagent_test.go deleted file mode 100644 index 38a52faede5..00000000000 --- a/internal/event/export/ocagent/ocagent_test.go +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ocagent_test - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "sync" - "testing" - "time" - - "golang.org/x/tools/internal/event" - "golang.org/x/tools/internal/event/core" - "golang.org/x/tools/internal/event/export" - "golang.org/x/tools/internal/event/export/metric" - "golang.org/x/tools/internal/event/export/ocagent" - "golang.org/x/tools/internal/event/keys" - "golang.org/x/tools/internal/event/label" -) - -const testNodeStr = `{ - "node":{ - "identifier":{ - "host_name":"tester", - "pid":1, - "start_timestamp":"1970-01-01T00:00:00Z" - }, - "library_info":{ - "language":4, - "exporter_version":"0.0.1", - "core_library_version":"x/tools" - }, - "service_info":{ - "name":"ocagent-tests" - } - },` - -var ( - keyDB = keys.NewString("db", "the database name") - keyMethod = keys.NewString("method", "a metric grouping key") - keyRoute = keys.NewString("route", "another metric grouping key") - - key1DB = keys.NewString("1_db", "A test string key") - - key2aAge = keys.NewFloat64("2a_age", "A test float64 key") - key2bTTL = keys.NewFloat32("2b_ttl", "A test float32 key") - key2cExpiryMS = keys.NewFloat64("2c_expiry_ms", "A test float64 key") - - key3aRetry = keys.NewBoolean("3a_retry", "A test boolean key") - key3bStale = keys.NewBoolean("3b_stale", "Another test boolean key") - - key4aMax = keys.NewInt("4a_max", "A test int key") - key4bOpcode = keys.NewInt8("4b_opcode", "A test int8 key") - key4cBase = keys.NewInt16("4c_base", "A test int16 key") - key4eChecksum = keys.NewInt32("4e_checksum", "A test int32 key") - key4fMode = keys.NewInt64("4f_mode", "A test int64 key") - - key5aMin = keys.NewUInt("5a_min", "A test uint key") - key5bMix = keys.NewUInt8("5b_mix", "A test uint8 key") - key5cPort = keys.NewUInt16("5c_port", "A test uint16 key") - key5dMinHops = keys.NewUInt32("5d_min_hops", "A test uint32 key") - key5eMaxHops = keys.NewUInt64("5e_max_hops", "A test uint64 key") - - recursiveCalls = keys.NewInt64("recursive_calls", "Number of recursive calls") - bytesIn = keys.NewInt64("bytes_in", "Number of bytes in") //, unit.Bytes) - latencyMs = keys.NewFloat64("latency", "The latency in milliseconds") //, unit.Milliseconds) - - metricLatency = metric.HistogramFloat64{ - Name: "latency_ms", - Description: "The latency of calls in milliseconds", - Keys: []label.Key{keyMethod, keyRoute}, - Buckets: []float64{0, 5, 10, 25, 50}, - } - - metricBytesIn = metric.HistogramInt64{ - Name: "latency_ms", - Description: "The latency of calls in milliseconds", - Keys: []label.Key{keyMethod, keyRoute}, - Buckets: []int64{0, 10, 50, 100, 500, 1000, 2000}, - } - - metricRecursiveCalls = metric.Scalar{ - Name: "latency_ms", - Description: "The latency of calls in milliseconds", - Keys: []label.Key{keyMethod, keyRoute}, - } -) - -type testExporter struct { - ocagent *ocagent.Exporter - sent fakeSender -} - -func registerExporter() *testExporter { - exporter := &testExporter{} - cfg := ocagent.Config{ - Host: "tester", - Process: 1, - Service: "ocagent-tests", - Client: &http.Client{Transport: &exporter.sent}, - } - cfg.Start, _ = time.Parse(time.RFC3339Nano, "1970-01-01T00:00:00Z") - exporter.ocagent = ocagent.Connect(&cfg) - - metrics := metric.Config{} - metricLatency.Record(&metrics, latencyMs) - metricBytesIn.Record(&metrics, bytesIn) - metricRecursiveCalls.SumInt64(&metrics, recursiveCalls) - - e := exporter.ocagent.ProcessEvent - e = metrics.Exporter(e) - e = spanFixer(e) - e = export.Spans(e) - e = export.Labels(e) - e = timeFixer(e) - event.SetExporter(e) - return exporter -} - -func timeFixer(output event.Exporter) event.Exporter { - start, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:30Z") - at, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:40Z") - end, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:50Z") - return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { - switch { - case event.IsStart(ev): - ev = core.CloneEvent(ev, start) - case event.IsEnd(ev): - ev = core.CloneEvent(ev, end) - default: - ev = core.CloneEvent(ev, at) - } - return output(ctx, ev, lm) - } -} - -func spanFixer(output event.Exporter) event.Exporter { - return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { - if event.IsStart(ev) { - span := export.GetSpan(ctx) - span.ID = export.SpanContext{} - } - return output(ctx, ev, lm) - } -} - -func (e *testExporter) Output(route string) []byte { - e.ocagent.Flush() - return e.sent.get(route) -} - -func checkJSON(t *testing.T, got, want []byte) { - // compare the compact form, to allow for formatting differences - g := &bytes.Buffer{} - if err := json.Compact(g, got); err != nil { - t.Fatal(err) - } - w := &bytes.Buffer{} - if err := json.Compact(w, want); err != nil { - t.Fatal(err) - } - if g.String() != w.String() { - t.Fatalf("Got:\n%s\nWant:\n%s", g, w) - } -} - -type fakeSender struct { - mu sync.Mutex - data map[string][]byte -} - -func (s *fakeSender) get(route string) []byte { - s.mu.Lock() - defer s.mu.Unlock() - data, found := s.data[route] - if found { - delete(s.data, route) - } - return data -} - -func (s *fakeSender) RoundTrip(req *http.Request) (*http.Response, error) { - s.mu.Lock() - defer s.mu.Unlock() - if s.data == nil { - s.data = make(map[string][]byte) - } - data, err := io.ReadAll(req.Body) - if err != nil { - return nil, err - } - path := req.URL.EscapedPath() - if _, found := s.data[path]; found { - return nil, fmt.Errorf("duplicate delivery to %v", path) - } - s.data[path] = data - return &http.Response{ - Status: "200 OK", - StatusCode: 200, - Proto: "HTTP/1.0", - ProtoMajor: 1, - ProtoMinor: 0, - }, nil -} diff --git a/internal/event/export/ocagent/trace_test.go b/internal/event/export/ocagent/trace_test.go deleted file mode 100644 index 99def34d149..00000000000 --- a/internal/event/export/ocagent/trace_test.go +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ocagent_test - -import ( - "context" - "errors" - "testing" - - "golang.org/x/tools/internal/event" -) - -func TestTrace(t *testing.T) { - exporter := registerExporter() - const prefix = testNodeStr + ` - "spans":[{ - "trace_id":"AAAAAAAAAAAAAAAAAAAAAA==", - "span_id":"AAAAAAAAAAA=", - "parent_span_id":"AAAAAAAAAAA=", - "name":{"value":"event span"}, - "start_time":"1970-01-01T00:00:30Z", - "end_time":"1970-01-01T00:00:50Z", - "time_events":{ -` - const suffix = ` - }, - "same_process_as_parent_span":true - }] -}` - - tests := []struct { - name string - run func(ctx context.Context) - want string - }{ - { - name: "no labels", - run: func(ctx context.Context) { - event.Label(ctx) - }, - want: prefix + ` - "timeEvent":[{"time":"1970-01-01T00:00:40Z"}] - ` + suffix, - }, - { - name: "description no error", - run: func(ctx context.Context) { - event.Log(ctx, "cache miss", keyDB.Of("godb")) - }, - want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{ -"description": { "value": "cache miss" }, -"attributes": { - "attributeMap": { - "db": { "stringValue": { "value": "godb" } } - } -} -}}]` + suffix, - }, - - { - name: "description and error", - run: func(ctx context.Context) { - event.Error(ctx, "cache miss", - errors.New("no network connectivity"), - keyDB.Of("godb"), - ) - }, - want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{ -"description": { "value": "cache miss" }, -"attributes": { - "attributeMap": { - "db": { "stringValue": { "value": "godb" } }, - "error": { "stringValue": { "value": "no network connectivity" } } - } -} -}}]` + suffix, - }, - { - name: "no description, but error", - run: func(ctx context.Context) { - event.Error(ctx, "", - errors.New("no network connectivity"), - keyDB.Of("godb"), - ) - }, - want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{ -"description": { "value": "no network connectivity" }, -"attributes": { - "attributeMap": { - "db": { "stringValue": { "value": "godb" } } - } -} -}}]` + suffix, - }, - { - name: "enumerate all attribute types", - run: func(ctx context.Context) { - event.Log(ctx, "cache miss", - key1DB.Of("godb"), - - key2aAge.Of(0.456), // Constant converted into "float64" - key2bTTL.Of(float32(5000)), - key2cExpiryMS.Of(float64(1e3)), - - key3aRetry.Of(false), - key3bStale.Of(true), - - key4aMax.Of(0x7fff), // Constant converted into "int" - key4bOpcode.Of(int8(0x7e)), - key4cBase.Of(int16(1<<9)), - key4eChecksum.Of(int32(0x11f7e294)), - key4fMode.Of(int64(0644)), - - key5aMin.Of(uint(1)), - key5bMix.Of(uint8(44)), - key5cPort.Of(uint16(55678)), - key5dMinHops.Of(uint32(1<<9)), - key5eMaxHops.Of(uint64(0xffffff)), - ) - }, - want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{ -"description": { "value": "cache miss" }, -"attributes": { - "attributeMap": { - "1_db": { "stringValue": { "value": "godb" } }, - "2a_age": { "doubleValue": 0.456 }, - "2b_ttl": { "doubleValue": 5000 }, - "2c_expiry_ms": { "doubleValue": 1000 }, - "3a_retry": {}, - "3b_stale": { "boolValue": true }, - "4a_max": { "intValue": 32767 }, - "4b_opcode": { "intValue": 126 }, - "4c_base": { "intValue": 512 }, - "4e_checksum": { "intValue": 301458068 }, - "4f_mode": { "intValue": 420 }, - "5a_min": { "intValue": 1 }, - "5b_mix": { "intValue": 44 }, - "5c_port": { "intValue": 55678 }, - "5d_min_hops": { "intValue": 512 }, - "5e_max_hops": { "intValue": 16777215 } - } -} -}}]` + suffix, - }, - } - ctx := context.TODO() - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - ctx, done := event.Start(ctx, "event span") - tt.run(ctx) - done() - got := exporter.Output("/v1/trace") - checkJSON(t, got, []byte(tt.want)) - }) - } -} diff --git a/internal/event/export/ocagent/wire/common.go b/internal/event/export/ocagent/wire/common.go deleted file mode 100644 index f22b535654c..00000000000 --- a/internal/event/export/ocagent/wire/common.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package wire - -// This file holds common ocagent types - -type Node struct { - Identifier *ProcessIdentifier `json:"identifier,omitempty"` - LibraryInfo *LibraryInfo `json:"library_info,omitempty"` - ServiceInfo *ServiceInfo `json:"service_info,omitempty"` - Attributes map[string]string `json:"attributes,omitempty"` -} - -type Resource struct { - Type string `json:"type,omitempty"` - Labels map[string]string `json:"labels,omitempty"` -} - -type TruncatableString struct { - Value string `json:"value,omitempty"` - TruncatedByteCount int32 `json:"truncated_byte_count,omitempty"` -} - -type Attributes struct { - AttributeMap map[string]Attribute `json:"attributeMap,omitempty"` - DroppedAttributesCount int32 `json:"dropped_attributes_count,omitempty"` -} - -type StringAttribute struct { - StringValue *TruncatableString `json:"stringValue,omitempty"` -} - -type IntAttribute struct { - IntValue int64 `json:"intValue,omitempty"` -} - -type BoolAttribute struct { - BoolValue bool `json:"boolValue,omitempty"` -} - -type DoubleAttribute struct { - DoubleValue float64 `json:"doubleValue,omitempty"` -} - -type Attribute interface { - labelAttribute() -} - -func (StringAttribute) labelAttribute() {} -func (IntAttribute) labelAttribute() {} -func (BoolAttribute) labelAttribute() {} -func (DoubleAttribute) labelAttribute() {} - -type StackTrace struct { - StackFrames *StackFrames `json:"stack_frames,omitempty"` - StackTraceHashID uint64 `json:"stack_trace_hash_id,omitempty"` -} - -type StackFrames struct { - Frame []*StackFrame `json:"frame,omitempty"` - DroppedFramesCount int32 `json:"dropped_frames_count,omitempty"` -} - -type StackFrame struct { - FunctionName *TruncatableString `json:"function_name,omitempty"` - OriginalFunctionName *TruncatableString `json:"original_function_name,omitempty"` - FileName *TruncatableString `json:"file_name,omitempty"` - LineNumber int64 `json:"line_number,omitempty"` - ColumnNumber int64 `json:"column_number,omitempty"` - LoadModule *Module `json:"load_module,omitempty"` - SourceVersion *TruncatableString `json:"source_version,omitempty"` -} - -type Module struct { - Module *TruncatableString `json:"module,omitempty"` - BuildID *TruncatableString `json:"build_id,omitempty"` -} - -type ProcessIdentifier struct { - HostName string `json:"host_name,omitempty"` - Pid uint32 `json:"pid,omitempty"` - StartTimestamp Timestamp `json:"start_timestamp,omitempty"` -} - -type LibraryInfo struct { - Language Language `json:"language,omitempty"` - ExporterVersion string `json:"exporter_version,omitempty"` - CoreLibraryVersion string `json:"core_library_version,omitempty"` -} - -type Language int32 - -const ( - LanguageGo Language = 4 -) - -type ServiceInfo struct { - Name string `json:"name,omitempty"` -} diff --git a/internal/event/export/ocagent/wire/core.go b/internal/event/export/ocagent/wire/core.go deleted file mode 100644 index 95c05d66906..00000000000 --- a/internal/event/export/ocagent/wire/core.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package wire - -// This file contains type that match core proto types - -type Timestamp = string - -type Int64Value struct { - Value int64 `json:"value,omitempty"` -} - -type DoubleValue struct { - Value float64 `json:"value,omitempty"` -} diff --git a/internal/event/export/ocagent/wire/metrics.go b/internal/event/export/ocagent/wire/metrics.go deleted file mode 100644 index 6cb58943c00..00000000000 --- a/internal/event/export/ocagent/wire/metrics.go +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package wire - -import ( - "encoding/json" - "fmt" -) - -type ExportMetricsServiceRequest struct { - Node *Node `json:"node,omitempty"` - Metrics []*Metric `json:"metrics,omitempty"` - Resource *Resource `json:"resource,omitempty"` -} - -type Metric struct { - MetricDescriptor *MetricDescriptor `json:"metric_descriptor,omitempty"` - Timeseries []*TimeSeries `json:"timeseries,omitempty"` - Resource *Resource `json:"resource,omitempty"` -} - -type MetricDescriptor struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - Unit string `json:"unit,omitempty"` - Type MetricDescriptor_Type `json:"type,omitempty"` - LabelKeys []*LabelKey `json:"label_keys,omitempty"` -} - -type MetricDescriptor_Type int32 - -const ( - MetricDescriptor_UNSPECIFIED MetricDescriptor_Type = 0 - MetricDescriptor_GAUGE_INT64 MetricDescriptor_Type = 1 - MetricDescriptor_GAUGE_DOUBLE MetricDescriptor_Type = 2 - MetricDescriptor_GAUGE_DISTRIBUTION MetricDescriptor_Type = 3 - MetricDescriptor_CUMULATIVE_INT64 MetricDescriptor_Type = 4 - MetricDescriptor_CUMULATIVE_DOUBLE MetricDescriptor_Type = 5 - MetricDescriptor_CUMULATIVE_DISTRIBUTION MetricDescriptor_Type = 6 - MetricDescriptor_SUMMARY MetricDescriptor_Type = 7 -) - -type LabelKey struct { - Key string `json:"key,omitempty"` - Description string `json:"description,omitempty"` -} - -type TimeSeries struct { - StartTimestamp *Timestamp `json:"start_timestamp,omitempty"` - LabelValues []*LabelValue `json:"label_values,omitempty"` - Points []*Point `json:"points,omitempty"` -} - -type LabelValue struct { - Value string `json:"value,omitempty"` - HasValue bool `json:"has_value,omitempty"` -} - -type Point struct { - Timestamp *Timestamp `json:"timestamp,omitempty"` - Value PointValue `json:"value,omitempty"` -} - -type PointInt64Value struct { - Int64Value int64 `json:"int64Value,omitempty"` -} - -// MarshalJSON creates JSON formatted the same way as jsonpb so that the -// OpenCensus service can correctly determine the underlying value type. -// This custom MarshalJSON exists because, -// by default *Point is JSON marshalled as: -// -// {"value": {"int64Value": 1}} -// -// but it should be marshalled as: -// -// {"int64Value": 1} -func (p *Point) MarshalJSON() ([]byte, error) { - if p == nil { - return []byte("null"), nil - } - - switch d := p.Value.(type) { - case PointInt64Value: - return json.Marshal(&struct { - Timestamp *Timestamp `json:"timestamp,omitempty"` - Value int64 `json:"int64Value,omitempty"` - }{ - Timestamp: p.Timestamp, - Value: d.Int64Value, - }) - case PointDoubleValue: - return json.Marshal(&struct { - Timestamp *Timestamp `json:"timestamp,omitempty"` - Value float64 `json:"doubleValue,omitempty"` - }{ - Timestamp: p.Timestamp, - Value: d.DoubleValue, - }) - case PointDistributionValue: - return json.Marshal(&struct { - Timestamp *Timestamp `json:"timestamp,omitempty"` - Value *DistributionValue `json:"distributionValue,omitempty"` - }{ - Timestamp: p.Timestamp, - Value: d.DistributionValue, - }) - default: - return nil, fmt.Errorf("unknown point type %T", p.Value) - } -} - -type PointDoubleValue struct { - DoubleValue float64 `json:"doubleValue,omitempty"` -} - -type PointDistributionValue struct { - DistributionValue *DistributionValue `json:"distributionValue,omitempty"` -} - -type PointSummaryValue struct { - SummaryValue *SummaryValue `json:"summaryValue,omitempty"` -} - -type PointValue interface { - labelPointValue() -} - -func (PointInt64Value) labelPointValue() {} -func (PointDoubleValue) labelPointValue() {} -func (PointDistributionValue) labelPointValue() {} -func (PointSummaryValue) labelPointValue() {} - -type DistributionValue struct { - Count int64 `json:"count,omitempty"` - Sum float64 `json:"sum,omitempty"` - SumOfSquaredDeviation float64 `json:"sum_of_squared_deviation,omitempty"` - BucketOptions BucketOptions `json:"bucket_options,omitempty"` - Buckets []*Bucket `json:"buckets,omitempty"` -} - -type BucketOptionsExplicit struct { - Bounds []float64 `json:"bounds,omitempty"` -} - -type BucketOptions interface { - labelBucketOptions() -} - -func (*BucketOptionsExplicit) labelBucketOptions() {} - -var _ BucketOptions = (*BucketOptionsExplicit)(nil) -var _ json.Marshaler = (*BucketOptionsExplicit)(nil) - -// Declared for the purpose of custom JSON marshaling without cycles. -type bucketOptionsExplicitAlias BucketOptionsExplicit - -// MarshalJSON creates JSON formatted the same way as jsonpb so that the -// OpenCensus service can correctly determine the underlying value type. -// This custom MarshalJSON exists because, -// by default BucketOptionsExplicit is JSON marshalled as: -// -// {"bounds":[1,2,3]} -// -// but it should be marshalled as: -// -// {"explicit":{"bounds":[1,2,3]}} -func (be *BucketOptionsExplicit) MarshalJSON() ([]byte, error) { - return json.Marshal(&struct { - Explicit *bucketOptionsExplicitAlias `json:"explicit,omitempty"` - }{ - Explicit: (*bucketOptionsExplicitAlias)(be), - }) -} - -type Bucket struct { - Count int64 `json:"count,omitempty"` - Exemplar *Exemplar `json:"exemplar,omitempty"` -} - -type Exemplar struct { - Value float64 `json:"value,omitempty"` - Timestamp *Timestamp `json:"timestamp,omitempty"` - Attachments map[string]string `json:"attachments,omitempty"` -} - -type SummaryValue struct { - Count *Int64Value `json:"count,omitempty"` - Sum *DoubleValue `json:"sum,omitempty"` - Snapshot *Snapshot `json:"snapshot,omitempty"` -} - -type Snapshot struct { - Count *Int64Value `json:"count,omitempty"` - Sum *DoubleValue `json:"sum,omitempty"` - PercentileValues []*SnapshotValueAtPercentile `json:"percentile_values,omitempty"` -} - -type SnapshotValueAtPercentile struct { - Percentile float64 `json:"percentile,omitempty"` - Value float64 `json:"value,omitempty"` -} diff --git a/internal/event/export/ocagent/wire/metrics_test.go b/internal/event/export/ocagent/wire/metrics_test.go deleted file mode 100644 index 34247ad6332..00000000000 --- a/internal/event/export/ocagent/wire/metrics_test.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2020 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package wire - -import ( - "reflect" - "testing" -) - -func TestMarshalJSON(t *testing.T) { - tests := []struct { - name string - pt *Point - want string - }{ - { - "PointInt64", - &Point{ - Value: PointInt64Value{ - Int64Value: 5, - }, - }, - `{"int64Value":5}`, - }, - { - "PointDouble", - &Point{ - Value: PointDoubleValue{ - DoubleValue: 3.14, - }, - }, - `{"doubleValue":3.14}`, - }, - { - "PointDistribution", - &Point{ - Value: PointDistributionValue{ - DistributionValue: &DistributionValue{ - Count: 3, - Sum: 10, - Buckets: []*Bucket{ - { - Count: 1, - }, - { - Count: 2, - }, - }, - BucketOptions: &BucketOptionsExplicit{ - Bounds: []float64{ - 0, 5, - }, - }, - }, - }, - }, - `{"distributionValue":{"count":3,"sum":10,"bucket_options":{"explicit":{"bounds":[0,5]}},"buckets":[{"count":1},{"count":2}]}}`, - }, - { - "nil point", - nil, - `null`, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - buf, err := tt.pt.MarshalJSON() - if err != nil { - t.Fatalf("Got:\n%v\nWant:\n%v", err, nil) - } - got := string(buf) - if !reflect.DeepEqual(got, tt.want) { - t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want) - } - }) - } -} diff --git a/internal/event/export/ocagent/wire/trace.go b/internal/event/export/ocagent/wire/trace.go deleted file mode 100644 index 88856673a18..00000000000 --- a/internal/event/export/ocagent/wire/trace.go +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package wire - -type ExportTraceServiceRequest struct { - Node *Node `json:"node,omitempty"` - Spans []*Span `json:"spans,omitempty"` - Resource *Resource `json:"resource,omitempty"` -} - -type Span struct { - TraceID []byte `json:"trace_id,omitempty"` - SpanID []byte `json:"span_id,omitempty"` - TraceState *TraceState `json:"tracestate,omitempty"` - ParentSpanID []byte `json:"parent_span_id,omitempty"` - Name *TruncatableString `json:"name,omitempty"` - Kind SpanKind `json:"kind,omitempty"` - StartTime Timestamp `json:"start_time,omitempty"` - EndTime Timestamp `json:"end_time,omitempty"` - Attributes *Attributes `json:"attributes,omitempty"` - StackTrace *StackTrace `json:"stack_trace,omitempty"` - TimeEvents *TimeEvents `json:"time_events,omitempty"` - Links *Links `json:"links,omitempty"` - Status *Status `json:"status,omitempty"` - Resource *Resource `json:"resource,omitempty"` - SameProcessAsParentSpan bool `json:"same_process_as_parent_span,omitempty"` - ChildSpanCount bool `json:"child_span_count,omitempty"` -} - -type TraceState struct { - Entries []*TraceStateEntry `json:"entries,omitempty"` -} - -type TraceStateEntry struct { - Key string `json:"key,omitempty"` - Value string `json:"value,omitempty"` -} - -type SpanKind int32 - -const ( - UnspecifiedSpanKind SpanKind = 0 - ServerSpanKind SpanKind = 1 - ClientSpanKind SpanKind = 2 -) - -type TimeEvents struct { - TimeEvent []TimeEvent `json:"timeEvent,omitempty"` - DroppedAnnotationsCount int32 `json:"dropped_annotations_count,omitempty"` - DroppedMessageEventsCount int32 `json:"dropped_message_events_count,omitempty"` -} - -type TimeEvent struct { - Time Timestamp `json:"time,omitempty"` - MessageEvent *MessageEvent `json:"messageEvent,omitempty"` - Annotation *Annotation `json:"annotation,omitempty"` -} - -type Annotation struct { - Description *TruncatableString `json:"description,omitempty"` - Attributes *Attributes `json:"attributes,omitempty"` -} - -type MessageEvent struct { - Type MessageEventType `json:"type,omitempty"` - ID uint64 `json:"id,omitempty"` - UncompressedSize uint64 `json:"uncompressed_size,omitempty"` - CompressedSize uint64 `json:"compressed_size,omitempty"` -} - -type MessageEventType int32 - -const ( - UnspecifiedMessageEvent MessageEventType = iota - SentMessageEvent - ReceivedMessageEvent -) - -type TimeEventValue interface { - labelTimeEventValue() -} - -func (Annotation) labelTimeEventValue() {} -func (MessageEvent) labelTimeEventValue() {} - -type Links struct { - Link []*Link `json:"link,omitempty"` - DroppedLinksCount int32 `json:"dropped_links_count,omitempty"` -} - -type Link struct { - TraceID []byte `json:"trace_id,omitempty"` - SpanID []byte `json:"span_id,omitempty"` - Type LinkType `json:"type,omitempty"` - Attributes *Attributes `json:"attributes,omitempty"` - TraceState *TraceState `json:"tracestate,omitempty"` -} - -type LinkType int32 - -const ( - UnspecifiedLinkType LinkType = 0 - ChildLinkType LinkType = 1 - ParentLinkType LinkType = 2 -) - -type Status struct { - Code int32 `json:"code,omitempty"` - Message string `json:"message,omitempty"` -} From ff03c59f3ffcb691d1205f8f2b57bcf992652358 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 27 Feb 2025 16:36:45 -0500 Subject: [PATCH 206/309] gopls/internal/analysis/modernize: append -> bytes.Clone This CL causes appendclipped to offer bytes.Clone in place of slices.Clone where the file already imports bytes but not slices. + test Updates golang/go#70815 Change-Id: I049698c3d5b8acf46abaa42ab34d72548a012a1a Reviewed-on: https://go-review.googlesource.com/c/tools/+/653455 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/slices.go | 33 ++++++++++++++++--- .../testdata/src/appendclipped/bytesclone.go | 11 +++++++ .../src/appendclipped/bytesclone.go.golden | 11 +++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go.golden diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index bdab9dea649..9cca3e98156 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -12,6 +12,7 @@ import ( "go/ast" "go/types" "slices" + "strconv" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -27,6 +28,10 @@ import ( // with a call to go1.21's slices.Concat(base, a, b, c), or simpler // replacements such as slices.Clone(a) in degenerate cases. // +// We offer bytes.Clone in preference to slices.Clone where +// appropriate, if the package already imports "bytes"; +// their behaviors are identical. +// // The base expression must denote a clipped slice (see [isClipped] // for definition), otherwise the replacement might eliminate intended // side effects to the base slice's array. @@ -41,7 +46,8 @@ import ( // The fix does not always preserve nilness the of base slice when the // addends (a, b, c) are all empty. func appendclipped(pass *analysis.Pass) { - if pass.Pkg.Path() == "slices" { + switch pass.Pkg.Path() { + case "slices", "bytes": return } @@ -94,15 +100,32 @@ func appendclipped(pass *analysis.Pass) { } } - // append(zerocap, s...) -> slices.Clone(s) - _, prefix, importEdits := analysisinternal.AddImport(info, file, "slices", "slices", "Clone", call.Pos()) + // If the slice type is []byte, and the file imports + // "bytes" but not "slices", prefer the (behaviorally + // identical) bytes.Clone for local consistency. + // https://go.dev/issue/70815#issuecomment-2671572984 + fileImports := func(path string) bool { + return slices.ContainsFunc(file.Imports, func(spec *ast.ImportSpec) bool { + value, _ := strconv.Unquote(spec.Path.Value) + return value == path + }) + } + clonepkg := cond( + types.Identical(info.TypeOf(call), byteSliceType) && + !fileImports("slices") && fileImports("bytes"), + "bytes", + "slices") + + // append(zerocap, s...) -> slices.Clone(s) or bytes.Clone(s) + _, prefix, importEdits := analysisinternal.AddImport(info, file, clonepkg, clonepkg, "Clone", call.Pos()) + message := fmt.Sprintf("Replace append with %s.Clone", clonepkg) pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), Category: "slicesclone", - Message: "Replace append with slices.Clone", + Message: message, SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Replace append with slices.Clone", + Message: message, TextEdits: append(importEdits, []analysis.TextEdit{{ Pos: call.Pos(), End: call.End(), diff --git a/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go b/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go new file mode 100644 index 00000000000..6425211b924 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go @@ -0,0 +1,11 @@ +package appendclipped + +import ( + "bytes" +) + +var _ bytes.Buffer + +func _(b []byte) { + print(append([]byte{}, b...)) // want "Replace append with bytes.Clone" +} diff --git a/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go.golden b/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go.golden new file mode 100644 index 00000000000..f49be6156b2 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/appendclipped/bytesclone.go.golden @@ -0,0 +1,11 @@ +package appendclipped + +import ( + "bytes" +) + +var _ bytes.Buffer + +func _(b []byte) { + print(bytes.Clone(b)) // want "Replace append with bytes.Clone" +} From 66eb306a364a3fd7c8ebb427be1425a3fd56262d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Feb 2025 17:03:33 -0500 Subject: [PATCH 207/309] Revert "internal/settings: drop "annotations" setting" This reverts commit 5fe60fd (CL 639835), which removed the ability for users to customize the subset of "annotations" (a misnomer for categories of compiler optimization details). Apparently some users were relying on this experimental feature. Minor tweaks were made to comments but not to logic. Fixes golang/go#71888 Change-Id: I3d0227f841582a2cb29521b9b999546226b670ef Reviewed-on: https://go-review.googlesource.com/c/tools/+/652595 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/doc/settings.md | 24 ++++++++ gopls/internal/doc/api.json | 35 +++++++++++ gopls/internal/golang/compileropt.go | 72 +++++++++++++++------- gopls/internal/settings/default.go | 6 ++ gopls/internal/settings/settings.go | 77 +++++++++++++++++++++++- gopls/internal/settings/settings_test.go | 11 ++++ 6 files changed, 203 insertions(+), 22 deletions(-) diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index 7aeab79a575..1f4f5746524 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -355,6 +355,30 @@ These analyses are documented on Default: `false`. + +### `annotations map[enum]bool` + +annotations specifies the various kinds of compiler +optimization details that should be reported as diagnostics +when enabled for a package by the "Toggle compiler +optimization details" (`gopls.gc_details`) command. + +(Some users care only about one kind of annotation in their +profiling efforts. More importantly, in large packages, the +number of annotations can sometimes overwhelm the user +interface and exceed the per-file diagnostic limit.) + +TODO(adonovan): rename this field to CompilerOptDetail. + +Each enum must be one of: + +* `"bounds"` controls bounds checking diagnostics. +* `"escape"` controls diagnostics about escape choices. +* `"inline"` controls diagnostics about inlining choices. +* `"nil"` controls nil checks. + +Default: `{"bounds":true,"escape":true,"inline":true,"nil":true}`. + ### `vulncheck enum` diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b6e53d18558..5775d0d4361 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -689,6 +689,41 @@ "Hierarchy": "ui.diagnostic", "DeprecationMessage": "" }, + { + "Name": "annotations", + "Type": "map[enum]bool", + "Doc": "annotations specifies the various kinds of compiler\noptimization details that should be reported as diagnostics\nwhen enabled for a package by the \"Toggle compiler\noptimization details\" (`gopls.gc_details`) command.\n\n(Some users care only about one kind of annotation in their\nprofiling efforts. More importantly, in large packages, the\nnumber of annotations can sometimes overwhelm the user\ninterface and exceed the per-file diagnostic limit.)\n\nTODO(adonovan): rename this field to CompilerOptDetail.\n", + "EnumKeys": { + "ValueType": "bool", + "Keys": [ + { + "Name": "\"bounds\"", + "Doc": "`\"bounds\"` controls bounds checking diagnostics.\n", + "Default": "true" + }, + { + "Name": "\"escape\"", + "Doc": "`\"escape\"` controls diagnostics about escape choices.\n", + "Default": "true" + }, + { + "Name": "\"inline\"", + "Doc": "`\"inline\"` controls diagnostics about inlining choices.\n", + "Default": "true" + }, + { + "Name": "\"nil\"", + "Doc": "`\"nil\"` controls nil checks.\n", + "Default": "true" + } + ] + }, + "EnumValues": null, + "Default": "{\"bounds\":true,\"escape\":true,\"inline\":true,\"nil\":true}", + "Status": "", + "Hierarchy": "ui.diagnostic", + "DeprecationMessage": "" + }, { "Name": "vulncheck", "Type": "enum", diff --git a/gopls/internal/golang/compileropt.go b/gopls/internal/golang/compileropt.go index f9f046463f6..bcce82c123f 100644 --- a/gopls/internal/golang/compileropt.go +++ b/gopls/internal/golang/compileropt.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/internal/event" ) @@ -65,7 +66,7 @@ func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, pkgDir pr reports := make(map[protocol.DocumentURI][]*cache.Diagnostic) var parseError error for _, fn := range files { - uri, diagnostics, err := parseDetailsFile(fn) + uri, diagnostics, err := parseDetailsFile(fn, snapshot.Options()) if err != nil { // expect errors for all the files, save 1 parseError = err @@ -87,7 +88,7 @@ func CompilerOptDetails(ctx context.Context, snapshot *cache.Snapshot, pkgDir pr } // parseDetailsFile parses the file written by the Go compiler which contains a JSON-encoded protocol.Diagnostic. -func parseDetailsFile(filename string) (protocol.DocumentURI, []*cache.Diagnostic, error) { +func parseDetailsFile(filename string, options *settings.Options) (protocol.DocumentURI, []*cache.Diagnostic, error) { buf, err := os.ReadFile(filename) if err != nil { return "", nil, err @@ -118,30 +119,14 @@ func parseDetailsFile(filename string) (protocol.DocumentURI, []*cache.Diagnosti if err := dec.Decode(d); err != nil { return "", nil, err } - if d.Source != "go compiler" { - continue - } d.Tags = []protocol.DiagnosticTag{} // must be an actual slice msg := d.Code.(string) if msg != "" { - // Typical message prefixes gathered by grepping the source of - // cmd/compile for literal arguments in calls to logopt.LogOpt. - // (It is not a well defined set.) - // - // - canInlineFunction - // - cannotInlineCall - // - cannotInlineFunction - // - copy - // - escape - // - escapes - // - isInBounds - // - isSliceInBounds - // - iteration-variable-to-{heap,stack} - // - leak - // - loop-modified-{range,for} - // - nilcheck msg = fmt.Sprintf("%s(%s)", msg, d.Message) } + if !showDiagnostic(msg, d.Source, options) { + continue + } // zeroIndexedRange subtracts 1 from the line and // range, because the compiler output neglects to @@ -186,6 +171,51 @@ func parseDetailsFile(filename string) (protocol.DocumentURI, []*cache.Diagnosti return uri, diagnostics, nil } +// showDiagnostic reports whether a given diagnostic should be shown to the end +// user, given the current options. +func showDiagnostic(msg, source string, o *settings.Options) bool { + if source != "go compiler" { + return false + } + if o.Annotations == nil { + return true + } + + // The strings below were gathered by grepping the source of + // cmd/compile for literal arguments in calls to logopt.LogOpt. + // (It is not a well defined set.) + // + // - canInlineFunction + // - cannotInlineCall + // - cannotInlineFunction + // - escape + // - escapes + // - isInBounds + // - isSliceInBounds + // - leak + // - nilcheck + // + // Additional ones not handled by logic below: + // - copy + // - iteration-variable-to-{heap,stack} + // - loop-modified-{range,for} + + switch { + case strings.HasPrefix(msg, "canInline") || + strings.HasPrefix(msg, "cannotInline") || + strings.HasPrefix(msg, "inlineCall"): + return o.Annotations[settings.Inline] + case strings.HasPrefix(msg, "escape") || msg == "leak": + return o.Annotations[settings.Escape] + case strings.HasPrefix(msg, "nilcheck"): + return o.Annotations[settings.Nil] + case strings.HasPrefix(msg, "isInBounds") || + strings.HasPrefix(msg, "isSliceInBounds"): + return o.Annotations[settings.Bounds] + } + return false +} + func findJSONFiles(dir string) ([]string, error) { ans := []string{} f := func(path string, fi os.FileInfo, _ error) error { diff --git a/gopls/internal/settings/default.go b/gopls/internal/settings/default.go index ebb3f1ccfae..aa81640f3e8 100644 --- a/gopls/internal/settings/default.go +++ b/gopls/internal/settings/default.go @@ -91,6 +91,12 @@ func DefaultOptions(overrides ...func(*Options)) *Options { }, UIOptions: UIOptions{ DiagnosticOptions: DiagnosticOptions{ + Annotations: map[Annotation]bool{ + Bounds: true, + Escape: true, + Inline: true, + Nil: true, + }, Vulncheck: ModeVulncheckOff, DiagnosticsDelay: 1 * time.Second, DiagnosticsTrigger: DiagnosticsOnEdit, diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 11b06040181..e98bc365935 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -18,6 +18,23 @@ import ( "golang.org/x/tools/gopls/internal/util/frob" ) +// An Annotation is a category of Go compiler optimization diagnostic. +type Annotation string + +const ( + // Nil controls nil checks. + Nil Annotation = "nil" + + // Escape controls diagnostics about escape choices. + Escape Annotation = "escape" + + // Inline controls diagnostics about inlining choices. + Inline Annotation = "inline" + + // Bounds controls bounds checking diagnostics. + Bounds Annotation = "bounds" +) + // Options holds various configuration that affects Gopls execution, organized // by the nature or origin of the settings. // @@ -436,6 +453,19 @@ type DiagnosticOptions struct { // [Staticcheck's website](https://staticcheck.io/docs/checks/). Staticcheck bool `status:"experimental"` + // Annotations specifies the various kinds of compiler + // optimization details that should be reported as diagnostics + // when enabled for a package by the "Toggle compiler + // optimization details" (`gopls.gc_details`) command. + // + // (Some users care only about one kind of annotation in their + // profiling efforts. More importantly, in large packages, the + // number of annotations can sometimes overwhelm the user + // interface and exceed the per-file diagnostic limit.) + // + // TODO(adonovan): rename this field to CompilerOptDetail. + Annotations map[Annotation]bool + // Vulncheck enables vulnerability scanning. Vulncheck VulncheckMode `status:"experimental"` @@ -1124,7 +1154,7 @@ func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error return setBoolMap(&o.Hints, value) case "annotations": - return nil, &SoftError{"the 'annotations' setting was removed in gopls/v0.18.0; all compiler optimization details are now shown"} + return setAnnotationMap(&o.Annotations, value) case "vulncheck": return setEnum(&o.Vulncheck, value, @@ -1420,6 +1450,51 @@ func setDuration(dest *time.Duration, value any) error { return nil } +func setAnnotationMap(dest *map[Annotation]bool, value any) ([]CounterPath, error) { + all, err := asBoolMap[string](value) + if err != nil { + return nil, err + } + var counters []CounterPath + // Default to everything enabled by default. + m := make(map[Annotation]bool) + for k, enabled := range all { + var a Annotation + cnts, err := setEnum(&a, k, + Nil, + Escape, + Inline, + Bounds) + if err != nil { + // In case of an error, process any legacy values. + switch k { + case "noEscape": + m[Escape] = false + return nil, fmt.Errorf(`"noEscape" is deprecated, set "Escape: false" instead`) + + case "noNilcheck": + m[Nil] = false + return nil, fmt.Errorf(`"noNilcheck" is deprecated, set "Nil: false" instead`) + + case "noInline": + m[Inline] = false + return nil, fmt.Errorf(`"noInline" is deprecated, set "Inline: false" instead`) + + case "noBounds": + m[Bounds] = false + return nil, fmt.Errorf(`"noBounds" is deprecated, set "Bounds: false" instead`) + + default: + return nil, err + } + } + counters = append(counters, cnts...) + m[a] = enabled + } + *dest = m + return counters, nil +} + func setBoolMap[K ~string](dest *map[K]bool, value any) ([]CounterPath, error) { m, err := asBoolMap[K](value) if err != nil { diff --git a/gopls/internal/settings/settings_test.go b/gopls/internal/settings/settings_test.go index bd9ec110874..d7a032e1938 100644 --- a/gopls/internal/settings/settings_test.go +++ b/gopls/internal/settings/settings_test.go @@ -180,6 +180,17 @@ func TestOptions_Set(t *testing.T) { return len(o.DirectoryFilters) == 0 }, }, + { + name: "annotations", + value: map[string]any{ + "Nil": false, + "noBounds": true, + }, + wantError: true, + check: func(o Options) bool { + return !o.Annotations[Nil] && !o.Annotations[Bounds] + }, + }, { name: "vulncheck", value: []any{"invalid"}, From 408d2e2cc08b50104f3e92800ce7b74e7c89daa2 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 28 Feb 2025 12:14:45 -0500 Subject: [PATCH 208/309] x/tools: remove workarounds for Go <1.23 Change-Id: I740769d6ed117bf140c9894b4464b3d3f7f326f1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653655 Auto-Submit: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- cmd/deadcode/deadcode.go | 54 +++--------------------- go/analysis/analysistest/analysistest.go | 16 ++----- go/callgraph/vta/graph.go | 7 ++- go/callgraph/vta/propagation.go | 7 ++- go/callgraph/vta/propagation_test.go | 5 +-- go/callgraph/vta/utils.go | 7 ++- go/callgraph/vta/vta.go | 10 ++--- go/ssa/builder.go | 6 ++- go/ssa/lift.go | 19 +-------- go/types/internal/play/play.go | 9 ---- gopls/internal/cache/parsego/parse.go | 34 +-------------- internal/refactor/inline/inline.go | 35 +-------------- internal/refactor/inline/util.go | 10 ----- internal/typesinternal/element_test.go | 9 ++-- internal/typesinternal/zerovalue_test.go | 23 +++++----- 15 files changed, 48 insertions(+), 203 deletions(-) diff --git a/cmd/deadcode/deadcode.go b/cmd/deadcode/deadcode.go index 0c66d07f79f..e164dc22ba8 100644 --- a/cmd/deadcode/deadcode.go +++ b/cmd/deadcode/deadcode.go @@ -15,11 +15,13 @@ import ( "go/types" "io" "log" + "maps" "os" "path/filepath" "regexp" "runtime" "runtime/pprof" + "slices" "sort" "strings" "text/template" @@ -290,9 +292,7 @@ func main() { // Build array of jsonPackage objects. var packages []any - pkgpaths := keys(byPkgPath) - sort.Strings(pkgpaths) - for _, pkgpath := range pkgpaths { + for _, pkgpath := range slices.Sorted(maps.Keys(byPkgPath)) { if !filter.MatchString(pkgpath) { continue } @@ -303,7 +303,7 @@ func main() { // declaration order. This tends to keep related // methods such as (T).Marshal and (*T).Unmarshal // together better than sorting. - fns := keys(m) + fns := slices.Collect(maps.Keys(m)) sort.Slice(fns, func(i, j int) bool { xposn := prog.Fset.Position(fns[i].Pos()) yposn := prog.Fset.Position(fns[j].Pos()) @@ -368,7 +368,7 @@ func prettyName(fn *ssa.Function, qualified bool) string { // anonymous? if fn.Parent() != nil { format(fn.Parent()) - i := index(fn.Parent().AnonFuncs, fn) + i := slices.Index(fn.Parent().AnonFuncs, fn) fmt.Fprintf(&buf, "$%d", i+1) return } @@ -427,7 +427,7 @@ func pathSearch(roots []*ssa.Function, res *rta.Result, targets map[*ssa.Functio // Sort roots into preferred order. importsTesting := func(fn *ssa.Function) bool { isTesting := func(p *types.Package) bool { return p.Path() == "testing" } - return containsFunc(fn.Pkg.Pkg.Imports(), isTesting) + return slices.ContainsFunc(fn.Pkg.Pkg.Imports(), isTesting) } sort.Slice(roots, func(i, j int) bool { x, y := roots[i], roots[j] @@ -461,7 +461,7 @@ func pathSearch(roots []*ssa.Function, res *rta.Result, targets map[*ssa.Functio for { edge := seen[node] if edge == nil { - reverse(path) + slices.Reverse(path) return path } path = append(path, edge) @@ -565,43 +565,3 @@ type jsonPosition struct { func (p jsonPosition) String() string { return fmt.Sprintf("%s:%d:%d", p.File, p.Line, p.Col) } - -// -- from the future -- - -// TODO(adonovan): use go1.22's slices and maps packages. - -func containsFunc[S ~[]E, E any](s S, f func(E) bool) bool { - return indexFunc(s, f) >= 0 -} - -func indexFunc[S ~[]E, E any](s S, f func(E) bool) int { - for i := range s { - if f(s[i]) { - return i - } - } - return -1 -} - -func index[S ~[]E, E comparable](s S, v E) int { - for i := range s { - if v == s[i] { - return i - } - } - return -1 -} - -func reverse[S ~[]E, E any](s S) { - for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - s[i], s[j] = s[j], s[i] - } -} - -func keys[M ~map[K]V, K comparable, V any](m M) []K { - r := make([]K, 0, len(m)) - for k := range m { - r = append(r, k) - } - return r -} diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 0b5cfe70bfe..143b4260346 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -7,12 +7,12 @@ package analysistest import ( "bytes" - "cmp" "fmt" "go/format" "go/token" "go/types" "log" + "maps" "os" "path/filepath" "regexp" @@ -215,7 +215,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns // Because the checking is driven by original // filenames, there is no way to express that a fix // (e.g. extract declaration) creates a new file. - for _, filename := range sortedKeys(allFilenames) { + for _, filename := range slices.Sorted(maps.Keys(allFilenames)) { // Read the original file. content, err := os.ReadFile(filename) if err != nil { @@ -266,7 +266,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns // Form #2: all suggested fixes are represented by a single file. want := ar.Comment var accumulated []diff.Edit - for _, message := range sortedKeys(fixEdits) { + for _, message := range slices.Sorted(maps.Keys(fixEdits)) { for _, fix := range fixEdits[message] { accumulated = merge(filename, message, accumulated, fix[filename]) } @@ -768,13 +768,3 @@ func sanitize(gopath, filename string) string { prefix := gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) return filepath.ToSlash(strings.TrimPrefix(filename, prefix)) } - -// TODO(adonovan): use better stuff from go1.23. -func sortedKeys[K cmp.Ordered, V any](m map[K]V) []K { - keys := make([]K, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - slices.Sort(keys) - return keys -} diff --git a/go/callgraph/vta/graph.go b/go/callgraph/vta/graph.go index c13b8a5e6cb..164018708ef 100644 --- a/go/callgraph/vta/graph.go +++ b/go/callgraph/vta/graph.go @@ -633,12 +633,12 @@ func (b *builder) call(c ssa.CallInstruction) { return } - siteCallees(c, b.callees)(func(f *ssa.Function) bool { + for f := range siteCallees(c, b.callees) { addArgumentFlows(b, c, f) site, ok := c.(ssa.Value) if !ok { - return true // go or defer + continue // go or defer } results := f.Signature.Results() @@ -653,8 +653,7 @@ func (b *builder) call(c ssa.CallInstruction) { b.addInFlowEdge(resultVar{f: f, index: i}, local) } } - return true - }) + } } func addArgumentFlows(b *builder, c ssa.CallInstruction, f *ssa.Function) { diff --git a/go/callgraph/vta/propagation.go b/go/callgraph/vta/propagation.go index 6ce1ca9e322..1c4dcd2888e 100644 --- a/go/callgraph/vta/propagation.go +++ b/go/callgraph/vta/propagation.go @@ -6,6 +6,7 @@ package vta import ( "go/types" + "iter" "slices" "golang.org/x/tools/go/callgraph/vta/internal/trie" @@ -113,11 +114,9 @@ type propType struct { // the role of a map from nodes to a set of propTypes. type propTypeMap map[node]*trie.MutMap -// propTypes returns a go1.23 iterator for the propTypes associated with +// propTypes returns an iterator for the propTypes associated with // node `n` in map `ptm`. -func (ptm propTypeMap) propTypes(n node) func(yield func(propType) bool) { - // TODO: when x/tools uses go1.23, change callers to use range-over-func - // (https://go.dev/issue/65237). +func (ptm propTypeMap) propTypes(n node) iter.Seq[propType] { return func(yield func(propType) bool) { if types := ptm[n]; types != nil { types.M.Range(func(_ uint64, elem any) bool { diff --git a/go/callgraph/vta/propagation_test.go b/go/callgraph/vta/propagation_test.go index 3885ef201cb..bc9ca1ecde6 100644 --- a/go/callgraph/vta/propagation_test.go +++ b/go/callgraph/vta/propagation_test.go @@ -98,10 +98,9 @@ func nodeToTypeString(pMap propTypeMap) map[string]string { nodeToTypeStr := make(map[string]string) for node := range pMap { var propStrings []string - pMap.propTypes(node)(func(prop propType) bool { + for prop := range pMap.propTypes(node) { propStrings = append(propStrings, propTypeString(prop)) - return true - }) + } sort.Strings(propStrings) nodeToTypeStr[node.String()] = strings.Join(propStrings, ";") } diff --git a/go/callgraph/vta/utils.go b/go/callgraph/vta/utils.go index bbd8400ec9b..3a708f220a7 100644 --- a/go/callgraph/vta/utils.go +++ b/go/callgraph/vta/utils.go @@ -6,6 +6,7 @@ package vta import ( "go/types" + "iter" "golang.org/x/tools/go/ssa" "golang.org/x/tools/internal/typeparams" @@ -147,10 +148,8 @@ func sliceArrayElem(t types.Type) types.Type { } } -// siteCallees returns a go1.23 iterator for the callees for call site `c`. -func siteCallees(c ssa.CallInstruction, callees calleesFunc) func(yield func(*ssa.Function) bool) { - // TODO: when x/tools uses go1.23, change callers to use range-over-func - // (https://go.dev/issue/65237). +// siteCallees returns an iterator for the callees for call site `c`. +func siteCallees(c ssa.CallInstruction, callees calleesFunc) iter.Seq[*ssa.Function] { return func(yield func(*ssa.Function) bool) { for _, callee := range callees(c) { if !yield(callee) { diff --git a/go/callgraph/vta/vta.go b/go/callgraph/vta/vta.go index 56fce13725f..ed12001fdb2 100644 --- a/go/callgraph/vta/vta.go +++ b/go/callgraph/vta/vta.go @@ -126,12 +126,11 @@ func (c *constructor) resolves(call ssa.CallInstruction) []*ssa.Function { // Cover the case of dynamic higher-order and interface calls. var res []*ssa.Function resolved := resolve(call, c.types, c.cache) - siteCallees(call, c.callees)(func(f *ssa.Function) bool { + for f := range siteCallees(call, c.callees) { if _, ok := resolved[f]; ok { res = append(res, f) } - return true - }) + } return res } @@ -140,12 +139,11 @@ func (c *constructor) resolves(call ssa.CallInstruction) []*ssa.Function { func resolve(c ssa.CallInstruction, types propTypeMap, cache methodCache) map[*ssa.Function]empty { fns := make(map[*ssa.Function]empty) n := local{val: c.Common().Value} - types.propTypes(n)(func(p propType) bool { + for p := range types.propTypes(n) { for _, f := range propFunc(p, c, cache) { fns[f] = empty{} } - return true - }) + } return fns } diff --git a/go/ssa/builder.go b/go/ssa/builder.go index 1761dcc3068..84ccbc0927a 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -82,6 +82,8 @@ import ( "runtime" "sync" + "slices" + "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/versions" ) @@ -2021,8 +2023,8 @@ func (b *builder) forStmtGo122(fn *Function, s *ast.ForStmt, label *lblock) { // Remove instructions for phi, load, and store. // lift() will remove the unused i_next *Alloc. isDead := func(i Instruction) bool { return dead[i] } - loop.Instrs = removeInstrsIf(loop.Instrs, isDead) - post.Instrs = removeInstrsIf(post.Instrs, isDead) + loop.Instrs = slices.DeleteFunc(loop.Instrs, isDead) + post.Instrs = slices.DeleteFunc(post.Instrs, isDead) } } diff --git a/go/ssa/lift.go b/go/ssa/lift.go index aada3dc3227..6138ca82e0e 100644 --- a/go/ssa/lift.go +++ b/go/ssa/lift.go @@ -43,6 +43,7 @@ import ( "go/token" "math/big" "os" + "slices" "golang.org/x/tools/internal/typeparams" ) @@ -105,23 +106,7 @@ func buildDomFrontier(fn *Function) domFrontier { } func removeInstr(refs []Instruction, instr Instruction) []Instruction { - return removeInstrsIf(refs, func(i Instruction) bool { return i == instr }) -} - -func removeInstrsIf(refs []Instruction, p func(Instruction) bool) []Instruction { - // TODO(taking): replace with go1.22 slices.DeleteFunc. - i := 0 - for _, ref := range refs { - if p(ref) { - continue - } - refs[i] = ref - i++ - } - for j := i; j != len(refs); j++ { - refs[j] = nil // aid GC - } - return refs[:i] + return slices.DeleteFunc(refs, func(i Instruction) bool { return i == instr }) } // lift replaces local and new Allocs accessed only with diff --git a/go/types/internal/play/play.go b/go/types/internal/play/play.go index 8d3b9d19346..f1318ac247a 100644 --- a/go/types/internal/play/play.go +++ b/go/types/internal/play/play.go @@ -430,12 +430,3 @@ textarea { width: 6in; } body { color: gray; } div#out { font-family: monospace; font-size: 80%; } ` - -// TODO(adonovan): use go1.21 built-in. -func min(x, y int) int { - if x < y { - return x - } else { - return y - } -} diff --git a/gopls/internal/cache/parsego/parse.go b/gopls/internal/cache/parsego/parse.go index db6089d8e6d..08a1c395a2a 100644 --- a/gopls/internal/cache/parsego/parse.go +++ b/gopls/internal/cache/parsego/parse.go @@ -27,7 +27,6 @@ import ( "golang.org/x/tools/gopls/internal/label" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/astutil" - "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/diff" @@ -65,39 +64,8 @@ func Parse(ctx context.Context, fset *token.FileSet, uri protocol.DocumentURI, s } // Inv: file != nil. - // Workaround for #70162 (missing File{Start,End} when - // parsing empty file) with go1.23. - // - // When parsing an empty file, or one without a valid - // package declaration, the go1.23 parser bails out before - // setting FileStart and End. - // - // This leaves us no way to find the original - // token.File that ParseFile created, so as a - // workaround, we recreate the token.File, and - // populate the FileStart and FileEnd fields. - // - // See also #53202. tokenFile := func(file *ast.File) *token.File { - tok := fset.File(file.FileStart) - if tok == nil { - // Invalid File.FileStart (also File.{Package,Name.Pos}). - if file.Package.IsValid() { - bug.Report("ast.File has valid Package but no FileStart") - } - if file.Name.Pos().IsValid() { - bug.Report("ast.File has valid Name.Pos but no FileStart") - } - tok = fset.AddFile(uri.Path(), -1, len(src)) - tok.SetLinesForContent(src) - // If the File contained any valid token.Pos values, - // they would all be invalid wrt the new token.File, - // but we have established that it lacks FileStart, - // Package, and Name.Pos. - file.FileStart = token.Pos(tok.Base()) - file.FileEnd = token.Pos(tok.Base() + tok.Size()) - } - return tok + return fset.File(file.FileStart) } tok := tokenFile(file) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 6f6ed4583a9..2b6f06242e7 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -363,7 +363,7 @@ func (st *state) inline() (*Result, error) { specToDelete := oldImport.spec for _, decl := range f.Decls { if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { - decl.Specs = slicesDeleteFunc(decl.Specs, func(spec ast.Spec) bool { + decl.Specs = slices.DeleteFunc(decl.Specs, func(spec ast.Spec) bool { imp := spec.(*ast.ImportSpec) // Since we re-parsed the file, we can't match by identity; // instead look for syntactic equivalence. @@ -2042,7 +2042,7 @@ func resolveEffects(logf logger, args []*argument, effects []int, sg substGraph) argi := args[i] if sg.has(argi) && !argi.pure { // i is not bound: check whether it must be bound due to hazards. - idx := index(effects, i) + idx := slices.Index(effects, i) if idx >= 0 { for _, j := range effects[:idx] { var ( @@ -3710,34 +3710,3 @@ func soleUse(info *types.Info, obj types.Object) (sole *ast.Ident) { } type unit struct{} // for representing sets as maps - -// slicesDeleteFunc removes any elements from s for which del returns true, -// returning the modified slice. -// slicesDeleteFunc zeroes the elements between the new length and the original length. -// TODO(adonovan): use go1.21 slices.DeleteFunc -func slicesDeleteFunc[S ~[]E, E any](s S, del func(E) bool) S { - i := slicesIndexFunc(s, del) - if i == -1 { - return s - } - // Don't start copying elements until we find one to delete. - for j := i + 1; j < len(s); j++ { - if v := s[j]; !del(v) { - s[i] = v - i++ - } - } - // clear(s[i:]) // zero/nil out the obsolete elements, for GC - return s[:i] -} - -// slicesIndexFunc returns the first index i satisfying f(s[i]), -// or -1 if none do. -func slicesIndexFunc[S ~[]E, E any](s S, f func(E) bool) int { - for i := range s { - if f(s[i]) { - return i - } - } - return -1 -} diff --git a/internal/refactor/inline/util.go b/internal/refactor/inline/util.go index 591dc4265c0..c3f049c73b0 100644 --- a/internal/refactor/inline/util.go +++ b/internal/refactor/inline/util.go @@ -22,16 +22,6 @@ func is[T any](x any) bool { return ok } -// TODO(adonovan): use go1.21's slices.Index. -func index[T comparable](slice []T, x T) int { - for i, elem := range slice { - if elem == x { - return i - } - } - return -1 -} - func btoi(b bool) int { if b { return 1 diff --git a/internal/typesinternal/element_test.go b/internal/typesinternal/element_test.go index b4475633270..95f1ab33478 100644 --- a/internal/typesinternal/element_test.go +++ b/internal/typesinternal/element_test.go @@ -9,6 +9,8 @@ import ( "go/parser" "go/token" "go/types" + "maps" + "slices" "strings" "testing" @@ -142,12 +144,7 @@ func TestForEachElement(t *testing.T) { } } if fail { - for k := range got { - t.Logf("got element: %s", k) - } - // TODO(adonovan): use this when go1.23 is assured: - // t.Logf("got elements:\n%s", - // strings.Join(slices.Sorted(maps.Keys(got)), "\n")) + t.Logf("got elements:\n%s", strings.Join(slices.Sorted(maps.Keys(got)), "\n")) } } } diff --git a/internal/typesinternal/zerovalue_test.go b/internal/typesinternal/zerovalue_test.go index 8ec1012dfda..67295a95020 100644 --- a/internal/typesinternal/zerovalue_test.go +++ b/internal/typesinternal/zerovalue_test.go @@ -68,15 +68,15 @@ type aliasNamed = foo func _[T any]() { type aliasTypeParam = T - // type aliasWithTypeParam[u any] = struct { - // x u - // y T - // } - // type aliasWithTypeParams[u, q any] = struct { - // x u - // y q - // z T - // } + type aliasWithTypeParam[u any] = struct { + x u + y T + } + type aliasWithTypeParams[u, q any] = struct { + x u + y q + z T + } type namedWithTypeParam[u any] struct { x u @@ -135,9 +135,8 @@ func _[T any]() { _ aliasTypeParam // *new(T) _ *aliasTypeParam // nil - // TODO(hxjiang): add test for alias type param after stop supporting go1.22. - // _ aliasWithTypeParam[int] // aliasWithTypeParam[int]{} - // _ aliasWithTypeParams[int, string] // aliasWithTypeParams[int, string]{} + _ aliasWithTypeParam[int] // aliasWithTypeParam[int]{} + _ aliasWithTypeParams[int, string] // aliasWithTypeParams[int, string]{} _ namedWithTypeParam[int] // namedWithTypeParam[int]{} _ namedWithTypeParams[int, string] // namedWithTypeParams[int, string]{} From 608d370dd53cb3898f3ddb6dfa5f0d29eae80d2d Mon Sep 17 00:00:00 2001 From: cuishuang Date: Thu, 27 Feb 2025 12:49:28 +0800 Subject: [PATCH 209/309] internal/imports: use a more straightforward return value Change-Id: Ibd8249da636a854dd1a53c047e3d215ef45c911f Reviewed-on: https://go-review.googlesource.com/c/tools/+/653196 Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- internal/imports/fix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index ee0efe48a55..737a9bfae8f 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -559,7 +559,7 @@ func fixImportsDefault(fset *token.FileSet, f *ast.File, filename string, env *P return err } apply(fset, f, fixes) - return err + return nil } // getFixes gets the import fixes that need to be made to f in order to fix the imports. From b2aa62b57015c812848d950d884f626839a43fd7 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 27 Feb 2025 16:03:51 -0500 Subject: [PATCH 210/309] internal/stdlib: provide API for import graph of std library This CL adds two functions for accessing the direct and transitive imports of the packages of the standard library: func Imports(pkgs ...string) iter.Seq[string] func Dependencies(pkgs ...string) iter.Seq[string] These are needed by modernizers so that they can avoid offering fixes that add an import of, say, "slices" while analyzing a package that is itself a dependency of "slices". The compressed graph is generated from the current toolchain; this may not exactly match the source code being analyzed by the application, but we expect drift to be small. Updates golang/go#70815 Change-Id: I2d7180bcff1d1c72ce61b8436a346b8921c02ba9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653356 Commit-Queue: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Ian Lance Taylor --- internal/stdlib/deps.go | 359 +++++++++++++++++++++++ internal/stdlib/deps_test.go | 36 +++ internal/stdlib/generate.go | 125 +++++++- internal/stdlib/import.go | 89 ++++++ internal/stdlib/manifest.go | 9 +- internal/stdlib/stdlib.go | 2 +- internal/stdlib/testdata/nethttp.deps | 171 +++++++++++ internal/stdlib/testdata/nethttp.imports | 47 +++ 8 files changed, 834 insertions(+), 4 deletions(-) create mode 100644 internal/stdlib/deps.go create mode 100644 internal/stdlib/deps_test.go create mode 100644 internal/stdlib/import.go create mode 100644 internal/stdlib/testdata/nethttp.deps create mode 100644 internal/stdlib/testdata/nethttp.imports diff --git a/internal/stdlib/deps.go b/internal/stdlib/deps.go new file mode 100644 index 00000000000..7cca431cd65 --- /dev/null +++ b/internal/stdlib/deps.go @@ -0,0 +1,359 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Code generated by generate.go. DO NOT EDIT. + +package stdlib + +type pkginfo struct { + name string + deps string // list of indices of dependencies, as varint-encoded deltas +} + +var deps = [...]pkginfo{ + {"archive/tar", "\x03k\x03E5\x01\v\x01#\x01\x01\x02\x05\t\x02\x01\x02\x02\v"}, + {"archive/zip", "\x02\x04a\a\x16\x0205\x01+\x05\x01\x10\x03\x02\r\x04"}, + {"bufio", "\x03k}E\x13"}, + {"bytes", "n+R\x03\fG\x02\x02"}, + {"cmp", ""}, + {"compress/bzip2", "\x02\x02\xe7\x01B"}, + {"compress/flate", "\x02l\x03z\r\x024\x01\x03"}, + {"compress/gzip", "\x02\x04a\a\x03\x15eT"}, + {"compress/lzw", "\x02l\x03z"}, + {"compress/zlib", "\x02\x04a\a\x03\x13\x01f"}, + {"container/heap", "\xae\x02"}, + {"container/list", ""}, + {"container/ring", ""}, + {"context", "n\\h\x01\f"}, + {"crypto", "\x84\x01gD"}, + {"crypto/aes", "\x10\n\a\x8e\x02"}, + {"crypto/cipher", "\x03\x1e\x01\x01\x1d\x11\x1d,Q"}, + {"crypto/des", "\x10\x13\x1d.,\x95\x01\x03"}, + {"crypto/dsa", "@\x04*}\x0e"}, + {"crypto/ecdh", "\x03\v\f\x0e\x04\x14\x04\r\x1d}"}, + {"crypto/ecdsa", "\x0e\x05\x03\x04\x01\x0e\x16\x01\x04\f\x01\x1d}\x0e\x04K\x01"}, + {"crypto/ed25519", "\x0e\x1c\x16\n\a\x1d}D"}, + {"crypto/elliptic", "0>}\x0e9"}, + {"crypto/fips140", " \x05\x91\x01"}, + {"crypto/hkdf", "-\x12\x01.\x16"}, + {"crypto/hmac", "\x1a\x14\x11\x01\x113"}, + {"crypto/internal/boring", "\x0e\x02\rg"}, + {"crypto/internal/boring/bbig", "\x1a\xdf\x01L"}, + {"crypto/internal/boring/bcache", "\xb3\x02\x12"}, + {"crypto/internal/boring/sig", ""}, + {"crypto/internal/cryptotest", "\x03\r\n)\x0e\x1a\x06\x13\x12#\a\t\x11\x11\x11\x1b\x01\f\f\x05\n"}, + {"crypto/internal/entropy", "E"}, + {"crypto/internal/fips140", ">0}9\f\x15"}, + {"crypto/internal/fips140/aes", "\x03\x1d\x03\x02\x13\x04\x01\x01\x05+\x8c\x015"}, + {"crypto/internal/fips140/aes/gcm", " \x01\x02\x02\x02\x11\x04\x01\x06+\x8a\x01"}, + {"crypto/internal/fips140/alias", "\xc5\x02"}, + {"crypto/internal/fips140/bigmod", "%\x17\x01\x06+\x8c\x01"}, + {"crypto/internal/fips140/check", " \x0e\x06\b\x02\xad\x01Z"}, + {"crypto/internal/fips140/check/checktest", "%\xff\x01!"}, + {"crypto/internal/fips140/drbg", "\x03\x1c\x01\x01\x04\x13\x04\b\x01)}\x0f8"}, + {"crypto/internal/fips140/ecdh", "\x03\x1d\x05\x02\t\f2}\x0f8"}, + {"crypto/internal/fips140/ecdsa", "\x03\x1d\x04\x01\x02\a\x02\x068}G"}, + {"crypto/internal/fips140/ed25519", "\x03\x1d\x05\x02\x04\v8\xc1\x01\x03"}, + {"crypto/internal/fips140/edwards25519", "%\a\f\x042\x8c\x018"}, + {"crypto/internal/fips140/edwards25519/field", "%\x13\x042\x8c\x01"}, + {"crypto/internal/fips140/hkdf", "\x03\x1d\x05\t\x06:"}, + {"crypto/internal/fips140/hmac", "\x03\x1d\x14\x01\x018"}, + {"crypto/internal/fips140/mlkem", "\x03\x1d\x05\x02\x0e\x03\x042"}, + {"crypto/internal/fips140/nistec", "%\f\a\x042\x8c\x01*\x0e\x13"}, + {"crypto/internal/fips140/nistec/fiat", "%\x136\x8c\x01"}, + {"crypto/internal/fips140/pbkdf2", "\x03\x1d\x05\t\x06:"}, + {"crypto/internal/fips140/rsa", "\x03\x1d\x04\x01\x02\r\x01\x01\x026}G"}, + {"crypto/internal/fips140/sha256", "\x03\x1d\x1c\x01\x06+\x8c\x01"}, + {"crypto/internal/fips140/sha3", "\x03\x1d\x18\x04\x011\x8c\x01K"}, + {"crypto/internal/fips140/sha512", "\x03\x1d\x1c\x01\x06+\x8c\x01"}, + {"crypto/internal/fips140/ssh", " \x05"}, + {"crypto/internal/fips140/subtle", "#\x19\xbe\x01"}, + {"crypto/internal/fips140/tls12", "\x03\x1d\x05\t\x06\x028"}, + {"crypto/internal/fips140/tls13", "\x03\x1d\x05\b\a\b2"}, + {"crypto/internal/fips140deps", ""}, + {"crypto/internal/fips140deps/byteorder", "\x9a\x01"}, + {"crypto/internal/fips140deps/cpu", "\xae\x01\a"}, + {"crypto/internal/fips140deps/godebug", "\xb6\x01"}, + {"crypto/internal/fips140hash", "5\x1a5\xc1\x01"}, + {"crypto/internal/fips140only", "'\r\x01\x01N25"}, + {"crypto/internal/fips140test", ""}, + {"crypto/internal/hpke", "\x0e\x01\x01\x03\x1a\x1d$,`M"}, + {"crypto/internal/impl", "\xb0\x02"}, + {"crypto/internal/randutil", "\xeb\x01\x12"}, + {"crypto/internal/sysrand", "\xd7\x01@\x1b\x01\f\x06"}, + {"crypto/internal/sysrand/internal/seccomp", "n"}, + {"crypto/md5", "\x0e2.\x16\x16`"}, + {"crypto/mlkem", "/"}, + {"crypto/pbkdf2", "2\r\x01.\x16"}, + {"crypto/rand", "\x1a\x06\a\x19\x04\x01)}\x0eL"}, + {"crypto/rc4", "#\x1d.\xc1\x01"}, + {"crypto/rsa", "\x0e\f\x01\t\x0f\f\x01\x04\x06\a\x1d\x03\x1325\r\x01"}, + {"crypto/sha1", "\x0e\f&.\x16\x16\x14L"}, + {"crypto/sha256", "\x0e\f\x1aP"}, + {"crypto/sha3", "\x0e'O\xc1\x01"}, + {"crypto/sha512", "\x0e\f\x1cN"}, + {"crypto/subtle", "8\x98\x01T"}, + {"crypto/tls", "\x03\b\x02\x01\x01\x01\x01\x02\x01\x01\x01\x03\x01\a\x01\v\x02\n\x01\b\x05\x03\x01\x01\x01\x01\x02\x01\x02\x01\x18\x02\x03\x13\x16\x14\b5\x16\x16\r\t\x01\x01\x01\x02\x01\f\x06\x02\x01"}, + {"crypto/tls/internal/fips140tls", " \x93\x02"}, + {"crypto/x509", "\x03\v\x01\x01\x01\x01\x01\x01\x01\x011\x03\x02\x01\x01\x02\x05\x01\x0e\x06\x02\x02\x03E5\x03\t\x01\x01\x01\a\x10\x05\t\x05\v\x01\x02\r\x02\x01\x01\x02\x03\x01"}, + {"crypto/x509/internal/macos", "\x03k'\x8f\x01\v\x10\x06"}, + {"crypto/x509/pkix", "d\x06\a\x88\x01F"}, + {"database/sql", "\x03\nK\x16\x03z\f\x06\"\x05\t\x02\x03\x01\f\x02\x02\x02"}, + {"database/sql/driver", "\ra\x03\xae\x01\x10\x10"}, + {"debug/buildinfo", "\x03X\x02\x01\x01\b\a\x03`\x18\x02\x01+\x10\x1e"}, + {"debug/dwarf", "\x03d\a\x03z1\x12\x01\x01"}, + {"debug/elf", "\x03\x06Q\r\a\x03`\x19\x01,\x18\x01\x15"}, + {"debug/gosym", "\x03d\n\xbd\x01\x01\x01\x02"}, + {"debug/macho", "\x03\x06Q\r\n`\x1a,\x18\x01"}, + {"debug/pe", "\x03\x06Q\r\a\x03`\x1a,\x18\x01\x15"}, + {"debug/plan9obj", "g\a\x03`\x1a,"}, + {"embed", "n+:\x18\x01S"}, + {"embed/internal/embedtest", ""}, + {"encoding", ""}, + {"encoding/ascii85", "\xeb\x01D"}, + {"encoding/asn1", "\x03k\x03\x87\x01\x01&\x0e\x02\x01\x0f\x03\x01"}, + {"encoding/base32", "\xeb\x01B\x02"}, + {"encoding/base64", "\x9a\x01QB\x02"}, + {"encoding/binary", "n}\r'\x0e\x05"}, + {"encoding/csv", "\x02\x01k\x03zE\x11\x02"}, + {"encoding/gob", "\x02`\x05\a\x03`\x1a\f\x01\x02\x1d\b\x13\x01\x0e\x02"}, + {"encoding/hex", "n\x03zB\x03"}, + {"encoding/json", "\x03\x01^\x04\b\x03z\r'\x0e\x02\x01\x02\x0f\x01\x01\x02"}, + {"encoding/pem", "\x03c\b}B\x03"}, + {"encoding/xml", "\x02\x01_\f\x03z4\x05\v\x01\x02\x0f\x02"}, + {"errors", "\xca\x01{"}, + {"expvar", "kK9\t\n\x15\r\t\x02\x03\x01\x10"}, + {"flag", "b\f\x03z,\b\x05\t\x02\x01\x0f"}, + {"fmt", "nE8\r\x1f\b\x0e\x02\x03\x11"}, + {"go/ast", "\x03\x01m\x0f\x01j\x03)\b\x0e\x02\x01"}, + {"go/ast/internal/tests", ""}, + {"go/build", "\x02\x01k\x03\x01\x03\x02\a\x02\x01\x17\x1e\x04\x02\t\x14\x12\x01+\x01\x04\x01\a\t\x02\x01\x11\x02\x02"}, + {"go/build/constraint", "n\xc1\x01\x01\x11\x02"}, + {"go/constant", "q\x10w\x01\x015\x01\x02\x11"}, + {"go/doc", "\x04m\x01\x06\t=-1\x11\x02\x01\x11\x02"}, + {"go/doc/comment", "\x03n\xbc\x01\x01\x01\x01\x11\x02"}, + {"go/format", "\x03n\x01\f\x01\x02jE"}, + {"go/importer", "t\a\x01\x01\x04\x01i9"}, + {"go/internal/gccgoimporter", "\x02\x01X\x13\x03\x05\v\x01g\x02,\x01\x05\x12\x01\v\b"}, + {"go/internal/gcimporter", "\x02o\x10\x01/\x05\x0e',\x16\x03\x02"}, + {"go/internal/srcimporter", "q\x01\x02\n\x03\x01i,\x01\x05\x13\x02\x13"}, + {"go/parser", "\x03k\x03\x01\x03\v\x01j\x01+\x06\x13"}, + {"go/printer", "q\x01\x03\x03\tj\r\x1f\x16\x02\x01\x02\n\x05\x02"}, + {"go/scanner", "\x03n\x10j2\x11\x01\x12\x02"}, + {"go/token", "\x04m\xbc\x01\x02\x03\x01\x0e\x02"}, + {"go/types", "\x03\x01\x06d\x03\x01\x04\b\x03\x02\x15\x1e\x06+\x04\x03\n%\a\t\x01\x01\x01\x02\x01\x0e\x02\x02"}, + {"go/version", "\xbb\x01u"}, + {"hash", "\xeb\x01"}, + {"hash/adler32", "n\x16\x16"}, + {"hash/crc32", "n\x16\x16\x14\x84\x01\x01"}, + {"hash/crc64", "n\x16\x16\x98\x01"}, + {"hash/fnv", "n\x16\x16`"}, + {"hash/maphash", "\x95\x01\x05\x1b\x03@M"}, + {"html", "\xb0\x02\x02\x11"}, + {"html/template", "\x03h\x06\x19,5\x01\v \x05\x01\x02\x03\r\x01\x02\v\x01\x03\x02"}, + {"image", "\x02l\x1f^\x0f5\x03\x01"}, + {"image/color", ""}, + {"image/color/palette", "\x8d\x01"}, + {"image/draw", "\x8c\x01\x01\x04"}, + {"image/gif", "\x02\x01\x05f\x03\x1b\x01\x01\x01\vQ"}, + {"image/internal/imageutil", "\x8c\x01"}, + {"image/jpeg", "\x02l\x1e\x01\x04Z"}, + {"image/png", "\x02\a^\n\x13\x02\x06\x01^D"}, + {"index/suffixarray", "\x03d\a}\r*\v\x01"}, + {"internal/abi", "\xb5\x01\x90\x01"}, + {"internal/asan", "\xc5\x02"}, + {"internal/bisect", "\xa4\x02\x0e\x01"}, + {"internal/buildcfg", "qG_\x06\x02\x05\v\x01"}, + {"internal/bytealg", "\xae\x01\x97\x01"}, + {"internal/byteorder", ""}, + {"internal/cfg", ""}, + {"internal/chacha8rand", "\x9a\x01\x1b\x90\x01"}, + {"internal/copyright", ""}, + {"internal/coverage", ""}, + {"internal/coverage/calloc", ""}, + {"internal/coverage/cfile", "k\x06\x17\x16\x01\x02\x01\x01\x01\x01\x01\x01\x01$\x01\x1e,\x06\a\v\x01\x03\f\x06"}, + {"internal/coverage/cformat", "\x04m-\x04I\f6\x01\x02\f"}, + {"internal/coverage/cmerge", "q-Z"}, + {"internal/coverage/decodecounter", "g\n-\v\x02@,\x18\x16"}, + {"internal/coverage/decodemeta", "\x02e\n\x17\x16\v\x02@,"}, + {"internal/coverage/encodecounter", "\x02e\n-\f\x01\x02>\f \x16"}, + {"internal/coverage/encodemeta", "\x02\x01d\n\x13\x04\x16\r\x02>,."}, + {"internal/coverage/pods", "\x04m-y\x06\x05\v\x02\x01"}, + {"internal/coverage/rtcov", "\xc5\x02"}, + {"internal/coverage/slicereader", "g\nzZ"}, + {"internal/coverage/slicewriter", "qz"}, + {"internal/coverage/stringtab", "q8\x04>"}, + {"internal/coverage/test", ""}, + {"internal/coverage/uleb128", ""}, + {"internal/cpu", "\xc5\x02"}, + {"internal/dag", "\x04m\xbc\x01\x03"}, + {"internal/diff", "\x03n\xbd\x01\x02"}, + {"internal/exportdata", "\x02\x01k\x03\x03]\x1a,\x01\x05\x12\x01\x02"}, + {"internal/filepathlite", "n+:\x19A"}, + {"internal/fmtsort", "\x04\x9b\x02\x0e"}, + {"internal/fuzz", "\x03\nA\x19\x04\x03\x03\x01\f\x0355\r\x02\x1d\x01\x05\x02\x05\v\x01\x02\x01\x01\v\x04\x02"}, + {"internal/goarch", ""}, + {"internal/godebug", "\x97\x01 {\x01\x12"}, + {"internal/godebugs", ""}, + {"internal/goexperiment", ""}, + {"internal/goos", ""}, + {"internal/goroot", "\x97\x02\x01\x05\x13\x02"}, + {"internal/gover", "\x04"}, + {"internal/goversion", ""}, + {"internal/itoa", ""}, + {"internal/lazyregexp", "\x97\x02\v\x0e\x02"}, + {"internal/lazytemplate", "\xeb\x01,\x19\x02\v"}, + {"internal/msan", "\xc5\x02"}, + {"internal/nettrace", ""}, + {"internal/obscuretestdata", "f\x85\x01,"}, + {"internal/oserror", "n"}, + {"internal/pkgbits", "\x03K\x19\a\x03\x05\vj\x0e\x1e\r\v\x01"}, + {"internal/platform", ""}, + {"internal/poll", "nO\x1a\x149\x0e\x01\x01\v\x06"}, + {"internal/profile", "\x03\x04g\x03z7\f\x01\x01\x0f"}, + {"internal/profilerecord", ""}, + {"internal/race", "\x95\x01\xb0\x01"}, + {"internal/reflectlite", "\x95\x01 3\x01P\x0e\x13\x12"}, + {"unsafe", ""}, + {"vendor/golang.org/x/crypto/chacha20", "\x10W\a\x8c\x01*&"}, + {"vendor/golang.org/x/crypto/chacha20poly1305", "\x10W\a\xd8\x01\x04\x01"}, + {"vendor/golang.org/x/crypto/cryptobyte", "d\n\x03\x88\x01& \n"}, + {"vendor/golang.org/x/crypto/cryptobyte/asn1", ""}, + {"vendor/golang.org/x/crypto/internal/alias", "\xc5\x02"}, + {"vendor/golang.org/x/crypto/internal/poly1305", "Q\x16\x93\x01"}, + {"vendor/golang.org/x/net/dns/dnsmessage", "n"}, + {"vendor/golang.org/x/net/http/httpguts", "\x81\x02\x14\x1b\x13\r"}, + {"vendor/golang.org/x/net/http/httpproxy", "n\x03\x90\x01\x15\x01\x19\x13\r"}, + {"vendor/golang.org/x/net/http2/hpack", "\x03k\x03zG"}, + {"vendor/golang.org/x/net/idna", "q\x87\x018\x13\x10\x02\x01"}, + {"vendor/golang.org/x/net/nettest", "\x03d\a\x03z\x11\x05\x16\x01\f\v\x01\x02\x02\x01\n"}, + {"vendor/golang.org/x/sys/cpu", "\x97\x02\r\v\x01\x15"}, + {"vendor/golang.org/x/text/secure/bidirule", "n\xd5\x01\x11\x01"}, + {"vendor/golang.org/x/text/transform", "\x03k}X"}, + {"vendor/golang.org/x/text/unicode/bidi", "\x03\bf~?\x15"}, + {"vendor/golang.org/x/text/unicode/norm", "g\nzG\x11\x11"}, + {"weak", "\x95\x01\x8f\x01!"}, +} diff --git a/internal/stdlib/deps_test.go b/internal/stdlib/deps_test.go new file mode 100644 index 00000000000..41d2d126ec5 --- /dev/null +++ b/internal/stdlib/deps_test.go @@ -0,0 +1,36 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stdlib_test + +import ( + "iter" + "os" + "slices" + "sort" + "strings" + "testing" + + "github.com/google/go-cmp/cmp" + "golang.org/x/tools/internal/stdlib" +) + +func TestImports(t *testing.T) { testDepsFunc(t, "testdata/nethttp.imports", stdlib.Imports) } +func TestDeps(t *testing.T) { testDepsFunc(t, "testdata/nethttp.deps", stdlib.Dependencies) } + +// testDepsFunc checks that the specified dependency function applied +// to net/http returns the set of dependencies in the named file. +func testDepsFunc(t *testing.T, filename string, depsFunc func(pkgs ...string) iter.Seq[string]) { + data, err := os.ReadFile(filename) + if err != nil { + t.Fatal(err) + } + want := strings.Split(strings.TrimSpace(string(data)), "\n") + got := slices.Collect(depsFunc("net/http")) + sort.Strings(want) + sort.Strings(got) + if diff := cmp.Diff(got, want); diff != "" { + t.Fatalf("Deps mismatch (-want +got):\n%s", diff) + } +} diff --git a/internal/stdlib/generate.go b/internal/stdlib/generate.go index 1192885405c..4c67d8bd797 100644 --- a/internal/stdlib/generate.go +++ b/internal/stdlib/generate.go @@ -7,11 +7,18 @@ // The generate command reads all the GOROOT/api/go1.*.txt files and // generates a single combined manifest.go file containing the Go // standard library API symbols along with versions. +// +// It also runs "go list -deps std" and records the import graph. This +// information may be used, for example, to ensure that tools don't +// suggest fixes that import package P when analyzing one of P's +// dependencies. package main import ( "bytes" "cmp" + "encoding/binary" + "encoding/json" "errors" "fmt" "go/format" @@ -19,6 +26,7 @@ import ( "io/fs" "log" "os" + "os/exec" "path/filepath" "regexp" "runtime" @@ -29,6 +37,13 @@ import ( ) func main() { + manifest() + deps() +} + +// -- generate std manifest -- + +func manifest() { pkgs := make(map[string]map[string]symInfo) // package -> symbol -> info symRE := regexp.MustCompile(`^pkg (\S+).*?, (var|func|type|const|method \([^)]*\)) ([\pL\p{Nd}_]+)(.*)`) @@ -131,7 +146,7 @@ func main() { // Write the combined manifest. var buf bytes.Buffer - buf.WriteString(`// Copyright 2024 The Go Authors. All rights reserved. + buf.WriteString(`// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -157,7 +172,7 @@ var PackageSymbols = map[string][]Symbol{ if err != nil { log.Fatal(err) } - if err := os.WriteFile("manifest.go", fmtbuf, 0666); err != nil { + if err := os.WriteFile("manifest.go", fmtbuf, 0o666); err != nil { log.Fatal(err) } } @@ -223,3 +238,109 @@ func removeTypeParam(s string) string { } return s } + +// -- generate dependency graph -- + +func deps() { + stdout := new(bytes.Buffer) + cmd := exec.Command("go", "list", "-deps", "-json", "std") + cmd.Stdout = stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Fatal(err) + } + + type Package struct { + // go list JSON output + ImportPath string // import path of package in dir + Imports []string // import paths used by this package + + // encoding + index int + deps []int // indices of direct imports, sorted + } + pkgs := make(map[string]*Package) + var keys []string + for dec := json.NewDecoder(stdout); dec.More(); { + var pkg Package + if err := dec.Decode(&pkg); err != nil { + log.Fatal(err) + } + pkgs[pkg.ImportPath] = &pkg + keys = append(keys, pkg.ImportPath) + } + + // Sort and number the packages. + // There are 344 as of Mar 2025. + slices.Sort(keys) + for i, name := range keys { + pkgs[name].index = i + } + + // Encode the dependencies. + for _, pkg := range pkgs { + for _, imp := range pkg.Imports { + if imp == "C" { + continue + } + pkg.deps = append(pkg.deps, pkgs[imp].index) + } + slices.Sort(pkg.deps) + } + + // Emit the table. + var buf bytes.Buffer + buf.WriteString(`// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Code generated by generate.go. DO NOT EDIT. + +package stdlib + +type pkginfo struct { + name string + deps string // list of indices of dependencies, as varint-encoded deltas +} +var deps = [...]pkginfo{ +`) + for _, name := range keys { + prev := 0 + var deps []int + for _, v := range pkgs[name].deps { + deps = append(deps, v-prev) // delta + prev = v + } + var data []byte + for _, v := range deps { + data = binary.AppendUvarint(data, uint64(v)) + } + fmt.Fprintf(&buf, "\t{%q, %q},\n", name, data) + } + fmt.Fprintln(&buf, "}") + + fmtbuf, err := format.Source(buf.Bytes()) + if err != nil { + log.Fatal(err) + } + if err := os.WriteFile("deps.go", fmtbuf, 0o666); err != nil { + log.Fatal(err) + } + + // Also generate the data for the test. + for _, t := range [...]struct{ flag, filename string }{ + {"-deps=true", "testdata/nethttp.deps"}, + {`-f={{join .Imports "\n"}}`, "testdata/nethttp.imports"}, + } { + stdout := new(bytes.Buffer) + cmd := exec.Command("go", "list", t.flag, "net/http") + cmd.Stdout = stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Fatal(err) + } + if err := os.WriteFile(t.filename, stdout.Bytes(), 0666); err != nil { + log.Fatal(err) + } + } +} diff --git a/internal/stdlib/import.go b/internal/stdlib/import.go new file mode 100644 index 00000000000..f6909878a8a --- /dev/null +++ b/internal/stdlib/import.go @@ -0,0 +1,89 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stdlib + +// This file provides the API for the import graph of the standard library. +// +// Be aware that the compiler-generated code for every package +// implicitly depends on package "runtime" and a handful of others +// (see runtimePkgs in GOROOT/src/cmd/internal/objabi/pkgspecial.go). + +import ( + "encoding/binary" + "iter" + "slices" + "strings" +) + +// Imports returns the sequence of packages directly imported by the +// named standard packages, in name order. +// The imports of an unknown package are the empty set. +// +// The graph is built into the application and may differ from the +// graph in the Go source tree being analyzed by the application. +func Imports(pkgs ...string) iter.Seq[string] { + return func(yield func(string) bool) { + for _, pkg := range pkgs { + if i, ok := find(pkg); ok { + var depIndex uint64 + for data := []byte(deps[i].deps); len(data) > 0; { + delta, n := binary.Uvarint(data) + depIndex += delta + if !yield(deps[depIndex].name) { + return + } + data = data[n:] + } + } + } + } +} + +// Dependencies returns the set of all dependencies of the named +// standard packages, including the initial package, +// in a deterministic topological order. +// The dependencies of an unknown package are the empty set. +// +// The graph is built into the application and may differ from the +// graph in the Go source tree being analyzed by the application. +func Dependencies(pkgs ...string) iter.Seq[string] { + return func(yield func(string) bool) { + for _, pkg := range pkgs { + if i, ok := find(pkg); ok { + var seen [1 + len(deps)/8]byte // bit set of seen packages + var visit func(i int) bool + visit = func(i int) bool { + bit := byte(1) << (i % 8) + if seen[i/8]&bit == 0 { + seen[i/8] |= bit + var depIndex uint64 + for data := []byte(deps[i].deps); len(data) > 0; { + delta, n := binary.Uvarint(data) + depIndex += delta + if !visit(int(depIndex)) { + return false + } + data = data[n:] + } + if !yield(deps[i].name) { + return false + } + } + return true + } + if !visit(i) { + return + } + } + } + } +} + +// find returns the index of pkg in the deps table. +func find(pkg string) (int, bool) { + return slices.BinarySearchFunc(deps[:], pkg, func(p pkginfo, n string) int { + return strings.Compare(p.name, n) + }) +} diff --git a/internal/stdlib/manifest.go b/internal/stdlib/manifest.go index e7d0aee2186..00776a31b60 100644 --- a/internal/stdlib/manifest.go +++ b/internal/stdlib/manifest.go @@ -1,4 +1,4 @@ -// Copyright 2024 The Go Authors. All rights reserved. +// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -7119,6 +7119,7 @@ var PackageSymbols = map[string][]Symbol{ {"FormatFileInfo", Func, 21}, {"Glob", Func, 16}, {"GlobFS", Type, 16}, + {"Lstat", Func, 25}, {"ModeAppend", Const, 16}, {"ModeCharDevice", Const, 16}, {"ModeDevice", Const, 16}, @@ -7143,6 +7144,8 @@ var PackageSymbols = map[string][]Symbol{ {"ReadDirFile", Type, 16}, {"ReadFile", Func, 16}, {"ReadFileFS", Type, 16}, + {"ReadLink", Func, 25}, + {"ReadLinkFS", Type, 25}, {"SkipAll", Var, 20}, {"SkipDir", Var, 16}, {"Stat", Func, 16}, @@ -9146,6 +9149,8 @@ var PackageSymbols = map[string][]Symbol{ {"(*ProcessState).SysUsage", Method, 0}, {"(*ProcessState).SystemTime", Method, 0}, {"(*ProcessState).UserTime", Method, 0}, + {"(*Root).Chmod", Method, 25}, + {"(*Root).Chown", Method, 25}, {"(*Root).Close", Method, 24}, {"(*Root).Create", Method, 24}, {"(*Root).FS", Method, 24}, @@ -16754,9 +16759,11 @@ var PackageSymbols = map[string][]Symbol{ }, "testing/fstest": { {"(MapFS).Glob", Method, 16}, + {"(MapFS).Lstat", Method, 25}, {"(MapFS).Open", Method, 16}, {"(MapFS).ReadDir", Method, 16}, {"(MapFS).ReadFile", Method, 16}, + {"(MapFS).ReadLink", Method, 25}, {"(MapFS).Stat", Method, 16}, {"(MapFS).Sub", Method, 16}, {"MapFS", Type, 16}, diff --git a/internal/stdlib/stdlib.go b/internal/stdlib/stdlib.go index 98904017f2c..3d96d3bf686 100644 --- a/internal/stdlib/stdlib.go +++ b/internal/stdlib/stdlib.go @@ -6,7 +6,7 @@ // Package stdlib provides a table of all exported symbols in the // standard library, along with the version at which they first -// appeared. +// appeared. It also provides the import graph of std packages. package stdlib import ( diff --git a/internal/stdlib/testdata/nethttp.deps b/internal/stdlib/testdata/nethttp.deps new file mode 100644 index 00000000000..e1235e84932 --- /dev/null +++ b/internal/stdlib/testdata/nethttp.deps @@ -0,0 +1,171 @@ +internal/goarch +unsafe +internal/abi +internal/unsafeheader +internal/cpu +internal/bytealg +internal/byteorder +internal/chacha8rand +internal/coverage/rtcov +internal/godebugs +internal/goexperiment +internal/goos +internal/profilerecord +internal/runtime/atomic +internal/runtime/exithook +internal/asan +internal/msan +internal/race +internal/runtime/math +internal/runtime/sys +internal/runtime/maps +internal/stringslite +internal/trace/tracev2 +runtime +internal/reflectlite +errors +sync/atomic +internal/sync +sync +io +iter +math/bits +unicode +unicode/utf8 +bytes +strings +bufio +cmp +internal/itoa +math +strconv +reflect +slices +internal/fmtsort +internal/oserror +path +internal/bisect +internal/godebug +syscall +time +io/fs +internal/filepathlite +internal/syscall/unix +internal/poll +internal/syscall/execenv +internal/testlog +os +fmt +sort +compress/flate +encoding/binary +hash +hash/crc32 +compress/gzip +container/list +context +crypto +crypto/internal/fips140deps/godebug +crypto/internal/fips140 +crypto/internal/fips140/alias +crypto/internal/fips140deps/byteorder +crypto/internal/fips140deps/cpu +crypto/internal/impl +crypto/internal/fips140/sha256 +crypto/internal/fips140/subtle +crypto/internal/fips140/sha3 +crypto/internal/fips140/sha512 +crypto/internal/fips140/hmac +crypto/internal/fips140/check +crypto/internal/fips140/aes +crypto/internal/sysrand +crypto/internal/entropy +math/rand/v2 +crypto/internal/randutil +crypto/internal/fips140/drbg +crypto/internal/fips140/aes/gcm +crypto/internal/fips140only +crypto/subtle +crypto/cipher +crypto/internal/boring/sig +crypto/internal/boring +math/rand +math/big +crypto/rand +crypto/aes +crypto/des +crypto/internal/fips140/nistec/fiat +crypto/internal/fips140/nistec +crypto/internal/fips140/ecdh +crypto/internal/fips140/edwards25519/field +crypto/ecdh +crypto/elliptic +crypto/internal/boring/bbig +crypto/internal/fips140/bigmod +crypto/internal/fips140/ecdsa +crypto/sha3 +crypto/internal/fips140hash +crypto/sha512 +unicode/utf16 +encoding/asn1 +vendor/golang.org/x/crypto/cryptobyte/asn1 +vendor/golang.org/x/crypto/cryptobyte +crypto/ecdsa +crypto/internal/fips140/edwards25519 +crypto/internal/fips140/ed25519 +crypto/ed25519 +crypto/hmac +crypto/internal/fips140/hkdf +crypto/internal/fips140/mlkem +crypto/internal/fips140/tls12 +crypto/internal/fips140/tls13 +vendor/golang.org/x/crypto/internal/alias +vendor/golang.org/x/crypto/chacha20 +vendor/golang.org/x/crypto/internal/poly1305 +vendor/golang.org/x/crypto/chacha20poly1305 +crypto/internal/hpke +crypto/md5 +crypto/rc4 +crypto/internal/fips140/rsa +crypto/rsa +crypto/sha1 +crypto/sha256 +crypto/tls/internal/fips140tls +crypto/dsa +crypto/x509/internal/macos +encoding/hex +crypto/x509/pkix +encoding/base64 +encoding/pem +maps +vendor/golang.org/x/net/dns/dnsmessage +internal/nettrace +weak +unique +net/netip +internal/routebsd +internal/singleflight +net +net/url +crypto/x509 +crypto/tls +vendor/golang.org/x/text/transform +log/internal +log +vendor/golang.org/x/text/unicode/bidi +vendor/golang.org/x/text/secure/bidirule +vendor/golang.org/x/text/unicode/norm +vendor/golang.org/x/net/idna +net/textproto +vendor/golang.org/x/net/http/httpguts +vendor/golang.org/x/net/http/httpproxy +vendor/golang.org/x/net/http2/hpack +mime +mime/quotedprintable +path/filepath +mime/multipart +net/http/httptrace +net/http/internal +net/http/internal/ascii +net/http/internal/httpcommon +net/http diff --git a/internal/stdlib/testdata/nethttp.imports b/internal/stdlib/testdata/nethttp.imports new file mode 100644 index 00000000000..77e78696bdd --- /dev/null +++ b/internal/stdlib/testdata/nethttp.imports @@ -0,0 +1,47 @@ +bufio +bytes +compress/gzip +container/list +context +crypto/rand +crypto/tls +encoding/base64 +encoding/binary +errors +fmt +vendor/golang.org/x/net/http/httpguts +vendor/golang.org/x/net/http/httpproxy +vendor/golang.org/x/net/http2/hpack +vendor/golang.org/x/net/idna +internal/godebug +io +io/fs +log +maps +math +math/bits +math/rand +mime +mime/multipart +net +net/http/httptrace +net/http/internal +net/http/internal/ascii +net/http/internal/httpcommon +net/textproto +net/url +os +path +path/filepath +reflect +runtime +slices +sort +strconv +strings +sync +sync/atomic +time +unicode +unicode/utf8 +unsafe From 5f02a3e879c4c45e42f3a630f971a0f6a13110e5 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 28 Feb 2025 10:31:17 -0500 Subject: [PATCH 211/309] gopls/internal/analysis/modernize: don't import slices within slices This CL strengthens the check that the various modernizer passes use to skip packages in which the fixes cannot be applied. Before, we would not add an import of "slices" from withing the "slices" package itself, but we cannot add this import from any package that "slices" itself transitively depends upon, as this would create an import cycle. So, we consult the std dependency graph baked into the executable. This feature was tested interactively by running modernize on std. Updates golang/go#71847 Change-Id: Iaec6ef07b58ca07df498db63369dae8087331ab9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653595 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/maps.go | 4 +++- .../internal/analysis/modernize/modernize.go | 22 +++++++++++++++++++ gopls/internal/analysis/modernize/slices.go | 5 +++-- .../analysis/modernize/slicescontains.go | 5 +++-- .../analysis/modernize/slicesdelete.go | 6 +++++ .../internal/analysis/modernize/sortslice.go | 4 +++- gopls/internal/util/moreiters/iters.go | 10 +++++++++ 7 files changed, 50 insertions(+), 6 deletions(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index dad329477cd..5577978278c 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -41,7 +41,9 @@ import ( // m = make(M) // m = M{} func mapsloop(pass *analysis.Pass) { - if pass.Pkg.Path() == "maps " { + // Skip the analyzer in packages where its + // fixes would create an import cycle. + if within(pass, "maps", "bytes", "runtime") { return } diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 0f7b58eed37..354836d6b40 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -18,8 +18,10 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/gopls/internal/util/astutil" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/stdlib" "golang.org/x/tools/internal/versions" ) @@ -125,6 +127,26 @@ func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) } } +// within reports whether the current pass is analyzing one of the +// specified standard packages or their dependencies. +func within(pass *analysis.Pass, pkgs ...string) bool { + path := pass.Pkg.Path() + return standard(path) && + moreiters.Contains(stdlib.Dependencies(pkgs...), path) +} + +// standard reports whether the specified package path belongs to a +// package in the standard library (including internal dependencies). +func standard(path string) bool { + // A standard package has no dot in its first segment. + // (It may yet have a dot, e.g. "vendor/golang.org/x/foo".) + slash := strings.IndexByte(path, '/') + if slash < 0 { + slash = len(path) + } + return !strings.Contains(path[:slash], ".") && path != "testdata" +} + var ( builtinAny = types.Universe.Lookup("any") builtinAppend = types.Universe.Lookup("append") diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index 9cca3e98156..7e0d9cbd92e 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -46,8 +46,9 @@ import ( // The fix does not always preserve nilness the of base slice when the // addends (a, b, c) are all empty. func appendclipped(pass *analysis.Pass) { - switch pass.Pkg.Path() { - case "slices", "bytes": + // Skip the analyzer in packages where its + // fixes would create an import cycle. + if within(pass, "slices", "bytes", "runtime") { return } diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index 09642448bb5..b59ea452a0f 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -47,8 +47,9 @@ import ( // (Mostly this appears to be a desirable optimization, avoiding // redundantly repeated evaluation.) func slicescontains(pass *analysis.Pass) { - // Don't modify the slices package itself. - if pass.Pkg.Path() == "slices" { + // Skip the analyzer in packages where its + // fixes would create an import cycle. + if within(pass, "slices", "runtime") { return } diff --git a/gopls/internal/analysis/modernize/slicesdelete.go b/gopls/internal/analysis/modernize/slicesdelete.go index 24b2182ca6a..3c3d880f62b 100644 --- a/gopls/internal/analysis/modernize/slicesdelete.go +++ b/gopls/internal/analysis/modernize/slicesdelete.go @@ -21,6 +21,12 @@ import ( // Other variations that will also have suggested replacements include: // append(s[:i-1], s[i:]...) and append(s[:i+k1], s[i+k2:]) where k2 > k1. func slicesdelete(pass *analysis.Pass) { + // Skip the analyzer in packages where its + // fixes would create an import cycle. + if within(pass, "slices", "runtime") { + return + } + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) info := pass.TypesInfo report := func(file *ast.File, call *ast.CallExpr, slice1, slice2 *ast.SliceExpr) { diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index a033be7f635..0437aaf2f67 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -36,7 +36,9 @@ import ( // - sort.Sort(x) where x has a named slice type whose Less method is the natural order. // -> sort.Slice(x) func sortslice(pass *analysis.Pass) { - if !analysisinternal.Imports(pass.Pkg, "sort") { + // Skip the analyzer in packages where its + // fixes would create an import cycle. + if within(pass, "slices", "sort", "runtime") { return } diff --git a/gopls/internal/util/moreiters/iters.go b/gopls/internal/util/moreiters/iters.go index e4d83ae8618..d41cb1d3bca 100644 --- a/gopls/internal/util/moreiters/iters.go +++ b/gopls/internal/util/moreiters/iters.go @@ -14,3 +14,13 @@ func First[T any](seq iter.Seq[T]) (z T, ok bool) { } return z, false } + +// Contains reports whether x is an element of the sequence seq. +func Contains[T comparable](seq iter.Seq[T], x T) bool { + for cand := range seq { + if cand == x { + return true + } + } + return false +} From d14149970b9aba669e3257bfc34df2994a2a2fbc Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Thu, 20 Feb 2025 13:43:48 +0200 Subject: [PATCH 212/309] cmd/toolstash: fix windows executable name handling Change-Id: I1ff643fae4c48b4f68b452eb6881fca99832930c Reviewed-on: https://go-review.googlesource.com/c/tools/+/650915 Reviewed-by: Junyang Shao Reviewed-by: Michael Pratt Auto-Submit: Sean Liao Commit-Queue: Sean Liao Reviewed-by: Sean Liao LUCI-TryBot-Result: Go LUCI --- cmd/toolstash/main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/toolstash/main.go b/cmd/toolstash/main.go index c533ed1e572..3a92c00bfff 100644 --- a/cmd/toolstash/main.go +++ b/cmd/toolstash/main.go @@ -225,7 +225,7 @@ func main() { return } - tool = cmd[0] + tool = exeName(cmd[0]) if i := strings.LastIndexAny(tool, `/\`); i >= 0 { tool = tool[i+1:] } @@ -530,7 +530,7 @@ func runCmd(cmd []string, keepLog bool, logName string) (output []byte, err erro }() } - xcmd := exec.Command(cmd[0], cmd[1:]...) + xcmd := exec.Command(exeName(cmd[0]), cmd[1:]...) if !keepLog { return xcmd.CombinedOutput() } @@ -571,9 +571,10 @@ func save() { if !shouldSave(name) { continue } - src := filepath.Join(binDir, name) + bin := exeName(name) + src := filepath.Join(binDir, bin) if _, err := os.Stat(src); err == nil { - cp(src, filepath.Join(stashDir, name)) + cp(src, filepath.Join(stashDir, bin)) } } @@ -641,3 +642,10 @@ func cp(src, dst string) { log.Fatal(err) } } + +func exeName(name string) string { + if runtime.GOOS == "windows" { + return name + ".exe" + } + return name +} From 0efa5e51a822f9f580ed226cd8cd96089bc2d80d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 28 Feb 2025 15:56:52 -0500 Subject: [PATCH 213/309] gopls/internal/analysis/modernize: rangeint: non-integer untyped constants This CL fixes a bug in rangeint that caused it to replace const limit = 1e3 for i := 0; i < limit; i++ {} with for range limit {} // error: limit is not an integer Now, we check that the type of limit is assignable to int, and if not insert an explicit int(limit) conversion. Updates golang/go#71847 (item d) Change-Id: Icfaa96e5506fcb5a3e6f3ed8f911bf4bda9cf32f Reviewed-on: https://go-review.googlesource.com/c/tools/+/653616 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/rangeint.go | 42 +++++++++++++++++++ .../testdata/src/rangeint/rangeint.go | 11 +++++ .../testdata/src/rangeint/rangeint.go.golden | 11 +++++ 3 files changed, 64 insertions(+) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index b94bff34431..2921bbb3468 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -31,6 +31,8 @@ import ( // - The ':=' may be replaced by '='. // - The fix may remove "i :=" if it would become unused. // +// TODO(adonovan): permit variants such as "i := int64(0)". +// // Restrictions: // - The variable i must not be assigned or address-taken within the // loop, because a "for range int" loop does not respect assignments @@ -120,6 +122,31 @@ func rangeint(pass *analysis.Pass) { limit = call.Args[0] } + // If the limit is a untyped constant of non-integer type, + // such as "const limit = 1e3", its effective type may + // differ between the two forms. + // In a for loop, it must be comparable with int i, + // for i := 0; i < limit; i++ + // but in a range loop it would become a float, + // for i := range limit {} + // which is a type error. We need to convert it to int + // in this case. + // + // Unfortunately go/types discards the untyped type + // (but see Untyped in golang/go#70638) so we must + // re-type check the expression to detect this case. + var beforeLimit, afterLimit string + if v := info.Types[limit].Value; v != nil { + beforeLimit, afterLimit = "int(", ")" + info2 := &types.Info{Types: make(map[ast.Expr]types.TypeAndValue)} + if types.CheckExpr(pass.Fset, pass.Pkg, limit.Pos(), limit, info2) == nil { + tLimit := types.Default(info2.TypeOf(limit)) + if types.AssignableTo(tLimit, types.Typ[types.Int]) { + beforeLimit, afterLimit = "", "" + } + } + } + pass.Report(analysis.Diagnostic{ Pos: init.Pos(), End: inc.End(), @@ -133,15 +160,30 @@ func rangeint(pass *analysis.Pass) { // ----- --- // ------- // for i := range limit {} + + // Delete init. { Pos: init.Rhs[0].Pos(), End: limit.Pos(), NewText: []byte("range "), }, + // Add "int(" before limit, if needed. + { + Pos: limit.Pos(), + End: limit.Pos(), + NewText: []byte(beforeLimit), + }, + // Delete inc. { Pos: limit.End(), End: inc.End(), }, + // Add ")" after limit, if needed. + { + Pos: limit.End(), + End: limit.End(), + NewText: []byte(afterLimit), + }, }...), }}, }) diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index 32628f5fae3..da486dcd32c 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -66,3 +66,14 @@ func nopePostconditionDiffers() { } println(i) // must print 5, not 4 } + +// Non-integer untyped constants need to be explicitly converted to int. +func issue71847d() { + const limit = 1e3 // float + for i := 0; i < limit; i++ { // want "for loop can be modernized using range over int" + } + + const limit2 = 1 + 0i // complex + for i := 0; i < limit2; i++ { // want "for loop can be modernized using range over int" + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 43cf220d699..01d28ccb92b 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -66,3 +66,14 @@ func nopePostconditionDiffers() { } println(i) // must print 5, not 4 } + +// Non-integer untyped constants need to be explicitly converted to int. +func issue71847d() { + const limit = 1e3 // float + for range int(limit) { // want "for loop can be modernized using range over int" + } + + const limit2 = 1 + 0i // complex + for range int(limit2) { // want "for loop can be modernized using range over int" + } +} From 2839096cd63a762fc544b0a489afe080032c472d Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 21 Feb 2025 15:03:43 -0500 Subject: [PATCH 214/309] gopls/internal/analysis/gofix: generic aliases Support inlining generic aliases. For golang/go#32816. Change-Id: Ic65e6fb30d65ee0f7d6e0093fd882a675de71da4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651617 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/gofix/gofix.go | 68 +++++++++++++------ .../analysis/gofix/testdata/src/a/a.go | 22 ++++++ .../analysis/gofix/testdata/src/a/a.go.golden | 25 +++++++ 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 7323028aa31..7a55d7ca93d 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -9,6 +9,7 @@ import ( "go/ast" "go/token" "go/types" + "slices" "strings" _ "embed" @@ -140,11 +141,6 @@ func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) { } } - if spec.TypeParams != nil { - // TODO(jba): handle generic aliases - return - } - // Remember that this is an inlinable alias. typ := &goFixInlineAliasFact{} lhs := a.pass.TypesInfo.Defs[spec.Name].(*types.TypeName) @@ -294,7 +290,7 @@ func (a *analyzer) inlineCall(call *ast.CallExpr, cur cursor.Cursor) { } // If tn is the TypeName of an inlinable alias, suggest inlining its use at cur. -func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { +func (a *analyzer) inlineAlias(tn *types.TypeName, curId cursor.Cursor) { inalias, ok := a.inlinableAliases[tn] if !ok { var fact goFixInlineAliasFact @@ -307,12 +303,17 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { return // nope } - // Get the alias's RHS. It has everything we need to format the replacement text. - rhs := tn.Type().(*types.Alias).Rhs() - + alias := tn.Type().(*types.Alias) + // Remember the names of the alias's type params. When we check for shadowing + // later, we'll ignore these because they won't appear in the replacement text. + typeParamNames := map[*types.TypeName]bool{} + for tp := range alias.TypeParams().TypeParams() { + typeParamNames[tp.Obj()] = true + } + rhs := alias.Rhs() curPath := a.pass.Pkg.Path() - curFile := currentFile(cur) - n := cur.Node().(*ast.Ident) + curFile := currentFile(curId) + id := curId.Node().(*ast.Ident) // We have an identifier A here (n), possibly qualified by a package // identifier (sel.n), and an inlinable "type A = rhs" elsewhere. // @@ -324,6 +325,10 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { edits []analysis.TextEdit ) for _, tn := range typenames(rhs) { + // Ignore the type parameters of the alias: they won't appear in the result. + if typeParamNames[tn] { + continue + } var pkgPath, pkgName string if pkg := tn.Pkg(); pkg != nil { pkgPath = pkg.Path() @@ -333,9 +338,9 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { // The name is in the current package or the universe scope, so no import // is required. Check that it is not shadowed (that is, that the type // it refers to in rhs is the same one it refers to at n). - scope := a.pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope - _, obj := scope.LookupParent(tn.Name(), n.Pos()) // what qn.name means in n's scope - if obj != tn { // shadowed + scope := a.pass.TypesInfo.Scopes[curFile].Innermost(id.Pos()) // n's scope + _, obj := scope.LookupParent(tn.Name(), id.Pos()) // what qn.name means in n's scope + if obj != tn { return } } else if !analysisinternal.CanImport(a.pass.Pkg.Path(), pkgPath) { @@ -345,15 +350,40 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, cur cursor.Cursor) { // Use AddImport to add pkgPath if it's not there already. Associate the prefix it assigns // with the package path for use by the TypeString qualifier below. _, prefix, eds := analysisinternal.AddImport( - a.pass.TypesInfo, curFile, pkgName, pkgPath, tn.Name(), n.Pos()) + a.pass.TypesInfo, curFile, pkgName, pkgPath, tn.Name(), id.Pos()) importPrefixes[pkgPath] = strings.TrimSuffix(prefix, ".") edits = append(edits, eds...) } } - // If n is qualified by a package identifier, we'll need the full selector expression. - var expr ast.Expr = n - if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { - expr = cur.Parent().Node().(ast.Expr) + // Find the complete identifier, which may take any of these forms: + // Id + // Id[T] + // Id[K, V] + // pkg.Id + // pkg.Id[T] + // pkg.Id[K, V] + var expr ast.Expr = id + if e, _ := curId.Edge(); e == edge.SelectorExpr_Sel { + curId = curId.Parent() + expr = curId.Node().(ast.Expr) + } + // If expr is part of an IndexExpr or IndexListExpr, we'll need that node. + // Given C[int], TypeOf(C) is generic but TypeOf(C[int]) is instantiated. + switch ek, _ := curId.Edge(); ek { + case edge.IndexExpr_X: + expr = curId.Parent().Node().(*ast.IndexExpr) + case edge.IndexListExpr_X: + expr = curId.Parent().Node().(*ast.IndexListExpr) + } + t := a.pass.TypesInfo.TypeOf(expr).(*types.Alias) // type of entire identifier + if targs := t.TypeArgs(); targs.Len() > 0 { + // Instantiate the alias with the type args from this use. + // For example, given type A = M[K, V], compute the type of the use + // A[int, Foo] as M[int, Foo]. + // Don't validate instantiation: it can't panic unless we have a bug, + // in which case seeing the stack trace via telemetry would be helpful. + instAlias, _ := types.Instantiate(nil, alias, slices.Collect(targs.Types()), false) + rhs = instAlias.(*types.Alias).Rhs() } // To get the replacement text, render the alias RHS using the package prefixes // we assigned above. diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index 49a0587c2b1..60a55052584 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -164,3 +164,25 @@ func _[P any]() { _ = x _ = y } + +// generic type aliases + +//go:fix inline +type ( + Mapset[T comparable] = map[T]bool // want Mapset: `goFixInline alias` + Pair[X, Y any] = struct { // want Pair: `goFixInline alias` + X X + Y Y + } +) + +var _ Mapset[int] // want `Type alias Mapset\[int\] should be inlined` + +var _ Pair[T, string] // want `Type alias Pair\[T, string\] should be inlined` + +func _[V any]() { + //go:fix inline + type M[K comparable] = map[K]V + + var _ M[int] // want `Type alias M\[int\] should be inlined` +} diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index 9d4c527919e..c637da103ee 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -165,3 +165,28 @@ func _[P any]() { _ = x _ = y } + +// generic type aliases + +//go:fix inline +type ( + Mapset[T comparable] = map[T]bool // want Mapset: `goFixInline alias` + Pair[X, Y any] = struct { // want Pair: `goFixInline alias` + X X + Y Y + } +) + +var _ map[int]bool // want `Type alias Mapset\[int\] should be inlined` + +var _ struct { + X T + Y string +} // want `Type alias Pair\[T, string\] should be inlined` + +func _[V any]() { + //go:fix inline + type M[K comparable] = map[K]V + + var _ map[int]V // want `Type alias M\[int\] should be inlined` +} From 0ffdb82ead2753b9fbba8bf0932ba396b13ba6ea Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 21 Feb 2025 16:55:53 -0500 Subject: [PATCH 215/309] gopls/internal/analysis/gofix: add vet analyzer Add a second analyzer that checks for valid go:fix inline directives without suggesting changes. Change-Id: I0b9ad3da79f554caef01dda66ef954c59718015d Reviewed-on: https://go-review.googlesource.com/c/tools/+/651656 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/gofix/doc.go | 10 ++- gopls/internal/analysis/gofix/gofix.go | 26 ++++++- gopls/internal/analysis/gofix/gofix_test.go | 5 ++ .../gofix/testdata/src/directive/directive.go | 63 ++++++++++++++++ .../src/directive/directive.go.golden | 71 +++++++++++++++++++ 5 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 gopls/internal/analysis/gofix/testdata/src/directive/directive.go create mode 100644 gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden diff --git a/gopls/internal/analysis/gofix/doc.go b/gopls/internal/analysis/gofix/doc.go index ad8b067daa4..15de4f28b27 100644 --- a/gopls/internal/analysis/gofix/doc.go +++ b/gopls/internal/analysis/gofix/doc.go @@ -5,7 +5,8 @@ /* Package gofix defines an Analyzer that inlines calls to functions and uses of constants -marked with a "//go:fix inline" doc comment. +marked with a "//go:fix inline" directive. +A second analyzer only checks uses of the directive. # Analyzer gofix @@ -81,5 +82,12 @@ The proposal https://go.dev/issue/32816 introduces the "//go:fix" directives. You can use this (officially unsupported) command to apply gofix fixes en masse: $ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./... + +# Analyzer gofixdirective + +gofixdirective: validate uses of gofix comment directives + +The gofixdirective analyzer checks "//go:fix inline" directives for correctness. +See the documentation for the gofix analyzer for more about "/go:fix inline". */ package gofix diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 7a55d7ca93d..df7154ca2fc 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -34,7 +34,20 @@ var Analyzer = &analysis.Analyzer{ Name: "gofix", Doc: analysisinternal.MustExtractDoc(doc, "gofix"), URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", - Run: run, + Run: func(pass *analysis.Pass) (any, error) { return run(pass, true) }, + FactTypes: []analysis.Fact{ + (*goFixInlineFuncFact)(nil), + (*goFixInlineConstFact)(nil), + (*goFixInlineAliasFact)(nil), + }, + Requires: []*analysis.Analyzer{inspect.Analyzer}, +} + +var DirectiveAnalyzer = &analysis.Analyzer{ + Name: "gofixdirective", + Doc: analysisinternal.MustExtractDoc(doc, "gofixdirective"), + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + Run: func(pass *analysis.Pass) (any, error) { return run(pass, false) }, FactTypes: []analysis.Fact{ (*goFixInlineFuncFact)(nil), (*goFixInlineConstFact)(nil), @@ -46,6 +59,7 @@ var Analyzer = &analysis.Analyzer{ // analyzer holds the state for this analysis. type analyzer struct { pass *analysis.Pass + fix bool // only suggest fixes if true; else, just check directives root cursor.Cursor // memoization of repeated calls for same file. fileContent map[string][]byte @@ -55,9 +69,10 @@ type analyzer struct { inlinableAliases map[*types.TypeName]*goFixInlineAliasFact } -func run(pass *analysis.Pass) (any, error) { +func run(pass *analysis.Pass, fix bool) (any, error) { a := &analyzer{ pass: pass, + fix: fix, root: cursor.Root(pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)), fileContent: make(map[string][]byte), inlinableFuncs: make(map[*types.Func]*inline.Callee), @@ -256,6 +271,10 @@ func (a *analyzer) inlineCall(call *ast.CallExpr, cur cursor.Cursor) { a.pass.Reportf(call.Lparen, "%v", err) return } + if !a.fix { + return + } + if res.Literalized { // Users are not fond of inlinings that literalize // f(x) to func() { ... }(), so avoid them. @@ -533,6 +552,9 @@ func (a *analyzer) inlineConst(con *types.Const, cur cursor.Cursor) { // reportInline reports a diagnostic for fixing an inlinable name. func (a *analyzer) reportInline(kind, capKind string, ident ast.Expr, edits []analysis.TextEdit, newText string) { + if !a.fix { + return + } edits = append(edits, analysis.TextEdit{ Pos: ident.Pos(), End: ident.End(), diff --git a/gopls/internal/analysis/gofix/gofix_test.go b/gopls/internal/analysis/gofix/gofix_test.go index dc98ef47181..4acc4daf2ff 100644 --- a/gopls/internal/analysis/gofix/gofix_test.go +++ b/gopls/internal/analysis/gofix/gofix_test.go @@ -22,6 +22,11 @@ func TestAnalyzer(t *testing.T) { analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "a", "b") } +func TestDirectiveAnalyzer(t *testing.T) { + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), DirectiveAnalyzer, "directive") + +} + func TestTypesWithNames(t *testing.T) { // Test setup inspired by internal/analysisinternal/addimport_test.go. testenv.NeedsDefaultImporter(t) diff --git a/gopls/internal/analysis/gofix/testdata/src/directive/directive.go b/gopls/internal/analysis/gofix/testdata/src/directive/directive.go new file mode 100644 index 00000000000..47c2884c386 --- /dev/null +++ b/gopls/internal/analysis/gofix/testdata/src/directive/directive.go @@ -0,0 +1,63 @@ +package directive + +// Functions. + +func f() { + One() + + new(T).Two() +} + +type T struct{} + +//go:fix inline +func One() int { return one } // want One:`goFixInline directive.One` + +const one = 1 + +//go:fix inline +func (T) Two() int { return 2 } // want Two:`goFixInline \(directive.T\).Two` + +// Constants. + +const Uno = 1 + +//go:fix inline +const In1 = Uno // want In1: `goFixInline const "directive".Uno` + +const ( + no1 = one + + //go:fix inline + In2 = one // want In2: `goFixInline const "directive".one` +) + +//go:fix inline +const bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +//go:fix inline +const in5, + in6, + bad2 = one, one, + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +// Make sure we don't crash on iota consts, but still process the whole decl. +// +//go:fix inline +const ( + a = iota // want `invalid //go:fix inline directive: const value is iota` + b + in7 = one +) + +const ( + x = 1 + //go:fix inline + in8 = x +) + +//go:fix inline +const in9 = iota // want `invalid //go:fix inline directive: const value is iota` + +//go:fix inline +type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array types not supported` diff --git a/gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden b/gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden new file mode 100644 index 00000000000..3e5b3409288 --- /dev/null +++ b/gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden @@ -0,0 +1,71 @@ +package golden + +import "a/internal" + +// Functions. + +func f() { + One() + + new(T).Two() +} + +type T struct{} + +//go:fix inline +func One() int { return one } + +const one = 1 + +//go:fix inline +func (T) Two() int { return 2 } + +// Constants. + +const Uno = 1 + +//go:fix inline +const In1 = Uno // want In1: `goFixInline const "a".Uno` + +const ( + no1 = one + + //go:fix inline + In2 = one // want In2: `goFixInline const "a".one` +) + +//go:fix inline +const bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +//go:fix inline +const in5, + in6, + bad2 = one, one, + one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant` + +// Make sure we don't crash on iota consts, but still process the whole decl. +// +//go:fix inline +const ( + a = iota // want `invalid //go:fix inline directive: const value is iota` + b + in7 = one +) + +const ( + x = 1 + //go:fix inline + in8 = x +) + +//go:fix inline +const a = iota // want `invalid //go:fix inline directive: const value is iota` + +//go:fix inline +type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array types not supported` + +// literal array lengths are OK +// +//go:fix inline +type EL = map[[2]string][]*T // want EL: `goFixInline alias` + From 2b1f55036370bc9a05bed74aa13fa85fecce40e2 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 21 Feb 2025 16:24:42 -0500 Subject: [PATCH 216/309] gopls/internal/analysis/gofix: allow literal array lengths An array type can be inlined if its length is a literal integer. For golang/go#32816. Change-Id: I80c7f18721c813a0ea7039411ddf8a804b5bf0b5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651655 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/analysis/gofix/gofix.go | 5 ++++- gopls/internal/analysis/gofix/testdata/src/a/a.go | 7 +++++++ gopls/internal/analysis/gofix/testdata/src/a/a.go.golden | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index df7154ca2fc..41cebcb63b9 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -148,9 +148,12 @@ func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) { // type A = [N]int // // would result in [5]int, breaking the connection with N. - // TODO(jba): accept type expressions where the array size is a literal integer for n := range ast.Preorder(spec.Type) { if ar, ok := n.(*ast.ArrayType); ok && ar.Len != nil { + // Make an exception when the array length is a literal int. + if lit, ok := ast.Unparen(ar.Len).(*ast.BasicLit); ok && lit.Kind == token.INT { + continue + } a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: array types not supported") return } diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/gopls/internal/analysis/gofix/testdata/src/a/a.go index 60a55052584..96f4f4d4e13 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go @@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array var _ E // nothing should happen here +// literal array lengths are OK +// +//go:fix inline +type EL = map[[2]string][]*T // want EL: `goFixInline alias` + +var _ EL // want `Type alias EL should be inlined` + //go:fix inline type F = map[internal.T]T // want F: `goFixInline alias` diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden index c637da103ee..64d08ec1548 100644 --- a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden @@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array var _ E // nothing should happen here +// literal array lengths are OK +// +//go:fix inline +type EL = map[[2]string][]*T // want EL: `goFixInline alias` + +var _ map[[2]string][]*T // want `Type alias EL should be inlined` + //go:fix inline type F = map[internal.T]T // want F: `goFixInline alias` From 455db21bd963fea3efdf0473e0ddce37313b8f91 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 28 Feb 2025 11:09:28 -0500 Subject: [PATCH 217/309] gopls/internal/cache/parsego: fix OOB crash in fixInitStmt This is a priori not how to use safetoken. I haven't attempted to reproduce the crash. Fixes golang/go#72026 Change-Id: I7a95383032f9a882c8b667203bbe0cf06f85a987 Reviewed-on: https://go-review.googlesource.com/c/tools/+/653596 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/cache/parsego/parse.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gopls/internal/cache/parsego/parse.go b/gopls/internal/cache/parsego/parse.go index 08a1c395a2a..fd598e235d1 100644 --- a/gopls/internal/cache/parsego/parse.go +++ b/gopls/internal/cache/parsego/parse.go @@ -528,11 +528,11 @@ func fixInitStmt(bad *ast.BadExpr, parent ast.Node, tok *token.File, src []byte) } // Try to extract a statement from the BadExpr. - start, end, err := safetoken.Offsets(tok, bad.Pos(), bad.End()-1) + start, end, err := safetoken.Offsets(tok, bad.Pos(), bad.End()) if err != nil { return false } - stmtBytes := src[start : end+1] + stmtBytes := src[start:end] stmt, err := parseStmt(tok, bad.Pos(), stmtBytes) if err != nil { return false From d81d6fcce1a24f2b8d0a9493f4d84b75c80176e4 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 28 Feb 2025 18:40:18 -0500 Subject: [PATCH 218/309] gopls/internal/util/asm: better assembly parsing This CL adds a rudimentary parser for symbols in Go .s files. It is a placeholder for a more principled implementation, but it is sufficient to make Definition support control labels (also in this CL) and for a cross-references index (future work). + test of Definition on control label + test of asm.Parse Updates golang/go#71754 Change-Id: I2ff19b4ade130c051197d6b097a1a3dbcd95555a Reviewed-on: https://go-review.googlesource.com/c/tools/+/654335 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- gopls/internal/goasm/definition.go | 59 +++-- gopls/internal/golang/assembly.go | 3 + .../test/marker/testdata/definition/asm.txt | 3 + gopls/internal/util/asm/parse.go | 245 ++++++++++++++++++ gopls/internal/util/asm/parse_test.go | 67 +++++ 5 files changed, 353 insertions(+), 24 deletions(-) create mode 100644 gopls/internal/util/asm/parse.go create mode 100644 gopls/internal/util/asm/parse_test.go diff --git a/gopls/internal/goasm/definition.go b/gopls/internal/goasm/definition.go index 4403e7cac7f..903916d265d 100644 --- a/gopls/internal/goasm/definition.go +++ b/gopls/internal/goasm/definition.go @@ -2,20 +2,20 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package goasm provides language-server features for files in Go +// assembly language (https://go.dev/doc/asm). package goasm import ( - "bytes" "context" "fmt" "go/token" - "strings" - "unicode" "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/cache/metadata" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/util/asm" "golang.org/x/tools/gopls/internal/util/morestrings" "golang.org/x/tools/internal/event" ) @@ -41,21 +41,27 @@ func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p return nil, err } + // Parse the assembly. + // + // TODO(adonovan): make this just another + // attribute of the type-checked cache.Package. + file := asm.Parse(content) + // Figure out the selected symbol. // For now, just find the identifier around the cursor. - // - // TODO(adonovan): use a real asm parser; see cmd/asm/internal/asm/parse.go. - // Ideally this would just be just another attribute of the - // type-checked cache.Package. - nonIdentRune := func(r rune) bool { return !isIdentRune(r) } - i := bytes.LastIndexFunc(content[:offset], nonIdentRune) - j := bytes.IndexFunc(content[offset:], nonIdentRune) - if j < 0 || j == 0 { - return nil, nil // identifier runs to EOF, or not an identifier + var found *asm.Ident + for _, id := range file.Idents { + if id.Offset <= offset && offset <= id.End() { + found = &id + break + } } - sym := string(content[i+1 : offset+j]) - sym = strings.ReplaceAll(sym, "·", ".") // (U+00B7 MIDDLE DOT) - sym = strings.ReplaceAll(sym, "∕", "/") // (U+2215 DIVISION SLASH) + if found == nil { + return nil, fmt.Errorf("not an identifier") + } + + // Resolve a symbol with a "." prefix to the current package. + sym := found.Name if sym != "" && sym[0] == '.' { sym = string(mp.PkgPath) + sym } @@ -92,18 +98,23 @@ func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p if err == nil { return []protocol.Location{loc}, nil } - } - // TODO(adonovan): support jump to var, block label, and other - // TEXT, DATA, and GLOBAL symbols in the same file. Needs asm parser. + } else { + // local symbols (funcs, vars, labels) + for _, id := range file.Idents { + if id.Name == found.Name && + (id.Kind == asm.Text || id.Kind == asm.Global || id.Kind == asm.Label) { - return nil, nil -} + loc, err := mapper.OffsetLocation(id.Offset, id.End()) + if err != nil { + return nil, err + } + return []protocol.Location{loc}, nil + } + } + } -// The assembler allows center dot (· U+00B7) and -// division slash (∕ U+2215) to work as identifier characters. -func isIdentRune(r rune) bool { - return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '·' || r == '∕' + return nil, nil } // TODO(rfindley): avoid the duplicate column mapping here, by associating a diff --git a/gopls/internal/golang/assembly.go b/gopls/internal/golang/assembly.go index 9e673dd9719..12244a58c59 100644 --- a/gopls/internal/golang/assembly.go +++ b/gopls/internal/golang/assembly.go @@ -10,6 +10,9 @@ package golang // - ./codeaction.go - computes the symbol and offers the CodeAction command. // - ../server/command.go - handles the command by opening a web page. // - ../server/server.go - handles the HTTP request and calls this function. +// +// For language-server behavior in Go assembly language files, +// see [golang.org/x/tools/gopls/internal/goasm]. import ( "bytes" diff --git a/gopls/internal/test/marker/testdata/definition/asm.txt b/gopls/internal/test/marker/testdata/definition/asm.txt index f0187d7e24a..250f237d299 100644 --- a/gopls/internal/test/marker/testdata/definition/asm.txt +++ b/gopls/internal/test/marker/testdata/definition/asm.txt @@ -26,6 +26,9 @@ var _ = ff // pacify unusedfunc analyzer TEXT ·ff(SB), $16 //@ loc(ffasm, "ff"), def("ff", ffgo) CALL example·com∕b·B //@ def("com", bB) JMP ·ff //@ def("ff", ffgo) + JMP label //@ def("label", label) +label: //@ loc(label,"label") + RET -- b/b.go -- package b diff --git a/gopls/internal/util/asm/parse.go b/gopls/internal/util/asm/parse.go new file mode 100644 index 00000000000..11c59a7cc3d --- /dev/null +++ b/gopls/internal/util/asm/parse.go @@ -0,0 +1,245 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package asm provides a simple parser for Go assembly files. +package asm + +import ( + "bufio" + "bytes" + "fmt" + "strings" + "unicode" +) + +// Kind describes the nature of an identifier in an assembly file. +type Kind uint8 + +const ( + Invalid Kind = iota // reserved zero value; not used by Ident + Ref // arbitrary reference to symbol or control label + Text // definition of TEXT (function) symbol + Global // definition of GLOBL (var) symbol + Data // initialization of GLOBL (var) symbol; effectively a reference + Label // definition of control label +) + +func (k Kind) String() string { + if int(k) < len(kindString) { + return kindString[k] + } + return fmt.Sprintf("Kind(%d)", k) +} + +var kindString = [...]string{ + Invalid: "invalid", + Ref: "ref", + Text: "text", + Global: "global", + Data: "data", + Label: "label", +} + +// A file represents a parsed file of Go assembly language. +type File struct { + Idents []Ident + + // TODO(adonovan): use token.File? This may be important in a + // future in which analyzers can report diagnostics in .s files. +} + +// Ident represents an identifier in an assembly file. +type Ident struct { + Name string // symbol name (after correcting [·∕]); Name[0]='.' => current package + Offset int // zero-based byte offset + Kind Kind +} + +// End returns the identifier's end offset. +func (id Ident) End() int { return id.Offset + len(id.Name) } + +// Parse extracts identifiers from Go assembly files. +// Since it is a best-effort parser, it never returns an error. +func Parse(content []byte) *File { + var idents []Ident + offset := 0 // byte offset of start of current line + + // TODO(adonovan) use a proper tokenizer that respects + // comments, string literals, line continuations, etc. + scan := bufio.NewScanner(bytes.NewReader(content)) + for ; scan.Scan(); offset += len(scan.Bytes()) + len("\n") { + line := scan.Text() + + // Strip comments. + if idx := strings.Index(line, "//"); idx >= 0 { + line = line[:idx] + } + + // Skip blank lines. + if strings.TrimSpace(line) == "" { + continue + } + + // Check for label definitions (ending with colon). + if colon := strings.IndexByte(line, ':'); colon > 0 { + label := strings.TrimSpace(line[:colon]) + if isIdent(label) { + idents = append(idents, Ident{ + Name: label, + Offset: offset + strings.Index(line, label), + Kind: Label, + }) + continue + } + } + + // Split line into words. + words := strings.Fields(line) + if len(words) == 0 { + continue + } + + // A line of the form + // TEXT ·sym(SB),NOSPLIT,$12 + // declares a text symbol "·sym". + if len(words) > 1 { + kind := Invalid + switch words[0] { + case "TEXT": + kind = Text + case "GLOBL": + kind = Global + case "DATA": + kind = Data + } + if kind != Invalid { + sym := words[1] + sym = cutBefore(sym, ",") // strip ",NOSPLIT,$12" etc + sym = cutBefore(sym, "(") // "sym(SB)" -> "sym" + sym = cutBefore(sym, "<") // "sym" -> "sym" + sym = strings.TrimSpace(sym) + if isIdent(sym) { + // (The Index call assumes sym is not itself "TEXT" etc.) + idents = append(idents, Ident{ + Name: cleanup(sym), + Kind: kind, + Offset: offset + strings.Index(line, sym), + }) + } + continue + } + } + + // Find references in the rest of the line. + pos := 0 + for _, word := range words { + // Find actual position of word within line. + tokenPos := strings.Index(line[pos:], word) + if tokenPos < 0 { + panic(line) + } + tokenPos += pos + pos = tokenPos + len(word) + + // Reject probable instruction mnemonics (e.g. MOV). + if len(word) >= 2 && word[0] != '·' && + !strings.ContainsFunc(word, unicode.IsLower) { + continue + } + + if word[0] == '$' { + word = word[1:] + tokenPos++ + + // Reject probable immediate values (e.g. "$123"). + if !strings.ContainsFunc(word, isNonDigit) { + continue + } + } + + // Reject probably registers (e.g. "PC"). + if len(word) <= 3 && !strings.ContainsFunc(word, unicode.IsLower) { + continue + } + + // Probable identifier reference. + // + // TODO(adonovan): handle FP symbols correctly; + // sym+8(FP) is essentially a comment about + // stack slot 8, not a reference to a symbol + // with a declaration somewhere; so they form + // an equivalence class without a canonical + // declaration. + // + // TODO(adonovan): handle pseudoregisters and field + // references such as: + // MOVD $runtime·g0(SB), g // pseudoreg + // MOVD R0, g_stackguard0(g) // field ref + + sym := cutBefore(word, "(") // "·sym(SB)" => "sym" + sym = cutBefore(sym, "+") // "sym+8(FP)" => "sym" + sym = cutBefore(sym, "<") // "sym" =>> "sym" + if isIdent(sym) { + idents = append(idents, Ident{ + Name: cleanup(sym), + Kind: Ref, + Offset: offset + tokenPos, + }) + } + } + } + + _ = scan.Err() // ignore scan errors + + return &File{Idents: idents} +} + +// isIdent reports whether s is a valid Go assembly identifier. +func isIdent(s string) bool { + for i, r := range s { + if !isIdentRune(r, i) { + return false + } + } + return len(s) > 0 +} + +// cutBefore returns the portion of s before the first occurrence of sep, if any. +func cutBefore(s, sep string) string { + if before, _, ok := strings.Cut(s, sep); ok { + return before + } + return s +} + +// cleanup converts a symbol name from assembler syntax to linker syntax. +func cleanup(sym string) string { + return repl.Replace(sym) +} + +var repl = strings.NewReplacer( + "·", ".", // (U+00B7 MIDDLE DOT) + "∕", "/", // (U+2215 DIVISION SLASH) +) + +func isNonDigit(r rune) bool { return !unicode.IsDigit(r) } + +// -- plundered from GOROOT/src/cmd/asm/internal/asm/parse.go -- + +// We want center dot (·) and division slash (∕) to work as identifier characters. +func isIdentRune(ch rune, i int) bool { + if unicode.IsLetter(ch) { + return true + } + switch ch { + case '_': // Underscore; traditional. + return true + case '\u00B7': // Represents the period in runtime.exit. U+00B7 '·' middle dot + return true + case '\u2215': // Represents the slash in runtime/debug.setGCPercent. U+2215 '∕' division slash + return true + } + // Digits are OK only after the first character. + return i > 0 && unicode.IsDigit(ch) +} diff --git a/gopls/internal/util/asm/parse_test.go b/gopls/internal/util/asm/parse_test.go new file mode 100644 index 00000000000..67a1286d28b --- /dev/null +++ b/gopls/internal/util/asm/parse_test.go @@ -0,0 +1,67 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package asm_test + +import ( + "bytes" + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/util/asm" +) + +// TestIdents checks that (likely) identifiers are extracted in the expected places. +func TestIdents(t *testing.T) { + src := []byte(` +// This is a nonsense file containing a variety of syntax. + +#include "foo.h" +#ifdef MACRO +DATA hello<>+0x00(SB)/64, $"Hello" +GLOBL hello<(SB), RODATA, $64 +#endif + +TEXT mypkg·f(SB),NOSPLIT,$0 + MOVD R1, 16(RSP) // another comment + MOVD $otherpkg·data(SB), R2 + JMP label +label: + BL ·g(SB) + +TEXT ·g(SB),NOSPLIT,$0 + MOVD $runtime·g0(SB), g + MOVD R0, g_stackguard0(g) + MOVD R0, (g_stack+stack_lo)(g) +`[1:]) + const filename = "asm.s" + m := protocol.NewMapper(protocol.URIFromPath(filename), src) + file := asm.Parse(src) + + want := ` +asm.s:5:6-11: data "hello" +asm.s:6:7-12: global "hello" +asm.s:9:6-13: text "mypkg.f" +asm.s:11:8-21: ref "otherpkg.data" +asm.s:12:6-11: ref "label" +asm.s:13:1-6: label "label" +asm.s:14:5-7: ref ".g" +asm.s:16:6-8: text ".g" +asm.s:17:8-18: ref "runtime.g0" +asm.s:17:25-26: ref "g" +asm.s:18:11-24: ref "g_stackguard0" +`[1:] + var buf bytes.Buffer + for _, id := range file.Idents { + line, col := m.OffsetLineCol8(id.Offset) + _, endCol := m.OffsetLineCol8(id.Offset + len(id.Name)) + fmt.Fprintf(&buf, "%s:%d:%d-%d:\t%s %q\n", filename, line, col, endCol, id.Kind, id.Name) + } + got := buf.String() + if got != want { + t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, want, cmp.Diff(want, got)) + } +} From 8d38122b0b1a9991f490aa06b7bfca7b4140bdad Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 3 Mar 2025 22:13:31 +0000 Subject: [PATCH 219/309] gopls/internal/cache: reproduce and fix crash on if cond overflow Through reverse engineering, I was able to reproduce the overflow of golang/go#72026, and verify the fix of CL 653596. Along the way, I incidentally reproduced golang/go#66766, which I think we can safely ignore now that we understand it. Updates golang/go#72026 Fixes golang/go#66766 Change-Id: I2131d771c13688c1ad47f6bc6285e524fb4c04a1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/654336 Reviewed-by: Alan Donovan Auto-Submit: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/check.go | 15 +++++++-------- gopls/internal/cache/parsego/parse.go | 1 + .../integration/completion/fixedbugs_test.go | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index aa1537c8705..27d5cfa240b 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -2005,15 +2005,14 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, inputs *typeCheckInputs, errs [ posn := safetoken.StartPosition(e.Fset, start) if !posn.IsValid() { // All valid positions produced by the type checker should described by - // its fileset. + // its fileset, yet since type checker errors are associated with + // positions in the AST, and AST nodes can overflow the file + // (golang/go#48300), we can't rely on this. // - // Note: in golang/go#64488, we observed an error that was positioned - // over fixed syntax, which overflowed its file. So it's definitely - // possible that we get here (it's hard to reason about fixing up the - // AST). Nevertheless, it's a bug. - if pkg.hasFixedFiles() { - bug.Reportf("internal error: type checker error %q outside its Fset (fixed files)", e) - } else { + // We should fix the parser, but in the meantime type errors are not + // significant if there are parse errors, so we can safely ignore this + // case. + if len(pkg.parseErrors) == 0 { bug.Reportf("internal error: type checker error %q outside its Fset", e) } continue diff --git a/gopls/internal/cache/parsego/parse.go b/gopls/internal/cache/parsego/parse.go index fd598e235d1..4b37816caff 100644 --- a/gopls/internal/cache/parsego/parse.go +++ b/gopls/internal/cache/parsego/parse.go @@ -532,6 +532,7 @@ func fixInitStmt(bad *ast.BadExpr, parent ast.Node, tok *token.File, src []byte) if err != nil { return false } + assert(end <= len(src), "offset overflow") // golang/go#72026 stmtBytes := src[start:end] stmt, err := parseStmt(tok, bad.Pos(), stmtBytes) if err != nil { diff --git a/gopls/internal/test/integration/completion/fixedbugs_test.go b/gopls/internal/test/integration/completion/fixedbugs_test.go index faa5324e138..ccec432904e 100644 --- a/gopls/internal/test/integration/completion/fixedbugs_test.go +++ b/gopls/internal/test/integration/completion/fixedbugs_test.go @@ -38,3 +38,20 @@ package } }) } + +func TestFixInitStatementCrash_Issue72026(t *testing.T) { + // This test checks that we don't crash when the if condition overflows the + // file (as is possible with a malformed struct type). + + const files = ` +-- go.mod -- +module example.com + +go 1.18 +` + + Run(t, files, func(t *testing.T, env *Env) { + env.CreateBuffer("p.go", "package p\nfunc _() {\n\tfor i := struct") + env.AfterChange() + }) +} From 07219402b2fc707689574d91ee3cfd2c9a544a87 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Tue, 4 Mar 2025 19:05:13 +0800 Subject: [PATCH 220/309] gopls/internal/analysis/modernize: strings.Fields -> FieldsSeq This CL enhances the existing modernizer to support calls to strings.Fields and bytes.Fields, that offers a fix to instead use go1.24's FieldsSeq, which avoids allocating an array. Fixes golang/go#72033 Change-Id: I2059f66f38a639c5a264b650137ced7b4f84550e Reviewed-on: https://go-review.googlesource.com/c/tools/+/654535 Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao --- gopls/doc/analyzers.md | 2 +- gopls/internal/analysis/modernize/doc.go | 2 +- .../internal/analysis/modernize/modernize.go | 2 +- .../analysis/modernize/modernize_test.go | 1 + .../modernize/{splitseq.go => stringsseq.go} | 31 +++++++++----- .../testdata/src/fieldsseq/fieldsseq.go | 42 +++++++++++++++++++ .../src/fieldsseq/fieldsseq.go.golden | 42 +++++++++++++++++++ .../testdata/src/fieldsseq/fieldsseq_go123.go | 1 + gopls/internal/doc/api.json | 4 +- 9 files changed, 111 insertions(+), 16 deletions(-) rename gopls/internal/analysis/modernize/{splitseq.go => stringsseq.go} (77%) create mode 100644 gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq_go123.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index dde95591718..aa95e024089 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -498,7 +498,7 @@ existing code by using more modern features of Go, such as: - replacing a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22; - replacing Split in "for range strings.Split(...)" by go1.24's - more efficient SplitSeq; + more efficient SplitSeq, or Fields with FieldSeq; To apply all modernization fixes en masse, you can use the following command: diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 3759fdb10c5..b12abab7063 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -31,7 +31,7 @@ // - replacing a 3-clause for i := 0; i < n; i++ {} loop by // for i := range n {}, added in go1.22; // - replacing Split in "for range strings.Split(...)" by go1.24's -// more efficient SplitSeq; +// more efficient SplitSeq, or Fields with FieldSeq; // // To apply all modernization fixes en masse, you can use the // following command: diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 354836d6b40..96e8b325df4 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -72,7 +72,7 @@ func run(pass *analysis.Pass) (any, error) { rangeint(pass) slicescontains(pass) slicesdelete(pass) - splitseq(pass) + stringsseq(pass) sortslice(pass) testingContext(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 6662914b28d..7bdc8014389 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -24,6 +24,7 @@ func Test(t *testing.T) { "slicescontains", "slicesdelete", "splitseq", + "fieldsseq", "sortslice", "testingcontext", ) diff --git a/gopls/internal/analysis/modernize/splitseq.go b/gopls/internal/analysis/modernize/stringsseq.go similarity index 77% rename from gopls/internal/analysis/modernize/splitseq.go rename to gopls/internal/analysis/modernize/stringsseq.go index 1f3da859e9b..ca9d918912e 100644 --- a/gopls/internal/analysis/modernize/splitseq.go +++ b/gopls/internal/analysis/modernize/stringsseq.go @@ -5,6 +5,7 @@ package modernize import ( + "fmt" "go/ast" "go/token" "go/types" @@ -17,8 +18,9 @@ import ( "golang.org/x/tools/internal/astutil/edge" ) -// splitseq offers a fix to replace a call to strings.Split with -// SplitSeq when it is the operand of a range loop, either directly: +// stringsseq offers a fix to replace a call to strings.Split with +// SplitSeq or strings.Fields with FieldsSeq +// when it is the operand of a range loop, either directly: // // for _, line := range strings.Split() {...} // @@ -29,7 +31,8 @@ import ( // // Variants: // - bytes.SplitSeq -func splitseq(pass *analysis.Pass) { +// - bytes.FieldsSeq +func stringsseq(pass *analysis.Pass) { if !analysisinternal.Imports(pass.Pkg, "strings") && !analysisinternal.Imports(pass.Pkg, "bytes") { return @@ -88,21 +91,27 @@ func splitseq(pass *analysis.Pass) { }) } - if sel, ok := call.Fun.(*ast.SelectorExpr); ok && - (analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "strings", "Split") || - analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "bytes", "Split")) { + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + continue + } + + obj := typeutil.Callee(info, call) + if analysisinternal.IsFunctionNamed(obj, "strings", "Split", "Fields") || + analysisinternal.IsFunctionNamed(obj, "bytes", "Split", "Fields") { + oldFnName := obj.Name() + seqFnName := fmt.Sprintf("%sSeq", oldFnName) pass.Report(analysis.Diagnostic{ Pos: sel.Pos(), End: sel.End(), - Category: "splitseq", - Message: "Ranging over SplitSeq is more efficient", + Category: "stringsseq", + Message: fmt.Sprintf("Ranging over %s is more efficient", seqFnName), SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Replace Split with SplitSeq", + Message: fmt.Sprintf("Replace %s with %s", oldFnName, seqFnName), TextEdits: append(edits, analysis.TextEdit{ - // Split -> SplitSeq Pos: sel.Sel.Pos(), End: sel.Sel.End(), - NewText: []byte("SplitSeq")}), + NewText: []byte(seqFnName)}), }}, }) } diff --git a/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go new file mode 100644 index 00000000000..b86df1a8a94 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go @@ -0,0 +1,42 @@ +//go:build go1.24 + +package fieldsseq + +import ( + "bytes" + "strings" +) + +func _() { + for _, line := range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" + println(line) + } + for i, line := range strings.Fields("") { // nope: uses index var + println(i, line) + } + for i, _ := range strings.Fields("") { // nope: uses index var + println(i) + } + for i := range strings.Fields("") { // nope: uses index var + println(i) + } + for _ = range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" + } + for range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" + } + for range bytes.Fields(nil) { // want "Ranging over FieldsSeq is more efficient" + } + { + lines := strings.Fields("") // want "Ranging over FieldsSeq is more efficient" + for _, line := range lines { + println(line) + } + } + { + lines := strings.Fields("") // nope: lines is used not just by range + for _, line := range lines { + println(line) + } + println(lines) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go.golden b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go.golden new file mode 100644 index 00000000000..9fa1bfd1b62 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go.golden @@ -0,0 +1,42 @@ +//go:build go1.24 + +package fieldsseq + +import ( + "bytes" + "strings" +) + +func _() { + for line := range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" + println(line) + } + for i, line := range strings.Fields( "") { // nope: uses index var + println(i, line) + } + for i, _ := range strings.Fields( "") { // nope: uses index var + println(i) + } + for i := range strings.Fields( "") { // nope: uses index var + println(i) + } + for range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" + } + for range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" + } + for range bytes.FieldsSeq(nil) { // want "Ranging over FieldsSeq is more efficient" + } + { + lines := strings.FieldsSeq("") // want "Ranging over FieldsSeq is more efficient" + for line := range lines { + println(line) + } + } + { + lines := strings.Fields( "") // nope: lines is used not just by range + for _, line := range lines { + println(line) + } + println(lines) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq_go123.go b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq_go123.go new file mode 100644 index 00000000000..c2bd314db75 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq_go123.go @@ -0,0 +1 @@ +package fieldsseq diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 5775d0d4361..4001e3605bb 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -514,7 +514,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", "Default": "true" }, { @@ -1228,7 +1228,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From 340f21a49b9cad20d07a2b58e483a991084dc481 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 5 Mar 2025 12:14:02 +0800 Subject: [PATCH 221/309] gopls: move gopls/doc/generate package This CL tracks adonovan's TODO by moving generate package from gopls/doc/generate to gopls/internal/doc/generate. Change-Id: I08fc90859cc6afe10ab5ac658a7b8a514d36cc32 Reviewed-on: https://go-review.googlesource.com/c/tools/+/654536 Reviewed-by: Alan Donovan Reviewed-by: Junyang Shao Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/doc/api.go | 2 +- gopls/{ => internal}/doc/generate/generate.go | 4 +--- gopls/{ => internal}/doc/generate/generate_test.go | 0 3 files changed, 2 insertions(+), 4 deletions(-) rename gopls/{ => internal}/doc/generate/generate.go (99%) rename gopls/{ => internal}/doc/generate/generate_test.go (100%) diff --git a/gopls/internal/doc/api.go b/gopls/internal/doc/api.go index 258f90d49ae..5011d2172ed 100644 --- a/gopls/internal/doc/api.go +++ b/gopls/internal/doc/api.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:generate go run ../../doc/generate +//go:generate go run ./generate // The doc package provides JSON metadata that documents gopls' public // interfaces. diff --git a/gopls/doc/generate/generate.go b/gopls/internal/doc/generate/generate.go similarity index 99% rename from gopls/doc/generate/generate.go rename to gopls/internal/doc/generate/generate.go index b0d3e8c49f6..51c8b89e39b 100644 --- a/gopls/doc/generate/generate.go +++ b/gopls/internal/doc/generate/generate.go @@ -11,9 +11,7 @@ // // Run it with this command: // -// $ cd gopls/internal/doc && go generate -// -// TODO(adonovan): move this package to gopls/internal/doc/generate. +// $ cd gopls/internal/doc/generate && go generate package main import ( diff --git a/gopls/doc/generate/generate_test.go b/gopls/internal/doc/generate/generate_test.go similarity index 100% rename from gopls/doc/generate/generate_test.go rename to gopls/internal/doc/generate/generate_test.go From ece9e9ba0760eb361376c8a890b24e89db031d9e Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Tue, 25 Feb 2025 13:48:15 -0500 Subject: [PATCH 222/309] gopls/doc/generate: add status in codelenses and inlayhints Features configurable through map[K]V can not be marked as experimental. To comply with deprecation guideline, this CL introduces a per key and per value status where gopls can mark a specific key or a specific value as experimental. The status can be indicated by the comment directives as part of the doc comment. The status can be delcared following pattern "//gopls:status X" very similar to struct tag. This clarifies the question: if "codelenses" is a released feature, are all enum keys configurable in "codelenses" are also released feature? VSCode-Go CL 652357 Change-Id: I4ddc5155751452d5f7b92bbb3610aa61680a29a4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652356 Auto-Submit: Hongxiang Jiang Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/codelenses.md | 4 + gopls/internal/analysis/gofix/directive.go | 95 ------ gopls/internal/analysis/gofix/gofix.go | 3 +- gopls/internal/doc/api.go | 8 +- gopls/internal/doc/api.json | 351 ++++++++++++++------- gopls/internal/doc/generate/generate.go | 55 +++- gopls/internal/settings/settings.go | 4 + internal/astutil/comment.go | 85 +++++ 8 files changed, 375 insertions(+), 230 deletions(-) delete mode 100644 gopls/internal/analysis/gofix/directive.go diff --git a/gopls/doc/codelenses.md b/gopls/doc/codelenses.md index d8aa8e1f479..fa7c6c68859 100644 --- a/gopls/doc/codelenses.md +++ b/gopls/doc/codelenses.md @@ -75,6 +75,8 @@ File type: Go ## `run_govulncheck`: Run govulncheck (legacy) +**This setting is experimental and may be deleted.** + This codelens source annotates the `module` directive in a go.mod file with a command to run Govulncheck asynchronously. @@ -134,6 +136,8 @@ File type: go.mod ## `vulncheck`: Run govulncheck +**This setting is experimental and may be deleted.** + This codelens source annotates the `module` directive in a go.mod file with a command to run govulncheck synchronously. diff --git a/gopls/internal/analysis/gofix/directive.go b/gopls/internal/analysis/gofix/directive.go deleted file mode 100644 index 20c45313cfb..00000000000 --- a/gopls/internal/analysis/gofix/directive.go +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2024 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package gofix - -import ( - "go/ast" - "go/token" - "strings" -) - -// -- plundered from the future (CL 605517, issue #68021) -- - -// TODO(adonovan): replace with ast.Directive after go1.25 (#68021). -// Beware of our local mods to handle analysistest -// "want" comments on the same line. - -// A directive is a comment line with special meaning to the Go -// toolchain or another tool. It has the form: -// -// //tool:name args -// -// The "tool:" portion is missing for the three directives named -// line, extern, and export. -// -// See https://go.dev/doc/comment#Syntax for details of Go comment -// syntax and https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives -// for details of directives used by the Go compiler. -type directive struct { - Pos token.Pos // of preceding "//" - Tool string - Name string - Args string // may contain internal spaces -} - -// directives returns the directives within the comment. -func directives(g *ast.CommentGroup) (res []*directive) { - if g != nil { - // Avoid (*ast.CommentGroup).Text() as it swallows directives. - for _, c := range g.List { - if len(c.Text) > 2 && - c.Text[1] == '/' && - c.Text[2] != ' ' && - isDirective(c.Text[2:]) { - - tool, nameargs, ok := strings.Cut(c.Text[2:], ":") - if !ok { - // Must be one of {line,extern,export}. - tool, nameargs = "", tool - } - name, args, _ := strings.Cut(nameargs, " ") // tab?? - // Permit an additional line comment after the args, chiefly to support - // [golang.org/x/tools/go/analysis/analysistest]. - args, _, _ = strings.Cut(args, "//") - res = append(res, &directive{ - Pos: c.Slash, - Tool: tool, - Name: name, - Args: strings.TrimSpace(args), - }) - } - } - } - return -} - -// isDirective reports whether c is a comment directive. -// This code is also in go/printer. -func isDirective(c string) bool { - // "//line " is a line directive. - // "//extern " is for gccgo. - // "//export " is for cgo. - // (The // has been removed.) - if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") { - return true - } - - // "//[a-z0-9]+:[a-z0-9]" - // (The // has been removed.) - colon := strings.Index(c, ":") - if colon <= 0 || colon+1 >= len(c) { - return false - } - for i := 0; i <= colon+1; i++ { - if i == colon { - continue - } - b := c[i] - if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') { - return false - } - } - return true -} diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 41cebcb63b9..a2380f1d644 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -20,6 +20,7 @@ import ( "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" "golang.org/x/tools/internal/diff" @@ -598,7 +599,7 @@ func currentFile(c cursor.Cursor) *ast.File { // hasFixInline reports the presence of a "//go:fix inline" directive // in the comments. func hasFixInline(cg *ast.CommentGroup) bool { - for _, d := range directives(cg) { + for _, d := range internalastutil.Directives(cg) { if d.Tool == "go" && d.Name == "fix" && d.Args == "inline" { return true } diff --git a/gopls/internal/doc/api.go b/gopls/internal/doc/api.go index 5011d2172ed..52101dda8c9 100644 --- a/gopls/internal/doc/api.go +++ b/gopls/internal/doc/api.go @@ -47,11 +47,13 @@ type EnumKey struct { Name string // in JSON syntax (quoted) Doc string Default string + Status string // = "" | "advanced" | "experimental" | "deprecated" } type EnumValue struct { - Value string // in JSON syntax (quoted) - Doc string // doc comment; always starts with `Value` + Value string // in JSON syntax (quoted) + Doc string // doc comment; always starts with `Value` + Status string // = "" | "advanced" | "experimental" | "deprecated" } type Lens struct { @@ -60,6 +62,7 @@ type Lens struct { Title string Doc string Default bool + Status string // = "" | "advanced" | "experimental" | "deprecated" } type Analyzer struct { @@ -73,4 +76,5 @@ type Hint struct { Name string Doc string Default bool + Status string // = "" | "advanced" | "experimental" | "deprecated" } diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 4001e3605bb..b9e0e78e950 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -124,23 +124,28 @@ "EnumValues": [ { "Value": "\"FullDocumentation\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"NoDocumentation\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"SingleLine\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"Structured\"", - "Doc": "`\"Structured\"` is a misguided experimental setting that returns a JSON\nhover format. This setting should not be used, as it will be removed in a\nfuture release of gopls.\n" + "Doc": "`\"Structured\"` is a misguided experimental setting that returns a JSON\nhover format. This setting should not be used, as it will be removed in a\nfuture release of gopls.\n", + "Status": "" }, { "Value": "\"SynopsisDocumentation\"", - "Doc": "" + "Doc": "", + "Status": "" } ], "Default": "\"FullDocumentation\"", @@ -173,15 +178,18 @@ "EnumValues": [ { "Value": "false", - "Doc": "false: do not show links" + "Doc": "false: do not show links", + "Status": "" }, { "Value": "true", - "Doc": "true: show links to the `linkTarget` domain" + "Doc": "true: show links to the `linkTarget` domain", + "Status": "" }, { "Value": "\"gopls\"", - "Doc": "`\"gopls\"`: show links to gopls' internal documentation viewer" + "Doc": "`\"gopls\"`: show links to gopls' internal documentation viewer", + "Status": "" } ], "Default": "true", @@ -228,15 +236,18 @@ "EnumValues": [ { "Value": "\"CaseInsensitive\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"CaseSensitive\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"Fuzzy\"", - "Doc": "" + "Doc": "", + "Status": "" } ], "Default": "\"Fuzzy\"", @@ -283,15 +294,18 @@ "EnumValues": [ { "Value": "\"Both\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"Definition\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"Link\"", - "Doc": "" + "Doc": "", + "Status": "" } ], "Default": "\"Both\"", @@ -310,19 +324,23 @@ "EnumValues": [ { "Value": "\"CaseInsensitive\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"CaseSensitive\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"FastFuzzy\"", - "Doc": "" + "Doc": "", + "Status": "" }, { "Value": "\"Fuzzy\"", - "Doc": "" + "Doc": "", + "Status": "" } ], "Default": "\"FastFuzzy\"", @@ -341,15 +359,18 @@ "EnumValues": [ { "Value": "\"Dynamic\"", - "Doc": "`\"Dynamic\"` uses whichever qualifier results in the highest scoring\nmatch for the given symbol query. Here a \"qualifier\" is any \"/\" or \".\"\ndelimited suffix of the fully qualified symbol. i.e. \"to/pkg.Foo.Field\" or\njust \"Foo.Field\".\n" + "Doc": "`\"Dynamic\"` uses whichever qualifier results in the highest scoring\nmatch for the given symbol query. Here a \"qualifier\" is any \"/\" or \".\"\ndelimited suffix of the fully qualified symbol. i.e. \"to/pkg.Foo.Field\" or\njust \"Foo.Field\".\n", + "Status": "" }, { "Value": "\"Full\"", - "Doc": "`\"Full\"` is fully qualified symbols, i.e.\n\"path/to/pkg.Foo.Field\".\n" + "Doc": "`\"Full\"` is fully qualified symbols, i.e.\n\"path/to/pkg.Foo.Field\".\n", + "Status": "" }, { "Value": "\"Package\"", - "Doc": "`\"Package\"` is package qualified symbols i.e.\n\"pkg.Foo.Field\".\n" + "Doc": "`\"Package\"` is package qualified symbols i.e.\n\"pkg.Foo.Field\".\n", + "Status": "" } ], "Default": "\"Dynamic\"", @@ -368,11 +389,13 @@ "EnumValues": [ { "Value": "\"all\"", - "Doc": "`\"all\"` matches symbols in any loaded package, including\ndependencies.\n" + "Doc": "`\"all\"` matches symbols in any loaded package, including\ndependencies.\n", + "Status": "" }, { "Value": "\"workspace\"", - "Doc": "`\"workspace\"` matches symbols in workspace packages only.\n" + "Doc": "`\"workspace\"` matches symbols in workspace packages only.\n", + "Status": "" } ], "Default": "\"all\"", @@ -390,282 +413,338 @@ { "Name": "\"appends\"", "Doc": "check for missing values after append\n\nThis checker reports calls to append that pass\nno values to be appended to the slice.\n\n\ts := []string{\"a\", \"b\", \"c\"}\n\t_ = append(s)\n\nSuch calls are always no-ops and often indicate an\nunderlying mistake.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"asmdecl\"", "Doc": "report mismatches between assembly files and Go declarations", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"assign\"", "Doc": "check for useless assignments\n\nThis checker reports assignments of the form x = x or a[i] = a[i].\nThese are almost always useless, and even when they aren't they are\nusually a mistake.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"atomic\"", "Doc": "check for common mistakes using the sync/atomic package\n\nThe atomic checker looks for assignment statements of the form:\n\n\tx = atomic.AddUint64(\u0026x, 1)\n\nwhich are not atomic.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"atomicalign\"", "Doc": "check for non-64-bits-aligned arguments to sync/atomic functions", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"bools\"", "Doc": "check for common mistakes involving boolean operators", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"buildtag\"", "Doc": "check //go:build and // +build directives", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"cgocall\"", "Doc": "detect some violations of the cgo pointer passing rules\n\nCheck for invalid cgo pointer passing.\nThis looks for code that uses cgo to call C code passing values\nwhose types are almost always invalid according to the cgo pointer\nsharing rules.\nSpecifically, it warns about attempts to pass a Go chan, map, func,\nor slice to C, either directly, or via a pointer, array, or struct.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"composites\"", "Doc": "check for unkeyed composite literals\n\nThis analyzer reports a diagnostic for composite literals of struct\ntypes imported from another package that do not use the field-keyed\nsyntax. Such literals are fragile because the addition of a new field\n(even if unexported) to the struct will cause compilation to fail.\n\nAs an example,\n\n\terr = \u0026net.DNSConfigError{err}\n\nshould be replaced by:\n\n\terr = \u0026net.DNSConfigError{Err: err}\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"copylocks\"", "Doc": "check for locks erroneously passed by value\n\nInadvertently copying a value containing a lock, such as sync.Mutex or\nsync.WaitGroup, may cause both copies to malfunction. Generally such\nvalues should be referred to through a pointer.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"deepequalerrors\"", "Doc": "check for calls of reflect.DeepEqual on error values\n\nThe deepequalerrors checker looks for calls of the form:\n\n reflect.DeepEqual(err1, err2)\n\nwhere err1 and err2 are errors. Using reflect.DeepEqual to compare\nerrors is discouraged.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"defers\"", "Doc": "report common mistakes in defer statements\n\nThe defers analyzer reports a diagnostic when a defer statement would\nresult in a non-deferred call to time.Since, as experience has shown\nthat this is nearly always a mistake.\n\nFor example:\n\n\tstart := time.Now()\n\t...\n\tdefer recordLatency(time.Since(start)) // error: call to time.Since is not deferred\n\nThe correct code is:\n\n\tdefer func() { recordLatency(time.Since(start)) }()", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"deprecated\"", "Doc": "check for use of deprecated identifiers\n\nThe deprecated analyzer looks for deprecated symbols and package\nimports.\n\nSee https://go.dev/wiki/Deprecated to learn about Go's convention\nfor documenting and signaling deprecated identifiers.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"directive\"", "Doc": "check Go toolchain directives such as //go:debug\n\nThis analyzer checks for problems with known Go toolchain directives\nin all Go source files in a package directory, even those excluded by\n//go:build constraints, and all non-Go source files too.\n\nFor //go:debug (see https://go.dev/doc/godebug), the analyzer checks\nthat the directives are placed only in Go source files, only above the\npackage comment, and only in package main or *_test.go files.\n\nSupport for other known directives may be added in the future.\n\nThis analyzer does not check //go:build, which is handled by the\nbuildtag analyzer.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"embed\"", "Doc": "check //go:embed directive usage\n\nThis analyzer checks that the embed package is imported if //go:embed\ndirectives are present, providing a suggested fix to add the import if\nit is missing.\n\nThis analyzer also checks that //go:embed directives precede the\ndeclaration of a single variable.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"errorsas\"", "Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analysis reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"fillreturns\"", "Doc": "suggest fixes for errors due to an incorrect number of return values\n\nThis checker provides suggested fixes for type errors of the\ntype \"wrong number of return values (want %d, got %d)\". For example:\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn\n\t}\n\nwill turn into\n\n\tfunc m() (int, string, *bool, error) {\n\t\treturn 0, \"\", nil, nil\n\t}\n\nThis functionality is similar to https://github.com/sqs/goreturns.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"framepointer\"", "Doc": "report assembly that clobbers the frame pointer before saving it", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"gofix\"", "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions and constants that are marked for inlining.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"hostport\"", "Doc": "check format of addresses passed to net.Dial\n\nThis analyzer flags code that produce network address strings using\nfmt.Sprintf, as in this example:\n\n addr := fmt.Sprintf(\"%s:%d\", host, 12345) // \"will not work with IPv6\"\n ...\n conn, err := net.Dial(\"tcp\", addr) // \"when passed to dial here\"\n\nThe analyzer suggests a fix to use the correct approach, a call to\nnet.JoinHostPort:\n\n addr := net.JoinHostPort(host, \"12345\")\n ...\n conn, err := net.Dial(\"tcp\", addr)\n\nA similar diagnostic and fix are produced for a format string of \"%s:%s\".\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"httpresponse\"", "Doc": "check for mistakes using HTTP responses\n\nA common mistake when using the net/http package is to defer a function\ncall to close the http.Response Body before checking the error that\ndetermines whether the response is valid:\n\n\tresp, err := http.Head(url)\n\tdefer resp.Body.Close()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// (defer statement belongs here)\n\nThis checker helps uncover latent nil dereference bugs by reporting a\ndiagnostic for such mistakes.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"ifaceassert\"", "Doc": "detect impossible interface-to-interface type assertions\n\nThis checker flags type assertions v.(T) and corresponding type-switch cases\nin which the static type V of v is an interface that cannot possibly implement\nthe target interface T. This occurs when V and T contain methods with the same\nname but different signatures. Example:\n\n\tvar v interface {\n\t\tRead()\n\t}\n\t_ = v.(io.Reader)\n\nThe Read method in v has a different signature than the Read method in\nio.Reader, so this assertion cannot succeed.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"infertypeargs\"", "Doc": "check for unnecessary type arguments in call expressions\n\nExplicit type arguments may be omitted from call expressions if they can be\ninferred from function arguments, or from other type arguments:\n\n\tfunc f[T any](T) {}\n\t\n\tfunc _() {\n\t\tf[string](\"foo\") // string could be inferred\n\t}\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"loopclosure\"", "Doc": "check references to loop variables from within nested functions\n\nThis analyzer reports places where a function literal references the\niteration variable of an enclosing loop, and the loop calls the function\nin such a way (e.g. with go or defer) that it may outlive the loop\niteration and possibly observe the wrong value of the variable.\n\nNote: An iteration variable can only outlive a loop iteration in Go versions \u003c=1.21.\nIn Go 1.22 and later, the loop variable lifetimes changed to create a new\niteration variable per loop iteration. (See go.dev/issue/60078.)\n\nIn this example, all the deferred functions run after the loop has\ncompleted, so all observe the final value of v [\u003cgo1.22].\n\n\tfor _, v := range list {\n\t defer func() {\n\t use(v) // incorrect\n\t }()\n\t}\n\nOne fix is to create a new variable for each iteration of the loop:\n\n\tfor _, v := range list {\n\t v := v // new var per iteration\n\t defer func() {\n\t use(v) // ok\n\t }()\n\t}\n\nAfter Go version 1.22, the previous two for loops are equivalent\nand both are correct.\n\nThe next example uses a go statement and has a similar problem [\u003cgo1.22].\nIn addition, it has a data race because the loop updates v\nconcurrent with the goroutines accessing it.\n\n\tfor _, v := range elem {\n\t go func() {\n\t use(v) // incorrect, and a data race\n\t }()\n\t}\n\nA fix is the same as before. The checker also reports problems\nin goroutines started by golang.org/x/sync/errgroup.Group.\nA hard-to-spot variant of this form is common in parallel tests:\n\n\tfunc Test(t *testing.T) {\n\t for _, test := range tests {\n\t t.Run(test.name, func(t *testing.T) {\n\t t.Parallel()\n\t use(test) // incorrect, and a data race\n\t })\n\t }\n\t}\n\nThe t.Parallel() call causes the rest of the function to execute\nconcurrent with the loop [\u003cgo1.22].\n\nThe analyzer reports references only in the last statement,\nas it is not deep enough to understand the effects of subsequent\nstatements that might render the reference benign.\n(\"Last statement\" is defined recursively in compound\nstatements such as if, switch, and select.)\n\nSee: https://golang.org/doc/go_faq.html#closures_and_goroutines", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"lostcancel\"", "Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nWithDeadline and variants such as WithCancelCause must be called,\nor the new context will remain live until its parent context is cancelled.\n(The background context is never cancelled.)", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"modernize\"", "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"nilfunc\"", "Doc": "check for useless comparisons between functions and nil\n\nA useless comparison is one like f == nil as opposed to f() == nil.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"nilness\"", "Doc": "check for redundant or impossible nil comparisons\n\nThe nilness checker inspects the control-flow graph of each function in\na package and reports nil pointer dereferences, degenerate nil\npointers, and panics with nil values. A degenerate comparison is of the form\nx==nil or x!=nil where x is statically known to be nil or non-nil. These are\noften a mistake, especially in control flow related to errors. Panics with nil\nvalues are checked because they are not detectable by\n\n\tif r := recover(); r != nil {\n\nThis check reports conditions such as:\n\n\tif f == nil { // impossible condition (f is a function)\n\t}\n\nand:\n\n\tp := \u0026v\n\t...\n\tif p != nil { // tautological condition\n\t}\n\nand:\n\n\tif p == nil {\n\t\tprint(*p) // nil dereference\n\t}\n\nand:\n\n\tif p == nil {\n\t\tpanic(p)\n\t}\n\nSometimes the control flow may be quite complex, making bugs hard\nto spot. In the example below, the err.Error expression is\nguaranteed to panic because, after the first return, err must be\nnil. The intervening loop is just a distraction.\n\n\t...\n\terr := g.Wait()\n\tif err != nil {\n\t\treturn err\n\t}\n\tpartialSuccess := false\n\tfor _, err := range errs {\n\t\tif err == nil {\n\t\t\tpartialSuccess = true\n\t\t\tbreak\n\t\t}\n\t}\n\tif partialSuccess {\n\t\treportStatus(StatusMessage{\n\t\t\tCode: code.ERROR,\n\t\t\tDetail: err.Error(), // \"nil dereference in dynamic method call\"\n\t\t})\n\t\treturn nil\n\t}\n\n...", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"nonewvars\"", "Doc": "suggested fixes for \"no new vars on left side of :=\"\n\nThis checker provides suggested fixes for type errors of the\ntype \"no new vars on left side of :=\". For example:\n\n\tz := 1\n\tz := 2\n\nwill turn into\n\n\tz := 1\n\tz = 2", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"noresultvalues\"", "Doc": "suggested fixes for unexpected return values\n\nThis checker provides suggested fixes for type errors of the\ntype \"no result values expected\" or \"too many return values\".\nFor example:\n\n\tfunc z() { return nil }\n\nwill turn into\n\n\tfunc z() { return }", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"printf\"", "Doc": "check consistency of Printf format strings and arguments\n\nThe check applies to calls of the formatting functions such as\n[fmt.Printf] and [fmt.Sprintf], as well as any detected wrappers of\nthose functions such as [log.Printf]. It reports a variety of\nmistakes such as syntax errors in the format string and mismatches\n(of number and type) between the verbs and their arguments.\n\nSee the documentation of the fmt package for the complete set of\nformat operators and their operand types.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"shadow\"", "Doc": "check for possible unintended shadowing of variables\n\nThis analyzer check for shadowed variables.\nA shadowed variable is a variable declared in an inner scope\nwith the same name and type as a variable in an outer scope,\nand where the outer variable is mentioned after the inner one\nis declared.\n\n(This definition can be refined; the module generates too many\nfalse positives and is not yet enabled by default.)\n\nFor example:\n\n\tfunc BadRead(f *os.File, buf []byte) error {\n\t\tvar err error\n\t\tfor {\n\t\t\tn, err := f.Read(buf) // shadows the function variable 'err'\n\t\t\tif err != nil {\n\t\t\t\tbreak // causes return of wrong value\n\t\t\t}\n\t\t\tfoo(buf)\n\t\t}\n\t\treturn err\n\t}", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"shift\"", "Doc": "check for shifts that equal or exceed the width of the integer", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"sigchanyzer\"", "Doc": "check for unbuffered channel of os.Signal\n\nThis checker reports call expression of the form\n\n\tsignal.Notify(c \u003c-chan os.Signal, sig ...os.Signal),\n\nwhere c is an unbuffered channel, which can be at risk of missing the signal.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"simplifycompositelit\"", "Doc": "check for composite literal simplifications\n\nAn array, slice, or map composite literal of the form:\n\n\t[]T{T{}, T{}}\n\nwill be simplified to:\n\n\t[]T{{}, {}}\n\nThis is one of the simplifications that \"gofmt -s\" applies.\n\nThis analyzer ignores generated code.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"simplifyrange\"", "Doc": "check for range statement simplifications\n\nA range of the form:\n\n\tfor x, _ = range v {...}\n\nwill be simplified to:\n\n\tfor x = range v {...}\n\nA range of the form:\n\n\tfor _ = range v {...}\n\nwill be simplified to:\n\n\tfor range v {...}\n\nThis is one of the simplifications that \"gofmt -s\" applies.\n\nThis analyzer ignores generated code.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"simplifyslice\"", "Doc": "check for slice simplifications\n\nA slice expression of the form:\n\n\ts[a:len(s)]\n\nwill be simplified to:\n\n\ts[a:]\n\nThis is one of the simplifications that \"gofmt -s\" applies.\n\nThis analyzer ignores generated code.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"slog\"", "Doc": "check for invalid structured logging calls\n\nThe slog checker looks for calls to functions from the log/slog\npackage that take alternating key-value pairs. It reports calls\nwhere an argument in a key position is neither a string nor a\nslog.Attr, and where a final key is missing its value.\nFor example,it would report\n\n\tslog.Warn(\"message\", 11, \"k\") // slog.Warn arg \"11\" should be a string or a slog.Attr\n\nand\n\n\tslog.Info(\"message\", \"k1\", v1, \"k2\") // call to slog.Info missing a final value", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"sortslice\"", "Doc": "check the argument type of sort.Slice\n\nsort.Slice requires an argument of a slice type. Check that\nthe interface{} value passed to sort.Slice is actually a slice.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"stdmethods\"", "Doc": "check signature of methods of well-known interfaces\n\nSometimes a type may be intended to satisfy an interface but may fail to\ndo so because of a mistake in its method signature.\nFor example, the result of this WriteTo method should be (int64, error),\nnot error, to satisfy io.WriterTo:\n\n\ttype myWriterTo struct{...}\n\tfunc (myWriterTo) WriteTo(w io.Writer) error { ... }\n\nThis check ensures that each method whose name matches one of several\nwell-known interface methods from the standard library has the correct\nsignature for that interface.\n\nChecked method names include:\n\n\tFormat GobEncode GobDecode MarshalJSON MarshalXML\n\tPeek ReadByte ReadFrom ReadRune Scan Seek\n\tUnmarshalJSON UnreadByte UnreadRune WriteByte\n\tWriteTo", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"stdversion\"", "Doc": "report uses of too-new standard library symbols\n\nThe stdversion analyzer reports references to symbols in the standard\nlibrary that were introduced by a Go release higher than the one in\nforce in the referring file. (Recall that the file's Go version is\ndefined by the 'go' directive its module's go.mod file, or by a\n\"//go:build go1.X\" build tag at the top of the file.)\n\nThe analyzer does not report a diagnostic for a reference to a \"too\nnew\" field or method of a type that is itself \"too new\", as this may\nhave false positives, for example if fields or methods are accessed\nthrough a type alias that is guarded by a Go version constraint.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"stringintconv\"", "Doc": "check for string(int) conversions\n\nThis checker flags conversions of the form string(x) where x is an integer\n(but not byte or rune) type. Such conversions are discouraged because they\nreturn the UTF-8 representation of the Unicode code point x, and not a decimal\nstring representation of x as one might expect. Furthermore, if x denotes an\ninvalid code point, the conversion cannot be statically rejected.\n\nFor conversions that intend on using the code point, consider replacing them\nwith string(rune(x)). Otherwise, strconv.Itoa and its equivalents return the\nstring representation of the value in the desired base.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"structtag\"", "Doc": "check that struct field tags conform to reflect.StructTag.Get\n\nAlso report certain struct tags (json, xml) used with unexported fields.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"testinggoroutine\"", "Doc": "report calls to (*testing.T).Fatal from goroutines started by a test\n\nFunctions that abruptly terminate a test, such as the Fatal, Fatalf, FailNow, and\nSkip{,f,Now} methods of *testing.T, must be called from the test goroutine itself.\nThis checker detects calls to these functions that occur within a goroutine\nstarted by the test. For example:\n\n\tfunc TestFoo(t *testing.T) {\n\t go func() {\n\t t.Fatal(\"oops\") // error: (*T).Fatal called from non-test goroutine\n\t }()\n\t}", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"tests\"", "Doc": "check for common mistaken usages of tests and examples\n\nThe tests checker walks Test, Benchmark, Fuzzing and Example functions checking\nmalformed names, wrong signatures and examples documenting non-existent\nidentifiers.\n\nPlease see the documentation for package testing in golang.org/pkg/testing\nfor the conventions that are enforced for Tests, Benchmarks, and Examples.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"timeformat\"", "Doc": "check for calls of (time.Time).Format or time.Parse with 2006-02-01\n\nThe timeformat checker looks for time formats with the 2006-02-01 (yyyy-dd-mm)\nformat. Internationally, \"yyyy-dd-mm\" does not occur in common calendar date\nstandards, and so it is more likely that 2006-01-02 (yyyy-mm-dd) was intended.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unmarshal\"", "Doc": "report passing non-pointer or non-interface values to unmarshal\n\nThe unmarshal analysis reports calls to functions such as json.Unmarshal\nin which the argument type is not a pointer or an interface.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unreachable\"", "Doc": "check for unreachable code\n\nThe unreachable analyzer finds statements that execution can never reach\nbecause they are preceded by a return statement, a call to panic, an\ninfinite loop, or similar constructs.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unsafeptr\"", "Doc": "check for invalid conversions of uintptr to unsafe.Pointer\n\nThe unsafeptr analyzer reports likely incorrect uses of unsafe.Pointer\nto convert integers to pointers. A conversion from uintptr to\nunsafe.Pointer is invalid if it implies that there is a uintptr-typed\nword in memory that holds a pointer value, because that word will be\ninvisible to stack copying and to the garbage collector.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unusedfunc\"", "Doc": "check for unused functions and methods\n\nThe unusedfunc analyzer reports functions and methods that are\nnever referenced outside of their own declaration.\n\nA function is considered unused if it is unexported and not\nreferenced (except within its own declaration).\n\nA method is considered unused if it is unexported, not referenced\n(except within its own declaration), and its name does not match\nthat of any method of an interface type declared within the same\npackage.\n\nThe tool may report false positives in some situations, for\nexample:\n\n - For a declaration of an unexported function that is referenced\n from another package using the go:linkname mechanism, if the\n declaration's doc comment does not also have a go:linkname\n comment.\n\n (Such code is in any case strongly discouraged: linkname\n annotations, if they must be used at all, should be used on both\n the declaration and the alias.)\n\n - For compiler intrinsics in the \"runtime\" package that, though\n never referenced, are known to the compiler and are called\n indirectly by compiled object code.\n\n - For functions called only from assembly.\n\n - For functions called only from files whose build tags are not\n selected in the current build configuration.\n\nSee https://github.com/golang/go/issues/71686 for discussion of\nthese limitations.\n\nThe unusedfunc algorithm is not as precise as the\ngolang.org/x/tools/cmd/deadcode tool, but it has the advantage that\nit runs within the modular analysis framework, enabling near\nreal-time feedback within gopls.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unusedparams\"", "Doc": "check for unused parameters of functions\n\nThe unusedparams analyzer checks functions to see if there are\nany parameters that are not being used.\n\nTo ensure soundness, it ignores:\n - \"address-taken\" functions, that is, functions that are used as\n a value rather than being called directly; their signatures may\n be required to conform to a func type.\n - exported functions or methods, since they may be address-taken\n in another package.\n - unexported methods whose name matches an interface method\n declared in the same package, since the method's signature\n may be required to conform to the interface type.\n - functions with empty bodies, or containing just a call to panic.\n - parameters that are unnamed, or named \"_\", the blank identifier.\n\nThe analyzer suggests a fix of replacing the parameter name by \"_\",\nbut in such cases a deeper fix can be obtained by invoking the\n\"Refactor: remove unused parameter\" code action, which will\neliminate the parameter entirely, along with all corresponding\narguments at call sites, while taking care to preserve any side\neffects in the argument expressions; see\nhttps://github.com/golang/tools/releases/tag/gopls%2Fv0.14.\n\nThis analyzer ignores generated code.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unusedresult\"", "Doc": "check for unused results of calls to some functions\n\nSome functions like fmt.Errorf return a result and have no side\neffects, so it is always a mistake to discard the result. Other\nfunctions may return an error that must not be ignored, or a cleanup\noperation that must be called. This analyzer reports calls to\nfunctions like these when the result of the call is ignored.\n\nThe set of functions may be controlled using flags.", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unusedvariable\"", "Doc": "check for unused variables and suggest fixes", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"unusedwrite\"", "Doc": "checks for unused writes\n\nThe analyzer reports instances of writes to struct fields and\narrays that are never read. Specifically, when a struct object\nor an array is copied, its elements are copied implicitly by\nthe compiler, and any element write to this copy does nothing\nwith the original object.\n\nFor example:\n\n\ttype T struct { x int }\n\n\tfunc f(input []T) {\n\t\tfor i, v := range input { // v is a copy\n\t\t\tv.x = i // unused write to field x\n\t\t}\n\t}\n\nAnother example is about non-pointer receiver:\n\n\ttype T struct { x int }\n\n\tfunc (t T) f() { // t is a copy\n\t\tt.x = i // unused write to field x\n\t}", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"waitgroup\"", "Doc": "check for misuses of sync.WaitGroup\n\nThis analyzer detects mistaken calls to the (*sync.WaitGroup).Add\nmethod from inside a new goroutine, causing Add to race with Wait:\n\n\t// WRONG\n\tvar wg sync.WaitGroup\n\tgo func() {\n\t wg.Add(1) // \"WaitGroup.Add called from inside new goroutine\"\n\t defer wg.Done()\n\t ...\n\t}()\n\twg.Wait() // (may return prematurely before new goroutine starts)\n\nThe correct code calls Add before starting the goroutine:\n\n\t// RIGHT\n\tvar wg sync.WaitGroup\n\twg.Add(1)\n\tgo func() {\n\t\tdefer wg.Done()\n\t\t...\n\t}()\n\twg.Wait()", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"yield\"", "Doc": "report calls to yield where the result is ignored\n\nAfter a yield function returns false, the caller should not call\nthe yield function again; generally the iterator should return\npromptly.\n\nThis example fails to check the result of the call to yield,\ncausing this analyzer to report a diagnostic:\n\n\tyield(1) // yield may be called again (on L2) after returning false\n\tyield(2)\n\nThe corrected code is either this:\n\n\tif yield(1) { yield(2) }\n\nor simply:\n\n\t_ = yield(1) \u0026\u0026 yield(2)\n\nIt is not always a mistake to ignore the result of yield.\nFor example, this is a valid single-element iterator:\n\n\tyield(1) // ok to ignore result\n\treturn\n\nIt is only a mistake when the yield call that returned false may be\nfollowed by another call.", - "Default": "true" + "Default": "true", + "Status": "" } ] }, @@ -699,22 +778,26 @@ { "Name": "\"bounds\"", "Doc": "`\"bounds\"` controls bounds checking diagnostics.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"escape\"", "Doc": "`\"escape\"` controls diagnostics about escape choices.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"inline\"", "Doc": "`\"inline\"` controls diagnostics about inlining choices.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"nil\"", "Doc": "`\"nil\"` controls nil checks.\n", - "Default": "true" + "Default": "true", + "Status": "" } ] }, @@ -735,11 +818,13 @@ "EnumValues": [ { "Value": "\"Imports\"", - "Doc": "`\"Imports\"`: In Imports mode, `gopls` will report vulnerabilities that affect packages\ndirectly and indirectly used by the analyzed main module.\n" + "Doc": "`\"Imports\"`: In Imports mode, `gopls` will report vulnerabilities that affect packages\ndirectly and indirectly used by the analyzed main module.\n", + "Status": "" }, { "Value": "\"Off\"", - "Doc": "`\"Off\"`: Disable vulnerability analysis.\n" + "Doc": "`\"Off\"`: Disable vulnerability analysis.\n", + "Status": "" } ], "Default": "\"Off\"", @@ -772,11 +857,13 @@ "EnumValues": [ { "Value": "\"Edit\"", - "Doc": "`\"Edit\"`: Trigger diagnostics on file edit and save. (default)\n" + "Doc": "`\"Edit\"`: Trigger diagnostics on file edit and save. (default)\n", + "Status": "" }, { "Value": "\"Save\"", - "Doc": "`\"Save\"`: Trigger diagnostics only on file save. Events like initial workspace load\nor configuration change will still trigger diagnostics.\n" + "Doc": "`\"Save\"`: Trigger diagnostics only on file save. Events like initial workspace load\nor configuration change will still trigger diagnostics.\n", + "Status": "" } ], "Default": "\"Edit\"", @@ -808,37 +895,44 @@ { "Name": "\"assignVariableTypes\"", "Doc": "`\"assignVariableTypes\"` controls inlay hints for variable types in assign statements:\n```go\n\ti/* int*/, j/* int*/ := 0, len(r)-1\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"compositeLiteralFields\"", "Doc": "`\"compositeLiteralFields\"` inlay hints for composite literal field names:\n```go\n\t{/*in: */\"Hello, world\", /*want: */\"dlrow ,olleH\"}\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"compositeLiteralTypes\"", "Doc": "`\"compositeLiteralTypes\"` controls inlay hints for composite literal types:\n```go\n\tfor _, c := range []struct {\n\t\tin, want string\n\t}{\n\t\t/*struct{ in string; want string }*/{\"Hello, world\", \"dlrow ,olleH\"},\n\t}\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"constantValues\"", "Doc": "`\"constantValues\"` controls inlay hints for constant values:\n```go\n\tconst (\n\t\tKindNone Kind = iota/* = 0*/\n\t\tKindPrint/* = 1*/\n\t\tKindPrintf/* = 2*/\n\t\tKindErrorf/* = 3*/\n\t)\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"functionTypeParameters\"", "Doc": "`\"functionTypeParameters\"` inlay hints for implicit type parameters on generic functions:\n```go\n\tmyFoo/*[int, string]*/(1, \"hello\")\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"parameterNames\"", "Doc": "`\"parameterNames\"` controls inlay hints for parameter names:\n```go\n\tparseInt(/* str: */ \"123\", /* radix: */ 8)\n```\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"rangeVariableTypes\"", "Doc": "`\"rangeVariableTypes\"` controls inlay hints for variable types in range statements:\n```go\n\tfor k/* int*/, v/* string*/ := range []string{} {\n\t\tfmt.Println(k, v)\n\t}\n```\n", - "Default": "false" + "Default": "false", + "Status": "" } ] }, @@ -858,42 +952,50 @@ { "Name": "\"generate\"", "Doc": "`\"generate\"`: Run `go generate`\n\nThis codelens source annotates any `//go:generate` comments\nwith commands to run `go generate` in this directory, on\nall directories recursively beneath this one.\n\nSee [Generating code](https://go.dev/blog/generate) for\nmore details.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"regenerate_cgo\"", "Doc": "`\"regenerate_cgo\"`: Re-generate cgo declarations\n\nThis codelens source annotates an `import \"C\"` declaration\nwith a command to re-run the [cgo\ncommand](https://pkg.go.dev/cmd/cgo) to regenerate the\ncorresponding Go declarations.\n\nUse this after editing the C code in comments attached to\nthe import, or in C header files included by it.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"run_govulncheck\"", "Doc": "`\"run_govulncheck\"`: Run govulncheck (legacy)\n\nThis codelens source annotates the `module` directive in a go.mod file\nwith a command to run Govulncheck asynchronously.\n\n[Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that\ncomputes the set of functions reachable within your application, including\ndependencies; queries a database of known security vulnerabilities; and\nreports any potential problems it finds.\n", - "Default": "false" + "Default": "false", + "Status": "experimental" }, { "Name": "\"test\"", "Doc": "`\"test\"`: Run tests and benchmarks\n\nThis codelens source annotates each `Test` and `Benchmark`\nfunction in a `*_test.go` file with a command to run it.\n\nThis source is off by default because VS Code has\na client-side custom UI for testing, and because progress\nnotifications are not a great UX for streamed test output.\nSee:\n- golang/go#67400 for a discussion of this feature.\n- https://github.com/joaotavora/eglot/discussions/1402\n for an alternative approach.\n", - "Default": "false" + "Default": "false", + "Status": "" }, { "Name": "\"tidy\"", "Doc": "`\"tidy\"`: Tidy go.mod file\n\nThis codelens source annotates the `module` directive in a\ngo.mod file with a command to run [`go mod\ntidy`](https://go.dev/ref/mod#go-mod-tidy), which ensures\nthat the go.mod file matches the source code in the module.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"upgrade_dependency\"", "Doc": "`\"upgrade_dependency\"`: Update dependencies\n\nThis codelens source annotates the `module` directive in a\ngo.mod file with commands to:\n\n- check for available upgrades,\n- upgrade direct dependencies, and\n- upgrade all dependencies transitively.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"vendor\"", "Doc": "`\"vendor\"`: Update vendor directory\n\nThis codelens source annotates the `module` directive in a\ngo.mod file with a command to run [`go mod\nvendor`](https://go.dev/ref/mod#go-mod-vendor), which\ncreates or updates the directory named `vendor` in the\nmodule root so that it contains an up-to-date copy of all\nnecessary package dependencies.\n", - "Default": "true" + "Default": "true", + "Status": "" }, { "Name": "\"vulncheck\"", "Doc": "`\"vulncheck\"`: Run govulncheck\n\nThis codelens source annotates the `module` directive in a go.mod file\nwith a command to run govulncheck synchronously.\n\n[Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that\ncomputes the set of functions reachable within your application, including\ndependencies; queries a database of known security vulnerabilities; and\nreports any potential problems it finds.\n", - "Default": "false" + "Default": "false", + "Status": "experimental" } ] }, @@ -1023,56 +1125,64 @@ "Lens": "generate", "Title": "Run `go generate`", "Doc": "\nThis codelens source annotates any `//go:generate` comments\nwith commands to run `go generate` in this directory, on\nall directories recursively beneath this one.\n\nSee [Generating code](https://go.dev/blog/generate) for\nmore details.\n", - "Default": true + "Default": true, + "Status": "" }, { "FileType": "Go", "Lens": "regenerate_cgo", "Title": "Re-generate cgo declarations", "Doc": "\nThis codelens source annotates an `import \"C\"` declaration\nwith a command to re-run the [cgo\ncommand](https://pkg.go.dev/cmd/cgo) to regenerate the\ncorresponding Go declarations.\n\nUse this after editing the C code in comments attached to\nthe import, or in C header files included by it.\n", - "Default": true + "Default": true, + "Status": "" }, { "FileType": "Go", "Lens": "test", "Title": "Run tests and benchmarks", "Doc": "\nThis codelens source annotates each `Test` and `Benchmark`\nfunction in a `*_test.go` file with a command to run it.\n\nThis source is off by default because VS Code has\na client-side custom UI for testing, and because progress\nnotifications are not a great UX for streamed test output.\nSee:\n- golang/go#67400 for a discussion of this feature.\n- https://github.com/joaotavora/eglot/discussions/1402\n for an alternative approach.\n", - "Default": false + "Default": false, + "Status": "" }, { "FileType": "go.mod", "Lens": "run_govulncheck", "Title": "Run govulncheck (legacy)", "Doc": "\nThis codelens source annotates the `module` directive in a go.mod file\nwith a command to run Govulncheck asynchronously.\n\n[Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that\ncomputes the set of functions reachable within your application, including\ndependencies; queries a database of known security vulnerabilities; and\nreports any potential problems it finds.\n", - "Default": false + "Default": false, + "Status": "experimental" }, { "FileType": "go.mod", "Lens": "tidy", "Title": "Tidy go.mod file", "Doc": "\nThis codelens source annotates the `module` directive in a\ngo.mod file with a command to run [`go mod\ntidy`](https://go.dev/ref/mod#go-mod-tidy), which ensures\nthat the go.mod file matches the source code in the module.\n", - "Default": true + "Default": true, + "Status": "" }, { "FileType": "go.mod", "Lens": "upgrade_dependency", "Title": "Update dependencies", "Doc": "\nThis codelens source annotates the `module` directive in a\ngo.mod file with commands to:\n\n- check for available upgrades,\n- upgrade direct dependencies, and\n- upgrade all dependencies transitively.\n", - "Default": true + "Default": true, + "Status": "" }, { "FileType": "go.mod", "Lens": "vendor", "Title": "Update vendor directory", "Doc": "\nThis codelens source annotates the `module` directive in a\ngo.mod file with a command to run [`go mod\nvendor`](https://go.dev/ref/mod#go-mod-vendor), which\ncreates or updates the directory named `vendor` in the\nmodule root so that it contains an up-to-date copy of all\nnecessary package dependencies.\n", - "Default": true + "Default": true, + "Status": "" }, { "FileType": "go.mod", "Lens": "vulncheck", "Title": "Run govulncheck", "Doc": "\nThis codelens source annotates the `module` directive in a go.mod file\nwith a command to run govulncheck synchronously.\n\n[Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that\ncomputes the set of functions reachable within your application, including\ndependencies; queries a database of known security vulnerabilities; and\nreports any potential problems it finds.\n", - "Default": false + "Default": false, + "Status": "experimental" } ], "Analyzers": [ @@ -1417,37 +1527,44 @@ { "Name": "assignVariableTypes", "Doc": "`\"assignVariableTypes\"` controls inlay hints for variable types in assign statements:\n```go\n\ti/* int*/, j/* int*/ := 0, len(r)-1\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "compositeLiteralFields", "Doc": "`\"compositeLiteralFields\"` inlay hints for composite literal field names:\n```go\n\t{/*in: */\"Hello, world\", /*want: */\"dlrow ,olleH\"}\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "compositeLiteralTypes", "Doc": "`\"compositeLiteralTypes\"` controls inlay hints for composite literal types:\n```go\n\tfor _, c := range []struct {\n\t\tin, want string\n\t}{\n\t\t/*struct{ in string; want string }*/{\"Hello, world\", \"dlrow ,olleH\"},\n\t}\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "constantValues", "Doc": "`\"constantValues\"` controls inlay hints for constant values:\n```go\n\tconst (\n\t\tKindNone Kind = iota/* = 0*/\n\t\tKindPrint/* = 1*/\n\t\tKindPrintf/* = 2*/\n\t\tKindErrorf/* = 3*/\n\t)\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "functionTypeParameters", "Doc": "`\"functionTypeParameters\"` inlay hints for implicit type parameters on generic functions:\n```go\n\tmyFoo/*[int, string]*/(1, \"hello\")\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "parameterNames", "Doc": "`\"parameterNames\"` controls inlay hints for parameter names:\n```go\n\tparseInt(/* str: */ \"123\", /* radix: */ 8)\n```\n", - "Default": false + "Default": false, + "Status": "" }, { "Name": "rangeVariableTypes", "Doc": "`\"rangeVariableTypes\"` controls inlay hints for variable types in range statements:\n```go\n\tfor k/* int*/, v/* string*/ := range []string{} {\n\t\tfmt.Println(k, v)\n\t}\n```\n", - "Default": false + "Default": false, + "Status": "" } ] } \ No newline at end of file diff --git a/gopls/internal/doc/generate/generate.go b/gopls/internal/doc/generate/generate.go index 51c8b89e39b..762fceeb4b9 100644 --- a/gopls/internal/doc/generate/generate.go +++ b/gopls/internal/doc/generate/generate.go @@ -317,9 +317,17 @@ func loadEnums(pkg *packages.Package) (map[types.Type][]doc.EnumValue, error) { spec := path[1].(*ast.ValueSpec) value := cnst.Val().ExactString() docstring := valueDoc(cnst.Name(), value, spec.Doc.Text()) + var status string + for _, d := range internalastutil.Directives(spec.Doc) { + if d.Tool == "gopls" && d.Name == "status" { + status = d.Args + break + } + } v := doc.EnumValue{ - Value: value, - Doc: docstring, + Value: value, + Doc: docstring, + Status: status, } enums[obj.Type()] = append(enums[obj.Type()], v) } @@ -354,6 +362,7 @@ func collectEnumKeys(m *types.Map, reflectField reflect.Value, enumValues []doc. keys = append(keys, doc.EnumKey{ Name: v.Value, Doc: v.Doc, + Status: v.Status, Default: def, }) } @@ -436,6 +445,7 @@ func loadLenses(settingsPkg *packages.Package, defaults map[settings.CodeLensSou // Find the CodeLensSource enums among the files of the protocol package. // Map each enum value to its doc comment. enumDoc := make(map[string]string) + enumStatus := make(map[string]string) for _, f := range settingsPkg.Syntax { for _, decl := range f.Decls { if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.CONST { @@ -455,6 +465,12 @@ func loadLenses(settingsPkg *packages.Package, defaults map[settings.CodeLensSou return nil, fmt.Errorf("%s: %s lacks doc comment", posn, spec.Names[0].Name) } enumDoc[value] = spec.Doc.Text() + for _, d := range internalastutil.Directives(spec.Doc) { + if d.Tool == "gopls" && d.Name == "status" { + enumStatus[value] = d.Args + break + } + } } } } @@ -479,6 +495,7 @@ func loadLenses(settingsPkg *packages.Package, defaults map[settings.CodeLensSou Title: title, Doc: docText, Default: defaults[source], + Status: enumStatus[string(source)], }) } return nil @@ -518,8 +535,9 @@ func loadHints(settingsPkg *packages.Package) ([]*doc.Hint, error) { for _, enumVal := range enums[inlayHint] { name, _ := strconv.Unquote(enumVal.Value) hints = append(hints, &doc.Hint{ - Name: name, - Doc: enumVal.Doc, + Name: name, + Doc: enumVal.Doc, + Status: enumVal.Status, }) } return hints, nil @@ -600,17 +618,7 @@ func rewriteSettings(prevContent []byte, api *doc.API) ([]byte, error) { fmt.Fprintf(&buf, "### `%s %s`\n\n", opt.Name, opt.Type) // status - switch opt.Status { - case "": - case "advanced": - fmt.Fprint(&buf, "**This is an advanced setting and should not be configured by most `gopls` users.**\n\n") - case "debug": - fmt.Fprint(&buf, "**This setting is for debugging purposes only.**\n\n") - case "experimental": - fmt.Fprint(&buf, "**This setting is experimental and may be deleted.**\n\n") - default: - fmt.Fprintf(&buf, "**Status: %s.**\n\n", opt.Status) - } + writeStatus(&buf, opt.Status) // doc comment buf.WriteString(opt.Doc) @@ -651,6 +659,22 @@ func rewriteSettings(prevContent []byte, api *doc.API) ([]byte, error) { return content, nil } +// writeStatus emits a Markdown paragraph to buf about the status of a feature, +// if nonempty. +func writeStatus(buf *bytes.Buffer, status string) { + switch status { + case "": + case "advanced": + fmt.Fprint(buf, "**This is an advanced setting and should not be configured by most `gopls` users.**\n\n") + case "debug": + fmt.Fprint(buf, "**This setting is for debugging purposes only.**\n\n") + case "experimental": + fmt.Fprint(buf, "**This setting is experimental and may be deleted.**\n\n") + default: + fmt.Fprintf(buf, "**Status: %s.**\n\n", status) + } +} + var parBreakRE = regexp.MustCompile("\n{2,}") func shouldShowEnumKeysInSettings(name string) bool { @@ -722,6 +746,7 @@ func rewriteCodeLenses(prevContent []byte, api *doc.API) ([]byte, error) { var buf bytes.Buffer for _, lens := range api.Lenses { fmt.Fprintf(&buf, "## `%s`: %s\n\n", lens.Lens, lens.Title) + writeStatus(&buf, lens.Status) fmt.Fprintf(&buf, "%s\n\n", lens.Doc) fmt.Fprintf(&buf, "Default: %v\n\n", onOff(lens.Default)) fmt.Fprintf(&buf, "File type: %s\n\n", lens.FileType) diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index e98bc365935..59b2aa1b87f 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -269,6 +269,8 @@ const ( // computes the set of functions reachable within your application, including // dependencies; queries a database of known security vulnerabilities; and // reports any potential problems it finds. + // + //gopls:status experimental CodeLensVulncheck CodeLensSource = "vulncheck" // Run govulncheck (legacy) @@ -280,6 +282,8 @@ const ( // computes the set of functions reachable within your application, including // dependencies; queries a database of known security vulnerabilities; and // reports any potential problems it finds. + // + //gopls:status experimental CodeLensRunGovulncheck CodeLensSource = "run_govulncheck" // Run tests and benchmarks diff --git a/internal/astutil/comment.go b/internal/astutil/comment.go index 192d6430de0..ee4be23f226 100644 --- a/internal/astutil/comment.go +++ b/internal/astutil/comment.go @@ -6,6 +6,7 @@ package astutil import ( "go/ast" + "go/token" "strings" ) @@ -26,3 +27,87 @@ func Deprecation(doc *ast.CommentGroup) string { } return "" } + +// -- plundered from the future (CL 605517, issue #68021) -- + +// TODO(adonovan): replace with ast.Directive after go1.25 (#68021). +// Beware of our local mods to handle analysistest +// "want" comments on the same line. + +// A directive is a comment line with special meaning to the Go +// toolchain or another tool. It has the form: +// +// //tool:name args +// +// The "tool:" portion is missing for the three directives named +// line, extern, and export. +// +// See https://go.dev/doc/comment#Syntax for details of Go comment +// syntax and https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives +// for details of directives used by the Go compiler. +type Directive struct { + Pos token.Pos // of preceding "//" + Tool string + Name string + Args string // may contain internal spaces +} + +// isDirective reports whether c is a comment directive. +// This code is also in go/printer. +func isDirective(c string) bool { + // "//line " is a line directive. + // "//extern " is for gccgo. + // "//export " is for cgo. + // (The // has been removed.) + if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") { + return true + } + + // "//[a-z0-9]+:[a-z0-9]" + // (The // has been removed.) + colon := strings.Index(c, ":") + if colon <= 0 || colon+1 >= len(c) { + return false + } + for i := 0; i <= colon+1; i++ { + if i == colon { + continue + } + b := c[i] + if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') { + return false + } + } + return true +} + +// Directives returns the directives within the comment. +func Directives(g *ast.CommentGroup) (res []*Directive) { + if g != nil { + // Avoid (*ast.CommentGroup).Text() as it swallows directives. + for _, c := range g.List { + if len(c.Text) > 2 && + c.Text[1] == '/' && + c.Text[2] != ' ' && + isDirective(c.Text[2:]) { + + tool, nameargs, ok := strings.Cut(c.Text[2:], ":") + if !ok { + // Must be one of {line,extern,export}. + tool, nameargs = "", tool + } + name, args, _ := strings.Cut(nameargs, " ") // tab?? + // Permit an additional line comment after the args, chiefly to support + // [golang.org/x/tools/go/analysis/analysistest]. + args, _, _ = strings.Cut(args, "//") + res = append(res, &Directive{ + Pos: c.Slash, + Tool: tool, + Name: name, + Args: strings.TrimSpace(args), + }) + } + } + } + return +} From db6008cb90f09485deb11255e5dd6da114b4ecef Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 5 Mar 2025 13:18:07 -0500 Subject: [PATCH 223/309] go/types/internal/play: show Cursor.Stack of selected node Change-Id: Iaf6a6369e05ded0b10b85f468d8fbf91269373e4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655135 Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/types/internal/play/play.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/go/types/internal/play/play.go b/go/types/internal/play/play.go index f1318ac247a..4212a6b82cf 100644 --- a/go/types/internal/play/play.go +++ b/go/types/internal/play/play.go @@ -30,8 +30,10 @@ import ( "strings" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/packages" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typeparams" ) @@ -161,6 +163,15 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) { innermostExpr = e } } + // Show the cursor stack too. + // It's usually the same, but may differ in edge + // cases (e.g. around FuncType.Func). + inspect := inspector.New([]*ast.File{file}) + if cur, ok := cursor.Root(inspect).FindPos(startPos, endPos); ok { + fmt.Fprintf(out, "Cursor.FindPos().Stack() = %v\n", cur.Stack(nil)) + } else { + fmt.Fprintf(out, "Cursor.FindPos() failed\n") + } fmt.Fprintf(out, "\n") // Expression type information From 25a90befcdf96d15f13dd947b7395c8531dc67de Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 3 Mar 2025 23:06:21 -0500 Subject: [PATCH 224/309] gopls/internal/golang: Implementations for func types This CL adds support to the Implementations query for function types. The query relates two sets of locations: 1. the "func" token of each function declaration (FuncDecl or FuncLit). These are analogous to declarations of concrete methods. 2. uses of abstract functions: (a) the "func" token of each FuncType that is not part of Func{Decl,Lit}. These are analogous to interface{...} types. (b) the "(" paren of each dynamic call on a value of an abstract function type. These are analogous to references to interface method names, but no names are involved, which has historically made them hard to search for. An Implementations query on a location in set 1 returns set 2, and vice versa. Only the local algorithm is implemented for now; the global one (using an index analogous to methodsets) will follow. This CL supersedes CL 448035 and CL 619515, both of which attempt to unify the treatment of functions and interfaces in the methodsets algorithm and in the index; but the two problems are not precisely analogous, and I think we'll end up with more but simpler code if we implement themn separately. + tests, docs, relnotes Updates golang/go#56572 Change-Id: I18e1a7cc2f6c320112b9f3589323d04f9a52ef3c Reviewed-on: https://go-review.googlesource.com/c/tools/+/654556 Commit-Queue: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/doc/features/navigation.md | 22 +- gopls/doc/release/v0.19.0.md | 27 ++ gopls/internal/golang/implementation.go | 291 ++++++++++++++++-- gopls/internal/test/marker/doc.go | 7 +- gopls/internal/test/marker/marker_test.go | 10 +- .../testdata/implementation/signature.txt | 79 +++++ 6 files changed, 403 insertions(+), 33 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/implementation/signature.txt diff --git a/gopls/doc/features/navigation.md b/gopls/doc/features/navigation.md index f46f2935683..f3454f7188c 100644 --- a/gopls/doc/features/navigation.md +++ b/gopls/doc/features/navigation.md @@ -85,7 +85,10 @@ Client support: The LSP [`textDocument/implementation`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_implementation) -request queries the "implements" relation between interfaces and concrete types: +request queries the relation between abstract and concrete types and +their methods. + +Interfaces and concrete types are matched using method sets: - When invoked on a reference to an **interface type**, it returns the location of the declaration of each type that implements @@ -111,6 +114,17 @@ types with methods due to embedding) may be missing from the results. but that is not consistent with the "scalable" gopls design. --> +Functions, `func` types, and dynamic function calls are matched using signatures: + +- When invoked on the `func` token of a **function definition**, + it returns the locations of the matching signature types + and dynamic call expressions. +- When invoked on the `func` token of a **signature type**, + it returns the locations of the matching concrete function definitions. +- When invoked on the `(` token of a **dynamic function call**, + it returns the locations of the matching concrete function + definitions. + If either the target type or the candidate type are generic, the results will include the candidate type if there is any instantiation of the two types that would allow one to implement the other. @@ -120,6 +134,12 @@ types, without regard to consistency of substitutions across the method set or even within a single method. This may lead to occasional spurious matches.) +Since a type may be both a function type and a named type with methods +(for example, `http.HandlerFunc`), it may participate in both kinds of +implementation queries (by method-sets and function signatures). +Queries using method-sets should be invoked on the type or method name, +and queries using signatures should be invoked on a `func` or `(` token. + Client support: - **VS Code**: Use [Go to Implementations](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-implementation) (`⌘F12`). - **Emacs + eglot**: Use `M-x eglot-find-implementation`. diff --git a/gopls/doc/release/v0.19.0.md b/gopls/doc/release/v0.19.0.md index 18088732656..149a474244a 100644 --- a/gopls/doc/release/v0.19.0.md +++ b/gopls/doc/release/v0.19.0.md @@ -7,6 +7,33 @@ # New features +## "Implementations" supports signature types + +The Implementations query reports the correspondence between abstract +and concrete types and their methods based on their method sets. +Now, it also reports the correspondence between function types, +dynamic function calls, and function definitions, based on their signatures. + +To use it, invoke an Implementations query on the `func` token of the +definition of a named function, named method, or function literal. +Gopls reports the set of function signature types that abstract this +function, and the set of dynamic calls through values of such types. + +Conversely, an Implementations query on the `func` token of a +signature type, or on the `(` paren of a dynamic function call, +reports the set of concrete functions that the signature abstracts +or that the call dispatches to. + +Since a type may be both a function type and a named type with methods +(for example, `http.HandlerFunc`), it may participate in both kinds of +Implements queries (method-sets and function signatures). +Queries using method-sets should be invoked on the type or method name, +and queries using signatures should be invoked on a `func` or `(` token. + +Only the local (same-package) algorithm is currently supported. +TODO: implement global. + + ## "Eliminate dot import" code action This code action, available on a dotted import, will offer to replace diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index a7a7e663d44..2d9a1e93ef3 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -12,6 +12,7 @@ import ( "go/token" "go/types" "reflect" + "slices" "sort" "strings" "sync" @@ -21,10 +22,13 @@ import ( "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/cache/metadata" "golang.org/x/tools/gopls/internal/cache/methodsets" + "golang.org/x/tools/gopls/internal/cache/parsego" "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/safetoken" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" "golang.org/x/tools/internal/event" ) @@ -74,9 +78,26 @@ func Implementation(ctx context.Context, snapshot *cache.Snapshot, f file.Handle } func implementations(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp protocol.Position) ([]protocol.Location, error) { - // First, find the object referenced at the cursor by type checking the - // current package. - obj, pkg, err := implementsObj(ctx, snapshot, fh.URI(), pp) + // Type check the current package. + pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI()) + if err != nil { + return nil, err + } + pos, err := pgf.PositionPos(pp) + if err != nil { + return nil, err + } + + // Find implementations based on func signatures. + if locs, err := implFuncs(pkg, pgf, pos); err != errNotHandled { + return locs, err + } + + // Find implementations based on method sets. + + // First, find the object referenced at the cursor. + // The object may be declared in a different package. + obj, err := implementsObj(pkg, pgf, pos) if err != nil { return nil, err } @@ -272,21 +293,9 @@ func offsetToLocation(ctx context.Context, snapshot *cache.Snapshot, filename st return m.OffsetLocation(start, end) } -// implementsObj returns the object to query for implementations, which is a -// type name or method. -// -// The returned Package is the narrowest package containing ppos, which is the -// package using the resulting obj but not necessarily the declaring package. -func implementsObj(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, ppos protocol.Position) (types.Object, *cache.Package, error) { - pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, uri) - if err != nil { - return nil, nil, err - } - pos, err := pgf.PositionPos(ppos) - if err != nil { - return nil, nil, err - } - +// implementsObj returns the object to query for implementations, +// which is a type name or method. +func implementsObj(pkg *cache.Package, pgf *parsego.File, pos token.Pos) (types.Object, error) { // This function inherits the limitation of its predecessor in // requiring the selection to be an identifier (of a type or // method). But there's no fundamental reason why one could @@ -299,11 +308,11 @@ func implementsObj(ctx context.Context, snapshot *cache.Snapshot, uri protocol.D // TODO(adonovan): simplify: use objectsAt? path := pathEnclosingObjNode(pgf.File, pos) if path == nil { - return nil, nil, ErrNoIdentFound + return nil, ErrNoIdentFound } id, ok := path[0].(*ast.Ident) if !ok { - return nil, nil, ErrNoIdentFound + return nil, ErrNoIdentFound } // Is the object a type or method? Reject other kinds. @@ -319,17 +328,18 @@ func implementsObj(ctx context.Context, snapshot *cache.Snapshot, uri protocol.D // ok case *types.Func: if obj.Signature().Recv() == nil { - return nil, nil, fmt.Errorf("%s is a function, not a method", id.Name) + return nil, fmt.Errorf("%s is a function, not a method (query at 'func' token to find matching signatures)", id.Name) } case nil: - return nil, nil, fmt.Errorf("%s denotes unknown object", id.Name) + return nil, fmt.Errorf("%s denotes unknown object", id.Name) default: // e.g. *types.Var -> "var". kind := strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types.")) - return nil, nil, fmt.Errorf("%s is a %s, not a type", id.Name, kind) + // TODO(adonovan): improve upon "nil is a nil, not a type". + return nil, fmt.Errorf("%s is a %s, not a type", id.Name, kind) } - return obj, pkg, nil + return obj, nil } // localImplementations searches within pkg for declarations of all @@ -679,9 +689,236 @@ func pathEnclosingObjNode(f *ast.File, pos token.Pos) []ast.Node { } // Reverse path so leaf is first element. - for i := 0; i < len(path)/2; i++ { - path[i], path[len(path)-1-i] = path[len(path)-1-i], path[i] - } + slices.Reverse(path) return path } + +// --- Implementations based on signature types -- + +// implFuncs finds Implementations based on func types. +// +// Just as an interface type abstracts a set of concrete methods, a +// function type abstracts a set of concrete functions. Gopls provides +// analogous operations for navigating from abstract to concrete and +// back in the domain of function types. +// +// A single type (for example http.HandlerFunc) can have both an +// underlying type of function (types.Signature) and have methods that +// cause it to implement an interface. To avoid a confusing user +// interface we want to separate the two operations so that the user +// can unambiguously specify the query they want. +// +// So, whereas Implementations queries on interface types are usually +// keyed by an identifier of a named type, Implementations queries on +// function types are keyed by the "func" keyword, or by the "(" of a +// call expression. The query relates two sets of locations: +// +// 1. the "func" token of each function declaration (FuncDecl or +// FuncLit). These are analogous to declarations of concrete +// methods. +// +// 2. uses of abstract functions: +// +// (a) the "func" token of each FuncType that is not part of +// Func{Decl,Lit}. These are analogous to interface{...} types. +// +// (b) the "(" paren of each dynamic call on a value of an +// abstract function type. These are analogous to references to +// interface method names, but no names are involved, which has +// historically made them hard to search for. +// +// An Implementations query on a location in set 1 returns set 2, +// and vice versa. +// +// implFuncs returns errNotHandled to indicate that we should try the +// regular method-sets algorithm. +func implFuncs(pkg *cache.Package, pgf *parsego.File, pos token.Pos) ([]protocol.Location, error) { + curSel, ok := pgf.Cursor.FindPos(pos, pos) + if !ok { + return nil, fmt.Errorf("no code selected") + } + + info := pkg.TypesInfo() + + // Find innermost enclosing FuncType or CallExpr. + // + // We are looking for specific tokens (FuncType.Func and + // CallExpr.Lparen), but FindPos prefers an adjoining + // subexpression: given f(x) without additional spaces between + // tokens, FindPos always returns either f or x, never the + // CallExpr itself. Thus we must ascend the tree. + // + // Another subtlety: due to an edge case in go/ast, FindPos at + // FuncDecl.Type.Func does not return FuncDecl.Type, only the + // FuncDecl, because the orders of tree positions and tokens + // are inconsistent. Consequently, the ancestors for a "func" + // token of Func{Lit,Decl} do not include FuncType, hence the + // explicit cases below. + for _, cur := range curSel.Stack(nil) { + switch n := cur.Node().(type) { + case *ast.FuncDecl, *ast.FuncLit: + if inToken(n.Pos(), "func", pos) { + // Case 1: concrete function declaration. + // Report uses of corresponding function types. + switch n := n.(type) { + case *ast.FuncDecl: + return funcUses(pkg, info.Defs[n.Name].Type()) + case *ast.FuncLit: + return funcUses(pkg, info.TypeOf(n.Type)) + } + } + + case *ast.FuncType: + if n.Func.IsValid() && inToken(n.Func, "func", pos) && !beneathFuncDef(cur) { + // Case 2a: function type. + // Report declarations of corresponding concrete functions. + return funcDefs(pkg, info.TypeOf(n)) + } + + case *ast.CallExpr: + if inToken(n.Lparen, "(", pos) { + t := dynamicFuncCallType(info, n) + if t == nil { + return nil, fmt.Errorf("not a dynamic function call") + } + // Case 2b: dynamic call of function value. + // Report declarations of corresponding concrete functions. + return funcDefs(pkg, t) + } + } + } + + // It's probably a query of a named type or method. + // Fall back to the method-sets computation. + return nil, errNotHandled +} + +var errNotHandled = errors.New("not handled") + +// funcUses returns all locations in the workspace that are dynamic +// uses of the specified function type. +func funcUses(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { + var locs []protocol.Location + + // local search + for _, pgf := range pkg.CompiledGoFiles() { + for cur := range pgf.Cursor.Preorder((*ast.CallExpr)(nil), (*ast.FuncType)(nil)) { + var pos, end token.Pos + var ftyp types.Type + switch n := cur.Node().(type) { + case *ast.CallExpr: + ftyp = dynamicFuncCallType(pkg.TypesInfo(), n) + pos, end = n.Lparen, n.Lparen+token.Pos(len("(")) + + case *ast.FuncType: + if !beneathFuncDef(cur) { + // func type (not def) + ftyp = pkg.TypesInfo().TypeOf(n) + pos, end = n.Func, n.Func+token.Pos(len("func")) + } + } + if ftyp == nil { + continue // missing type information + } + if unify(t, ftyp) { + loc, err := pgf.PosLocation(pos, end) + if err != nil { + return nil, err + } + locs = append(locs, loc) + } + } + } + + // TODO(adonovan): implement global search + + return locs, nil +} + +// funcDefs returns all locations in the workspace that define +// functions of the specified type. +func funcDefs(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { + var locs []protocol.Location + + // local search + for _, pgf := range pkg.CompiledGoFiles() { + for curFn := range pgf.Cursor.Preorder((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { + fn := curFn.Node() + var ftyp types.Type + switch fn := fn.(type) { + case *ast.FuncDecl: + ftyp = pkg.TypesInfo().Defs[fn.Name].Type() + case *ast.FuncLit: + ftyp = pkg.TypesInfo().TypeOf(fn) + } + if ftyp == nil { + continue // missing type information + } + if unify(t, ftyp) { + pos := fn.Pos() + loc, err := pgf.PosLocation(pos, pos+token.Pos(len("func"))) + if err != nil { + return nil, err + } + locs = append(locs, loc) + } + } + } + + // TODO(adonovan): implement global search, by analogy with + // methodsets algorithm. + // + // One optimization: if any signature type has free package + // names, look for matches only in packages among the rdeps of + // those packages. + + return locs, nil +} + +// beneathFuncDef reports whether the specified FuncType cursor is a +// child of Func{Decl,Lit}. +func beneathFuncDef(cur cursor.Cursor) bool { + ek, _ := cur.Edge() + switch ek { + case edge.FuncDecl_Type, edge.FuncLit_Type: + return true + } + return false +} + +// dynamicFuncCallType reports whether call is a dynamic (non-method) function call. +// If so, it returns the function type, otherwise nil. +// +// Tested via ../test/marker/testdata/implementation/signature.txt. +func dynamicFuncCallType(info *types.Info, call *ast.CallExpr) types.Type { + fun := ast.Unparen(call.Fun) + tv := info.Types[fun] + + // Reject conversion, or call to built-in. + if !tv.IsValue() { + return nil + } + + // Reject call to named func/method. + if id, ok := fun.(*ast.Ident); ok && is[*types.Func](info.Uses[id]) { + return nil + } + + // Reject method selections (T.method() or x.method()) + if sel, ok := fun.(*ast.SelectorExpr); ok { + seln, ok := info.Selections[sel] + if !ok || seln.Kind() != types.FieldVal { + return nil + } + } + + // TODO(adonovan): consider x() where x : TypeParam. + return tv.Type.Underlying() // e.g. x() or x.field() +} + +// inToken reports whether pos is within the token of +// the specified position and string. +func inToken(tokPos token.Pos, tokStr string, pos token.Pos) bool { + return tokPos <= pos && pos <= tokPos+token.Pos(len(tokStr)) +} diff --git a/gopls/internal/test/marker/doc.go b/gopls/internal/test/marker/doc.go index dff8dfa109f..2fc3e042061 100644 --- a/gopls/internal/test/marker/doc.go +++ b/gopls/internal/test/marker/doc.go @@ -212,9 +212,10 @@ Here is the list of supported action markers: - hovererr(src, sm stringMatcher): performs a textDocument/hover at the src location, and checks that the error matches the given stringMatcher. - - implementations(src location, want ...location): makes a - textDocument/implementation query at the src location and - checks that the resulting set of locations matches want. + - implementation(src location, want ...location, err=stringMatcher): + makes a textDocument/implementation query at the src location and + checks that the resulting set of locations matches want. If err is + set, the implementation query must fail with the expected error. - incomingcalls(src location, want ...location): makes a callHierarchy/incomingCalls query at the src location, and checks that diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index a3e62d35968..3ff7da65ac5 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -584,7 +584,7 @@ var actionMarkerFuncs = map[string]func(marker){ "highlightall": actionMarkerFunc(highlightAllMarker), "hover": actionMarkerFunc(hoverMarker), "hovererr": actionMarkerFunc(hoverErrMarker), - "implementation": actionMarkerFunc(implementationMarker), + "implementation": actionMarkerFunc(implementationMarker, "err"), "incomingcalls": actionMarkerFunc(incomingCallsMarker), "inlayhints": actionMarkerFunc(inlayhintsMarker), "outgoingcalls": actionMarkerFunc(outgoingCallsMarker), @@ -2375,13 +2375,19 @@ func refsMarker(mark marker, src protocol.Location, want ...protocol.Location) { // implementationMarker implements the @implementation marker. func implementationMarker(mark marker, src protocol.Location, want ...protocol.Location) { + wantErr := namedArgFunc(mark, "err", convertStringMatcher, stringMatcher{}) + got, err := mark.server().Implementation(mark.ctx(), &protocol.ImplementationParams{ TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(src), }) - if err != nil { + if err != nil && wantErr.empty() { mark.errorf("implementation at %s failed: %v", src, err) return } + if !wantErr.empty() { + wantErr.checkErr(mark, err) + return + } if err := compareLocations(mark, got, want); err != nil { mark.errorf("implementation: %v", err) } diff --git a/gopls/internal/test/marker/testdata/implementation/signature.txt b/gopls/internal/test/marker/testdata/implementation/signature.txt new file mode 100644 index 00000000000..b94d048a135 --- /dev/null +++ b/gopls/internal/test/marker/testdata/implementation/signature.txt @@ -0,0 +1,79 @@ +Test of local Implementation queries using function signatures. + +Assertions: +- Query on "func" of a function type returns the corresponding concrete functions. +- Query on "func" of a concrete function returns corresponding function types. +- Query on "(" of a dynamic function call returns corresponding function types. +- Different signatures (Nullary vs Handler) don't correspond. + +The @loc markers use the suffixes Func, Type, Call for the three kinds. +Each query maps between these two sets: {Func} <=> {Type,Call}. + +-- go.mod -- +module example.com +go 1.18 + +-- a/a.go -- +package a + +// R is short for Record. +type R struct{} + +// H is short for Handler. +type H func(*R) //@ loc(HType, "func"), implementation("func", aFunc, bFunc, cFunc) + +func aFunc(*R) {} //@ loc(aFunc, "func"), implementation("func", HType, hParamType, hCall) + +var bFunc = func(*R) {} //@ loc(bFunc, "func"), implementation("func", hParamType, hCall, HType) + +func nullary() { //@ loc(nullaryFunc, "func"), implementation("func", Nullary, fieldCall) + cFunc := func(*R) {} //@ loc(cFunc, "func"), implementation("func", hParamType, hCall, HType) + _ = cFunc +} + +type Nullary func() //@ loc(Nullary, "func") + +func _( + h func(*R)) { //@ loc(hParamType, "func"), implementation("func", aFunc, bFunc, cFunc) + + _ = aFunc // pacify unusedfunc + _ = nullary // pacify unusedfunc + _ = h + + h(nil) //@ loc(hCall, "("), implementation("(", aFunc, bFunc, cFunc) +} + +// generics: + +func _[T any](complex128) { + f1 := func(T) int { return 0 } //@ loc(f1Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call) + f2 := func(string) int { return 0 } //@ loc(f2Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call) + f3 := func(int) int { return 0 } //@ loc(f3Func, "func"), implementation("func", f1Call) + + f1(*new(T)) //@ loc(f1Call, "("), implementation("(", f1Func, f2Func, f3Func, f4Func) + f2("") //@ loc(f2Call, "("), implementation("(", f1Func, f2Func, f4Func) + _ = f3 // not called +} + +func f4[T any](T) int { return 0 } //@ loc(f4Func, "func"), implementation("func", fParamType, fCall, f1Call, f2Call) + +var _ = f4[string] // pacify unusedfunc + +func _( + f func(string) int, //@ loc(fParamType, "func"), implementation("func", f1Func, f2Func, f4Func) + err error) { + + f("") //@ loc(fCall, "("), implementation("(", f1Func, f2Func, f4Func) + + struct{x Nullary}{}.x() //@ loc(fieldCall, "("), implementation("(", nullaryFunc) + + // Calls that are not dynamic function calls: + _ = len("") //@ implementation("(", err="not a dynamic function call") + _ = int(0) //@ implementation("(", err="not a dynamic function call") + _ = error.Error(nil) //@ implementation("(", err="not a dynamic function call") + _ = err.Error() //@ implementation("(", err="not a dynamic function call") + _ = f4(0) //@ implementation("(", err="not a dynamic function call"), loc(f4Call, "(") +} + + + From 6a5b66bef78dc7a1cf8593b276f35102ec0cb11c Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Wed, 5 Mar 2025 11:56:25 -0800 Subject: [PATCH 225/309] go.mod: update golang.org/x dependencies Update golang.org/x dependencies to their latest tagged versions. Change-Id: I13ce38cd00119b55ee384af53f27a72feb72572b Reviewed-on: https://go-review.googlesource.com/c/tools/+/655020 Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Auto-Submit: Gopher Robot --- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- gopls/go.mod | 8 ++++---- gopls/go.sum | 22 +++++++++++----------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index bc7636b4cf8..3a120629b94 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ go 1.23.0 require ( github.com/google/go-cmp v0.6.0 github.com/yuin/goldmark v1.4.13 - golang.org/x/mod v0.23.0 - golang.org/x/net v0.35.0 - golang.org/x/sync v0.11.0 + golang.org/x/mod v0.24.0 + golang.org/x/net v0.37.0 + golang.org/x/sync v0.12.0 golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 ) -require golang.org/x/sys v0.30.0 // indirect +require golang.org/x/sys v0.31.0 // indirect diff --git a/go.sum b/go.sum index 2d11b060c08..3d0337c8351 100644 --- a/go.sum +++ b/go.sum @@ -2,13 +2,13 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= -golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= -golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= -golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= -golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= -golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMePxjXAJctxKmyjKUsjA11Uzuk= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= diff --git a/gopls/go.mod b/gopls/go.mod index 210943206b8..da7303222d2 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -5,11 +5,11 @@ go 1.24.0 require ( github.com/google/go-cmp v0.6.0 github.com/jba/templatecheck v0.7.1 - golang.org/x/mod v0.23.0 - golang.org/x/sync v0.11.0 - golang.org/x/sys v0.30.0 + golang.org/x/mod v0.24.0 + golang.org/x/sync v0.12.0 + golang.org/x/sys v0.31.0 golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc - golang.org/x/text v0.22.0 + golang.org/x/text v0.23.0 golang.org/x/tools v0.30.0 golang.org/x/vuln v1.1.4 gopkg.in/yaml.v3 v3.0.1 diff --git a/gopls/go.sum b/gopls/go.sum index ef93b2c4601..20633541388 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -16,36 +16,36 @@ github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGK github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa h1:Br3+0EZZohShrmVVc85znGpxw7Ca8hsUJlrdT/JQGw8= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:LKZHyeOpPuZcMgxeHjJp4p5yvxrCX1xDvH10zYHhjjQ= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= -golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= -golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc h1:HS+G1Mhh2dxM8ObutfYKdjfD7zpkyeP/UxeRnJpIZtQ= golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc/go.mod h1:bDzXkYUaHzz51CtDy5kh/jR4lgPxsdbqC37kp/dzhCc= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= +golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= -golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= golang.org/x/vuln v1.1.4 h1:Ju8QsuyhX3Hk8ma3CesTbO8vfJD9EvUBgHvkxHBzj0I= golang.org/x/vuln v1.1.4/go.mod h1:F+45wmU18ym/ca5PLTPLsSzr2KppzswxPP603ldA67s= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From b08c7a26ea3c519d19f4e2095d070ca8ce65161a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 5 Mar 2025 13:45:05 -0500 Subject: [PATCH 226/309] gopls/internal/util/fingerprint: split from cache/methodsets This CL splits the fingerprint data type into its own package, as the index for Implementations by signatures will need it, but is otherwise unrelated to the logic to build the index by method sets. Updates golang/go#56572 Change-Id: I87905d3c5f3d555f100f318b97080e6802b616e4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655175 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Commit-Queue: Alan Donovan --- gopls/internal/cache/methodsets/methodsets.go | 11 +-- .../fingerprint}/fingerprint.go | 85 ++++++++++++------- .../fingerprint}/fingerprint_test.go | 41 ++++----- 3 files changed, 78 insertions(+), 59 deletions(-) rename gopls/internal/{cache/methodsets => util/fingerprint}/fingerprint.go (83%) rename gopls/internal/{cache/methodsets => util/fingerprint}/fingerprint_test.go (79%) diff --git a/gopls/internal/cache/methodsets/methodsets.go b/gopls/internal/cache/methodsets/methodsets.go index 3026819ee81..2387050f2d9 100644 --- a/gopls/internal/cache/methodsets/methodsets.go +++ b/gopls/internal/cache/methodsets/methodsets.go @@ -52,6 +52,7 @@ import ( "golang.org/x/tools/go/types/objectpath" "golang.org/x/tools/gopls/internal/util/bug" + "golang.org/x/tools/gopls/internal/util/fingerprint" "golang.org/x/tools/gopls/internal/util/frob" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/typesinternal" @@ -195,7 +196,7 @@ func implements(x, y *gobMethodSet) bool { // so a string match is sufficient. match = mx.Sum&my.Sum == my.Sum && mx.Fingerprint == my.Fingerprint } else { - match = unify(mx.parse(), my.parse()) + match = fingerprint.Matches(mx.parse(), my.parse()) } return !match } @@ -326,7 +327,7 @@ func methodSetInfo(t types.Type, setIndexInfo func(*gobMethod, *types.Func)) *go for i := 0; i < mset.Len(); i++ { m := mset.At(i).Obj().(*types.Func) id := m.Id() - fp, isTricky := fingerprint(m.Signature()) + fp, isTricky := fingerprint.Encode(m.Signature()) if isTricky { tricky = true } @@ -389,7 +390,7 @@ type gobMethod struct { ObjectPath int // object path of method relative to PkgPath // internal fields (not serialized) - tree atomic.Pointer[sexpr] // fingerprint tree, parsed on demand + tree atomic.Pointer[fingerprint.Tree] // fingerprint tree, parsed on demand } // A gobPosition records the file, offset, and length of an identifier. @@ -400,10 +401,10 @@ type gobPosition struct { // parse returns the method's parsed fingerprint tree. // It may return a new instance or a cached one. -func (m *gobMethod) parse() sexpr { +func (m *gobMethod) parse() fingerprint.Tree { ptr := m.tree.Load() if ptr == nil { - tree := parseFingerprint(m.Fingerprint) + tree := fingerprint.Parse(m.Fingerprint) ptr = &tree m.tree.Store(ptr) // may race; that's ok } diff --git a/gopls/internal/cache/methodsets/fingerprint.go b/gopls/internal/util/fingerprint/fingerprint.go similarity index 83% rename from gopls/internal/cache/methodsets/fingerprint.go rename to gopls/internal/util/fingerprint/fingerprint.go index 05ccfe0911c..2b657ba7857 100644 --- a/gopls/internal/cache/methodsets/fingerprint.go +++ b/gopls/internal/util/fingerprint/fingerprint.go @@ -1,7 +1,13 @@ -// Copyright 2024 The Go Authors. All rights reserved. +// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package methodsets + +// Package fingerprint defines a function to [Encode] types as strings +// with the property that identical types have equal string encodings, +// in most cases. In the remaining cases (mostly involving generic +// types), the encodings can be parsed using [Parse] into [Tree] form +// and matched using [Matches]. +package fingerprint import ( "fmt" @@ -12,6 +18,52 @@ import ( "text/scanner" ) +// Encode returns an encoding of a [types.Type] such that, in +// most cases, Encode(x) == Encode(y) iff [types.Identical](x, y). +// +// For a minority of types, mostly involving type parameters, identity +// cannot be reduced to string comparison; these types are called +// "tricky", and are indicated by the boolean result. +// +// In general, computing identity correctly for tricky types requires +// the type checker. However, the fingerprint encoding can be parsed +// by [Parse] into a [Tree] form that permits simple matching sufficient +// to allow a type parameter to unify with any subtree; see [Match]. +// +// In the standard library, 99.8% of package-level types have a +// non-tricky method-set. The most common exceptions are due to type +// parameters. +// +// fingerprint.Encode is defined only for the signature types of functions +// and methods. It must not be called for "untyped" basic types, nor +// the type of a generic function. +func Encode(t types.Type) (_ string, tricky bool) { return fingerprint(t) } + +// A Tree is a parsed form of a fingerprint for use with [Matches]. +type Tree struct{ tree sexpr } + +// String returns the tree in an unspecified human-readable form. +func (tree Tree) String() string { + var out strings.Builder + writeSexpr(&out, tree.tree) + return out.String() +} + +// Parse parses a fingerprint into tree form. +// +// The input must have been produced by [Encode] at the same source +// version; parsing is thus infallible. +func Parse(fp string) Tree { + return Tree{parseFingerprint(fp)} +} + +// Matches reports whether two fingerprint trees match, meaning that +// under some conditions (for example, particular instantiations of +// type parameters) the two types may be identical. +func Matches(x, y Tree) bool { + return unify(x.tree, y.tree) +} + // Fingerprint syntax // // The lexical syntax is essentially Lisp S-expressions: @@ -38,25 +90,6 @@ import ( // // field = IDENT IDENT STRING Ï„ -- name, embedded?, tag, type -// fingerprint returns an encoding of a [types.Type] such that, in -// most cases, fingerprint(x) == fingerprint(t) iff types.Identical(x, y). -// -// For a minority of types, mostly involving type parameters, identity -// cannot be reduced to string comparison; these types are called -// "tricky", and are indicated by the boolean result. -// -// In general, computing identity correctly for tricky types requires -// the type checker. However, the fingerprint encoding can be parsed -// by [parseFingerprint] into a tree form that permits simple matching -// sufficient to allow a type parameter to unify with any subtree. -// -// In the standard library, 99.8% of package-level types have a -// non-tricky method-set. The most common exceptions are due to type -// parameters. -// -// fingerprint is defined only for the signature types of methods. It -// must not be called for "untyped" basic types, nor the type of a -// generic function. func fingerprint(t types.Type) (string, bool) { var buf strings.Builder tricky := false @@ -202,8 +235,6 @@ func fingerprint(t types.Type) (string, bool) { return buf.String(), tricky } -const symTypeparam = "typeparam" - // sexpr defines the representation of a fingerprint tree. type ( sexpr any // = string | int | symbol | *cons | nil @@ -272,12 +303,6 @@ func parseFingerprint(fp string) sexpr { return parse() } -func sexprString(x sexpr) string { - var out strings.Builder - writeSexpr(&out, x) - return out.String() -} - // writeSexpr formats an S-expression. // It is provided for debugging. func writeSexpr(out *strings.Builder, x sexpr) { @@ -355,3 +380,5 @@ func isTypeParam(x sexpr) int { } return -1 } + +const symTypeparam = "typeparam" diff --git a/gopls/internal/cache/methodsets/fingerprint_test.go b/gopls/internal/util/fingerprint/fingerprint_test.go similarity index 79% rename from gopls/internal/cache/methodsets/fingerprint_test.go rename to gopls/internal/util/fingerprint/fingerprint_test.go index 795ddaa965b..7a7a2fe7569 100644 --- a/gopls/internal/cache/methodsets/fingerprint_test.go +++ b/gopls/internal/util/fingerprint/fingerprint_test.go @@ -1,13 +1,8 @@ -// Copyright 2024 The Go Authors. All rights reserved. +// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package methodsets - -// This is an internal test of [fingerprint] and [unify]. -// -// TODO(adonovan): avoid internal tests. -// Break fingerprint.go off into its own package? +package fingerprint_test import ( "go/types" @@ -15,15 +10,15 @@ import ( "golang.org/x/tools/go/packages" "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/internal/testenv" + "golang.org/x/tools/gopls/internal/util/fingerprint" "golang.org/x/tools/internal/testfiles" "golang.org/x/tools/txtar" ) -// Test_fingerprint runs the fingerprint encoder, decoder, and printer +// Test runs the fingerprint encoder, decoder, and printer // on the types of all package-level symbols in gopls, and ensures // that parse+print is lossless. -func Test_fingerprint(t *testing.T) { +func Test(t *testing.T) { if testing.Short() { t.Skip("skipping slow test") } @@ -54,7 +49,7 @@ func Test_fingerprint(t *testing.T) { continue // untyped constant } - fp, tricky := fingerprint(typ) // check Type encoder doesn't panic + fp, tricky := fingerprint.Encode(typ) // check Type encoder doesn't panic // All equivalent (non-tricky) types have the same fingerprint. if !tricky { @@ -66,8 +61,8 @@ func Test_fingerprint(t *testing.T) { } } - tree := parseFingerprint(fp) // check parser doesn't panic - fp2 := sexprString(tree) // check formatter doesn't pannic + tree := fingerprint.Parse(fp) // check parser doesn't panic + fp2 := tree.String() // check formatter doesn't pannic // A parse+print round-trip should be lossless. if fp != fp2 { @@ -79,12 +74,8 @@ func Test_fingerprint(t *testing.T) { } } -// Test_unify exercises the matching algorithm for generic types. -func Test_unify(t *testing.T) { - if testenv.Go1Point() < 24 { - testenv.NeedsGoExperiment(t, "aliastypeparams") // testenv.Go1Point() >= 24 implies aliastypeparams=1 - } - +// TestMatches exercises the matching algorithm for generic types. +func TestMatches(t *testing.T) { const src = ` -- go.mod -- module example.com @@ -167,17 +158,17 @@ func E3(int8) uint32 a := lookup(test.a) b := lookup(test.b) - afp, _ := fingerprint(a) - bfp, _ := fingerprint(b) + afp, _ := fingerprint.Encode(a) + bfp, _ := fingerprint.Encode(b) - atree := parseFingerprint(afp) - btree := parseFingerprint(bfp) + atree := fingerprint.Parse(afp) + btree := fingerprint.Parse(bfp) - got := unify(atree, btree) + got := fingerprint.Matches(atree, btree) if got != test.want { t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", test.a, test.b, test.method, - got, sexprString(atree), sexprString(btree)) + got, atree, btree) } } } From 7435a8148d95389cf0c726d9954341d63f69cc66 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 7 Mar 2025 11:52:17 -0500 Subject: [PATCH 227/309] gopls/internal/analysis/modernize: document workflow Also, add and document a -category flag on the modernize analyzer. (This is a stopgap until the checker driver supports this flag generally; see issue 72008 and CL 655555.) Fixes golang/go#72008 Change-Id: Iad5bc277b1251f2edb935f16077fd3add61041c5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655436 Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/doc/analyzers.md | 89 ++++++++++++++----- gopls/internal/analysis/modernize/doc.go | 89 ++++++++++++++----- .../internal/analysis/modernize/modernize.go | 42 +++++++-- gopls/internal/doc/api.json | 4 +- 4 files changed, 166 insertions(+), 58 deletions(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index aa95e024089..bcf5590090a 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -476,39 +476,80 @@ Package documentation: [lostcancel](https://pkg.go.dev/golang.org/x/tools/go/ana This analyzer reports opportunities for simplifying and clarifying -existing code by using more modern features of Go, such as: - - - replacing an if/else conditional assignment by a call to the - built-in min or max functions added in go1.21; - - replacing sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } - by a call to slices.Sort(s), added in go1.21; - - replacing interface{} by the 'any' type added in go1.18; - - replacing append([]T(nil), s...) by slices.Clone(s) or - slices.Concat(s), added in go1.21; - - replacing a loop around an m[k]=v map update by a call - to one of the Collect, Copy, Clone, or Insert functions - from the maps package, added in go1.21; - - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), - added in go1.19; - - replacing uses of context.WithCancel in tests with t.Context, added in - go1.24; - - replacing omitempty by omitzero on structs, added in go1.24; - - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), - added in go1.21 - - replacing a 3-clause for i := 0; i < n; i++ {} loop by - for i := range n {}, added in go1.22; - - replacing Split in "for range strings.Split(...)" by go1.24's - more efficient SplitSeq, or Fields with FieldSeq; +existing code by using more modern features of Go and its standard +library. + +Each diagnostic provides a fix. Our intent is that these fixes may +be safely applied en masse without changing the behavior of your +program. In some cases the suggested fixes are imperfect and may +lead to (for example) unused imports or unused local variables, +causing build breakage. However, these problems are generally +trivial to fix. We regard any modernizer whose fix changes program +behavior to have a serious bug and will endeavor to fix it. To apply all modernization fixes en masse, you can use the following command: - $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./... + $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... If the tool warns of conflicting fixes, you may need to run it more than once until it has applied all fixes cleanly. This command is not an officially supported interface and may change in the future. +Changes produced by this tool should be reviewed as usual before +being merged. In some cases, a loop may be replaced by a simple +function call, causing comments within the loop to be discarded. +Human judgment may be required to avoid losing comments of value. + +Each diagnostic reported by modernize has a specific category. (The +categories are listed below.) Diagnostics in some categories, such +as "efaceany" (which replaces "interface{}" with "any" where it is +safe to do so) are particularly numerous. It may ease the burden of +code review to apply fixes in two passes, the first change +consisting only of fixes of category "efaceany", the second +consisting of all others. This can be achieved using the -category flag: + + $ modernize -category=efaceany -fix -test ./... + $ modernize -category=-efaceany -fix -test ./... + +Categories of modernize diagnostic: + + - minmax: replace an if/else conditional assignment by a call to + the built-in min or max functions added in go1.21. + + - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } + by a call to slices.Sort(s), added in go1.21. + + - efaceany: replace interface{} by the 'any' type added in go1.18. + + - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or + slices.Concat(s), added in go1.21. + + - mapsloop: replace a loop around an m[k]=v map update by a call + to one of the Collect, Copy, Clone, or Insert functions from + the maps package, added in go1.21. + + - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), + added in go1.19. + + - testingcontext: replace uses of context.WithCancel in tests + with t.Context, added in go1.24. + + - omitzero: replace omitempty by omitzero on structs, added in go1.24. + + - bloop: replace "for i := range b.N" or "for range b.N" in a + benchmark with "for b.Loop()", and remove any preceding calls + to b.StopTimer, b.StartTimer, and b.ResetTimer. + + - slicesdelete: replace append(s[:i], s[i+1]...) by + slices.Delete(s, i, i+1), added in go1.21. + + - rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by + "for i := range n", added in go1.22. + + - stringseq: replace Split in "for range strings.Split(...)" by go1.24's + more efficient SplitSeq, or Fields with FieldSeq. + Default: on. Package documentation: [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index b12abab7063..931a2e6bd45 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -9,36 +9,77 @@ // modernize: simplify code by using modern constructs // // This analyzer reports opportunities for simplifying and clarifying -// existing code by using more modern features of Go, such as: -// -// - replacing an if/else conditional assignment by a call to the -// built-in min or max functions added in go1.21; -// - replacing sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } -// by a call to slices.Sort(s), added in go1.21; -// - replacing interface{} by the 'any' type added in go1.18; -// - replacing append([]T(nil), s...) by slices.Clone(s) or -// slices.Concat(s), added in go1.21; -// - replacing a loop around an m[k]=v map update by a call -// to one of the Collect, Copy, Clone, or Insert functions -// from the maps package, added in go1.21; -// - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), -// added in go1.19; -// - replacing uses of context.WithCancel in tests with t.Context, added in -// go1.24; -// - replacing omitempty by omitzero on structs, added in go1.24; -// - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), -// added in go1.21 -// - replacing a 3-clause for i := 0; i < n; i++ {} loop by -// for i := range n {}, added in go1.22; -// - replacing Split in "for range strings.Split(...)" by go1.24's -// more efficient SplitSeq, or Fields with FieldSeq; +// existing code by using more modern features of Go and its standard +// library. +// +// Each diagnostic provides a fix. Our intent is that these fixes may +// be safely applied en masse without changing the behavior of your +// program. In some cases the suggested fixes are imperfect and may +// lead to (for example) unused imports or unused local variables, +// causing build breakage. However, these problems are generally +// trivial to fix. We regard any modernizer whose fix changes program +// behavior to have a serious bug and will endeavor to fix it. // // To apply all modernization fixes en masse, you can use the // following command: // -// $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./... +// $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... // // If the tool warns of conflicting fixes, you may need to run it more // than once until it has applied all fixes cleanly. This command is // not an officially supported interface and may change in the future. +// +// Changes produced by this tool should be reviewed as usual before +// being merged. In some cases, a loop may be replaced by a simple +// function call, causing comments within the loop to be discarded. +// Human judgment may be required to avoid losing comments of value. +// +// Each diagnostic reported by modernize has a specific category. (The +// categories are listed below.) Diagnostics in some categories, such +// as "efaceany" (which replaces "interface{}" with "any" where it is +// safe to do so) are particularly numerous. It may ease the burden of +// code review to apply fixes in two passes, the first change +// consisting only of fixes of category "efaceany", the second +// consisting of all others. This can be achieved using the -category flag: +// +// $ modernize -category=efaceany -fix -test ./... +// $ modernize -category=-efaceany -fix -test ./... +// +// Categories of modernize diagnostic: +// +// - minmax: replace an if/else conditional assignment by a call to +// the built-in min or max functions added in go1.21. +// +// - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } +// by a call to slices.Sort(s), added in go1.21. +// +// - efaceany: replace interface{} by the 'any' type added in go1.18. +// +// - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or +// slices.Concat(s), added in go1.21. +// +// - mapsloop: replace a loop around an m[k]=v map update by a call +// to one of the Collect, Copy, Clone, or Insert functions from +// the maps package, added in go1.21. +// +// - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), +// added in go1.19. +// +// - testingcontext: replace uses of context.WithCancel in tests +// with t.Context, added in go1.24. +// +// - omitzero: replace omitempty by omitzero on structs, added in go1.24. +// +// - bloop: replace "for i := range b.N" or "for range b.N" in a +// benchmark with "for b.Loop()", and remove any preceding calls +// to b.StopTimer, b.StartTimer, and b.ResetTimer. +// +// - slicesdelete: replace append(s[:i], s[i+1]...) by +// slices.Delete(s, i, i+1), added in go1.21. +// +// - rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by +// "for i := range n", added in go1.22. +// +// - stringseq: replace Split in "for range strings.Split(...)" by go1.24's +// more efficient SplitSeq, or Fields with FieldSeq. package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 96e8b325df4..fb7d43eb8d7 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -12,6 +12,7 @@ import ( "go/types" "iter" "regexp" + "slices" "strings" "golang.org/x/tools/go/analysis" @@ -36,6 +37,15 @@ var Analyzer = &analysis.Analyzer{ URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", } +// Stopgap until general solution in CL 655555 lands. A change to the +// cmd/vet CLI requires a proposal whereas a change to an analyzer's +// flag set does not. +var category string + +func init() { + Analyzer.Flags.StringVar(&category, "category", "", "comma-separated list of categories to apply; with a leading '-', a list of categories to ignore") +} + func run(pass *analysis.Pass) (any, error) { // Decorate pass.Report to suppress diagnostics in generated files. // @@ -55,6 +65,10 @@ func run(pass *analysis.Pass) (any, error) { if diag.Category == "" { panic("Diagnostic.Category is unset") } + // TODO(adonovan): stopgap until CL 655555 lands. + if !enabledCategory(category, diag.Category) { + return + } if _, ok := generated[pass.Fset.File(diag.Pos)]; ok { return // skip checking if it's generated code } @@ -76,14 +90,7 @@ func run(pass *analysis.Pass) (any, error) { sortslice(pass) testingContext(pass) - // TODO(adonovan): - // - more modernizers here; see #70815. - // - opt: interleave these micro-passes within a single inspection. - // - solve the "duplicate import" problem (#68765) when a number of - // fixes in the same file are applied in parallel and all add - // the same import. The tests exhibit the problem. - // - should all diagnostics be of the form "x can be modernized by y" - // or is that a foolish consistency? + // TODO(adonovan): opt: interleave these micro-passes within a single inspection. return nil, nil } @@ -159,3 +166,22 @@ var ( byteSliceType = types.NewSlice(types.Typ[types.Byte]) omitemptyRegex = regexp.MustCompile(`(?:^json| json):"[^"]*(,omitempty)(?:"|,[^"]*")\s?`) ) + +// enabledCategory reports whether a given category is enabled by the specified +// filter. filter is a comma-separated list of categories, optionally prefixed +// with `-` to disable all provided categories. All categories are enabled with +// an empty filter. +// +// (Will be superseded by https://go.dev/cl/655555.) +func enabledCategory(filter, category string) bool { + if filter == "" { + return true + } + // negation must be specified at the start + filter, exclude := strings.CutPrefix(filter, "-") + filters := strings.Split(filter, ",") + if slices.Contains(filters, category) { + return !exclude + } + return exclude +} diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b9e0e78e950..b47d635638c 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -562,7 +562,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.", "Default": "true", "Status": "" }, @@ -1338,7 +1338,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go, such as:\n\n - replacing an if/else conditional assignment by a call to the\n built-in min or max functions added in go1.21;\n - replacing sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21;\n - replacing interface{} by the 'any' type added in go1.18;\n - replacing append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21;\n - replacing a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions\n from the maps package, added in go1.21;\n - replacing []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19;\n - replacing uses of context.WithCancel in tests with t.Context, added in\n go1.24;\n - replacing omitempty by omitzero on structs, added in go1.24;\n - replacing append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1),\n added in go1.21\n - replacing a 3-clause for i := 0; i \u003c n; i++ {} loop by\n for i := range n {}, added in go1.22;\n - replacing Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq;\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From 29f81e9da6bf86f0b8ca0004cd7ea205e80c8ab5 Mon Sep 17 00:00:00 2001 From: Hongxiang Jiang Date: Tue, 4 Mar 2025 16:20:08 -0500 Subject: [PATCH 228/309] gopls/internal/cache: filter **/foo match any depth Based on description of "build.directoryFilters": "-**/node_modules" should match node_modules at any level. Filter interpreted "**/foo" as "/foo/" but input path is "foo/" causing the mismatch. This CL make following changes: - add "^/" instead of "/" in the front when interpreting user input filters. - ensure the leading "/" when consuming file paths. - keep the performance by removing the leading "^/.*". Replace Filterer by a function accepting rules (raw filters) and returning a function can be used to determine the input path is included or not. Eliminate double-negative by returning true if included, false otherwise. For golang/vscode-go#3692 Change-Id: Ia7d2ab76154db1411dc8c96cd0211eb9c008c3ac Reviewed-on: https://go-review.googlesource.com/c/tools/+/654357 Reviewed-by: Alan Donovan Auto-Submit: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/filterer.go | 74 +++++++++++-------- gopls/internal/cache/session.go | 4 +- gopls/internal/cache/view.go | 8 +- gopls/internal/cache/view_test.go | 6 +- gopls/internal/golang/workspace_symbol.go | 5 +- .../internal/golang/workspace_symbol_test.go | 14 +++- 6 files changed, 63 insertions(+), 48 deletions(-) diff --git a/gopls/internal/cache/filterer.go b/gopls/internal/cache/filterer.go index 0ec18369bdf..13dbd8a1b04 100644 --- a/gopls/internal/cache/filterer.go +++ b/gopls/internal/cache/filterer.go @@ -11,45 +11,55 @@ import ( "strings" ) -type Filterer struct { - // Whether a filter is excluded depends on the operator (first char of the raw filter). - // Slices filters and excluded then should have the same length. - filters []*regexp.Regexp - excluded []bool -} - -// NewFilterer computes regular expression form of all raw filters -func NewFilterer(rawFilters []string) *Filterer { - var f Filterer - for _, filter := range rawFilters { +// PathIncludeFunc creates a function that determines if a given file path +// should be included based on a set of inclusion/exclusion rules. +// +// The `rules` parameter is a slice of strings, where each string represents a +// filtering rule. Each rule consists of an operator (`+` for inclusion, `-` +// for exclusion) followed by a path pattern. See more detail of rules syntax +// at [settings.BuildOptions.DirectoryFilters]. +// +// Rules are evaluated in order, and the last matching rule determines +// whether a path is included or excluded. +// +// Examples: +// - []{"-foo"}: Exclude "foo" at the current depth. +// - []{"-**foo"}: Exclude "foo" at any depth. +// - []{"+bar"}: Include "bar" at the current depth. +// - []{"-foo", "+foo/**/bar"}: Exclude all "foo" at current depth except +// directory "bar" under "foo" at any depth. +func PathIncludeFunc(rules []string) func(string) bool { + var matchers []*regexp.Regexp + var included []bool + for _, filter := range rules { filter = path.Clean(filepath.ToSlash(filter)) // TODO(dungtuanle): fix: validate [+-] prefix. op, prefix := filter[0], filter[1:] - // convertFilterToRegexp adds "/" at the end of prefix to handle cases where a filter is a prefix of another filter. + // convertFilterToRegexp adds "/" at the end of prefix to handle cases + // where a filter is a prefix of another filter. // For example, it prevents [+foobar, -foo] from excluding "foobar". - f.filters = append(f.filters, convertFilterToRegexp(filepath.ToSlash(prefix))) - f.excluded = append(f.excluded, op == '-') + matchers = append(matchers, convertFilterToRegexp(filepath.ToSlash(prefix))) + included = append(included, op == '+') } - return &f -} - -// Disallow return true if the path is excluded from the filterer's filters. -func (f *Filterer) Disallow(path string) bool { - // Ensure trailing but not leading slash. - path = strings.TrimPrefix(path, "/") - if !strings.HasSuffix(path, "/") { - path += "/" - } + return func(path string) bool { + // Ensure leading and trailing slashes. + if !strings.HasPrefix(path, "/") { + path = "/" + path + } + if !strings.HasSuffix(path, "/") { + path += "/" + } - // TODO(adonovan): opt: iterate in reverse and break at first match. - excluded := false - for i, filter := range f.filters { - if filter.MatchString(path) { - excluded = f.excluded[i] // last match wins + // TODO(adonovan): opt: iterate in reverse and break at first match. + include := true + for i, filter := range matchers { + if filter.MatchString(path) { + include = included[i] // last match wins + } } + return include } - return excluded } // convertFilterToRegexp replaces glob-like operator substrings in a string file path to their equivalent regex forms. @@ -60,7 +70,7 @@ func convertFilterToRegexp(filter string) *regexp.Regexp { return regexp.MustCompile(".*") } var ret strings.Builder - ret.WriteString("^") + ret.WriteString("^/") segs := strings.Split(filter, "/") for _, seg := range segs { // Inv: seg != "" since path is clean. @@ -77,7 +87,7 @@ func convertFilterToRegexp(filter string) *regexp.Regexp { // BenchmarkWorkspaceSymbols time by ~20% (even though // filter CPU time increased by only by ~2.5%) when the // default filter was changed to "**/node_modules". - pattern = strings.TrimPrefix(pattern, "^.*") + pattern = strings.TrimPrefix(pattern, "^/.*") return regexp.MustCompile(pattern) } diff --git a/gopls/internal/cache/session.go b/gopls/internal/cache/session.go index c2f57e985f7..c46fc78b975 100644 --- a/gopls/internal/cache/session.go +++ b/gopls/internal/cache/session.go @@ -169,14 +169,14 @@ func (s *Session) createView(ctx context.Context, def *viewDefinition) (*View, * // Compute a prefix match, respecting segment boundaries, by ensuring // the pattern (dir) has a trailing slash. dirPrefix := strings.TrimSuffix(string(def.folder.Dir), "/") + "/" - filterer := NewFilterer(def.folder.Options.DirectoryFilters) + pathIncluded := PathIncludeFunc(def.folder.Options.DirectoryFilters) skipPath = func(dir string) bool { uri := strings.TrimSuffix(string(protocol.URIFromPath(dir)), "/") // Note that the logic below doesn't handle the case where uri == // v.folder.Dir, because there is no point in excluding the entire // workspace folder! if rel := strings.TrimPrefix(uri, dirPrefix); rel != uri { - return filterer.Disallow(rel) + return !pathIncluded(rel) } return false } diff --git a/gopls/internal/cache/view.go b/gopls/internal/cache/view.go index fc1ac5724ed..6bb0ae8edeb 100644 --- a/gopls/internal/cache/view.go +++ b/gopls/internal/cache/view.go @@ -477,11 +477,11 @@ func (v *View) filterFunc() func(protocol.DocumentURI) bool { modcacheFilter := "-" + strings.TrimPrefix(filepath.ToSlash(pref), "/") filters = append(filters, modcacheFilter) } - filterer := NewFilterer(filters) + pathIncluded := PathIncludeFunc(filters) v._filterFunc = func(uri protocol.DocumentURI) bool { // Only filter relative to the configured root directory. if pathutil.InDir(folderDir, uri.Path()) { - return relPathExcludedByFilter(strings.TrimPrefix(uri.Path(), folderDir), filterer) + return relPathExcludedByFilter(strings.TrimPrefix(uri.Path(), folderDir), pathIncluded) } return false } @@ -1264,7 +1264,7 @@ func allFilesExcluded(files []string, filterFunc func(protocol.DocumentURI) bool return true } -func relPathExcludedByFilter(path string, filterer *Filterer) bool { +func relPathExcludedByFilter(path string, pathIncluded func(string) bool) bool { path = strings.TrimPrefix(filepath.ToSlash(path), "/") - return filterer.Disallow(path) + return !pathIncluded(path) } diff --git a/gopls/internal/cache/view_test.go b/gopls/internal/cache/view_test.go index 992a3d61828..46000191e42 100644 --- a/gopls/internal/cache/view_test.go +++ b/gopls/internal/cache/view_test.go @@ -90,14 +90,14 @@ func TestFilters(t *testing.T) { } for _, tt := range tests { - filterer := NewFilterer(tt.filters) + pathIncluded := PathIncludeFunc(tt.filters) for _, inc := range tt.included { - if relPathExcludedByFilter(inc, filterer) { + if relPathExcludedByFilter(inc, pathIncluded) { t.Errorf("filters %q excluded %v, wanted included", tt.filters, inc) } } for _, exc := range tt.excluded { - if !relPathExcludedByFilter(exc, filterer) { + if !relPathExcludedByFilter(exc, pathIncluded) { t.Errorf("filters %q included %v, wanted excluded", tt.filters, exc) } } diff --git a/gopls/internal/golang/workspace_symbol.go b/gopls/internal/golang/workspace_symbol.go index 89c144b9230..91c5ee22925 100644 --- a/gopls/internal/golang/workspace_symbol.go +++ b/gopls/internal/golang/workspace_symbol.go @@ -300,8 +300,7 @@ func collectSymbols(ctx context.Context, snapshots []*cache.Snapshot, matcherTyp // whether a URI is in any open workspace. folderURI := snapshot.Folder() - filters := snapshot.Options().DirectoryFilters - filterer := cache.NewFilterer(filters) + pathIncluded := cache.PathIncludeFunc(snapshot.Options().DirectoryFilters) folder := filepath.ToSlash(folderURI.Path()) var ( @@ -371,7 +370,7 @@ func collectSymbols(ctx context.Context, snapshots []*cache.Snapshot, matcherTyp uri := sp.Files[i] norm := filepath.ToSlash(uri.Path()) nm := strings.TrimPrefix(norm, folder) - if filterer.Disallow(nm) { + if !pathIncluded(nm) { continue } // Only scan each file once. diff --git a/gopls/internal/golang/workspace_symbol_test.go b/gopls/internal/golang/workspace_symbol_test.go index 4982b767754..fbfec8e1204 100644 --- a/gopls/internal/golang/workspace_symbol_test.go +++ b/gopls/internal/golang/workspace_symbol_test.go @@ -47,7 +47,7 @@ func TestParseQuery(t *testing.T) { } } -func TestFiltererDisallow(t *testing.T) { +func TestPathIncludeFunc(t *testing.T) { tests := []struct { filters []string included []string @@ -119,18 +119,24 @@ func TestFiltererDisallow(t *testing.T) { []string{"a/b/c.go", "bb"}, []string{"b/c/d.go", "b"}, }, + // golang/vscode-go#3692 + { + []string{"-**/foo", "+**/bar"}, + []string{"bar/a.go", "a/bar/b.go"}, + []string{"foo/a.go", "a/foo/b.go"}, + }, } for _, test := range tests { - filterer := cache.NewFilterer(test.filters) + pathIncluded := cache.PathIncludeFunc(test.filters) for _, inc := range test.included { - if filterer.Disallow(inc) { + if !pathIncluded(inc) { t.Errorf("Filters %v excluded %v, wanted included", test.filters, inc) } } for _, exc := range test.excluded { - if !filterer.Disallow(exc) { + if pathIncluded(exc) { t.Errorf("Filters %v included %v, wanted excluded", test.filters, exc) } } From 8fa586e1a64f1e145c3915541170e37228e69fe1 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Wed, 5 Mar 2025 19:53:51 -0500 Subject: [PATCH 229/309] internal/analysis: add function to delete a statement In gopls, for unused variables and for modernizers, there is a need to provide edits to delete a specific statement. To avoid duplication these will use this new DeleteStatemnt. This CL, the first of a series, is to install the new function and its signature, and provide tests. Immediatley following CLs will use the new function to replace existing code. Change-Id: I1213cfaf14e66eaa9a111b11094a536bb869b298 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655295 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/analysisinternal/analysis.go | 121 +++++++++++ internal/analysisinternal/analysis_test.go | 223 ++++++++++++++++++++- 2 files changed, 343 insertions(+), 1 deletion(-) diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 5eb7ac5a939..cc3b351708e 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -20,6 +20,8 @@ import ( "strings" "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal" ) @@ -487,3 +489,122 @@ func CanImport(from, to string) bool { } return true } + +// DeleteStmt returns the edits to remove stmt if it is contained +// in a BlockStmt, CaseClause, CommClause, or is the STMT in switch STMT; ... {...} +// The report function abstracts gopls' bug.Report. +func DeleteStmt(fset *token.FileSet, astFile *ast.File, stmt ast.Stmt, report func(string, token.Pos)) []analysis.TextEdit { + // TODO: pass in the cursor to a ast.Stmt. callers should provide the Cursor + insp := inspector.New([]*ast.File{astFile}) + root := cursor.Root(insp) + cstmt, ok := root.FindNode(stmt) + if !ok { + report("%s not found in file", stmt.Pos()) + return nil + } + // some paranoia + if !stmt.Pos().IsValid() || !stmt.End().IsValid() { + report("%s: stmt has invalid position", stmt.Pos()) + return nil + } + + // if the stmt is on a line by itself delete the whole line + // otherwise just delete the statement. + + // this logic would be a lot simpler with the file contents, and somewhat simpler + // if the cursors included the comments. + + tokFile := fset.File(stmt.Pos()) + lineOf := tokFile.Line + stmtStartLine, stmtEndLine := lineOf(stmt.Pos()), lineOf(stmt.End()) + + var from, to token.Pos + // bounds of adjacent syntax/comments on same line, if any + limits := func(left, right token.Pos) { + if lineOf(left) == stmtStartLine { + from = left + } + if lineOf(right) == stmtEndLine { + to = right + } + } + // TODO(pjw): there are other places a statement might be removed: + // IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] . + // (removing the blocks requires more rewriting than this routine would do) + // CommCase = "case" ( SendStmt | RecvStmt ) | "default" . + // (removing the stmt requires more rewriting, and it's unclear what the user means) + switch parent := cstmt.Parent().Node().(type) { + case *ast.SwitchStmt: + limits(parent.Switch, parent.Body.Lbrace) + case *ast.TypeSwitchStmt: + limits(parent.Switch, parent.Body.Lbrace) + if parent.Assign == stmt { + return nil // don't let the user break the type switch + } + case *ast.BlockStmt: + limits(parent.Lbrace, parent.Rbrace) + case *ast.CommClause: + limits(parent.Colon, cstmt.Parent().Parent().Node().(*ast.BlockStmt).Rbrace) + if parent.Comm == stmt { + return nil // maybe the user meant to remove the entire CommClause? + } + case *ast.CaseClause: + limits(parent.Colon, cstmt.Parent().Parent().Node().(*ast.BlockStmt).Rbrace) + case *ast.ForStmt: + limits(parent.For, parent.Body.Lbrace) + + default: + return nil // not one of ours + } + + if prev, found := cstmt.PrevSibling(); found && lineOf(prev.Node().End()) == stmtStartLine { + from = prev.Node().End() // preceding statement ends on same line + } + if next, found := cstmt.NextSibling(); found && lineOf(next.Node().Pos()) == stmtEndLine { + to = next.Node().Pos() // following statement begins on same line + } + // and now for the comments +Outer: + for _, cg := range astFile.Comments { + for _, co := range cg.List { + if lineOf(co.End()) < stmtStartLine { + continue + } else if lineOf(co.Pos()) > stmtEndLine { + break Outer // no more are possible + } + if lineOf(co.End()) == stmtStartLine && co.End() < stmt.Pos() { + if !from.IsValid() || co.End() > from { + from = co.End() + continue // maybe there are more + } + } + if lineOf(co.Pos()) == stmtEndLine && co.Pos() > stmt.End() { + if !to.IsValid() || co.Pos() < to { + to = co.Pos() + continue // maybe there are more + } + } + } + } + // if either from or to is valid, just remove the statement + // otherwise remove the line + edit := analysis.TextEdit{Pos: stmt.Pos(), End: stmt.End()} + if from.IsValid() || to.IsValid() { + // remove just the statment. + // we can't tell if there is a ; or whitespace right after the statment + // ideally we'd like to remove the former and leave the latter + // (if gofmt has run, there likely won't be a ;) + // In type switches we know there's a semicolon somewhere after the statement, + // but the extra work for this special case is not worth it, as gofmt will fix it. + return []analysis.TextEdit{edit} + } + // remove the whole line + for lineOf(edit.Pos) == stmtStartLine { + edit.Pos-- + } + edit.Pos++ // get back tostmtStartLine + for lineOf(edit.End) == stmtEndLine { + edit.End++ + } + return []analysis.TextEdit{edit} +} diff --git a/internal/analysisinternal/analysis_test.go b/internal/analysisinternal/analysis_test.go index 0b21876d386..530e57250c2 100644 --- a/internal/analysisinternal/analysis_test.go +++ b/internal/analysisinternal/analysis_test.go @@ -4,7 +4,15 @@ package analysisinternal -import "testing" +import ( + "go/ast" + "go/parser" + "go/token" + "testing" + + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/astutil/cursor" +) func TestCanImport(t *testing.T) { for _, tt := range []struct { @@ -32,3 +40,216 @@ func TestCanImport(t *testing.T) { } } } + +func TestDeleteStmt(t *testing.T) { + type testCase struct { + in string + which int // count of ast.Stmt in ast.Inspect traversal to remove + want string + name string // should contain exactly one of [block,switch,case,comm,for,type] + } + tests := []testCase{ + { // do nothing when asked to remove a function body + in: "package p; func f() { }", + which: 0, + want: "package p; func f() { }", + name: "block0", + }, + { + in: "package p; func f() { abcd()}", + which: 1, + want: "package p; func f() { }", + name: "block1", + }, + { + in: "package p; func f() { a() }", + which: 1, + want: "package p; func f() { }", + name: "block2", + }, + { + in: "package p; func f() { a();}", + which: 1, + want: "package p; func f() { ;}", + name: "block3", + }, + { + in: "package p; func f() {\n a() \n\n}", + which: 1, + want: "package p; func f() {\n\n}", + name: "block4", + }, + { + in: "package p; func f() { a()// comment\n}", + which: 1, + want: "package p; func f() { // comment\n}", + name: "block5", + }, + { + in: "package p; func f() { /*c*/a() \n}", + which: 1, + want: "package p; func f() { /*c*/ \n}", + name: "block6", + }, + { + in: "package p; func f() { a();b();}", + which: 2, + want: "package p; func f() { a();;}", + name: "block7", + }, + { + in: "package p; func f() {\n\ta()\n\tb()\n}", + which: 2, + want: "package p; func f() {\n\ta()\n}", + name: "block8", + }, + { + in: "package p; func f() {\n\ta()\n\tb()\n\tc()\n}", + which: 2, + want: "package p; func f() {\n\ta()\n\tc()\n}", + name: "block9", + }, + { + in: "package p\nfunc f() {a()+b()}", + which: 1, + want: "package p\nfunc f() {}", + name: "block10", + }, + { + in: "package p\nfunc f() {(a()+b())}", + which: 1, + want: "package p\nfunc f() {}", + name: "block11", + }, + { + in: "package p; func f() { switch a(); b() {}}", + which: 2, // 0 is the func body, 1 is the switch statement + want: "package p; func f() { switch ; b() {}}", + name: "switch0", + }, + { + in: "package p; func f() { switch /*c*/a(); {}}", + which: 2, // 0 is the func body, 1 is the switch statement + want: "package p; func f() { switch /*c*/; {}}", + name: "switch1", + }, + { + in: "package p; func f() { switch a()/*c*/; {}}", + which: 2, // 0 is the func body, 1 is the switch statement + want: "package p; func f() { switch /*c*/; {}}", + name: "switch2", + }, + { + in: "package p; func f() { select {default: a()}}", + which: 4, // 0 is the func body, 1 is the select statement, 2 is its body, 3 is the comm clause + want: "package p; func f() { select {default: }}", + name: "comm0", + }, + { + in: "package p; func f(x chan any) { select {case x <- a: a(x)}}", + which: 5, // 0 is the func body, 1 is the select statement, 2 is its body, 3 is the comm clause + want: "package p; func f(x chan any) { select {case x <- a: }}", + name: "comm1", + }, + { + in: "package p; func f(x chan any) { select {case x <- a: a(x)}}", + which: 4, // 0 is the func body, 1 is the select statement, 2 is its body, 3 is the comm clause + want: "package p; func f(x chan any) { select {case x <- a: a(x)}}", + name: "comm2", + }, + { + in: "package p; func f() { switch {default: a()}}", + which: 4, // 0 is the func body, 1 is the select statement, 2 is its body + want: "package p; func f() { switch {default: }}", + name: "case0", + }, + { + in: "package p; func f() { switch {case 3: a()}}", + which: 4, // 0 is the func body, 1 is the select statement, 2 is its body + want: "package p; func f() { switch {case 3: }}", + name: "case1", + }, + { + in: "package p; func f() {for a();;b() {}}", + which: 2, + want: "package p; func f() {for ;;b() {}}", + name: "for0", + }, + { + in: "package p; func f() {for a();c();b() {}}", + which: 3, + want: "package p; func f() {for a();c(); {}}", + name: "for1", + }, + { + in: "package p; func f() {for\na();c()\nb() {}}", + which: 2, + want: "package p; func f() {for\n;c()\nb() {}}", + name: "for2", + }, + { + in: "package p; func f() {for a();\nc();b() {}}", + which: 3, + want: "package p; func f() {for a();\nc(); {}}", + name: "for3", + }, + { + in: "package p; func f() {switch a();b().(type){}}", + which: 2, + want: "package p; func f() {switch ;b().(type){}}", + name: "type0", + }, + { + in: "package p; func f() {switch a();b().(type){}}", + which: 3, + want: "package p; func f() {switch a();b().(type){}}", + name: "type1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, tt.name, tt.in, parser.ParseComments) + if err != nil { + t.Fatalf("%s: %v", tt.name, err) + } + insp := inspector.New([]*ast.File{f}) + root := cursor.Root(insp) + var stmt cursor.Cursor + cnt := 0 + for cn := range root.Preorder() { // Preorder(ast.Stmt(nil)) doesn't work + if _, ok := cn.Node().(ast.Stmt); !ok { + continue + } + if cnt == tt.which { + stmt = cn + break + } + cnt++ + } + if cnt != tt.which { + t.Fatalf("test %s does not contain desired statement %d", tt.name, tt.which) + } + edits := DeleteStmt(fset, f, stmt.Node().(ast.Stmt), nil) + if tt.want == tt.in { + if len(edits) != 0 { + t.Fatalf("%s: got %d edits, expected 0", tt.name, len(edits)) + } + return + } + if len(edits) != 1 { + t.Fatalf("%s: got %d edits, expected 1", tt.name, len(edits)) + } + tokFile := fset.File(f.Pos()) + + left := tokFile.Offset(edits[0].Pos) + right := tokFile.Offset(edits[0].End) + + got := tt.in[:left] + tt.in[right:] + if got != tt.want { + t.Errorf("%s: got\n%q, want\n%q", tt.name, got, tt.want) + } + }) + + } +} From 5a45ac2d4cce31a435c1c9436717ae061d981e23 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Fri, 7 Mar 2025 18:34:55 +0800 Subject: [PATCH 230/309] x/tools: use range over function for some API This CL tracks the todo to use range over function when go1.23 is assured. Change-Id: Iee685ce89571443443d21e6991d018c13a9c2af2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655776 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- go/analysis/internal/checker/checker.go | 13 +++++-------- go/callgraph/vta/graph.go | 3 ++- go/callgraph/vta/graph_test.go | 10 ++++++---- go/callgraph/vta/propagation.go | 10 ++++------ go/callgraph/vta/propagation_test.go | 11 ++++------- go/ssa/func.go | 4 ++-- go/ssa/sanity.go | 6 ++---- 7 files changed, 25 insertions(+), 32 deletions(-) diff --git a/go/analysis/internal/checker/checker.go b/go/analysis/internal/checker/checker.go index 2a9ff2931b3..bc57dc6e673 100644 --- a/go/analysis/internal/checker/checker.go +++ b/go/analysis/internal/checker/checker.go @@ -242,15 +242,14 @@ func printDiagnostics(graph *checker.Graph) (exitcode int) { // Compute the exit code. var numErrors, rootDiags int - // TODO(adonovan): use "for act := range graph.All() { ... }" in go1.23. - graph.All()(func(act *checker.Action) bool { + for act := range graph.All() { if act.Err != nil { numErrors++ } else if act.IsRoot { rootDiags += len(act.Diagnostics) } - return true - }) + } + if numErrors > 0 { exitcode = 1 // analysis failed, at least partially } else if rootDiags > 0 { @@ -266,12 +265,10 @@ func printDiagnostics(graph *checker.Graph) (exitcode int) { var list []*checker.Action var total time.Duration - // TODO(adonovan): use "for act := range graph.All() { ... }" in go1.23. - graph.All()(func(act *checker.Action) bool { + for act := range graph.All() { list = append(list, act) total += act.Duration - return true - }) + } // Print actions accounting for 90% of the total. sort.Slice(list, func(i, j int) bool { diff --git a/go/callgraph/vta/graph.go b/go/callgraph/vta/graph.go index 164018708ef..26225e7db37 100644 --- a/go/callgraph/vta/graph.go +++ b/go/callgraph/vta/graph.go @@ -8,6 +8,7 @@ import ( "fmt" "go/token" "go/types" + "iter" "golang.org/x/tools/go/ssa" "golang.org/x/tools/go/types/typeutil" @@ -270,7 +271,7 @@ func (g *vtaGraph) numNodes() int { return len(g.idx) } -func (g *vtaGraph) successors(x idx) func(yield func(y idx) bool) { +func (g *vtaGraph) successors(x idx) iter.Seq[idx] { return func(yield func(y idx) bool) { for y := range g.m[x] { if !yield(y) { diff --git a/go/callgraph/vta/graph_test.go b/go/callgraph/vta/graph_test.go index 9e780c7e4e2..725749ea6ab 100644 --- a/go/callgraph/vta/graph_test.go +++ b/go/callgraph/vta/graph_test.go @@ -148,7 +148,9 @@ func TestVtaGraph(t *testing.T) { {n4, 0}, } { sl := 0 - g.successors(g.idx[test.n])(func(_ idx) bool { sl++; return true }) + for range g.successors(g.idx[test.n]) { + sl++ + } if sl != test.l { t.Errorf("want %d successors; got %d", test.l, sl) } @@ -163,10 +165,10 @@ func vtaGraphStr(g *vtaGraph) []string { var vgs []string for n := 0; n < g.numNodes(); n++ { var succStr []string - g.successors(idx(n))(func(s idx) bool { + for s := range g.successors(idx(n)) { succStr = append(succStr, g.node[s].String()) - return true - }) + } + sort.Strings(succStr) entry := fmt.Sprintf("%v -> %v", g.node[n].String(), strings.Join(succStr, ", ")) vgs = append(vgs, removeModulePrefix(entry)) diff --git a/go/callgraph/vta/propagation.go b/go/callgraph/vta/propagation.go index 1c4dcd2888e..a71c5b0034a 100644 --- a/go/callgraph/vta/propagation.go +++ b/go/callgraph/vta/propagation.go @@ -42,7 +42,7 @@ func scc(g *vtaGraph) (sccs [][]idx, idxToSccID []int) { *ns = state{pre: nextPre, lowLink: nextPre, onStack: true} stack = append(stack, n) - g.successors(n)(func(s idx) bool { + for s := range g.successors(n) { if ss := &states[s]; ss.pre == 0 { // Analyze successor s that has not been visited yet. doSCC(s) @@ -52,8 +52,7 @@ func scc(g *vtaGraph) (sccs [][]idx, idxToSccID []int) { // in the current SCC. ns.lowLink = min(ns.lowLink, ss.pre) } - return true - }) + } // if n is a root node, pop the stack and generate a new SCC. if ns.lowLink == ns.pre { @@ -166,10 +165,9 @@ func propagate(graph *vtaGraph, canon *typeutil.Map) propTypeMap { for i := len(sccs) - 1; i >= 0; i-- { nextSccs := make(map[int]empty) for _, n := range sccs[i] { - graph.successors(n)(func(succ idx) bool { + for succ := range graph.successors(n) { nextSccs[idxToSccID[succ]] = empty{} - return true - }) + } } // Propagate types to all successor SCCs. for nextScc := range nextSccs { diff --git a/go/callgraph/vta/propagation_test.go b/go/callgraph/vta/propagation_test.go index bc9ca1ecde6..2b36cf39bb7 100644 --- a/go/callgraph/vta/propagation_test.go +++ b/go/callgraph/vta/propagation_test.go @@ -123,17 +123,14 @@ func sccEqual(sccs1 []string, sccs2 []string) bool { // // for every edge x -> y in g, nodeToScc[x] > nodeToScc[y] func isRevTopSorted(g *vtaGraph, idxToScc []int) bool { - result := true - for n := 0; n < len(idxToScc); n++ { - g.successors(idx(n))(func(s idx) bool { + for n := range idxToScc { + for s := range g.successors(idx(n)) { if idxToScc[n] < idxToScc[s] { - result = false return false } - return true - }) + } } - return result + return true } func sccMapsConsistent(sccs [][]idx, idxToSccID []int) bool { diff --git a/go/ssa/func.go b/go/ssa/func.go index 010c128a9ec..a6e6b149fd9 100644 --- a/go/ssa/func.go +++ b/go/ssa/func.go @@ -13,6 +13,7 @@ import ( "go/token" "go/types" "io" + "iter" "os" "strings" @@ -187,8 +188,7 @@ func targetedBlock(f *Function, tok token.Token) *BasicBlock { } // instrs returns an iterator that returns each reachable instruction of the SSA function. -// TODO: return an iter.Seq once x/tools is on 1.23 -func (f *Function) instrs() func(yield func(i Instruction) bool) { +func (f *Function) instrs() iter.Seq[Instruction] { return func(yield func(i Instruction) bool) { for _, block := range f.Blocks { for _, instr := range block.Instrs { diff --git a/go/ssa/sanity.go b/go/ssa/sanity.go index 97ef886e3cf..3b862992680 100644 --- a/go/ssa/sanity.go +++ b/go/ssa/sanity.go @@ -529,12 +529,10 @@ func (s *sanity) checkFunction(fn *Function) bool { // Build the set of valid referrers. s.instrs = make(map[Instruction]unit) - // TODO: switch to range-over-func when x/tools updates to 1.23. // instrs are the instructions that are present in the function. - fn.instrs()(func(instr Instruction) bool { + for instr := range fn.instrs() { s.instrs[instr] = unit{} - return true - }) + } // Check all Locals allocations appear in the function instruction. for i, l := range fn.Locals { From 03f197e9708232c1425b20c5a26d25da07e31df4 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Sat, 1 Mar 2025 21:44:58 -0500 Subject: [PATCH 231/309] gopls/internal/modernize: remove assignment in ranges As of go1.22, the var := var idiom as the first statement of a for range statement is no longer necessary. This modernizer will remove it. Change-Id: Ia3102bc221540de962d5e357a3eb21eaf8feac4b Reviewed-on: https://go-review.googlesource.com/c/tools/+/654035 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/go.sum | 1 + gopls/internal/analysis/modernize/forvar.go | 117 ++++++++++++++++++ .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + .../modernize/testdata/src/forvar/forvar.go | 62 ++++++++++ .../testdata/src/forvar/forvar.go.golden | 62 ++++++++++ internal/analysisinternal/analysis.go | 2 +- 7 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 gopls/internal/analysis/modernize/forvar.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go.golden diff --git a/gopls/go.sum b/gopls/go.sum index 20633541388..5a7914737a4 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -15,6 +15,7 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGKbLGBPtR/8/oO74W6hmz0qE5q0z9aqSAewaaM= github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/dl v0.0.0-20250211172903-ae3823a6a0a3/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa h1:Br3+0EZZohShrmVVc85znGpxw7Ca8hsUJlrdT/JQGw8= diff --git a/gopls/internal/analysis/modernize/forvar.go b/gopls/internal/analysis/modernize/forvar.go new file mode 100644 index 00000000000..3a7eee4be9c --- /dev/null +++ b/gopls/internal/analysis/modernize/forvar.go @@ -0,0 +1,117 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "go/ast" + "go/token" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/gopls/internal/util/bug" + "golang.org/x/tools/internal/analysisinternal" +) + +// forvar offers to fix unnecessary copying of a for variable +// +// for _, x := range foo { +// x := x // offer to remove this superfluous assignment +// } +// +// Prerequisites: +// First statement in a range loop has to be := +// where the two idents are the same, +// and the ident is defined (:=) as a variable in the for statement. +// (Note that this 'fix' does not work for three clause loops +// because the Go specification says "The variable used by each subsequent iteration +// is declared implicitly before executing the post statement and initialized to the +// value of the previous iteration's variable at that moment.") +func forvar(pass *analysis.Pass) { + info := pass.TypesInfo + + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.22") { + for curLoop := range curFile.Preorder((*ast.RangeStmt)(nil)) { + // in a range loop. Is the first statement var := var? + // if so, is var one of the range vars, and is it defined + // in the for statement? + // If so, decide how much to delete. + loop := curLoop.Node().(*ast.RangeStmt) + if loop.Tok != token.DEFINE { + continue + } + v, stmt := loopVarRedecl(loop.Body) + if v == nil { + continue // index is not redeclared + } + if (loop.Key == nil || !equalSyntax(loop.Key, v)) && + (loop.Value == nil || !equalSyntax(loop.Value, v)) { + continue + } + astFile := curFile.Node().(*ast.File) + edits := analysisinternal.DeleteStmt(pass.Fset, astFile, stmt, bug.Reportf) + if len(edits) == 0 { + bug.Reportf("forvar failed to delete statement") + continue + } + remove := edits[0] + diag := analysis.Diagnostic{ + Pos: remove.Pos, + End: remove.End, + Category: "forvar", + Message: "copying variable is unneeded", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Remove unneeded redeclaration", + TextEdits: []analysis.TextEdit{remove}, + }}, + } + pass.Report(diag) + } + } +} + +// if the expression is an Ident, return its name +func simplevar(expr ast.Expr) string { + if expr == nil { + return "" + } + if ident, ok := expr.(*ast.Ident); ok { + return ident.Name + } + return "" +} + +func usefulRangeVars(loop *ast.RangeStmt) []string { + ans := make([]string, 0, 2) + if v := simplevar(loop.Key); v != "" { + ans = append(ans, v) + } + if v := simplevar(loop.Value); v != "" { + ans = append(ans, v) + } + return ans +} + +// if the first statement is var := var, return var and the stmt +func loopVarRedecl(body *ast.BlockStmt) (*ast.Ident, *ast.AssignStmt) { + if len(body.List) < 1 { + return nil, nil + } + stmt, ok := body.List[0].(*ast.AssignStmt) + if !ok || !isSimpleAssign(stmt) || stmt.Tok != token.DEFINE { + return nil, nil + } + if _, ok := stmt.Lhs[0].(*ast.Ident); !ok { + return nil, nil + } + if _, ok := stmt.Rhs[0].(*ast.Ident); !ok { + return nil, nil + } + if stmt.Lhs[0].(*ast.Ident).Name == stmt.Rhs[0].(*ast.Ident).Name { + return stmt.Lhs[0].(*ast.Ident), stmt + } + return nil, nil +} diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index fb7d43eb8d7..5dd94a82a6b 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -80,6 +80,7 @@ func run(pass *analysis.Pass) (any, error) { bloop(pass) efaceany(pass) fmtappendf(pass) + forvar(pass) mapsloop(pass) minmax(pass) omitzero(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 7bdc8014389..f9727d1e253 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -17,6 +17,7 @@ func Test(t *testing.T) { "bloop", "efaceany", "fmtappendf", + "forvar", "mapsloop", "minmax", "omitzero", diff --git a/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go b/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go new file mode 100644 index 00000000000..dd5ecd75e29 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go @@ -0,0 +1,62 @@ +package forvar + +func _(m map[int]int, s []int) { + // changed + for i := range s { + i := i // want "copying variable is unneeded" + go f(i) + } + for _, v := range s { + v := v // want "copying variable is unneeded" + go f(v) + } + for k, v := range m { + k := k // want "copying variable is unneeded" + v := v // nope: report only the first redeclaration + go f(k) + go f(v) + } + for _, v := range m { + v := v // want "copying variable is unneeded" + go f(v) + } + for i := range s { + /* hi */ i := i // want "copying variable is unneeded" + go f(i) + } + // nope + var i, k, v int + + for i = range s { // nope, scope change + i := i + go f(i) + } + for _, v = range s { // nope, scope change + v := v + go f(v) + } + for k = range m { // nope, scope change + k := k + go f(k) + } + for k, v = range m { // nope, scope change + k := k + v := v + go f(k) + go f(v) + } + for _, v = range m { // nope, scope change + v := v + go f(v) + } + for _, v = range m { // nope, not x := x + v := i + go f(v) + } + for i := range s { + i := (i) + go f(i) + } +} + +func f(n int) {} diff --git a/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go.golden b/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go.golden new file mode 100644 index 00000000000..35f71404c35 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/forvar/forvar.go.golden @@ -0,0 +1,62 @@ +package forvar + +func _(m map[int]int, s []int) { + // changed + for i := range s { + // want "copying variable is unneeded" + go f(i) + } + for _, v := range s { + // want "copying variable is unneeded" + go f(v) + } + for k, v := range m { + // want "copying variable is unneeded" + v := v // nope: report only the first redeclaration + go f(k) + go f(v) + } + for _, v := range m { + // want "copying variable is unneeded" + go f(v) + } + for i := range s { + /* hi */ // want "copying variable is unneeded" + go f(i) + } + // nope + var i, k, v int + + for i = range s { // nope, scope change + i := i + go f(i) + } + for _, v = range s { // nope, scope change + v := v + go f(v) + } + for k = range m { // nope, scope change + k := k + go f(k) + } + for k, v = range m { // nope, scope change + k := k + v := v + go f(k) + go f(v) + } + for _, v = range m { // nope, scope change + v := v + go f(v) + } + for _, v = range m { // nope, not x := x + v := i + go f(v) + } + for i := range s { + i := (i) + go f(i) + } +} + +func f(n int) {} diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index cc3b351708e..69e21a14ca9 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -493,7 +493,7 @@ func CanImport(from, to string) bool { // DeleteStmt returns the edits to remove stmt if it is contained // in a BlockStmt, CaseClause, CommClause, or is the STMT in switch STMT; ... {...} // The report function abstracts gopls' bug.Report. -func DeleteStmt(fset *token.FileSet, astFile *ast.File, stmt ast.Stmt, report func(string, token.Pos)) []analysis.TextEdit { +func DeleteStmt(fset *token.FileSet, astFile *ast.File, stmt ast.Stmt, report func(string, ...any)) []analysis.TextEdit { // TODO: pass in the cursor to a ast.Stmt. callers should provide the Cursor insp := inspector.New([]*ast.File{astFile}) root := cursor.Root(insp) From cc7d6983044e11af79ce7fd42729be1714961587 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 10 Mar 2025 20:44:31 +0000 Subject: [PATCH 232/309] gopls/internal/test/integration/misc: fix TestAssembly for CL 639515 To avoid the test failure in CL 639515, where the naming of closures functions is being revisited, loosen the checked conditions of TestAssembly. Change-Id: I8b44a76d7fe72f8747db1c8130c8db52542d25f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/656456 Reviewed-by: Alan Donovan Auto-Submit: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/test/integration/misc/webserver_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gopls/internal/test/integration/misc/webserver_test.go b/gopls/internal/test/integration/misc/webserver_test.go index 79a6548ee3e..691d45baa6e 100644 --- a/gopls/internal/test/integration/misc/webserver_test.go +++ b/gopls/internal/test/integration/misc/webserver_test.go @@ -589,13 +589,15 @@ func init() { checkMatch(t, true, report, `CALL runtime.printlock`) checkMatch(t, true, report, `CALL runtime.printstring`) checkMatch(t, true, report, `CALL runtime.printunlock`) - checkMatch(t, true, report, `CALL example.com/a.f.deferwrap1`) + checkMatch(t, true, report, `CALL example.com/a.f.deferwrap`) checkMatch(t, true, report, `RET`) checkMatch(t, true, report, `CALL runtime.morestack_noctxt`) } // Nested functions are also shown. - checkMatch(t, true, report, `TEXT.*example.com/a.f.deferwrap1`) + // + // The condition here was relaxed to unblock go.dev/cl/639515. + checkMatch(t, true, report, `example.com/a.f.deferwrap`) // But other functions are not. checkMatch(t, false, report, `TEXT.*example.com/a.g`) From 381d68d88c9845fe24f00f8b5d6c6f23aa8a56df Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 7 Mar 2025 15:05:04 -0500 Subject: [PATCH 233/309] gopls/internal/util/fingerprint/fingerprint: unify type params Enhance Matches to observe the bindings of type parameters. Previously, each occurrence of a type parameter matched any type. For example, matching these two signatures: func f[T any](T, T) func g(int, bool) succeeded even though g is not an instantiation of f. This CL tracks the bindings of type parameters, so that the above match fails but matching f with this function: func h(int, int) succeeds. Change-Id: Ia1ed653b24168d8e307593ca98d7c151b9dbb458 Reviewed-on: https://go-review.googlesource.com/c/tools/+/655995 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- .../internal/util/fingerprint/fingerprint.go | 132 ++++++++++++++---- .../util/fingerprint/fingerprint_test.go | 58 ++++++-- 2 files changed, 151 insertions(+), 39 deletions(-) diff --git a/gopls/internal/util/fingerprint/fingerprint.go b/gopls/internal/util/fingerprint/fingerprint.go index 2b657ba7857..22817e4cb2f 100644 --- a/gopls/internal/util/fingerprint/fingerprint.go +++ b/gopls/internal/util/fingerprint/fingerprint.go @@ -338,44 +338,126 @@ func writeSexpr(out *strings.Builder, x sexpr) { } } -// unify reports whether the types of methods x and y match, in the -// presence of type parameters, each of which matches anything at all. -// (It's not true unification as we don't track substitutions.) -// -// TODO(adonovan): implement full unification. +// unify reports whether x and y match, in the presence of type parameters. +// The constraints on type parameters are ignored, but each type parameter must +// have a consistent binding. func unify(x, y sexpr) bool { - if isTypeParam(x) >= 0 || isTypeParam(y) >= 0 { - return true // a type parameter matches anything + + // maxTypeParam returns the maximum type parameter index in x. + var maxTypeParam func(x sexpr) int + maxTypeParam = func(x sexpr) int { + if i := typeParamIndex(x); i >= 0 { + return i + } + if c, ok := x.(*cons); ok { + return max(maxTypeParam(c.car), maxTypeParam(c.cdr)) + } + return 0 } - if reflect.TypeOf(x) != reflect.TypeOf(y) { - return false // type mismatch + + // xBindings[i] is the binding for type parameter #i in x, and similarly for y. + // Although type parameters are nominally bound to sexprs, each bindings[i] + // is a *sexpr, so unbound variables can share a binding. + xBindings := make([]*sexpr, maxTypeParam(x)+1) + for i := range len(xBindings) { + xBindings[i] = new(sexpr) } - switch x := x.(type) { - case nil, string, int, symbol: - return x == y - case *cons: - y := y.(*cons) - if !unify(x.car, y.car) { + yBindings := make([]*sexpr, maxTypeParam(y)+1) + for i := range len(yBindings) { + yBindings[i] = new(sexpr) + } + + // bind sets binding b to s from bindings if it does not occur in s. + bind := func(b *sexpr, s sexpr, bindings []*sexpr) bool { + // occurs reports whether b is present in s. + var occurs func(s sexpr) bool + occurs = func(s sexpr) bool { + if j := typeParamIndex(s); j >= 0 { + return b == bindings[j] + } + if c, ok := s.(*cons); ok { + return occurs(c.car) || occurs(c.cdr) + } return false } - if x.cdr == nil { - return y.cdr == nil - } - if y.cdr == nil { + + if occurs(s) { return false } - return unify(x.cdr, y.cdr) - default: - panic(fmt.Sprintf("unify %T %T", x, y)) + *b = s + return true + } + + var uni func(x, y sexpr) bool + uni = func(x, y sexpr) bool { + var bx, by *sexpr + ix := typeParamIndex(x) + if ix >= 0 { + bx = xBindings[ix] + } + iy := typeParamIndex(y) + if iy >= 0 { + by = yBindings[iy] + } + + if bx != nil || by != nil { + // If both args are type params and neither is bound, have them share a binding. + if bx != nil && by != nil && *bx == nil && *by == nil { + xBindings[ix] = yBindings[iy] + return true + } + // Treat param bindings like original args in what follows. + if bx != nil && *bx != nil { + x = *bx + } + if by != nil && *by != nil { + y = *by + } + // If the x param is unbound, bind it to y. + if bx != nil && *bx == nil { + return bind(bx, y, yBindings) + } + // If the y param is unbound, bind it to x. + if by != nil && *by == nil { + return bind(by, x, xBindings) + } + // Unify the binding of a bound parameter. + return uni(x, y) + } + + // Neither arg is a type param. + if reflect.TypeOf(x) != reflect.TypeOf(y) { + return false // type mismatch + } + switch x := x.(type) { + case nil, string, int, symbol: + return x == y + case *cons: + y := y.(*cons) + if !uni(x.car, y.car) { + return false + } + if x.cdr == nil { + return y.cdr == nil + } + if y.cdr == nil { + return false + } + return uni(x.cdr, y.cdr) + default: + panic(fmt.Sprintf("unify %T %T", x, y)) + } } + // At least one param is bound. Unify its binding with the other. + return uni(x, y) } -// isTypeParam returns the index of the type parameter, +// typeParamIndex returns the index of the type parameter, // if x has the form "(typeparam INTEGER)", otherwise -1. -func isTypeParam(x sexpr) int { +func typeParamIndex(x sexpr) int { if x, ok := x.(*cons); ok { if sym, ok := x.car.(symbol); ok && sym == symTypeparam { - return 0 + return x.cdr.(*cons).car.(int) } } return -1 diff --git a/gopls/internal/util/fingerprint/fingerprint_test.go b/gopls/internal/util/fingerprint/fingerprint_test.go index 7a7a2fe7569..737c6896157 100644 --- a/gopls/internal/util/fingerprint/fingerprint_test.go +++ b/gopls/internal/util/fingerprint/fingerprint_test.go @@ -104,6 +104,7 @@ func C2[U any](int, int, ...U) bool { panic(0) } func C3(int, bool, ...string) rune func C4(int, bool, ...string) func C5(int, float64, bool, string) bool +func C6(int, bool, ...string) bool func DAny[T any](Named[T]) { panic(0) } func DString(Named[string]) @@ -114,6 +115,17 @@ type Named[T any] struct { x T } func E1(byte) rune func E2(uint8) int32 func E3(int8) uint32 + +// generic vs. generic +func F1[T any](T) { panic(0) } +func F2[T any](*T) { panic(0) } +func F3[T any](T, T) { panic(0) } +func F4[U any](U, *U) {panic(0) } +func F5[T, U any](T, U, U) { panic(0) } +func F6[T any](T, int, T) { panic(0) } +func F7[T any](bool, T, T) { panic(0) } +func F8[V any](*V, int, int) { panic(0) } +func F9[V any](V, *V, V) { panic(0) } ` pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] scope := pkg.Types.Scope() @@ -128,11 +140,12 @@ func E3(int8) uint32 {"B", "String", "", true}, {"B", "Int", "", true}, {"B", "A", "", true}, - {"C1", "C2", "", true}, // matches despite inconsistent substitution - {"C1", "C3", "", true}, + {"C1", "C2", "", false}, + {"C1", "C3", "", false}, {"C1", "C4", "", false}, {"C1", "C5", "", false}, - {"C2", "C3", "", false}, // intransitive (C1≡C2 ^ C1≡C3) + {"C1", "C6", "", true}, + {"C2", "C3", "", false}, {"C2", "C4", "", false}, {"C3", "C4", "", false}, {"DAny", "DString", "", true}, @@ -140,6 +153,13 @@ func E3(int8) uint32 {"DString", "DInt", "", false}, // different instantiations of Named {"E1", "E2", "", true}, // byte and rune are just aliases {"E2", "E3", "", false}, + // The following tests cover all of the type param cases of unify. + {"F1", "F2", "", true}, // F1[*int] = F2[int] + {"F3", "F4", "", false}, // would require U identical to *U, prevented by occur check + {"F5", "F6", "", true}, // one param is bound, the other is not + {"F6", "F7", "", false}, // both are bound + {"F5", "F8", "", true}, // T=*int, U=int, V=int + {"F5", "F9", "", false}, // T is unbound, V is bound, and T occurs in V } { lookup := func(name string) types.Type { obj := scope.Lookup(name) @@ -155,20 +175,30 @@ func E3(int8) uint32 return obj.Type() } - a := lookup(test.a) - b := lookup(test.b) + check := func(sa, sb string, want bool) { + t.Helper() + + a := lookup(sa) + b := lookup(sb) - afp, _ := fingerprint.Encode(a) - bfp, _ := fingerprint.Encode(b) + afp, _ := fingerprint.Encode(a) + bfp, _ := fingerprint.Encode(b) - atree := fingerprint.Parse(afp) - btree := fingerprint.Parse(bfp) + atree := fingerprint.Parse(afp) + btree := fingerprint.Parse(bfp) - got := fingerprint.Matches(atree, btree) - if got != test.want { - t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", - test.a, test.b, test.method, - got, atree, btree) + got := fingerprint.Matches(atree, btree) + if got != want { + t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", + sa, sb, test.method, got, a, b) + } } + + check(test.a, test.b, test.want) + // Matches is symmetric + check(test.b, test.a, test.want) + // Matches is reflexive + check(test.a, test.a, true) + check(test.b, test.b, true) } } From bf70295789942e4b20ca70a8cd2fe1f3ca2a70bd Mon Sep 17 00:00:00 2001 From: Sean Liao Date: Mon, 10 Mar 2025 22:23:15 +0000 Subject: [PATCH 234/309] cmd/go-contrib-init: drop unneeded GOPATH checks in module mode Fixes golang/go#72773 Change-Id: I72728446de0e7ddb01c2219523533e7f7f0cb910 Reviewed-on: https://go-review.googlesource.com/c/tools/+/656515 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Ian Lance Taylor --- cmd/go-contrib-init/contrib.go | 38 ---------------------------------- 1 file changed, 38 deletions(-) diff --git a/cmd/go-contrib-init/contrib.go b/cmd/go-contrib-init/contrib.go index 9254b86388f..0ab93c90f73 100644 --- a/cmd/go-contrib-init/contrib.go +++ b/cmd/go-contrib-init/contrib.go @@ -160,44 +160,6 @@ GOPATH: %s } return } - - gopath := firstGoPath() - if gopath == "" { - log.Fatal("Your GOPATH is not set, please set it") - } - - rightdir := filepath.Join(gopath, "src", "golang.org", "x", *repo) - if !strings.HasPrefix(wd, rightdir) { - dirExists, err := exists(rightdir) - if err != nil { - log.Fatal(err) - } - if !dirExists { - log.Fatalf("The repo you want to work on is currently not on your system.\n"+ - "Run %q to obtain this repo\n"+ - "then go to the directory %q\n", - "go get -d golang.org/x/"+*repo, rightdir) - } - log.Fatalf("Your current directory is:%q\n"+ - "Working on golang/x/%v requires you be in %q\n", - wd, *repo, rightdir) - } -} - -func firstGoPath() string { - list := filepath.SplitList(build.Default.GOPATH) - if len(list) < 1 { - return "" - } - return list[0] -} - -func exists(path string) (bool, error) { - _, err := os.Stat(path) - if os.IsNotExist(err) { - return false, nil - } - return true, err } func inGoPath(wd string) bool { From 4ee50fe6264385fde55bff9cda80aa103d98e64b Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 12 Mar 2025 23:10:45 -0600 Subject: [PATCH 235/309] gopls/internal/analysis/modernize: rangeint: avoid offering wrong fix This change adds additional checking to ensure that rangeint won't offer a fix in cases where RHS of 'i < limit' depends on loop var. Given the code snippet below, this change will no longer offer a wrong fix as it did before. var n, kd int for j := 0; j < min(n-j, kd+1); j++ { } - offered fix before(build error 'undefined: j') var n, kd int for j := range min(n-j, kd+1){ } Fixes golang/go#72726 Change-Id: I78c5457406258c44dd2fa861aa43d9ddb9c707fc Reviewed-on: https://go-review.googlesource.com/c/tools/+/656975 Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/rangeint.go | 8 +++++++ .../testdata/src/rangeint/rangeint.go | 21 +++++++++++++++++++ .../testdata/src/rangeint/rangeint.go.golden | 21 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 2921bbb3468..d51bd79433e 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -62,7 +62,15 @@ func rangeint(pass *analysis.Pass) { compare.Op == token.LSS && equalSyntax(compare.X, init.Lhs[0]) { // Have: for i = 0; i < limit; ... {} + limit := compare.Y + curLimit, _ := curLoop.FindNode(limit) + // Don't offer a fix if the limit expression depends on the loop index. + for cur := range curLimit.Preorder((*ast.Ident)(nil)) { + if cur.Node().(*ast.Ident).Name == index.Name { + continue nextLoop + } + } // Skip loops up to b.N in benchmarks; see [bloop]. if sel, ok := limit.(*ast.SelectorExpr); ok && diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index da486dcd32c..915f122b4fc 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -77,3 +77,24 @@ func issue71847d() { for i := 0; i < limit2; i++ { // want "for loop can be modernized using range over int" } } + +func issue72726() { + var n, kd int + for i := 0; i < n; i++ { // want "for loop can be modernized using range over int" + // nope: j will be invisible once it's refactored to 'for j := range min(n-j, kd+1)' + for j := 0; j < min(n-j, kd+1); j++ { // nope + _, _ = i, j + } + } + + for i := 0; i < i; i++ { // nope + } + + var i int + for i = 0; i < i/2; i++ { // nope + } + + var arr []int + for i = 0; i < arr[i]; i++ { // nope + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 01d28ccb92b..bd76ce688bb 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -77,3 +77,24 @@ func issue71847d() { for range int(limit2) { // want "for loop can be modernized using range over int" } } + +func issue72726() { + var n, kd int + for i := range n { // want "for loop can be modernized using range over int" + // nope: j will be invisible once it's refactored to 'for j := range min(n-j, kd+1)' + for j := 0; j < min(n-j, kd+1); j++ { // nope + _, _ = i, j + } + } + + for i := 0; i < i; i++ { // nope + } + + var i int + for i = 0; i < i/2; i++ { // nope + } + + var arr []int + for i = 0; i < arr[i]; i++ { // nope + } +} From e59d6c5d501f1e31cc418cb4e6dcb1cea096c368 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Tue, 11 Mar 2025 17:44:11 -0500 Subject: [PATCH 236/309] gopls/internal/cache/testfuncs: handle recursive subtests Resolves an issue that caused runaway recursive allocation. Previously, the following would cause unbounded recursion: func Test(t *testing.T) { t.Run("Test", Test) } Now, subtests that reference a top-level test will not be scanned, and a subtest that has already been scanned will not be scanned again. Additionally, there is now an arbitrary recursion depth limit of 100. Fixes golang/go#72769. Change-Id: I0322c55ac5db65bb01cc8fc92ecf015484bbccd8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/656875 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/cache/testfuncs/tests.go | 48 +++++++++++---- .../integration/workspace/packages_test.go | 60 +++++++++++++++++++ 2 files changed, 96 insertions(+), 12 deletions(-) diff --git a/gopls/internal/cache/testfuncs/tests.go b/gopls/internal/cache/testfuncs/tests.go index 1182795b37b..e0e3ce1beca 100644 --- a/gopls/internal/cache/testfuncs/tests.go +++ b/gopls/internal/cache/testfuncs/tests.go @@ -57,6 +57,7 @@ func NewIndex(files []*parsego.File, info *types.Info) *Index { b := &indexBuilder{ fileIndex: make(map[protocol.DocumentURI]int), subNames: make(map[string]int), + visited: make(map[*types.Func]bool), } return b.build(files, info) } @@ -101,6 +102,7 @@ func (b *indexBuilder) build(files []*parsego.File, info *types.Info) *Index { } b.Files[i].Tests = append(b.Files[i].Tests, t) + b.visited[obj] = true // Check for subtests if isTest { @@ -168,27 +170,48 @@ func (b *indexBuilder) findSubtests(parent gobTest, typ *ast.FuncType, body *ast t.Location.Range, _ = file.NodeRange(call) tests = append(tests, t) - if typ, body := findFunc(files, info, body, call.Args[1]); typ != nil { + fn, typ, body := findFunc(files, info, body, call.Args[1]) + if typ == nil { + continue + } + + // Function literals don't have an associated object + if fn == nil { tests = append(tests, b.findSubtests(t, typ, body, file, files, info)...) + continue + } + + // Never recurse if the second argument is a top-level test function + if isTest, _ := isTestOrExample(fn); isTest { + continue + } + + // Don't recurse into functions that have already been visited + if b.visited[fn] { + continue } + + b.visited[fn] = true + tests = append(tests, b.findSubtests(t, typ, body, file, files, info)...) } return tests } // findFunc finds the type and body of the given expr, which may be a function -// literal or reference to a declared function. -// -// If no function is found, findFunc returns (nil, nil). -func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr ast.Expr) (*ast.FuncType, *ast.BlockStmt) { +// literal or reference to a declared function. If the expression is a declared +// function, findFunc returns its [types.Func]. If the expression is a function +// literal, findFunc returns nil for the first return value. If no function is +// found, findFunc returns (nil, nil, nil). +func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr ast.Expr) (*types.Func, *ast.FuncType, *ast.BlockStmt) { var obj types.Object switch arg := expr.(type) { case *ast.FuncLit: - return arg.Type, arg.Body + return nil, arg.Type, arg.Body case *ast.Ident: obj = info.ObjectOf(arg) if obj == nil { - return nil, nil + return nil, nil, nil } case *ast.SelectorExpr: @@ -198,12 +221,12 @@ func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr // complex. However, those cases should be rare. sel, ok := info.Selections[arg] if !ok { - return nil, nil + return nil, nil, nil } obj = sel.Obj() default: - return nil, nil + return nil, nil, nil } if v, ok := obj.(*types.Var); ok { @@ -211,7 +234,7 @@ func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr // the file), but that doesn't account for assignment. If the variable // is assigned multiple times, we could easily get the wrong one. _, _ = v, body - return nil, nil + return nil, nil, nil } for _, file := range files { @@ -228,11 +251,11 @@ func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr } if info.ObjectOf(decl.Name) == obj { - return decl.Type, decl.Body + return obj.(*types.Func), decl.Type, decl.Body } } } - return nil, nil + return nil, nil, nil } // isTestOrExample reports whether the given func is a testing func or an @@ -308,6 +331,7 @@ type indexBuilder struct { gobPackage fileIndex map[protocol.DocumentURI]int subNames map[string]int + visited map[*types.Func]bool } // -- serial format of index -- diff --git a/gopls/internal/test/integration/workspace/packages_test.go b/gopls/internal/test/integration/workspace/packages_test.go index fdee21d822f..3420e32e084 100644 --- a/gopls/internal/test/integration/workspace/packages_test.go +++ b/gopls/internal/test/integration/workspace/packages_test.go @@ -433,6 +433,66 @@ func (X) SubtestMethod(t *testing.T) { }) } +func TestRecursiveSubtest(t *testing.T) { + const files = ` +-- go.mod -- +module foo + +-- foo_test.go -- +package foo + +import "testing" + +func TestFoo(t *testing.T) { t.Run("Foo", TestFoo) } +func TestBar(t *testing.T) { t.Run("Foo", TestFoo) } + +func TestBaz(t *testing.T) { + var sub func(t *testing.T) + sub = func(t *testing.T) { t.Run("Sub", sub) } + t.Run("Sub", sub) +} +` + + Run(t, files, func(t *testing.T, env *Env) { + checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("foo_test.go")}, false, command.NeedTests, []command.Package{ + { + Path: "foo", + ForTest: "foo", + ModulePath: "foo", + TestFiles: []command.TestFile{ + { + URI: env.Editor.DocumentURI("foo_test.go"), + Tests: []command.TestCase{ + {Name: "TestFoo"}, + {Name: "TestFoo/Foo"}, + {Name: "TestBar"}, + {Name: "TestBar/Foo"}, + {Name: "TestBaz"}, + {Name: "TestBaz/Sub"}, + }, + }, + }, + }, + }, map[string]command.Module{ + "foo": { + Path: "foo", + GoMod: env.Editor.DocumentURI("go.mod"), + }, + }, []string{ + `func TestFoo(t *testing.T) { t.Run("Foo", TestFoo) }`, + `t.Run("Foo", TestFoo)`, + `func TestBar(t *testing.T) { t.Run("Foo", TestFoo) }`, + `t.Run("Foo", TestFoo)`, + `func TestBaz(t *testing.T) { + var sub func(t *testing.T) + sub = func(t *testing.T) { t.Run("Sub", sub) } + t.Run("Sub", sub) +}`, + `t.Run("Sub", sub)`, + }) + }) +} + func checkPackages(t testing.TB, env *Env, files []protocol.DocumentURI, recursive bool, mode command.PackagesMode, wantPkg []command.Package, wantModule map[string]command.Module, wantSource []string) { t.Helper() From 40f8cca0a7780784a66e1d0bb1d41e87283ceea9 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 12 Mar 2025 16:24:42 -0400 Subject: [PATCH 237/309] internal/imports: fix extra logf argument Thanks vikblom for pointing it out. Unfortunately, the printf analyzer does not handle this type of call. Change-Id: I314914dcbf68e9ab4310a88e3031413cc09fc975 Reviewed-on: https://go-review.googlesource.com/c/tools/+/657335 LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley Reviewed-by: Alan Donovan --- internal/imports/fix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index 737a9bfae8f..c78d10f2d61 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -585,7 +585,7 @@ func getFixesWithSource(ctx context.Context, fset *token.FileSet, f *ast.File, f srcDir := filepath.Dir(abs) if logf != nil { - logf("fixImports(filename=%q), srcDir=%q ...", filename, abs, srcDir) + logf("fixImports(filename=%q), srcDir=%q ...", filename, srcDir) } // First pass: looking only at f, and using the naive algorithm to From dcc4b8a191617e187055ef6ce3be7798867f4daa Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 24 Feb 2025 15:20:07 +0000 Subject: [PATCH 238/309] gopls/internal/golang: use slices.Reverse in pathEnclosingObjNode Change-Id: I1ffa1708564a87125cc38355baf481c3ea6b85b8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/652016 Reviewed-by: Alan Donovan Auto-Submit: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/implementation.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 2d9a1e93ef3..0ccab640709 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -665,6 +665,7 @@ func pathEnclosingObjNode(f *ast.File, pos token.Pos) []ast.Node { // handled this by calling astutil.PathEnclosingInterval twice, // once for "pos" and once for "pos-1". found = n.Pos() <= pos && pos <= n.End() + case *ast.ImportSpec: if n.Path.Pos() <= pos && pos < n.Path.End() { found = true @@ -674,6 +675,7 @@ func pathEnclosingObjNode(f *ast.File, pos token.Pos) []ast.Node { path = append(path, n.Name) } } + case *ast.StarExpr: // Follow star expressions to the inner identifier. if pos == n.Star { @@ -690,7 +692,6 @@ func pathEnclosingObjNode(f *ast.File, pos token.Pos) []ast.Node { // Reverse path so leaf is first element. slices.Reverse(path) - return path } From 6c3e542dfc660fed39233c18adf1467c5cdb359d Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Thu, 13 Mar 2025 01:57:28 -0600 Subject: [PATCH 239/309] gopls/internal/analysis/modernize: preserves comments in minmax This CL changes the original deletion (from after rh0 to the end of the if stmt) into deletion from the start of assignment to the end of if stmt), add all comments between them and create a new assigment as-is. This change preserves all comments inside if stmt and the comments after the line of assignment and before if stmt, causing comments B,C,D to be preserved and put on the top of min/max function call after fix. - source: lhs0 = rhs0 // A // B if rhs0 < b { // C lhs0 = b // D } - fixed: // A // B // C // D lhs0 = max(rhs0,b) Fixes golang/go#72727 Change-Id: I7c193711aac5834ebb0d5e8ae22c26ae7990c34f Reviewed-on: https://go-review.googlesource.com/c/tools/+/656655 Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/minmax.go | 27 ++++++++--- .../modernize/testdata/src/minmax/minmax.go | 35 ++++++++++++--- .../testdata/src/minmax/minmax.go.golden | 35 ++++++++++++++- internal/analysisinternal/analysis.go | 22 +++++++++ internal/analysisinternal/analysis_test.go | 45 +++++++++++++++++++ 5 files changed, 148 insertions(+), 16 deletions(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index 8888383afec..a72506c3bbb 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -9,6 +9,7 @@ import ( "go/ast" "go/token" "go/types" + "strings" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -32,7 +33,7 @@ func minmax(pass *analysis.Pass) { // check is called for all statements of this form: // if a < b { lhs = rhs } - check := func(curIfStmt cursor.Cursor, compare *ast.BinaryExpr) { + check := func(file *ast.File, curIfStmt cursor.Cursor, compare *ast.BinaryExpr) { var ( ifStmt = curIfStmt.Node().(*ast.IfStmt) tassign = ifStmt.Body.List[0].(*ast.AssignStmt) @@ -44,6 +45,14 @@ func minmax(pass *analysis.Pass) { sign = isInequality(compare.Op) ) + allComments := func(file *ast.File, start, end token.Pos) string { + var buf strings.Builder + for co := range analysisinternal.Comments(file, start, end) { + _, _ = fmt.Fprintf(&buf, "%s\n", co.Text) + } + return buf.String() + } + if fblock, ok := ifStmt.Else.(*ast.BlockStmt); ok && isAssignBlock(fblock) { fassign := fblock.List[0].(*ast.AssignStmt) @@ -85,7 +94,8 @@ func minmax(pass *analysis.Pass) { // Replace IfStmt with lhs = min(a, b). Pos: ifStmt.Pos(), End: ifStmt.End(), - NewText: fmt.Appendf(nil, "%s = %s(%s, %s)", + NewText: fmt.Appendf(nil, "%s%s = %s(%s, %s)", + allComments(file, ifStmt.Pos(), ifStmt.End()), analysisinternal.Format(pass.Fset, lhs), sym, analysisinternal.Format(pass.Fset, a), @@ -144,10 +154,13 @@ func minmax(pass *analysis.Pass) { SuggestedFixes: []analysis.SuggestedFix{{ Message: fmt.Sprintf("Replace if/else with %s", sym), TextEdits: []analysis.TextEdit{{ - // Replace rhs0 and IfStmt with min(a, b) - Pos: rhs0.Pos(), + Pos: fassign.Pos(), End: ifStmt.End(), - NewText: fmt.Appendf(nil, "%s(%s, %s)", + // Replace "x := a; if ... {}" with "x = min(...)", preserving comments. + NewText: fmt.Appendf(nil, "%s %s %s %s(%s, %s)", + allComments(file, fassign.Pos(), ifStmt.End()), + analysisinternal.Format(pass.Fset, lhs), + fassign.Tok.String(), sym, analysisinternal.Format(pass.Fset, a), analysisinternal.Format(pass.Fset, b)), @@ -161,16 +174,16 @@ func minmax(pass *analysis.Pass) { // Find all "if a < b { lhs = rhs }" statements. inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for curFile := range filesUsing(inspect, pass.TypesInfo, "go1.21") { + astFile := curFile.Node().(*ast.File) for curIfStmt := range curFile.Preorder((*ast.IfStmt)(nil)) { ifStmt := curIfStmt.Node().(*ast.IfStmt) - if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok && ifStmt.Init == nil && isInequality(compare.Op) != 0 && isAssignBlock(ifStmt.Body) { // Have: if a < b { lhs = rhs } - check(curIfStmt, compare) + check(astFile, curIfStmt, compare) } } } diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index 44ba7c9193a..e0ac6da2734 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -1,9 +1,12 @@ package minmax func ifmin(a, b int) { - x := a + x := a // A + // B if a < b { // want "if statement can be modernized using max" - x = b + // C + x = b // D + // E } print(x) } @@ -33,20 +36,30 @@ func ifmaxvariant(a, b int) { } func ifelsemin(a, b int) { - var x int + var x int // A + // B if a <= b { // want "if/else statement can be modernized using min" - x = a + // C + x = a // D + // E } else { - x = b + // F + x = b // G + // H } print(x) } func ifelsemax(a, b int) { - var x int + // A + var x int // B + // C if a >= b { // want "if/else statement can be modernized using max" - x = a + // D + x = a // E + // F } else { + // G x = b } print(x) @@ -115,3 +128,11 @@ func nopeHasElseBlock(x int) int { } return y } + +func fix72727(a, b int) { + o := a - 42 + // some important comment. DO NOT REMOVE. + if o < b { // want "if statement can be modernized using max" + o = b + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index df1d5180f8a..5a62435ac0c 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -1,33 +1,57 @@ package minmax func ifmin(a, b int) { + // A + // B + // want "if statement can be modernized using max" + // C + // D + // E x := max(a, b) print(x) } func ifmax(a, b int) { + // want "if statement can be modernized using min" x := min(a, b) print(x) } func ifminvariant(a, b int) { + // want "if statement can be modernized using min" x := min(a, b) print(x) } func ifmaxvariant(a, b int) { + // want "if statement can be modernized using min" x := min(a, b) print(x) } func ifelsemin(a, b int) { - var x int + var x int // A + // B + // want "if/else statement can be modernized using min" + // C + // D + // E + // F + // G + // H x = min(a, b) print(x) } func ifelsemax(a, b int) { - var x int + // A + var x int // B + // C + // want "if/else statement can be modernized using max" + // D + // E + // F + // G x = max(a, b) print(x) } @@ -55,6 +79,7 @@ func nopeIfStmtHasInitStmt() { // Regression test for a bug: fix was "y := max(x, y)". func oops() { x := 1 + // want "if statement can be modernized using max" y := max(x, 2) print(y) } @@ -92,3 +117,9 @@ func nopeHasElseBlock(x int) int { } return y } + +func fix72727(a, b int) { + // some important comment. DO NOT REMOVE. + // want "if statement can be modernized using max" + o := max(a-42, b) +} diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index 69e21a14ca9..bc10f66da25 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -15,6 +15,7 @@ import ( "go/scanner" "go/token" "go/types" + "iter" pathpkg "path" "slices" "strings" @@ -608,3 +609,24 @@ Outer: } return []analysis.TextEdit{edit} } + +// Comments returns an iterator over the comments overlapping the specified interval. +func Comments(file *ast.File, start, end token.Pos) iter.Seq[*ast.Comment] { + // TODO(adonovan): optimize use binary O(log n) instead of linear O(n) search. + return func(yield func(*ast.Comment) bool) { + for _, cg := range file.Comments { + for _, co := range cg.List { + if co.Pos() > end { + return + } + if co.End() < start { + continue + } + + if !yield(co) { + return + } + } + } + } +} diff --git a/internal/analysisinternal/analysis_test.go b/internal/analysisinternal/analysis_test.go index 530e57250c2..e3c760aff5a 100644 --- a/internal/analysisinternal/analysis_test.go +++ b/internal/analysisinternal/analysis_test.go @@ -8,6 +8,7 @@ import ( "go/ast" "go/parser" "go/token" + "slices" "testing" "golang.org/x/tools/go/ast/inspector" @@ -253,3 +254,47 @@ func TestDeleteStmt(t *testing.T) { } } + +func TestComments(t *testing.T) { + src := ` +package main + +// A +func fn() { }` + var fset token.FileSet + f, err := parser.ParseFile(&fset, "", []byte(src), parser.ParseComments|parser.AllErrors) + if err != nil { + t.Fatal(err) + } + + commentA := f.Comments[0].List[0] + commentAMidPos := (commentA.Pos() + commentA.End()) / 2 + + want := []*ast.Comment{commentA} + testCases := []struct { + name string + start, end token.Pos + want []*ast.Comment + }{ + {name: "comment totally overlaps with given interval", start: f.Pos(), end: f.End(), want: want}, + {name: "interval from file start to mid of comment A", start: f.Pos(), end: commentAMidPos, want: want}, + {name: "interval from mid of comment A to file end", start: commentAMidPos, end: commentA.End(), want: want}, + {name: "interval from start of comment A to mid of comment A", start: commentA.Pos(), end: commentAMidPos, want: want}, + {name: "interval from mid of comment A to comment A end", start: commentAMidPos, end: commentA.End(), want: want}, + {name: "interval at the start of comment A", start: commentA.Pos(), end: commentA.Pos(), want: want}, + {name: "interval at the end of comment A", start: commentA.End(), end: commentA.End(), want: want}, + {name: "interval from file start to the front of comment A start", start: f.Pos(), end: commentA.Pos() - 1, want: nil}, + {name: "interval from the position after end of comment A to file end", start: commentA.End() + 1, end: f.End(), want: nil}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + var got []*ast.Comment + for co := range Comments(f, tc.start, tc.end) { + got = append(got, co) + } + if !slices.Equal(got, tc.want) { + t.Errorf("%s: got %v, want %v", tc.name, got, tc.want) + } + }) + } +} From e06efb48035968dc7c72ac5b66f17048d53f0549 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sun, 16 Mar 2025 17:27:53 -0400 Subject: [PATCH 240/309] internal/gcimporter: bug.Report in export's panic handler This CL effectively refines the telemetry report golang/go#71067 by pushing the bug.Report call down into the panic handler where the stack is still available. Fixes golang/go#71067 Change-Id: I354f01d3085f1547232bca499d0bd1f0bf2daef3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658355 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- gopls/internal/cache/check.go | 5 ++++- internal/gcimporter/iexport.go | 23 +++++++++++++++-------- internal/gcimporter/iexport_test.go | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index 27d5cfa240b..909003288bc 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -637,7 +637,10 @@ func (b *typeCheckBatch) checkPackageForImport(ctx context.Context, ph *packageH go func() { exportData, err := gcimporter.IExportShallow(b.fset, pkg, bug.Reportf) if err != nil { - bug.Reportf("exporting package %v: %v", ph.mp.ID, err) + // Internal error; the stack will have been reported via + // bug.Reportf within IExportShallow, so there's not much + // to do here (issue #71067). + event.Error(ctx, "IExportShallow failed", err, label.Package.Of(string(ph.mp.ID))) return } if err := filecache.Set(exportDataKind, ph.key, exportData); err != nil { diff --git a/internal/gcimporter/iexport.go b/internal/gcimporter/iexport.go index 253d6493c21..48e90b29ded 100644 --- a/internal/gcimporter/iexport.go +++ b/internal/gcimporter/iexport.go @@ -271,10 +271,10 @@ import ( // file system, be sure to include a cryptographic digest of the executable in // the key to avoid version skew. // -// If the provided reportf func is non-nil, it will be used for reporting bugs -// encountered during export. -// TODO(rfindley): remove reportf when we are confident enough in the new -// objectpath encoding. +// If the provided reportf func is non-nil, it is used for reporting +// bugs (e.g. recovered panics) encountered during export, enabling us +// to obtain via telemetry the stack that would otherwise be lost by +// merely returning an error. func IExportShallow(fset *token.FileSet, pkg *types.Package, reportf ReportFunc) ([]byte, error) { // In principle this operation can only fail if out.Write fails, // but that's impossible for bytes.Buffer---and as a matter of @@ -283,7 +283,7 @@ func IExportShallow(fset *token.FileSet, pkg *types.Package, reportf ReportFunc) // TODO(adonovan): use byte slices throughout, avoiding copying. const bundle, shallow = false, true var out bytes.Buffer - err := iexportCommon(&out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}) + err := iexportCommon(&out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}, reportf) return out.Bytes(), err } @@ -323,20 +323,27 @@ const bundleVersion = 0 // so that calls to IImportData can override with a provided package path. func IExportData(out io.Writer, fset *token.FileSet, pkg *types.Package) error { const bundle, shallow = false, false - return iexportCommon(out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}) + return iexportCommon(out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}, nil) } // IExportBundle writes an indexed export bundle for pkgs to out. func IExportBundle(out io.Writer, fset *token.FileSet, pkgs []*types.Package) error { const bundle, shallow = true, false - return iexportCommon(out, fset, bundle, shallow, iexportVersion, pkgs) + return iexportCommon(out, fset, bundle, shallow, iexportVersion, pkgs, nil) } -func iexportCommon(out io.Writer, fset *token.FileSet, bundle, shallow bool, version int, pkgs []*types.Package) (err error) { +func iexportCommon(out io.Writer, fset *token.FileSet, bundle, shallow bool, version int, pkgs []*types.Package, reportf ReportFunc) (err error) { if !debug { defer func() { if e := recover(); e != nil { + // Report the stack via telemetry (see #71067). + if reportf != nil { + reportf("panic in exporter") + } if ierr, ok := e.(internalError); ok { + // internalError usually means we exported a + // bad go/types data structure: a violation + // of an implicit precondition of Export. err = ierr return } diff --git a/internal/gcimporter/iexport_test.go b/internal/gcimporter/iexport_test.go index 5707b3784a5..fa8ecd30dc1 100644 --- a/internal/gcimporter/iexport_test.go +++ b/internal/gcimporter/iexport_test.go @@ -29,7 +29,7 @@ import ( func iexport(fset *token.FileSet, version int, pkg *types.Package) ([]byte, error) { var buf bytes.Buffer const bundle, shallow = false, false - if err := gcimporter.IExportCommon(&buf, fset, bundle, shallow, version, []*types.Package{pkg}); err != nil { + if err := gcimporter.IExportCommon(&buf, fset, bundle, shallow, version, []*types.Package{pkg}, nil); err != nil { return nil, err } return buf.Bytes(), nil From 066484ed0313db3236f05d8c2b3049e4a52e8983 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 17 Mar 2025 14:41:33 -0400 Subject: [PATCH 241/309] gopls/internal/test/integration/misc: test "annotations" setting This CL adds a test that the "annotations" config setting is honored by the "Toggle compiler optimization details" setting. (The test resulted from the investigation of the comment at https://github.com/golang/go/issues/71888#issuecomment-2727492170.) Updates golang/go#71888 Change-Id: I002d945fe8883ecd2dc95a2d43c0ccf2aa93a2c2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658555 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../test/integration/misc/compileropt_test.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/gopls/internal/test/integration/misc/compileropt_test.go b/gopls/internal/test/integration/misc/compileropt_test.go index 175ec640042..68138fabc43 100644 --- a/gopls/internal/test/integration/misc/compileropt_test.go +++ b/gopls/internal/test/integration/misc/compileropt_test.go @@ -166,3 +166,66 @@ func H(x int) any { return &x } ) }) } + +// TestCompilerOptDetails_config exercises that the "want optimization +// details" flag honors the "annotation" configuration setting. +func TestCompilerOptDetails_config(t *testing.T) { + if runtime.GOOS == "android" { + t.Skipf("the compiler optimization details code action doesn't work on Android") + } + + const mod = ` +-- go.mod -- +module mod.com +go 1.18 + +-- a/a.go -- +package a + +func F(x int) any { return &x } // escape(x escapes to heap) +func G() { defer func(){} () } // cannotInlineFunction(unhandled op DEFER) +` + + for _, escape := range []bool{true, false} { + WithOptions( + Settings{"annotations": map[string]any{"inline": true, "escape": escape}}, + ).Run(t, mod, func(t *testing.T, env *Env) { + env.OpenFile("a/a.go") + actions := env.CodeActionForFile("a/a.go", nil) + + docAction, err := codeActionByKind(actions, settings.GoToggleCompilerOptDetails) + if err != nil { + t.Fatal(err) + } + params := &protocol.ExecuteCommandParams{ + Command: docAction.Command.Command, + Arguments: docAction.Command.Arguments, + } + env.ExecuteCommand(params, nil) + + env.OnceMet( + CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 1, true), + cond(escape, Diagnostics, NoDiagnostics)( + ForFile("a/a.go"), + AtPosition("a/a.go", 2, 7), + WithMessage("x escapes to heap"), + WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), + ), + Diagnostics( + ForFile("a/a.go"), + AtPosition("a/a.go", 3, 5), + WithMessage("cannotInlineFunction(unhandled op DEFER)"), + WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), + ), + ) + }) + } +} + +func cond[T any](cond bool, x, y T) T { + if cond { + return x + } else { + return y + } +} From 95eb16e6031d0496dca9bac57362606d1cfd70c6 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Fri, 14 Mar 2025 13:11:44 -0400 Subject: [PATCH 242/309] gopls/internal/test/integration: skip x_tools-gotip-openbsd-amd64 (7.6) The new openbsd/amd64 7.6 builder is generally working well everywhere but this one place. Add a skip for now to buy time to investigate this issue. Note that the previous openbsd/amd64 7.2 builder was also running into problems with these tests, as tracked in go.dev/issue/54461, though it wasn't happening as consistently as it is now. For golang/go#72145. For golang/go#54461. Change-Id: I6dd34fcdcca99c90282f0b9119936efa6bebf458 Cq-Include-Trybots: luci.golang.try:x_tools-gotip-openbsd-amd64 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658015 LUCI-TryBot-Result: Go LUCI Reviewed-by: Cherry Mui Reviewed-by: Alan Donovan Auto-Submit: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov --- gopls/internal/test/integration/runner.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gopls/internal/test/integration/runner.go b/gopls/internal/test/integration/runner.go index b3e98b859d3..b4b9d3a2a4d 100644 --- a/gopls/internal/test/integration/runner.go +++ b/gopls/internal/test/integration/runner.go @@ -266,10 +266,10 @@ func ConnectGoplsEnv(t testing.TB, ctx context.Context, sandbox *fake.Sandbox, c // longBuilders maps builders that are skipped when -short is set to a // (possibly empty) justification. var longBuilders = map[string]string{ - "openbsd-amd64-64": "go.dev/issue/42789", - "openbsd-386-64": "go.dev/issue/42789", - "openbsd-386-68": "go.dev/issue/42789", - "openbsd-amd64-68": "go.dev/issue/42789", + "x_tools-gotip-openbsd-amd64": "go.dev/issue/72145", + "x_tools-go1.24-openbsd-amd64": "go.dev/issue/72145", + "x_tools-go1.23-openbsd-amd64": "go.dev/issue/72145", + "darwin-amd64-10_12": "", "freebsd-amd64-race": "", "illumos-amd64": "", From e7b4c64c77184852af2327225261d6f3f2ff38e7 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 17 Mar 2025 18:41:02 -0400 Subject: [PATCH 243/309] gopls/internal/golang: fix crash in source.test code action We forgot to set needPkg=true. Clearly this code has never been tested since its inception in CL 231959. Also, add the missing test. Fixes golang/go#72907 Change-Id: I077b27ab4c64900ecefa19cb1329eb47d9cd6f28 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658556 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/golang/codeaction.go | 2 +- .../test/integration/misc/test_test.go | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 gopls/internal/test/integration/misc/test_test.go diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 74f3c2b6085..a5591edf1f9 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -240,7 +240,7 @@ var codeActionProducers = [...]codeActionProducer{ {kind: settings.GoAssembly, fn: goAssembly, needPkg: true}, {kind: settings.GoDoc, fn: goDoc, needPkg: true}, {kind: settings.GoFreeSymbols, fn: goFreeSymbols}, - {kind: settings.GoTest, fn: goTest}, + {kind: settings.GoTest, fn: goTest, needPkg: true}, {kind: settings.GoToggleCompilerOptDetails, fn: toggleCompilerOptDetails}, {kind: settings.GoplsDocFeatures, fn: goplsDocFeatures}, {kind: settings.RefactorExtractFunction, fn: refactorExtractFunction}, diff --git a/gopls/internal/test/integration/misc/test_test.go b/gopls/internal/test/integration/misc/test_test.go new file mode 100644 index 00000000000..b282bf57a95 --- /dev/null +++ b/gopls/internal/test/integration/misc/test_test.go @@ -0,0 +1,82 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package misc + +// This file defines tests of the source.test ("Run tests and +// benchmarks") code action. + +import ( + "os" + "path/filepath" + "testing" + + "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/gopls/internal/settings" + . "golang.org/x/tools/gopls/internal/test/integration" +) + +func TestRunTestsAndBenchmarks(t *testing.T) { + file := filepath.Join(t.TempDir(), "out") + os.Setenv("TESTFILE", file) + + const src = ` +-- go.mod -- +module example.com +go 1.19 + +-- a/a.go -- +package a + +-- a/a_test.go -- +package a + +import ( + "os" + "testing" +) + +func Test(t *testing.T) { + os.WriteFile(os.Getenv("TESTFILE"), []byte("ok"), 0644) +} + +` + Run(t, src, func(t *testing.T, env *Env) { + env.OpenFile("a/a_test.go") + loc := env.RegexpSearch("a/a_test.go", "WriteFile") + + // Request code actions. (settings.GoTest is special: + // it is returned only when explicitly requested.) + actions, err := env.Editor.Server.CodeAction(env.Ctx, &protocol.CodeActionParams{ + TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI}, + Range: loc.Range, + Context: protocol.CodeActionContext{ + Only: []protocol.CodeActionKind{settings.GoTest}, + }, + }) + if err != nil { + t.Fatal(err) + } + if len(actions) != 1 { + t.Fatalf("CodeAction returned %#v, want one source.test action", actions) + } + if actions[0].Command == nil { + t.Fatalf("CodeActions()[0] has no Command") + } + + // Execute test. + // (ExecuteCommand fails if the test fails.) + t.Logf("Running %s...", actions[0].Title) + env.ExecuteCommand(&protocol.ExecuteCommandParams{ + Command: actions[0].Command.Command, + Arguments: actions[0].Command.Arguments, + }, nil) + + // Check test had expected side effect. + data, err := os.ReadFile(file) + if string(data) != "ok" { + t.Fatalf("Test did not write expected content of %s; ReadFile returned (%q, %v)", file, data, err) + } + }) +} From 3d22fef61cb20f382db36aafa102b442df090c87 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 14 Mar 2025 11:00:38 -0400 Subject: [PATCH 244/309] gopls/internal/analysis/modernize: disable minmax on floating point The built-in min and max functions return NaN if any operand is NaN, so the minmax transformation is not sound for certain inputs. Since it is usually infeasible to prove that the operands are not NaN, this CL disables minmax for floating-point operands. Behavior-preserving translation: celebrating 75 years of being harder than it looks. Fixes golang/go#72829 Change-Id: Idb3454fea7ec37842e622154f66d5898703a392f Reviewed-on: https://go-review.googlesource.com/c/tools/+/657955 Auto-Submit: Alan Donovan Commit-Queue: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/minmax.go | 26 +++++++++++++++++-- .../modernize/testdata/src/minmax/minmax.go | 13 ++++++++++ .../testdata/src/minmax/minmax.go.golden | 13 ++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index a72506c3bbb..a996f9bd56a 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -16,6 +16,7 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/internal/analysisinternal" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typeparams" ) // The minmax pass replaces if/else statements with calls to min or max. @@ -25,6 +26,10 @@ import ( // 1. if a < b { x = a } else { x = b } => x = min(a, b) // 2. x = a; if a < b { x = b } => x = max(a, b) // +// Pattern 1 requires that a is not NaN, and pattern 2 requires that b +// is not Nan. Since this is hard to prove, we reject floating-point +// numbers. +// // Variants: // - all four ordered comparisons // - "x := a" or "x = a" or "var x = a" in pattern 2 @@ -172,15 +177,17 @@ func minmax(pass *analysis.Pass) { } // Find all "if a < b { lhs = rhs }" statements. + info := pass.TypesInfo inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - for curFile := range filesUsing(inspect, pass.TypesInfo, "go1.21") { + for curFile := range filesUsing(inspect, info, "go1.21") { astFile := curFile.Node().(*ast.File) for curIfStmt := range curFile.Preorder((*ast.IfStmt)(nil)) { ifStmt := curIfStmt.Node().(*ast.IfStmt) if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok && ifStmt.Init == nil && isInequality(compare.Op) != 0 && - isAssignBlock(ifStmt.Body) { + isAssignBlock(ifStmt.Body) && + !maybeNaN(info.TypeOf(ifStmt.Body.List[0].(*ast.AssignStmt).Lhs[0])) { // lhs // Have: if a < b { lhs = rhs } check(astFile, curIfStmt, compare) @@ -219,6 +226,21 @@ func isSimpleAssign(n ast.Node) bool { len(assign.Rhs) == 1 } +// maybeNaN reports whether t is (or may be) a floating-point type. +func maybeNaN(t types.Type) bool { + // For now, we rely on core types. + // TODO(adonovan): In the post-core-types future, + // follow the approach of types.Checker.applyTypeFunc. + t = typeparams.CoreType(t) + if t == nil { + return true // fail safe + } + if basic, ok := t.(*types.Basic); ok && basic.Info()&types.IsFloat != 0 { + return true + } + return false +} + // -- utils -- func is[T any](x any) bool { diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index e0ac6da2734..cd117dabf84 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -136,3 +136,16 @@ func fix72727(a, b int) { o = b } } + +type myfloat float64 + +// The built-in min/max differ in their treatement of NaN, +// so reject floating-point numbers (#72829). +func nopeFloat(a, b myfloat) (res myfloat) { + if a < b { + res = a + } else { + res = b + } + return +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index 5a62435ac0c..23bfd6f9ecd 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -123,3 +123,16 @@ func fix72727(a, b int) { // want "if statement can be modernized using max" o := max(a-42, b) } + +type myfloat float64 + +// The built-in min/max differ in their treatement of NaN, +// so reject floating-point numbers (#72829). +func nopeFloat(a, b myfloat) (res myfloat) { + if a < b { + res = a + } else { + res = b + } + return +} From 7042bab9ca1ab65dd39be8accbb1870862e2600b Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 19 Mar 2025 02:10:01 -0600 Subject: [PATCH 245/309] gopls/internal/analysis/modernize: modernizer to suggest using strings.CutPrefix This CL defines a modernizer to suggest users using strings.CutPrefix rather than a combination of strings.HasPrefix and strings.TrimPrefix; or strings.TrimPrefix first with a further comparison in an if statement. Updates golang/go#71369 Change-Id: Id373bbf34292231f3fbfa41d7ffcf23505682beb Reviewed-on: https://go-review.googlesource.com/c/tools/+/655777 Reviewed-by: Robert Findley Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/doc/analyzers.md | 3 + gopls/internal/analysis/modernize/doc.go | 3 + .../internal/analysis/modernize/modernize.go | 1 + .../analysis/modernize/modernize_test.go | 1 + .../analysis/modernize/stringscutprefix.go | 197 ++++++++++++++++++ .../stringscutprefix/bytescutprefix_dot.go | 13 ++ .../bytescutprefix_dot.go.golden | 13 ++ .../src/stringscutprefix/stringscutprefix.go | 134 ++++++++++++ .../stringscutprefix.go.golden | 134 ++++++++++++ .../stringscutprefix/stringscutprefix_dot.go | 23 ++ .../stringscutprefix_dot.go.golden | 23 ++ gopls/internal/doc/api.json | 4 +- internal/analysisinternal/analysis.go | 23 +- 13 files changed, 562 insertions(+), 10 deletions(-) create mode 100644 gopls/internal/analysis/modernize/stringscutprefix.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index bcf5590090a..4ec7fcbd1d0 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -550,6 +550,9 @@ Categories of modernize diagnostic: - stringseq: replace Split in "for range strings.Split(...)" by go1.24's more efficient SplitSeq, or Fields with FieldSeq. + - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, + added to the strings package in go1.20. + Default: on. Package documentation: [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 931a2e6bd45..354bf0955d3 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -82,4 +82,7 @@ // // - stringseq: replace Split in "for range strings.Split(...)" by go1.24's // more efficient SplitSeq, or Fields with FieldSeq. +// +// - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, +// added to the strings package in go1.20. package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 5dd94a82a6b..75f5b4014b6 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -87,6 +87,7 @@ func run(pass *analysis.Pass) (any, error) { rangeint(pass) slicescontains(pass) slicesdelete(pass) + stringscutprefix(pass) stringsseq(pass) sortslice(pass) testingContext(pass) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index f9727d1e253..9f17d159073 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -24,6 +24,7 @@ func Test(t *testing.T) { "rangeint", "slicescontains", "slicesdelete", + "stringscutprefix", "splitseq", "fieldsseq", "sortslice", diff --git a/gopls/internal/analysis/modernize/stringscutprefix.go b/gopls/internal/analysis/modernize/stringscutprefix.go new file mode 100644 index 00000000000..28c42c93b05 --- /dev/null +++ b/gopls/internal/analysis/modernize/stringscutprefix.go @@ -0,0 +1,197 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "fmt" + "go/ast" + "go/token" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" +) + +// stringscutprefix offers a fix to replace an if statement which +// calls to the 2 patterns below with strings.CutPrefix. +// +// Patterns: +// +// 1. if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre) } +// => +// if after, ok := strings.CutPrefix(s, pre); ok { use(after) } +// +// 2. if after := strings.TrimPrefix(s, pre); after != s { use(after) } +// => +// if after, ok := strings.CutPrefix(s, pre); ok { use(after) } +// +// The use must occur within the first statement of the block, and the offered fix +// only replaces the first occurrence of strings.TrimPrefix. +// +// Variants: +// - bytes.HasPrefix usage as pattern 1. +func stringscutprefix(pass *analysis.Pass) { + if !analysisinternal.Imports(pass.Pkg, "strings") && + !analysisinternal.Imports(pass.Pkg, "bytes") { + return + } + + const ( + category = "stringscutprefix" + fixedMessage = "Replace HasPrefix/TrimPrefix with CutPrefix" + ) + + info := pass.TypesInfo + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, pass.TypesInfo, "go1.20") { + for curIfStmt := range curFile.Preorder((*ast.IfStmt)(nil)) { + ifStmt := curIfStmt.Node().(*ast.IfStmt) + + // pattern1 + if call, ok := ifStmt.Cond.(*ast.CallExpr); ok && len(ifStmt.Body.List) > 0 { + obj := typeutil.Callee(info, call) + if !analysisinternal.IsFunctionNamed(obj, "strings", "HasPrefix") && + !analysisinternal.IsFunctionNamed(obj, "bytes", "HasPrefix") { + continue + } + + // Replace the first occurrence of strings.TrimPrefix(s, pre) in the first statement only, + // but not later statements in case s or pre are modified by intervening logic. + firstStmt := curIfStmt.Child(ifStmt.Body).Child(ifStmt.Body.List[0]) + for curCall := range firstStmt.Preorder((*ast.CallExpr)(nil)) { + call1 := curCall.Node().(*ast.CallExpr) + obj1 := typeutil.Callee(info, call1) + if !analysisinternal.IsFunctionNamed(obj1, "strings", "TrimPrefix") && + !analysisinternal.IsFunctionNamed(obj1, "bytes", "TrimPrefix") { + continue + } + + // Have: if strings.HasPrefix(s0, pre0) { ...strings.TrimPrefix(s, pre)... } + var ( + s0 = call.Args[0] + pre0 = call.Args[1] + s = call1.Args[0] + pre = call1.Args[1] + ) + + // check whether the obj1 uses the exact the same argument with strings.HasPrefix + // shadow variables won't be valid because we only access the first statement. + if equalSyntax(s0, s) && equalSyntax(pre0, pre) { + after := analysisinternal.FreshName(info.Scopes[ifStmt], ifStmt.Pos(), "after") + _, prefix, importEdits := analysisinternal.AddImport( + info, + curFile.Node().(*ast.File), + obj1.Pkg().Name(), + obj1.Pkg().Path(), + "CutPrefix", + call.Pos(), + ) + okVarName := analysisinternal.FreshName(info.Scopes[ifStmt], ifStmt.Pos(), "ok") + pass.Report(analysis.Diagnostic{ + // highlight at HasPrefix call. + Pos: call.Pos(), + End: call.End(), + Category: category, + Message: "HasPrefix + TrimPrefix can be simplified to CutPrefix", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fixedMessage, + // if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre)) } + // ------------ ----------------- ----- -------------------------- + // if after, ok := strings.CutPrefix(s, pre); ok { use(after) } + TextEdits: append(importEdits, []analysis.TextEdit{ + { + Pos: call.Fun.Pos(), + End: call.Fun.Pos(), + NewText: []byte(fmt.Sprintf("%s, %s :=", after, okVarName)), + }, + { + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: fmt.Appendf(nil, "%sCutPrefix", prefix), + }, + { + Pos: call.End(), + End: call.End(), + NewText: []byte(fmt.Sprintf("; %s ", okVarName)), + }, + { + Pos: call1.Pos(), + End: call1.End(), + NewText: []byte(after), + }, + }...), + }}}, + ) + break + } + } + } + + // pattern2 + if bin, ok := ifStmt.Cond.(*ast.BinaryExpr); ok && + bin.Op == token.NEQ && + ifStmt.Init != nil && + isSimpleAssign(ifStmt.Init) { + assign := ifStmt.Init.(*ast.AssignStmt) + if call, ok := assign.Rhs[0].(*ast.CallExpr); ok && assign.Tok == token.DEFINE { + lhs := assign.Lhs[0] + obj := typeutil.Callee(info, call) + if analysisinternal.IsFunctionNamed(obj, "strings", "TrimPrefix") && + (equalSyntax(lhs, bin.X) && equalSyntax(call.Args[0], bin.Y) || + (equalSyntax(lhs, bin.Y) && equalSyntax(call.Args[0], bin.X))) { + okVarName := analysisinternal.FreshName(info.Scopes[ifStmt], ifStmt.Pos(), "ok") + // Have one of: + // if rest := TrimPrefix(s, prefix); rest != s { + // if rest := TrimPrefix(s, prefix); s != rest { + + // We use AddImport not to add an import (since it exists already) + // but to compute the correct prefix in the dot-import case. + _, prefix, importEdits := analysisinternal.AddImport( + info, + curFile.Node().(*ast.File), + obj.Pkg().Name(), + obj.Pkg().Path(), + "CutPrefix", + call.Pos(), + ) + + pass.Report(analysis.Diagnostic{ + // highlight from the init and the condition end. + Pos: ifStmt.Init.Pos(), + End: ifStmt.Cond.End(), + Category: category, + Message: "TrimPrefix can be simplified to CutPrefix", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fixedMessage, + // if x := strings.TrimPrefix(s, pre); x != s ... + // ---- ---------- ------ + // if x, ok := strings.CutPrefix (s, pre); ok ... + TextEdits: append(importEdits, []analysis.TextEdit{ + { + Pos: assign.Lhs[0].End(), + End: assign.Lhs[0].End(), + NewText: fmt.Appendf(nil, ", %s", okVarName), + }, + { + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: fmt.Appendf(nil, "%sCutPrefix", prefix), + }, + { + Pos: ifStmt.Cond.Pos(), + End: ifStmt.Cond.End(), + NewText: []byte(okVarName), + }, + }...), + }}, + }) + } + } + } + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go new file mode 100644 index 00000000000..4da9ed52e13 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go @@ -0,0 +1,13 @@ +package stringscutprefix + +import ( + . "bytes" +) + +// test supported cases of pattern 1 +func _() { + if HasPrefix(bss, bspre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := TrimPrefix(bss, bspre) + _ = a + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden new file mode 100644 index 00000000000..054214cabf1 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden @@ -0,0 +1,13 @@ +package stringscutprefix + +import ( + . "bytes" +) + +// test supported cases of pattern 1 +func _() { + if after, ok := CutPrefix(bss, bspre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } +} \ No newline at end of file diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go new file mode 100644 index 00000000000..f5f890f4171 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go @@ -0,0 +1,134 @@ +package stringscutprefix + +import ( + "bytes" + "strings" +) + +var ( + s, pre string + bss, bspre []byte +) + +// test supported cases of pattern 1 +func _() { + if strings.HasPrefix(s, pre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := strings.TrimPrefix(s, pre) + _ = a + } + if strings.HasPrefix("", "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := strings.TrimPrefix("", "") + _ = a + } + if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + println([]byte(strings.TrimPrefix(s, ""))) + } + if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b := "", strings.TrimPrefix(s, "") + _, _ = a, b + } + if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b := strings.TrimPrefix(s, ""), strings.TrimPrefix(s, "") // only replace the first occurrence + s = "123" + b = strings.TrimPrefix(s, "") // only replace the first occurrence + _, _ = a, b + } + + if bytes.HasPrefix(bss, bspre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := bytes.TrimPrefix(bss, bspre) + _ = a + } + if bytes.HasPrefix([]byte(""), []byte("")) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := bytes.TrimPrefix([]byte(""), []byte("")) + _ = a + } + var a, b string + if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b = "", strings.TrimPrefix(s, "") + _, _ = a, b + } +} + +// test cases that are not supported by pattern1 +func _() { + ok := strings.HasPrefix("", "") + if ok { // noop, currently it doesn't track the result usage of HasPrefix + a := strings.TrimPrefix("", "") + _ = a + } + if strings.HasPrefix(s, pre) { + a := strings.TrimPrefix("", "") // noop, as the argument isn't the same + _ = a + } + if strings.HasPrefix(s, pre) { + var result string + result = strings.TrimPrefix("", "") // noop, as we believe define is more popular. + _ = result + } + if strings.HasPrefix("", "") { + a := strings.TrimPrefix(s, pre) // noop, as the argument isn't the same + _ = a + } +} + +var value0 string + +// test supported cases of pattern2 +func _() { + if after := strings.TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after := strings.TrimPrefix(s, pre); s != after { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after := strings.TrimPrefix(s, pre); s != after { // want "TrimPrefix can be simplified to CutPrefix" + println(strings.TrimPrefix(s, pre)) // noop here + } + if after := strings.TrimPrefix(s, ""); s != after { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + var ok bool // define an ok variable to test the fix won't shadow it for its if stmt body + _ = ok + if after := strings.TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + var predefined string + if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop + println(predefined) + } + if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop + println(&predefined) + } + var value string + if value = strings.TrimPrefix(s, pre); s != value { // noop + println(value) + } + lhsMap := make(map[string]string) + if lhsMap[""] = strings.TrimPrefix(s, pre); s != lhsMap[""] { // noop + println(lhsMap[""]) + } + arr := make([]string, 0) + if arr[0] = strings.TrimPrefix(s, pre); s != arr[0] { // noop + println(arr[0]) + } + type example struct { + field string + } + var e example + if e.field = strings.TrimPrefix(s, pre); s != e.field { // noop + println(e.field) + } +} + +// test cases that not supported by pattern2 +func _() { + if after := strings.TrimPrefix(s, pre); s != pre { // noop + println(after) + } + if after := strings.TrimPrefix(s, pre); after != pre { // noop + println(after) + } + if strings.TrimPrefix(s, pre) != s { + println(strings.TrimPrefix(s, pre)) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden new file mode 100644 index 00000000000..d8b7b2ba47f --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden @@ -0,0 +1,134 @@ +package stringscutprefix + +import ( + "bytes" + "strings" +) + +var ( + s, pre string + bss, bspre []byte +) + +// test supported cases of pattern 1 +func _() { + if after, ok := strings.CutPrefix(s, pre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } + if after, ok := strings.CutPrefix("", ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } + if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + println([]byte(after)) + } + if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b := "", after + _, _ = a, b + } + if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b := after, strings.TrimPrefix(s, "") // only replace the first occurrence + s = "123" + b = strings.TrimPrefix(s, "") // only replace the first occurrence + _, _ = a, b + } + + if after, ok := bytes.CutPrefix(bss, bspre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } + if after, ok := bytes.CutPrefix([]byte(""), []byte("")); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } + var a, b string + if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b = "", after + _, _ = a, b + } +} + +// test cases that are not supported by pattern1 +func _() { + ok := strings.HasPrefix("", "") + if ok { // noop, currently it doesn't track the result usage of HasPrefix + a := strings.TrimPrefix("", "") + _ = a + } + if strings.HasPrefix(s, pre) { + a := strings.TrimPrefix("", "") // noop, as the argument isn't the same + _ = a + } + if strings.HasPrefix(s, pre) { + var result string + result = strings.TrimPrefix("", "") // noop, as we believe define is more popular. + _ = result + } + if strings.HasPrefix("", "") { + a := strings.TrimPrefix(s, pre) // noop, as the argument isn't the same + _ = a + } +} + +var value0 string + +// test supported cases of pattern2 +func _() { + if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(strings.TrimPrefix(s, pre)) // noop here + } + if after, ok := strings.CutPrefix(s, ""); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + var ok bool // define an ok variable to test the fix won't shadow it for its if stmt body + _ = ok + if after, ok0 := strings.CutPrefix(s, pre); ok0 { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + var predefined string + if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop + println(predefined) + } + if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop + println(&predefined) + } + var value string + if value = strings.TrimPrefix(s, pre); s != value { // noop + println(value) + } + lhsMap := make(map[string]string) + if lhsMap[""] = strings.TrimPrefix(s, pre); s != lhsMap[""] { // noop + println(lhsMap[""]) + } + arr := make([]string, 0) + if arr[0] = strings.TrimPrefix(s, pre); s != arr[0] { // noop + println(arr[0]) + } + type example struct { + field string + } + var e example + if e.field = strings.TrimPrefix(s, pre); s != e.field { // noop + println(e.field) + } +} + +// test cases that not supported by pattern2 +func _() { + if after := strings.TrimPrefix(s, pre); s != pre { // noop + println(after) + } + if after := strings.TrimPrefix(s, pre); after != pre { // noop + println(after) + } + if strings.TrimPrefix(s, pre) != s { + println(strings.TrimPrefix(s, pre)) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go new file mode 100644 index 00000000000..75ce5bbe39b --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go @@ -0,0 +1,23 @@ +package stringscutprefix + +import ( + . "strings" +) + +// test supported cases of pattern 1 +func _() { + if HasPrefix(s, pre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := TrimPrefix(s, pre) + _ = a + } +} + +// test supported cases of pattern2 +func _() { + if after := TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after := TrimPrefix(s, pre); s != after { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden new file mode 100644 index 00000000000..b5f97b3695a --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden @@ -0,0 +1,23 @@ +package stringscutprefix + +import ( + . "strings" +) + +// test supported cases of pattern 1 +func _() { + if after, ok := CutPrefix(s, pre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } +} + +// test supported cases of pattern2 +func _() { + if after, ok := CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } + if after, ok := CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } +} \ No newline at end of file diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index b47d635638c..f731e0d7984 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -562,7 +562,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.", "Default": "true", "Status": "" }, @@ -1338,7 +1338,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go index bc10f66da25..b22e314cf45 100644 --- a/internal/analysisinternal/analysis.go +++ b/internal/analysisinternal/analysis.go @@ -220,7 +220,7 @@ func CheckReadable(pass *analysis.Pass, filename string) error { // to form a qualified name, and the edit for the new import. // // In the special case that pkgpath is dot-imported then member, the -// identifer for which the import is being added, is consulted. If +// identifier for which the import is being added, is consulted. If // member is not shadowed at pos, AddImport returns (".", "", nil). // (AddImport accepts the caller's implicit claim that the imported // package declares member.) @@ -252,13 +252,7 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member // We must add a new import. // Ensure we have a fresh name. - newName := preferredName - for i := 0; ; i++ { - if _, obj := scope.LookupParent(newName, pos); obj == nil { - break // fresh - } - newName = fmt.Sprintf("%s%d", preferredName, i) - } + newName := FreshName(scope, pos, preferredName) // Create a new import declaration either before the first existing // declaration (which must exist), including its comments; or @@ -298,6 +292,19 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member }} } +// FreshName returns the name of an identifier that is undefined +// at the specified position, based on the preferred name. +func FreshName(scope *types.Scope, pos token.Pos, preferred string) string { + newName := preferred + for i := 0; ; i++ { + if _, obj := scope.LookupParent(newName, pos); obj == nil { + break // fresh + } + newName = fmt.Sprintf("%s%d", preferred, i) + } + return newName +} + // Format returns a string representation of the expression e. func Format(fset *token.FileSet, e ast.Expr) string { var buf strings.Builder From a70d348b8d7b57339a4ba6c769ff28a3bade686d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 18 Mar 2025 18:36:17 -0400 Subject: [PATCH 246/309] gopls/internal/util/persistent: add concurrency test It didn't find any problems. Updates golang/go#72931 Change-Id: Idb65548480af1fd6777dffdcc0e6c6e89b5a06f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659015 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/util/persistent/map.go | 2 + gopls/internal/util/persistent/race_test.go | 66 +++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 gopls/internal/util/persistent/race_test.go diff --git a/gopls/internal/util/persistent/map.go b/gopls/internal/util/persistent/map.go index 193f98791d8..d97a9494c41 100644 --- a/gopls/internal/util/persistent/map.go +++ b/gopls/internal/util/persistent/map.go @@ -203,6 +203,8 @@ func (pm *Map[K, V]) SetAll(other *Map[K, V]) { // Set updates the value associated with the specified key. // If release is non-nil, it will be called with entry's key and value once the // key is no longer contained in the map or any clone. +// +// TODO(adonovan): fix release, which has the wrong type. func (pm *Map[K, V]) Set(key K, value V, release func(key, value any)) { first := pm.root second := newNodeWithRef(key, value, release) diff --git a/gopls/internal/util/persistent/race_test.go b/gopls/internal/util/persistent/race_test.go new file mode 100644 index 00000000000..827791a78dc --- /dev/null +++ b/gopls/internal/util/persistent/race_test.go @@ -0,0 +1,66 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build race + +package persistent + +import ( + "context" + "maps" + "testing" + "time" + + "golang.org/x/sync/errgroup" +) + +// TestConcurrency exercises concurrent map access. +// It doesn't assert anything, but it runs under the race detector. +func TestConcurrency(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + var orig Map[int, int] // maps subset of [0-10] to itself (values aren't interesting) + for i := range 10 { + orig.Set(i, i, func(k, v any) { /* just for good measure*/ }) + } + g, ctx := errgroup.WithContext(ctx) + const N = 10 // concurrency level + g.SetLimit(N) + for range N { + g.Go(func() error { + // Each thread has its own clone of the original, + // sharing internal structures. Each map is accessed + // only by a single thread; the shared data is immutable. + m := orig.Clone() + + // Run until the timeout. + for ctx.Err() == nil { + for i := range 1000 { + key := i % 10 + + switch { + case i%2 == 0: + _, _ = m.Get(key) + case i%11 == 0: + m.Set(key, key, func(key, value any) {}) + case i%13 == 0: + _ = maps.Collect(m.All()) + case i%17 == 0: + _ = m.Delete(key) + case i%19 == 0: + _ = m.Keys() + case i%31 == 0: + _ = m.String() + case i%23 == 0: + _ = m.Clone() + } + // Don't call m.Clear(), as it would + // disentangle the various maps from each other. + } + } + return nil + }) + } + g.Wait() // no errors +} From be0d52b7f28e0282c48e226c51fd5c823904dc82 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 15 Mar 2025 09:59:38 -0400 Subject: [PATCH 247/309] gopls/internal/cache: improve build constraint trimming Generalize trimContentForPortMatch to handle +build directives. It assumed only go:build directives, but the +build variety is still valid, and in fact there is a file in the Go build on my local Google-internal machine that has them. This fixes a test that was failing for me because of that file. Change-Id: I534a18ef6e66575d242406e7b81c32055e3c8ace Reviewed-on: https://go-review.googlesource.com/c/tools/+/658195 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/port.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/gopls/internal/cache/port.go b/gopls/internal/cache/port.go index 40005bcf6d4..8caaa801b68 100644 --- a/gopls/internal/cache/port.go +++ b/gopls/internal/cache/port.go @@ -7,6 +7,7 @@ package cache import ( "bytes" "go/build" + "go/build/constraint" "go/parser" "go/token" "io" @@ -173,12 +174,16 @@ func (p port) matches(path string, content []byte) bool { // without trimming content. func trimContentForPortMatch(content []byte) []byte { buildComment := buildComment(content) - return []byte(buildComment + "\npackage p") // package name does not matter + // The package name does not matter, but +build lines + // require a blank line before the package declaration. + return []byte(buildComment + "\n\npackage p") } // buildComment returns the first matching //go:build comment in the given // content, or "" if none exists. func buildComment(content []byte) string { + var lines []string + f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly|parser.ParseComments) if err != nil { return "" @@ -186,24 +191,15 @@ func buildComment(content []byte) string { for _, cg := range f.Comments { for _, c := range cg.List { - if isGoBuildComment(c.Text) { + if constraint.IsGoBuild(c.Text) { + // A file must have only one //go:build line. return c.Text } + if constraint.IsPlusBuild(c.Text) { + // A file may have several // +build lines. + lines = append(lines, c.Text) + } } } - return "" -} - -// Adapted from go/build/build.go. -// -// TODO(rfindley): use constraint.IsGoBuild once we are on 1.19+. -func isGoBuildComment(line string) bool { - const goBuildComment = "//go:build" - if !strings.HasPrefix(line, goBuildComment) { - return false - } - // Report whether //go:build is followed by a word boundary. - line = strings.TrimSpace(line) - rest := line[len(goBuildComment):] - return len(rest) == 0 || len(strings.TrimSpace(rest)) < len(rest) + return strings.Join(lines, "\n") } From 58e40aec2e1187bbfcfa12319d977ed2bcd1e82a Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 19 Mar 2025 19:09:07 +0000 Subject: [PATCH 248/309] gopls/internal/golang/completion: avoid crash in addFieldItems For now, be defensive and avoid the crash reported in golang/go#72828. No attempt was made to reproduce. Longer term, as the TODO indicates, we should investigate the logic error that leads to addFieldItems being called with nil surrounding. Fixes golang/go#72828 Change-Id: I2300406b49fc3d53561b288d42f64793429e3fbd Reviewed-on: https://go-review.googlesource.com/c/tools/+/659237 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- gopls/internal/golang/completion/completion.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index a6c0e49c311..19c50453017 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -1177,7 +1177,10 @@ func isValidIdentifierChar(char byte) bool { // adds struct fields, interface methods, function declaration fields to completion func (c *completer) addFieldItems(fields *ast.FieldList) { - if fields == nil { + // TODO: in golang/go#72828, we get here with a nil surrounding. + // This indicates a logic bug elsewhere: we should only be interrogating the + // surrounding if it is set. + if fields == nil || c.surrounding == nil { return } From 3a64d74429d40fce4072eccecd4061b7a650444d Mon Sep 17 00:00:00 2001 From: cuishuang Date: Thu, 20 Mar 2025 21:21:51 +0800 Subject: [PATCH 249/309] all: make function comment match function name Change-Id: I16e2fd79a65693179680dfdeed84bbe0fe4e0b54 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659535 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- go/ssa/func.go | 2 +- godoc/vfs/zipfs/zipfs_test.go | 2 +- gopls/internal/cache/analysis.go | 2 +- gopls/internal/golang/completion/util.go | 2 +- gopls/internal/mod/diagnostics.go | 2 +- gopls/internal/server/link.go | 2 +- gopls/internal/settings/settings.go | 2 +- gopls/internal/telemetry/cmd/stacks/stacks.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go/ssa/func.go b/go/ssa/func.go index a6e6b149fd9..2d52309b623 100644 --- a/go/ssa/func.go +++ b/go/ssa/func.go @@ -817,7 +817,7 @@ func blockExit(fn *Function, block *BasicBlock, pos token.Pos) *exit { return e } -// blockExit creates a new exit to a yield fn that returns the source function. +// returnExit creates a new exit to a yield fn that returns the source function. func returnExit(fn *Function, pos token.Pos) *exit { e := &exit{ id: unique(fn), diff --git a/godoc/vfs/zipfs/zipfs_test.go b/godoc/vfs/zipfs/zipfs_test.go index b6f2431b0b5..cb000361745 100644 --- a/godoc/vfs/zipfs/zipfs_test.go +++ b/godoc/vfs/zipfs/zipfs_test.go @@ -59,7 +59,7 @@ func TestMain(t *testing.M) { os.Exit(t.Run()) } -// setups state each of the tests uses +// setup state each of the tests uses func setup() error { // create zipfs b := new(bytes.Buffer) diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go index 4083f49d2d6..cf5518cf79f 100644 --- a/gopls/internal/cache/analysis.go +++ b/gopls/internal/cache/analysis.go @@ -637,7 +637,7 @@ func (an *analysisNode) runCached(ctx context.Context, key file.Hash) (*analyzeS return summary, nil } -// analysisCacheKey returns a cache key that is a cryptographic digest +// cacheKey returns a cache key that is a cryptographic digest // of the all the values that might affect type checking and analysis: // the analyzer names, package metadata, names and contents of // compiled Go files, and vdeps (successor) information diff --git a/gopls/internal/golang/completion/util.go b/gopls/internal/golang/completion/util.go index 7a4729413ae..306078296c1 100644 --- a/gopls/internal/golang/completion/util.go +++ b/gopls/internal/golang/completion/util.go @@ -171,7 +171,7 @@ func deslice(T types.Type) types.Type { return nil } -// isSelector returns the enclosing *ast.SelectorExpr when pos is in the +// enclosingSelector returns the enclosing *ast.SelectorExpr when pos is in the // selector. func enclosingSelector(path []ast.Node, pos token.Pos) *ast.SelectorExpr { if len(path) == 0 { diff --git a/gopls/internal/mod/diagnostics.go b/gopls/internal/mod/diagnostics.go index a89c148d7a7..8ad1ece05e7 100644 --- a/gopls/internal/mod/diagnostics.go +++ b/gopls/internal/mod/diagnostics.go @@ -34,7 +34,7 @@ func ParseDiagnostics(ctx context.Context, snapshot *cache.Snapshot) (map[protoc return collectDiagnostics(ctx, snapshot, parseDiagnostics) } -// Diagnostics returns diagnostics from running go mod tidy. +// TidyDiagnostics returns diagnostics from running go mod tidy. func TidyDiagnostics(ctx context.Context, snapshot *cache.Snapshot) (map[protocol.DocumentURI][]*cache.Diagnostic, error) { ctx, done := event.Start(ctx, "mod.Diagnostics", snapshot.Labels()...) defer done() diff --git a/gopls/internal/server/link.go b/gopls/internal/server/link.go index c888904baab..851ec036d4d 100644 --- a/gopls/internal/server/link.go +++ b/gopls/internal/server/link.go @@ -211,7 +211,7 @@ var acceptedSchemes = map[string]bool{ "https": true, } -// urlRegexp is the user-supplied regular expression to match URL. +// findLinksInString is the user-supplied regular expression to match URL. // srcOffset is the start offset of 'src' within m's file. func findLinksInString(urlRegexp *regexp.Regexp, src string, srcOffset int, m *protocol.Mapper) ([]protocol.DocumentLink, error) { var links []protocol.DocumentLink diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 59b2aa1b87f..a47a69b0296 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -1387,7 +1387,7 @@ func (o *Options) EnabledSemanticTokenModifiers() map[semtok.Modifier]bool { return copy } -// EncodeSemanticTokenTypes returns a map of types to boolean. +// EnabledSemanticTokenTypes returns a map of types to boolean. func (o *Options) EnabledSemanticTokenTypes() map[semtok.Type]bool { copy := make(map[semtok.Type]bool, len(o.SemanticTokenTypes)) for k, v := range o.SemanticTokenTypes { diff --git a/gopls/internal/telemetry/cmd/stacks/stacks.go b/gopls/internal/telemetry/cmd/stacks/stacks.go index 36a675d0eb0..f8caabd67e6 100644 --- a/gopls/internal/telemetry/cmd/stacks/stacks.go +++ b/gopls/internal/telemetry/cmd/stacks/stacks.go @@ -529,7 +529,7 @@ func parsePredicate(s string) (func(string) bool, error) { }, nil } -// claimStack maps each stack ID to its issue (if any). +// claimStacks maps each stack ID to its issue (if any). // // It returns a map of stack text to the issue that claimed it. // From cfd8cf5ce27e4287604691e79b24027ada5c1b7f Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 20 Mar 2025 10:39:41 -0400 Subject: [PATCH 250/309] internal/astutil/cursor: split Edge into Parent{Edge,Index} The ergonomics are better, even if it requires unpacking twice. Also, add ChildAt(e edge.Kind, index int). Invariant: c.Parent.ChildAt(c.ParentEdge, c.ParentIndex)==c. Change-Id: I7e5c03515a98ceefa44e9a234db3d470cbc93578 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659575 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- go/ast/inspector/inspector.go | 2 + gopls/internal/analysis/gofix/gofix.go | 6 +- gopls/internal/analysis/modernize/rangeint.go | 4 +- .../internal/analysis/modernize/stringsseq.go | 2 +- .../analysis/modernize/testingcontext.go | 3 +- .../analysis/unusedparams/unusedparams.go | 3 +- gopls/internal/golang/implementation.go | 3 +- internal/astutil/cursor/cursor.go | 60 ++++++++++++++++--- internal/astutil/cursor/cursor_test.go | 23 +++++-- internal/astutil/cursor/hooks.go | 3 + 10 files changed, 85 insertions(+), 24 deletions(-) diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index 0d5050fe405..03511d11a2b 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -10,6 +10,7 @@ // builds a list of push/pop events and their node type. Subsequent // method calls that request a traversal scan this list, rather than walk // the AST, and perform type filtering using efficient bit sets. +// This representation is sometimes called a "balanced parenthesis tree". // // Experiments suggest the inspector's traversals are about 2.5x faster // than ast.Inspect, but it may take around 5 traversals for this @@ -50,6 +51,7 @@ type Inspector struct { //go:linkname events func events(in *Inspector) []event { return in.events } +//go:linkname packEdgeKindAndIndex func packEdgeKindAndIndex(ek edge.Kind, index int) int32 { return int32(uint32(index+1)<<7 | uint32(ek)) } diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index a2380f1d644..333b9a690e7 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -386,13 +386,13 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId cursor.Cursor) { // pkg.Id[T] // pkg.Id[K, V] var expr ast.Expr = id - if e, _ := curId.Edge(); e == edge.SelectorExpr_Sel { + if curId.ParentEdge() == edge.SelectorExpr_Sel { curId = curId.Parent() expr = curId.Node().(ast.Expr) } // If expr is part of an IndexExpr or IndexListExpr, we'll need that node. // Given C[int], TypeOf(C) is generic but TypeOf(C[int]) is instantiated. - switch ek, _ := curId.Edge(); ek { + switch curId.ParentEdge() { case edge.IndexExpr_X: expr = curId.Parent().Node().(*ast.IndexExpr) case edge.IndexListExpr_X: @@ -548,7 +548,7 @@ func (a *analyzer) inlineConst(con *types.Const, cur cursor.Cursor) { } // If n is qualified by a package identifier, we'll need the full selector expression. var expr ast.Expr = n - if e, _ := cur.Edge(); e == edge.SelectorExpr_Sel { + if cur.ParentEdge() == edge.SelectorExpr_Sel { expr = cur.Parent().Node().(ast.Expr) } a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index d51bd79433e..655f5b1c6bf 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -215,10 +215,10 @@ func isScalarLvalue(curId cursor.Cursor) bool { cur := curId // Strip enclosing parens. - ek, _ := cur.Edge() + ek := cur.ParentEdge() for ek == edge.ParenExpr_X { cur = cur.Parent() - ek, _ = cur.Edge() + ek = cur.ParentEdge() } switch ek { diff --git a/gopls/internal/analysis/modernize/stringsseq.go b/gopls/internal/analysis/modernize/stringsseq.go index ca9d918912e..51d4053eeb5 100644 --- a/gopls/internal/analysis/modernize/stringsseq.go +++ b/gopls/internal/analysis/modernize/stringsseq.go @@ -55,7 +55,7 @@ func stringsseq(pass *analysis.Pass) { if !ok { if id, ok := rng.X.(*ast.Ident); ok { if v, ok := info.Uses[id].(*types.Var); ok { - if ek, idx := curRange.Edge(); ek == edge.BlockStmt_List && idx > 0 { + if curRange.ParentEdge() == edge.BlockStmt_List && curRange.ParentIndex() > 0 { curPrev, _ := curRange.PrevSibling() if assign, ok := curPrev.Node().(*ast.AssignStmt); ok && assign.Tok == token.DEFINE && diff --git a/gopls/internal/analysis/modernize/testingcontext.go b/gopls/internal/analysis/modernize/testingcontext.go index 9bdc11ccfca..ca4ba1397e3 100644 --- a/gopls/internal/analysis/modernize/testingcontext.go +++ b/gopls/internal/analysis/modernize/testingcontext.go @@ -110,7 +110,8 @@ func testingContext(pass *analysis.Pass) { if curFunc, ok := enclosingFunc(cur); ok { switch n := curFunc.Node().(type) { case *ast.FuncLit: - if e, idx := curFunc.Edge(); e == edge.CallExpr_Args && idx == 1 { + if curFunc.ParentEdge() == edge.CallExpr_Args && + curFunc.ParentIndex() == 1 { // Have: call(..., func(...) { ...context.WithCancel(...)... }) obj := typeutil.Callee(info, curFunc.Parent().Node().(*ast.CallExpr)) if (analysisinternal.IsMethodNamed(obj, "testing", "T", "Run") || diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index 2986dfd6e41..9316e6bd5af 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -202,7 +202,8 @@ func run(pass *analysis.Pass) (any, error) { case *ast.AssignStmt: // f = func() {...} // f := func() {...} - if e, idx := c.Edge(); e == edge.AssignStmt_Rhs { + if c.ParentEdge() == edge.AssignStmt_Rhs { + idx := c.ParentIndex() // Inv: n == AssignStmt.Rhs[idx] if id, ok := parent.Lhs[idx].(*ast.Ident); ok { fn = pass.TypesInfo.ObjectOf(id) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 0ccab640709..7c414dcdb8a 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -880,8 +880,7 @@ func funcDefs(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { // beneathFuncDef reports whether the specified FuncType cursor is a // child of Func{Decl,Lit}. func beneathFuncDef(cur cursor.Cursor) bool { - ek, _ := cur.Edge() - switch ek { + switch cur.ParentEdge() { case edge.FuncDecl_Type, edge.FuncLit_Type: return true } diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 83a47e09058..f6691ce0684 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -209,17 +209,36 @@ func (c Cursor) Parent() Cursor { return Cursor{c.in, c.events()[c.index].parent} } -// Edge returns the identity of the field in the parent node -// that holds this cursor's node, and if it is a list, the index within it. +// ParentEdge returns the identity of the field in the parent node +// that holds this cursor's node. // // For example, f(x, y) is a CallExpr whose three children are Idents. -// f has edge kind [edge.CallExpr_Fun] and index -1. -// x and y have kind [edge.CallExpr_Args] and indices 0 and 1, respectively. +// f has edge kind [edge.CallExpr_Fun] and x and y have kind +// [edge.CallExpr_Args]. // -// Edge must not be called on the Root node (whose [Cursor.Node] returns nil). +// If called on a child of the Root node, it returns [edge.Invalid]. // -// If called on a child of the Root node, it returns ([edge.Invalid], -1). -func (c Cursor) Edge() (edge.Kind, int) { +// ParentEdge must not be called on the Root node (whose [Cursor.Node] returns nil). +func (c Cursor) ParentEdge() edge.Kind { + k, _ := c.parentEdgeAndIndex() + return k +} + +// ParentIndex returns the slice index of this cursor's node within +// the field of the parent node that holds it. +// +// For example, f(x, y) is a CallExpr whose three children are Idents. +// x and y have indices 0 and 1, respectively; f has index -1. +// +// If called on a child of the Root node, it returns -1. +// +// ParentIndex must not be called on the Root node (whose [Cursor.Node] returns nil). +func (c Cursor) ParentIndex() int { + _, idx := c.parentEdgeAndIndex() + return idx +} + +func (c Cursor) parentEdgeAndIndex() (edge.Kind, int) { if c.index < 0 { panic("Cursor.Edge called on Root node") } @@ -228,6 +247,31 @@ func (c Cursor) Edge() (edge.Kind, int) { return unpackEdgeKindAndIndex(events[pop].parent) } +// ChildAt returns the cursor for the child of the +// current node identified by its edge and index. +// The index must be -1 if the edge.Kind is not a slice. +// The indicated child node must exist. +// +// ChildAt must not be called on the Root node (whose [Cursor.Node] returns nil). +func (c Cursor) ChildAt(k edge.Kind, idx int) Cursor { + target := packEdgeKindAndIndex(k, idx) + + // Unfortunately there's no shortcut to looping. + events := c.events() + i := c.index + 1 + for { + pop := events[i].index + if pop < i { + break + } + if events[pop].parent == target { + return Cursor{c.in, i} + } + i = pop + 1 + } + panic(fmt.Sprintf("ChildAt(%v, %d): no such child of %v", k, idx, c)) +} + // Child returns the cursor for n, which must be a direct child of c's Node. // // Child must not be called on the Root node (whose [Cursor.Node] returns nil). @@ -355,7 +399,7 @@ func (c Cursor) LastChild() (Cursor, bool) { // So, do not assume that the previous sibling of an ast.Stmt is also // an ast.Stmt, or if it is, that they are executed sequentially, // unless you have established that, say, its parent is a BlockStmt -// or its [Cursor.Edge] is [edge.BlockStmt_List]. +// or its [Cursor.ParentEdge] is [edge.BlockStmt_List]. // For example, given "for S1; ; S2 {}", the predecessor of S2 is S1, // even though they are not executed in sequence. func (c Cursor) Children() iter.Seq[Cursor] { diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 67e91544c4d..3fd80802c15 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -131,9 +131,11 @@ func g() { _ = curFunc.Node().(*ast.FuncDecl) // Check edge and index. - if e, idx := curFunc.Edge(); e != edge.File_Decls || idx != nfuncs { - t.Errorf("%v.Edge() = (%v, %v), want edge.File_Decls, %d", - curFunc, e, idx, nfuncs) + if k := curFunc.ParentEdge(); k != edge.File_Decls { + t.Errorf("%v.ParentEdge() = %v, want edge.File_Decls", curFunc, k) + } + if idx := curFunc.ParentIndex(); idx != nfuncs { + t.Errorf("%v.ParentIndex() = %d, want %d", curFunc, idx, nfuncs) } nfuncs++ @@ -367,8 +369,11 @@ func TestCursor_Edge(t *testing.T) { continue // root node } - e, idx := cur.Edge() - parent := cur.Parent() + var ( + parent = cur.Parent() + e = cur.ParentEdge() + idx = cur.ParentIndex() + ) // ast.File, child of root? if parent.Node() == nil { @@ -384,12 +389,18 @@ func TestCursor_Edge(t *testing.T) { e.NodeType(), parent.Node()) } - // Check consistency of c.Edge.Get(c.Parent().Node()) == c.Node(). + // Check c.Edge.Get(c.Parent.Node) == c.Node. if got := e.Get(parent.Node(), idx); got != cur.Node() { t.Errorf("cur=%v@%s: %s.Get(cur.Parent().Node(), %d) = %T@%s, want cur.Node()", cur, netFset.Position(cur.Node().Pos()), e, idx, got, netFset.Position(got.Pos())) } + // Check c.Parent.ChildAt(c.ParentEdge, c.ParentIndex) == c. + if got := parent.ChildAt(e, idx); got != cur { + t.Errorf("cur=%v@%s: cur.Parent().ChildAt(%v, %d) = %T@%s, want cur", + cur, netFset.Position(cur.Node().Pos()), e, idx, got.Node(), netFset.Position(got.Node().Pos())) + } + // Check that reflection on the parent finds the current node. fv := reflect.ValueOf(parent.Node()).Elem().FieldByName(e.FieldName()) if idx >= 0 { diff --git a/internal/astutil/cursor/hooks.go b/internal/astutil/cursor/hooks.go index 8b61f5ddc11..0257d61d778 100644 --- a/internal/astutil/cursor/hooks.go +++ b/internal/astutil/cursor/hooks.go @@ -31,6 +31,9 @@ func maskOf(nodes []ast.Node) uint64 //go:linkname events golang.org/x/tools/go/ast/inspector.events func events(in *inspector.Inspector) []event +//go:linkname packEdgeKindAndIndex golang.org/x/tools/go/ast/inspector.packEdgeKindAndIndex +func packEdgeKindAndIndex(edge.Kind, int) int32 + //go:linkname unpackEdgeKindAndIndex golang.org/x/tools/go/ast/inspector.unpackEdgeKindAndIndex func unpackEdgeKindAndIndex(int32) (edge.Kind, int) From 9b2264a60f31f593728fbda4debf9e87145477fe Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 19 Mar 2025 16:29:02 -0400 Subject: [PATCH 251/309] gopls/internal/golang/completion: ensure expectedCompositeLiteralType arg is not nil It was possible for expectedCompositeLiteralType to be given a nil *compLitInfo. Check for nilness outside the offending call site and skip the call. Fixes golang/go#72136. Change-Id: Idc661145ae409a19909e0b4e1f74163acb11f5b5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659375 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- gopls/internal/golang/completion/completion.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index 19c50453017..600219370b9 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -2001,6 +2001,8 @@ func (c *completer) structLiteralFieldName(ctx context.Context) error { // enclosingCompositeLiteral returns information about the composite literal enclosing the // position. +// It returns nil on failure; for example, if there is no type information for a +// node on path. func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info) *compLitInfo { for _, n := range path { switch n := n.(type) { @@ -2565,7 +2567,7 @@ func inferExpectedResultTypes(c *completer, callNodeIdx int) []types.Type { switch node := c.path[callNodeIdx+1].(type) { case *ast.KeyValueExpr: enclosingCompositeLiteral := enclosingCompositeLiteral(c.path[callNodeIdx:], callNode.Pos(), c.pkg.TypesInfo()) - if !wantStructFieldCompletions(enclosingCompositeLiteral) { + if enclosingCompositeLiteral != nil && !wantStructFieldCompletions(enclosingCompositeLiteral) { expectedResults = append(expectedResults, expectedCompositeLiteralType(enclosingCompositeLiteral, callNode.Pos())) } case *ast.AssignStmt: From c2768b73f46671714e183a72727bf0b36e828f9b Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Fri, 21 Mar 2025 10:03:02 -0400 Subject: [PATCH 252/309] gopls/modernize: remove unused functions These functions should have been removed in a previous CL. Change-Id: I3e25a49ce2660a472b25f4b1a4a91bfdc4739fde Reviewed-on: https://go-review.googlesource.com/c/tools/+/659895 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/analysis/modernize/forvar.go | 22 --------------------- 1 file changed, 22 deletions(-) diff --git a/gopls/internal/analysis/modernize/forvar.go b/gopls/internal/analysis/modernize/forvar.go index 3a7eee4be9c..6f88ab77ed9 100644 --- a/gopls/internal/analysis/modernize/forvar.go +++ b/gopls/internal/analysis/modernize/forvar.go @@ -73,28 +73,6 @@ func forvar(pass *analysis.Pass) { } } -// if the expression is an Ident, return its name -func simplevar(expr ast.Expr) string { - if expr == nil { - return "" - } - if ident, ok := expr.(*ast.Ident); ok { - return ident.Name - } - return "" -} - -func usefulRangeVars(loop *ast.RangeStmt) []string { - ans := make([]string, 0, 2) - if v := simplevar(loop.Key); v != "" { - ans = append(ans, v) - } - if v := simplevar(loop.Value); v != "" { - ans = append(ans, v) - } - return ans -} - // if the first statement is var := var, return var and the stmt func loopVarRedecl(body *ast.BlockStmt) (*ast.Ident, *ast.AssignStmt) { if len(body.List) < 1 { From cb292c67c19fd2ca6a9be9ad561c136043fd0472 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 20 Mar 2025 13:35:25 -0400 Subject: [PATCH 253/309] internal/astutil/cursor: unsplit Parent{Edge,Index} -> ParentEdge This CL is a partial revert of CL 659575, which was merged by mistake. During review, we agreed that the original form was more logical (if sometimes inconvenient), but that we liked the new name, and the new ChildAt function. Change-Id: Ib1d4e0dfa8cefdb944eb1394be946006e8285390 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659635 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- go/ast/inspector/inspector.go | 2 +- gopls/internal/analysis/gofix/gofix.go | 6 ++-- gopls/internal/analysis/modernize/rangeint.go | 4 +-- .../internal/analysis/modernize/stringsseq.go | 2 +- .../analysis/modernize/testingcontext.go | 3 +- .../analysis/unusedparams/unusedparams.go | 3 +- gopls/internal/golang/implementation.go | 2 +- internal/astutil/cursor/cursor.go | 33 +++++-------------- internal/astutil/cursor/cursor_test.go | 12 +++---- 9 files changed, 22 insertions(+), 45 deletions(-) diff --git a/go/ast/inspector/inspector.go b/go/ast/inspector/inspector.go index 03511d11a2b..1da4a361f0b 100644 --- a/go/ast/inspector/inspector.go +++ b/go/ast/inspector/inspector.go @@ -10,7 +10,7 @@ // builds a list of push/pop events and their node type. Subsequent // method calls that request a traversal scan this list, rather than walk // the AST, and perform type filtering using efficient bit sets. -// This representation is sometimes called a "balanced parenthesis tree". +// This representation is sometimes called a "balanced parenthesis tree." // // Experiments suggest the inspector's traversals are about 2.5x faster // than ast.Inspect, but it may take around 5 traversals for this diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index 333b9a690e7..bff4120a39a 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -386,13 +386,13 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId cursor.Cursor) { // pkg.Id[T] // pkg.Id[K, V] var expr ast.Expr = id - if curId.ParentEdge() == edge.SelectorExpr_Sel { + if ek, _ := curId.ParentEdge(); ek == edge.SelectorExpr_Sel { curId = curId.Parent() expr = curId.Node().(ast.Expr) } // If expr is part of an IndexExpr or IndexListExpr, we'll need that node. // Given C[int], TypeOf(C) is generic but TypeOf(C[int]) is instantiated. - switch curId.ParentEdge() { + switch ek, _ := curId.ParentEdge(); ek { case edge.IndexExpr_X: expr = curId.Parent().Node().(*ast.IndexExpr) case edge.IndexListExpr_X: @@ -548,7 +548,7 @@ func (a *analyzer) inlineConst(con *types.Const, cur cursor.Cursor) { } // If n is qualified by a package identifier, we'll need the full selector expression. var expr ast.Expr = n - if cur.ParentEdge() == edge.SelectorExpr_Sel { + if ek, _ := cur.ParentEdge(); ek == edge.SelectorExpr_Sel { expr = cur.Parent().Node().(ast.Expr) } a.reportInline("constant", "Constant", expr, edits, importPrefix+incon.RHSName) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 655f5b1c6bf..3d3b33f4a97 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -215,10 +215,10 @@ func isScalarLvalue(curId cursor.Cursor) bool { cur := curId // Strip enclosing parens. - ek := cur.ParentEdge() + ek, _ := cur.ParentEdge() for ek == edge.ParenExpr_X { cur = cur.Parent() - ek = cur.ParentEdge() + ek, _ = cur.ParentEdge() } switch ek { diff --git a/gopls/internal/analysis/modernize/stringsseq.go b/gopls/internal/analysis/modernize/stringsseq.go index 51d4053eeb5..a26b8da705c 100644 --- a/gopls/internal/analysis/modernize/stringsseq.go +++ b/gopls/internal/analysis/modernize/stringsseq.go @@ -55,7 +55,7 @@ func stringsseq(pass *analysis.Pass) { if !ok { if id, ok := rng.X.(*ast.Ident); ok { if v, ok := info.Uses[id].(*types.Var); ok { - if curRange.ParentEdge() == edge.BlockStmt_List && curRange.ParentIndex() > 0 { + if ek, idx := curRange.ParentEdge(); ek == edge.BlockStmt_List && idx > 0 { curPrev, _ := curRange.PrevSibling() if assign, ok := curPrev.Node().(*ast.AssignStmt); ok && assign.Tok == token.DEFINE && diff --git a/gopls/internal/analysis/modernize/testingcontext.go b/gopls/internal/analysis/modernize/testingcontext.go index ca4ba1397e3..05c0b811ab7 100644 --- a/gopls/internal/analysis/modernize/testingcontext.go +++ b/gopls/internal/analysis/modernize/testingcontext.go @@ -110,8 +110,7 @@ func testingContext(pass *analysis.Pass) { if curFunc, ok := enclosingFunc(cur); ok { switch n := curFunc.Node().(type) { case *ast.FuncLit: - if curFunc.ParentEdge() == edge.CallExpr_Args && - curFunc.ParentIndex() == 1 { + if ek, idx := curFunc.ParentEdge(); ek == edge.CallExpr_Args && idx == 1 { // Have: call(..., func(...) { ...context.WithCancel(...)... }) obj := typeutil.Callee(info, curFunc.Parent().Node().(*ast.CallExpr)) if (analysisinternal.IsMethodNamed(obj, "testing", "T", "Run") || diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index 9316e6bd5af..559b65d2bc2 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -202,8 +202,7 @@ func run(pass *analysis.Pass) (any, error) { case *ast.AssignStmt: // f = func() {...} // f := func() {...} - if c.ParentEdge() == edge.AssignStmt_Rhs { - idx := c.ParentIndex() + if ek, idx := c.ParentEdge(); ek == edge.AssignStmt_Rhs { // Inv: n == AssignStmt.Rhs[idx] if id, ok := parent.Lhs[idx].(*ast.Ident); ok { fn = pass.TypesInfo.ObjectOf(id) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 7c414dcdb8a..93ac8879550 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -880,7 +880,7 @@ func funcDefs(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { // beneathFuncDef reports whether the specified FuncType cursor is a // child of Func{Decl,Lit}. func beneathFuncDef(cur cursor.Cursor) bool { - switch cur.ParentEdge() { + switch ek, _ := cur.ParentEdge(); ek { case edge.FuncDecl_Type, edge.FuncLit_Type: return true } diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index f6691ce0684..889733ed92f 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -210,37 +210,18 @@ func (c Cursor) Parent() Cursor { } // ParentEdge returns the identity of the field in the parent node -// that holds this cursor's node. +// that holds this cursor's node, and if it is a list, the index within it. // // For example, f(x, y) is a CallExpr whose three children are Idents. -// f has edge kind [edge.CallExpr_Fun] and x and y have kind -// [edge.CallExpr_Args]. +// f has edge kind [edge.CallExpr_Fun] and index -1. +// x and y have kind [edge.CallExpr_Args] and indices 0 and 1, respectively. // -// If called on a child of the Root node, it returns [edge.Invalid]. +// If called on a child of the Root node, it returns ([edge.Invalid], -1). // // ParentEdge must not be called on the Root node (whose [Cursor.Node] returns nil). -func (c Cursor) ParentEdge() edge.Kind { - k, _ := c.parentEdgeAndIndex() - return k -} - -// ParentIndex returns the slice index of this cursor's node within -// the field of the parent node that holds it. -// -// For example, f(x, y) is a CallExpr whose three children are Idents. -// x and y have indices 0 and 1, respectively; f has index -1. -// -// If called on a child of the Root node, it returns -1. -// -// ParentIndex must not be called on the Root node (whose [Cursor.Node] returns nil). -func (c Cursor) ParentIndex() int { - _, idx := c.parentEdgeAndIndex() - return idx -} - -func (c Cursor) parentEdgeAndIndex() (edge.Kind, int) { +func (c Cursor) ParentEdge() (edge.Kind, int) { if c.index < 0 { - panic("Cursor.Edge called on Root node") + panic("Cursor.ParentEdge called on Root node") } events := c.events() pop := events[c.index].index @@ -253,6 +234,8 @@ func (c Cursor) parentEdgeAndIndex() (edge.Kind, int) { // The indicated child node must exist. // // ChildAt must not be called on the Root node (whose [Cursor.Node] returns nil). +// +// Invariant: c.Parent().ChildAt(c.ParentEdge()) == c. func (c Cursor) ChildAt(k edge.Kind, idx int) Cursor { target := packEdgeKindAndIndex(k, idx) diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 3fd80802c15..76e7232bc86 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -131,11 +131,8 @@ func g() { _ = curFunc.Node().(*ast.FuncDecl) // Check edge and index. - if k := curFunc.ParentEdge(); k != edge.File_Decls { - t.Errorf("%v.ParentEdge() = %v, want edge.File_Decls", curFunc, k) - } - if idx := curFunc.ParentIndex(); idx != nfuncs { - t.Errorf("%v.ParentIndex() = %d, want %d", curFunc, idx, nfuncs) + if k, idx := curFunc.ParentEdge(); k != edge.File_Decls || idx != nfuncs { + t.Errorf("%v.ParentEdge() = (%v, %d), want edge.File_Decls, %d", curFunc, k, idx, nfuncs) } nfuncs++ @@ -371,8 +368,7 @@ func TestCursor_Edge(t *testing.T) { var ( parent = cur.Parent() - e = cur.ParentEdge() - idx = cur.ParentIndex() + e, idx = cur.ParentEdge() ) // ast.File, child of root? @@ -395,7 +391,7 @@ func TestCursor_Edge(t *testing.T) { cur, netFset.Position(cur.Node().Pos()), e, idx, got, netFset.Position(got.Pos())) } - // Check c.Parent.ChildAt(c.ParentEdge, c.ParentIndex) == c. + // Check c.Parent.ChildAt(c.ParentEdge()) == c. if got := parent.ChildAt(e, idx); got != cur { t.Errorf("cur=%v@%s: cur.Parent().ChildAt(%v, %d) = %T@%s, want cur", cur, netFset.Position(cur.Node().Pos()), e, idx, got.Node(), netFset.Position(got.Node().Pos())) From 9abefc5f9e63648ff7076e2483dae802a82142cf Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Thu, 20 Mar 2025 01:47:36 -0600 Subject: [PATCH 254/309] gopls/internal/analysis/modernize: permit int/uint type variants in rangeint This CL tracks the todo and enhances the rangeint modernizer to provide fixes for all int/uint variants, such as 'for i := int32(0); ...'. It now supports all int/uint variants when defining a new index variable with a zero value, including: - int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 In addition to the newly supported variants, this CL also handles zero float literals explicitly cast to integer/unsigned integer types, such as: - 'for i := int32(0.); ...' - 'for i := int32(.0); ...' Change-Id: I1058d78eb6d3cbd8318c8c7e0d6b951ef0a5648c Reviewed-on: https://go-review.googlesource.com/c/tools/+/659155 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Reviewed-by: Alan Donovan --- gopls/internal/analysis/modernize/bloop.go | 2 +- .../internal/analysis/modernize/modernize.go | 8 +-- gopls/internal/analysis/modernize/rangeint.go | 16 +++-- gopls/internal/analysis/modernize/slices.go | 2 +- .../testdata/src/rangeint/rangeint.go | 58 +++++++++++++++++++ .../testdata/src/rangeint/rangeint.go.golden | 58 +++++++++++++++++++ 6 files changed, 133 insertions(+), 11 deletions(-) diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index f851a6688e1..a70246b5e0e 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -101,7 +101,7 @@ func bloop(pass *analysis.Pass) { if assign, ok := n.Init.(*ast.AssignStmt); ok && assign.Tok == token.DEFINE && len(assign.Rhs) == 1 && - isZeroLiteral(assign.Rhs[0]) && + isZeroIntLiteral(info, assign.Rhs[0]) && is[*ast.IncDecStmt](n.Post) && n.Post.(*ast.IncDecStmt).Tok == token.INC && equalSyntax(n.Post.(*ast.IncDecStmt).X, assign.Lhs[0]) && diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 75f5b4014b6..4c49f6d1ecf 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -7,6 +7,7 @@ package modernize import ( _ "embed" "go/ast" + "go/constant" "go/format" "go/token" "go/types" @@ -117,10 +118,9 @@ func formatExprs(fset *token.FileSet, exprs []ast.Expr) string { return buf.String() } -// isZeroLiteral reports whether e is the literal 0. -func isZeroLiteral(e ast.Expr) bool { - lit, ok := e.(*ast.BasicLit) - return ok && lit.Kind == token.INT && lit.Value == "0" +// isZeroIntLiteral reports whether e is an integer whose value is 0. +func isZeroIntLiteral(info *types.Info, e ast.Expr) bool { + return info.Types[e].Value == constant.MakeInt64(0) } // filesUsing returns a cursor for each *ast.File in the inspector diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 3d3b33f4a97..2a500085e01 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -31,8 +31,6 @@ import ( // - The ':=' may be replaced by '='. // - The fix may remove "i :=" if it would become unused. // -// TODO(adonovan): permit variants such as "i := int64(0)". -// // Restrictions: // - The variable i must not be assigned or address-taken within the // loop, because a "for range int" loop does not respect assignments @@ -54,7 +52,7 @@ func rangeint(pass *analysis.Pass) { if init, ok := loop.Init.(*ast.AssignStmt); ok && isSimpleAssign(init) && is[*ast.Ident](init.Lhs[0]) && - isZeroLiteral(init.Rhs[0]) { + isZeroIntLiteral(info, init.Rhs[0]) { // Have: for i = 0; ... (or i := 0) index := init.Lhs[0].(*ast.Ident) @@ -145,11 +143,19 @@ func rangeint(pass *analysis.Pass) { // re-type check the expression to detect this case. var beforeLimit, afterLimit string if v := info.Types[limit].Value; v != nil { - beforeLimit, afterLimit = "int(", ")" + tVar := info.TypeOf(init.Rhs[0]) + + // TODO(adonovan): use a types.Qualifier that respects the existing + // imports of this file that are visible (not shadowed) at the current position, + // and adds new imports as needed, similar to analysisinternal.AddImport. + // (Unfortunately types.Qualifier doesn't provide the name of the package + // member to be qualified, a qualifier cannot perform the necessary shadowing + // check for dot-imported names.) + beforeLimit, afterLimit = fmt.Sprintf("%s(", types.TypeString(tVar, (*types.Package).Name)), ")" info2 := &types.Info{Types: make(map[ast.Expr]types.TypeAndValue)} if types.CheckExpr(pass.Fset, pass.Pkg, limit.Pos(), limit, info2) == nil { tLimit := types.Default(info2.TypeOf(limit)) - if types.AssignableTo(tLimit, types.Typ[types.Int]) { + if types.AssignableTo(tLimit, tVar) { beforeLimit, afterLimit = "", "" } } diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index 7e0d9cbd92e..22999b60cc5 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -216,7 +216,7 @@ func clippedSlice(info *types.Info, e ast.Expr) (res ast.Expr, empty bool) { // x[:0:0], x[:len(x):len(x)], x[:k:k] if e.Slice3 && e.High != nil && e.Max != nil && equalSyntax(e.High, e.Max) { // x[:k:k] res = e - empty = isZeroLiteral(e.High) // x[:0:0] + empty = isZeroIntLiteral(info, e.High) // x[:0:0] if call, ok := e.High.(*ast.CallExpr); ok && typeutil.Callee(info, call) == builtinLen && equalSyntax(call.Args[0], e.X) { diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index 915f122b4fc..b2a7459e5a3 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -1,9 +1,51 @@ package rangeint +import ( + "os" + os1 "os" +) + func _(i int, s struct{ i int }, slice []int) { for i := 0; i < 10; i++ { // want "for loop can be modernized using range over int" println(i) } + for j := int(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int8(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int16(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int32(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int64(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := uint8(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := uint16(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := uint32(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := uint64(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int8(0.); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := int8(.0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + for j := os.FileMode(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } + { var i int for i = 0; i < f(); i++ { // want "for loop can be modernized using range over int" @@ -21,6 +63,12 @@ func _(i int, s struct{ i int }, slice []int) { } // nope + for j := .0; j < 10; j++ { // nope: j is a float type + println(j) + } + for j := float64(0); j < 10; j++ { // nope: j is a float type + println(j) + } for i := 0; i < 10; { // nope: missing increment } for i := 0; i < 10; i-- { // nope: negative increment @@ -72,6 +120,10 @@ func issue71847d() { const limit = 1e3 // float for i := 0; i < limit; i++ { // want "for loop can be modernized using range over int" } + for i := int(0); i < limit; i++ { // want "for loop can be modernized using range over int" + } + for i := uint(0); i < limit; i++ { // want "for loop can be modernized using range over int" + } const limit2 = 1 + 0i // complex for i := 0; i < limit2; i++ { // want "for loop can be modernized using range over int" @@ -98,3 +150,9 @@ func issue72726() { for i = 0; i < arr[i]; i++ { // nope } } + +func todo() { + for j := os1.FileMode(0); j < 10; j++ { // want "for loop can be modernized using range over int" + println(j) + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index bd76ce688bb..f323879e13f 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -1,9 +1,51 @@ package rangeint +import ( + "os" + os1 "os" +) + func _(i int, s struct{ i int }, slice []int) { for i := range 10 { // want "for loop can be modernized using range over int" println(i) } + for j := range 10 { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int8(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int16(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int32(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int64(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range uint8(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range uint16(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range uint32(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range uint64(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int8(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range int8(10) { // want "for loop can be modernized using range over int" + println(j) + } + for j := range os.FileMode(10) { // want "for loop can be modernized using range over int" + println(j) + } + { var i int for i = range f() { // want "for loop can be modernized using range over int" @@ -21,6 +63,12 @@ func _(i int, s struct{ i int }, slice []int) { } // nope + for j := .0; j < 10; j++ { // nope: j is a float type + println(j) + } + for j := float64(0); j < 10; j++ { // nope: j is a float type + println(j) + } for i := 0; i < 10; { // nope: missing increment } for i := 0; i < 10; i-- { // nope: negative increment @@ -72,6 +120,10 @@ func issue71847d() { const limit = 1e3 // float for range int(limit) { // want "for loop can be modernized using range over int" } + for range int(limit) { // want "for loop can be modernized using range over int" + } + for range uint(limit) { // want "for loop can be modernized using range over int" + } const limit2 = 1 + 0i // complex for range int(limit2) { // want "for loop can be modernized using range over int" @@ -98,3 +150,9 @@ func issue72726() { for i = 0; i < arr[i]; i++ { // nope } } + +func todo() { + for j := range os.FileMode(10) { // want "for loop can be modernized using range over int" + println(j) + } +} From 084551fb2c220b27775cbeb1a4b884dd29aac742 Mon Sep 17 00:00:00 2001 From: Kyle Weingartner Date: Fri, 21 Mar 2025 10:56:03 -0700 Subject: [PATCH 255/309] go/analysis/passes/maprange: check for redundant Keys/Values calls Add an analyzer for redundant use of the functions maps.Keys and maps.Values in "for" statements with "range" clauses. Updates golang/go#72908 Change-Id: I19589dc42fa9cc2465c6d5aa1542175af6aaa6ea Reviewed-on: https://go-review.googlesource.com/c/tools/+/658695 Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../analysis/maprange/cmd/maprange/main.go | 14 ++ gopls/internal/analysis/maprange/doc.go | 37 ++++ gopls/internal/analysis/maprange/maprange.go | 155 +++++++++++++ .../analysis/maprange/maprange_test.go | 23 ++ .../analysis/maprange/testdata/basic.txtar | 209 ++++++++++++++++++ .../analysis/maprange/testdata/old.txtar | 62 ++++++ 6 files changed, 500 insertions(+) create mode 100644 gopls/internal/analysis/maprange/cmd/maprange/main.go create mode 100644 gopls/internal/analysis/maprange/doc.go create mode 100644 gopls/internal/analysis/maprange/maprange.go create mode 100644 gopls/internal/analysis/maprange/maprange_test.go create mode 100644 gopls/internal/analysis/maprange/testdata/basic.txtar create mode 100644 gopls/internal/analysis/maprange/testdata/old.txtar diff --git a/gopls/internal/analysis/maprange/cmd/maprange/main.go b/gopls/internal/analysis/maprange/cmd/maprange/main.go new file mode 100644 index 00000000000..ec1fd5ca93c --- /dev/null +++ b/gopls/internal/analysis/maprange/cmd/maprange/main.go @@ -0,0 +1,14 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The maprange command applies the golang.org/x/tools/gopls/internal/analysis/maprange +// analysis to the specified packages of Go source code. +package main + +import ( + "golang.org/x/tools/go/analysis/singlechecker" + "golang.org/x/tools/gopls/internal/analysis/maprange" +) + +func main() { singlechecker.Main(maprange.Analyzer) } diff --git a/gopls/internal/analysis/maprange/doc.go b/gopls/internal/analysis/maprange/doc.go new file mode 100644 index 00000000000..46f465059a9 --- /dev/null +++ b/gopls/internal/analysis/maprange/doc.go @@ -0,0 +1,37 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package maprange defines an Analyzer that checks for redundant use +// of the functions maps.Keys and maps.Values in "for" statements with +// "range" clauses. +// +// # Analyzer maprange +// +// maprange: checks for unnecessary calls to maps.Keys and maps.Values in range statements +// +// Consider a loop written like this: +// +// for val := range maps.Values(m) { +// fmt.Println(val) +// } +// +// This should instead be written without the call to maps.Values: +// +// for _, val := range m { +// fmt.Println(val) +// } +// +// golang.org/x/exp/maps returns slices for Keys/Values instead of iterators, +// but unnecessary calls should similarly be removed: +// +// for _, key := range maps.Keys(m) { +// fmt.Println(key) +// } +// +// should be rewritten as: +// +// for key := range m { +// fmt.Println(key) +// } +package maprange diff --git a/gopls/internal/analysis/maprange/maprange.go b/gopls/internal/analysis/maprange/maprange.go new file mode 100644 index 00000000000..c3990f9ea75 --- /dev/null +++ b/gopls/internal/analysis/maprange/maprange.go @@ -0,0 +1,155 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package maprange + +import ( + _ "embed" + "fmt" + "go/ast" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" + "golang.org/x/tools/internal/versions" +) + +//go:embed doc.go +var doc string + +var Analyzer = &analysis.Analyzer{ + Name: "maprange", + Doc: analysisinternal.MustExtractDoc(doc, "maprange"), + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/maprange", + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Run: run, +} + +// This is a variable because the package name is different in Google's code base. +var xmaps = "golang.org/x/exp/maps" + +func run(pass *analysis.Pass) (any, error) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + + switch pass.Pkg.Path() { + case "maps", xmaps: + // These packages know how to use their own APIs. + return nil, nil + } + + if !(analysisinternal.Imports(pass.Pkg, "maps") || analysisinternal.Imports(pass.Pkg, xmaps)) { + return nil, nil // fast path + } + + inspect.Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, func(n ast.Node) { + rangeStmt, ok := n.(*ast.RangeStmt) + if !ok { + return + } + call, ok := rangeStmt.X.(*ast.CallExpr) + if !ok { + return + } + callee := typeutil.Callee(pass.TypesInfo, call) + if !analysisinternal.IsFunctionNamed(callee, "maps", "Keys", "Values") && + !analysisinternal.IsFunctionNamed(callee, xmaps, "Keys", "Values") { + return + } + version := pass.Pkg.GoVersion() + pkg, fn := callee.Pkg().Path(), callee.Name() + key, value := rangeStmt.Key, rangeStmt.Value + + edits := editRangeStmt(pass, version, pkg, fn, key, value, call) + if len(edits) > 0 { + pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Message: fmt.Sprintf("unnecessary and inefficient call of %s.%s", pkg, fn), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Remove unnecessary call to %s.%s", pkg, fn), + TextEdits: edits, + }}, + }) + } + }) + + return nil, nil +} + +// editRangeStmt returns edits to transform a range statement that calls +// maps.Keys or maps.Values (either the stdlib or the x/exp/maps version). +// +// It reports a diagnostic if an edit cannot be made because the Go version is too old. +// +// It returns nil if no edits are needed. +func editRangeStmt(pass *analysis.Pass, version, pkg, fn string, key, value ast.Expr, call *ast.CallExpr) []analysis.TextEdit { + var edits []analysis.TextEdit + + // Check if the call to maps.Keys or maps.Values can be removed/replaced. + // Example: + // for range maps.Keys(m) + // ^^^^^^^^^ removeCall + // for i, _ := range maps.Keys(m) + // ^^^^^^^^^ replace with `len` + // + // If we have: for i, k := range maps.Keys(m) (only possible using x/exp/maps) + // or: for i, v = range maps.Values(m) + // do not remove the call. + removeCall := !isSet(key) || !isSet(value) + replace := "" + if pkg == xmaps && isSet(key) && value == nil { + // If we have: for i := range maps.Keys(m) (using x/exp/maps), + // Replace with: for i := range len(m) + replace = "len" + } + if removeCall { + edits = append(edits, analysis.TextEdit{ + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: []byte(replace)}) + } + // Check if the key of the range statement should be removed. + // Example: + // for _, k := range maps.Keys(m) + // ^^^ removeKey ^^^^^^^^^ removeCall + removeKey := pkg == xmaps && fn == "Keys" && !isSet(key) && isSet(value) + if removeKey { + edits = append(edits, analysis.TextEdit{ + Pos: key.Pos(), + End: value.Pos(), + }) + } + // Check if a key should be inserted to the range statement. + // Example: + // for _, v := range maps.Values(m) + // ^^^ addKey ^^^^^^^^^^^ removeCall + addKey := pkg == "maps" && fn == "Values" && isSet(key) + if addKey { + edits = append(edits, analysis.TextEdit{ + Pos: key.Pos(), + End: key.Pos(), + NewText: []byte("_, "), + }) + } + + // Range over int was added in Go 1.22. + // If the Go version is too old, report a diagnostic but do not make any edits. + if replace == "len" && versions.Before(pass.Pkg.GoVersion(), versions.Go1_22) { + pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Message: fmt.Sprintf("likely incorrect use of %s.%s (returns a slice)", pkg, fn), + }) + return nil + } + + return edits +} + +// isSet reports whether an ast.Expr is a non-nil expression that is not the blank identifier. +func isSet(expr ast.Expr) bool { + ident, ok := expr.(*ast.Ident) + return expr != nil && (!ok || ident.Name != "_") +} diff --git a/gopls/internal/analysis/maprange/maprange_test.go b/gopls/internal/analysis/maprange/maprange_test.go new file mode 100644 index 00000000000..1759dc1db99 --- /dev/null +++ b/gopls/internal/analysis/maprange/maprange_test.go @@ -0,0 +1,23 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package maprange_test + +import ( + "golang.org/x/tools/go/analysis/analysistest" + "golang.org/x/tools/gopls/internal/analysis/maprange" + "golang.org/x/tools/internal/testfiles" + "path/filepath" + "testing" +) + +func TestBasic(t *testing.T) { + dir := testfiles.ExtractTxtarFileToTmp(t, filepath.Join(analysistest.TestData(), "basic.txtar")) + analysistest.RunWithSuggestedFixes(t, dir, maprange.Analyzer, "maprange") +} + +func TestOld(t *testing.T) { + dir := testfiles.ExtractTxtarFileToTmp(t, filepath.Join(analysistest.TestData(), "old.txtar")) + analysistest.RunWithSuggestedFixes(t, dir, maprange.Analyzer, "maprange") +} diff --git a/gopls/internal/analysis/maprange/testdata/basic.txtar b/gopls/internal/analysis/maprange/testdata/basic.txtar new file mode 100644 index 00000000000..1950e958218 --- /dev/null +++ b/gopls/internal/analysis/maprange/testdata/basic.txtar @@ -0,0 +1,209 @@ +Test of fixing redundant calls to maps.Keys and maps.Values +(both stdlib "maps" and "golang.org/x/exp/maps") for Go 1.24. + +-- go.mod -- +module maprange + +require golang.org/x/exp v0.0.0 + +replace golang.org/x/exp => ./exp + +go 1.24 + +-- basic.go -- +package basic + +import "maps" + +func _() { + m := make(map[int]int) + + for range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys` + } + + for range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values` + } + + var x struct { + Map map[int]int + } + x.Map = make(map[int]int) + for x.Map[1] = range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys` + } + + for x.Map[2] = range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values` + } + + for k := range maps.Keys(m) { // want `unnecessary and inefficient call of maps.Keys` + _ = k + } + + for v := range maps.Values(m) { // want `unnecessary and inefficient call of maps.Values` + _ = v + } + + for range maps.Keys(x.Map) { // want `unnecessary and inefficient call of maps.Keys` + } + + for /* comment */ k := range /* comment */ maps.Keys(/* comment */ m) { // want `unnecessary and inefficient call of maps.Keys` + _ = k + } +} + +-- basic.go.golden -- +package basic + +import "maps" + +func _() { + m := make(map[int]int) + + for range m { // want `unnecessary and inefficient call of maps.Keys` + } + + for range m { // want `unnecessary and inefficient call of maps.Values` + } + + var x struct { + Map map[int]int + } + x.Map = make(map[int]int) + for x.Map[1] = range m { // want `unnecessary and inefficient call of maps.Keys` + } + + for _, x.Map[2] = range m { // want `unnecessary and inefficient call of maps.Values` + } + + for k := range m { // want `unnecessary and inefficient call of maps.Keys` + _ = k + } + + for _, v := range m { // want `unnecessary and inefficient call of maps.Values` + _ = v + } + + for range x.Map { // want `unnecessary and inefficient call of maps.Keys` + } + + for /* comment */ k := range /* comment */ /* comment */ m { // want `unnecessary and inefficient call of maps.Keys` + _ = k + } +} + +-- xmaps.go -- +package basic + +import "golang.org/x/exp/maps" + +func _() { + m := make(map[int]int) + + for range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } + + for i := range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + _ = i + } + + var x struct { + Map map[int]int + } + x.Map = make(map[int]int) + for _, x.Map[1] = range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for _, x.Map[2] = range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } + + for _, k := range maps.Keys(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + _ = k + } + + for _, v := range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + _ = v + } + + for range maps.Keys(x.Map) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for i, k := range maps.Keys(m) { // ok: this can't be straightforwardly rewritten + _, _ = i, k + } + + for _, _ = range maps.Values(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } +} + +-- xmaps.go.golden -- +package basic + +import "golang.org/x/exp/maps" + +func _() { + m := make(map[int]int) + + for range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } + + for i := range len(m) { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + _ = i + } + + var x struct { + Map map[int]int + } + x.Map = make(map[int]int) + for x.Map[1] = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for _, x.Map[2] = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } + + for k := range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + _ = k + } + + for _, v := range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + _ = v + } + + for range x.Map { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Keys` + } + + for i, k := range maps.Keys(m) { // ok: this can't be straightforwardly rewritten + _, _ = i, k + } + + for _, _ = range m { // want `unnecessary and inefficient call of golang.org/x/exp/maps.Values` + } +} + +-- exp/go.mod -- +module golang.org/x/exp + +go 1.24 + +-- exp/maps/maps.go -- +package maps + +func Keys[M ~map[K]V, K comparable, V any](m M) []K { + r := make([]K, 0, len(m)) + for k := range m { + r = append(r, k) + } + return r +} + +func Values[M ~map[K]V, K comparable, V any](m M) []V { + r := make([]V, 0, len(m)) + for _, v := range m { + r = append(r, v) + } + return r +} \ No newline at end of file diff --git a/gopls/internal/analysis/maprange/testdata/old.txtar b/gopls/internal/analysis/maprange/testdata/old.txtar new file mode 100644 index 00000000000..d27ff8c2a22 --- /dev/null +++ b/gopls/internal/analysis/maprange/testdata/old.txtar @@ -0,0 +1,62 @@ +Test of fixing redundant calls to maps.Keys and maps.Values +(both stdlib "maps" and "golang.org/x/exp/maps") for Go 1.21, +before range over int made suggesting a fix for a rare case easier. + +-- go.mod -- +module maprange + +require golang.org/x/exp v0.0.0 + +replace golang.org/x/exp => ./exp + +go 1.21 + +-- old.go -- +package old + +import "golang.org/x/exp/maps" + +func _() { + m := make(map[int]int) + + for i := range maps.Keys(m) { // want `likely incorrect use of golang.org/x/exp/maps.Keys \(returns a slice\)` + _ = i + } +} + +-- old.go.golden -- +package old + +import "golang.org/x/exp/maps" + +func _() { + m := make(map[int]int) + + for i := range maps.Keys(m) { // want `likely incorrect use of golang.org/x/exp/maps.Keys \(returns a slice\)` + _ = i + } +} + +-- exp/go.mod -- +module golang.org/x/exp + +go 1.21 + +-- exp/maps/maps.go -- +package maps + +func Keys[M ~map[K]V, K comparable, V any](m M) []K { + r := make([]K, 0, len(m)) + for k := range m { + r = append(r, k) + } + return r +} + +func Values[M ~map[K]V, K comparable, V any](m M) []V { + r := make([]V, 0, len(m)) + for _, v := range m { + r = append(r, v) + } + return r +} \ No newline at end of file From 20f8890687341c0dc67b20e3df0ab48651dc3618 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 21 Mar 2025 14:27:10 -0400 Subject: [PATCH 256/309] internal/astutil/cursor: add Cursor.Contains(Cursor) bool The inspector representation gives us an extremely efficient O(1) check for containment. Change-Id: If40834922c8d1a8f6a847ea674f84d6ead6cb026 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660015 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- internal/astutil/cursor/cursor.go | 12 ++++++++++ internal/astutil/cursor/cursor_test.go | 33 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 889733ed92f..144182f38cd 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -394,6 +394,18 @@ func (c Cursor) Children() iter.Seq[Cursor] { } } +// Contains reports whether c contains or is equal to c2. +// +// Both Cursors must belong to the same [Inspector]; +// neither may be its Root node. +func (c Cursor) Contains(c2 Cursor) bool { + if c.in != c2.in { + panic("different inspectors") + } + events := c.events() + return c.index <= c2.index && events[c2.index].index <= events[c.index].index +} + // FindNode returns the cursor for node n if it belongs to the subtree // rooted at c. It returns zero if n is not found. func (c Cursor) FindNode(n ast.Node) (Cursor, bool) { diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 76e7232bc86..9f540ffdc76 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -415,6 +415,39 @@ func TestCursor_Edge(t *testing.T) { if cur.Parent().Child(cur.Node()) != cur { t.Errorf("Cursor.Parent.Child = %v, want %v", cur.Parent().Child(cur.Node()), cur) } + + // Check invariants of Contains: + + // A cursor contains itself. + if !cur.Contains(cur) { + t.Errorf("!cur.Contains(cur): %v", cur) + } + // A parent contains its child, but not the inverse. + if !parent.Contains(cur) { + t.Errorf("!cur.Parent().Contains(cur): %v", cur) + } + if cur.Contains(parent) { + t.Errorf("cur.Contains(cur.Parent()): %v", cur) + } + // A grandparent contains its grandchild, but not the inverse. + if grandparent := cur.Parent(); grandparent.Node() != nil { + if !grandparent.Contains(cur) { + t.Errorf("!cur.Parent().Parent().Contains(cur): %v", cur) + } + if cur.Contains(grandparent) { + t.Errorf("cur.Contains(cur.Parent().Parent()): %v", cur) + } + } + // A cursor and its uncle/aunt do not contain each other. + if uncle, ok := parent.NextSibling(); ok { + if uncle.Contains(cur) { + t.Errorf("cur.Parent().NextSibling().Contains(cur): %v", cur) + } + if cur.Contains(uncle) { + t.Errorf("cur.Contains(cur.Parent().NextSibling()): %v", cur) + } + } + } } From ec542a7d37be24d241637dd53fad3a4ee3617e7a Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Fri, 21 Mar 2025 10:50:53 -0400 Subject: [PATCH 257/309] gopls/internal/fuzzy: apply modernizers to the fuzzy matcher Uses of b.N changed to b.Loop, one use of min(...), and several three-clause for statements were change to range over ints. Change-Id: If50f1f19232751635f7bb0ec2c27f29b575a062a Reviewed-on: https://go-review.googlesource.com/c/tools/+/659935 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/fuzzy/input.go | 6 +++--- gopls/internal/fuzzy/input_test.go | 2 +- gopls/internal/fuzzy/matcher.go | 13 +++++-------- gopls/internal/fuzzy/matcher_test.go | 3 +-- gopls/internal/fuzzy/self_test.go | 4 ++-- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/gopls/internal/fuzzy/input.go b/gopls/internal/fuzzy/input.go index c1038163f1a..fd8575f6382 100644 --- a/gopls/internal/fuzzy/input.go +++ b/gopls/internal/fuzzy/input.go @@ -36,7 +36,7 @@ func RuneRoles(candidate []byte, reuse []RuneRole) []RuneRole { } prev, prev2 := rtNone, rtNone - for i := 0; i < len(candidate); i++ { + for i := range candidate { r := rune(candidate[i]) role := RNone @@ -122,7 +122,7 @@ func LastSegment(input string, roles []RuneRole) string { func fromChunks(chunks []string, buffer []byte) []byte { ii := 0 for _, chunk := range chunks { - for i := 0; i < len(chunk); i++ { + for i := range len(chunk) { if ii >= cap(buffer) { break } @@ -143,7 +143,7 @@ func toLower(input []byte, reuse []byte) []byte { output = make([]byte, len(input)) } - for i := 0; i < len(input); i++ { + for i := range input { r := rune(input[i]) if input[i] <= unicode.MaxASCII { if 'A' <= r && r <= 'Z' { diff --git a/gopls/internal/fuzzy/input_test.go b/gopls/internal/fuzzy/input_test.go index ffe147241b6..dd751b8f0c2 100644 --- a/gopls/internal/fuzzy/input_test.go +++ b/gopls/internal/fuzzy/input_test.go @@ -127,7 +127,7 @@ func BenchmarkRoles(b *testing.B) { str := "AbstractSWTFactory" out := make([]fuzzy.RuneRole, len(str)) - for i := 0; i < b.N; i++ { + for b.Loop() { fuzzy.RuneRoles([]byte(str), out) } b.SetBytes(int64(len(str))) diff --git a/gopls/internal/fuzzy/matcher.go b/gopls/internal/fuzzy/matcher.go index 29d1b36501e..eff86efac34 100644 --- a/gopls/internal/fuzzy/matcher.go +++ b/gopls/internal/fuzzy/matcher.go @@ -134,10 +134,7 @@ func (m *Matcher) ScoreChunks(chunks []string) float32 { if sc < 0 { sc = 0 } - normalizedScore := float32(sc) * m.scoreScale - if normalizedScore > 1 { - normalizedScore = 1 - } + normalizedScore := min(float32(sc)*m.scoreScale, 1) return normalizedScore } @@ -177,7 +174,7 @@ func (m *Matcher) MatchedRanges() []int { i-- } // Reverse slice. - for i := 0; i < len(ret)/2; i++ { + for i := range len(ret) / 2 { ret[i], ret[len(ret)-1-i] = ret[len(ret)-1-i], ret[i] } return ret @@ -211,7 +208,7 @@ func (m *Matcher) computeScore(candidate []byte, candidateLower []byte) int { m.scores[0][0][0] = score(0, 0) // Start with 0. segmentsLeft, lastSegStart := 1, 0 - for i := 0; i < candLen; i++ { + for i := range candLen { if m.roles[i] == RSep { segmentsLeft++ lastSegStart = i + 1 @@ -304,7 +301,7 @@ func (m *Matcher) computeScore(candidate []byte, candidateLower []byte) int { // Third dimension encodes whether there is a gap between the previous match and the current // one. - for k := 0; k < 2; k++ { + for k := range 2 { sc := m.scores[i-1][j-1][k].val() + charScore isConsecutive := k == 1 || i-1 == 0 || i-1 == lastSegStart @@ -342,7 +339,7 @@ func (m *Matcher) ScoreTable(candidate string) string { var line1, line2, separator bytes.Buffer line1.WriteString("\t") line2.WriteString("\t") - for j := 0; j < len(m.pattern); j++ { + for j := range len(m.pattern) { line1.WriteString(fmt.Sprintf("%c\t\t", m.pattern[j])) separator.WriteString("----------------") } diff --git a/gopls/internal/fuzzy/matcher_test.go b/gopls/internal/fuzzy/matcher_test.go index 056da25d675..f743be0c5ef 100644 --- a/gopls/internal/fuzzy/matcher_test.go +++ b/gopls/internal/fuzzy/matcher_test.go @@ -293,8 +293,7 @@ func BenchmarkMatcher(b *testing.B) { matcher := fuzzy.NewMatcher(pattern) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, c := range candidates { matcher.Score(c) } diff --git a/gopls/internal/fuzzy/self_test.go b/gopls/internal/fuzzy/self_test.go index 1c64f1953df..7cdb4fdef96 100644 --- a/gopls/internal/fuzzy/self_test.go +++ b/gopls/internal/fuzzy/self_test.go @@ -14,7 +14,7 @@ func BenchmarkSelf_Matcher(b *testing.B) { idents := collectIdentifiers(b) patterns := generatePatterns() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, pattern := range patterns { sm := NewMatcher(pattern) for _, ident := range idents { @@ -28,7 +28,7 @@ func BenchmarkSelf_SymbolMatcher(b *testing.B) { idents := collectIdentifiers(b) patterns := generatePatterns() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, pattern := range patterns { sm := NewSymbolMatcher(pattern) for _, ident := range idents { From bf12eb7e7db44e3f510e54843d4064c99b782068 Mon Sep 17 00:00:00 2001 From: Zamir Ashurbekov Date: Fri, 21 Mar 2025 19:31:29 +0000 Subject: [PATCH 258/309] gopls/internal/analysis/modernize: fix slicedelete triggers on slice identifiers with side effects Add a check that the expression defining the slice has no side effects to trigger slicedelete. This is a necessary condition to ensure that the change does not change the program behavior. Fixes golang/go#72955 Change-Id: Ic326baa37e0b621fa7ba204bbfeb61c3e7daea47 GitHub-Last-Rev: 54e9082718e3d24ee82c681c494035fdc0e4e177 GitHub-Pull-Request: golang/tools#567 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659295 Reviewed-by: Cherry Mui Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- .../internal/analysis/modernize/modernize.go | 38 +++++++++++++++++ gopls/internal/analysis/modernize/slices.go | 3 ++ .../analysis/modernize/slicescontains.go | 3 ++ .../analysis/modernize/slicesdelete.go | 2 +- .../testdata/src/slicesdelete/slicesdelete.go | 8 ++++ .../src/slicesdelete/slicesdelete.go.golden | 42 +++++++++++-------- 6 files changed, 78 insertions(+), 18 deletions(-) diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 4c49f6d1ecf..16fea0d8896 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -187,3 +187,41 @@ func enabledCategory(filter, category string) bool { } return exclude } + +// noEffects reports whether the expression has no side effects, i.e., it +// does not modify the memory state. This function is conservative: it may +// return false even when the expression has no effect. +func noEffects(info *types.Info, expr ast.Expr) bool { + noEffects := true + ast.Inspect(expr, func(n ast.Node) bool { + switch v := n.(type) { + case nil, *ast.Ident, *ast.BasicLit, *ast.BinaryExpr, *ast.ParenExpr, + *ast.SelectorExpr, *ast.IndexExpr, *ast.SliceExpr, *ast.TypeAssertExpr, + *ast.StarExpr, *ast.CompositeLit, *ast.ArrayType, *ast.StructType, + *ast.MapType, *ast.InterfaceType, *ast.KeyValueExpr: + // No effect + case *ast.UnaryExpr: + // Channel send <-ch has effects + if v.Op == token.ARROW { + noEffects = false + } + case *ast.CallExpr: + // Type conversion has no effects + if !info.Types[v].IsType() { + // TODO(adonovan): Add a case for built-in functions without side + // effects (by using callsPureBuiltin from tools/internal/refactor/inline) + + noEffects = false + } + case *ast.FuncLit: + // A FuncLit has no effects, but do not descend into it. + return false + default: + // All other expressions have effects + noEffects = false + } + + return noEffects + }) + return noEffects +} diff --git a/gopls/internal/analysis/modernize/slices.go b/gopls/internal/analysis/modernize/slices.go index 22999b60cc5..18e02d51ebf 100644 --- a/gopls/internal/analysis/modernize/slices.go +++ b/gopls/internal/analysis/modernize/slices.go @@ -210,6 +210,9 @@ func appendclipped(pass *analysis.Pass) { // x[:len(x):len(x)] (nonempty) res=x // x[:k:k] (nonempty) // slices.Clip(x) (nonempty) res=x +// +// TODO(adonovan): Add a check that the expression x has no side effects in +// case x[:len(x):len(x)] -> x. Now the program behavior may change. func clippedSlice(info *types.Info, e ast.Expr) (res ast.Expr, empty bool) { switch e := e.(type) { case *ast.SliceExpr: diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index b59ea452a0f..589efe7ffc8 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -46,6 +46,9 @@ import ( // It may change cardinality of effects of the "needle" expression. // (Mostly this appears to be a desirable optimization, avoiding // redundantly repeated evaluation.) +// +// TODO(adonovan): Add a check that needle/predicate expression from +// if-statement has no effects. Now the program behavior may change. func slicescontains(pass *analysis.Pass) { // Skip the analyzer in packages where its // fixes would create an import cycle. diff --git a/gopls/internal/analysis/modernize/slicesdelete.go b/gopls/internal/analysis/modernize/slicesdelete.go index 3c3d880f62b..493009c35be 100644 --- a/gopls/internal/analysis/modernize/slicesdelete.go +++ b/gopls/internal/analysis/modernize/slicesdelete.go @@ -94,7 +94,7 @@ func slicesdelete(pass *analysis.Pass) { slice2, ok2 := call.Args[1].(*ast.SliceExpr) if ok1 && slice1.Low == nil && !slice1.Slice3 && ok2 && slice2.High == nil && !slice2.Slice3 && - equalSyntax(slice1.X, slice2.X) && + equalSyntax(slice1.X, slice2.X) && noEffects(info, slice1.X) && increasingSliceIndices(info, slice1.High, slice2.Low) { // Have append(s[:a], s[b:]...) where we can verify a < b. report(file, call, slice1, slice2) diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go index a710d06f2fe..0ee608d8f9f 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go @@ -2,6 +2,10 @@ package slicesdelete var g struct{ f []int } +func h() []int { return []int{} } + +var ch chan []int + func slicesdelete(test, other []byte, i int) { const k = 1 _ = append(test[:i], test[i+1:]...) // want "Replace append with slices.Delete" @@ -26,6 +30,10 @@ func slicesdelete(test, other []byte, i int) { _ = append(g.f[:i], g.f[i+k:]...) // want "Replace append with slices.Delete" + _ = append(h()[:i], h()[i+1:]...) // potentially has side effects + + _ = append((<-ch)[:i], (<-ch)[i+1:]...) // has side effects + _ = append(test[:3], test[i+1:]...) // cannot verify a < b _ = append(test[:i-4], test[i-1:]...) // want "Replace append with slices.Delete" diff --git a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden index 2d9447af3a3..a15eb07dee9 100644 --- a/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/slicesdelete/slicesdelete.go.golden @@ -4,35 +4,43 @@ import "slices" var g struct{ f []int } +func h() []int { return []int{} } + +var ch chan []int + func slicesdelete(test, other []byte, i int) { - const k = 1 - _ = slices.Delete(test, i, i+1) // want "Replace append with slices.Delete" + const k = 1 + _ = slices.Delete(test, i, i+1) // want "Replace append with slices.Delete" + + _ = slices.Delete(test, i+1, i+2) // want "Replace append with slices.Delete" + + _ = append(test[:i+1], test[i+1:]...) // not deleting any slice elements - _ = slices.Delete(test, i+1, i+2) // want "Replace append with slices.Delete" + _ = append(test[:i], test[i-1:]...) // not deleting any slice elements - _ = append(test[:i+1], test[i+1:]...) // not deleting any slice elements + _ = slices.Delete(test, i-1, i) // want "Replace append with slices.Delete" - _ = append(test[:i], test[i-1:]...) // not deleting any slice elements + _ = slices.Delete(test, i-2, i+1) // want "Replace append with slices.Delete" - _ = slices.Delete(test, i-1, i) // want "Replace append with slices.Delete" + _ = append(test[:i-2], other[i+1:]...) // different slices "test" and "other" - _ = slices.Delete(test, i-2, i+1) // want "Replace append with slices.Delete" + _ = append(test[:i-2], other[i+1+k:]...) // cannot verify a < b - _ = append(test[:i-2], other[i+1:]...) // different slices "test" and "other" + _ = append(test[:i-2], test[11:]...) // cannot verify a < b - _ = append(test[:i-2], other[i+1+k:]...) // cannot verify a < b + _ = slices.Delete(test, 1, 3) // want "Replace append with slices.Delete" - _ = append(test[:i-2], test[11:]...) // cannot verify a < b + _ = slices.Delete(g.f, i, i+k) // want "Replace append with slices.Delete" - _ = slices.Delete(test, 1, 3) // want "Replace append with slices.Delete" + _ = append(h()[:i], h()[i+1:]...) // potentially has side effects - _ = slices.Delete(g.f, i, i+k) // want "Replace append with slices.Delete" + _ = append((<-ch)[:i], (<-ch)[i+1:]...) // has side effects - _ = append(test[:3], test[i+1:]...) // cannot verify a < b + _ = append(test[:3], test[i+1:]...) // cannot verify a < b - _ = slices.Delete(test, i-4, i-1) // want "Replace append with slices.Delete" + _ = slices.Delete(test, i-4, i-1) // want "Replace append with slices.Delete" - _ = slices.Delete(test, 1+2, 3+4) // want "Replace append with slices.Delete" + _ = slices.Delete(test, 1+2, 3+4) // want "Replace append with slices.Delete" - _ = append(test[:1+2], test[i-1:]...) // cannot verify a < b -} \ No newline at end of file + _ = append(test[:1+2], test[i-1:]...) // cannot verify a < b +} From 961631ad41f23a4c926e37b7cf89b64351a01ce7 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Mon, 24 Mar 2025 19:07:31 +0800 Subject: [PATCH 259/309] internal/testfiles: replace outdated function with os.CopyFS Change-Id: I3e8ccfa7e529a8e0c7469fde580edb02035cbfb9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660335 LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan --- internal/testfiles/testfiles.go | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/internal/testfiles/testfiles.go b/internal/testfiles/testfiles.go index 78733976b3b..dee63c1c2f0 100644 --- a/internal/testfiles/testfiles.go +++ b/internal/testfiles/testfiles.go @@ -7,7 +7,6 @@ package testfiles import ( - "io" "io/fs" "os" "path/filepath" @@ -46,7 +45,7 @@ import ( func CopyToTmp(t testing.TB, src fs.FS, rename ...string) string { dstdir := t.TempDir() - if err := copyFS(dstdir, src); err != nil { + if err := os.CopyFS(dstdir, src); err != nil { t.Fatal(err) } for _, r := range rename { @@ -64,33 +63,6 @@ func CopyToTmp(t testing.TB, src fs.FS, rename ...string) string { return dstdir } -// Copy the files in src to dst. -// Use os.CopyFS when 1.23 can be used in x/tools. -func copyFS(dstdir string, src fs.FS) error { - return fs.WalkDir(src, ".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - newpath := filepath.Join(dstdir, path) - if d.IsDir() { - return os.MkdirAll(newpath, 0777) - } - r, err := src.Open(path) - if err != nil { - return err - } - defer r.Close() - - w, err := os.Create(newpath) - if err != nil { - return err - } - defer w.Close() - _, err = io.Copy(w, r) - return err - }) -} - // ExtractTxtarFileToTmp read a txtar archive on a given path, // extracts it to a temporary directory, and returns the // temporary directory. From baedf716f743020c064dcf41a2251d633c42c826 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 12 Mar 2025 09:15:19 -0400 Subject: [PATCH 260/309] gopls/internal/golang: unify tracks type params Enhance unify to take type params into account. Unify(x, y) will return false if there is no assignment to the type parameters of x and y that will make x identical to y. (Except that type param constraints and interface literals are not handled; unify "fails open" for these, returning true when the right answer might be false. That is the same behavior as previously.) The API supports initial bindings for type params, in order to handle types like C in instantiations of F: func F[T any]() { type C *T } The implementation matches that in internal/util/fingerprint.go, except that it works on actual types.Type values instead of the reconstituted fingerprint types. This CL tests the return value when there are no initial bindings. Subsequent CLs will test the final values of bindings, with and without initial values. Change-Id: I2402f818b86a5ccac874491e0801bb503b449cd6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/657076 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/golang/implementation.go | 379 +++++++++++++----- gopls/internal/golang/implementation_test.go | 140 +++++++ .../internal/util/fingerprint/fingerprint.go | 2 +- .../util/fingerprint/fingerprint_test.go | 2 +- gopls/internal/util/moreiters/iters.go | 21 + 5 files changed, 440 insertions(+), 104 deletions(-) create mode 100644 gopls/internal/golang/implementation_test.go diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 93ac8879550..53fde6c147b 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -11,6 +11,7 @@ import ( "go/ast" "go/token" "go/types" + "iter" "reflect" "slices" "sort" @@ -26,6 +27,7 @@ import ( "golang.org/x/tools/gopls/internal/file" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/util/bug" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/gopls/internal/util/safetoken" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" @@ -497,133 +499,306 @@ func concreteImplementsIntf(msets *typeutil.MethodSetCache, x, y types.Type) boo if !ok { return false // x lacks a method of y } - if !unify(xm.Signature(), ym.Signature()) { + if !unify(xm.Signature(), ym.Signature(), nil) { return false // signatures do not match } } return true // all methods found } -// unify reports whether the types of x and y match, allowing free -// type parameters to stand for anything at all, without regard to -// consistency of substitutions. +// unify reports whether the types of x and y match. // -// TODO(adonovan): implement proper unification (#63982), finding the -// most general unifier across all the interface methods. +// If unifier is nil, unify reports only whether it succeeded. +// If unifier is non-nil, it is populated with the values +// of type parameters determined during a successful unification. +// If unification succeeds without binding a type parameter, that parameter +// will not be present in the map. // -// See also: unify in cache/methodsets/fingerprint, which uses a -// similar ersatz unification approach on type fingerprints, for -// the global index. -func unify(x, y types.Type) bool { - x = types.Unalias(x) - y = types.Unalias(y) - - // For now, allow a type parameter to match anything, - // without regard to consistency of substitutions. - if is[*types.TypeParam](x) || is[*types.TypeParam](y) { - return true +// On entry, the unifier's contents are treated as the values of already-bound type +// parameters, constraining the unification. +// +// For example, if unifier is an empty (not nil) map on entry, then the types +// +// func[T any](T, int) +// +// and +// +// func[U any](bool, U) +// +// will unify, with T=bool and U=int. +// That is, the contents of unifier after unify returns will be +// +// {T: bool, U: int} +// +// where "T" is the type parameter T and "bool" is the basic type for bool. +// +// But if unifier is {T: int} is int on entry, then unification will fail, because T +// does not unify with bool. +// +// See also: unify in cache/methodsets/fingerprint, which implements +// unification for type fingerprints, for the global index. +// +// BUG: literal interfaces are not handled properly. But this function is currently +// used only for signatures, where such types are very rare. +func unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool { + // bindings[tp] is the binding for type parameter tp. + // Although type parameters are nominally bound to types, each bindings[tp] + // is a pointer to a type, so unbound variables that unify can share a binding. + bindings := map[*types.TypeParam]*types.Type{} + + // Bindings is initialized with pointers to the provided types. + for tp, t := range unifier { + bindings[tp] = &t } - if reflect.TypeOf(x) != reflect.TypeOf(y) { - return false // mismatched types - } - - switch x := x.(type) { - case *types.Array: - y := y.(*types.Array) - return x.Len() == y.Len() && - unify(x.Elem(), y.Elem()) - - case *types.Basic: - y := y.(*types.Basic) - return x.Kind() == y.Kind() - - case *types.Chan: - y := y.(*types.Chan) - return x.Dir() == y.Dir() && - unify(x.Elem(), y.Elem()) - - case *types.Interface: - y := y.(*types.Interface) - // TODO(adonovan): fix: for correctness, we must check - // that both interfaces have the same set of methods - // modulo type parameters, while avoiding the risk of - // unbounded interface recursion. - // - // Since non-empty interface literals are vanishingly - // rare in methods signatures, we ignore this for now. - // If more precision is needed we could compare method - // names and arities, still without full recursion. - return x.NumMethods() == y.NumMethods() - - case *types.Map: - y := y.(*types.Map) - return unify(x.Key(), y.Key()) && - unify(x.Elem(), y.Elem()) - - case *types.Named: - y := y.(*types.Named) - if x.Origin() != y.Origin() { - return false // different named types + // bindingFor returns the *types.Type in bindings for tp if tp is not nil, + // creating one if needed. + bindingFor := func(tp *types.TypeParam) *types.Type { + if tp == nil { + return nil } - xtargs := x.TypeArgs() - ytargs := y.TypeArgs() - if xtargs.Len() != ytargs.Len() { - return false // arity error (ill-typed) + b := bindings[tp] + if b == nil { + b = new(types.Type) + bindings[tp] = b } - for i := range xtargs.Len() { - if !unify(xtargs.At(i), ytargs.At(i)) { - return false // mismatched type args + return b + } + + // bind sets b to t if b does not occur in t. + bind := func(b *types.Type, t types.Type) bool { + for tp := range typeParams(t) { + if b == bindings[tp] { + return false // failed "occurs" check } } + *b = t return true + } - case *types.Pointer: - y := y.(*types.Pointer) - return unify(x.Elem(), y.Elem()) - - case *types.Signature: - y := y.(*types.Signature) - return x.Variadic() == y.Variadic() && - unify(x.Params(), y.Params()) && - unify(x.Results(), y.Results()) + // uni performs the actual unification. + var uni func(x, y types.Type) bool + uni = func(x, y types.Type) bool { + x = types.Unalias(x) + y = types.Unalias(y) + + tpx, _ := x.(*types.TypeParam) + tpy, _ := y.(*types.TypeParam) + if tpx != nil || tpy != nil { + bx := bindingFor(tpx) + by := bindingFor(tpy) + + // If both args are type params and neither is bound, have them share a binding. + if bx != nil && by != nil && *bx == nil && *by == nil { + // Arbitrarily give y's binding to x. + bindings[tpx] = by + return true + } + // Treat param bindings like original args in what follows. + if bx != nil && *bx != nil { + x = *bx + } + if by != nil && *by != nil { + y = *by + } + // If the x param is unbound, bind it to y. + if bx != nil && *bx == nil { + return bind(bx, y) + } + // If the y param is unbound, bind it to x. + if by != nil && *by == nil { + return bind(by, x) + } + // Unify the binding of a bound parameter. + return uni(x, y) + } - case *types.Slice: - y := y.(*types.Slice) - return unify(x.Elem(), y.Elem()) + // Neither arg is a type param. - case *types.Struct: - y := y.(*types.Struct) - if x.NumFields() != y.NumFields() { - return false + if reflect.TypeOf(x) != reflect.TypeOf(y) { + return false // mismatched types } - for i := range x.NumFields() { - xf := x.Field(i) - yf := y.Field(i) - if xf.Embedded() != yf.Embedded() || - xf.Name() != yf.Name() || - x.Tag(i) != y.Tag(i) || - !xf.Exported() && xf.Pkg() != yf.Pkg() || - !unify(xf.Type(), yf.Type()) { + + switch x := x.(type) { + case *types.Array: + y := y.(*types.Array) + return x.Len() == y.Len() && + uni(x.Elem(), y.Elem()) + + case *types.Basic: + y := y.(*types.Basic) + return x.Kind() == y.Kind() + + case *types.Chan: + y := y.(*types.Chan) + return x.Dir() == y.Dir() && + uni(x.Elem(), y.Elem()) + + case *types.Interface: + y := y.(*types.Interface) + // TODO(adonovan,jba): fix: for correctness, we must check + // that both interfaces have the same set of methods + // modulo type parameters, while avoiding the risk of + // unbounded interface recursion. + // + // Since non-empty interface literals are vanishingly + // rare in methods signatures, we ignore this for now. + // If more precision is needed we could compare method + // names and arities, still without full recursion. + return x.NumMethods() == y.NumMethods() + + case *types.Map: + y := y.(*types.Map) + return uni(x.Key(), y.Key()) && + uni(x.Elem(), y.Elem()) + + case *types.Named: + y := y.(*types.Named) + if x.Origin() != y.Origin() { + return false // different named types + } + xtargs := x.TypeArgs() + ytargs := y.TypeArgs() + if xtargs.Len() != ytargs.Len() { + return false // arity error (ill-typed) + } + for i := range xtargs.Len() { + if !uni(xtargs.At(i), ytargs.At(i)) { + return false // mismatched type args + } + } + return true + + case *types.Pointer: + y := y.(*types.Pointer) + return uni(x.Elem(), y.Elem()) + + case *types.Signature: + y := y.(*types.Signature) + return x.Variadic() == y.Variadic() && + uni(x.Params(), y.Params()) && + uni(x.Results(), y.Results()) + + case *types.Slice: + y := y.(*types.Slice) + return uni(x.Elem(), y.Elem()) + + case *types.Struct: + y := y.(*types.Struct) + if x.NumFields() != y.NumFields() { return false } + for i := range x.NumFields() { + xf := x.Field(i) + yf := y.Field(i) + if xf.Embedded() != yf.Embedded() || + xf.Name() != yf.Name() || + x.Tag(i) != y.Tag(i) || + !xf.Exported() && xf.Pkg() != yf.Pkg() || + !uni(xf.Type(), yf.Type()) { + return false + } + } + return true + + case *types.Tuple: + y := y.(*types.Tuple) + if x.Len() != y.Len() { + return false + } + for i := range x.Len() { + if !uni(x.At(i).Type(), y.At(i).Type()) { + return false + } + } + return true + + default: // incl. *Union, *TypeParam + panic(fmt.Sprintf("unexpected Type %#v", x)) } - return true + } - case *types.Tuple: - y := y.(*types.Tuple) - if x.Len() != y.Len() { - return false + if !uni(x, y) { + return false + } + + // Populate the input map with the resulting types. + if unifier != nil { + for tparam, tptr := range bindings { + unifier[tparam] = *tptr } - for i := range x.Len() { - if !unify(x.At(i).Type(), y.At(i).Type()) { - return false + } + return true +} + +// typeParams yields all the free type parameters within t that are relevant for +// unification. +func typeParams(t types.Type) iter.Seq[*types.TypeParam] { + + return func(yield func(*types.TypeParam) bool) { + seen := map[*types.TypeParam]bool{} // yield each type param only once + + // tps(t) yields each TypeParam in t and returns false to stop. + var tps func(types.Type) bool + tps = func(t types.Type) bool { + t = types.Unalias(t) + + switch t := t.(type) { + case *types.TypeParam: + if seen[t] { + return true + } + seen[t] = true + return yield(t) + + case *types.Basic: + return true + + case *types.Array: + return tps(t.Elem()) + + case *types.Chan: + return tps(t.Elem()) + + case *types.Interface: + // TODO(jba): implement. + return true + + case *types.Map: + return tps(t.Key()) && tps(t.Elem()) + + case *types.Named: + if t.Origin() == t { + // generic type: look at type params + return moreiters.Every(t.TypeParams().TypeParams(), + func(tp *types.TypeParam) bool { return tps(tp) }) + } + // instantiated type: look at type args + return moreiters.Every(t.TypeArgs().Types(), tps) + + case *types.Pointer: + return tps(t.Elem()) + + case *types.Signature: + return tps(t.Params()) && tps(t.Results()) + + case *types.Slice: + return tps(t.Elem()) + + case *types.Struct: + return moreiters.Every(t.Fields(), + func(v *types.Var) bool { return tps(v.Type()) }) + + case *types.Tuple: + return moreiters.Every(t.Variables(), + func(v *types.Var) bool { return tps(v.Type()) }) + + default: // incl. *Union + panic(fmt.Sprintf("unexpected Type %#v", t)) } } - return true - default: // incl. *Union, *TypeParam - panic(fmt.Sprintf("unexpected Type %#v", x)) + tps(t) } } @@ -822,7 +997,7 @@ func funcUses(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { if ftyp == nil { continue // missing type information } - if unify(t, ftyp) { + if unify(t, ftyp, nil) { loc, err := pgf.PosLocation(pos, end) if err != nil { return nil, err @@ -856,7 +1031,7 @@ func funcDefs(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { if ftyp == nil { continue // missing type information } - if unify(t, ftyp) { + if unify(t, ftyp, nil) { pos := fn.Pos() loc, err := pgf.PosLocation(pos, pos+token.Pos(len("func"))) if err != nil { diff --git a/gopls/internal/golang/implementation_test.go b/gopls/internal/golang/implementation_test.go new file mode 100644 index 00000000000..08b1d281204 --- /dev/null +++ b/gopls/internal/golang/implementation_test.go @@ -0,0 +1,140 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package golang + +import ( + "go/types" + "testing" + + "golang.org/x/tools/internal/testfiles" + "golang.org/x/tools/txtar" +) + +// TODO(jba): test unify with some params already bound. + +func TestUnifyEmptyInfo(t *testing.T) { + // Check unify with no initial bound type params. + // This is currently the only case in use. + // Test cases from TestMatches in gopls/internal/util/fingerprint/fingerprint_test.go. + const src = ` +-- go.mod -- +module example.com +go 1.24 + +-- a/a.go -- +package a + +type Int = int +type String = string + +// Eq.Equal matches casefold.Equal. +type Eq[T any] interface { Equal(T, T) bool } +type casefold struct{} +func (casefold) Equal(x, y string) bool + +// A matches AString. +type A[T any] = struct { x T } +type AString = struct { x string } + +// B matches anything! +type B[T any] = T + +func C1[T any](int, T, ...string) T { panic(0) } +func C2[U any](int, int, ...U) bool { panic(0) } +func C3(int, bool, ...string) rune +func C4(int, bool, ...string) +func C5(int, float64, bool, string) bool +func C6(int, bool, ...string) bool + +func DAny[T any](Named[T]) { panic(0) } +func DString(Named[string]) +func DInt(Named[int]) + +type Named[T any] struct { x T } + +func E1(byte) rune +func E2(uint8) int32 +func E3(int8) uint32 + +// generic vs. generic +func F1[T any](T) { panic(0) } +func F2[T any](*T) { panic(0) } +func F3[T any](T, T) { panic(0) } +func F4[U any](U, *U) {panic(0) } +func F5[T, U any](T, U, U) { panic(0) } +func F6[T any](T, int, T) { panic(0) } +func F7[T any](bool, T, T) { panic(0) } +func F8[V any](*V, int, int) { panic(0) } +func F9[V any](V, *V, V) { panic(0) } +` + pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] + scope := pkg.Types.Scope() + for _, test := range []struct { + a, b string + method string // optional field or method + want bool + }{ + {"Eq", "casefold", "Equal", true}, + {"A", "AString", "", true}, + {"A", "Eq", "", false}, // completely unrelated + {"B", "String", "", true}, + {"B", "Int", "", true}, + {"B", "A", "", true}, + {"C1", "C2", "", false}, + {"C1", "C3", "", false}, + {"C1", "C4", "", false}, + {"C1", "C5", "", false}, + {"C1", "C6", "", true}, + {"C2", "C3", "", false}, + {"C2", "C4", "", false}, + {"C3", "C4", "", false}, + {"DAny", "DString", "", true}, + {"DAny", "DInt", "", true}, + {"DString", "DInt", "", false}, // different instantiations of Named + {"E1", "E2", "", true}, // byte and rune are just aliases + {"E2", "E3", "", false}, + // // The following tests cover all of the type param cases of unify. + {"F1", "F2", "", true}, // F1[*int] = F2[int] + {"F3", "F4", "", false}, // would require U identical to *U, prevented by occur check + {"F5", "F6", "", true}, // one param is bound, the other is not + {"F6", "F7", "", false}, // both are bound + {"F5", "F8", "", true}, // T=*int, U=int, V=int + {"F5", "F9", "", false}, // T is unbound, V is bound, and T occurs in V + } { + lookup := func(name string) types.Type { + obj := scope.Lookup(name) + if obj == nil { + t.Fatalf("Lookup %s failed", name) + } + if test.method != "" { + obj, _, _ = types.LookupFieldOrMethod(obj.Type(), true, pkg.Types, test.method) + if obj == nil { + t.Fatalf("Lookup %s.%s failed", name, test.method) + } + } + return obj.Type() + } + + check := func(sa, sb string, want bool) { + t.Helper() + + a := lookup(sa) + b := lookup(sb) + + got := unify(a, b, nil) + if got != want { + t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", + sa, sb, test.method, got, a, b) + } + } + + check(test.a, test.b, test.want) + // unify is symmetric + check(test.b, test.a, test.want) + // unify is reflexive + check(test.a, test.a, true) + check(test.b, test.b, true) + } +} diff --git a/gopls/internal/util/fingerprint/fingerprint.go b/gopls/internal/util/fingerprint/fingerprint.go index 22817e4cb2f..b279003d081 100644 --- a/gopls/internal/util/fingerprint/fingerprint.go +++ b/gopls/internal/util/fingerprint/fingerprint.go @@ -352,7 +352,7 @@ func unify(x, y sexpr) bool { if c, ok := x.(*cons); ok { return max(maxTypeParam(c.car), maxTypeParam(c.cdr)) } - return 0 + return -1 } // xBindings[i] is the binding for type parameter #i in x, and similarly for y. diff --git a/gopls/internal/util/fingerprint/fingerprint_test.go b/gopls/internal/util/fingerprint/fingerprint_test.go index 737c6896157..40ea2ede34e 100644 --- a/gopls/internal/util/fingerprint/fingerprint_test.go +++ b/gopls/internal/util/fingerprint/fingerprint_test.go @@ -120,7 +120,7 @@ func E3(int8) uint32 func F1[T any](T) { panic(0) } func F2[T any](*T) { panic(0) } func F3[T any](T, T) { panic(0) } -func F4[U any](U, *U) {panic(0) } +func F4[U any](U, *U) { panic(0) } func F5[T, U any](T, U, U) { panic(0) } func F6[T any](T, int, T) { panic(0) } func F7[T any](bool, T, T) { panic(0) } diff --git a/gopls/internal/util/moreiters/iters.go b/gopls/internal/util/moreiters/iters.go index d41cb1d3bca..69c76ccb9b6 100644 --- a/gopls/internal/util/moreiters/iters.go +++ b/gopls/internal/util/moreiters/iters.go @@ -24,3 +24,24 @@ func Contains[T comparable](seq iter.Seq[T], x T) bool { } return false } + +// Every reports whether every pred(t) for t in seq returns true, +// stopping at the first false element. +func Every[T any](seq iter.Seq[T], pred func(T) bool) bool { + for t := range seq { + if !pred(t) { + return false + } + } + return true +} + +// Any reports whether any pred(t) for t in seq returns true. +func Any[T any](seq iter.Seq[T], pred func(T) bool) bool { + for t := range seq { + if pred(t) { + return true + } + } + return false +} From 95701555d661b1971669aeb90053be92e2f885c8 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 13 Mar 2025 16:40:39 -0400 Subject: [PATCH 261/309] gopls/internal/golang: test unify result bindings Add checks to the test of unify that verify the type param bindings that it reports. Still to be done: tests of initial bindings. Change-Id: I8251220b5e849579cda719669ec7ca5626667ec1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/657637 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/implementation.go | 7 + gopls/internal/golang/implementation_test.go | 194 +++++++++++++++---- 2 files changed, 158 insertions(+), 43 deletions(-) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 53fde6c147b..8453c571ba7 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -535,6 +535,13 @@ func concreteImplementsIntf(msets *typeutil.MethodSetCache, x, y types.Type) boo // But if unifier is {T: int} is int on entry, then unification will fail, because T // does not unify with bool. // +// Unify does not preserve aliases. For example, given the following: +// +// type String = string +// type A[T] = T +// +// unification succeeds with T bound to string, not String. +// // See also: unify in cache/methodsets/fingerprint, which implements // unification for type fingerprints, for the global index. // diff --git a/gopls/internal/golang/implementation_test.go b/gopls/internal/golang/implementation_test.go index 08b1d281204..80403443cd9 100644 --- a/gopls/internal/golang/implementation_test.go +++ b/gopls/internal/golang/implementation_test.go @@ -6,17 +6,14 @@ package golang import ( "go/types" + "maps" "testing" "golang.org/x/tools/internal/testfiles" "golang.org/x/tools/txtar" ) -// TODO(jba): test unify with some params already bound. - -func TestUnifyEmptyInfo(t *testing.T) { - // Check unify with no initial bound type params. - // This is currently the only case in use. +func TestUnify(t *testing.T) { // Test cases from TestMatches in gopls/internal/util/fingerprint/fingerprint_test.go. const src = ` -- go.mod -- @@ -69,39 +66,135 @@ func F7[T any](bool, T, T) { panic(0) } func F8[V any](*V, int, int) { panic(0) } func F9[V any](V, *V, V) { panic(0) } ` + type tmap = map[*types.TypeParam]types.Type + + var ( + boolType = types.Typ[types.Bool] + intType = types.Typ[types.Int] + stringType = types.Typ[types.String] + ) pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] scope := pkg.Types.Scope() + + tparam := func(name string, index int) *types.TypeParam { + obj := scope.Lookup(name) + var tps *types.TypeParamList + switch obj := obj.(type) { + case *types.Func: + tps = obj.Signature().TypeParams() + case *types.TypeName: + if n, ok := obj.Type().(*types.Named); ok { + tps = n.TypeParams() + } else { + tps = obj.Type().(*types.Alias).TypeParams() + } + default: + t.Fatalf("unsupported object of type %T", obj) + } + return tps.At(index) + } + for _, test := range []struct { - a, b string - method string // optional field or method - want bool + x, y string // the symbols in the above source code whose types to unify + method string // optional field or method + params tmap // initial values of type params + want bool // success or failure + wantParams tmap // expected output }{ - {"Eq", "casefold", "Equal", true}, - {"A", "AString", "", true}, - {"A", "Eq", "", false}, // completely unrelated - {"B", "String", "", true}, - {"B", "Int", "", true}, - {"B", "A", "", true}, - {"C1", "C2", "", false}, - {"C1", "C3", "", false}, - {"C1", "C4", "", false}, - {"C1", "C5", "", false}, - {"C1", "C6", "", true}, - {"C2", "C3", "", false}, - {"C2", "C4", "", false}, - {"C3", "C4", "", false}, - {"DAny", "DString", "", true}, - {"DAny", "DInt", "", true}, - {"DString", "DInt", "", false}, // different instantiations of Named - {"E1", "E2", "", true}, // byte and rune are just aliases - {"E2", "E3", "", false}, - // // The following tests cover all of the type param cases of unify. - {"F1", "F2", "", true}, // F1[*int] = F2[int] - {"F3", "F4", "", false}, // would require U identical to *U, prevented by occur check - {"F5", "F6", "", true}, // one param is bound, the other is not - {"F6", "F7", "", false}, // both are bound - {"F5", "F8", "", true}, // T=*int, U=int, V=int - {"F5", "F9", "", false}, // T is unbound, V is bound, and T occurs in V + { + // In Eq[T], T is bound to string. + x: "Eq", + y: "casefold", + method: "Equal", + want: true, + wantParams: tmap{tparam("Eq", 0): stringType}, + }, + { + // If we unify A[T] and A[string], T should be bound to string. + x: "A", + y: "AString", + want: true, + wantParams: tmap{tparam("A", 0): stringType}, + }, + {x: "A", y: "Eq", want: false}, // completely unrelated + { + x: "B", + y: "String", + want: true, + wantParams: tmap{tparam("B", 0): stringType}, + }, + { + x: "B", + y: "Int", + want: true, + wantParams: tmap{tparam("B", 0): intType}, + }, + { + x: "B", + y: "A", + want: true, + // B's T is bound to A's struct { x T } + wantParams: tmap{tparam("B", 0): scope.Lookup("A").Type().Underlying()}, + }, + { + // C1's U unifies with C6's bool. + x: "C1", + y: "C6", + wantParams: tmap{tparam("C1", 0): boolType}, + want: true, + }, + // C1 fails to unify with C2 because C1's T must be bound to both int and bool. + {x: "C1", y: "C2", want: false}, + // The remaining "C" cases fail for less interesting reasons, usually different numbers + // or types of parameters or results. + {x: "C1", y: "C3", want: false}, + {x: "C1", y: "C4", want: false}, + {x: "C1", y: "C5", want: false}, + {x: "C2", y: "C3", want: false}, + {x: "C2", y: "C4", want: false}, + {x: "C3", y: "C4", want: false}, + { + x: "DAny", + y: "DString", + want: true, + wantParams: tmap{tparam("DAny", 0): stringType}, + }, + {x: "DString", y: "DInt", want: false}, // different instantiations of Named + {x: "E1", y: "E2", want: true}, // byte and rune are just aliases + {x: "E2", y: "E3", want: false}, + + // The following tests cover all of the type param cases of unify. + { + // F1[*int] = F2[int], for example + // F1's T is bound to a pointer to F2's T. + x: "F1", + y: "F2", + want: true, + wantParams: tmap{tparam("F1", 0): types.NewPointer(tparam("F2", 0))}, + }, + {x: "F3", y: "F4", want: false}, // would require U identical to *U, prevented by occur check + { + x: "F5", + y: "F6", + want: true, + wantParams: tmap{ + tparam("F5", 0): intType, + tparam("F5", 1): intType, + tparam("F6", 0): intType, + }, + }, + {x: "F6", y: "F7", want: false}, // both are bound + { + // T=*V, U=int, V=int + x: "F5", + y: "F8", + want: true, + wantParams: tmap{ + tparam("F5", 0): types.NewPointer(tparam("F8", 0)), + tparam("F5", 1): intType, + }, + }, + {x: "F5", y: "F9", want: false}, // T is unbound, V is bound, and T occurs in V } { lookup := func(name string) types.Type { obj := scope.Lookup(name) @@ -117,24 +210,39 @@ func F9[V any](V, *V, V) { panic(0) } return obj.Type() } - check := func(sa, sb string, want bool) { + check := func(a, b string, want, compareParams bool) { t.Helper() - a := lookup(sa) - b := lookup(sb) + ta := lookup(a) + tb := lookup(b) - got := unify(a, b, nil) + var gotParams tmap + if test.params == nil { + // Get the unifier even if there are no input params. + gotParams = tmap{} + } else { + gotParams = maps.Clone(test.params) + } + got := unify(ta, tb, gotParams) if got != want { t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", - sa, sb, test.method, got, a, b) + a, b, test.method, got, a, b) + return + } + if !compareParams { + return + } + if !maps.EqualFunc(gotParams, test.wantParams, types.Identical) { + t.Errorf("x=%s y=%s method=%s: xParams: got %v, want %v", + a, b, test.method, gotParams, test.wantParams) } } - check(test.a, test.b, test.want) + check(test.x, test.y, test.want, true) // unify is symmetric - check(test.b, test.a, test.want) + check(test.y, test.x, test.want, true) // unify is reflexive - check(test.a, test.a, true) - check(test.b, test.b, true) + check(test.x, test.x, true, false) + check(test.y, test.y, true, false) } } From 45b8eacdc2a69465d7ec78ecf6ffe7b2c2af252e Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 13 Mar 2025 17:29:34 -0400 Subject: [PATCH 262/309] gopls/internal/golang: test initial bindings to unify Add tests to unify that check that bindings provided on input behave as expected. One test case uncovered an infinite recursion. Fixed that, but in case there are more, added a depth check. For golang/go#63982. Change-Id: Ib3685948243391c450d5a85d30dad0eaea3c459a Reviewed-on: https://go-review.googlesource.com/c/tools/+/657638 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/implementation.go | 14 +++++ gopls/internal/golang/implementation_test.go | 65 ++++++++++++++++++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 8453c571ba7..b9a332ac62a 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -584,14 +584,27 @@ func unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool { } // uni performs the actual unification. + depth := 0 var uni func(x, y types.Type) bool uni = func(x, y types.Type) bool { + // Panic if recursion gets too deep, to detect bugs before + // overflowing the stack. + depth++ + defer func() { depth-- }() + if depth > 100 { + panic("unify: max depth exceeded") + } + x = types.Unalias(x) y = types.Unalias(y) tpx, _ := x.(*types.TypeParam) tpy, _ := y.(*types.TypeParam) if tpx != nil || tpy != nil { + // Identical type params unify. + if tpx == tpy { + return true + } bx := bindingFor(tpx) by := bindingFor(tpy) @@ -726,6 +739,7 @@ func unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool { } if !uni(x, y) { + clear(unifier) return false } diff --git a/gopls/internal/golang/implementation_test.go b/gopls/internal/golang/implementation_test.go index 80403443cd9..b7253bb8bf7 100644 --- a/gopls/internal/golang/implementation_test.go +++ b/gopls/internal/golang/implementation_test.go @@ -14,7 +14,7 @@ import ( ) func TestUnify(t *testing.T) { - // Test cases from TestMatches in gopls/internal/util/fingerprint/fingerprint_test.go. + // Most cases from TestMatches in gopls/internal/util/fingerprint/fingerprint_test.go. const src = ` -- go.mod -- module example.com @@ -60,6 +60,7 @@ func F1[T any](T) { panic(0) } func F2[T any](*T) { panic(0) } func F3[T any](T, T) { panic(0) } func F4[U any](U, *U) {panic(0) } +func F4a[U any](U, Named[U]) {panic(0) } func F5[T, U any](T, U, U) { panic(0) } func F6[T any](T, int, T) { panic(0) } func F7[T any](bool, T, T) { panic(0) } @@ -73,6 +74,7 @@ func F9[V any](V, *V, V) { panic(0) } intType = types.Typ[types.Int] stringType = types.Typ[types.String] ) + pkg := testfiles.LoadPackages(t, txtar.Parse([]byte(src)), "./a")[0] scope := pkg.Types.Scope() @@ -167,12 +169,14 @@ func F9[V any](V, *V, V) { panic(0) } { // F1[*int] = F2[int], for example // F1's T is bound to a pointer to F2's T. - x: "F1", + x: "F1", + // F2's T is unbound: any instantiation works. y: "F2", want: true, wantParams: tmap{tparam("F1", 0): types.NewPointer(tparam("F2", 0))}, }, - {x: "F3", y: "F4", want: false}, // would require U identical to *U, prevented by occur check + {x: "F3", y: "F4", want: false}, // would require U identical to *U, prevented by occur check + {x: "F3", y: "F4a", want: false}, // occur check through Named[T] { x: "F5", y: "F6", @@ -184,6 +188,24 @@ func F9[V any](V, *V, V) { panic(0) } }, }, {x: "F6", y: "F7", want: false}, // both are bound + { + x: "F5", + y: "F6", + params: tmap{tparam("F6", 0): intType}, // consistent with the result + want: true, + wantParams: tmap{ + tparam("F5", 0): intType, + tparam("F5", 1): intType, + tparam("F6", 0): intType, + }, + }, + { + x: "F5", + y: "F6", + params: tmap{tparam("F6", 0): boolType}, // not consistent + want: false, + }, + {x: "F6", y: "F7", want: false}, // both are bound { // T=*V, U=int, V=int x: "F5", @@ -194,8 +216,41 @@ func F9[V any](V, *V, V) { panic(0) } tparam("F5", 1): intType, }, }, + { + // T=*V, U=int, V=int + // Partial initial information is fine, as long as it's consistent. + x: "F5", + y: "F8", + want: true, + params: tmap{tparam("F5", 1): intType}, + wantParams: tmap{ + tparam("F5", 0): types.NewPointer(tparam("F8", 0)), + tparam("F5", 1): intType, + }, + }, + { + // T=*V, U=int, V=int + // Partial initial information is fine, as long as it's consistent. + x: "F5", + y: "F8", + want: true, + params: tmap{tparam("F5", 0): types.NewPointer(tparam("F8", 0))}, + wantParams: tmap{ + tparam("F5", 0): types.NewPointer(tparam("F8", 0)), + tparam("F5", 1): intType, + }, + }, {x: "F5", y: "F9", want: false}, // T is unbound, V is bound, and T occurs in V + { + // T bound to Named[T'] + x: "F1", + y: "DAny", + want: true, + wantParams: tmap{ + tparam("F1", 0): scope.Lookup("DAny").(*types.Func).Signature().Params().At(0).Type()}, + }, } { + lookup := func(name string) types.Type { obj := scope.Lookup(name) if obj == nil { @@ -226,14 +281,14 @@ func F9[V any](V, *V, V) { panic(0) } got := unify(ta, tb, gotParams) if got != want { t.Errorf("a=%s b=%s method=%s: unify returned %t for these inputs:\n- %s\n- %s", - a, b, test.method, got, a, b) + a, b, test.method, got, ta, tb) return } if !compareParams { return } if !maps.EqualFunc(gotParams, test.wantParams, types.Identical) { - t.Errorf("x=%s y=%s method=%s: xParams: got %v, want %v", + t.Errorf("x=%s y=%s method=%s: params: got %v, want %v", a, b, test.method, gotParams, test.wantParams) } } From 19f73a601401cc3b0748f6dd74dda19177a0a760 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 14 Mar 2025 10:23:22 -0400 Subject: [PATCH 263/309] internal/typesinternal/typeindex: index of types.Info This CL provides: - typeindex.Index, a reverse index of types.Info, allowing efficient query of the defining or using identifiers of a given types.Object symbol. - typeindex.Analyzer, an Analyzer that builds an Index and offers it to later analysis passes. For example, several analyzers scan over the entirety of Info.Uses looking for a particular object; now they can make a direct reverse query with typeindex.Index.Uses(Object). - a demonstration of its use in the hostport analyzer, which uses it to: (a) implement a much more specific initial "fast path" check to reject candidate packages, and (b) to optimize the navigation from a use of a variable to its declaration. - a demonstration of it with the fmtappendf modernizer, which now locates the calls of interest directly. - a benchmark, showing that the time to locate a single call to net.Dial in a large package such as net/http is about 10,000x (!) faster. This is admittedly an extreme case. The one-time overhead is about 6ms, roughly twice the cost of building an Inspector. - new cursor API to extract the index from a Cursor and to reconstruct a Cursor from its index. This allows for a compact encoding of Uses as varint- encoded deltas (~2 byte per Cursor instead of 16). Follow-up changes will make use of the index in other analyzers. Change-Id: If28c31d3a4d360b7c2ea2285896a3d06e6af0a0d Reviewed-on: https://go-review.googlesource.com/c/tools/+/657958 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam Commit-Queue: Alan Donovan --- gopls/internal/analysis/hostport/hostport.go | 203 ++++++++-------- gopls/internal/analysis/modernize/bloop.go | 1 + .../internal/analysis/modernize/fmtappendf.go | 45 ++-- .../internal/analysis/modernize/modernize.go | 17 +- .../testdata/src/fmtappendf/fmtappendf.go | 4 +- .../src/fmtappendf/fmtappendf.go.golden | 4 +- .../analysisinternal/typeindex/typeindex.go | 33 +++ internal/astutil/cursor/cursor.go | 31 +++ internal/astutil/cursor/cursor_test.go | 1 - internal/typesinternal/typeindex/typeindex.go | 223 ++++++++++++++++++ .../typesinternal/typeindex/typeindex_test.go | 157 ++++++++++++ 11 files changed, 588 insertions(+), 131 deletions(-) create mode 100644 internal/analysisinternal/typeindex/typeindex.go create mode 100644 internal/typesinternal/typeindex/typeindex.go create mode 100644 internal/typesinternal/typeindex/typeindex_test.go diff --git a/gopls/internal/analysis/hostport/hostport.go b/gopls/internal/analysis/hostport/hostport.go index a7030ae116f..d95e475d1bf 100644 --- a/gopls/internal/analysis/hostport/hostport.go +++ b/gopls/internal/analysis/hostport/hostport.go @@ -14,11 +14,10 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/gopls/internal/util/safetoken" - "golang.org/x/tools/internal/analysisinternal" - "golang.org/x/tools/internal/astutil/cursor" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/typesinternal/typeindex" ) const Doc = `check format of addresses passed to net.Dial @@ -44,20 +43,20 @@ var Analyzer = &analysis.Analyzer{ Name: "hostport", Doc: Doc, URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/hostport", - Requires: []*analysis.Analyzer{inspect.Analyzer}, + Requires: []*analysis.Analyzer{inspect.Analyzer, typeindexanalyzer.Analyzer}, Run: run, } func run(pass *analysis.Pass) (any, error) { - // Fast path: if the package doesn't import net and fmt, skip - // the traversal. - if !analysisinternal.Imports(pass.Pkg, "net") || - !analysisinternal.Imports(pass.Pkg, "fmt") { - return nil, nil + var ( + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + fmtSprintf = index.Object("fmt", "Sprintf") + ) + if !index.Used(fmtSprintf) { + return nil, nil // fast path: package doesn't use fmt.Sprintf } - info := pass.TypesInfo - // checkAddr reports a diagnostic (and returns true) if e // is a call of the form fmt.Sprintf("%d:%d", ...). // The diagnostic includes a fix. @@ -65,96 +64,94 @@ func run(pass *analysis.Pass) (any, error) { // dialCall is non-nil if the Dial call is non-local // but within the same file. checkAddr := func(e ast.Expr, dialCall *ast.CallExpr) { - if call, ok := e.(*ast.CallExpr); ok { - obj := typeutil.Callee(info, call) - if analysisinternal.IsFunctionNamed(obj, "fmt", "Sprintf") { - // Examine format string. - formatArg := call.Args[0] - if tv := info.Types[formatArg]; tv.Value != nil { - numericPort := false - format := constant.StringVal(tv.Value) - switch format { - case "%s:%d": - // Have: fmt.Sprintf("%s:%d", host, port) - numericPort = true - - case "%s:%s": - // Have: fmt.Sprintf("%s:%s", host, portStr) - // Keep port string as is. - - default: - return - } + if call, ok := e.(*ast.CallExpr); ok && typeutil.Callee(info, call) == fmtSprintf { + // Examine format string. + formatArg := call.Args[0] + if tv := info.Types[formatArg]; tv.Value != nil { + numericPort := false + format := constant.StringVal(tv.Value) + switch format { + case "%s:%d": + // Have: fmt.Sprintf("%s:%d", host, port) + numericPort = true + + case "%s:%s": + // Have: fmt.Sprintf("%s:%s", host, portStr) + // Keep port string as is. + + default: + return + } - // Use granular edits to preserve original formatting. - edits := []analysis.TextEdit{ - { - // Replace fmt.Sprintf with net.JoinHostPort. - Pos: call.Fun.Pos(), - End: call.Fun.End(), - NewText: []byte("net.JoinHostPort"), - }, - { - // Delete format string. - Pos: formatArg.Pos(), - End: call.Args[1].Pos(), - }, - } + // Use granular edits to preserve original formatting. + edits := []analysis.TextEdit{ + { + // Replace fmt.Sprintf with net.JoinHostPort. + Pos: call.Fun.Pos(), + End: call.Fun.End(), + NewText: []byte("net.JoinHostPort"), + }, + { + // Delete format string. + Pos: formatArg.Pos(), + End: call.Args[1].Pos(), + }, + } - // Turn numeric port into a string. - if numericPort { - // port => fmt.Sprintf("%d", port) - // 123 => "123" - port := call.Args[2] - newPort := fmt.Sprintf(`fmt.Sprintf("%%d", %s)`, port) - if port := info.Types[port].Value; port != nil { - if i, ok := constant.Int64Val(port); ok { - newPort = fmt.Sprintf(`"%d"`, i) // numeric constant - } + // Turn numeric port into a string. + if numericPort { + // port => fmt.Sprintf("%d", port) + // 123 => "123" + port := call.Args[2] + newPort := fmt.Sprintf(`fmt.Sprintf("%%d", %s)`, port) + if port := info.Types[port].Value; port != nil { + if i, ok := constant.Int64Val(port); ok { + newPort = fmt.Sprintf(`"%d"`, i) // numeric constant } - - edits = append(edits, analysis.TextEdit{ - Pos: port.Pos(), - End: port.End(), - NewText: []byte(newPort), - }) - } - - // Refer to Dial call, if not adjacent. - suffix := "" - if dialCall != nil { - suffix = fmt.Sprintf(" (passed to net.Dial at L%d)", - safetoken.StartPosition(pass.Fset, dialCall.Pos()).Line) } - pass.Report(analysis.Diagnostic{ - // Highlight the format string. - Pos: formatArg.Pos(), - End: formatArg.End(), - Message: fmt.Sprintf("address format %q does not work with IPv6%s", format, suffix), - SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Replace fmt.Sprintf with net.JoinHostPort", - TextEdits: edits, - }}, + edits = append(edits, analysis.TextEdit{ + Pos: port.Pos(), + End: port.End(), + NewText: []byte(newPort), }) } + + // Refer to Dial call, if not adjacent. + suffix := "" + if dialCall != nil { + suffix = fmt.Sprintf(" (passed to net.Dial at L%d)", + safetoken.StartPosition(pass.Fset, dialCall.Pos()).Line) + } + + pass.Report(analysis.Diagnostic{ + // Highlight the format string. + Pos: formatArg.Pos(), + End: formatArg.End(), + Message: fmt.Sprintf("address format %q does not work with IPv6%s", format, suffix), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Replace fmt.Sprintf with net.JoinHostPort", + TextEdits: edits, + }}, + }) } } } // Check address argument of each call to net.Dial et al. - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - for curCall := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil)) { - call := curCall.Node().(*ast.CallExpr) - - obj := typeutil.Callee(info, call) - if analysisinternal.IsFunctionNamed(obj, "net", "Dial", "DialTimeout") || - analysisinternal.IsMethodNamed(obj, "net", "Dialer", "Dial") { - + for _, callee := range []types.Object{ + index.Object("net", "Dial"), + index.Object("net", "DialTimeout"), + index.Selection("net", "Dialer", "Dial"), + } { + for curCall := range index.Calls(callee) { + call := curCall.Node().(*ast.CallExpr) switch address := call.Args[1].(type) { case *ast.CallExpr: - // net.Dial("tcp", fmt.Sprintf("%s:%d", ...)) - checkAddr(address, nil) + if len(call.Args) == 2 { // avoid spread-call edge case + // net.Dial("tcp", fmt.Sprintf("%s:%d", ...)) + checkAddr(address, nil) + } case *ast.Ident: // addr := fmt.Sprintf("%s:%d", ...) @@ -162,25 +159,23 @@ func run(pass *analysis.Pass) (any, error) { // net.Dial("tcp", addr) // Search for decl of addrVar within common ancestor of addrVar and Dial call. + // TODO(adonovan): abstract "find RHS of statement that assigns var v". + // TODO(adonovan): reject if there are other assignments to var v. if addrVar, ok := info.Uses[address].(*types.Var); ok { - pos := addrVar.Pos() - for curAncestor := range curCall.Ancestors() { - if curIdent, ok := curAncestor.FindPos(pos, pos); ok { - // curIdent is the declaring ast.Ident of addr. - switch parent := curIdent.Parent().Node().(type) { - case *ast.AssignStmt: - if len(parent.Rhs) == 1 { - // Have: addr := fmt.Sprintf("%s:%d", ...) - checkAddr(parent.Rhs[0], call) - } - - case *ast.ValueSpec: - if len(parent.Values) == 1 { - // Have: var addr = fmt.Sprintf("%s:%d", ...) - checkAddr(parent.Values[0], call) - } + if curId, ok := index.Def(addrVar); ok { + // curIdent is the declaring ast.Ident of addr. + switch parent := curId.Parent().Node().(type) { + case *ast.AssignStmt: + if len(parent.Rhs) == 1 { + // Have: addr := fmt.Sprintf("%s:%d", ...) + checkAddr(parent.Rhs[0], call) + } + + case *ast.ValueSpec: + if len(parent.Values) == 1 { + // Have: var addr = fmt.Sprintf("%s:%d", ...) + checkAddr(parent.Values[0], call) } - break } } } diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index a70246b5e0e..2ebaa606508 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -152,6 +152,7 @@ func bloop(pass *analysis.Pass) { } // uses reports whether the subtree cur contains a use of obj. +// TODO(adonovan): opt: use typeindex. func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { for curId := range cur.Preorder((*ast.Ident)(nil)) { if info.Uses[curId.Node().(*ast.Ident)] == obj { diff --git a/gopls/internal/analysis/modernize/fmtappendf.go b/gopls/internal/analysis/modernize/fmtappendf.go index 8575827aa3e..199a626a86e 100644 --- a/gopls/internal/analysis/modernize/fmtappendf.go +++ b/gopls/internal/analysis/modernize/fmtappendf.go @@ -5,33 +5,35 @@ package modernize import ( + "fmt" "go/ast" "go/types" "strings" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" - "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // The fmtappend function replaces []byte(fmt.Sprintf(...)) by -// fmt.Appendf(nil, ...). +// fmt.Appendf(nil, ...), and similarly for Sprint, Sprintln. func fmtappendf(pass *analysis.Pass) { - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - info := pass.TypesInfo - for curFile := range filesUsing(inspect, info, "go1.19") { - for curCallExpr := range curFile.Preorder((*ast.CallExpr)(nil)) { - conv := curCallExpr.Node().(*ast.CallExpr) - tv := info.Types[conv.Fun] - if tv.IsType() && types.Identical(tv.Type, byteSliceType) { - call, ok := conv.Args[0].(*ast.CallExpr) - if ok { - obj := typeutil.Callee(info, call) - if !analysisinternal.IsFunctionNamed(obj, "fmt", "Sprintf", "Sprintln", "Sprint") { - continue - } + index := pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + for _, fn := range []types.Object{ + index.Object("fmt", "Sprintf"), + index.Object("fmt", "Sprintln"), + index.Object("fmt", "Sprint"), + } { + for curCall := range index.Calls(fn) { + call := curCall.Node().(*ast.CallExpr) + if ek, idx := curCall.ParentEdge(); ek == edge.CallExpr_Args && idx == 0 { + // Is parent a T(fmt.SprintX(...)) conversion? + conv := curCall.Parent().Node().(*ast.CallExpr) + tv := pass.TypesInfo.Types[conv.Fun] + if tv.IsType() && types.Identical(tv.Type, byteSliceType) && + fileUses(pass.TypesInfo, curCall, "go1.19") { + // Have: []byte(fmt.SprintX(...)) // Find "Sprint" identifier. var id *ast.Ident @@ -42,13 +44,14 @@ func fmtappendf(pass *analysis.Pass) { id = e // "Sprint" after `import . "fmt"` } + old, new := fn.Name(), strings.Replace(fn.Name(), "Sprint", "Append", 1) pass.Report(analysis.Diagnostic{ Pos: conv.Pos(), End: conv.End(), Category: "fmtappendf", - Message: "Replace []byte(fmt.Sprintf...) with fmt.Appendf", + Message: fmt.Sprintf("Replace []byte(fmt.%s...) with fmt.%s", old, new), SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Replace []byte(fmt.Sprintf...) with fmt.Appendf", + Message: fmt.Sprintf("Replace []byte(fmt.%s...) with fmt.%s", old, new), TextEdits: []analysis.TextEdit{ { // delete "[]byte(" @@ -63,7 +66,7 @@ func fmtappendf(pass *analysis.Pass) { { Pos: id.Pos(), End: id.End(), - NewText: []byte(strings.Replace(obj.Name(), "Sprint", "Append", 1)), + NewText: []byte(new), }, { Pos: call.Lparen + 1, diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 16fea0d8896..831376bde38 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -22,6 +22,7 @@ import ( "golang.org/x/tools/gopls/internal/util/astutil" "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/stdlib" "golang.org/x/tools/internal/versions" @@ -33,7 +34,7 @@ var doc string var Analyzer = &analysis.Analyzer{ Name: "modernize", Doc: analysisinternal.MustExtractDoc(doc, "modernize"), - Requires: []*analysis.Analyzer{inspect.Analyzer}, + Requires: []*analysis.Analyzer{inspect.Analyzer, typeindexanalyzer.Analyzer}, Run: run, URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", } @@ -125,6 +126,9 @@ func isZeroIntLiteral(info *types.Info, e ast.Expr) bool { // filesUsing returns a cursor for each *ast.File in the inspector // that uses at least the specified version of Go (e.g. "go1.24"). +// +// TODO(adonovan): opt: eliminate this function, instead following the +// approach of [fmtappendf], which uses typeindex and [fileUses]. func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) iter.Seq[cursor.Cursor] { return func(yield func(cursor.Cursor) bool) { for curFile := range cursor.Root(inspect).Children() { @@ -136,6 +140,17 @@ func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) } } +// fileUses reports whether the file containing the specified cursor +// uses at least the specified version of Go (e.g. "go1.24"). +func fileUses(info *types.Info, c cursor.Cursor, version string) bool { + // TODO(adonovan): make Ancestors reflexive so !ok becomes impossible. + if curFile, ok := moreiters.First(c.Ancestors((*ast.File)(nil))); ok { + c = curFile + } + file := c.Node().(*ast.File) + return !versions.Before(info.FileVersions[file], version) +} + // within reports whether the current pass is analyzing one of the // specified standard packages or their dependencies. func within(pass *analysis.Pass, pkgs ...string) bool { diff --git a/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go b/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go index a39a03ee786..a435b6a6461 100644 --- a/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go +++ b/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go @@ -29,8 +29,8 @@ func typealias() { } func otherprints() { - sprint := []byte(fmt.Sprint("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf" + sprint := []byte(fmt.Sprint("bye %d", 1)) // want "Replace .*Sprint.* with fmt.Append" print(sprint) - sprintln := []byte(fmt.Sprintln("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf" + sprintln := []byte(fmt.Sprintln("bye %d", 1)) // want "Replace .*Sprintln.* with fmt.Appendln" print(sprintln) } diff --git a/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go.golden b/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go.golden index 7c8aa7b9a5e..4fd2b136b82 100644 --- a/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/fmtappendf/fmtappendf.go.golden @@ -29,8 +29,8 @@ func typealias() { } func otherprints() { - sprint := fmt.Append(nil, "bye %d", 1) // want "Replace .*Sprintf.* with fmt.Appendf" + sprint := fmt.Append(nil, "bye %d", 1) // want "Replace .*Sprint.* with fmt.Append" print(sprint) - sprintln := fmt.Appendln(nil, "bye %d", 1) // want "Replace .*Sprintf.* with fmt.Appendf" + sprintln := fmt.Appendln(nil, "bye %d", 1) // want "Replace .*Sprintln.* with fmt.Appendln" print(sprintln) } \ No newline at end of file diff --git a/internal/analysisinternal/typeindex/typeindex.go b/internal/analysisinternal/typeindex/typeindex.go new file mode 100644 index 00000000000..bba21c6ea01 --- /dev/null +++ b/internal/analysisinternal/typeindex/typeindex.go @@ -0,0 +1,33 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package typeindex defines an analyzer that provides a +// [golang.org/x/tools/internal/typesinternal/typeindex.Index]. +// +// Like [golang.org/x/tools/go/analysis/passes/inspect], it is +// intended to be used as a helper by other analyzers; it reports no +// diagnostics of its own. +package typeindex + +import ( + "reflect" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/internal/typesinternal/typeindex" +) + +var Analyzer = &analysis.Analyzer{ + Name: "typeindex", + Doc: "indexes of type information for later passes", + URL: "https://pkg.go.dev/golang.org/x/tools/internal/analysisinternal/typeindex", + Run: func(pass *analysis.Pass) (any, error) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + return typeindex.New(inspect, pass.Pkg, pass.TypesInfo), nil + }, + RunDespiteErrors: true, + Requires: []*analysis.Analyzer{inspect.Analyzer}, + ResultType: reflect.TypeOf(new(typeindex.Index)), +} diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 144182f38cd..9139d4e516c 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -44,6 +44,37 @@ func Root(in *inspector.Inspector) Cursor { return Cursor{in, -1} } +// At returns the cursor at the specified index in the traversal, +// which must have been obtained from [Cursor.Index] on a Cursor +// belonging to the same Inspector. +func At(in *inspector.Inspector, index int32) Cursor { + if index < 0 { + panic("negative index") + } + events := events(in) + if int(index) >= len(events) { + panic("index out of range for this inspector") + } + if events[index].index < index { + panic("invalid index") // (a push, not a pop) + } + return Cursor{in, index} +} + +// Index returns the index of this cursor position within the package. +// +// Clients should not assume anything about the numeric Index value +// except that it increases monotonically throughout the traversal. +// It is provided for use with [At]. +// +// Index must not be called on the Root node. +func (c Cursor) Index() int32 { + if c.index < 0 { + panic("Index called on Root node") + } + return c.index +} + // Node returns the node at the current cursor position, // or nil for the cursor returned by [Inspector.Root]. func (c Cursor) Node() ast.Node { diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 9f540ffdc76..380414df790 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -447,7 +447,6 @@ func TestCursor_Edge(t *testing.T) { t.Errorf("cur.Contains(cur.Parent().NextSibling()): %v", cur) } } - } } diff --git a/internal/typesinternal/typeindex/typeindex.go b/internal/typesinternal/typeindex/typeindex.go new file mode 100644 index 00000000000..a6cc6956892 --- /dev/null +++ b/internal/typesinternal/typeindex/typeindex.go @@ -0,0 +1,223 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package typeindex provides an [Index] of type information for a +// package, allowing efficient lookup of, say, whether a given symbol +// is referenced and, if so, where from; or of the [cursor.Cursor] for +// the declaration of a particular [types.Object] symbol. +package typeindex + +import ( + "encoding/binary" + "go/ast" + "go/types" + "iter" + + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal" +) + +// New constructs an Index for the package of type-annotated syntax +// +// TODO(adonovan): accept a FileSet too? +// We regret not requiring one in inspector.New. +func New(inspect *inspector.Inspector, pkg *types.Package, info *types.Info) *Index { + ix := &Index{ + inspect: inspect, + info: info, + packages: make(map[string]*types.Package), + def: make(map[types.Object]cursor.Cursor), + uses: make(map[types.Object]*uses), + } + + addPackage := func(pkg2 *types.Package) { + if pkg2 != nil && pkg2 != pkg { + ix.packages[pkg2.Path()] = pkg2 + } + } + + for cur := range cursor.Root(inspect).Preorder((*ast.ImportSpec)(nil), (*ast.Ident)(nil)) { + switch n := cur.Node().(type) { + case *ast.ImportSpec: + // Index direct imports, including blank ones. + if pkgname := info.PkgNameOf(n); pkgname != nil { + addPackage(pkgname.Imported()) + } + + case *ast.Ident: + // Index all defining and using identifiers. + if obj := info.Defs[n]; obj != nil { + ix.def[obj] = cur + } + + if obj := info.Uses[n]; obj != nil { + // Index indirect dependencies (via fields and methods). + if !typesinternal.IsPackageLevel(obj) { + addPackage(obj.Pkg()) + } + + us, ok := ix.uses[obj] + if !ok { + us = &uses{} + us.code = us.initial[:0] + ix.uses[obj] = us + } + delta := cur.Index() - us.last + if delta < 0 { + panic("non-monotonic") + } + us.code = binary.AppendUvarint(us.code, uint64(delta)) + us.last = cur.Index() + } + } + } + return ix +} + +// An Index holds an index mapping [types.Object] symbols to their syntax. +// In effect, it is the inverse of [types.Info]. +type Index struct { + inspect *inspector.Inspector + info *types.Info + packages map[string]*types.Package // packages of all symbols referenced from this package + def map[types.Object]cursor.Cursor // Cursor of *ast.Ident that defines the Object + uses map[types.Object]*uses // Cursors of *ast.Idents that use the Object +} + +// A uses holds the list of Cursors of Idents that use a given symbol. +// +// The Uses map of [types.Info] is substantial, so it pays to compress +// its inverse mapping here, both in space and in CPU due to reduced +// allocation. A Cursor is 2 words; a Cursor.Index is 4 bytes; but +// since Cursors are naturally delivered in ascending order, we can +// use varint-encoded deltas at a cost of only ~1.7-2.2 bytes per use. +// +// Many variables have only one or two uses, so their encoded uses may +// fit in the 4 bytes of initial, saving further CPU and space +// essentially for free since the struct's size class is 4 words. +type uses struct { + code []byte // varint-encoded deltas of successive Cursor.Index values + last int32 // most recent Cursor.Index value; used during encoding + initial [4]byte // use slack in size class as initial space for code +} + +// Uses returns the sequence of Cursors of [*ast.Ident]s in this package +// that refer to obj. If obj is nil, the sequence is empty. +func (ix *Index) Uses(obj types.Object) iter.Seq[cursor.Cursor] { + return func(yield func(cursor.Cursor) bool) { + if uses := ix.uses[obj]; uses != nil { + var last int32 + for code := uses.code; len(code) > 0; { + delta, n := binary.Uvarint(code) + last += int32(delta) + if !yield(cursor.At(ix.inspect, last)) { + return + } + code = code[n:] + } + } + } +} + +// Used reports whether any of the specified objects are used, in +// other words, obj != nil && Uses(obj) is non-empty for some obj in objs. +// +// (This treatment of nil allows Used to be called directly on the +// result of [Index.Object] so that analyzers can conveniently skip +// packages that don't use a symbol of interest.) +func (ix *Index) Used(objs ...types.Object) bool { + for _, obj := range objs { + if obj != nil && ix.uses[obj] != nil { + return true + } + } + return false +} + +// Def returns the Cursor of the [*ast.Ident] in this package +// that declares the specified object, if any. +func (ix *Index) Def(obj types.Object) (cursor.Cursor, bool) { + cur, ok := ix.def[obj] + return cur, ok +} + +// Package returns the package of the specified path, +// or nil if it is not referenced from this package. +func (ix *Index) Package(path string) *types.Package { + return ix.packages[path] +} + +// Object returns the package-level symbol name within the package of +// the specified path, or nil if the package or symbol does not exist +// or is not visible from this package. +func (ix *Index) Object(path, name string) types.Object { + if pkg := ix.Package(path); pkg != nil { + return pkg.Scope().Lookup(name) + } + return nil +} + +// Selection returns the named method or field belonging to the +// package-level type returned by Object(path, typename). +func (ix *Index) Selection(path, typename, name string) types.Object { + if obj := ix.Object(path, typename); obj != nil { + if tname, ok := obj.(*types.TypeName); ok { + obj, _, _ := types.LookupFieldOrMethod(tname.Type(), true, obj.Pkg(), name) + return obj + } + } + return nil +} + +// Calls returns the sequence of cursors for *ast.CallExpr nodes that +// call the specified callee, as defined by [typeutil.Callee]. +// If callee is nil, the sequence is empty. +func (ix *Index) Calls(callee types.Object) iter.Seq[cursor.Cursor] { + return func(yield func(cursor.Cursor) bool) { + for cur := range ix.Uses(callee) { + ek, _ := cur.ParentEdge() + + // The call may be of the form f() or x.f(), + // optionally with parens; ascend from f to call. + // + // It is tempting but wrong to use the first + // CallExpr ancestor: we have to make sure the + // ident is in the CallExpr.Fun position, otherwise + // f(f, f) would have two spurious matches. + // Avoiding Ancestors is also significantly faster. + + // inverse unparen: f -> (f) + for ek == edge.ParenExpr_X { + cur = cur.Parent() + ek, _ = cur.ParentEdge() + } + + // ascend selector: f -> x.f + if ek == edge.SelectorExpr_Sel { + cur = cur.Parent() + ek, _ = cur.ParentEdge() + } + + // inverse unparen again + for ek == edge.ParenExpr_X { + cur = cur.Parent() + ek, _ = cur.ParentEdge() + } + + // ascend from f or x.f to call + if ek == edge.CallExpr_Fun { + curCall := cur.Parent() + call := curCall.Node().(*ast.CallExpr) + if typeutil.Callee(ix.info, call) == callee { + if !yield(curCall) { + return + } + } + } + } + } +} diff --git a/internal/typesinternal/typeindex/typeindex_test.go b/internal/typesinternal/typeindex/typeindex_test.go new file mode 100644 index 00000000000..767d183ac44 --- /dev/null +++ b/internal/typesinternal/typeindex/typeindex_test.go @@ -0,0 +1,157 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build go1.24 + +package typeindex_test + +import ( + "go/ast" + "slices" + "testing" + + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/packages" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typesinternal/typeindex" +) + +func TestIndex(t *testing.T) { + var ( + pkg = loadNetHTTP(t) + inspect = inspector.New(pkg.Syntax) + index = typeindex.New(inspect, pkg.Types, pkg.TypesInfo) + fmtSprintf = index.Object("fmt", "Sprintf") + ) + + // Gather calls and uses of fmt.Sprintf in net/http. + var ( + wantUses []*ast.Ident + wantCalls []*ast.CallExpr + ) + for n := range inspect.PreorderSeq((*ast.CallExpr)(nil), (*ast.Ident)(nil)) { + switch n := n.(type) { + case *ast.CallExpr: + if typeutil.Callee(pkg.TypesInfo, n) == fmtSprintf { + wantCalls = append(wantCalls, n) + } + case *ast.Ident: + if pkg.TypesInfo.Uses[n] == fmtSprintf { + wantUses = append(wantUses, n) + } + } + } + // sanity check (expect about 60 of each) + if wantUses == nil || wantCalls == nil { + t.Fatalf("no calls or uses of fmt.Sprintf in net/http") + } + + var ( + gotUses []*ast.Ident + gotCalls []*ast.CallExpr + ) + for curId := range index.Uses(fmtSprintf) { + gotUses = append(gotUses, curId.Node().(*ast.Ident)) + } + for curCall := range index.Calls(fmtSprintf) { + gotCalls = append(gotCalls, curCall.Node().(*ast.CallExpr)) + } + + if !slices.Equal(gotUses, wantUses) { + t.Errorf("index.Uses(fmt.Sprintf) = %v, want %v", gotUses, wantUses) + } + if !slices.Equal(gotCalls, wantCalls) { + t.Errorf("index.Calls(fmt.Sprintf) = %v, want %v", gotCalls, wantCalls) + } +} + +func loadNetHTTP(tb testing.TB) *packages.Package { + cfg := &packages.Config{Mode: packages.LoadSyntax} + pkgs, err := packages.Load(cfg, "net/http") + if err != nil { + tb.Fatal(err) + } + return pkgs[0] +} + +func BenchmarkIndex(b *testing.B) { + // Load net/http, a large package, and find calls to net.Dial. + // + // There is currently exactly one, which provides an extreme + // demonstration of the performance advantage of the Index. + // + // Index construction costs approximately 7x the cursor + // traversal, so it breaks even when it replaces 7 passes. + // The cost of index lookup is approximately zero. + pkg := loadNetHTTP(b) + + // Build the Inspector (~2.8ms). + var inspect *inspector.Inspector + b.Run("inspector.New", func(b *testing.B) { + for b.Loop() { + inspect = inspector.New(pkg.Syntax) + } + }) + + // Build the Index (~6.6ms). + var index *typeindex.Index + b.Run("typeindex.New", func(b *testing.B) { + b.ReportAllocs() // 2.48MB/op + for b.Loop() { + index = typeindex.New(inspect, pkg.Types, pkg.TypesInfo) + } + }) + + target := index.Object("net", "Dial") + + var countA, countB, countC int + + // unoptimized inspect implementation (~1.6ms, 1x) + b.Run("inspect", func(b *testing.B) { + for b.Loop() { + countA = 0 + for _, file := range pkg.Syntax { + ast.Inspect(file, func(n ast.Node) bool { + if call, ok := n.(*ast.CallExpr); ok { + if typeutil.Callee(pkg.TypesInfo, call) == target { + countA++ + } + } + return true + }) + } + } + }) + if countA == 0 { + b.Errorf("target %v not found", target) + } + + // unoptimized cursor implementation (~390us, 4x faster) + b.Run("cursor", func(b *testing.B) { + for b.Loop() { + countB = 0 + for curCall := range cursor.Root(inspect).Preorder((*ast.CallExpr)(nil)) { + call := curCall.Node().(*ast.CallExpr) + if typeutil.Callee(pkg.TypesInfo, call) == target { + countB++ + } + } + } + }) + + // indexed implementation (~120ns, >10,000x faster) + b.Run("index", func(b *testing.B) { + for b.Loop() { + countC = 0 + for range index.Calls(target) { + countC++ + } + } + }) + + if countA != countB || countA != countC { + b.Fatalf("inconsistent results (inspect=%d, cursor=%d, index=%d)", countA, countB, countC) + } +} From 11a3153db611913acc03b4b5a3c2d4a4cdf3e095 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 24 Mar 2025 17:25:37 -0400 Subject: [PATCH 264/309] gopls/internal/analysis/modernize: rangeint: respect side effects This CL fixes a serious bug in rangeint that caused it to offer a fix from "for i := 0; i < len(s); i++" to "for range s" even when the loop may modify the value of slice s. (The for loop reads the length on each iteration, whereas the range loop reads it only once.) + test Fixes golang/go#72917 Change-Id: Id0c926f0590241736c7fe7589c2796a075d05744 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660435 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- gopls/internal/analysis/modernize/rangeint.go | 74 ++++++++++++++----- .../testdata/src/rangeint/rangeint.go | 48 +++++++++++- .../testdata/src/rangeint/rangeint.go.golden | 48 +++++++++++- 3 files changed, 151 insertions(+), 19 deletions(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 2a500085e01..a8106f08d57 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -15,8 +15,11 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // rangeint offers a fix to replace a 3-clause 'for' loop: @@ -38,13 +41,23 @@ import ( // - The limit must not be b.N, to avoid redundancy with bloop's fixes. // // Caveats: -// - The fix will cause the limit expression to be evaluated exactly -// once, instead of once per iteration. The limit may be a function call -// (e.g. seq.Len()). The fix may change the cardinality of side effects. +// +// The fix causes the limit expression to be evaluated exactly once, +// instead of once per iteration. So, to avoid changing the +// cardinality of side effects, the limit expression must not involve +// function calls (e.g. seq.Len()) or channel receives. Moreover, the +// value of the limit expression must be loop invariant, which in +// practice means it must take one of the following forms: +// +// - a local variable that is assigned only once and not address-taken; +// - a constant; or +// - len(s), where s has the above properties. func rangeint(pass *analysis.Pass) { info := pass.TypesInfo inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + typeindex := pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + for curFile := range filesUsing(inspect, info, "go1.22") { nextLoop: for curLoop := range curFile.Preorder((*ast.ForStmt)(nil)) { @@ -62,19 +75,39 @@ func rangeint(pass *analysis.Pass) { // Have: for i = 0; i < limit; ... {} limit := compare.Y - curLimit, _ := curLoop.FindNode(limit) - // Don't offer a fix if the limit expression depends on the loop index. - for cur := range curLimit.Preorder((*ast.Ident)(nil)) { - if cur.Node().(*ast.Ident).Name == index.Name { - continue nextLoop - } + + // If limit is "len(slice)", simplify it to "slice". + // + // (Don't replace "for i := 0; i < len(map); i++" + // with "for range m" because it's too hard to prove + // that len(m) is loop-invariant). + if call, ok := limit.(*ast.CallExpr); ok && + typeutil.Callee(info, call) == builtinLen && + is[*types.Slice](info.TypeOf(call.Args[0]).Underlying()) { + limit = call.Args[0] } - // Skip loops up to b.N in benchmarks; see [bloop]. - if sel, ok := limit.(*ast.SelectorExpr); ok && - sel.Sel.Name == "N" && - analysisinternal.IsPointerToNamed(info.TypeOf(sel.X), "testing", "B") { - continue // skip b.N + // Check the form of limit: must be a constant, + // or a local var that is not assigned or address-taken. + limitOK := false + if info.Types[limit].Value != nil { + limitOK = true // constant + } else if id, ok := limit.(*ast.Ident); ok { + if v, ok := info.Uses[id].(*types.Var); ok && + !(v.Exported() && typesinternal.IsPackageLevel(v)) { + // limit is a local or unexported global var. + // (An exported global may have uses we can't see.) + for cur := range typeindex.Uses(v) { + if isScalarLvalue(info, cur) { + // Limit var is assigned or address-taken. + continue nextLoop + } + } + limitOK = true + } + } + if !limitOK { + continue nextLoop } if inc, ok := loop.Post.(*ast.IncDecStmt); ok && @@ -93,7 +126,7 @@ func rangeint(pass *analysis.Pass) { // Reject if any is an l-value (assigned or address-taken): // a "for range int" loop does not respect assignments to // the loop variable. - if isScalarLvalue(curId) { + if isScalarLvalue(info, curId) { continue nextLoop } } @@ -213,7 +246,7 @@ func rangeint(pass *analysis.Pass) { // // This function is valid only for scalars (x = ...), // not for aggregates (x.a[i] = ...) -func isScalarLvalue(curId cursor.Cursor) bool { +func isScalarLvalue(info *types.Info, curId cursor.Cursor) bool { // Unfortunately we can't simply use info.Types[e].Assignable() // as it is always true for a variable even when that variable is // used only as an r-value. So we must inspect enclosing syntax. @@ -229,7 +262,14 @@ func isScalarLvalue(curId cursor.Cursor) bool { switch ek { case edge.AssignStmt_Lhs: - return true // i = j + assign := cur.Parent().Node().(*ast.AssignStmt) + if assign.Tok == token.ASSIGN { + return true // i = j + } + id := curId.Node().(*ast.Ident) + if v, ok := info.Defs[id]; ok && v.Pos() != id.Pos() { + return true // reassignment of i (i, j := 1, 2) + } case edge.IncDecStmt_X: return true // i++, i-- case edge.UnaryExpr_X: diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index b2a7459e5a3..0890051f0ba 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -48,7 +48,7 @@ func _(i int, s struct{ i int }, slice []int) { { var i int - for i = 0; i < f(); i++ { // want "for loop can be modernized using range over int" + for i = 0; i < 10; i++ { // want "for loop can be modernized using range over int" } // NB: no uses of i after loop. } @@ -90,8 +90,54 @@ func _(i int, s struct{ i int }, slice []int) { for i := 0; i < 10; i++ { // nope: assigns i i = 8 } + + // The limit expression must be loop invariant; + // see https://github.com/golang/go/issues/72917 + for i := 0; i < f(); i++ { // nope + } + { + var s struct{ limit int } + for i := 0; i < s.limit; i++ { // nope: limit is not a const or local var + } + } + { + const k = 10 + for i := 0; i < k; i++ { // want "for loop can be modernized using range over int" + } + } + { + var limit = 10 + for i := 0; i < limit; i++ { // want "for loop can be modernized using range over int" + } + } + { + var limit = 10 + for i := 0; i < limit; i++ { // nope: limit is address-taken + } + print(&limit) + } + { + limit := 10 + limit++ + for i := 0; i < limit; i++ { // nope: limit is assigned other than by its declaration + } + } + for i := 0; i < Global; i++ { // nope: limit is an exported global var; may be updated elsewhere + } + for i := 0; i < len(table); i++ { // want "for loop can be modernized using range over int" + } + { + s := []string{} + for i := 0; i < len(s); i++ { // nope: limit is not loop-invariant + s = s[1:] + } + } } +var Global int + +var table = []string{"hello", "world"} + func f() int { return 0 } // Repro for part of #71847: ("for range n is invalid if the loop body contains i++"): diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index f323879e13f..a6b3755840a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -48,7 +48,7 @@ func _(i int, s struct{ i int }, slice []int) { { var i int - for i = range f() { // want "for loop can be modernized using range over int" + for i = range 10 { // want "for loop can be modernized using range over int" } // NB: no uses of i after loop. } @@ -90,8 +90,54 @@ func _(i int, s struct{ i int }, slice []int) { for i := 0; i < 10; i++ { // nope: assigns i i = 8 } + + // The limit expression must be loop invariant; + // see https://github.com/golang/go/issues/72917 + for i := 0; i < f(); i++ { // nope + } + { + var s struct{ limit int } + for i := 0; i < s.limit; i++ { // nope: limit is not a const or local var + } + } + { + const k = 10 + for range k { // want "for loop can be modernized using range over int" + } + } + { + var limit = 10 + for range limit { // want "for loop can be modernized using range over int" + } + } + { + var limit = 10 + for i := 0; i < limit; i++ { // nope: limit is address-taken + } + print(&limit) + } + { + limit := 10 + limit++ + for i := 0; i < limit; i++ { // nope: limit is assigned other than by its declaration + } + } + for i := 0; i < Global; i++ { // nope: limit is an exported global var; may be updated elsewhere + } + for range table { // want "for loop can be modernized using range over int" + } + { + s := []string{} + for i := 0; i < len(s); i++ { // nope: limit is not loop-invariant + s = s[1:] + } + } } +var Global int + +var table = []string{"hello", "world"} + func f() int { return 0 } // Repro for part of #71847: ("for range n is invalid if the loop body contains i++"): From 30641f5ab3e08af28ea60644441d2fd05f2bc054 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Mar 2025 12:57:46 -0400 Subject: [PATCH 265/309] gopls/internal/analysis/modernize: use typeindex throughout Change-Id: Ibc59b715430f60a4d311adff5fe75287ae2b897a Reviewed-on: https://go-review.googlesource.com/c/tools/+/660616 Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan --- gopls/internal/analysis/modernize/bloop.go | 18 +++-- .../internal/analysis/modernize/fmtappendf.go | 2 +- .../internal/analysis/modernize/modernize.go | 14 ++-- .../analysis/modernize/slicescontains.go | 13 ++- .../internal/analysis/modernize/sortslice.go | 34 +++----- .../analysis/modernize/stringscutprefix.go | 20 +++-- .../internal/analysis/modernize/stringsseq.go | 26 +++--- .../analysis/modernize/testingcontext.go | 80 +++++++------------ 8 files changed, 103 insertions(+), 104 deletions(-) diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 2ebaa606508..1fc07169486 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -15,7 +15,9 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // bloop updates benchmarks that use "for range b.N", replacing it @@ -31,7 +33,11 @@ func bloop(pass *analysis.Pass) { return } - info := pass.TypesInfo + var ( + inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + ) // edits computes the text edits for a matched for/range loop // at the specified cursor. b is the *testing.B value, and @@ -76,7 +82,6 @@ func bloop(pass *analysis.Pass) { } // Find all for/range statements. - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) loops := []ast.Node{ (*ast.ForStmt)(nil), (*ast.RangeStmt)(nil), @@ -105,7 +110,7 @@ func bloop(pass *analysis.Pass) { is[*ast.IncDecStmt](n.Post) && n.Post.(*ast.IncDecStmt).Tok == token.INC && equalSyntax(n.Post.(*ast.IncDecStmt).X, assign.Lhs[0]) && - !uses(info, body, info.Defs[assign.Lhs[0].(*ast.Ident)]) { + !uses(index, body, info.Defs[assign.Lhs[0].(*ast.Ident)]) { delStart, delEnd = n.Init.Pos(), n.Post.End() } @@ -152,10 +157,9 @@ func bloop(pass *analysis.Pass) { } // uses reports whether the subtree cur contains a use of obj. -// TODO(adonovan): opt: use typeindex. -func uses(info *types.Info, cur cursor.Cursor, obj types.Object) bool { - for curId := range cur.Preorder((*ast.Ident)(nil)) { - if info.Uses[curId.Node().(*ast.Ident)] == obj { +func uses(index *typeindex.Index, cur cursor.Cursor, obj types.Object) bool { + for use := range index.Uses(obj) { + if cur.Contains(use) { return true } } diff --git a/gopls/internal/analysis/modernize/fmtappendf.go b/gopls/internal/analysis/modernize/fmtappendf.go index 199a626a86e..6b01d38050e 100644 --- a/gopls/internal/analysis/modernize/fmtappendf.go +++ b/gopls/internal/analysis/modernize/fmtappendf.go @@ -32,7 +32,7 @@ func fmtappendf(pass *analysis.Pass) { conv := curCall.Parent().Node().(*ast.CallExpr) tv := pass.TypesInfo.Types[conv.Fun] if tv.IsType() && types.Identical(tv.Type, byteSliceType) && - fileUses(pass.TypesInfo, curCall, "go1.19") { + fileUses(pass.TypesInfo, enclosingFile(curCall), "go1.19") { // Have: []byte(fmt.SprintX(...)) // Find "Sprint" identifier. diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index 831376bde38..ac4a5c28e5f 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -140,15 +140,19 @@ func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) } } -// fileUses reports whether the file containing the specified cursor -// uses at least the specified version of Go (e.g. "go1.24"). -func fileUses(info *types.Info, c cursor.Cursor, version string) bool { +// fileUses reports whether the specified file uses at least the +// specified version of Go (e.g. "go1.24"). +func fileUses(info *types.Info, file *ast.File, version string) bool { + return !versions.Before(info.FileVersions[file], version) +} + +// enclosingFile returns the syntax tree for the file enclosing c. +func enclosingFile(c cursor.Cursor) *ast.File { // TODO(adonovan): make Ancestors reflexive so !ok becomes impossible. if curFile, ok := moreiters.First(c.Ancestors((*ast.File)(nil))); ok { c = curFile } - file := c.Node().(*ast.File) - return !versions.Before(info.FileVersions[file], version) + return c.Node().(*ast.File) } // within reports whether the current pass is analyzing one of the diff --git a/gopls/internal/analysis/modernize/slicescontains.go b/gopls/internal/analysis/modernize/slicescontains.go index 589efe7ffc8..e99474df6ab 100644 --- a/gopls/internal/analysis/modernize/slicescontains.go +++ b/gopls/internal/analysis/modernize/slicescontains.go @@ -15,8 +15,10 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // The slicescontains pass identifies loops that can be replaced by a @@ -56,7 +58,11 @@ func slicescontains(pass *analysis.Pass) { return } - info := pass.TypesInfo + var ( + inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + ) // check is called for each RangeStmt of this form: // for i, elem := range s { if cond { ... } } @@ -144,8 +150,8 @@ func slicescontains(pass *analysis.Pass) { if !ok { panic(fmt.Sprintf("FindNode(%T) failed", n)) } - return uses(info, cur, info.Defs[rng.Key.(*ast.Ident)]) || - rng.Value != nil && uses(info, cur, info.Defs[rng.Value.(*ast.Ident)]) + return uses(index, cur, info.Defs[rng.Key.(*ast.Ident)]) || + rng.Value != nil && uses(index, cur, info.Defs[rng.Value.(*ast.Ident)]) } if usesRangeVar(body) { // Body uses range var "i" or "elem". @@ -349,7 +355,6 @@ func slicescontains(pass *analysis.Pass) { } } - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for curFile := range filesUsing(inspect, info, "go1.21") { file := curFile.Node().(*ast.File) diff --git a/gopls/internal/analysis/modernize/sortslice.go b/gopls/internal/analysis/modernize/sortslice.go index 0437aaf2f67..bbd04e9293d 100644 --- a/gopls/internal/analysis/modernize/sortslice.go +++ b/gopls/internal/analysis/modernize/sortslice.go @@ -10,10 +10,9 @@ import ( "go/types" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" - "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // The sortslice pass replaces sort.Slice(slice, less) with @@ -42,14 +41,13 @@ func sortslice(pass *analysis.Pass) { return } - info := pass.TypesInfo - - check := func(file *ast.File, call *ast.CallExpr) { - // call to sort.Slice? - obj := typeutil.Callee(info, call) - if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice") { - return - } + var ( + info = pass.TypesInfo + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + sortSlice = index.Object("sort", "Slice") + ) + for curCall := range index.Calls(sortSlice) { + call := curCall.Node().(*ast.CallExpr) if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 { sig := info.Types[lit.Type].Type.(*types.Signature) @@ -68,7 +66,9 @@ func sortslice(pass *analysis.Pass) { is[*ast.Ident](index.Index) && info.Uses[index.Index.(*ast.Ident)] == v } - if isIndex(compare.X, i) && isIndex(compare.Y, j) { + file := enclosingFile(curCall) + if isIndex(compare.X, i) && isIndex(compare.Y, j) && + fileUses(info, file, "go1.21") { // Have: sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) _, prefix, importEdits := analysisinternal.AddImport( @@ -102,14 +102,4 @@ func sortslice(pass *analysis.Pass) { } } } - - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - for curFile := range filesUsing(inspect, info, "go1.21") { - file := curFile.Node().(*ast.File) - - for curCall := range curFile.Preorder((*ast.CallExpr)(nil)) { - call := curCall.Node().(*ast.CallExpr) - check(file, call) - } - } } diff --git a/gopls/internal/analysis/modernize/stringscutprefix.go b/gopls/internal/analysis/modernize/stringscutprefix.go index 28c42c93b05..9e9239c0f21 100644 --- a/gopls/internal/analysis/modernize/stringscutprefix.go +++ b/gopls/internal/analysis/modernize/stringscutprefix.go @@ -14,6 +14,8 @@ import ( "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // stringscutprefix offers a fix to replace an if statement which @@ -35,8 +37,15 @@ import ( // Variants: // - bytes.HasPrefix usage as pattern 1. func stringscutprefix(pass *analysis.Pass) { - if !analysisinternal.Imports(pass.Pkg, "strings") && - !analysisinternal.Imports(pass.Pkg, "bytes") { + var ( + inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + + stringsTrimPrefix = index.Object("strings", "TrimPrefix") + bytesTrimPrefix = index.Object("bytes", "TrimPrefix") + ) + if !index.Used(stringsTrimPrefix, bytesTrimPrefix) { return } @@ -45,8 +54,6 @@ func stringscutprefix(pass *analysis.Pass) { fixedMessage = "Replace HasPrefix/TrimPrefix with CutPrefix" ) - info := pass.TypesInfo - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for curFile := range filesUsing(inspect, pass.TypesInfo, "go1.20") { for curIfStmt := range curFile.Preorder((*ast.IfStmt)(nil)) { ifStmt := curIfStmt.Node().(*ast.IfStmt) @@ -65,8 +72,7 @@ func stringscutprefix(pass *analysis.Pass) { for curCall := range firstStmt.Preorder((*ast.CallExpr)(nil)) { call1 := curCall.Node().(*ast.CallExpr) obj1 := typeutil.Callee(info, call1) - if !analysisinternal.IsFunctionNamed(obj1, "strings", "TrimPrefix") && - !analysisinternal.IsFunctionNamed(obj1, "bytes", "TrimPrefix") { + if obj1 != stringsTrimPrefix && obj1 != bytesTrimPrefix { continue } @@ -140,7 +146,7 @@ func stringscutprefix(pass *analysis.Pass) { if call, ok := assign.Rhs[0].(*ast.CallExpr); ok && assign.Tok == token.DEFINE { lhs := assign.Lhs[0] obj := typeutil.Callee(info, call) - if analysisinternal.IsFunctionNamed(obj, "strings", "TrimPrefix") && + if obj == stringsTrimPrefix && (equalSyntax(lhs, bin.X) && equalSyntax(call.Args[0], bin.Y) || (equalSyntax(lhs, bin.Y) && equalSyntax(call.Args[0], bin.X))) { okVarName := analysisinternal.FreshName(info.Scopes[ifStmt], ifStmt.Pos(), "ok") diff --git a/gopls/internal/analysis/modernize/stringsseq.go b/gopls/internal/analysis/modernize/stringsseq.go index a26b8da705c..d32f8be754f 100644 --- a/gopls/internal/analysis/modernize/stringsseq.go +++ b/gopls/internal/analysis/modernize/stringsseq.go @@ -14,8 +14,9 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // stringsseq offers a fix to replace a call to strings.Split with @@ -33,12 +34,20 @@ import ( // - bytes.SplitSeq // - bytes.FieldsSeq func stringsseq(pass *analysis.Pass) { - if !analysisinternal.Imports(pass.Pkg, "strings") && - !analysisinternal.Imports(pass.Pkg, "bytes") { + var ( + inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + + stringsSplit = index.Object("strings", "Split") + stringsFields = index.Object("strings", "Fields") + bytesSplit = index.Object("bytes", "Split") + bytesFields = index.Object("bytes", "Fields") + ) + if !index.Used(stringsSplit, stringsFields, bytesSplit, bytesFields) { return } - info := pass.TypesInfo - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + for curFile := range filesUsing(inspect, info, "go1.24") { for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) { rng := curRange.Node().(*ast.RangeStmt) @@ -62,7 +71,7 @@ func stringsseq(pass *analysis.Pass) { len(assign.Lhs) == 1 && len(assign.Rhs) == 1 && info.Defs[assign.Lhs[0].(*ast.Ident)] == v && - soleUse(info, v) == id { + soleUseIs(index, v, id) { // Have: // lines := ... // for _, line := range lines {...} @@ -96,9 +105,8 @@ func stringsseq(pass *analysis.Pass) { continue } - obj := typeutil.Callee(info, call) - if analysisinternal.IsFunctionNamed(obj, "strings", "Split", "Fields") || - analysisinternal.IsFunctionNamed(obj, "bytes", "Split", "Fields") { + switch obj := typeutil.Callee(info, call); obj { + case stringsSplit, stringsFields, bytesSplit, bytesFields: oldFnName := obj.Name() seqFnName := fmt.Sprintf("%sSeq", oldFnName) pass.Report(analysis.Diagnostic{ diff --git a/gopls/internal/analysis/modernize/testingcontext.go b/gopls/internal/analysis/modernize/testingcontext.go index 05c0b811ab7..de52f756ab8 100644 --- a/gopls/internal/analysis/modernize/testingcontext.go +++ b/gopls/internal/analysis/modernize/testingcontext.go @@ -14,12 +14,11 @@ import ( "unicode/utf8" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" - "golang.org/x/tools/internal/astutil/cursor" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal/typeindex" ) // The testingContext pass replaces calls to context.WithCancel from within @@ -41,38 +40,32 @@ import ( // - the call is within a test or subtest function // - the relevant testing.{T,B,F} is named and not shadowed at the call func testingContext(pass *analysis.Pass) { - if !analysisinternal.Imports(pass.Pkg, "testing") { - return - } + var ( + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo - info := pass.TypesInfo + contextWithCancel = index.Object("context", "WithCancel") + ) - // checkCall finds eligible calls to context.WithCancel to replace. - checkCall := func(cur cursor.Cursor) { +calls: + for cur := range index.Calls(contextWithCancel) { call := cur.Node().(*ast.CallExpr) - obj := typeutil.Callee(info, call) - if !analysisinternal.IsFunctionNamed(obj, "context", "WithCancel") { - return - } - - // Have: context.WithCancel(arg) + // Have: context.WithCancel(...) arg, ok := call.Args[0].(*ast.CallExpr) if !ok { - return + continue } - if obj := typeutil.Callee(info, arg); !analysisinternal.IsFunctionNamed(obj, "context", "Background", "TODO") { - return + if !analysisinternal.IsFunctionNamed(typeutil.Callee(info, arg), "context", "Background", "TODO") { + continue } - // Have: context.WithCancel(context.{Background,TODO}()) parent := cur.Parent() assign, ok := parent.Node().(*ast.AssignStmt) if !ok || assign.Tok != token.DEFINE { - return + continue } - // Have: a, b := context.WithCancel(context.{Background,TODO}()) // Check that both a and b are declared, not redeclarations. @@ -80,27 +73,27 @@ func testingContext(pass *analysis.Pass) { for _, expr := range assign.Lhs { id, ok := expr.(*ast.Ident) if !ok { - return + continue calls } obj, ok := info.Defs[id] if !ok { - return + continue calls } lhs = append(lhs, obj) } next, ok := parent.NextSibling() if !ok { - return + continue } defr, ok := next.Node().(*ast.DeferStmt) if !ok { - return + continue } - if soleUse(info, lhs[1]) != defr.Call.Fun { - return + deferId, ok := defr.Call.Fun.(*ast.Ident) + if !ok || !soleUseIs(index, lhs[1], deferId) { + continue // b is used elsewhere } - // Have: // a, b := context.WithCancel(context.{Background,TODO}()) // defer b() @@ -126,8 +119,7 @@ func testingContext(pass *analysis.Pass) { testObj = isTestFn(info, n) } } - - if testObj != nil { + if testObj != nil && fileUses(info, enclosingFile(cur), "go1.24") { // Have a test function. Check that we can resolve the relevant // testing.{T,B,F} at the current position. if _, obj := lhs[0].Parent().LookupParent(testObj.Name(), lhs[0].Pos()); obj == testObj { @@ -148,29 +140,19 @@ func testingContext(pass *analysis.Pass) { } } } - - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - for curFile := range filesUsing(inspect, info, "go1.24") { - for cur := range curFile.Preorder((*ast.CallExpr)(nil)) { - checkCall(cur) - } - } } -// soleUse returns the ident that refers to obj, if there is exactly one. -// -// TODO(rfindley): consider factoring to share with gopls/internal/refactor/inline. -func soleUse(info *types.Info, obj types.Object) (sole *ast.Ident) { - // This is not efficient, but it is called infrequently. - for id, obj2 := range info.Uses { - if obj2 == obj { - if sole != nil { - return nil // not unique - } - sole = id +// soleUseIs reports whether id is the sole Ident that uses obj. +// (It returns false if there were no uses of obj.) +func soleUseIs(index *typeindex.Index, obj types.Object, id *ast.Ident) bool { + empty := true + for use := range index.Uses(obj) { + empty = false + if use.Node() != id { + return false } } - return sole + return !empty } // isTestFn checks whether fn is a test function (TestX, BenchmarkX, FuzzX), From 7efe9a8b020b4f51bbbd887c6ecffde35fb8a4e1 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Mar 2025 14:23:33 -0400 Subject: [PATCH 266/309] gopls/internal/analysis/modernize: rangeint: fix yet another bug ASSIGN and DEFINE are not the only kinds of AssignStmt. + test Change-Id: I81b5122b0ac0db9be5178b6685c00212f3c4c469 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660695 Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan --- gopls/internal/analysis/modernize/rangeint.go | 4 ++-- .../analysis/modernize/testdata/src/rangeint/rangeint.go | 3 +++ .../modernize/testdata/src/rangeint/rangeint.go.golden | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index a8106f08d57..4ca87e40aec 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -263,8 +263,8 @@ func isScalarLvalue(info *types.Info, curId cursor.Cursor) bool { switch ek { case edge.AssignStmt_Lhs: assign := cur.Parent().Node().(*ast.AssignStmt) - if assign.Tok == token.ASSIGN { - return true // i = j + if assign.Tok != token.DEFINE { + return true // i = j or i += j } id := curId.Node().(*ast.Ident) if v, ok := info.Defs[id]; ok && v.Pos() != id.Pos() { diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index 0890051f0ba..7048fea1148 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -132,6 +132,9 @@ func _(i int, s struct{ i int }, slice []int) { s = s[1:] } } + for i := 0; i < len(slice); i++ { // nope: i is incremented within loop + i += 1 + } } var Global int diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index a6b3755840a..8c3fdc40b77 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -132,6 +132,9 @@ func _(i int, s struct{ i int }, slice []int) { s = s[1:] } } + for i := 0; i < len(slice); i++ { // nope: i is incremented within loop + i += 1 + } } var Global int From b75dab25fbcec2c54025cd5007c22b5d8b648063 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Mar 2025 16:10:22 -0400 Subject: [PATCH 267/309] internal/typesinternal/typeindex: suppress test on js Fixes golang/go#73043 Change-Id: Ia0524bb45562095356984693f705a51fb0a35224 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660735 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- internal/typesinternal/typeindex/typeindex_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/typesinternal/typeindex/typeindex_test.go b/internal/typesinternal/typeindex/typeindex_test.go index 767d183ac44..c8b08dc9d00 100644 --- a/internal/typesinternal/typeindex/typeindex_test.go +++ b/internal/typesinternal/typeindex/typeindex_test.go @@ -15,10 +15,12 @@ import ( "golang.org/x/tools/go/packages" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/testenv" "golang.org/x/tools/internal/typesinternal/typeindex" ) func TestIndex(t *testing.T) { + testenv.NeedsGoPackages(t) var ( pkg = loadNetHTTP(t) inspect = inspector.New(pkg.Syntax) From 8be0d5f6f63449d5236aca9528f40e7bf6ef0215 Mon Sep 17 00:00:00 2001 From: Kyle Weingartner Date: Tue, 25 Mar 2025 13:59:55 -0700 Subject: [PATCH 268/309] gopls/internal/analysis/maprange: use typeindex Updates golang/go#72908 Change-Id: Ie6cfbfd9326b02d3b1c7421e7da8f700d4bb5de5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660755 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Reviewed-by: Alan Donovan --- gopls/internal/analysis/maprange/maprange.go | 132 ++++++++++--------- 1 file changed, 69 insertions(+), 63 deletions(-) diff --git a/gopls/internal/analysis/maprange/maprange.go b/gopls/internal/analysis/maprange/maprange.go index c3990f9ea75..4bd7066b8cb 100644 --- a/gopls/internal/analysis/maprange/maprange.go +++ b/gopls/internal/analysis/maprange/maprange.go @@ -8,11 +8,14 @@ import ( _ "embed" "fmt" "go/ast" + "go/types" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" - "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/astutil/edge" + "golang.org/x/tools/internal/typesinternal/typeindex" "golang.org/x/tools/internal/versions" ) @@ -23,7 +26,7 @@ var Analyzer = &analysis.Analyzer{ Name: "maprange", Doc: analysisinternal.MustExtractDoc(doc, "maprange"), URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/maprange", - Requires: []*analysis.Analyzer{inspect.Analyzer}, + Requires: []*analysis.Analyzer{typeindexanalyzer.Analyzer}, Run: run, } @@ -31,60 +34,44 @@ var Analyzer = &analysis.Analyzer{ var xmaps = "golang.org/x/exp/maps" func run(pass *analysis.Pass) (any, error) { - inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) - switch pass.Pkg.Path() { case "maps", xmaps: // These packages know how to use their own APIs. return nil, nil } - - if !(analysisinternal.Imports(pass.Pkg, "maps") || analysisinternal.Imports(pass.Pkg, xmaps)) { - return nil, nil // fast path - } - - inspect.Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, func(n ast.Node) { - rangeStmt, ok := n.(*ast.RangeStmt) - if !ok { - return - } - call, ok := rangeStmt.X.(*ast.CallExpr) - if !ok { - return - } - callee := typeutil.Callee(pass.TypesInfo, call) - if !analysisinternal.IsFunctionNamed(callee, "maps", "Keys", "Values") && - !analysisinternal.IsFunctionNamed(callee, xmaps, "Keys", "Values") { - return - } - version := pass.Pkg.GoVersion() - pkg, fn := callee.Pkg().Path(), callee.Name() - key, value := rangeStmt.Key, rangeStmt.Value - - edits := editRangeStmt(pass, version, pkg, fn, key, value, call) - if len(edits) > 0 { - pass.Report(analysis.Diagnostic{ - Pos: call.Pos(), - End: call.End(), - Message: fmt.Sprintf("unnecessary and inefficient call of %s.%s", pkg, fn), - SuggestedFixes: []analysis.SuggestedFix{{ - Message: fmt.Sprintf("Remove unnecessary call to %s.%s", pkg, fn), - TextEdits: edits, - }}, - }) + var ( + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + mapsKeys = index.Object("maps", "Keys") + mapsValues = index.Object("maps", "Values") + xmapsKeys = index.Object(xmaps, "Keys") + xmapsValues = index.Object(xmaps, "Values") + ) + for _, callee := range []types.Object{mapsKeys, mapsValues, xmapsKeys, xmapsValues} { + for curCall := range index.Calls(callee) { + if ek, _ := curCall.ParentEdge(); ek != edge.RangeStmt_X { + continue + } + analyzeRangeStmt(pass, callee, curCall) } - }) - + } return nil, nil } -// editRangeStmt returns edits to transform a range statement that calls -// maps.Keys or maps.Values (either the stdlib or the x/exp/maps version). -// -// It reports a diagnostic if an edit cannot be made because the Go version is too old. +// analyzeRangeStmt analyzes range statements iterating over calls to maps.Keys +// or maps.Values (from the standard library "maps" or "golang.org/x/exp/maps"). // -// It returns nil if no edits are needed. -func editRangeStmt(pass *analysis.Pass, version, pkg, fn string, key, value ast.Expr, call *ast.CallExpr) []analysis.TextEdit { +// It reports a diagnostic with a suggested fix to simplify the loop by removing +// the unnecessary function call and adjusting range variables, if possible. +// For certain patterns involving x/exp/maps.Keys before Go 1.22, it reports +// a diagnostic about potential incorrect usage without a suggested fix. +// No diagnostic is reported if the range statement doesn't require changes. +func analyzeRangeStmt(pass *analysis.Pass, callee types.Object, curCall cursor.Cursor) { + var ( + call = curCall.Node().(*ast.CallExpr) + rangeStmt = curCall.Parent().Node().(*ast.RangeStmt) + pkg = callee.Pkg().Path() + fn = callee.Name() + ) var edits []analysis.TextEdit // Check if the call to maps.Keys or maps.Values can be removed/replaced. @@ -97,12 +84,21 @@ func editRangeStmt(pass *analysis.Pass, version, pkg, fn string, key, value ast. // If we have: for i, k := range maps.Keys(m) (only possible using x/exp/maps) // or: for i, v = range maps.Values(m) // do not remove the call. - removeCall := !isSet(key) || !isSet(value) + removeCall := !isSet(rangeStmt.Key) || !isSet(rangeStmt.Value) replace := "" - if pkg == xmaps && isSet(key) && value == nil { + if pkg == xmaps && isSet(rangeStmt.Key) && rangeStmt.Value == nil { // If we have: for i := range maps.Keys(m) (using x/exp/maps), // Replace with: for i := range len(m) replace = "len" + canRangeOverInt := fileUses(pass.TypesInfo, curCall, "go1.22") + if !canRangeOverInt { + pass.Report(analysis.Diagnostic{ + Pos: call.Pos(), + End: call.End(), + Message: fmt.Sprintf("likely incorrect use of %s.%s (returns a slice)", pkg, fn), + }) + return + } } if removeCall { edits = append(edits, analysis.TextEdit{ @@ -114,38 +110,37 @@ func editRangeStmt(pass *analysis.Pass, version, pkg, fn string, key, value ast. // Example: // for _, k := range maps.Keys(m) // ^^^ removeKey ^^^^^^^^^ removeCall - removeKey := pkg == xmaps && fn == "Keys" && !isSet(key) && isSet(value) + removeKey := pkg == xmaps && fn == "Keys" && !isSet(rangeStmt.Key) && isSet(rangeStmt.Value) if removeKey { edits = append(edits, analysis.TextEdit{ - Pos: key.Pos(), - End: value.Pos(), + Pos: rangeStmt.Key.Pos(), + End: rangeStmt.Value.Pos(), }) } // Check if a key should be inserted to the range statement. // Example: // for _, v := range maps.Values(m) // ^^^ addKey ^^^^^^^^^^^ removeCall - addKey := pkg == "maps" && fn == "Values" && isSet(key) + addKey := pkg == "maps" && fn == "Values" && isSet(rangeStmt.Key) if addKey { edits = append(edits, analysis.TextEdit{ - Pos: key.Pos(), - End: key.Pos(), + Pos: rangeStmt.Key.Pos(), + End: rangeStmt.Key.Pos(), NewText: []byte("_, "), }) } - // Range over int was added in Go 1.22. - // If the Go version is too old, report a diagnostic but do not make any edits. - if replace == "len" && versions.Before(pass.Pkg.GoVersion(), versions.Go1_22) { + if len(edits) > 0 { pass.Report(analysis.Diagnostic{ Pos: call.Pos(), End: call.End(), - Message: fmt.Sprintf("likely incorrect use of %s.%s (returns a slice)", pkg, fn), + Message: fmt.Sprintf("unnecessary and inefficient call of %s.%s", pkg, fn), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: fmt.Sprintf("Remove unnecessary call to %s.%s", pkg, fn), + TextEdits: edits, + }}, }) - return nil } - - return edits } // isSet reports whether an ast.Expr is a non-nil expression that is not the blank identifier. @@ -153,3 +148,14 @@ func isSet(expr ast.Expr) bool { ident, ok := expr.(*ast.Ident) return expr != nil && (!ok || ident.Name != "_") } + +// fileUses reports whether the file containing the specified cursor +// uses at least the specified version of Go (e.g. "go1.24"). +func fileUses(info *types.Info, c cursor.Cursor, version string) bool { + // TODO(adonovan): make Ancestors reflexive so !ok becomes impossible. + if curFile, ok := moreiters.First(c.Ancestors((*ast.File)(nil))); ok { + c = curFile + } + file := c.Node().(*ast.File) + return !versions.Before(info.FileVersions[file], version) +} From d70c04eddcecab264eae45dafa22a9811ff16c0c Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 24 Mar 2025 14:43:22 -0400 Subject: [PATCH 269/309] internal/refactor/inline: replace extractTxtar Use existing building blocks. Doing so uncovered a bug in a test file: duplicate filenames that were being overwritten. Fix by splitting into two files. Change-Id: I084b5aae00964be611b10c00096d196ab27b6cc9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660576 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/inline_test.go | 20 +---- .../inline/testdata/import-shadow-2.txtar | 75 +++++++++++++++++ .../inline/testdata/import-shadow.txtar | 82 +------------------ 3 files changed, 83 insertions(+), 94 deletions(-) create mode 100644 internal/refactor/inline/testdata/import-shadow-2.txtar diff --git a/internal/refactor/inline/inline_test.go b/internal/refactor/inline/inline_test.go index 3be37d5ecde..7f6c95e15f5 100644 --- a/internal/refactor/inline/inline_test.go +++ b/internal/refactor/inline/inline_test.go @@ -29,6 +29,7 @@ import ( "golang.org/x/tools/internal/expect" "golang.org/x/tools/internal/refactor/inline" "golang.org/x/tools/internal/testenv" + "golang.org/x/tools/internal/testfiles" "golang.org/x/tools/txtar" ) @@ -56,10 +57,11 @@ func TestData(t *testing.T) { if err != nil { t.Fatal(err) } - dir := t.TempDir() - if err := extractTxtar(ar, dir); err != nil { + fs, err := txtar.FS(ar) + if err != nil { t.Fatal(err) } + dir := testfiles.CopyToTmp(t, fs) // Load packages. cfg := &packages.Config{ @@ -1941,20 +1943,6 @@ func checkTranscode(callee *inline.Callee) error { return nil } -// TODO(adonovan): publish this a helper (#61386). -func extractTxtar(ar *txtar.Archive, dir string) error { - for _, file := range ar.Files { - name := filepath.Join(dir, file.Name) - if err := os.MkdirAll(filepath.Dir(name), 0777); err != nil { - return err - } - if err := os.WriteFile(name, file.Data, 0666); err != nil { - return err - } - } - return nil -} - // deepHash computes a cryptographic hash of an ast.Node so that // if the data structure is mutated, the hash changes. // It assumes Go variables do not change address. diff --git a/internal/refactor/inline/testdata/import-shadow-2.txtar b/internal/refactor/inline/testdata/import-shadow-2.txtar new file mode 100644 index 00000000000..14cd045c6c3 --- /dev/null +++ b/internal/refactor/inline/testdata/import-shadow-2.txtar @@ -0,0 +1,75 @@ +See import-shadow.txtar for a description. + +-- go.mod -- +module testdata +go 1.12 + +-- a/a.go -- +package a + +import "testdata/b" + +var x b.T + +func A(b int) { + x.F() //@ inline(re"F", fresult) +} + +-- b/b.go -- +package b + +type T struct{} + +func (T) F() { + One() + Two() +} + +func One() {} +func Two() {} + +-- fresult -- +package a + +import ( + "testdata/b" + b0 "testdata/b" +) + +var x b.T + +func A(b int) { + b0.One() + b0.Two() //@ inline(re"F", fresult) +} + +-- d/d.go -- +package d + +import "testdata/e" + +func D() { + const log = "shadow" + e.E() //@ inline(re"E", eresult) +} + +-- e/e.go -- +package e + +import "log" + +func E() { + log.Printf("") +} + +-- eresult -- +package d + +import ( + log0 "log" +) + +func D() { + const log = "shadow" + log0.Printf("") //@ inline(re"E", eresult) +} diff --git a/internal/refactor/inline/testdata/import-shadow.txtar b/internal/refactor/inline/testdata/import-shadow.txtar index 9d1abdb9e95..a1078e2495b 100644 --- a/internal/refactor/inline/testdata/import-shadow.txtar +++ b/internal/refactor/inline/testdata/import-shadow.txtar @@ -2,10 +2,10 @@ Just because a package (e.g. log) is imported by the caller, and the name log is in scope, doesn't mean the name in scope refers to the package: it could be locally shadowed. -In all three scenarios below, renaming import with a fresh name is -added because the usual name is locally shadowed: in cases 1, 2 an -existing import is shadowed by (respectively) a local constant, -parameter; in case 3 there is no existing import. +In all three scenarios in this file and import-shadow-2.txtar, a renaming +import with a fresh name is added because the usual name is locally +shadowed: in cases 1, 2 an existing import is shadowed by (respectively) +a local constant, parameter; in case 3 there is no existing import. -- go.mod -- module testdata @@ -47,77 +47,3 @@ func A() { } var _ log.Logger - --- go.mod -- -module testdata -go 1.12 - --- a/a.go -- -package a - -import "testdata/b" - -var x b.T - -func A(b int) { - x.F() //@ inline(re"F", fresult) -} - --- b/b.go -- -package b - -type T struct{} - -func (T) F() { - One() - Two() -} - -func One() {} -func Two() {} - --- fresult -- -package a - -import ( - "testdata/b" - b0 "testdata/b" -) - -var x b.T - -func A(b int) { - b0.One() - b0.Two() //@ inline(re"F", fresult) -} - --- d/d.go -- -package d - -import "testdata/e" - -func D() { - const log = "shadow" - e.E() //@ inline(re"E", eresult) -} - --- e/e.go -- -package e - -import "log" - -func E() { - log.Printf("") -} - --- eresult -- -package d - -import ( - log0 "log" -) - -func D() { - const log = "shadow" - log0.Printf("") //@ inline(re"E", eresult) -} From c1b6839e804981926f118cb500a8faf52670fac8 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 25 Mar 2025 17:02:13 -0400 Subject: [PATCH 270/309] internal/astutil/cursor: Ancestors -> Enclosing (+ reflexive) This CL makes Ancestors reflexive, which is the behaviour nearly always wanted by clients. (The former irreflexive behavior is easily requested by adding a call to Cursor.Parent.) Also, rename it to Enclosing, which is at least ambiguous as to its reflexivity, unlike Ancestors. Change-Id: I83dc92c58939058e9a30f8f54727667014fcf3c2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660775 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- .../analysis/fillreturns/fillreturns.go | 16 +++++----------- gopls/internal/analysis/gofix/gofix.go | 2 +- gopls/internal/analysis/maprange/maprange.go | 6 ++---- gopls/internal/analysis/modernize/bloop.go | 6 ++---- .../internal/analysis/modernize/modernize.go | 5 +---- .../internal/analysis/nonewvars/nonewvars.go | 9 +++------ .../analysis/noresultvalues/noresultvalues.go | 2 +- gopls/internal/golang/codeaction.go | 2 +- .../golang/stubmethods/stubmethods.go | 2 +- internal/astutil/cursor/cursor.go | 19 +++++++++---------- internal/astutil/cursor/cursor_test.go | 11 +++++------ internal/typesinternal/typeindex/typeindex.go | 2 +- 12 files changed, 32 insertions(+), 50 deletions(-) diff --git a/gopls/internal/analysis/fillreturns/fillreturns.go b/gopls/internal/analysis/fillreturns/fillreturns.go index 184aac5ea1f..a90105f6f56 100644 --- a/gopls/internal/analysis/fillreturns/fillreturns.go +++ b/gopls/internal/analysis/fillreturns/fillreturns.go @@ -55,12 +55,9 @@ outer: } // Find cursor for enclosing return statement (which may be curErr itself). - curRet := curErr - if _, ok := curRet.Node().(*ast.ReturnStmt); !ok { - curRet, ok = moreiters.First(curErr.Ancestors((*ast.ReturnStmt)(nil))) - if !ok { - continue // no enclosing return - } + curRet, ok := moreiters.First(curErr.Enclosing((*ast.ReturnStmt)(nil))) + if !ok { + continue // no enclosing return } ret := curRet.Node().(*ast.ReturnStmt) @@ -114,7 +111,7 @@ outer: retTyps = append(retTyps, retTyp) } - curFile, _ := moreiters.First(curRet.Ancestors((*ast.File)(nil))) + curFile, _ := moreiters.First(curRet.Enclosing((*ast.File)(nil))) file := curFile.Node().(*ast.File) matches := analysisinternal.MatchingIdents(retTyps, file, ret.Pos(), info, pass.Pkg) qual := typesinternal.FileQualifier(file, pass.Pkg) @@ -230,8 +227,5 @@ func fixesError(err types.Error) bool { // enclosingFunc returns the cursor for the innermost Func{Decl,Lit} // that encloses c, if any. func enclosingFunc(c cursor.Cursor) (cursor.Cursor, bool) { - for curAncestor := range c.Ancestors((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { - return curAncestor, true - } - return cursor.Cursor{}, false + return moreiters.First(c.Enclosing((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil))) } diff --git a/gopls/internal/analysis/gofix/gofix.go b/gopls/internal/analysis/gofix/gofix.go index bff4120a39a..6f4c8a6e2fd 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/gopls/internal/analysis/gofix/gofix.go @@ -592,7 +592,7 @@ func (a *analyzer) readFile(node ast.Node) ([]byte, error) { // currentFile returns the unique ast.File for a cursor. func currentFile(c cursor.Cursor) *ast.File { - cf, _ := moreiters.First(c.Ancestors((*ast.File)(nil))) + cf, _ := moreiters.First(c.Enclosing((*ast.File)(nil))) return cf.Node().(*ast.File) } diff --git a/gopls/internal/analysis/maprange/maprange.go b/gopls/internal/analysis/maprange/maprange.go index 4bd7066b8cb..eed04b14e72 100644 --- a/gopls/internal/analysis/maprange/maprange.go +++ b/gopls/internal/analysis/maprange/maprange.go @@ -9,6 +9,7 @@ import ( "fmt" "go/ast" "go/types" + "golang.org/x/tools/go/analysis" "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" @@ -152,10 +153,7 @@ func isSet(expr ast.Expr) bool { // fileUses reports whether the file containing the specified cursor // uses at least the specified version of Go (e.g. "go1.24"). func fileUses(info *types.Info, c cursor.Cursor, version string) bool { - // TODO(adonovan): make Ancestors reflexive so !ok becomes impossible. - if curFile, ok := moreiters.First(c.Ancestors((*ast.File)(nil))); ok { - c = curFile - } + c, _ = moreiters.First(c.Enclosing((*ast.File)(nil))) file := c.Node().(*ast.File) return !versions.Before(info.FileVersions[file], version) } diff --git a/gopls/internal/analysis/modernize/bloop.go b/gopls/internal/analysis/modernize/bloop.go index 1fc07169486..5bfb0b7d8e8 100644 --- a/gopls/internal/analysis/modernize/bloop.go +++ b/gopls/internal/analysis/modernize/bloop.go @@ -14,6 +14,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" "golang.org/x/tools/internal/astutil/cursor" @@ -169,8 +170,5 @@ func uses(index *typeindex.Index, cur cursor.Cursor, obj types.Object) bool { // enclosingFunc returns the cursor for the innermost Func{Decl,Lit} // that encloses c, if any. func enclosingFunc(c cursor.Cursor) (cursor.Cursor, bool) { - for curAncestor := range c.Ancestors((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { - return curAncestor, true - } - return cursor.Cursor{}, false + return moreiters.First(c.Enclosing((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil))) } diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index ac4a5c28e5f..ebf83ab1bc3 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -148,10 +148,7 @@ func fileUses(info *types.Info, file *ast.File, version string) bool { // enclosingFile returns the syntax tree for the file enclosing c. func enclosingFile(c cursor.Cursor) *ast.File { - // TODO(adonovan): make Ancestors reflexive so !ok becomes impossible. - if curFile, ok := moreiters.First(c.Ancestors((*ast.File)(nil))); ok { - c = curFile - } + c, _ = moreiters.First(c.Enclosing((*ast.File)(nil))) return c.Node().(*ast.File) } diff --git a/gopls/internal/analysis/nonewvars/nonewvars.go b/gopls/internal/analysis/nonewvars/nonewvars.go index b7f861ba7f1..eeae7211c97 100644 --- a/gopls/internal/analysis/nonewvars/nonewvars.go +++ b/gopls/internal/analysis/nonewvars/nonewvars.go @@ -49,14 +49,11 @@ func run(pass *analysis.Pass) (any, error) { } // Find enclosing assignment (which may be curErr itself). - assign, ok := curErr.Node().(*ast.AssignStmt) + curAssign, ok := moreiters.First(curErr.Enclosing((*ast.AssignStmt)(nil))) if !ok { - cur, ok := moreiters.First(curErr.Ancestors((*ast.AssignStmt)(nil))) - if !ok { - continue // no enclosing assignment - } - assign = cur.Node().(*ast.AssignStmt) + continue // no enclosing assignment } + assign := curAssign.Node().(*ast.AssignStmt) if assign.Tok != token.DEFINE { continue // not a := statement } diff --git a/gopls/internal/analysis/noresultvalues/noresultvalues.go b/gopls/internal/analysis/noresultvalues/noresultvalues.go index 6b8f9d895e4..848f6532ce0 100644 --- a/gopls/internal/analysis/noresultvalues/noresultvalues.go +++ b/gopls/internal/analysis/noresultvalues/noresultvalues.go @@ -48,7 +48,7 @@ func run(pass *analysis.Pass) (any, error) { continue // can't find errant node } // Find first enclosing return statement, if any. - if curRet, ok := moreiters.First(curErr.Ancestors((*ast.ReturnStmt)(nil))); ok { + if curRet, ok := moreiters.First(curErr.Enclosing((*ast.ReturnStmt)(nil))); ok { ret := curRet.Node() pass.Report(analysis.Diagnostic{ Pos: start, diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index a5591edf1f9..d9f2af47d24 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -956,7 +956,7 @@ func goAssembly(ctx context.Context, req *codeActionsRequest) error { sym.WriteString(".") curSel, _ := req.pgf.Cursor.FindPos(req.start, req.end) - for cur := range curSel.Ancestors((*ast.FuncDecl)(nil), (*ast.ValueSpec)(nil)) { + for cur := range curSel.Enclosing((*ast.FuncDecl)(nil), (*ast.ValueSpec)(nil)) { var name string // in command title switch node := cur.Node().(type) { case *ast.FuncDecl: diff --git a/gopls/internal/golang/stubmethods/stubmethods.go b/gopls/internal/golang/stubmethods/stubmethods.go index a060993b1ab..43842264d70 100644 --- a/gopls/internal/golang/stubmethods/stubmethods.go +++ b/gopls/internal/golang/stubmethods/stubmethods.go @@ -54,7 +54,7 @@ type IfaceStubInfo struct { func GetIfaceStubInfo(fset *token.FileSet, info *types.Info, pgf *parsego.File, pos, end token.Pos) *IfaceStubInfo { // TODO(adonovan): simplify, using Cursor: // curErr, _ := pgf.Cursor.FindPos(pos, end) - // for cur := range curErr.Ancestors() { + // for cur := range curErr.Enclosing() { // switch n := cur.Node().(type) {... path, _ := astutil.PathEnclosingInterval(pgf.File, pos, end) for _, n := range path { diff --git a/internal/astutil/cursor/cursor.go b/internal/astutil/cursor/cursor.go index 9139d4e516c..3f015998c52 100644 --- a/internal/astutil/cursor/cursor.go +++ b/internal/astutil/cursor/cursor.go @@ -198,30 +198,29 @@ func (c Cursor) Stack(stack []Cursor) []Cursor { panic("Cursor.Stack called on Root node") } - stack = append(stack, c) - stack = slices.AppendSeq(stack, c.Ancestors()) + stack = slices.AppendSeq(stack, c.Enclosing()) slices.Reverse(stack) return stack } -// Ancestors returns an iterator over the ancestors of the current -// node, starting with [Cursor.Parent]. +// Enclosing returns an iterator over the nodes enclosing the current +// current node, starting with the Cursor itself. // -// Ancestors must not be called on the Root node (whose [Cursor.Node] returns nil). +// Enclosing must not be called on the Root node (whose [Cursor.Node] returns nil). // // The types argument, if non-empty, enables type-based filtering of -// events: the sequence includes only ancestors whose type matches an -// element of the types slice. -func (c Cursor) Ancestors(types ...ast.Node) iter.Seq[Cursor] { +// events: the sequence includes only enclosing nodes whose type +// matches an element of the types slice. +func (c Cursor) Enclosing(types ...ast.Node) iter.Seq[Cursor] { if c.index < 0 { - panic("Cursor.Ancestors called on Root node") + panic("Cursor.Enclosing called on Root node") } mask := maskOf(types) return func(yield func(Cursor) bool) { events := c.events() - for i := events[c.index].parent; i >= 0; i = events[i].parent { + for i := c.index; i >= 0; i = events[i].parent { if events[i].typ&mask != 0 && !yield(Cursor{c.in, i}) { break } diff --git a/internal/astutil/cursor/cursor_test.go b/internal/astutil/cursor/cursor_test.go index 380414df790..9effae912a3 100644 --- a/internal/astutil/cursor/cursor_test.go +++ b/internal/astutil/cursor/cursor_test.go @@ -162,11 +162,10 @@ func g() { t.Errorf("curCall.Stack() = %q, want %q", got, want) } - // Ancestors = Reverse(Stack[:last]). - stack = stack[:len(stack)-1] + // Enclosing = Reverse(Stack()). slices.Reverse(stack) - if got, want := slices.Collect(curCall.Ancestors()), stack; !reflect.DeepEqual(got, want) { - t.Errorf("Ancestors = %v, Reverse(Stack - last element) = %v", got, want) + if got, want := slices.Collect(curCall.Enclosing()), stack; !reflect.DeepEqual(got, want) { + t.Errorf("Enclosing = %v, Reverse(Stack - last element) = %v", got, want) } } @@ -542,12 +541,12 @@ func BenchmarkInspectCalls(b *testing.B) { } }) - b.Run("CursorAncestors", func(b *testing.B) { + b.Run("CursorEnclosing", func(b *testing.B) { var ncalls int for range b.N { for cur := range cursor.Root(inspect).Preorder(callExprs...) { _ = cur.Node().(*ast.CallExpr) - for range cur.Ancestors() { + for range cur.Enclosing() { } ncalls++ } diff --git a/internal/typesinternal/typeindex/typeindex.go b/internal/typesinternal/typeindex/typeindex.go index a6cc6956892..34087a98fbf 100644 --- a/internal/typesinternal/typeindex/typeindex.go +++ b/internal/typesinternal/typeindex/typeindex.go @@ -188,7 +188,7 @@ func (ix *Index) Calls(callee types.Object) iter.Seq[cursor.Cursor] { // CallExpr ancestor: we have to make sure the // ident is in the CallExpr.Fun position, otherwise // f(f, f) would have two spurious matches. - // Avoiding Ancestors is also significantly faster. + // Avoiding Enclosing is also significantly faster. // inverse unparen: f -> (f) for ek == edge.ParenExpr_X { From b3ce3e13267b25b8adf8d9c7d39cd549b3674012 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Sat, 22 Mar 2025 00:52:46 -0600 Subject: [PATCH 271/309] gopls/completion: use high score for package name main when current package is main This CL gives main package a high score when it finds the current package name is main to ensure new files under main package will have main package as best suggestion. Besides, this CL sorts the package candidates based on their scores in descending order, which was missed before, as the other functions do. Fixes golang/go#72993 Change-Id: Ib09199b915ad8731c7ee359dddf6e37280df39b3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/659995 Reviewed-by: Peter Weinberger Commit-Queue: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Dmitri Shuralyov Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- .../internal/golang/completion/completion.go | 18 +++++++------- gopls/internal/golang/completion/package.go | 24 +++++++++++++++---- gopls/internal/server/completion.go | 2 ++ .../integration/completion/completion_test.go | 11 +++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index 600219370b9..a3270f97909 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -164,14 +164,14 @@ func (i *CompletionItem) addConversion(c *completer, conv conversionEdits) error // Scoring constants are used for weighting the relevance of different candidates. const ( + // lowScore indicates an irrelevant or not useful completion item. + lowScore float64 = 0.01 + // stdScore is the base score for all completion items. stdScore float64 = 1.0 // highScore indicates a very relevant completion item. highScore float64 = 10.0 - - // lowScore indicates an irrelevant or not useful completion item. - lowScore float64 = 0.01 ) // matcher matches a candidate's label against the user input. The @@ -702,7 +702,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p // depend on other candidates having already been collected. c.addStatementCandidates() - c.sortItems() + sortItems(c.items) return c.items, c.getSurrounding(), nil } @@ -830,16 +830,16 @@ func (c *completer) scanToken(contents []byte) (token.Pos, token.Token, string) } } -func (c *completer) sortItems() { - sort.SliceStable(c.items, func(i, j int) bool { +func sortItems(items []CompletionItem) { + sort.SliceStable(items, func(i, j int) bool { // Sort by score first. - if c.items[i].Score != c.items[j].Score { - return c.items[i].Score > c.items[j].Score + if items[i].Score != items[j].Score { + return items[i].Score > items[j].Score } // Then sort by label so order stays consistent. This also has the // effect of preferring shorter candidates. - return c.items[i].Label < c.items[j].Label + return items[i].Label < items[j].Label }) } diff --git a/gopls/internal/golang/completion/package.go b/gopls/internal/golang/completion/package.go index 5fd6c04144d..01d5622c7f7 100644 --- a/gopls/internal/golang/completion/package.go +++ b/gopls/internal/golang/completion/package.go @@ -62,7 +62,7 @@ func packageClauseCompletions(ctx context.Context, snapshot *cache.Snapshot, fh Score: pkg.score, }) } - + sortItems(items) return items, surrounding, nil } @@ -197,11 +197,20 @@ func packageSuggestions(ctx context.Context, snapshot *cache.Snapshot, fileURI p } matcher := fuzzy.NewMatcher(prefix) + var currentPackageName string + if variants, err := snapshot.MetadataForFile(ctx, fileURI); err == nil && + len(variants) != 0 { + currentPackageName = string(variants[0].Name) + } // Always try to suggest a main package defer func() { + mainScore := lowScore + if currentPackageName == "main" { + mainScore = highScore + } if score := float64(matcher.Score("main")); score > 0 { - packages = append(packages, toCandidate("main", score*lowScore)) + packages = append(packages, toCandidate("main", score*mainScore)) } }() @@ -254,15 +263,20 @@ func packageSuggestions(ctx context.Context, snapshot *cache.Snapshot, fileURI p seenPkgs[testPkgName] = struct{}{} } - // Add current directory name as a low relevance suggestion. if _, ok := seenPkgs[pkgName]; !ok { + // Add current directory name as a low relevance suggestion. + dirNameScore := lowScore + // if current package name is empty, the dir name is the best choice. + if currentPackageName == "" { + dirNameScore = highScore + } if score := float64(matcher.Score(string(pkgName))); score > 0 { - packages = append(packages, toCandidate(string(pkgName), score*lowScore)) + packages = append(packages, toCandidate(string(pkgName), score*dirNameScore)) } testPkgName := pkgName + "_test" if score := float64(matcher.Score(string(testPkgName))); score > 0 { - packages = append(packages, toCandidate(string(testPkgName), score*lowScore)) + packages = append(packages, toCandidate(string(testPkgName), score*dirNameScore)) } } diff --git a/gopls/internal/server/completion.go b/gopls/internal/server/completion.go index 6c185e93717..e72d156de05 100644 --- a/gopls/internal/server/completion.go +++ b/gopls/internal/server/completion.go @@ -102,6 +102,8 @@ func (s *server) saveLastCompletion(uri protocol.DocumentURI, version int32, ite s.efficacyItems = items } +// toProtocolCompletionItems converts the candidates to the protocol completion items, +// the candidates must be sorted based on score as it will be respected by client side. func toProtocolCompletionItems(candidates []completion.CompletionItem, surrounding *completion.Selection, options *settings.Options) ([]protocol.CompletionItem, error) { replaceRng, err := surrounding.Range() if err != nil { diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index 0713b1f62b9..8fa03908c01 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go @@ -53,6 +53,10 @@ func TestPackageCompletion(t *testing.T) { module mod.com go 1.12 +-- cmd/main.go -- +package main +-- cmd/testfile.go -- +package -- fruits/apple.go -- package apple @@ -95,6 +99,13 @@ package want []string editRegexp string }{ + { + name: "main package completion after package keyword", + filename: "cmd/testfile.go", + triggerRegexp: "package()", + want: []string{"package main", "package cmd", "package cmd_test"}, + editRegexp: "package", + }, { name: "package completion at valid position", filename: "fruits/testfile.go", From 6a913554a79edb44963cc65b675f01ad4ed911a7 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 26 Mar 2025 08:21:39 -0400 Subject: [PATCH 272/309] internal/refactor/inline: factor out import map construction Pull out some code from inlineCall to simplify it. Change-Id: I4f354493648c78c26bec93935f014e78ef48694c Reviewed-on: https://go-review.googlesource.com/c/tools/+/660955 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/inline.go | 98 ++++++++++++++++-------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 2b6f06242e7..749d8cb7a4f 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -586,52 +586,10 @@ func (st *state) inlineCall() (*inlineCallResult, error) { assign1 = func(v *types.Var) bool { return !updatedLocals[v] } } - // import map, initially populated with caller imports, and updated below + // Build a map, initially populated with caller imports, and updated below // with new imports necessary to reference free symbols in the callee. - // - // For simplicity we ignore existing dot imports, so that a qualified - // identifier (QI) in the callee is always represented by a QI in the caller, - // allowing us to treat a QI like a selection on a package name. - importMap := make(map[string][]string) // maps package path to local name(s) - var oldImports []oldImport // imports referenced only by caller.Call.Fun - - for _, imp := range caller.File.Imports { - if pkgName, ok := importedPkgName(caller.Info, imp); ok && - pkgName.Name() != "." && - pkgName.Name() != "_" { - - // If the import's sole use is in caller.Call.Fun of the form p.F(...), - // where p.F is a qualified identifier, the p import may not be - // necessary. - // - // Only the qualified identifier case matters, as other references to - // imported package names in the Call.Fun expression (e.g. - // x.after(3*time.Second).f() or time.Second.String()) will remain after - // inlining, as arguments. - // - // If that is the case, proactively check if any of the callee FreeObjs - // need this import. Doing so eagerly simplifies the resulting logic. - needed := true - sel, ok := ast.Unparen(caller.Call.Fun).(*ast.SelectorExpr) - if ok && soleUse(caller.Info, pkgName) == sel.X { - needed = false // no longer needed by caller - // Check to see if any of the inlined free objects need this package. - for _, obj := range callee.FreeObjs { - if obj.PkgPath == pkgName.Imported().Path() && obj.Shadow[pkgName.Name()] == 0 { - needed = true // needed by callee - break - } - } - } - - if needed { - path := pkgName.Imported().Path() - importMap[path] = append(importMap[path], pkgName.Name()) - } else { - oldImports = append(oldImports, oldImport{pkgName: pkgName, spec: imp}) - } - } - } + // oldImports are caller imports that won't be needed after inlining. + importMap, oldImports := callerImportMap(caller, callee) // importName finds an existing import name to use in a particular shadowing // context. It is used to determine the set of new imports in @@ -1390,6 +1348,56 @@ func (st *state) inlineCall() (*inlineCallResult, error) { return res, nil } +// callerImportMap returns a map from package paths in the caller's file to local names. +// The map excludes imports not needed by the caller or callee after inlining; the second +// return value holds these. +func callerImportMap(caller *Caller, callee *gobCallee) (map[string][]string, []oldImport) { + // For simplicity we ignore existing dot imports, so that a qualified + // identifier (QI) in the callee is always represented by a QI in the caller, + // allowing us to treat a QI like a selection on a package name. + importMap := make(map[string][]string) // maps package path to local name(s) + var oldImports []oldImport // imports referenced only by caller.Call.Fun + + for _, imp := range caller.File.Imports { + if pkgName, ok := importedPkgName(caller.Info, imp); ok && + pkgName.Name() != "." && + pkgName.Name() != "_" { + + // If the import's sole use is in caller.Call.Fun of the form p.F(...), + // where p.F is a qualified identifier, the p import may not be + // necessary. + // + // Only the qualified identifier case matters, as other references to + // imported package names in the Call.Fun expression (e.g. + // x.after(3*time.Second).f() or time.Second.String()) will remain after + // inlining, as arguments. + // + // If that is the case, proactively check if any of the callee FreeObjs + // need this import. Doing so eagerly simplifies the resulting logic. + needed := true + sel, ok := ast.Unparen(caller.Call.Fun).(*ast.SelectorExpr) + if ok && soleUse(caller.Info, pkgName) == sel.X { + needed = false // no longer needed by caller + // Check to see if any of the inlined free objects need this package. + for _, obj := range callee.FreeObjs { + if obj.PkgPath == pkgName.Imported().Path() && obj.Shadow[pkgName.Name()] == 0 { + needed = true // needed by callee + break + } + } + } + + if needed { + path := pkgName.Imported().Path() + importMap[path] = append(importMap[path], pkgName.Name()) + } else { + oldImports = append(oldImports, oldImport{pkgName: pkgName, spec: imp}) + } + } + } + return importMap, oldImports +} + type argument struct { expr ast.Expr typ types.Type // may be tuple for sole non-receiver arg in spread call From 2d8ef138e247bcb82c39faca236398bb434f95ba Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 26 Mar 2025 07:44:39 -0400 Subject: [PATCH 273/309] internal/refactor/inline: document test file format Change-Id: Ie4b4a9548b66f83690e795755eefcce726150a1b Reviewed-on: https://go-review.googlesource.com/c/tools/+/660895 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/refactor/inline/inline_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/refactor/inline/inline_test.go b/internal/refactor/inline/inline_test.go index 7f6c95e15f5..611541c9677 100644 --- a/internal/refactor/inline/inline_test.go +++ b/internal/refactor/inline/inline_test.go @@ -34,6 +34,28 @@ import ( ) // TestData executes test scenarios specified by files in testdata/*.txtar. +// Each txtar file describes two sets of files, some containing Go source +// and others expected results. +// +// The Go source files and go.mod are parsed and type-checked as a Go module. +// Some of these files contain marker comments (in a form described below) describing +// the inlinings to perform and whether they should succeed or fail. A marker +// indicating success refers to another file in the txtar, not a .go +// file, that should contain the contents of the first file after inlining. +// +// The marker format for success is +// +// @inline(re"pat", wantfile) +// +// The first call in the marker's line that matches pat is inlined, and the contents +// of the resulting file must match the contents of wantfile. +// +// The marker format for failure is +// +// @inline(re"pat", re"errpat") +// +// The first argument selects the call for inlining as before, and the second +// is a regular expression that must match the text of resulting error. func TestData(t *testing.T) { testenv.NeedsGoPackages(t) @@ -120,8 +142,9 @@ func TestData(t *testing.T) { var want any switch x := note.Args[1].(type) { case string, expect.Identifier: + name := fmt.Sprint(x) for _, file := range ar.Files { - if file.Name == fmt.Sprint(x) { + if file.Name == name { want = file.Data break } From fbb70473486945bd125dd973858578e184619dc1 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 26 Mar 2025 12:41:46 -0400 Subject: [PATCH 274/309] internal/refactor/inline: extract import handling from inlineCall Change-Id: I4102e9a11aca35daf52fbaa343d30bde50dd9fb1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660957 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/inline.go | 308 +++++++++++++++-------------- 1 file changed, 157 insertions(+), 151 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 749d8cb7a4f..d89a62972c6 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -470,6 +470,156 @@ type newImport struct { spec *ast.ImportSpec } +// importState tracks information about imports. +type importState struct { + logf func(string, ...any) + caller *Caller + importMap map[string][]string // from package paths in the caller's file to local names + newImports []newImport // for references to free names in callee; to be added to the file + oldImports []oldImport // referenced only by caller.Call.Fun; to be removed from the file +} + +// newImportState returns an importState with initial information about the caller's imports. +func newImportState(logf func(string, ...any), caller *Caller, callee *gobCallee) *importState { + // For simplicity we ignore existing dot imports, so that a qualified + // identifier (QI) in the callee is always represented by a QI in the caller, + // allowing us to treat a QI like a selection on a package name. + is := &importState{ + logf: logf, + caller: caller, + importMap: make(map[string][]string), + } + + for _, imp := range caller.File.Imports { + if pkgName, ok := importedPkgName(caller.Info, imp); ok && + pkgName.Name() != "." && + pkgName.Name() != "_" { + + // If the import's sole use is in caller.Call.Fun of the form p.F(...), + // where p.F is a qualified identifier, the p import may not be + // necessary. + // + // Only the qualified identifier case matters, as other references to + // imported package names in the Call.Fun expression (e.g. + // x.after(3*time.Second).f() or time.Second.String()) will remain after + // inlining, as arguments. + // + // If that is the case, proactively check if any of the callee FreeObjs + // need this import. Doing so eagerly simplifies the resulting logic. + needed := true + sel, ok := ast.Unparen(caller.Call.Fun).(*ast.SelectorExpr) + if ok && soleUse(caller.Info, pkgName) == sel.X { + needed = false // no longer needed by caller + // Check to see if any of the inlined free objects need this package. + for _, obj := range callee.FreeObjs { + if obj.PkgPath == pkgName.Imported().Path() && obj.Shadow[pkgName.Name()] == 0 { + needed = true // needed by callee + break + } + } + } + + // Exclude imports not needed by the caller or callee after inlining; the second + // return value holds these. + if needed { + path := pkgName.Imported().Path() + is.importMap[path] = append(is.importMap[path], pkgName.Name()) + } else { + is.oldImports = append(is.oldImports, oldImport{pkgName: pkgName, spec: imp}) + } + } + } + return is +} + +// importName finds an existing import name to use in a particular shadowing +// context. It is used to determine the set of new imports in +// getOrMakeImportName, and is also used for writing out names in inlining +// strategies below. +func (i *importState) importName(pkgPath string, shadow shadowMap) string { + for _, name := range i.importMap[pkgPath] { + // Check that either the import preexisted, or that it was newly added + // (no PkgName) but is not shadowed, either in the callee (shadows) or + // caller (caller.lookup). + if shadow[name] == 0 { + found := i.caller.lookup(name) + if is[*types.PkgName](found) || found == nil { + return name + } + } + } + return "" +} + +// localName returns the local name for a given imported package path, +// adding one if it doesn't exists. +func (i *importState) localName(pkgPath, pkgName string, shadow shadowMap) string { + // Does an import already exist that works in this shadowing context? + if name := i.importName(pkgPath, shadow); name != "" { + return name + } + + newlyAdded := func(name string) bool { + for _, new := range i.newImports { + if new.pkgName == name { + return true + } + } + return false + } + + // shadowedInCaller reports whether a candidate package name + // already refers to a declaration in the caller. + shadowedInCaller := func(name string) bool { + obj := i.caller.lookup(name) + if obj == nil { + return false + } + // If obj will be removed, the name is available. + for _, old := range i.oldImports { + if old.pkgName == obj { + return false + } + } + return true + } + + // import added by callee + // + // Choose local PkgName based on last segment of + // package path plus, if needed, a numeric suffix to + // ensure uniqueness. + // + // "init" is not a legal PkgName. + // + // TODO(rfindley): is it worth preserving local package names for callee + // imports? Are they likely to be better or worse than the name we choose + // here? + base := pkgName + name := base + for n := 0; shadow[name] != 0 || shadowedInCaller(name) || newlyAdded(name) || name == "init"; n++ { + name = fmt.Sprintf("%s%d", base, n) + } + i.logf("adding import %s %q", name, pkgPath) + spec := &ast.ImportSpec{ + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: strconv.Quote(pkgPath), + }, + } + // Use explicit pkgname (out of necessity) when it differs from the declared name, + // or (for good style) when it differs from base(pkgpath). + if name != pkgName || name != pathpkg.Base(pkgPath) { + spec.Name = makeIdent(name) + } + i.newImports = append(i.newImports, newImport{ + pkgName: name, + spec: spec, + }) + i.importMap[pkgPath] = append(i.importMap[pkgPath], name) + return name +} + type inlineCallResult struct { newImports []newImport // to add oldImports []oldImport // to remove @@ -586,102 +736,8 @@ func (st *state) inlineCall() (*inlineCallResult, error) { assign1 = func(v *types.Var) bool { return !updatedLocals[v] } } - // Build a map, initially populated with caller imports, and updated below - // with new imports necessary to reference free symbols in the callee. - // oldImports are caller imports that won't be needed after inlining. - importMap, oldImports := callerImportMap(caller, callee) - - // importName finds an existing import name to use in a particular shadowing - // context. It is used to determine the set of new imports in - // getOrMakeImportName, and is also used for writing out names in inlining - // strategies below. - importName := func(pkgPath string, shadow shadowMap) string { - for _, name := range importMap[pkgPath] { - // Check that either the import preexisted, or that it was newly added - // (no PkgName) but is not shadowed, either in the callee (shadows) or - // caller (caller.lookup). - if shadow[name] == 0 { - found := caller.lookup(name) - if is[*types.PkgName](found) || found == nil { - return name - } - } - } - return "" - } - - // keep track of new imports that are necessary to reference any free names - // in the callee. - var newImports []newImport - - // getOrMakeImportName returns the local name for a given imported package path, - // adding one if it doesn't exists. - getOrMakeImportName := func(pkgPath, pkgName string, shadow shadowMap) string { - // Does an import already exist that works in this shadowing context? - if name := importName(pkgPath, shadow); name != "" { - return name - } - - newlyAdded := func(name string) bool { - for _, new := range newImports { - if new.pkgName == name { - return true - } - } - return false - } - - // shadowedInCaller reports whether a candidate package name - // already refers to a declaration in the caller. - shadowedInCaller := func(name string) bool { - obj := caller.lookup(name) - if obj == nil { - return false - } - // If obj will be removed, the name is available. - for _, old := range oldImports { - if old.pkgName == obj { - return false - } - } - return true - } - - // import added by callee - // - // Choose local PkgName based on last segment of - // package path plus, if needed, a numeric suffix to - // ensure uniqueness. - // - // "init" is not a legal PkgName. - // - // TODO(rfindley): is it worth preserving local package names for callee - // imports? Are they likely to be better or worse than the name we choose - // here? - base := pkgName - name := base - for n := 0; shadow[name] != 0 || shadowedInCaller(name) || newlyAdded(name) || name == "init"; n++ { - name = fmt.Sprintf("%s%d", base, n) - } - logf("adding import %s %q", name, pkgPath) - spec := &ast.ImportSpec{ - Path: &ast.BasicLit{ - Kind: token.STRING, - Value: strconv.Quote(pkgPath), - }, - } - // Use explicit pkgname (out of necessity) when it differs from the declared name, - // or (for good style) when it differs from base(pkgpath). - if name != pkgName || name != pathpkg.Base(pkgPath) { - spec.Name = makeIdent(name) - } - newImports = append(newImports, newImport{ - pkgName: name, - spec: spec, - }) - importMap[pkgPath] = append(importMap[pkgPath], name) - return name - } + // Extract information about the caller's imports. + istate := newImportState(logf, caller, callee) // Compute the renaming of the callee's free identifiers. objRenames := make([]ast.Expr, len(callee.FreeObjs)) // nil => no change @@ -709,7 +765,7 @@ func (st *state) inlineCall() (*inlineCallResult, error) { var newName ast.Expr if obj.Kind == "pkgname" { // Use locally appropriate import, creating as needed. - n := getOrMakeImportName(obj.PkgPath, obj.PkgName, obj.Shadow) + n := istate.localName(obj.PkgPath, obj.PkgName, obj.Shadow) newName = makeIdent(n) // imported package } else if !obj.ValidPos { // Built-in function, type, or value (e.g. nil, zero): @@ -754,7 +810,7 @@ func (st *state) inlineCall() (*inlineCallResult, error) { // Form a qualified identifier, pkg.Name. if qualify { - pkgName := getOrMakeImportName(obj.PkgPath, obj.PkgName, obj.Shadow) + pkgName := istate.localName(obj.PkgPath, obj.PkgName, obj.Shadow) newName = &ast.SelectorExpr{ X: makeIdent(pkgName), Sel: makeIdent(obj.Name), @@ -765,8 +821,8 @@ func (st *state) inlineCall() (*inlineCallResult, error) { } res := &inlineCallResult{ - newImports: newImports, - oldImports: oldImports, + newImports: istate.newImports, + oldImports: istate.oldImports, } // Parse callee function declaration. @@ -1115,7 +1171,7 @@ func (st *state) inlineCall() (*inlineCallResult, error) { (!needBindingDecl || (bindingDecl != nil && len(bindingDecl.names) == 0)) { // Reduces to: { var (bindings); lhs... := rhs... } - if newStmts, ok := st.assignStmts(stmt, results, importName); ok { + if newStmts, ok := st.assignStmts(stmt, results, istate.importName); ok { logf("strategy: reduce assign-context call to { return exprs }") clearPositions(calleeDecl.Body) @@ -1348,56 +1404,6 @@ func (st *state) inlineCall() (*inlineCallResult, error) { return res, nil } -// callerImportMap returns a map from package paths in the caller's file to local names. -// The map excludes imports not needed by the caller or callee after inlining; the second -// return value holds these. -func callerImportMap(caller *Caller, callee *gobCallee) (map[string][]string, []oldImport) { - // For simplicity we ignore existing dot imports, so that a qualified - // identifier (QI) in the callee is always represented by a QI in the caller, - // allowing us to treat a QI like a selection on a package name. - importMap := make(map[string][]string) // maps package path to local name(s) - var oldImports []oldImport // imports referenced only by caller.Call.Fun - - for _, imp := range caller.File.Imports { - if pkgName, ok := importedPkgName(caller.Info, imp); ok && - pkgName.Name() != "." && - pkgName.Name() != "_" { - - // If the import's sole use is in caller.Call.Fun of the form p.F(...), - // where p.F is a qualified identifier, the p import may not be - // necessary. - // - // Only the qualified identifier case matters, as other references to - // imported package names in the Call.Fun expression (e.g. - // x.after(3*time.Second).f() or time.Second.String()) will remain after - // inlining, as arguments. - // - // If that is the case, proactively check if any of the callee FreeObjs - // need this import. Doing so eagerly simplifies the resulting logic. - needed := true - sel, ok := ast.Unparen(caller.Call.Fun).(*ast.SelectorExpr) - if ok && soleUse(caller.Info, pkgName) == sel.X { - needed = false // no longer needed by caller - // Check to see if any of the inlined free objects need this package. - for _, obj := range callee.FreeObjs { - if obj.PkgPath == pkgName.Imported().Path() && obj.Shadow[pkgName.Name()] == 0 { - needed = true // needed by callee - break - } - } - } - - if needed { - path := pkgName.Imported().Path() - importMap[path] = append(importMap[path], pkgName.Name()) - } else { - oldImports = append(oldImports, oldImport{pkgName: pkgName, spec: imp}) - } - } - } - return importMap, oldImports -} - type argument struct { expr ast.Expr typ types.Type // may be tuple for sole non-receiver arg in spread call From 8c42f8aeff807484822b4be1294b82696f8fc17e Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 26 Mar 2025 22:11:08 -0600 Subject: [PATCH 275/309] gopls/internal/analysis/modernize: use types.RelativeTo to respect current package This CL fixes a bug introduced in CL659155. It leverages types.RelativeTo to determine the package name of a variable, ensuring that the package name is not added for types declared within the same package. This prevents invalid type errors. Additionally, this CL introduces additional test cases. Fixes golang/go#73037 Change-Id: I6ad0c80c02ad1a679a956b93a53a05a8eb1ba9c2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/660815 LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan --- gopls/internal/analysis/modernize/rangeint.go | 2 +- .../testdata/src/rangeint/rangeint.go | 26 +++++++++++++++++++ .../testdata/src/rangeint/rangeint.go.golden | 26 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/gopls/internal/analysis/modernize/rangeint.go b/gopls/internal/analysis/modernize/rangeint.go index 4ca87e40aec..1d3f4b5db0c 100644 --- a/gopls/internal/analysis/modernize/rangeint.go +++ b/gopls/internal/analysis/modernize/rangeint.go @@ -184,7 +184,7 @@ func rangeint(pass *analysis.Pass) { // (Unfortunately types.Qualifier doesn't provide the name of the package // member to be qualified, a qualifier cannot perform the necessary shadowing // check for dot-imported names.) - beforeLimit, afterLimit = fmt.Sprintf("%s(", types.TypeString(tVar, (*types.Package).Name)), ")" + beforeLimit, afterLimit = fmt.Sprintf("%s(", types.TypeString(tVar, types.RelativeTo(pass.Pkg))), ")" info2 := &types.Info{Types: make(map[ast.Expr]types.TypeAndValue)} if types.CheckExpr(pass.Fset, pass.Pkg, limit.Pos(), limit, info2) == nil { tLimit := types.Default(info2.TypeOf(limit)) diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go index 7048fea1148..74f3488546c 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go @@ -205,3 +205,29 @@ func todo() { println(j) } } + +type T uint +type TAlias = uint + +func Fn(a int) T { + return T(a) +} + +func issue73037() { + var q T + for a := T(0); a < q; a++ { // want "for loop can be modernized using range over int" + println(a) + } + for a := Fn(0); a < q; a++ { + println(a) + } + var qa TAlias + for a := TAlias(0); a < qa; a++ { // want "for loop can be modernized using range over int" + println(a) + } + for a := T(0); a < 10; a++ { // want "for loop can be modernized using range over int" + for b := T(0); b < 10; b++ { // want "for loop can be modernized using range over int" + println(a, b) + } + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index 8c3fdc40b77..a21bd7e8607 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -205,3 +205,29 @@ func todo() { println(j) } } + +type T uint +type TAlias = uint + +func Fn(a int) T { + return T(a) +} + +func issue73037() { + var q T + for a := range q { // want "for loop can be modernized using range over int" + println(a) + } + for a := Fn(0); a < q; a++ { + println(a) + } + var qa TAlias + for a := range qa { // want "for loop can be modernized using range over int" + println(a) + } + for a := range T(10) { // want "for loop can be modernized using range over int" + for b := range T(10) { // want "for loop can be modernized using range over int" + println(a, b) + } + } +} From 48421ae1421f1b5c6f0ae26a4eeef04e9e4d094d Mon Sep 17 00:00:00 2001 From: acehinnnqru Date: Mon, 24 Mar 2025 01:22:28 +0800 Subject: [PATCH 276/309] gopls/internal/analysis/modernize: preserves comments in mapsloop This CL changes the original deletion to new deletion, - (from the start of mrhs to the end of the loop) to (from the start of the assigment to the end of the loop) - unchanged: (from the start of the loop to the end) to (from the start of the loop to the end) and put all comments between them on top of the new expression. This change preserves all comments between the mapsloop range, causing comments B,C,D to be preserved and put on the top of new exprs. Here shows an example of `maps.Copy` - source: m1 = make(map) m2 = make(map) // A for k, v := range m1 { // B m2[k] = b // C } - fixed: m1 = make(map) m2 = make(map) // A // B // C maps.Copy(m2, m1) Fixes: golang/go#72958 Change-Id: Id751c39151880504683c533f7b38599c6ab6e19e Reviewed-on: https://go-review.googlesource.com/c/tools/+/660255 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: Alan Donovan --- gopls/internal/analysis/modernize/maps.go | 27 +++++++++++++++--- gopls/internal/analysis/modernize/minmax.go | 17 +++++------ .../testdata/src/mapsloop/mapsloop.go | 14 +++++++++- .../testdata/src/mapsloop/mapsloop.go.golden | 28 +++++++++++++++++++ .../src/mapsloop/mapsloop_dot.go.golden | 2 ++ 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/gopls/internal/analysis/modernize/maps.go b/gopls/internal/analysis/modernize/maps.go index 5577978278c..1a5e2c3eeee 100644 --- a/gopls/internal/analysis/modernize/maps.go +++ b/gopls/internal/analysis/modernize/maps.go @@ -156,16 +156,35 @@ func mapsloop(pass *analysis.Pass) { start, end token.Pos ) if mrhs != nil { - // Replace RHS of preceding m=... assignment (and loop) with expression. - start, end = mrhs.Pos(), rng.End() - newText = fmt.Appendf(nil, "%s%s(%s)", + // Replace assignment and loop with expression. + // + // m = make(...) + // for k, v := range x { /* comments */ m[k] = v } + // + // -> + // + // /* comments */ + // m = maps.Copy(x) + curPrev, _ := curRange.PrevSibling() + start, end = curPrev.Node().Pos(), rng.End() + newText = fmt.Appendf(nil, "%s%s = %s%s(%s)", + allComments(file, start, end), + analysisinternal.Format(pass.Fset, m), prefix, funcName, analysisinternal.Format(pass.Fset, x)) } else { // Replace loop with call statement. + // + // for k, v := range x { /* comments */ m[k] = v } + // + // -> + // + // /* comments */ + // maps.Copy(m, x) start, end = rng.Pos(), rng.End() - newText = fmt.Appendf(nil, "%s%s(%s, %s)", + newText = fmt.Appendf(nil, "%s%s%s(%s, %s)", + allComments(file, start, end), prefix, funcName, analysisinternal.Format(pass.Fset, m), diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index a996f9bd56a..415e9fc5661 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -50,14 +50,6 @@ func minmax(pass *analysis.Pass) { sign = isInequality(compare.Op) ) - allComments := func(file *ast.File, start, end token.Pos) string { - var buf strings.Builder - for co := range analysisinternal.Comments(file, start, end) { - _, _ = fmt.Fprintf(&buf, "%s\n", co.Text) - } - return buf.String() - } - if fblock, ok := ifStmt.Else.(*ast.BlockStmt); ok && isAssignBlock(fblock) { fassign := fblock.List[0].(*ast.AssignStmt) @@ -196,6 +188,15 @@ func minmax(pass *analysis.Pass) { } } +// allComments collects all the comments from start to end. +func allComments(file *ast.File, start, end token.Pos) string { + var buf strings.Builder + for co := range analysisinternal.Comments(file, start, end) { + _, _ = fmt.Fprintf(&buf, "%s\n", co.Text) + } + return buf.String() +} + // isInequality reports non-zero if tok is one of < <= => >: // +1 for > and -1 for <. func isInequality(tok token.Token) int { diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go index 68ff9154ffd..7d0f7d17e91 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go @@ -16,6 +16,7 @@ type M map[int]string func useCopy(dst, src map[int]string) { // Replace loop by maps.Copy. for key, value := range src { + // A dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } } @@ -23,6 +24,7 @@ func useCopy(dst, src map[int]string) { func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { // Replace loop by maps.Copy. for key, value := range src { + // A dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } } @@ -32,12 +34,18 @@ func useCopyNotClone(src map[int]string) { // Replace make(...) by maps.Copy. dst := make(map[int]string, len(src)) + // A for key, value := range src { + // B dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" + // C } + // A dst = map[int]string{} + // B for key, value := range src { + // C dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Copy" } println(dst) @@ -126,8 +134,10 @@ func useInsert_assignableToSeq2(dst map[int]string, src func(yield func(int, str func useCollect(src iter.Seq2[int, string]) { // Replace loop and make(...) by maps.Collect. var dst map[int]string - dst = make(map[int]string) + dst = make(map[int]string) // A + // B for key, value := range src { + // C dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Collect" } } @@ -137,7 +147,9 @@ func useInsert_typesDifferAssign(src iter.Seq2[int, string]) { // that is assignable to M. var dst M dst = make(M) + // A for key, value := range src { + // B dst[key] = value // want "Replace m\\[k\\]=v loop with maps.Collect" } } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden index be189673d9a..9136105b908 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop.go.golden @@ -15,11 +15,15 @@ type M map[int]string func useCopy(dst, src map[int]string) { // Replace loop by maps.Copy. + // A + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) } func useCopyGeneric[K comparable, V any, M ~map[K]V](dst, src M) { // Replace loop by maps.Copy. + // A + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) } @@ -28,9 +32,17 @@ func useCopyNotClone(src map[int]string) { // Replace make(...) by maps.Copy. dst := make(map[int]string, len(src)) + // A + // B + // want "Replace m\\[k\\]=v loop with maps.Copy" + // C maps.Copy(dst, src) + // A dst = map[int]string{} + // B + // C + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -40,9 +52,11 @@ func useCopyParen(src map[int]string) { // Replace (make)(...) by maps.Clone. dst := (make)(map[int]string, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) dst = (map[int]string{}) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -50,6 +64,7 @@ func useCopyParen(src map[int]string) { func useCopy_typesDiffer(src M) { // Replace loop but not make(...) as maps.Copy(src) would return wrong type M. dst := make(map[int]string, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -57,6 +72,7 @@ func useCopy_typesDiffer(src M) { func useCopy_typesDiffer2(src map[int]string) { // Replace loop but not make(...) as maps.Copy(src) would return wrong type map[int]string. dst := make(M, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -68,6 +84,7 @@ func useClone_typesDiffer3(src map[int]string) { // which is assignable to M. var dst M dst = make(M, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -79,6 +96,7 @@ func useClone_typesDiffer4(src map[int]string) { // which is assignable to M. var dst M dst = make(M, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -88,6 +106,7 @@ func useClone_generic[Map ~map[K]V, K comparable, V any](src Map) { // Replace loop and make(...) by maps.Clone dst := make(Map, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" maps.Copy(dst, src) println(dst) } @@ -96,12 +115,17 @@ func useClone_generic[Map ~map[K]V, K comparable, V any](src Map) { func useInsert_assignableToSeq2(dst map[int]string, src func(yield func(int, string) bool)) { // Replace loop by maps.Insert because src is assignable to iter.Seq2. + // want "Replace m\\[k\\]=v loop with maps.Insert" maps.Insert(dst, src) } func useCollect(src iter.Seq2[int, string]) { // Replace loop and make(...) by maps.Collect. var dst map[int]string + // A + // B + // C + // want "Replace m\\[k\\]=v loop with maps.Collect" dst = maps.Collect(src) } @@ -109,6 +133,9 @@ func useInsert_typesDifferAssign(src iter.Seq2[int, string]) { // Replace loop and make(...): maps.Collect returns an unnamed map type // that is assignable to M. var dst M + // A + // B + // want "Replace m\\[k\\]=v loop with maps.Collect" dst = maps.Collect(src) } @@ -116,6 +143,7 @@ func useInsert_typesDifferDeclare(src iter.Seq2[int, string]) { // Replace loop but not make(...) as maps.Collect would return an // unnamed map type that would change the type of dst. dst := make(M) + // want "Replace m\\[k\\]=v loop with maps.Insert" maps.Insert(dst, src) } diff --git a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden index e992314cf56..6347d56360a 100644 --- a/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/mapsloop/mapsloop_dot.go.golden @@ -8,6 +8,7 @@ var _ = Clone[M] // force "maps" import so that each diagnostic doesn't add one func useCopyDot(dst, src map[int]string) { // Replace loop by maps.Copy. + // want "Replace m\\[k\\]=v loop with maps.Copy" Copy(dst, src) } @@ -16,6 +17,7 @@ func useCloneDot(src map[int]string) { // Replace make(...) by maps.Copy. dst := make(map[int]string, len(src)) + // want "Replace m\\[k\\]=v loop with maps.Copy" Copy(dst, src) println(dst) } From 07cbcde02556290809fe12e096943d8d751dbaab Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 26 Mar 2025 17:32:00 -0400 Subject: [PATCH 277/309] gopls/internal/cmd: suppress TestImplementation on go1.23 The types.CheckExpr data race (#71817), fixed at master and backported to go1.24, is not backported to go1.23. The race itself is benign, but it causes this one test to flake. So we suppress it on go1.23. Fixes golang/go#72082 Change-Id: I64731adc50137aefbbecc0b7a47a41036d831eab Reviewed-on: https://go-review.googlesource.com/c/tools/+/661176 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Griesemer --- gopls/internal/cmd/integration_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index e7ac774f5c0..9d135ceadb2 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -508,6 +508,14 @@ func f() { func TestImplementations(t *testing.T) { t.Parallel() + // types.CheckExpr, now used in the rangeint modernizer, had a + // data race (#71817) that was fixed in go1.25 and backported + // to go1.24 but not to go1.23. Although in principle it could + // affect a lot of tests, it (weirdly) only seems to show up + // in this one (#72082). Rather than backport again, we + // suppress this test. + testenv.NeedsGo1Point(t, 24) + tree := writeTree(t, ` -- a.go -- package a From 1b0b68818a49feca5d1a8a204e8e11d75c71e117 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Thu, 27 Mar 2025 19:51:54 +0800 Subject: [PATCH 278/309] gopls: fix indent issue and track a TODO Change-Id: I634a7f8c5bf1e32be8d9bc0a6620c992f8aa01ce Reviewed-on: https://go-review.googlesource.com/c/tools/+/661216 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Dmitri Shuralyov --- .../testdata/src/rangeint/rangeint.go.golden | 12 +- gopls/internal/telemetry/telemetry_test.go | 4 +- .../test/integration/bench/completion_test.go | 2 +- .../test/integration/bench/repo_test.go | 4 +- gopls/internal/test/integration/env.go | 8 +- .../internal/test/integration/expectation.go | 2 +- .../test/integration/misc/highlight_test.go | 2 +- .../integration/misc/workspace_symbol_test.go | 6 +- gopls/internal/test/integration/runner.go | 2 +- gopls/internal/test/integration/wrappers.go | 224 +++++++++--------- gopls/internal/test/marker/marker_test.go | 20 +- 11 files changed, 143 insertions(+), 143 deletions(-) diff --git a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden index a21bd7e8607..cdd2f118997 100644 --- a/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/rangeint/rangeint.go.golden @@ -214,17 +214,17 @@ func Fn(a int) T { } func issue73037() { - var q T + var q T for a := range q { // want "for loop can be modernized using range over int" - println(a) + println(a) } for a := Fn(0); a < q; a++ { println(a) } - var qa TAlias - for a := range qa { // want "for loop can be modernized using range over int" - println(a) - } + var qa TAlias + for a := range qa { // want "for loop can be modernized using range over int" + println(a) + } for a := range T(10) { // want "for loop can be modernized using range over int" for b := range T(10) { // want "for loop can be modernized using range over int" println(a, b) diff --git a/gopls/internal/telemetry/telemetry_test.go b/gopls/internal/telemetry/telemetry_test.go index 4c41cc40dc9..1e56012182f 100644 --- a/gopls/internal/telemetry/telemetry_test.go +++ b/gopls/internal/telemetry/telemetry_test.go @@ -168,7 +168,7 @@ func addForwardedCounters(env *Env, names []string, values []int64) { Names: names, Values: values, }) if err != nil { - env.T.Fatal(err) + env.TB.Fatal(err) } var res error env.ExecuteCommand(&protocol.ExecuteCommandParams{ @@ -176,7 +176,7 @@ func addForwardedCounters(env *Env, names []string, values []int64) { Arguments: args, }, &res) if res != nil { - env.T.Errorf("%v failed - %v", command.AddTelemetryCounters, res) + env.TB.Errorf("%v failed - %v", command.AddTelemetryCounters, res) } } diff --git a/gopls/internal/test/integration/bench/completion_test.go b/gopls/internal/test/integration/bench/completion_test.go index d84512d1f8f..48ecf0cefd6 100644 --- a/gopls/internal/test/integration/bench/completion_test.go +++ b/gopls/internal/test/integration/bench/completion_test.go @@ -69,7 +69,7 @@ func endRangeInBuffer(env *Env, name string) protocol.Range { m := protocol.NewMapper("", []byte(buffer)) rng, err := m.OffsetRange(len(buffer), len(buffer)) if err != nil { - env.T.Fatal(err) + env.TB.Fatal(err) } return rng } diff --git a/gopls/internal/test/integration/bench/repo_test.go b/gopls/internal/test/integration/bench/repo_test.go index 50370e73491..65728c00552 100644 --- a/gopls/internal/test/integration/bench/repo_test.go +++ b/gopls/internal/test/integration/bench/repo_test.go @@ -211,7 +211,7 @@ func (r *repo) sharedEnv(tb testing.TB) *Env { }) return &Env{ - T: tb, + TB: tb, Ctx: context.Background(), Editor: r.editor, Sandbox: r.sandbox, @@ -238,7 +238,7 @@ func (r *repo) newEnv(tb testing.TB, config fake.EditorConfig, forOperation stri } return &Env{ - T: tb, + TB: tb, Ctx: context.Background(), Editor: editor, Sandbox: sandbox, diff --git a/gopls/internal/test/integration/env.go b/gopls/internal/test/integration/env.go index f19a426316d..822120e8324 100644 --- a/gopls/internal/test/integration/env.go +++ b/gopls/internal/test/integration/env.go @@ -21,7 +21,7 @@ import ( // wrapper methods that hide the boilerplate of plumbing contexts and checking // errors. type Env struct { - T testing.TB // TODO(rfindley): rename to TB + TB testing.TB Ctx context.Context // Most tests should not need to access the scratch area, editor, server, or @@ -311,9 +311,9 @@ func (a *Awaiter) checkConditionsLocked() { // Use AfterChange or OnceMet instead, so that the runner knows when to stop // waiting. func (e *Env) Await(expectations ...Expectation) { - e.T.Helper() + e.TB.Helper() if err := e.Awaiter.Await(e.Ctx, AllOf(expectations...)); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -321,7 +321,7 @@ func (e *Env) Await(expectations ...Expectation) { // unmeetable. If it was met, OnceMet checks that the state meets all // expectations in mustMeets. func (e *Env) OnceMet(pre Expectation, mustMeets ...Expectation) { - e.T.Helper() + e.TB.Helper() e.Await(OnceMet(pre, AllOf(mustMeets...))) } diff --git a/gopls/internal/test/integration/expectation.go b/gopls/internal/test/integration/expectation.go index 70a16fd6b3a..98554ddccc3 100644 --- a/gopls/internal/test/integration/expectation.go +++ b/gopls/internal/test/integration/expectation.go @@ -352,7 +352,7 @@ func (e *Env) DoneDiagnosingChanges() Expectation { // - workspace/didChangeWatchedFiles // - workspace/didChangeConfiguration func (e *Env) AfterChange(expectations ...Expectation) { - e.T.Helper() + e.TB.Helper() e.OnceMet( e.DoneDiagnosingChanges(), expectations..., diff --git a/gopls/internal/test/integration/misc/highlight_test.go b/gopls/internal/test/integration/misc/highlight_test.go index e4da558e5d0..36bddf25057 100644 --- a/gopls/internal/test/integration/misc/highlight_test.go +++ b/gopls/internal/test/integration/misc/highlight_test.go @@ -124,7 +124,7 @@ func main() {}` } func checkHighlights(env *Env, loc protocol.Location, highlightCount int) { - t := env.T + t := env.TB t.Helper() highlights := env.DocumentHighlight(loc) diff --git a/gopls/internal/test/integration/misc/workspace_symbol_test.go b/gopls/internal/test/integration/misc/workspace_symbol_test.go index 9420b146d85..f1148539447 100644 --- a/gopls/internal/test/integration/misc/workspace_symbol_test.go +++ b/gopls/internal/test/integration/misc/workspace_symbol_test.go @@ -8,8 +8,8 @@ import ( "testing" "github.com/google/go-cmp/cmp" - . "golang.org/x/tools/gopls/internal/test/integration" "golang.org/x/tools/gopls/internal/settings" + . "golang.org/x/tools/gopls/internal/test/integration" ) func TestWorkspaceSymbolMissingMetadata(t *testing.T) { @@ -103,12 +103,12 @@ const ( } func checkSymbols(env *Env, query string, want ...string) { - env.T.Helper() + env.TB.Helper() var got []string for _, info := range env.Symbol(query) { got = append(got, info.Name) } if diff := cmp.Diff(got, want); diff != "" { - env.T.Errorf("unexpected Symbol(%q) result (+want -got):\n%s", query, diff) + env.TB.Errorf("unexpected Symbol(%q) result (+want -got):\n%s", query, diff) } } diff --git a/gopls/internal/test/integration/runner.go b/gopls/internal/test/integration/runner.go index b4b9d3a2a4d..c4609cb8f91 100644 --- a/gopls/internal/test/integration/runner.go +++ b/gopls/internal/test/integration/runner.go @@ -253,7 +253,7 @@ func ConnectGoplsEnv(t testing.TB, ctx context.Context, sandbox *fake.Sandbox, c t.Fatal(err) } env := &Env{ - T: t, + TB: t, Ctx: ctx, Sandbox: sandbox, Server: connector, diff --git a/gopls/internal/test/integration/wrappers.go b/gopls/internal/test/integration/wrappers.go index 989ae913acf..6389cdb74e8 100644 --- a/gopls/internal/test/integration/wrappers.go +++ b/gopls/internal/test/integration/wrappers.go @@ -18,19 +18,19 @@ import ( // RemoveWorkspaceFile deletes a file on disk but does nothing in the // editor. It calls t.Fatal on any error. func (e *Env) RemoveWorkspaceFile(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Sandbox.Workdir.RemoveFile(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // ReadWorkspaceFile reads a file from the workspace, calling t.Fatal on any // error. func (e *Env) ReadWorkspaceFile(name string) string { - e.T.Helper() + e.TB.Helper() content, err := e.Sandbox.Workdir.ReadFile(name) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return string(content) } @@ -38,55 +38,55 @@ func (e *Env) ReadWorkspaceFile(name string) string { // WriteWorkspaceFile writes a file to disk but does nothing in the editor. // It calls t.Fatal on any error. func (e *Env) WriteWorkspaceFile(name, content string) { - e.T.Helper() + e.TB.Helper() if err := e.Sandbox.Workdir.WriteFile(e.Ctx, name, content); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // WriteWorkspaceFiles deletes a file on disk but does nothing in the // editor. It calls t.Fatal on any error. func (e *Env) WriteWorkspaceFiles(files map[string]string) { - e.T.Helper() + e.TB.Helper() if err := e.Sandbox.Workdir.WriteFiles(e.Ctx, files); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // ListFiles lists relative paths to files in the given directory. // It calls t.Fatal on any error. func (e *Env) ListFiles(dir string) []string { - e.T.Helper() + e.TB.Helper() paths, err := e.Sandbox.Workdir.ListFiles(dir) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return paths } // OpenFile opens a file in the editor, calling t.Fatal on any error. func (e *Env) OpenFile(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.OpenFile(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // CreateBuffer creates a buffer in the editor, calling t.Fatal on any error. func (e *Env) CreateBuffer(name string, content string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.CreateBuffer(e.Ctx, name, content); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // BufferText returns the current buffer contents for the file with the given // relative path, calling t.Fatal if the file is not open in a buffer. func (e *Env) BufferText(name string) string { - e.T.Helper() + e.TB.Helper() text, ok := e.Editor.BufferText(name) if !ok { - e.T.Fatalf("buffer %q is not open", name) + e.TB.Fatalf("buffer %q is not open", name) } return text } @@ -94,24 +94,24 @@ func (e *Env) BufferText(name string) string { // CloseBuffer closes an editor buffer without saving, calling t.Fatal on any // error. func (e *Env) CloseBuffer(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.CloseBuffer(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // EditBuffer applies edits to an editor buffer, calling t.Fatal on any error. func (e *Env) EditBuffer(name string, edits ...protocol.TextEdit) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.EditBuffer(e.Ctx, name, edits); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } func (e *Env) SetBufferContent(name string, content string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.SetBufferContent(e.Ctx, name, content); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -119,7 +119,7 @@ func (e *Env) SetBufferContent(name string, content string) { // editing session: it returns the buffer content for an open file, the // on-disk content for an unopened file, or "" for a non-existent file. func (e *Env) FileContent(name string) string { - e.T.Helper() + e.TB.Helper() text, ok := e.Editor.BufferText(name) if ok { return text @@ -129,7 +129,7 @@ func (e *Env) FileContent(name string) string { if errors.Is(err, os.ErrNotExist) { return "" } else { - e.T.Fatal(err) + e.TB.Fatal(err) } } return string(content) @@ -138,14 +138,14 @@ func (e *Env) FileContent(name string) string { // FileContentAt returns the file content at the given location, using the // file's mapper. func (e *Env) FileContentAt(location protocol.Location) string { - e.T.Helper() + e.TB.Helper() mapper, err := e.Editor.Mapper(location.URI.Path()) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } start, end, err := mapper.RangeOffsets(location.Range) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return string(mapper.Content[start:end]) } @@ -154,13 +154,13 @@ func (e *Env) FileContentAt(location protocol.Location) string { // buffer specified by name, calling t.Fatal on any error. It first searches // for the position in open buffers, then in workspace files. func (e *Env) RegexpSearch(name, re string) protocol.Location { - e.T.Helper() + e.TB.Helper() loc, err := e.Editor.RegexpSearch(name, re) if err == fake.ErrUnknownBuffer { loc, err = e.Sandbox.Workdir.RegexpSearch(name, re) } if err != nil { - e.T.Fatalf("RegexpSearch: %v, %v for %q", name, err, re) + e.TB.Fatalf("RegexpSearch: %v, %v for %q", name, err, re) } return loc } @@ -168,24 +168,24 @@ func (e *Env) RegexpSearch(name, re string) protocol.Location { // RegexpReplace replaces the first group in the first match of regexpStr with // the replace text, calling t.Fatal on any error. func (e *Env) RegexpReplace(name, regexpStr, replace string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.RegexpReplace(e.Ctx, name, regexpStr, replace); err != nil { - e.T.Fatalf("RegexpReplace: %v", err) + e.TB.Fatalf("RegexpReplace: %v", err) } } // SaveBuffer saves an editor buffer, calling t.Fatal on any error. func (e *Env) SaveBuffer(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.SaveBuffer(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } func (e *Env) SaveBufferWithoutActions(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.SaveBufferWithoutActions(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -194,64 +194,64 @@ func (e *Env) SaveBufferWithoutActions(name string) { // // TODO(rfindley): rename this to just 'Definition'. func (e *Env) GoToDefinition(loc protocol.Location) protocol.Location { - e.T.Helper() + e.TB.Helper() loc, err := e.Editor.Definition(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return loc } func (e *Env) TypeDefinition(loc protocol.Location) protocol.Location { - e.T.Helper() + e.TB.Helper() loc, err := e.Editor.TypeDefinition(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return loc } // FormatBuffer formats the editor buffer, calling t.Fatal on any error. func (e *Env) FormatBuffer(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.FormatBuffer(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // OrganizeImports processes the source.organizeImports codeAction, calling // t.Fatal on any error. func (e *Env) OrganizeImports(name string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.OrganizeImports(e.Ctx, name); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error. func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) { - e.T.Helper() + e.TB.Helper() loc := e.Sandbox.Workdir.EntireFile(path) if err := e.Editor.ApplyQuickFixes(e.Ctx, loc, diagnostics); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // ApplyCodeAction applies the given code action, calling t.Fatal on any error. func (e *Env) ApplyCodeAction(action protocol.CodeAction) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.ApplyCodeAction(e.Ctx, action); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // Diagnostics returns diagnostics for the given file, calling t.Fatal on any // error. func (e *Env) Diagnostics(name string) []protocol.Diagnostic { - e.T.Helper() + e.TB.Helper() diags, err := e.Editor.Diagnostics(e.Ctx, name) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return diags } @@ -259,11 +259,11 @@ func (e *Env) Diagnostics(name string) []protocol.Diagnostic { // GetQuickFixes returns the available quick fix code actions, calling t.Fatal // on any error. func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []protocol.CodeAction { - e.T.Helper() + e.TB.Helper() loc := e.Sandbox.Workdir.EntireFile(path) actions, err := e.Editor.GetQuickFixes(e.Ctx, loc, diagnostics) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return actions } @@ -271,28 +271,28 @@ func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []pr // Hover in the editor, calling t.Fatal on any error. // It may return (nil, zero) even on success. func (e *Env) Hover(loc protocol.Location) (*protocol.MarkupContent, protocol.Location) { - e.T.Helper() + e.TB.Helper() c, loc, err := e.Editor.Hover(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return c, loc } func (e *Env) DocumentLink(name string) []protocol.DocumentLink { - e.T.Helper() + e.TB.Helper() links, err := e.Editor.DocumentLink(e.Ctx, name) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return links } func (e *Env) DocumentHighlight(loc protocol.Location) []protocol.DocumentHighlight { - e.T.Helper() + e.TB.Helper() highlights, err := e.Editor.DocumentHighlight(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return highlights } @@ -301,9 +301,9 @@ func (e *Env) DocumentHighlight(loc protocol.Location) []protocol.DocumentHighli // It waits for the generate command to complete and checks for file changes // before returning. func (e *Env) RunGenerate(dir string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.RunGenerate(e.Ctx, dir); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } e.Await(NoOutstandingWork(IgnoreTelemetryPromptWork)) // Ideally the editor.Workspace would handle all synthetic file watching, but @@ -315,10 +315,10 @@ func (e *Env) RunGenerate(dir string) { // RunGoCommand runs the given command in the sandbox's default working // directory. func (e *Env) RunGoCommand(verb string, args ...string) []byte { - e.T.Helper() + e.TB.Helper() out, err := e.Sandbox.RunGoCommand(e.Ctx, "", verb, args, nil, true) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return out } @@ -326,28 +326,28 @@ func (e *Env) RunGoCommand(verb string, args ...string) []byte { // RunGoCommandInDir is like RunGoCommand, but executes in the given // relative directory of the sandbox. func (e *Env) RunGoCommandInDir(dir, verb string, args ...string) { - e.T.Helper() + e.TB.Helper() if _, err := e.Sandbox.RunGoCommand(e.Ctx, dir, verb, args, nil, true); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // RunGoCommandInDirWithEnv is like RunGoCommand, but executes in the given // relative directory of the sandbox with the given additional environment variables. func (e *Env) RunGoCommandInDirWithEnv(dir string, env []string, verb string, args ...string) { - e.T.Helper() + e.TB.Helper() if _, err := e.Sandbox.RunGoCommand(e.Ctx, dir, verb, args, env, true); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // GoVersion checks the version of the go command. // It returns the X in Go 1.X. func (e *Env) GoVersion() int { - e.T.Helper() + e.TB.Helper() v, err := e.Sandbox.GoVersion(e.Ctx) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return v } @@ -355,33 +355,33 @@ func (e *Env) GoVersion() int { // DumpGoSum prints the correct go.sum contents for dir in txtar format, // for use in creating integration tests. func (e *Env) DumpGoSum(dir string) { - e.T.Helper() + e.TB.Helper() if _, err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "./..."}, nil, true); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } sumFile := path.Join(dir, "go.sum") - e.T.Log("\n\n-- " + sumFile + " --\n" + e.ReadWorkspaceFile(sumFile)) - e.T.Fatal("see contents above") + e.TB.Log("\n\n-- " + sumFile + " --\n" + e.ReadWorkspaceFile(sumFile)) + e.TB.Fatal("see contents above") } // CheckForFileChanges triggers a manual poll of the workspace for any file // changes since creation, or since last polling. It is a workaround for the // lack of true file watching support in the fake workspace. func (e *Env) CheckForFileChanges() { - e.T.Helper() + e.TB.Helper() if err := e.Sandbox.Workdir.CheckForFileChanges(e.Ctx); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // CodeLens calls textDocument/codeLens for the given path, calling t.Fatal on // any error. func (e *Env) CodeLens(path string) []protocol.CodeLens { - e.T.Helper() + e.TB.Helper() lens, err := e.Editor.CodeLens(e.Ctx, path) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return lens } @@ -391,9 +391,9 @@ func (e *Env) CodeLens(path string) []protocol.CodeLens { // // result is a pointer to a variable to be populated by json.Unmarshal. func (e *Env) ExecuteCodeLensCommand(path string, cmd command.Command, result any) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.ExecuteCodeLensCommand(e.Ctx, path, cmd, result); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -402,9 +402,9 @@ func (e *Env) ExecuteCodeLensCommand(path string, cmd command.Command, result an // // result is a pointer to a variable to be populated by json.Unmarshal. func (e *Env) ExecuteCommand(params *protocol.ExecuteCommandParams, result any) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.ExecuteCommand(e.Ctx, params, result); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -430,7 +430,7 @@ func (e *Env) StartProfile() (stop func() string) { // This would be a lot simpler if we generated params constructors. args, err := command.MarshalArgs(command.StartProfileArgs{}) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } params := &protocol.ExecuteCommandParams{ Command: command.StartProfile.String(), @@ -442,7 +442,7 @@ func (e *Env) StartProfile() (stop func() string) { return func() string { stopArgs, err := command.MarshalArgs(command.StopProfileArgs{}) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } stopParams := &protocol.ExecuteCommandParams{ Command: command.StopProfile.String(), @@ -457,91 +457,91 @@ func (e *Env) StartProfile() (stop func() string) { // InlayHints calls textDocument/inlayHints for the given path, calling t.Fatal on // any error. func (e *Env) InlayHints(path string) []protocol.InlayHint { - e.T.Helper() + e.TB.Helper() hints, err := e.Editor.InlayHint(e.Ctx, path) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return hints } // Symbol calls workspace/symbol func (e *Env) Symbol(query string) []protocol.SymbolInformation { - e.T.Helper() + e.TB.Helper() ans, err := e.Editor.Symbols(e.Ctx, query) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return ans } // References wraps Editor.References, calling t.Fatal on any error. func (e *Env) References(loc protocol.Location) []protocol.Location { - e.T.Helper() + e.TB.Helper() locations, err := e.Editor.References(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return locations } // Rename wraps Editor.Rename, calling t.Fatal on any error. func (e *Env) Rename(loc protocol.Location, newName string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.Rename(e.Ctx, loc, newName); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // Implementations wraps Editor.Implementations, calling t.Fatal on any error. func (e *Env) Implementations(loc protocol.Location) []protocol.Location { - e.T.Helper() + e.TB.Helper() locations, err := e.Editor.Implementations(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return locations } // RenameFile wraps Editor.RenameFile, calling t.Fatal on any error. func (e *Env) RenameFile(oldPath, newPath string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.RenameFile(e.Ctx, oldPath, newPath); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // SignatureHelp wraps Editor.SignatureHelp, calling t.Fatal on error func (e *Env) SignatureHelp(loc protocol.Location) *protocol.SignatureHelp { - e.T.Helper() + e.TB.Helper() sighelp, err := e.Editor.SignatureHelp(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return sighelp } // Completion executes a completion request on the server. func (e *Env) Completion(loc protocol.Location) *protocol.CompletionList { - e.T.Helper() + e.TB.Helper() completions, err := e.Editor.Completion(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return completions } func (e *Env) SetSuggestionInsertReplaceMode(useReplaceMode bool) { - e.T.Helper() + e.TB.Helper() e.Editor.SetSuggestionInsertReplaceMode(e.Ctx, useReplaceMode) } // AcceptCompletion accepts a completion for the given item at the given // position. func (e *Env) AcceptCompletion(loc protocol.Location, item protocol.CompletionItem) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.AcceptCompletion(e.Ctx, loc, item); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } @@ -554,38 +554,38 @@ func (e *Env) CodeActionForFile(path string, diagnostics []protocol.Diagnostic) // CodeAction calls textDocument/codeAction for a selection, // and calls t.Fatal if there were errors. func (e *Env) CodeAction(loc protocol.Location, diagnostics []protocol.Diagnostic, trigger protocol.CodeActionTriggerKind) []protocol.CodeAction { - e.T.Helper() + e.TB.Helper() actions, err := e.Editor.CodeAction(e.Ctx, loc, diagnostics, trigger) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return actions } // ChangeConfiguration updates the editor config, calling t.Fatal on any error. func (e *Env) ChangeConfiguration(newConfig fake.EditorConfig) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.ChangeConfiguration(e.Ctx, newConfig); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // ChangeWorkspaceFolders updates the editor workspace folders, calling t.Fatal // on any error. func (e *Env) ChangeWorkspaceFolders(newFolders ...string) { - e.T.Helper() + e.TB.Helper() if err := e.Editor.ChangeWorkspaceFolders(e.Ctx, newFolders); err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } } // SemanticTokensFull invokes textDocument/semanticTokens/full, calling t.Fatal // on any error. func (e *Env) SemanticTokensFull(path string) []fake.SemanticToken { - e.T.Helper() + e.TB.Helper() toks, err := e.Editor.SemanticTokensFull(e.Ctx, path) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return toks } @@ -593,10 +593,10 @@ func (e *Env) SemanticTokensFull(path string) []fake.SemanticToken { // SemanticTokensRange invokes textDocument/semanticTokens/range, calling t.Fatal // on any error. func (e *Env) SemanticTokensRange(loc protocol.Location) []fake.SemanticToken { - e.T.Helper() + e.TB.Helper() toks, err := e.Editor.SemanticTokensRange(e.Ctx, loc) if err != nil { - e.T.Fatal(err) + e.TB.Fatal(err) } return toks } @@ -606,9 +606,9 @@ func (e *Env) SemanticTokensRange(loc protocol.Location) []fake.SemanticToken { func (e *Env) Close() { ctx := xcontext.Detach(e.Ctx) if err := e.Editor.Close(ctx); err != nil { - e.T.Errorf("closing editor: %v", err) + e.TB.Errorf("closing editor: %v", err) } if err := e.Sandbox.Close(); err != nil { - e.T.Errorf("cleaning up sandbox: %v", err) + e.TB.Errorf("cleaning up sandbox: %v", err) } } diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index 3ff7da65ac5..8c27adc9018 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -321,7 +321,7 @@ type marker struct { func (m marker) ctx() context.Context { return m.run.env.Ctx } // T returns the testing.TB for this mark. -func (m marker) T() testing.TB { return m.run.env.T } +func (m marker) T() testing.TB { return m.run.env.TB } // server returns the LSP server for the marker test run. func (m marker) editor() *fake.Editor { return m.run.env.Editor } @@ -982,7 +982,7 @@ func newEnv(t *testing.T, cache *cache.Cache, files, proxyFiles map[string][]byt t.Fatal(err) } return &integration.Env{ - T: t, + TB: t, Ctx: ctx, Editor: editor, Sandbox: sandbox, @@ -1035,17 +1035,17 @@ func (c *marker) sprintf(format string, args ...any) string { func (run *markerTestRun) fmtPos(pos token.Pos) string { file := run.test.fset.File(pos) if file == nil { - run.env.T.Errorf("position %d not in test fileset", pos) + run.env.TB.Errorf("position %d not in test fileset", pos) return "" } m, err := run.env.Editor.Mapper(file.Name()) if err != nil { - run.env.T.Errorf("%s", err) + run.env.TB.Errorf("%s", err) return "" } loc, err := m.PosLocation(file, pos, pos) if err != nil { - run.env.T.Errorf("Mapper(%s).PosLocation failed: %v", file.Name(), err) + run.env.TB.Errorf("Mapper(%s).PosLocation failed: %v", file.Name(), err) } return run.fmtLoc(loc) } @@ -1055,7 +1055,7 @@ func (run *markerTestRun) fmtPos(pos token.Pos) string { // archive file. func (run *markerTestRun) fmtLoc(loc protocol.Location) string { if loc == (protocol.Location{}) { - run.env.T.Errorf("unable to find %s in test archive", loc) + run.env.TB.Errorf("unable to find %s in test archive", loc) return "" } lines := bytes.Count(run.test.archive.Comment, []byte("\n")) @@ -1094,12 +1094,12 @@ func (run *markerTestRun) mapLocation(loc protocol.Location) (name string, start name = run.env.Sandbox.Workdir.URIToPath(loc.URI) m, err := run.env.Editor.Mapper(name) if err != nil { - run.env.T.Errorf("internal error: %v", err) + run.env.TB.Errorf("internal error: %v", err) return } start, end, err := m.RangeOffsets(loc.Range) if err != nil { - run.env.T.Errorf("error formatting location %s: %v", loc, err) + run.env.TB.Errorf("error formatting location %s: %v", loc, err) return } startLine, startCol = m.OffsetLineCol8(start) @@ -2306,11 +2306,11 @@ func codeActionChanges(env *integration.Env, uri protocol.DocumentURI, rng proto if action.Edit != nil { if len(action.Edit.Changes) > 0 { - env.T.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Edit.Changes", action.Kind, action.Title) + env.TB.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Edit.Changes", action.Kind, action.Title) } if action.Edit.DocumentChanges != nil { if action.Command != nil { - env.T.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Command", action.Kind, action.Title) + env.TB.Errorf("internal error: discarding unexpected CodeAction{Kind=%s, Title=%q}.Command", action.Kind, action.Title) } return action.Edit.DocumentChanges, nil } From eb75b19426d3efd2bd643265b1094772288b35c0 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 27 Mar 2025 08:57:51 -0400 Subject: [PATCH 279/309] internal/refactor/inline: modernize Apply simple modernizations. Change-Id: Ie40a5989f3b414c189ad675faf16eae93da0eff5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/661295 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/callee.go | 15 +++++++-------- internal/refactor/inline/calleefx.go | 2 +- internal/refactor/inline/inline.go | 24 +++++++----------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/internal/refactor/inline/callee.go b/internal/refactor/inline/callee.go index b4ec43d551c..ca9426a2656 100644 --- a/internal/refactor/inline/callee.go +++ b/internal/refactor/inline/callee.go @@ -14,6 +14,7 @@ import ( "go/parser" "go/token" "go/types" + "slices" "strings" "golang.org/x/tools/go/types/typeutil" @@ -303,7 +304,7 @@ func AnalyzeCallee(logf func(string, ...any), fset *token.FileSet, pkg *types.Pa return nil, tuple.At(i).Type() } } - for i := 0; i < sig.Results().Len(); i++ { + for i := range sig.Results().Len() { expr, typ := argInfo(i) var flags returnOperandFlags if typ == types.Typ[types.UntypedNil] { // untyped nil is preserved by go/types @@ -572,11 +573,9 @@ func analyzeAssignment(info *types.Info, stack []ast.Node) (assignable, ifaceAss // Types do not need to match for an initializer with known type. if spec, ok := parent.(*ast.ValueSpec); ok && spec.Type != nil { - for _, v := range spec.Values { - if v == expr { - typ := info.TypeOf(spec.Type) - return true, typ == nil || types.IsInterface(typ), false - } + if slices.Contains(spec.Values, expr) { + typ := info.TypeOf(spec.Type) + return true, typ == nil || types.IsInterface(typ), false } } @@ -616,7 +615,7 @@ func analyzeAssignment(info *types.Info, stack []ast.Node) (assignable, ifaceAss return true, types.IsInterface(under.Elem()), false case *types.Struct: // Struct{k: expr} if id, _ := kv.Key.(*ast.Ident); id != nil { - for fi := 0; fi < under.NumFields(); fi++ { + for fi := range under.NumFields() { field := under.Field(fi) if info.Uses[id] == field { return true, types.IsInterface(field.Type()), false @@ -715,7 +714,7 @@ func paramTypeAtIndex(sig *types.Signature, call *ast.CallExpr, index int) types // given outer-to-inner stack, after stripping parentheses, along with the // remaining stack up to the parent node. // -// If no such context exists, returns (nil, nil). +// If no such context exists, returns (nil, nil, nil). func exprContext(stack []ast.Node) (remaining []ast.Node, parent ast.Node, expr ast.Expr) { expr, _ = stack[len(stack)-1].(ast.Expr) if expr == nil { diff --git a/internal/refactor/inline/calleefx.go b/internal/refactor/inline/calleefx.go index 11246e5b969..26dc02c010b 100644 --- a/internal/refactor/inline/calleefx.go +++ b/internal/refactor/inline/calleefx.go @@ -31,7 +31,7 @@ const ( // } // // is [1 0 -2 2], indicating reads of y and x, followed by the unknown -// effects of the g() call. and finally the read of parameter z. This +// effects of the g() call, and finally the read of parameter z. This // information is used during inlining to ascertain when it is safe // for parameter references to be replaced by their corresponding // argument expressions. Such substitutions are permitted only when diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index d89a62972c6..127a70c680b 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -534,7 +534,7 @@ func newImportState(logf func(string, ...any), caller *Caller, callee *gobCallee // importName finds an existing import name to use in a particular shadowing // context. It is used to determine the set of new imports in -// getOrMakeImportName, and is also used for writing out names in inlining +// localName, and is also used for writing out names in inlining // strategies below. func (i *importState) importName(pkgPath string, shadow shadowMap) string { for _, name := range i.importMap[pkgPath] { @@ -560,12 +560,7 @@ func (i *importState) localName(pkgPath, pkgName string, shadow shadowMap) strin } newlyAdded := func(name string) bool { - for _, new := range i.newImports { - if new.pkgName == name { - return true - } - } - return false + return slices.ContainsFunc(i.newImports, func(n newImport) bool { return n.pkgName == name }) } // shadowedInCaller reports whether a candidate package name @@ -576,12 +571,7 @@ func (i *importState) localName(pkgPath, pkgName string, shadow shadowMap) strin return false } // If obj will be removed, the name is available. - for _, old := range i.oldImports { - if old.pkgName == obj { - return false - } - } - return true + return !slices.ContainsFunc(i.oldImports, func(o oldImport) bool { return o.pkgName == obj }) } // import added by callee @@ -3030,13 +3020,13 @@ func replaceNode(root ast.Node, from, to ast.Node) { } case reflect.Struct: - for i := 0; i < v.Type().NumField(); i++ { + for i := range v.Type().NumField() { visit(v.Field(i)) } case reflect.Slice: compact := false - for i := 0; i < v.Len(); i++ { + for i := range v.Len() { visit(v.Index(i)) if v.Index(i).IsNil() { compact = true @@ -3047,7 +3037,7 @@ func replaceNode(root ast.Node, from, to ast.Node) { // (Do this is a second pass to avoid // unnecessary writes in the common case.) j := 0 - for i := 0; i < v.Len(); i++ { + for i := range v.Len() { if !v.Index(i).IsNil() { v.Index(j).Set(v.Index(i)) j++ @@ -3107,7 +3097,7 @@ func clearPositions(root ast.Node) { if n != nil { v := reflect.ValueOf(n).Elem() // deref the pointer to struct fields := v.Type().NumField() - for i := 0; i < fields; i++ { + for i := range fields { f := v.Field(i) // Clearing Pos arbitrarily is destructive, // as its presence may be semantically significant From a857356d5cc56c01228c895b060f0594e537b4eb Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 27 Mar 2025 14:44:16 -0400 Subject: [PATCH 280/309] internal/refactor/inline: improve freeishNames doc Change-Id: Ifbc6db97671a173237c55f0f415ad2d0eff6ecff Reviewed-on: https://go-review.googlesource.com/c/tools/+/661375 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/inline.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 127a70c680b..8ffd8315547 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -2449,7 +2449,12 @@ func freeVars(info *types.Info, e ast.Expr) map[string]bool { } // freeishNames computes an over-approximation to the free names -// of the type syntax t, inserting values into the map. +// of the expression (type or term) t, inserting values into the map. +// +// If t is a type expression, the approximation is not too far off (see below). For +// terms, it simply gathers all unqualified identifiers, ignoring scopes established +// by function and composite literals, so in some cases it can over-estimate quite +// a lot. // // Because we don't have go/types annotations, we can't give an exact // result in all cases. In particular, an array type [n]T might have a @@ -2468,9 +2473,9 @@ func freeishNames(free map[string]bool, t ast.Expr) { return false // don't visit .Sel case *ast.Field: + // Visit Type (which may have free references) + // but not Names (which are defs, not uses). ast.Inspect(n.Type, visit) - // Don't visit .Names: - // FuncType parameters, interface methods, struct fields return false } return true From aac3cf020c8adcf240806fba32b816bc55214ea5 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 28 Mar 2025 11:16:53 -0400 Subject: [PATCH 281/309] internal/refactor/inline: improve freeishNames Perform a more sophisticated free-name analysis. Adapt the name resolution logic from go/parser to find free names in an ast.Node. As the function doc says, the new function is stymied only by the composite-lit ambiguity and thus significantly more accurate that the old implementation, which did not take into account bindings within expressions. The test provides full coverage, except for some uninteresting edge cases. Change-Id: Id1e18b5e9b1aae78feb2066f5243fd565568ffdd Reviewed-on: https://go-review.googlesource.com/c/tools/+/661337 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/refactor/inline/free.go | 367 ++++++++++++++++++++++++++ internal/refactor/inline/free_test.go | 206 +++++++++++++++ internal/refactor/inline/inline.go | 35 --- 3 files changed, 573 insertions(+), 35 deletions(-) create mode 100644 internal/refactor/inline/free.go create mode 100644 internal/refactor/inline/free_test.go diff --git a/internal/refactor/inline/free.go b/internal/refactor/inline/free.go new file mode 100644 index 00000000000..76c8010add6 --- /dev/null +++ b/internal/refactor/inline/free.go @@ -0,0 +1,367 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Copied, with considerable changes, from go/parser/resolver.go +// at af53bd2c03. + +package inline + +import ( + "go/ast" + "go/token" +) + +// freeishNames computes an over-approximation to the free names of the AST +// at node n based solely on syntax, inserting values into the map. +// +// In the absence of composite literals, the set of free names is exact. Composite +// literals introduce an ambiguity that can only be resolved with type information: +// whether F is a field name or a value in `T{F: ...}`. +// This function conservatively assumes T is not a struct type, so the +// resulting set may contain spurious entries that are not free lexical +// references but are references to struct fields. +// +// The code is based on go/parser.resolveFile, but heavily simplified. Crucial +// differences are: +// - Instead of resolving names to their objects, this function merely records +// whether they are free. +// - Labels are ignored: they do not refer to values. +// - This is never called on FuncDecls or ImportSpecs, so the function +// panics if it sees one. +func freeishNames(free map[string]bool, n ast.Node) { + r := &freeVisitor{free: free} + ast.Walk(r, n) + assert(r.scope == nil, "unbalanced scopes") +} + +// A freeVisitor holds state for a free-name analysis. +type freeVisitor struct { + scope *scope // the current innermost scope + free map[string]bool // free names seen so far +} + +// scope contains all the names defined in a lexical scope. +// It is like ast.Scope, but without deprecation warnings. +type scope struct { + names map[string]bool + outer *scope +} + +func (s *scope) defined(name string) bool { + for ; s != nil; s = s.outer { + if s.names[name] { + return true + } + } + return false +} + +func (v *freeVisitor) Visit(n ast.Node) ast.Visitor { + switch n := n.(type) { + + // Expressions. + case *ast.Ident: + v.resolve(n) + + case *ast.FuncLit: + v.openScope() + defer v.closeScope() + v.walkFuncType(n.Type) + v.walkBody(n.Body) + + case *ast.SelectorExpr: + v.walk(n.X) + // Skip n.Sel: it cannot be free. + + case *ast.StructType: + v.openScope() + defer v.closeScope() + v.walkFieldList(n.Fields) + + case *ast.FuncType: + v.openScope() + defer v.closeScope() + v.walkFuncType(n) + + case *ast.CompositeLit: + v.walk(n.Type) + for _, e := range n.Elts { + if kv, _ := e.(*ast.KeyValueExpr); kv != nil { + if ident, _ := kv.Key.(*ast.Ident); ident != nil { + // It is not possible from syntax alone to know whether + // an identifier used as a composite literal key is + // a struct field (if n.Type is a struct) or a value + // (if n.Type is a map, slice or array). + // Over-approximate by treating both cases as potentially + // free names. + v.resolve(ident) + } else { + v.walk(kv.Key) + } + v.walk(kv.Value) + } else { + v.walk(e) + } + } + + case *ast.InterfaceType: + v.openScope() + defer v.closeScope() + v.walkFieldList(n.Methods) + + // Statements + case *ast.AssignStmt: + walkSlice(v, n.Rhs) + if n.Tok == token.DEFINE { + v.shortVarDecl(n.Lhs) + } else { + walkSlice(v, n.Lhs) + } + + case *ast.LabeledStmt: + // ignore labels + // TODO(jba): consider labels? + v.walk(n.Stmt) + + case *ast.BranchStmt: + // Ignore labels. + // TODO(jba): consider labels? + + case *ast.BlockStmt: + v.openScope() + defer v.closeScope() + walkSlice(v, n.List) + + case *ast.IfStmt: + v.openScope() + defer v.closeScope() + v.walk(n.Init) + v.walk(n.Cond) + v.walk(n.Body) + v.walk(n.Else) + + case *ast.CaseClause: + walkSlice(v, n.List) + v.openScope() + defer v.closeScope() + walkSlice(v, n.Body) + + case *ast.SwitchStmt: + v.openScope() + defer v.closeScope() + v.walk(n.Init) + v.walk(n.Tag) + v.walkBody(n.Body) + + case *ast.TypeSwitchStmt: + if n.Init != nil { + v.openScope() + defer v.closeScope() + v.walk(n.Init) + } + v.openScope() + defer v.closeScope() + v.walk(n.Assign) + // We can use walkBody here because we don't track label scopes. + v.walkBody(n.Body) + + case *ast.CommClause: + v.openScope() + defer v.closeScope() + v.walk(n.Comm) + walkSlice(v, n.Body) + + case *ast.SelectStmt: + v.walkBody(n.Body) + + case *ast.ForStmt: + v.openScope() + defer v.closeScope() + v.walk(n.Init) + v.walk(n.Cond) + v.walk(n.Post) + v.walk(n.Body) + + case *ast.RangeStmt: + v.openScope() + defer v.closeScope() + v.walk(n.X) + var lhs []ast.Expr + if n.Key != nil { + lhs = append(lhs, n.Key) + } + if n.Value != nil { + lhs = append(lhs, n.Value) + } + if len(lhs) > 0 { + if n.Tok == token.DEFINE { + v.shortVarDecl(lhs) + } else { + walkSlice(v, lhs) + } + } + v.walk(n.Body) + + // Declarations + case *ast.GenDecl: + switch n.Tok { + case token.CONST, token.VAR: + for _, spec := range n.Specs { + spec := spec.(*ast.ValueSpec) + walkSlice(v, spec.Values) + if spec.Type != nil { + v.walk(spec.Type) + } + v.declare(spec.Names...) + } + case token.TYPE: + for _, spec := range n.Specs { + spec := spec.(*ast.TypeSpec) + // Go spec: The scope of a type identifier declared inside a + // function begins at the identifier in the TypeSpec and ends + // at the end of the innermost containing block. + v.declare(spec.Name) + if spec.TypeParams != nil { + v.openScope() + defer v.closeScope() + v.walkTypeParams(spec.TypeParams) + } + v.walk(spec.Type) + } + + case token.IMPORT: + panic("encountered import declaration in free analysis") + } + + case *ast.FuncDecl: + panic("encountered top-level function declaration in free analysis") + + default: + return v + } + + return nil +} + +func (r *freeVisitor) openScope() { + r.scope = &scope{map[string]bool{}, r.scope} +} + +func (r *freeVisitor) closeScope() { + r.scope = r.scope.outer +} + +func (r *freeVisitor) walk(n ast.Node) { + if n != nil { + ast.Walk(r, n) + } +} + +// walkFuncType walks a function type. It is used for explicit +// function types, like this: +// +// type RunFunc func(context.Context) error +// +// and function literals, like this: +// +// func(a, b int) int { return a + b} +// +// neither of which have type parameters. +// Function declarations do involve type parameters, but we don't +// handle them. +func (r *freeVisitor) walkFuncType(typ *ast.FuncType) { + // The order here doesn't really matter, because names in + // a field list cannot appear in types. + // (The situation is different for type parameters, for which + // see [freeVisitor.walkTypeParams].) + r.resolveFieldList(typ.Params) + r.resolveFieldList(typ.Results) + r.declareFieldList(typ.Params) + r.declareFieldList(typ.Results) +} + +// walkTypeParams is like walkFieldList, but declares type parameters eagerly so +// that they may be resolved in the constraint expressions held in the field +// Type. +func (r *freeVisitor) walkTypeParams(list *ast.FieldList) { + r.declareFieldList(list) + r.resolveFieldList(list) +} + +func (r *freeVisitor) walkBody(body *ast.BlockStmt) { + if body == nil { + return + } + walkSlice(r, body.List) +} + +func (r *freeVisitor) walkFieldList(list *ast.FieldList) { + if list == nil { + return + } + r.resolveFieldList(list) // .Type may contain references + r.declareFieldList(list) // .Names declares names +} + +func (r *freeVisitor) shortVarDecl(lhs []ast.Expr) { + // Go spec: A short variable declaration may redeclare variables provided + // they were originally declared in the same block with the same type, and + // at least one of the non-blank variables is new. + // + // However, it doesn't matter to free analysis whether a variable is declared + // fresh or redeclared. + for _, x := range lhs { + // In a well-formed program each expr must be an identifier, + // but be forgiving. + if id, ok := x.(*ast.Ident); ok { + r.declare(id) + } + } +} + +func walkSlice[S ~[]E, E ast.Node](r *freeVisitor, list S) { + for _, e := range list { + r.walk(e) + } +} + +// resolveFieldList resolves the types of the fields in list. +// The companion method declareFieldList declares the names of the fields. +func (r *freeVisitor) resolveFieldList(list *ast.FieldList) { + if list == nil { + return + } + for _, f := range list.List { + r.walk(f.Type) + } +} + +// declareFieldList declares the names of the fields in list. +// (Names in a FieldList always establish new bindings.) +// The companion method resolveFieldList resolves the types of the fields. +func (r *freeVisitor) declareFieldList(list *ast.FieldList) { + if list == nil { + return + } + for _, f := range list.List { + r.declare(f.Names...) + } +} + +// resolve marks ident as free if it is not in scope. +// TODO(jba): rename: no resolution is happening. +func (r *freeVisitor) resolve(ident *ast.Ident) { + if s := ident.Name; s != "_" && !r.scope.defined(s) { + r.free[s] = true + } +} + +// declare adds each non-blank ident to the current scope. +func (r *freeVisitor) declare(idents ...*ast.Ident) { + for _, id := range idents { + if id.Name != "_" { + r.scope.names[id.Name] = true + } + } +} diff --git a/internal/refactor/inline/free_test.go b/internal/refactor/inline/free_test.go new file mode 100644 index 00000000000..72543e7ae81 --- /dev/null +++ b/internal/refactor/inline/free_test.go @@ -0,0 +1,206 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package inline + +import ( + "go/ast" + "go/parser" + "go/token" + "maps" + "slices" + "strings" + "testing" +) + +func TestFreeishNames(t *testing.T) { + elems := func(m map[string]bool) string { + return strings.Join(slices.Sorted(maps.Keys(m)), " ") + } + + for _, test := range []struct { + code string // one or more exprs, decls or stmts + want string // space-separated list of free names + }{ + { + `x`, + "x", + }, + { + `x.y.z`, + "x", + }, + { + `T{a: 1, b: 2, c.d: e}`, + "a b c e T", + }, + { + `f(x)`, + "f x", + }, + { + `f.m(x)`, + "f x", + }, + { + `func(x int) int { return x + y }`, + "int y", + }, + { + `x = func(x int) int { return 2*x }()`, + "int x", + }, + { + `func(x int) (y int) { return x + y }`, + "int", + }, + { + `struct{a **int; b map[int][]bool}`, + "bool int", + }, + { + `struct{f int}{f: 0}`, + "f int", + }, + { + `interface{m1(int) bool; m2(x int) (y bool)}`, + "bool int", + }, + { + `x := 1; x++`, + "", + }, + { + `x = 1`, + "x", + }, + { + `_ = 1`, + "", + }, + { + `x, y := 1, 2; x = y + z`, + "z", + }, + { + `x, y := y, x; x = y + z`, + "x y z", + }, + { + `a, b := 0, 0; b, c := 0, 0; print(a, b, c, d)`, + "d print", + }, + { + `label: x++`, + "x", + }, + { + `if x == y {x}`, + "x y", + }, + { + `if x := 1; x == y {x}`, + "y", + }, + { + `if x := 1; x == y {x} else {z}`, + "y z", + }, + { + `switch x { case 1: x; case y: z }`, + "x y z", + }, + { + `switch x := 1; x { case 1: x; case y: z }`, + "y z", + }, + { + `switch x.(type) { case int: x; case []int: y }`, + "int x y", + }, + { + `switch x := 1; x.(type) { case int: x; case []int: y }`, + "int y", + }, + { + `switch y := x.(type) { case int: x; case []int: y }`, + "int x", + }, + { + `select { case c <- 1: x; case x := <-c: 2; default: y}`, + "c x y", + }, + { + `for i := 0; i < 9; i++ { c <- j }`, + "c j", + }, + { + `for i = 0; i < 9; i++ { c <- j }`, + "c i j", + }, + { + `for i := range 9 { c <- j }`, + "c j", + }, + { + `for i = range 9 { c <- j }`, + "c i j", + }, + { + `for _, e := range []int{1, 2, x} {e}`, + "int x", + }, + { + `var x, y int; f(x, y)`, + "f int", + }, + { + `{var x, y int}; f(x, y)`, + "f int x y", + }, + { + `const x = 1; { const y = iota; return x, y }`, + "iota", + }, + { + `type t int; t(0)`, + "int", + }, + { + `type t[T ~int] struct { t T }; x = t{t: 1}.t`, // field t shadowed by type decl + "int x", + }, + { + `type t[S ~[]E, E any] S`, + "any", + }, + { + `var a [unsafe.Sizeof(func(x int) { x + y })]int`, + "int unsafe y", + }, + } { + _, f := mustParse(t, "free.go", `package p; func _() {`+test.code+`}`) + n := f.Decls[0].(*ast.FuncDecl).Body + got := map[string]bool{} + want := map[string]bool{} + for _, n := range strings.Fields(test.want) { + want[n] = true + } + + freeishNames(got, n) + + if !maps.Equal(got, want) { + t.Errorf("\ncode %s\ngot %v\nwant %v", test.code, elems(got), elems(want)) + } + } +} + +func mustParse(t *testing.T, filename string, content any) (*token.FileSet, *ast.File) { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, filename, content, parser.ParseComments|parser.SkipObjectResolution) + if err != nil { + t.Fatalf("ParseFile: %v", err) + } + return fset, f +} diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 8ffd8315547..7d65b583524 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -2448,41 +2448,6 @@ func freeVars(info *types.Info, e ast.Expr) map[string]bool { return free } -// freeishNames computes an over-approximation to the free names -// of the expression (type or term) t, inserting values into the map. -// -// If t is a type expression, the approximation is not too far off (see below). For -// terms, it simply gathers all unqualified identifiers, ignoring scopes established -// by function and composite literals, so in some cases it can over-estimate quite -// a lot. -// -// Because we don't have go/types annotations, we can't give an exact -// result in all cases. In particular, an array type [n]T might have a -// size such as unsafe.Sizeof(func() int{stmts...}()) and now the -// precise answer depends upon all the statement syntax too. But that -// never happens in practice. -func freeishNames(free map[string]bool, t ast.Expr) { - var visit func(n ast.Node) bool - visit = func(n ast.Node) bool { - switch n := n.(type) { - case *ast.Ident: - free[n.Name] = true - - case *ast.SelectorExpr: - ast.Inspect(n.X, visit) - return false // don't visit .Sel - - case *ast.Field: - // Visit Type (which may have free references) - // but not Names (which are defs, not uses). - ast.Inspect(n.Type, visit) - return false - } - return true - } - ast.Inspect(t, visit) -} - // effects reports whether an expression might change the state of the // program (through function calls and channel receives) and affect // the evaluation of subsequent expressions. From 659a8cd099cf8a6cd8d13de88269c081c6e069c5 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Mon, 31 Mar 2025 15:13:55 +0800 Subject: [PATCH 282/309] go/analysis/analysistest: report input rather result when error happens Change-Id: I6ca92dd6c0aab72399341646a0eca908b08cad54 Reviewed-on: https://go-review.googlesource.com/c/tools/+/661755 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- go/analysis/analysistest/analysistest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 143b4260346..a20773fe26d 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -298,7 +298,7 @@ func applyDiffsAndCompare(filename string, original, want []byte, edits []diff.E } fixed, err := format.Source(fixedBytes) if err != nil { - return fmt.Errorf("%s: error formatting resulting source: %v\n%s", filename, err, fixed) + return fmt.Errorf("%s: error formatting resulting source: %v\n%s", filename, err, fixedBytes) } want, err = format.Source(want) From 5c9a69f93423f50b7224a9bfffe4e10e5f156b8a Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 28 Mar 2025 13:53:15 -0400 Subject: [PATCH 283/309] internal/refactor/inline: get rid of imports.Process Rewrite the part of the inliner that removes unnecessary new imports to avoid imports.Process. Now the inliner does not depend on the go command. There are two parts to the solution: 1. Use the recently improved free-name analysis to find unused imports. This CL further improves the algorithm by removing composite-literal keys from the set of possible free names for this call: such keys can never be package names. So freeishNames returns all of the used package names and none of the unused ones. Other calls to freeishNames retain the composite-literal keys to ensure that all free names are found. 2. Replace the ad-hoc code that deleted old imports with astutil.DeleteNamedImport. The more careful approach of that function results in prettier import decls that are closer to those of imports.Process, at the cost of deleting the old imports one by one. But it's unlikely that there are more than a couple of old imports. Some tests were tweaked to match the current behavior: - The new algorithm produces a slightly nicer result on assignment.txtar. - import-shadow.txtar's input already contained imports that would not have appeared in the wild to users of goimports or similar tools; its output is now similarly atypical. The algorithm behaves better on a version of import-shadow with cleaned-up imports. Both variants appear as tests. Some tests in other packages also needed minor changes. Change-Id: I410a2808511b3ccfe1cc555f37b832a0b8b6ea5c Reviewed-on: https://go-review.googlesource.com/c/tools/+/661635 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- .../analysis/gofix/testdata/src/b/b.go.golden | 6 +- .../testdata/codeaction/inline_issue67336.txt | 1 - .../codeaction/removeparam_imports.txt | 12 +- internal/refactor/inline/free.go | 35 +- internal/refactor/inline/free_test.go | 367 ++++++++++-------- internal/refactor/inline/inline.go | 141 +++---- .../refactor/inline/testdata/assignment.txtar | 4 +- .../inline/testdata/import-shadow-1.txtar | 48 +++ .../inline/testdata/import-shadow.txtar | 6 +- 9 files changed, 332 insertions(+), 288 deletions(-) create mode 100644 internal/refactor/inline/testdata/import-shadow-1.txtar diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden index fd8d87a2ef1..4de7f09710f 100644 --- a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden +++ b/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden @@ -4,10 +4,8 @@ import a0 "a" import "io" -import ( - "a" - . "c" -) +import "a" +import . "c" func f() { a.One() // want `cannot inline call to a.One because body refers to non-exported one` diff --git a/gopls/internal/test/marker/testdata/codeaction/inline_issue67336.txt b/gopls/internal/test/marker/testdata/codeaction/inline_issue67336.txt index 437fb474fb2..f15ca29397b 100644 --- a/gopls/internal/test/marker/testdata/codeaction/inline_issue67336.txt +++ b/gopls/internal/test/marker/testdata/codeaction/inline_issue67336.txt @@ -54,7 +54,6 @@ package c import ( "context" - "example.com/define/my/typ" "example.com/one/more/pkg" pkg0 "example.com/some/other/pkg" diff --git a/gopls/internal/test/marker/testdata/codeaction/removeparam_imports.txt b/gopls/internal/test/marker/testdata/codeaction/removeparam_imports.txt index d9f4f22dc7e..cd5f910a70d 100644 --- a/gopls/internal/test/marker/testdata/codeaction/removeparam_imports.txt +++ b/gopls/internal/test/marker/testdata/codeaction/removeparam_imports.txt @@ -65,9 +65,7 @@ func B(x, y c.C) { //@codeaction("x", "refactor.rewrite.removeUnusedParam", resu -- @b/a/a3.go -- package a -import ( - "mod.test/b" -) +import "mod.test/b" func _() { b.B(<-b.Chan) @@ -79,9 +77,7 @@ func _() { -- @b/a/a2.go -- package a -import ( - "mod.test/b" -) +import "mod.test/b" func _() { b.B(<-b.Chan) @@ -90,9 +86,7 @@ func _() { -- @b/a/a1.go -- package a -import ( - "mod.test/b" -) +import "mod.test/b" func _() { b.B(<-b.Chan) diff --git a/internal/refactor/inline/free.go b/internal/refactor/inline/free.go index 76c8010add6..28cebeea3db 100644 --- a/internal/refactor/inline/free.go +++ b/internal/refactor/inline/free.go @@ -12,15 +12,19 @@ import ( "go/token" ) -// freeishNames computes an over-approximation to the free names of the AST +// freeishNames computes an approximation to the free names of the AST // at node n based solely on syntax, inserting values into the map. // // In the absence of composite literals, the set of free names is exact. Composite // literals introduce an ambiguity that can only be resolved with type information: // whether F is a field name or a value in `T{F: ...}`. -// This function conservatively assumes T is not a struct type, so the -// resulting set may contain spurious entries that are not free lexical -// references but are references to struct fields. +// If includeComplitIdents is true, this function conservatively assumes +// T is not a struct type, so freeishNames overapproximates: the resulting +// set may contain spurious entries that are not free lexical references +// but are references to struct fields. +// If includeComplitIdents is false, this function assumes that T *is* +// a struct type, so freeishNames underapproximates: the resulting set +// may omit names that are free lexical references. // // The code is based on go/parser.resolveFile, but heavily simplified. Crucial // differences are: @@ -29,16 +33,17 @@ import ( // - Labels are ignored: they do not refer to values. // - This is never called on FuncDecls or ImportSpecs, so the function // panics if it sees one. -func freeishNames(free map[string]bool, n ast.Node) { - r := &freeVisitor{free: free} - ast.Walk(r, n) - assert(r.scope == nil, "unbalanced scopes") +func freeishNames(free map[string]bool, n ast.Node, includeComplitIdents bool) { + v := &freeVisitor{free: free, includeComplitIdents: includeComplitIdents} + ast.Walk(v, n) + assert(v.scope == nil, "unbalanced scopes") } // A freeVisitor holds state for a free-name analysis. type freeVisitor struct { - scope *scope // the current innermost scope - free map[string]bool // free names seen so far + scope *scope // the current innermost scope + free map[string]bool // free names seen so far + includeComplitIdents bool // include identifier key in composite literals } // scope contains all the names defined in a lexical scope. @@ -93,9 +98,13 @@ func (v *freeVisitor) Visit(n ast.Node) ast.Visitor { // an identifier used as a composite literal key is // a struct field (if n.Type is a struct) or a value // (if n.Type is a map, slice or array). - // Over-approximate by treating both cases as potentially - // free names. - v.resolve(ident) + if v.includeComplitIdents { + // Over-approximate by treating both cases as potentially + // free names. + v.resolve(ident) + } else { + // Under-approximate by ignoring potentially free names. + } } else { v.walk(kv.Key) } diff --git a/internal/refactor/inline/free_test.go b/internal/refactor/inline/free_test.go index 72543e7ae81..28fa56db099 100644 --- a/internal/refactor/inline/free_test.go +++ b/internal/refactor/inline/free_test.go @@ -5,6 +5,7 @@ package inline import ( + "fmt" "go/ast" "go/parser" "go/token" @@ -19,180 +20,212 @@ func TestFreeishNames(t *testing.T) { return strings.Join(slices.Sorted(maps.Keys(m)), " ") } - for _, test := range []struct { + type testcase struct { code string // one or more exprs, decls or stmts want string // space-separated list of free names + } + + for _, tc := range []struct { + includeComplitIdents bool + cases []testcase }{ - { - `x`, - "x", - }, - { - `x.y.z`, - "x", - }, - { - `T{a: 1, b: 2, c.d: e}`, - "a b c e T", - }, - { - `f(x)`, - "f x", - }, - { - `f.m(x)`, - "f x", - }, - { - `func(x int) int { return x + y }`, - "int y", - }, - { - `x = func(x int) int { return 2*x }()`, - "int x", - }, - { - `func(x int) (y int) { return x + y }`, - "int", - }, - { - `struct{a **int; b map[int][]bool}`, - "bool int", - }, - { - `struct{f int}{f: 0}`, - "f int", - }, - { - `interface{m1(int) bool; m2(x int) (y bool)}`, - "bool int", - }, - { - `x := 1; x++`, - "", - }, - { - `x = 1`, - "x", - }, - { - `_ = 1`, - "", - }, - { - `x, y := 1, 2; x = y + z`, - "z", - }, - { - `x, y := y, x; x = y + z`, - "x y z", - }, - { - `a, b := 0, 0; b, c := 0, 0; print(a, b, c, d)`, - "d print", - }, - { - `label: x++`, - "x", - }, - { - `if x == y {x}`, - "x y", - }, - { - `if x := 1; x == y {x}`, - "y", - }, - { - `if x := 1; x == y {x} else {z}`, - "y z", - }, - { - `switch x { case 1: x; case y: z }`, - "x y z", - }, - { - `switch x := 1; x { case 1: x; case y: z }`, - "y z", - }, - { - `switch x.(type) { case int: x; case []int: y }`, - "int x y", - }, - { - `switch x := 1; x.(type) { case int: x; case []int: y }`, - "int y", - }, - { - `switch y := x.(type) { case int: x; case []int: y }`, - "int x", - }, - { - `select { case c <- 1: x; case x := <-c: 2; default: y}`, - "c x y", - }, - { - `for i := 0; i < 9; i++ { c <- j }`, - "c j", - }, - { - `for i = 0; i < 9; i++ { c <- j }`, - "c i j", - }, - { - `for i := range 9 { c <- j }`, - "c j", - }, - { - `for i = range 9 { c <- j }`, - "c i j", - }, - { - `for _, e := range []int{1, 2, x} {e}`, - "int x", - }, - { - `var x, y int; f(x, y)`, - "f int", - }, - { - `{var x, y int}; f(x, y)`, - "f int x y", - }, - { - `const x = 1; { const y = iota; return x, y }`, - "iota", - }, - { - `type t int; t(0)`, - "int", - }, - { - `type t[T ~int] struct { t T }; x = t{t: 1}.t`, // field t shadowed by type decl - "int x", - }, - { - `type t[S ~[]E, E any] S`, - "any", - }, - { - `var a [unsafe.Sizeof(func(x int) { x + y })]int`, - "int unsafe y", + {true, []testcase{ + { + `x`, + "x", + }, + { + `x.y.z`, + "x", + }, + { + `T{a: 1, b: 2, c.d: e}`, + "a b c e T", + }, + { + `f(x)`, + "f x", + }, + { + `f.m(x)`, + "f x", + }, + { + `func(x int) int { return x + y }`, + "int y", + }, + { + `x = func(x int) int { return 2*x }()`, + "int x", + }, + { + `func(x int) (y int) { return x + y }`, + "int", + }, + { + `struct{a **int; b map[int][]bool}`, + "bool int", + }, + { + `struct{f int}{f: 0}`, + "f int", + }, + { + `interface{m1(int) bool; m2(x int) (y bool)}`, + "bool int", + }, + { + `x := 1; x++`, + "", + }, + { + `x = 1`, + "x", + }, + { + `_ = 1`, + "", + }, + { + `x, y := 1, 2; x = y + z`, + "z", + }, + { + `x, y := y, x; x = y + z`, + "x y z", + }, + { + `a, b := 0, 0; b, c := 0, 0; print(a, b, c, d)`, + "d print", + }, + { + `label: x++`, + "x", + }, + { + `if x == y {x}`, + "x y", + }, + { + `if x := 1; x == y {x}`, + "y", + }, + { + `if x := 1; x == y {x} else {z}`, + "y z", + }, + { + `switch x { case 1: x; case y: z }`, + "x y z", + }, + { + `switch x := 1; x { case 1: x; case y: z }`, + "y z", + }, + { + `switch x.(type) { case int: x; case []int: y }`, + "int x y", + }, + { + `switch x := 1; x.(type) { case int: x; case []int: y }`, + "int y", + }, + { + `switch y := x.(type) { case int: x; case []int: y }`, + "int x", + }, + { + `select { case c <- 1: x; case x := <-c: 2; default: y}`, + "c x y", + }, + { + `for i := 0; i < 9; i++ { c <- j }`, + "c j", + }, + { + `for i = 0; i < 9; i++ { c <- j }`, + "c i j", + }, + { + `for i := range 9 { c <- j }`, + "c j", + }, + { + `for i = range 9 { c <- j }`, + "c i j", + }, + { + `for _, e := range []int{1, 2, x} {e}`, + "int x", + }, + { + `var x, y int; f(x, y)`, + "f int", + }, + { + `{var x, y int}; f(x, y)`, + "f int x y", + }, + { + `const x = 1; { const y = iota; return x, y }`, + "iota", + }, + { + `type t int; t(0)`, + "int", + }, + { + `type t[T ~int] struct { t T }; x = t{t: 1}.t`, // field t shadowed by type decl + "int x", + }, + { + `type t[S ~[]E, E any] S`, + "any", + }, + { + `var a [unsafe.Sizeof(func(x int) { x + y })]int`, + "int unsafe y", + }, + }}, + { + false, + []testcase{ + { + `x`, + "x", + }, + { + `x.y.z`, + "x", + }, + { + `T{a: 1, b: 2, c.d: e}`, + "c e T", // omit a and b + }, + { + `type t[T ~int] struct { t T }; x = t{t: 1}.t`, // field t shadowed by type decl + "int x", + }, + }, }, } { - _, f := mustParse(t, "free.go", `package p; func _() {`+test.code+`}`) - n := f.Decls[0].(*ast.FuncDecl).Body - got := map[string]bool{} - want := map[string]bool{} - for _, n := range strings.Fields(test.want) { - want[n] = true - } + t.Run(fmt.Sprintf("includeComplitIdents=%t", tc.includeComplitIdents), func(t *testing.T) { + for _, test := range tc.cases { + _, f := mustParse(t, "free.go", `package p; func _() {`+test.code+`}`) + n := f.Decls[0].(*ast.FuncDecl).Body + got := map[string]bool{} + want := map[string]bool{} + for _, n := range strings.Fields(test.want) { + want[n] = true + } - freeishNames(got, n) + freeishNames(got, n, tc.includeComplitIdents) - if !maps.Equal(got, want) { - t.Errorf("\ncode %s\ngot %v\nwant %v", test.code, elems(got), elems(want)) - } + if !maps.Equal(got, want) { + t.Errorf("\ncode %s\ngot %v\nwant %v", test.code, elems(got), elems(want)) + } + } + }) } } diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 7d65b583524..7817444150e 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -22,7 +22,6 @@ import ( "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/imports" "golang.org/x/tools/internal/analysisinternal" internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/typeparams" @@ -271,12 +270,12 @@ func (st *state) inline() (*Result, error) { } } - // Add new imports. - // + // Add new imports that are still used. + newImports := trimNewImports(res.newImports, res.new) // Insert new imports after last existing import, // to avoid migration of pre-import comments. // The imports will be organized below. - if len(res.newImports) > 0 { + if len(newImports) > 0 { // If we have imports to add, do so independent of the rest of the file. // Otherwise, the length of the new imports may consume floating comments, // causing them to be printed inside the imports block. @@ -329,7 +328,7 @@ func (st *state) inline() (*Result, error) { } } // Add new imports. - for _, imp := range res.newImports { + for _, imp := range newImports { // Check that the new imports are accessible. path, _ := strconv.Unquote(imp.spec.Path.Value) if !analysisinternal.CanImport(caller.Types.Path(), path) { @@ -355,30 +354,14 @@ func (st *state) inline() (*Result, error) { } // Delete imports referenced only by caller.Call.Fun. - // - // (We can't let imports.Process take care of it as it may - // mistake obsolete imports for missing new imports when the - // names are similar, as is common during a package migration.) for _, oldImport := range res.oldImports { specToDelete := oldImport.spec - for _, decl := range f.Decls { - if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { - decl.Specs = slices.DeleteFunc(decl.Specs, func(spec ast.Spec) bool { - imp := spec.(*ast.ImportSpec) - // Since we re-parsed the file, we can't match by identity; - // instead look for syntactic equivalence. - return imp.Path.Value == specToDelete.Path.Value && - (imp.Name != nil) == (specToDelete.Name != nil) && - (imp.Name == nil || imp.Name.Name == specToDelete.Name.Name) - }) - - // Edge case: import "foo" => import (). - if !decl.Lparen.IsValid() { - decl.Lparen = decl.TokPos + token.Pos(len("import")) - decl.Rparen = decl.Lparen + 1 - } - } + name := "" + if specToDelete.Name != nil { + name = specToDelete.Name.Name } + path, _ := strconv.Unquote(specToDelete.Path.Value) + astutil.DeleteNamedImport(caller.Fset, f, name, path) } var out bytes.Buffer @@ -387,66 +370,6 @@ func (st *state) inline() (*Result, error) { } newSrc := out.Bytes() - // Remove imports that are no longer referenced. - // - // It ought to be possible to compute the set of PkgNames used - // by the "old" code, compute the free identifiers of the - // "new" code using a syntax-only (no go/types) algorithm, and - // see if the reduction in the number of uses of any PkgName - // equals the number of times it appears in caller.Info.Uses, - // indicating that it is no longer referenced by res.new. - // - // However, the notorious ambiguity of resolving T{F: 0} makes this - // unreliable: without types, we can't tell whether F refers to - // a field of struct T, or a package-level const/var of a - // dot-imported (!) package. - // - // So, for now, we run imports.Process, which is - // unsatisfactory as it has to run the go command, and it - // looks at the user's module cache state--unnecessarily, - // since this step cannot add new imports. - // - // TODO(adonovan): replace with a simpler implementation since - // all the necessary imports are present but merely untidy. - // That will be faster, and also less prone to nondeterminism - // if there are bugs in our logic for import maintenance. - // - // However, golang.org/x/tools/internal/imports.ApplyFixes is - // too simple as it requires the caller to have figured out - // all the logical edits. In our case, we know all the new - // imports that are needed (see newImports), each of which can - // be specified as: - // - // &imports.ImportFix{ - // StmtInfo: imports.ImportInfo{path, name, - // IdentName: name, - // FixType: imports.AddImport, - // } - // - // but we don't know which imports are made redundant by the - // inlining itself. For example, inlining a call to - // fmt.Println may make the "fmt" import redundant. - // - // Also, both imports.Process and internal/imports.ApplyFixes - // reformat the entire file, which is not ideal for clients - // such as gopls. (That said, the point of a canonical format - // is arguably that any tool can reformat as needed without - // this being inconvenient.) - // - // We could invoke imports.Process and parse its result, - // compare against the original AST, compute a list of import - // fixes, and return that too. - - // Recompute imports only if there were existing ones. - if len(f.Imports) > 0 { - formatted, err := imports.Process("output", newSrc, nil) - if err != nil { - logf("cannot reformat: %v <<%s>>", err, &out) - return nil, err // cannot reformat (a bug?) - } - newSrc = formatted - } - literalized := false if call, ok := res.new.(*ast.CallExpr); ok && is[*ast.FuncLit](call.Fun) { literalized = true @@ -610,6 +533,43 @@ func (i *importState) localName(pkgPath, pkgName string, shadow shadowMap) strin return name } +// trimNewImports removes imports that are no longer needed. +// +// The list of new imports as constructed by calls to [importState.localName] +// includes all of the packages referenced by the callee. +// But in the process of inlining, we may have dropped some of those references. +// For example, if the callee looked like this: +// +// func F(x int) (p.T) {... /* no mention of p */ ...} +// +// and we inlined by assignment: +// +// v := ... +// +// then the reference to package p drops away. +// +// Remove the excess imports by seeing which remain in new, the expression +// to be inlined. +// We can find those by looking at the free names in new. +// The list of free names cannot include spurious package names. +// Free-name tracking is precise except for the case of an identifier +// key in a composite literal, which names either a field or a value. +// Neither fields nor values are package names. +// Since they are not relevant to removing unused imports, we instruct +// freeishNames to omit composite-literal keys that are identifiers. +func trimNewImports(newImports []newImport, new ast.Node) []newImport { + free := map[string]bool{} + const omitComplitIdents = false + freeishNames(free, new, omitComplitIdents) + var res []newImport + for _, ni := range newImports { + if free[ni.pkgName] { + res = append(res, ni) + } + } + return res +} + type inlineCallResult struct { newImports []newImport // to add oldImports []oldImport // to remove @@ -2317,7 +2277,8 @@ func createBindingDecl(logf logger, caller *Caller, args []*argument, calleeDecl free[name] = true } } - freeishNames(free, spec.Type) + const includeComplitIdents = true + freeishNames(free, spec.Type, includeComplitIdents) for name := range free { if names[name] { logf("binding decl would shadow free name %q", name) @@ -3390,12 +3351,14 @@ func (st *state) assignStmts(callerStmt *ast.AssignStmt, returnOperands []ast.Ex freeNames = make(map[string]bool) // free(ish) names among rhs expressions nonTrivial = make(map[int]bool) // indexes in rhs of nontrivial result conversions ) + const includeComplitIdents = true + for i, expr := range callerStmt.Rhs { if expr == caller.Call { assert(callIdx == -1, "malformed (duplicative) AST") callIdx = i for j, returnOperand := range returnOperands { - freeishNames(freeNames, returnOperand) + freeishNames(freeNames, returnOperand, includeComplitIdents) rhs = append(rhs, returnOperand) if resultInfo[j]&nonTrivialResult != 0 { nonTrivial[i+j] = true @@ -3408,7 +3371,7 @@ func (st *state) assignStmts(callerStmt *ast.AssignStmt, returnOperands []ast.Ex // We must clone before clearing positions, since e came from the caller. expr = internalastutil.CloneNode(expr) clearPositions(expr) - freeishNames(freeNames, expr) + freeishNames(freeNames, expr, includeComplitIdents) rhs = append(rhs, expr) } } diff --git a/internal/refactor/inline/testdata/assignment.txtar b/internal/refactor/inline/testdata/assignment.txtar index c79c1732934..e201d601480 100644 --- a/internal/refactor/inline/testdata/assignment.txtar +++ b/internal/refactor/inline/testdata/assignment.txtar @@ -103,9 +103,7 @@ func _() { -- b2 -- package a -import ( - "testdata/b" -) +import "testdata/b" func _() { var y int diff --git a/internal/refactor/inline/testdata/import-shadow-1.txtar b/internal/refactor/inline/testdata/import-shadow-1.txtar new file mode 100644 index 00000000000..dc960ac3213 --- /dev/null +++ b/internal/refactor/inline/testdata/import-shadow-1.txtar @@ -0,0 +1,48 @@ +This file is identical to import-shadow.txtar except +that the imports in a/a.go are not grouped. +That is unusual, since goimports and related tools +form groups. + +The result of inlining (bresult) also looks strange, +but again, goimports would fix it up. + +-- go.mod -- +module testdata +go 1.12 + +-- a/a.go -- +package a + +import "testdata/b" +import "log" + +func A() { + const log = "shadow" + b.B() //@ inline(re"B", bresult) +} + +var _ log.Logger + +-- b/b.go -- +package b + +import "log" + +func B() { + log.Printf("") +} + +-- bresult -- +package a + +import ( + log0 "log" +) +import "log" + +func A() { + const log = "shadow" + log0.Printf("") //@ inline(re"B", bresult) +} + +var _ log.Logger diff --git a/internal/refactor/inline/testdata/import-shadow.txtar b/internal/refactor/inline/testdata/import-shadow.txtar index a1078e2495b..c4ea9a61624 100644 --- a/internal/refactor/inline/testdata/import-shadow.txtar +++ b/internal/refactor/inline/testdata/import-shadow.txtar @@ -14,8 +14,10 @@ go 1.12 -- a/a.go -- package a -import "testdata/b" -import "log" +import ( + "testdata/b" + "log" +) func A() { const log = "shadow" From 5fba861ea8db4da158308a9a347e5de5887a183a Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 11 Mar 2025 15:25:00 -0400 Subject: [PATCH 284/309] internal/typesinternal: add Object and ClassifyCall Add two functions that provide information about expressions, typically those that are in the function position of calls. Object returns the object related to any expression. It is intended to be called on values in the field CallExpr.Fun. ClassifyCall returns information about all forms of syntactic function calls in Go, including conversions. A subsequent CL will remimplement Callee and StaticCallee with these functions. Change-Id: I812c9c89fa7369a968eb31bd11bb16257f5936ba Reviewed-on: https://go-review.googlesource.com/c/tools/+/658196 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- internal/typesinternal/classify_call.go | 173 +++++++++++++++++++ internal/typesinternal/classify_call_test.go | 165 ++++++++++++++++++ 2 files changed, 338 insertions(+) create mode 100644 internal/typesinternal/classify_call.go create mode 100644 internal/typesinternal/classify_call_test.go diff --git a/internal/typesinternal/classify_call.go b/internal/typesinternal/classify_call.go new file mode 100644 index 00000000000..1e79eb2b7ac --- /dev/null +++ b/internal/typesinternal/classify_call.go @@ -0,0 +1,173 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typesinternal + +import ( + "fmt" + "go/ast" + "go/types" +) + +// CallKind describes the function position of an [*ast.CallExpr]. +type CallKind int + +const ( + CallStatic CallKind = iota // static call to known function + CallInterface // dynamic call through an interface method + CallDynamic // dynamic call of a func value + CallBuiltin // call to a builtin function + CallConversion // a conversion (not a call) +) + +var callKindNames = []string{ + "CallStatic", + "CallInterface", + "CallDynamic", + "CallBuiltin", + "CallConversion", +} + +func (k CallKind) String() string { + if i := int(k); i >= 0 && i < len(callKindNames) { + return callKindNames[i] + } + return fmt.Sprintf("typeutil.CallKind(%d)", k) +} + +// ClassifyCall classifies the function position of a call expression ([*ast.CallExpr]). +// It distinguishes among true function calls, calls to builtins, and type conversions, +// and further classifies function calls as static calls (where the function is known), +// dynamic interface calls, and other dynamic calls. +// +// For static, interface and builtin calls, ClassifyCall returns the [types.Object] +// for the name of the caller. For calls of instantiated functions and +// methods, it returns the object for the corresponding generic function +// or method on the generic type. +// The relationships between the return values are: +// +// CallKind object +// CallStatic *types.Func +// CallInterface *types.Func +// CallBuiltin *types.Builtin +// CallDynamic nil +// CallConversion nil +// +// For the declarations: +// +// func f() {} +// func g[T any]() {} +// var v func() +// var s []func() +// type I interface { M() } +// var i I +// +// ClassifyCall returns the following: +// +// f() CallStatic the *types.Func for f +// g[int]() CallStatic the *types.Func for g[T] +// i.M() CallInterface the *types.Func for i.M +// min(1, 2) CallBuiltin the *types.Builtin for min +// v() CallDynamic nil +// s[0]() CallDynamic nil +// int(x) CallConversion nil +// []byte("") CallConversion nil +func ClassifyCall(info *types.Info, call *ast.CallExpr) (CallKind, types.Object) { + if info.Types[call.Fun].IsType() { + return CallConversion, nil + } + obj := Used(info, call.Fun) + // Classify the call by the type of the object, if any. + switch obj := obj.(type) { + case *types.Builtin: + return CallBuiltin, obj + case *types.Func: + if interfaceMethod(obj) { + return CallInterface, obj + } + return CallStatic, obj + default: + return CallDynamic, nil + } +} + +// Used returns the [types.Object] used by e, if any. +// If e is one of various forms of reference: +// +// f, c, v, T lexical reference +// pkg.X qualified identifier +// f[T] or pkg.F[K,V] instantiations of the above kinds +// expr.f field or method value selector +// T.f method expression selector +// +// Used returns the object to which it refers. +// +// For the declarations: +// +// func F[T any] {...} +// type I interface { M() } +// var ( +// x int +// s struct { f int } +// a []int +// i I +// ) +// +// Used returns the following: +// +// Expr Used +// x the *types.Var for x +// s.f the *types.Var for f +// F[int] the *types.Func for F[T] (not F[int]) +// i.M the *types.Func for i.M +// I.M the *types.Func for I.M +// min the *types.Builtin for min +// int the *types.TypeName for int +// 1 nil +// a[0] nil +// []byte nil +// +// Note: if e is an instantiated function or method, Used returns +// the corresponding generic function or method on the generic type. +func Used(info *types.Info, e ast.Expr) types.Object { + return used(info, e) +} + +// placeholder: will be moved and documented in the next CL. +func used(info *types.Info, e ast.Expr) types.Object { + e = ast.Unparen(e) + // Look through type instantiation if necessary. + isIndexed := false + switch d := e.(type) { + case *ast.IndexExpr: + if info.Types[d.Index].IsType() { + e = d.X + } + case *ast.IndexListExpr: + e = d.X + } + var obj types.Object + switch e := e.(type) { + case *ast.Ident: + obj = info.Uses[e] // type, var, builtin, or declared func + case *ast.SelectorExpr: + if sel, ok := info.Selections[e]; ok { + obj = sel.Obj() // method or field + } else { + obj = info.Uses[e.Sel] // qualified identifier? + } + } + // If a variable like a slice or map is being indexed, do not + // return an object. + if _, ok := obj.(*types.Var); ok && isIndexed { + return nil + } + return obj +} + +// placeholder: will be moved and documented in the next CL. +func interfaceMethod(f *types.Func) bool { + recv := f.Signature().Recv() + return recv != nil && types.IsInterface(recv.Type()) +} diff --git a/internal/typesinternal/classify_call_test.go b/internal/typesinternal/classify_call_test.go new file mode 100644 index 00000000000..8a6e75a3b0d --- /dev/null +++ b/internal/typesinternal/classify_call_test.go @@ -0,0 +1,165 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typesinternal_test + +import ( + "bytes" + "fmt" + "go/ast" + "go/format" + "go/importer" + "go/parser" + "go/token" + "go/types" + "testing" + + ti "golang.org/x/tools/internal/typesinternal" +) + +func TestClassifyCallAndUsed(t *testing.T) { + // This function directly tests ClassifyCall, but since that + // function's second return value is always the result of Used, + // it effectively tests Used as well. + const src = ` + package p + + func g(int) + + type A[T any] *T + + func F[T any](T) {} + + type S struct{ f func(int) } + func (S) g(int) + + type I interface{ m(int) } + + var ( + z S + a struct{b struct{c S}} + f = g + m map[int]func() + n []func() + p *int + ) + + func tests[T int]() { + var zt T + + g(1) + f(1) + println() + z.g(1) // a concrete method + a.b.c.g(1) // same + S.g(z, 1) // method expression + z.f(1) // struct field + I(nil).m(1) // interface method, then type conversion (preorder traversal) + m[0]() // a map + n[0]() // a slice + F[int](1) // instantiated function + F[T](zt) // generic function + func() {}() // function literal + _=[]byte("") // type expression + _=A[int](p) // instantiated type + _=T(1) // type param + // parenthesized forms + (z.g)(1) + (z).g(1) + + + // A[T](1) // generic type: illegal + } + ` + + fset := token.NewFileSet() + cfg := &types.Config{ + Error: func(err error) { t.Fatal(err) }, + Importer: importer.Default(), + } + info := &types.Info{ + Instances: make(map[*ast.Ident]types.Instance), + Uses: make(map[*ast.Ident]types.Object), + Defs: make(map[*ast.Ident]types.Object), + Types: make(map[ast.Expr]types.TypeAndValue), + Selections: make(map[*ast.SelectorExpr]*types.Selection), + } + // parse + f, err := parser.ParseFile(fset, "classify.go", src, 0) + if err != nil { + t.Fatal(err) + } + + // type-check + pkg, err := cfg.Check(f.Name.Name, fset, []*ast.File{f}, info) + if err != nil { + t.Fatal(err) + } + + lookup := func(sym string) types.Object { + return pkg.Scope().Lookup(sym) + } + + member := func(sym, fieldOrMethod string) types.Object { + obj, _, _ := types.LookupFieldOrMethod(lookup(sym).Type(), false, pkg, fieldOrMethod) + return obj + } + + printlnObj := types.Universe.Lookup("println") + + // Expected Calls are in the order of CallExprs at the end of src, above. + wants := []struct { + kind ti.CallKind + obj types.Object + }{ + {ti.CallStatic, lookup("g")}, // g + {ti.CallDynamic, nil}, // f + {ti.CallBuiltin, printlnObj}, // println + {ti.CallStatic, member("S", "g")}, // z.g + {ti.CallStatic, member("S", "g")}, // a.b.c.g + {ti.CallStatic, member("S", "g")}, // S.g(z, 1) + {ti.CallDynamic, nil}, // z.f + {ti.CallInterface, member("I", "m")}, // I(nil).m + {ti.CallConversion, nil}, // I(nil) + {ti.CallDynamic, nil}, // m[0] + {ti.CallDynamic, nil}, // n[0] + {ti.CallStatic, lookup("F")}, // F[int] + {ti.CallStatic, lookup("F")}, // F[T] + {ti.CallDynamic, nil}, // f(){} + {ti.CallConversion, nil}, // []byte + {ti.CallConversion, nil}, // A[int] + {ti.CallConversion, nil}, // T + {ti.CallStatic, member("S", "g")}, // (z.g) + {ti.CallStatic, member("S", "g")}, // (z).g + } + + i := 0 + ast.Inspect(f, func(n ast.Node) bool { + if call, ok := n.(*ast.CallExpr); ok { + if i >= len(wants) { + t.Fatal("more calls than wants") + } + var buf bytes.Buffer + if err := format.Node(&buf, fset, n); err != nil { + t.Fatal(err) + } + prefix := fmt.Sprintf("%s (#%d)", buf.String(), i) + + gotKind, gotObj := ti.ClassifyCall(info, call) + want := wants[i] + + if gotKind != want.kind { + t.Errorf("%s kind: got %s, want %s", prefix, gotKind, want.kind) + } + if gotObj != want.obj { + t.Errorf("%s obj: got %v (%[2]T), want %v", prefix, gotObj, want.obj) + } + i++ + } + return true + }) + if i != len(wants) { + t.Fatal("more wants than calls") + } +} From f3a6b96d653fc1b187ba386b7ea7a286a1cd527e Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Tue, 1 Apr 2025 01:21:27 -0600 Subject: [PATCH 285/309] gopls/internal/analysis/modernize: add modernizer for WaitGroup.Go This CL supports a modernizer to replace old complex usages of WaitGroup by WaitGroup.Go from go1.25. Fixes: golang/go#73059 Change-Id: I8e2f8df0cca0fae4996d2a46c6a8229cf1d37e2c Reviewed-on: https://go-review.googlesource.com/c/tools/+/661775 Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee --- gopls/doc/analyzers.md | 2 + gopls/internal/analysis/modernize/doc.go | 2 + .../internal/analysis/modernize/modernize.go | 8 +- .../analysis/modernize/modernize_test.go | 1 + .../testdata/src/waitgroup/waitgroup.go | 152 ++++++++++++++++++ .../src/waitgroup/waitgroup.go.golden | 143 ++++++++++++++++ .../testdata/src/waitgroup/waitgroup_alias.go | 21 +++ .../src/waitgroup/waitgroup_alias.go.golden | 19 +++ .../testdata/src/waitgroup/waitgroup_dot.go | 22 +++ .../src/waitgroup/waitgroup_dot.go.golden | 20 +++ .../internal/analysis/modernize/waitgroup.go | 131 +++++++++++++++ gopls/internal/doc/api.json | 4 +- 12 files changed, 522 insertions(+), 3 deletions(-) create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go.golden create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go.golden create mode 100644 gopls/internal/analysis/modernize/waitgroup.go diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 4ec7fcbd1d0..82b0e8753f9 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -553,6 +553,8 @@ Categories of modernize diagnostic: - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, added to the strings package in go1.20. + - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25. + Default: on. Package documentation: [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 354bf0955d3..7bcde40f900 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -85,4 +85,6 @@ // // - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, // added to the strings package in go1.20. +// +// - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25. package modernize diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index ebf83ab1bc3..dbef72fe5cf 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -93,6 +93,7 @@ func run(pass *analysis.Pass) (any, error) { stringsseq(pass) sortslice(pass) testingContext(pass) + waitgroup(pass) // TODO(adonovan): opt: interleave these micro-passes within a single inspection. @@ -121,7 +122,12 @@ func formatExprs(fset *token.FileSet, exprs []ast.Expr) string { // isZeroIntLiteral reports whether e is an integer whose value is 0. func isZeroIntLiteral(info *types.Info, e ast.Expr) bool { - return info.Types[e].Value == constant.MakeInt64(0) + return isIntLiteral(info, e, 0) +} + +// isIntLiteral reports whether e is an integer with given value. +func isIntLiteral(info *types.Info, e ast.Expr, n int64) bool { + return info.Types[e].Value == constant.MakeInt64(n) } // filesUsing returns a cursor for each *ast.File in the inspector diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index 9f17d159073..e9f91f2262c 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -29,5 +29,6 @@ func Test(t *testing.T) { "fieldsseq", "sortslice", "testingcontext", + "waitgroup", ) } diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go new file mode 100644 index 00000000000..8269235bda7 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go @@ -0,0 +1,152 @@ +package waitgroup + +import ( + "fmt" + "sync" +) + +// supported case for pattern 1. +func _() { + var wg sync.WaitGroup + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + }() + + for range 10 { + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + fmt.Println() + }() + } +} + +// supported case for pattern 2. +func _() { + var wg sync.WaitGroup + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + fmt.Println() + wg.Done() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + wg.Done() + }() + + for range 10 { + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + fmt.Println() + wg.Done() + }() + } +} + +// this function puts some wrong usages but waitgroup modernizer will still offer fixes. +func _() { + var wg sync.WaitGroup + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + fmt.Println() + wg.Done() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + fmt.Println() + wg.Done() + wg.Done() + }() +} + +// this function puts the unsupported cases of pattern 1. +func _() { + var wg sync.WaitGroup + wg.Add(1) + go func() {}() + + wg.Add(1) + go func(i int) { + defer wg.Done() + fmt.Println(i) + }(1) + + wg.Add(1) + go func() { + fmt.Println() + defer wg.Done() + }() + + wg.Add(1) + go func() { // noop: no wg.Done call inside function body. + fmt.Println() + }() + + go func() { // noop: no Add call before this go stmt. + defer wg.Done() + fmt.Println() + }() + + wg.Add(2) // noop: only support Add(1). + go func() { + defer wg.Done() + }() + + var wg1 sync.WaitGroup + wg1.Add(1) // noop: Add and Done should be the same object. + go func() { + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // noop: Add and Done should be the same object. + go func() { + defer wg1.Done() + fmt.Println() + }() +} + +// this function puts the unsupported cases of pattern 2. +func _() { + var wg sync.WaitGroup + wg.Add(1) + go func() { + wg.Done() + fmt.Println() + }() + + go func() { // noop: no Add call before this go stmt. + fmt.Println() + wg.Done() + }() + + var wg1 sync.WaitGroup + wg1.Add(1) // noop: Add and Done should be the same object. + go func() { + fmt.Println() + wg.Done() + }() + + wg.Add(1) // noop: Add and Done should be the same object. + go func() { + fmt.Println() + wg1.Done() + }() +} diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go.golden b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go.golden new file mode 100644 index 00000000000..dd98429da0d --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup.go.golden @@ -0,0 +1,143 @@ +package waitgroup + +import ( + "fmt" + "sync" +) + +// supported case for pattern 1. +func _() { + var wg sync.WaitGroup + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + }) + + for range 10 { + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + } +} + +// supported case for pattern 2. +func _() { + var wg sync.WaitGroup + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + }) + + for range 10 { + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + } +} + +// this function puts some wrong usages but waitgroup modernizer will still offer fixes. +func _() { + var wg sync.WaitGroup + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + defer wg.Done() + fmt.Println() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + wg.Done() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + wg.Done() + }) +} + +// this function puts the unsupported cases of pattern 1. +func _() { + var wg sync.WaitGroup + wg.Add(1) + go func() {}() + + wg.Add(1) + go func(i int) { + defer wg.Done() + fmt.Println(i) + }(1) + + wg.Add(1) + go func() { + fmt.Println() + defer wg.Done() + }() + + wg.Add(1) + go func() { // noop: no wg.Done call inside function body. + fmt.Println() + }() + + go func() { // noop: no Add call before this go stmt. + defer wg.Done() + fmt.Println() + }() + + wg.Add(2) // noop: only support Add(1). + go func() { + defer wg.Done() + }() + + var wg1 sync.WaitGroup + wg1.Add(1) // noop: Add and Done should be the same object. + go func() { + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // noop: Add and Done should be the same object. + go func() { + defer wg1.Done() + fmt.Println() + }() +} + +// this function puts the unsupported cases of pattern 2. +func _() { + var wg sync.WaitGroup + wg.Add(1) + go func() { + wg.Done() + fmt.Println() + }() + + go func() { // noop: no Add call before this go stmt. + fmt.Println() + wg.Done() + }() + + var wg1 sync.WaitGroup + wg1.Add(1) // noop: Add and Done should be the same object. + go func() { + fmt.Println() + wg.Done() + }() + + wg.Add(1) // noop: Add and Done should be the same object. + go func() { + fmt.Println() + wg1.Done() + }() +} diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go new file mode 100644 index 00000000000..087edba27be --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go @@ -0,0 +1,21 @@ +package waitgroup + +import ( + "fmt" + sync1 "sync" +) + +func _() { + var wg sync1.WaitGroup + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + fmt.Println() + wg.Done() + }() +} diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go.golden b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go.golden new file mode 100644 index 00000000000..377973bc689 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_alias.go.golden @@ -0,0 +1,19 @@ +package waitgroup + +import ( + "fmt" + sync1 "sync" +) + +func _() { + var wg sync1.WaitGroup + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) +} \ No newline at end of file diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go new file mode 100644 index 00000000000..b4d1e150dbc --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go @@ -0,0 +1,22 @@ +package waitgroup + +import ( + "fmt" + . "sync" +) + +// supported case for pattern 1. +func _() { + var wg WaitGroup + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + defer wg.Done() + fmt.Println() + }() + + wg.Add(1) // want "Goroutine creation can be simplified using WaitGroup.Go" + go func() { + fmt.Println() + wg.Done() + }() +} diff --git a/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go.golden new file mode 100644 index 00000000000..37584be72f8 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/waitgroup/waitgroup_dot.go.golden @@ -0,0 +1,20 @@ +package waitgroup + +import ( + "fmt" + . "sync" +) + +// supported case for pattern 1. +func _() { + var wg WaitGroup + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) + + // want "Goroutine creation can be simplified using WaitGroup.Go" + wg.Go(func() { + fmt.Println() + }) +} \ No newline at end of file diff --git a/gopls/internal/analysis/modernize/waitgroup.go b/gopls/internal/analysis/modernize/waitgroup.go new file mode 100644 index 00000000000..37a12da5657 --- /dev/null +++ b/gopls/internal/analysis/modernize/waitgroup.go @@ -0,0 +1,131 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modernize + +import ( + "fmt" + "go/ast" + "slices" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + "golang.org/x/tools/internal/analysisinternal" + typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" + "golang.org/x/tools/internal/astutil/cursor" + "golang.org/x/tools/internal/typesinternal/typeindex" +) + +// The waitgroup pass replaces old more complex code with +// go1.25 added API WaitGroup.Go. +// +// Patterns: +// +// 1. wg.Add(1); go func() { defer wg.Done(); ... }() +// => +// wg.Go(go func() { ... }) +// +// 2. wg.Add(1); go func() { ...; wg.Done() }() +// => +// wg.Go(go func() { ... }) +// +// The wg.Done must occur within the first statement of the block in a defer format or last statement of the block, +// and the offered fix only removes the first/last wg.Done call. It doesn't fix the existing wrong usage of sync.WaitGroup. +func waitgroup(pass *analysis.Pass) { + var ( + inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) + info = pass.TypesInfo + syncWaitGroup = index.Object("sync", "WaitGroup") + syncWaitGroupAdd = index.Selection("sync", "WaitGroup", "Add") + syncWaitGroupDone = index.Selection("sync", "WaitGroup", "Done") + ) + if !index.Used(syncWaitGroup, syncWaitGroupAdd, syncWaitGroupDone) { + return + } + + checkWaitGroup := func(file *ast.File, curGostmt cursor.Cursor) { + gostmt := curGostmt.Node().(*ast.GoStmt) + + lit, ok := gostmt.Call.Fun.(*ast.FuncLit) + // go statement must have a no-arg function literal. + if !ok || len(gostmt.Call.Args) != 0 { + return + } + + // previous node must call wg.Add. + prev, ok := curGostmt.PrevSibling() + if !ok { + return + } + prevNode := prev.Node() + if !is[*ast.ExprStmt](prevNode) || !is[*ast.CallExpr](prevNode.(*ast.ExprStmt).X) { + return + } + + prevCall := prevNode.(*ast.ExprStmt).X.(*ast.CallExpr) + if typeutil.Callee(info, prevCall) != syncWaitGroupAdd || !isIntLiteral(info, prevCall.Args[0], 1) { + return + } + + addCallRecv := ast.Unparen(prevCall.Fun).(*ast.SelectorExpr).X + list := lit.Body.List + if len(list) == 0 { + return + } + + var doneStmt ast.Stmt + if deferStmt, ok := list[0].(*ast.DeferStmt); ok && + typeutil.Callee(info, deferStmt.Call) == syncWaitGroupDone && + equalSyntax(ast.Unparen(deferStmt.Call.Fun).(*ast.SelectorExpr).X, addCallRecv) { + // wg.Add(1); go func() { defer wg.Done(); ... }() + // --------- ------ --------------- - + // wg.Go(func() { ... } ) + doneStmt = deferStmt + } else if lastStmt, ok := list[len(list)-1].(*ast.ExprStmt); ok { + if doneCall, ok := lastStmt.X.(*ast.CallExpr); ok && + typeutil.Callee(info, doneCall) == syncWaitGroupDone && + equalSyntax(ast.Unparen(doneCall.Fun).(*ast.SelectorExpr).X, addCallRecv) { + // wg.Add(1); go func() { ... ;wg.Done();}() + // --------- ------ ---------- - + // wg.Go(func() { ... } ) + doneStmt = lastStmt + } + } + if doneStmt != nil { + pass.Report(analysis.Diagnostic{ + Pos: prevNode.Pos(), + End: gostmt.End(), + Category: "waitgroup", + Message: "Goroutine creation can be simplified using WaitGroup.Go", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Simplify by using WaitGroup.Go", + TextEdits: slices.Concat( + analysisinternal.DeleteStmt(pass.Fset, file, prevNode.(*ast.ExprStmt), nil), + analysisinternal.DeleteStmt(pass.Fset, file, doneStmt, nil), + []analysis.TextEdit{ + { + Pos: gostmt.Pos(), + End: gostmt.Call.Pos(), + NewText: fmt.Appendf(nil, "%s.Go(", addCallRecv), + }, + { + Pos: gostmt.Call.Lparen, + End: gostmt.Call.Rparen, + }, + }, + ), + }}, + }) + } + } + + for curFile := range filesUsing(inspect, info, "go1.25") { + for curGostmt := range curFile.Preorder((*ast.GoStmt)(nil)) { + checkWaitGroup(curFile.Node().(*ast.File), curGostmt) + } + } +} diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index f731e0d7984..9dc7aef266d 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -562,7 +562,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", "Default": "true", "Status": "" }, @@ -1338,7 +1338,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From ead1fea4b0600fc954c44ea316557ef6eb0c6b72 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Tue, 1 Apr 2025 23:09:43 -0600 Subject: [PATCH 286/309] internal/analysis/modernize: add nil check before comparing with index object This CL introduces an additional nil check before comparing with the output of index.Object to prevent false-positive matches and avoid runtime errors. Running modernize on go/analysis/passes/buildtag/buildtag.go could reproduce the error. This issue occurs because one of bytesTrimPrefix and stringsTrimPrefix is nil and obj1 is also nil, leading to a false postive match and a runtime error "index out of range". This CL also separates the test cases to import either strings or bytes at a time, which helps prevent similar issues in the future. Change-Id: Iafbd38a55a0a2e0c39a2a418cbd571c67dbe50f0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/661995 Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- .../analysis/modernize/modernize_test.go | 1 + .../analysis/modernize/stringscutprefix.go | 6 ++- .../bytescutprefix/bytescutprefix.go | 16 ++++++++ .../bytescutprefix/bytescutprefix.go.golden | 16 ++++++++ .../bytescutprefix_dot.go | 4 +- .../bytescutprefix_dot.go.golden | 4 +- .../src/stringscutprefix/stringscutprefix.go | 12 +----- .../stringscutprefix.go.golden | 40 +++++++------------ .../stringscutprefix_dot.go.golden | 2 +- 9 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go create mode 100644 gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go.golden rename gopls/internal/analysis/modernize/testdata/src/stringscutprefix/{ => bytescutprefix}/bytescutprefix_dot.go (81%) rename gopls/internal/analysis/modernize/testdata/src/stringscutprefix/{ => bytescutprefix}/bytescutprefix_dot.go.golden (81%) diff --git a/gopls/internal/analysis/modernize/modernize_test.go b/gopls/internal/analysis/modernize/modernize_test.go index e9f91f2262c..e823e983995 100644 --- a/gopls/internal/analysis/modernize/modernize_test.go +++ b/gopls/internal/analysis/modernize/modernize_test.go @@ -25,6 +25,7 @@ func Test(t *testing.T) { "slicescontains", "slicesdelete", "stringscutprefix", + "stringscutprefix/bytescutprefix", "splitseq", "fieldsseq", "sortslice", diff --git a/gopls/internal/analysis/modernize/stringscutprefix.go b/gopls/internal/analysis/modernize/stringscutprefix.go index 9e9239c0f21..cd053539910 100644 --- a/gopls/internal/analysis/modernize/stringscutprefix.go +++ b/gopls/internal/analysis/modernize/stringscutprefix.go @@ -72,10 +72,12 @@ func stringscutprefix(pass *analysis.Pass) { for curCall := range firstStmt.Preorder((*ast.CallExpr)(nil)) { call1 := curCall.Node().(*ast.CallExpr) obj1 := typeutil.Callee(info, call1) - if obj1 != stringsTrimPrefix && obj1 != bytesTrimPrefix { + // bytesTrimPrefix or stringsTrimPrefix might be nil if the file doesn't import it, + // so we need to ensure the obj1 is not nil otherwise the call1 is not TrimPrefix and cause a panic. + if obj1 == nil || + obj1 != stringsTrimPrefix && obj1 != bytesTrimPrefix { continue } - // Have: if strings.HasPrefix(s0, pre0) { ...strings.TrimPrefix(s, pre)... } var ( s0 = call.Args[0] diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go new file mode 100644 index 00000000000..7c5363e6c8d --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go @@ -0,0 +1,16 @@ +package bytescutprefix + +import ( + "bytes" +) + +func _() { + if bytes.HasPrefix(bss, bspre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := bytes.TrimPrefix(bss, bspre) + _ = a + } + if bytes.HasPrefix([]byte(""), []byte("")) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := bytes.TrimPrefix([]byte(""), []byte("")) + _ = a + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go.golden new file mode 100644 index 00000000000..8d41a8bf343 --- /dev/null +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix.go.golden @@ -0,0 +1,16 @@ +package bytescutprefix + +import ( + "bytes" +) + +func _() { + if after, ok := bytes.CutPrefix(bss, bspre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } + if after, ok := bytes.CutPrefix([]byte(""), []byte("")); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a := after + _ = a + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go similarity index 81% rename from gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go rename to gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go index 4da9ed52e13..bfde6b7a461 100644 --- a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go @@ -1,9 +1,11 @@ -package stringscutprefix +package bytescutprefix import ( . "bytes" ) +var bss, bspre []byte + // test supported cases of pattern 1 func _() { if HasPrefix(bss, bspre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go.golden similarity index 81% rename from gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden rename to gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go.golden index 054214cabf1..8eb562e7940 100644 --- a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix_dot.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/bytescutprefix/bytescutprefix_dot.go.golden @@ -1,9 +1,11 @@ -package stringscutprefix +package bytescutprefix import ( . "bytes" ) +var bss, bspre []byte + // test supported cases of pattern 1 func _() { if after, ok := CutPrefix(bss, bspre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go index f5f890f4171..7679bdb6e67 100644 --- a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go @@ -1,13 +1,11 @@ package stringscutprefix import ( - "bytes" "strings" ) var ( - s, pre string - bss, bspre []byte + s, pre string ) // test supported cases of pattern 1 @@ -34,14 +32,6 @@ func _() { _, _ = a, b } - if bytes.HasPrefix(bss, bspre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := bytes.TrimPrefix(bss, bspre) - _ = a - } - if bytes.HasPrefix([]byte(""), []byte("")) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := bytes.TrimPrefix([]byte(""), []byte("")) - _ = a - } var a, b string if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" a, b = "", strings.TrimPrefix(s, "") diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden index d8b7b2ba47f..a6c52b08802 100644 --- a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix.go.golden @@ -1,27 +1,25 @@ package stringscutprefix import ( - "bytes" "strings" ) var ( - s, pre string - bss, bspre []byte + s, pre string ) // test supported cases of pattern 1 func _() { if after, ok := strings.CutPrefix(s, pre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := after + a := after _ = a } if after, ok := strings.CutPrefix("", ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := after + a := after _ = a } if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - println([]byte(after)) + println([]byte(after)) } if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" a, b := "", after @@ -34,19 +32,11 @@ func _() { _, _ = a, b } - if after, ok := bytes.CutPrefix(bss, bspre); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := after - _ = a - } - if after, ok := bytes.CutPrefix([]byte(""), []byte("")); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a := after - _ = a - } - var a, b string - if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" - a, b = "", after - _, _ = a, b - } + var a, b string + if after, ok := strings.CutPrefix(s, ""); ok { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" + a, b = "", after + _, _ = a, b + } } // test cases that are not supported by pattern1 @@ -81,12 +71,12 @@ func _() { if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" println(after) } - if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" - println(strings.TrimPrefix(s, pre)) // noop here - } - if after, ok := strings.CutPrefix(s, ""); ok { // want "TrimPrefix can be simplified to CutPrefix" - println(after) - } + if after, ok := strings.CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(strings.TrimPrefix(s, pre)) // noop here + } + if after, ok := strings.CutPrefix(s, ""); ok { // want "TrimPrefix can be simplified to CutPrefix" + println(after) + } var ok bool // define an ok variable to test the fix won't shadow it for its if stmt body _ = ok if after, ok0 := strings.CutPrefix(s, pre); ok0 { // want "TrimPrefix can be simplified to CutPrefix" diff --git a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden index b5f97b3695a..50e3b6ff0ca 100644 --- a/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/stringscutprefix/stringscutprefix_dot.go.golden @@ -15,7 +15,7 @@ func _() { // test supported cases of pattern2 func _() { if after, ok := CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" - println(after) + println(after) } if after, ok := CutPrefix(s, pre); ok { // want "TrimPrefix can be simplified to CutPrefix" println(after) From 255cfd76c54799184664fbf1e87d643aec61c429 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Tue, 1 Apr 2025 00:58:23 -0600 Subject: [PATCH 287/309] gopls: automatically insert package clause for new go files This CL introduces a new feature in gopls to handle didCreateFiles requests from the client. When a new Go file is created, gopls will automatically insert the appropriate package clause at the beginning of the file, streamlining the file initialization process. Updates golang/go#72930 Change-Id: I72277294764300bc81f6c8d17ce54b7ed2cc55eb Reviewed-on: https://go-review.googlesource.com/c/tools/+/659595 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- gopls/doc/release/v0.19.0.md | 7 + gopls/internal/cmd/cmd.go | 14 +- gopls/internal/golang/addtest.go | 2 +- gopls/internal/golang/completion/newfile.go | 65 ++++++++ gopls/internal/golang/completion/package.go | 19 +++ gopls/internal/golang/extracttofile.go | 2 +- gopls/internal/golang/util.go | 4 +- gopls/internal/server/general.go | 9 ++ gopls/internal/server/unimplemented.go | 4 - gopls/internal/server/workspace.go | 30 ++++ .../internal/test/integration/fake/editor.go | 13 ++ .../workspace/didcreatefiles_test.go | 146 ++++++++++++++++++ gopls/internal/test/integration/wrappers.go | 8 + 13 files changed, 310 insertions(+), 13 deletions(-) create mode 100644 gopls/internal/golang/completion/newfile.go create mode 100644 gopls/internal/test/integration/workspace/didcreatefiles_test.go diff --git a/gopls/doc/release/v0.19.0.md b/gopls/doc/release/v0.19.0.md index 149a474244a..f6208417ebc 100644 --- a/gopls/doc/release/v0.19.0.md +++ b/gopls/doc/release/v0.19.0.md @@ -39,3 +39,10 @@ TODO: implement global. This code action, available on a dotted import, will offer to replace the import with a regular one and qualify each use of the package with its name. + +### Auto-complete package clause for new Go files + +Gopls now automatically adds the appropriate `package` clause to newly created Go files, +so that you can immediately get started writing the interesting part. + +It requires client support for `workspace/didCreateFiles` \ No newline at end of file diff --git a/gopls/internal/cmd/cmd.go b/gopls/internal/cmd/cmd.go index 4a00afc4115..fed96388fb4 100644 --- a/gopls/internal/cmd/cmd.go +++ b/gopls/internal/cmd/cmd.go @@ -343,7 +343,8 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti // Make sure to respect configured options when sending initialize request. opts := settings.DefaultOptions(options) - // If you add an additional option here, you must update the map key in connect. + // If you add an additional option here, + // you must update the map key of settings.DefaultOptions called in (*Application).connect. params.Capabilities.TextDocument.Hover = &protocol.HoverClientCapabilities{ ContentFormat: []protocol.MarkupKind{opts.PreferredContentFormat}, } @@ -351,7 +352,7 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti params.Capabilities.TextDocument.SemanticTokens = protocol.SemanticTokensClientCapabilities{} params.Capabilities.TextDocument.SemanticTokens.Formats = []protocol.TokenFormat{"relative"} params.Capabilities.TextDocument.SemanticTokens.Requests.Range = &protocol.Or_ClientSemanticTokensRequestOptions_range{Value: true} - //params.Capabilities.TextDocument.SemanticTokens.Requests.Range.Value = true + // params.Capabilities.TextDocument.SemanticTokens.Requests.Range.Value = true params.Capabilities.TextDocument.SemanticTokens.Requests.Full = &protocol.Or_ClientSemanticTokensRequestOptions_full{Value: true} params.Capabilities.TextDocument.SemanticTokens.TokenTypes = moreslices.ConvertStrings[string](semtok.TokenTypes) params.Capabilities.TextDocument.SemanticTokens.TokenModifiers = moreslices.ConvertStrings[string](semtok.TokenModifiers) @@ -363,6 +364,9 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti }, } params.Capabilities.Window.WorkDoneProgress = true + params.Capabilities.Workspace.FileOperations = &protocol.FileOperationClientCapabilities{ + DidCreate: true, + } params.InitializationOptions = map[string]any{ "symbolMatcher": string(opts.SymbolMatcher), @@ -817,10 +821,10 @@ func (c *connection) diagnoseFiles(ctx context.Context, files []protocol.Documen } func (c *connection) terminate(ctx context.Context) { - //TODO: do we need to handle errors on these calls? + // TODO: do we need to handle errors on these calls? c.Shutdown(ctx) - //TODO: right now calling exit terminates the process, we should rethink that - //server.Exit(ctx) + // TODO: right now calling exit terminates the process, we should rethink that + // server.Exit(ctx) } // Implement io.Closer. diff --git a/gopls/internal/golang/addtest.go b/gopls/internal/golang/addtest.go index 4a43a82ffee..e952874e109 100644 --- a/gopls/internal/golang/addtest.go +++ b/gopls/internal/golang/addtest.go @@ -319,7 +319,7 @@ func AddTestForFunc(ctx context.Context, snapshot *cache.Snapshot, loc protocol. // package decl based on the originating file. // Search for something that looks like a copyright header, to replicate // in the new file. - if c := copyrightComment(pgf.File); c != nil { + if c := CopyrightComment(pgf.File); c != nil { start, end, err := pgf.NodeOffsets(c) if err != nil { return nil, err diff --git a/gopls/internal/golang/completion/newfile.go b/gopls/internal/golang/completion/newfile.go new file mode 100644 index 00000000000..d9869a2f050 --- /dev/null +++ b/gopls/internal/golang/completion/newfile.go @@ -0,0 +1,65 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package completion + +import ( + "bytes" + "context" + "fmt" + + "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/cache/parsego" + "golang.org/x/tools/gopls/internal/file" + "golang.org/x/tools/gopls/internal/golang" + "golang.org/x/tools/gopls/internal/protocol" +) + +// NewFile returns a document change to complete an empty go file. +func NewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) (*protocol.DocumentChange, error) { + if bs, err := fh.Content(); err != nil || len(bs) != 0 { + return nil, err + } + meta, err := golang.NarrowestMetadataForFile(ctx, snapshot, fh.URI()) + if err != nil { + return nil, err + } + var buf bytes.Buffer + // Copy the copyright header from the first existing file that has one. + for _, fileURI := range meta.GoFiles { + if fileURI == fh.URI() { + continue + } + fh, err := snapshot.ReadFile(ctx, fileURI) + if err != nil { + continue + } + pgf, err := snapshot.ParseGo(ctx, fh, parsego.Header) + if err != nil { + continue + } + if group := golang.CopyrightComment(pgf.File); group != nil { + start, end, err := pgf.NodeOffsets(group) + if err != nil { + continue + } + buf.Write(pgf.Src[start:end]) + buf.WriteString("\n\n") + break + } + } + + pkgName, err := bestPackage(ctx, snapshot, fh.URI()) + if err != nil { + return nil, err + } + + fmt.Fprintf(&buf, "package %s\n", pkgName) + change := protocol.DocumentChangeEdit(fh, []protocol.TextEdit{{ + Range: protocol.Range{}, // insert at start of file + NewText: buf.String(), + }}) + + return &change, nil +} diff --git a/gopls/internal/golang/completion/package.go b/gopls/internal/golang/completion/package.go index 01d5622c7f7..d1698ee6580 100644 --- a/gopls/internal/golang/completion/package.go +++ b/gopls/internal/golang/completion/package.go @@ -15,6 +15,7 @@ import ( "go/token" "go/types" "path/filepath" + "sort" "strings" "unicode" @@ -27,6 +28,24 @@ import ( "golang.org/x/tools/gopls/internal/util/safetoken" ) +// bestPackage offers the best package name for a package declaration when +// one is not present in the given file. +func bestPackage(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (string, error) { + suggestions, err := packageSuggestions(ctx, snapshot, uri, "") + if err != nil { + return "", err + } + // sort with the same way of sortItems. + sort.SliceStable(suggestions, func(i, j int) bool { + if suggestions[i].score != suggestions[j].score { + return suggestions[i].score > suggestions[j].score + } + return suggestions[i].name < suggestions[j].name + }) + + return suggestions[0].name, nil +} + // packageClauseCompletions offers completions for a package declaration when // one is not present in the given file. func packageClauseCompletions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) ([]CompletionItem, *Selection, error) { diff --git a/gopls/internal/golang/extracttofile.go b/gopls/internal/golang/extracttofile.go index 39fb28e624b..d3026d4ee0f 100644 --- a/gopls/internal/golang/extracttofile.go +++ b/gopls/internal/golang/extracttofile.go @@ -138,7 +138,7 @@ func ExtractToNewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Han } var buf bytes.Buffer - if c := copyrightComment(pgf.File); c != nil { + if c := CopyrightComment(pgf.File); c != nil { start, end, err := pgf.NodeOffsets(c) if err != nil { return nil, err diff --git a/gopls/internal/golang/util.go b/gopls/internal/golang/util.go index a81ff3fbe58..b13056e02b9 100644 --- a/gopls/internal/golang/util.go +++ b/gopls/internal/golang/util.go @@ -361,9 +361,9 @@ func AbbreviateVarName(s string) string { return b.String() } -// copyrightComment returns the copyright comment group from the input file, or +// CopyrightComment returns the copyright comment group from the input file, or // nil if not found. -func copyrightComment(file *ast.File) *ast.CommentGroup { +func CopyrightComment(file *ast.File) *ast.CommentGroup { if len(file.Comments) == 0 { return nil } diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go index b7b69931103..7368206f578 100644 --- a/gopls/internal/server/general.go +++ b/gopls/internal/server/general.go @@ -189,6 +189,15 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ Supported: true, ChangeNotifications: "workspace/didChangeWorkspaceFolders", }, + FileOperations: &protocol.FileOperationOptions{ + DidCreate: &protocol.FileOperationRegistrationOptions{ + Filters: []protocol.FileOperationFilter{{ + Scheme: "file", + // gopls is only interested with files in .go extension. + Pattern: protocol.FileOperationPattern{Glob: "**/*.go"}, + }}, + }, + }, }, }, ServerInfo: &protocol.ServerInfo{ diff --git a/gopls/internal/server/unimplemented.go b/gopls/internal/server/unimplemented.go index 7375dc4bb1b..d3bb07cb647 100644 --- a/gopls/internal/server/unimplemented.go +++ b/gopls/internal/server/unimplemented.go @@ -34,10 +34,6 @@ func (s *server) DidCloseNotebookDocument(context.Context, *protocol.DidCloseNot return notImplemented("DidCloseNotebookDocument") } -func (s *server) DidCreateFiles(context.Context, *protocol.CreateFilesParams) error { - return notImplemented("DidCreateFiles") -} - func (s *server) DidDeleteFiles(context.Context, *protocol.DeleteFilesParams) error { return notImplemented("DidDeleteFiles") } diff --git a/gopls/internal/server/workspace.go b/gopls/internal/server/workspace.go index 84e663c1049..8074ecca444 100644 --- a/gopls/internal/server/workspace.go +++ b/gopls/internal/server/workspace.go @@ -12,6 +12,8 @@ import ( "sync" "golang.org/x/tools/gopls/internal/cache" + "golang.org/x/tools/gopls/internal/file" + "golang.org/x/tools/gopls/internal/golang/completion" "golang.org/x/tools/gopls/internal/protocol" "golang.org/x/tools/gopls/internal/settings" "golang.org/x/tools/internal/event" @@ -139,3 +141,31 @@ func (s *server) DidChangeConfiguration(ctx context.Context, _ *protocol.DidChan return nil } + +func (s *server) DidCreateFiles(ctx context.Context, params *protocol.CreateFilesParams) error { + ctx, done := event.Start(ctx, "lsp.Server.didCreateFiles") + defer done() + + var allChanges []protocol.DocumentChange + for _, createdFile := range params.Files { + uri := protocol.DocumentURI(createdFile.URI) + fh, snapshot, release, err := s.fileOf(ctx, uri) + if err != nil { + event.Error(ctx, "fail to call fileOf", err) + continue + } + defer release() + + switch snapshot.FileKind(fh) { + case file.Go: + change, err := completion.NewFile(ctx, snapshot, fh) + if err != nil { + continue + } + allChanges = append(allChanges, *change) + default: + } + } + + return applyChanges(ctx, s.client, allChanges) +} diff --git a/gopls/internal/test/integration/fake/editor.go b/gopls/internal/test/integration/fake/editor.go index 170a9823cad..01f3de8aba9 100644 --- a/gopls/internal/test/integration/fake/editor.go +++ b/gopls/internal/test/integration/fake/editor.go @@ -1309,6 +1309,19 @@ func (e *Editor) Completion(ctx context.Context, loc protocol.Location) (*protoc return completions, nil } +func (e *Editor) DidCreateFiles(ctx context.Context, files ...protocol.DocumentURI) error { + if e.Server == nil { + return nil + } + params := &protocol.CreateFilesParams{} + for _, file := range files { + params.Files = append(params.Files, protocol.FileCreate{ + URI: string(file), + }) + } + return e.Server.DidCreateFiles(ctx, params) +} + func (e *Editor) SetSuggestionInsertReplaceMode(_ context.Context, useReplaceMode bool) { e.mu.Lock() defer e.mu.Unlock() diff --git a/gopls/internal/test/integration/workspace/didcreatefiles_test.go b/gopls/internal/test/integration/workspace/didcreatefiles_test.go new file mode 100644 index 00000000000..cba0daf472e --- /dev/null +++ b/gopls/internal/test/integration/workspace/didcreatefiles_test.go @@ -0,0 +1,146 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package workspace + +import ( + "context" + "fmt" + "testing" + + . "golang.org/x/tools/gopls/internal/test/integration" +) + +// TestAutoFillPackageDecl tests that creation of a new .go file causes +// gopls to choose a sensible package name and fill in the package declaration. +func TestAutoFillPackageDecl(t *testing.T) { + const existFiles = ` +-- go.mod -- +module mod.com + +go 1.12 + +-- dog/a_test.go -- +package dog +-- fruits/apple.go -- +package apple + +fun apple() int { + return 0 +} + +-- license/license.go -- +/* Copyright 2025 The Go Authors. All rights reserved. +Use of this source code is governed by a BSD-style +license that can be found in the LICENSE file. */ + +package license + +-- license1/license.go -- +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package license1 + +-- cmd/main.go -- +package main + +-- integration/a_test.go -- +package integration_test + +-- nopkg/testfile.go -- +package +` + for _, tc := range []struct { + name string + newfile string + want string + }{ + { + name: "new file in folder with a_test.go", + newfile: "dog/newfile.go", + want: "package dog\n", + }, + { + name: "new file in folder with go file", + newfile: "fruits/newfile.go", + want: "package apple\n", + }, + { + name: "new test file in folder with go file", + newfile: "fruits/newfile_test.go", + want: "package apple\n", + }, + { + name: "new file in folder with go file that contains license comment", + newfile: "license/newfile.go", + want: `/* Copyright 2025 The Go Authors. All rights reserved. +Use of this source code is governed by a BSD-style +license that can be found in the LICENSE file. */ + +package license +`, + }, + { + name: "new file in folder with go file that contains license comment", + newfile: "license1/newfile.go", + want: `// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package license1 +`, + }, + { + name: "new file in folder with main package", + newfile: "cmd/newfile.go", + want: "package main\n", + }, + { + name: "new file in empty folder", + newfile: "empty_folder/newfile.go", + want: "package emptyfolder\n", + }, + { + name: "new file in folder with integration_test package", + newfile: "integration/newfile.go", + want: "package integration\n", + }, + { + name: "new test file in folder with integration_test package", + newfile: "integration/newfile_test.go", + want: "package integration\n", + }, + { + name: "new file in folder with incomplete package clause", + newfile: "incomplete/newfile.go", + want: "package incomplete\n", + }, + { + name: "package completion for dir name with punctuation", + newfile: "123f_r.u~its-123/newfile.go", + want: "package fruits123\n", + }, + { + name: "package completion for dir name with invalid dir name", + newfile: "123f_r.u~its-123/newfile.go", + want: "package fruits123\n", + }, + } { + t.Run(tc.name, func(t *testing.T) { + createFiles := fmt.Sprintf("%s\n-- %s --", existFiles, tc.newfile) + Run(t, createFiles, func(t *testing.T, env *Env) { + env.DidCreateFiles(env.Editor.DocumentURI(tc.newfile)) + // save buffer to ensure the edits take effects in the file system. + if err := env.Editor.SaveBuffer(context.Background(), tc.newfile); err != nil { + t.Fatal(err) + } + if got := env.FileContent(tc.newfile); tc.want != got { + t.Fatalf("want '%s' but got '%s'", tc.want, got) + } + }) + }) + } +} diff --git a/gopls/internal/test/integration/wrappers.go b/gopls/internal/test/integration/wrappers.go index 6389cdb74e8..17e0cf329c4 100644 --- a/gopls/internal/test/integration/wrappers.go +++ b/gopls/internal/test/integration/wrappers.go @@ -531,6 +531,14 @@ func (e *Env) Completion(loc protocol.Location) *protocol.CompletionList { return completions } +func (e *Env) DidCreateFiles(files ...protocol.DocumentURI) { + e.TB.Helper() + err := e.Editor.DidCreateFiles(e.Ctx, files...) + if err != nil { + e.TB.Fatal(err) + } +} + func (e *Env) SetSuggestionInsertReplaceMode(useReplaceMode bool) { e.TB.Helper() e.Editor.SetSuggestionInsertReplaceMode(e.Ctx, useReplaceMode) From 97789e843eb9b75f676253a35cf0b4b2ed529ec2 Mon Sep 17 00:00:00 2001 From: alingse Date: Sun, 30 Mar 2025 14:22:48 +0800 Subject: [PATCH 288/309] gopls/internal/lsprpc: fix call function with wrong err Change-Id: I60dff0375e18d45ec074498ade25e89c7b0ac6b7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/661695 Reviewed-by: Carlos Amedee Auto-Submit: Carlos Amedee LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- gopls/internal/lsprpc/lsprpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopls/internal/lsprpc/lsprpc.go b/gopls/internal/lsprpc/lsprpc.go index 9255f9176bc..3d26bdd6896 100644 --- a/gopls/internal/lsprpc/lsprpc.go +++ b/gopls/internal/lsprpc/lsprpc.go @@ -392,7 +392,7 @@ func (f *forwarder) replyWithDebugAddress(outerCtx context.Context, r jsonrpc2.R addr, err = di.Serve(outerCtx, addr) if err != nil { event.Error(outerCtx, "starting debug server", err) - return r(ctx, result, outerErr) + return r(ctx, result, err) } urls := []string{"http://" + addr} modified.URLs = append(urls, modified.URLs...) From aee7ae56af35e2e9123b64d80813f6224781add3 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 1 Apr 2025 13:39:28 -0400 Subject: [PATCH 289/309] internal/typesinternal: support checking for full types.Info Add RequiresFullInfo, which panics if its types.Info argument is missing any maps. We will deploy this incrementally to enforce fully populated Infos across x/tools. Ultimately this will reduce bugs and panics from partially initialized Infos. Also add NewTypesInfo, which creates a types.Info with all its map fields populated. Perhaps this could live in go/types or go/types/typeutil. Change-Id: I09124eee470286c9b73d1ba17b89b63aef1abc87 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662115 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Jonathan Amsterdam --- internal/typesinternal/types.go | 36 ++++++++++++++++++++++++ internal/typesinternal/types_test.go | 41 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 internal/typesinternal/types_test.go diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index edf0347ec3b..d9ef55ebc77 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -7,9 +7,12 @@ package typesinternal import ( + "fmt" + "go/ast" "go/token" "go/types" "reflect" + "strings" "unsafe" "golang.org/x/tools/internal/aliases" @@ -127,3 +130,36 @@ func Origin(t NamedOrAlias) NamedOrAlias { func IsPackageLevel(obj types.Object) bool { return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope() } + +// NewTypesInfo returns a *types.Info with all maps populated. +func NewTypesInfo() *types.Info { + return &types.Info{ + Types: map[ast.Expr]types.TypeAndValue{}, + Instances: map[*ast.Ident]types.Instance{}, + Defs: map[*ast.Ident]types.Object{}, + Uses: map[*ast.Ident]types.Object{}, + Implicits: map[ast.Node]types.Object{}, + Selections: map[*ast.SelectorExpr]*types.Selection{}, + Scopes: map[ast.Node]*types.Scope{}, + FileVersions: map[*ast.File]string{}, + } +} + +// RequiresFullInfo panics unless info has non-nil values for all maps. +func RequiresFullInfo(info *types.Info) { + v := reflect.ValueOf(info).Elem() + t := v.Type() + var missing []string + for i := range t.NumField() { + f := t.Field(i) + if f.Type.Kind() == reflect.Map && v.Field(i).IsNil() { + missing = append(missing, f.Name) + } + } + if len(missing) > 0 { + msg := fmt.Sprintf(`A fully populated types.Info value is required. +This one is missing the following fields: +%s`, strings.Join(missing, ", ")) + panic(msg) + } +} diff --git a/internal/typesinternal/types_test.go b/internal/typesinternal/types_test.go new file mode 100644 index 00000000000..2a715549408 --- /dev/null +++ b/internal/typesinternal/types_test.go @@ -0,0 +1,41 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typesinternal + +import ( + "fmt" + "go/ast" + "go/types" + "regexp" + "testing" +) + +func TestRequiresFullInfo(t *testing.T) { + info := &types.Info{ + Uses: map[*ast.Ident]types.Object{}, + Scopes: map[ast.Node]*types.Scope{}, + } + panics(t, "Types, Instances, Defs, Implicits, Selections, FileVersions", func() { + RequiresFullInfo(info) + }) + + // Shouldn't panic. + RequiresFullInfo(NewTypesInfo()) +} + +// panics asserts that f() panics with with a value whose printed form matches the regexp want. +// Copied from go/analysis/internal/checker/fix_test.go. +func panics(t *testing.T, want string, f func()) { + defer func() { + if x := recover(); x == nil { + t.Errorf("function returned normally, wanted panic") + } else if m, err := regexp.MatchString(want, fmt.Sprint(x)); err != nil { + t.Errorf("panics: invalid regexp %q", want) + } else if !m { + t.Errorf("function panicked with value %q, want match for %q", x, want) + } + }() + f() +} From 300a853e6a9abb7867847abc12c1f7e0be9d8d56 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 1 Apr 2025 16:48:45 -0400 Subject: [PATCH 290/309] gopls: require go1.24.2 This is needed for the backport of the fix to the race in go/types (#71817), which, though benign, causes tests to flake. Updates golang/go#71817 Fixes golang/go#72082 Change-Id: Ie1e02095b971f93fe384e830018ec13126f403d0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662036 LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- gopls/go.mod | 2 +- gopls/go.sum | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/gopls/go.mod b/gopls/go.mod index da7303222d2..5cabb7974de 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -1,6 +1,6 @@ module golang.org/x/tools/gopls -go 1.24.0 +go 1.24.2 require ( github.com/google/go-cmp v0.6.0 diff --git a/gopls/go.sum b/gopls/go.sum index 5a7914737a4..20633541388 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -15,7 +15,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGKbLGBPtR/8/oO74W6hmz0qE5q0z9aqSAewaaM= github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/dl v0.0.0-20250211172903-ae3823a6a0a3/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa h1:Br3+0EZZohShrmVVc85znGpxw7Ca8hsUJlrdT/JQGw8= From 66c560d9a060d7007d6a034a8f0c162eeab7ba27 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 2 Apr 2025 13:34:28 +0800 Subject: [PATCH 291/309] x/tools: apply modernize fixes The changes are made by running 'modernize -fix ./...' under x/tools. Change-Id: Iefe9fc799edf105b347dcef9a495ed8b12e8e6c6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662196 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Alan Donovan Reviewed-by: Robert Findley --- cmd/callgraph/main.go | 5 +--- cmd/deadcode/deadcode_test.go | 1 - cmd/file2fuzz/main.go | 2 +- cmd/godex/writetype.go | 13 +++++----- cmd/godoc/godoc_test.go | 8 ++---- cmd/godoc/main.go | 2 +- cmd/goimports/goimports.go | 4 +-- cmd/goyacc/yacc.go | 10 +++---- cmd/html2article/conv.go | 7 +++-- cmd/present/main.go | 4 +-- cmd/present2md/main.go | 8 +++--- .../internal/fuzz-generator/gen_test.go | 4 +-- .../internal/fuzz-generator/generator.go | 26 ++++++++----------- cmd/stringer/golden_test.go | 1 - container/intsets/sparse_test.go | 20 +++++++------- go/analysis/analysistest/analysistest.go | 4 +-- go/analysis/passes/composite/composite.go | 2 +- go/analysis/passes/copylock/copylock.go | 2 +- .../passes/fieldalignment/fieldalignment.go | 6 ++--- go/analysis/passes/httpmux/httpmux.go | 8 ++---- go/analysis/passes/structtag/structtag.go | 8 +++--- go/analysis/passes/testinggoroutine/util.go | 8 ++---- go/ast/astutil/imports.go | 3 ++- go/ast/astutil/rewrite_test.go | 1 - go/buildutil/allpackages.go | 2 -- go/callgraph/cha/cha_test.go | 2 +- go/callgraph/rta/rta_test.go | 4 +-- go/callgraph/vta/helpers_test.go | 2 +- go/callgraph/vta/internal/trie/op_test.go | 21 ++++++--------- go/gcexportdata/example_test.go | 9 ++----- go/gcexportdata/gcexportdata.go | 5 +--- go/packages/external.go | 2 +- go/packages/overlay_test.go | 14 +++++----- go/packages/packages_test.go | 10 +++---- go/packages/packagestest/export.go | 2 -- go/ssa/builder_test.go | 3 --- go/ssa/dom.go | 9 ++++--- go/ssa/emit.go | 2 +- go/ssa/instantiate.go | 8 ++---- go/ssa/interp/external.go | 7 +++-- go/ssa/lift.go | 8 +++--- go/ssa/sanity.go | 25 +++--------------- go/ssa/subst.go | 4 +-- go/ssa/util.go | 2 +- godoc/index.go | 7 +++-- godoc/snippet.go | 7 +++-- godoc/static/gen_test.go | 2 +- godoc/versions_test.go | 8 ++---- godoc/vfs/os.go | 7 +++-- godoc/vfs/zipfs/zipfs_test.go | 2 +- internal/bisect/bisect.go | 6 ++--- internal/diff/diff.go | 3 ++- internal/diff/diff_test.go | 4 +-- internal/diff/lcs/common_test.go | 9 +++---- internal/diff/lcs/old.go | 5 +--- internal/diff/lcs/old_test.go | 4 +-- internal/diff/ndiff.go | 2 +- internal/diffp/diff.go | 7 ++--- internal/event/label/label.go | 7 +++-- internal/gcimporter/gcimporter_test.go | 4 +-- internal/gcimporter/iexport.go | 11 ++++---- internal/gcimporter/iimport.go | 3 ++- internal/gocommand/invoke.go | 2 +- internal/gopathwalk/walk.go | 7 +++-- internal/imports/fix.go | 9 +++---- internal/imports/fix_test.go | 4 +-- internal/imports/mod.go | 5 ++-- internal/imports/mod_cache.go | 4 +-- internal/imports/mod_test.go | 13 +++------- internal/imports/sortimports.go | 5 ++-- internal/modindex/lookup.go | 4 +-- internal/packagestest/export.go | 2 -- internal/pkgbits/decoder.go | 2 +- internal/proxydir/proxydir.go | 2 +- internal/refactor/inline/calleefx_test.go | 1 - internal/refactor/inline/everything_test.go | 3 ++- internal/refactor/inline/inline.go | 5 ++-- internal/refactor/inline/inline_test.go | 4 +-- internal/testenv/testenv.go | 6 ++--- internal/typeparams/free.go | 2 +- playground/socket/socket.go | 8 ++---- playground/socket/socket_test.go | 2 +- present/link.go | 8 +++--- present/parse_test.go | 1 - refactor/eg/eg.go | 17 ++++-------- 85 files changed, 203 insertions(+), 309 deletions(-) diff --git a/cmd/callgraph/main.go b/cmd/callgraph/main.go index 9e440bbafb9..e489de883d0 100644 --- a/cmd/callgraph/main.go +++ b/cmd/callgraph/main.go @@ -148,10 +148,7 @@ func init() { // If $GOMAXPROCS isn't set, use the full capacity of the machine. // For small machines, use at least 4 threads. if os.Getenv("GOMAXPROCS") == "" { - n := runtime.NumCPU() - if n < 4 { - n = 4 - } + n := max(runtime.NumCPU(), 4) runtime.GOMAXPROCS(n) } } diff --git a/cmd/deadcode/deadcode_test.go b/cmd/deadcode/deadcode_test.go index 90c067331dc..a9b8327c7d7 100644 --- a/cmd/deadcode/deadcode_test.go +++ b/cmd/deadcode/deadcode_test.go @@ -34,7 +34,6 @@ func Test(t *testing.T) { t.Fatal(err) } for _, filename := range matches { - filename := filename t.Run(filename, func(t *testing.T) { t.Parallel() diff --git a/cmd/file2fuzz/main.go b/cmd/file2fuzz/main.go index 2a86c2ece88..f9d4708cd28 100644 --- a/cmd/file2fuzz/main.go +++ b/cmd/file2fuzz/main.go @@ -34,7 +34,7 @@ import ( var encVersion1 = "go test fuzz v1" func encodeByteSlice(b []byte) []byte { - return []byte(fmt.Sprintf("%s\n[]byte(%q)", encVersion1, b)) + return fmt.Appendf(nil, "%s\n[]byte(%q)", encVersion1, b) } func usage() { diff --git a/cmd/godex/writetype.go b/cmd/godex/writetype.go index 866f718f05f..f59760a81c6 100644 --- a/cmd/godex/writetype.go +++ b/cmd/godex/writetype.go @@ -14,6 +14,7 @@ package main import ( "go/types" + "slices" ) func (p *printer) writeType(this *types.Package, typ types.Type) { @@ -28,11 +29,9 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited // practice deeply nested composite types with unnamed component // types are uncommon. This code is likely more efficient than // using a map. - for _, t := range visited { - if t == typ { - p.printf("â—‹%T", typ) // cycle to typ - return - } + if slices.Contains(visited, typ) { + p.printf("â—‹%T", typ) // cycle to typ + return } visited = append(visited, typ) @@ -72,7 +71,7 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited p.print("struct {\n") p.indent++ - for i := 0; i < n; i++ { + for i := range n { f := t.Field(i) if !f.Anonymous() { p.printf("%s ", f.Name()) @@ -120,7 +119,7 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited if GcCompatibilityMode { // print flattened interface // (useful to compare against gc-generated interfaces) - for i := 0; i < n; i++ { + for i := range n { m := t.Method(i) p.print(m.Name()) p.writeSignatureInternal(this, m.Type().(*types.Signature), visited) diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index 66b93f10630..7cd38574233 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -16,6 +16,7 @@ import ( "os/exec" "regexp" "runtime" + "slices" "strings" "sync" "testing" @@ -127,12 +128,7 @@ func waitForServer(t *testing.T, ctx context.Context, url, match string, reverse // hasTag checks whether a given release tag is contained in the current version // of the go binary. func hasTag(t string) bool { - for _, v := range build.Default.ReleaseTags { - if t == v { - return true - } - } - return false + return slices.Contains(build.Default.ReleaseTags, t) } func TestURL(t *testing.T) { diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index a665be0769d..1bce091f269 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -114,7 +114,7 @@ func loggingHandler(h http.Handler) http.Handler { func handleURLFlag() { // Try up to 10 fetches, following redirects. urlstr := *urlFlag - for i := 0; i < 10; i++ { + for range 10 { // Prepare request. u, err := url.Parse(urlstr) if err != nil { diff --git a/cmd/goimports/goimports.go b/cmd/goimports/goimports.go index dcb5023a2e7..11f56e0e865 100644 --- a/cmd/goimports/goimports.go +++ b/cmd/goimports/goimports.go @@ -361,8 +361,8 @@ func replaceTempFilename(diff []byte, filename string) ([]byte, error) { } // Always print filepath with slash separator. f := filepath.ToSlash(filename) - bs[0] = []byte(fmt.Sprintf("--- %s%s", f+".orig", t0)) - bs[1] = []byte(fmt.Sprintf("+++ %s%s", f, t1)) + bs[0] = fmt.Appendf(nil, "--- %s%s", f+".orig", t0) + bs[1] = fmt.Appendf(nil, "+++ %s%s", f, t1) return bytes.Join(bs, []byte{'\n'}), nil } diff --git a/cmd/goyacc/yacc.go b/cmd/goyacc/yacc.go index 965a76f14dc..be084da3690 100644 --- a/cmd/goyacc/yacc.go +++ b/cmd/goyacc/yacc.go @@ -1478,7 +1478,7 @@ func symnam(i int) string { // set elements 0 through n-1 to c func aryfil(v []int, n, c int) { - for i := 0; i < n; i++ { + for i := range n { v[i] = c } } @@ -1840,7 +1840,7 @@ func closure(i int) { nexts: // initially fill the sets - for s := 0; s < n; s++ { + for s := range n { prd := curres[s] // @@ -2609,7 +2609,7 @@ func callopt() { if adb > 2 { for p = 0; p <= maxa; p += 10 { fmt.Fprintf(ftable, "%v ", p) - for i = 0; i < 10; i++ { + for i = range 10 { fmt.Fprintf(ftable, "%v ", amem[p+i]) } ftable.WriteRune('\n') @@ -2653,7 +2653,7 @@ func gin(i int) { // now, find amem place for it nextgp: - for p := 0; p < ACTSIZE; p++ { + for p := range ACTSIZE { if amem[p] != 0 { continue } @@ -3117,7 +3117,7 @@ func aryeq(a []int, b []int) int { if len(b) != n { return 0 } - for ll := 0; ll < n; ll++ { + for ll := range n { if a[ll] != b[ll] { return 0 } diff --git a/cmd/html2article/conv.go b/cmd/html2article/conv.go index 604bb1fd7cd..e2946431ce2 100644 --- a/cmd/html2article/conv.go +++ b/cmd/html2article/conv.go @@ -16,6 +16,7 @@ import ( "net/url" "os" "regexp" + "slices" "strings" "golang.org/x/net/html" @@ -270,10 +271,8 @@ func hasClass(name string) selector { return func(n *html.Node) bool { for _, a := range n.Attr { if a.Key == "class" { - for _, c := range strings.Fields(a.Val) { - if c == name { - return true - } + if slices.Contains(strings.Fields(a.Val), name) { + return true } } } diff --git a/cmd/present/main.go b/cmd/present/main.go index 340025276f9..99ed838e926 100644 --- a/cmd/present/main.go +++ b/cmd/present/main.go @@ -73,8 +73,8 @@ func main() { origin := &url.URL{Scheme: "http"} if *originHost != "" { - if strings.HasPrefix(*originHost, "https://") { - *originHost = strings.TrimPrefix(*originHost, "https://") + if after, ok := strings.CutPrefix(*originHost, "https://"); ok { + *originHost = after origin.Scheme = "https" } *originHost = strings.TrimPrefix(*originHost, "http://") diff --git a/cmd/present2md/main.go b/cmd/present2md/main.go index a11e57ecf8b..e23bb33daed 100644 --- a/cmd/present2md/main.go +++ b/cmd/present2md/main.go @@ -447,10 +447,10 @@ func parseInlineLink(s string) (link string, length int) { // If the URL is http://foo.com, drop the http:// // In other words, render [[http://golang.org]] as: // golang.org - if strings.HasPrefix(rawURL, url.Scheme+"://") { - simpleURL = strings.TrimPrefix(rawURL, url.Scheme+"://") - } else if strings.HasPrefix(rawURL, url.Scheme+":") { - simpleURL = strings.TrimPrefix(rawURL, url.Scheme+":") + if after, ok := strings.CutPrefix(rawURL, url.Scheme+"://"); ok { + simpleURL = after + } else if after, ok := strings.CutPrefix(rawURL, url.Scheme+":"); ok { + simpleURL = after } } return renderLink(rawURL, simpleURL), end + 2 diff --git a/cmd/signature-fuzzer/internal/fuzz-generator/gen_test.go b/cmd/signature-fuzzer/internal/fuzz-generator/gen_test.go index 4bd5bab7c38..f10a7e9a7df 100644 --- a/cmd/signature-fuzzer/internal/fuzz-generator/gen_test.go +++ b/cmd/signature-fuzzer/internal/fuzz-generator/gen_test.go @@ -35,7 +35,7 @@ func mkGenState() *genstate { func TestBasic(t *testing.T) { checkTunables(tunables) s := mkGenState() - for i := 0; i < 1000; i++ { + for i := range 1000 { s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic) fp := s.GenFunc(i, i) var buf bytes.Buffer @@ -58,7 +58,7 @@ func TestMoreComplicated(t *testing.T) { checkTunables(tunables) s := mkGenState() - for i := 0; i < 10000; i++ { + for i := range 10000 { s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic) fp := s.GenFunc(i, i) var buf bytes.Buffer diff --git a/cmd/signature-fuzzer/internal/fuzz-generator/generator.go b/cmd/signature-fuzzer/internal/fuzz-generator/generator.go index 6c8002f9f0c..261dd6c029b 100644 --- a/cmd/signature-fuzzer/internal/fuzz-generator/generator.go +++ b/cmd/signature-fuzzer/internal/fuzz-generator/generator.go @@ -48,6 +48,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" ) @@ -561,12 +562,7 @@ func (s *genstate) popTunables() { // See precludeSelectedTypes below for more info. func (s *genstate) redistributeFraction(toIncorporate uint8, avoid []int) { inavoid := func(j int) bool { - for _, k := range avoid { - if j == k { - return true - } - } - return false + return slices.Contains(avoid, j) } doredis := func() { @@ -631,7 +627,7 @@ func (s *genstate) GenParm(f *funcdef, depth int, mkctl bool, pidx int) parm { // Convert tf into a cumulative sum tf := s.tunables.typeFractions sum := uint8(0) - for i := 0; i < len(tf); i++ { + for i := range len(tf) { sum += tf[i] tf[i] = sum } @@ -662,7 +658,7 @@ func (s *genstate) GenParm(f *funcdef, depth int, mkctl bool, pidx int) parm { f.structdefs = append(f.structdefs, sp) tnf := int64(s.tunables.nStructFields) / int64(depth+1) nf := int(s.wr.Intn(tnf)) - for fi := 0; fi < nf; fi++ { + for range nf { fp := s.GenParm(f, depth+1, false, pidx) skComp := tunables.doSkipCompare && uint8(s.wr.Intn(100)) < s.tunables.skipCompareFraction @@ -832,7 +828,7 @@ func (s *genstate) GenFunc(fidx int, pidx int) *funcdef { needControl := f.recur f.dodefc = uint8(s.wr.Intn(100)) pTaken := uint8(s.wr.Intn(100)) < s.tunables.takenFraction - for pi := 0; pi < numParams; pi++ { + for range numParams { newparm := s.GenParm(f, 0, needControl, pidx) if !pTaken { newparm.SetAddrTaken(notAddrTaken) @@ -848,7 +844,7 @@ func (s *genstate) GenFunc(fidx int, pidx int) *funcdef { } rTaken := uint8(s.wr.Intn(100)) < s.tunables.takenFraction - for ri := 0; ri < numReturns; ri++ { + for range numReturns { r := s.GenReturn(f, 0, pidx) if !rTaken { r.SetAddrTaken(notAddrTaken) @@ -903,7 +899,7 @@ func (s *genstate) emitCompareFunc(f *funcdef, b *bytes.Buffer, p parm) { b.WriteString(" return ") numel := p.NumElements() ncmp := 0 - for i := 0; i < numel; i++ { + for i := range numel { lelref, lelparm := p.GenElemRef(i, "left") relref, _ := p.GenElemRef(i, "right") if lelref == "" || lelref == "_" { @@ -1501,7 +1497,7 @@ func (s *genstate) emitParamChecks(f *funcdef, b *bytes.Buffer, pidx int, value } else { numel := p.NumElements() cel := checkableElements(p) - for i := 0; i < numel; i++ { + for i := range numel { verb(4, "emitting check-code for p%d el %d value=%d", pi, i, value) elref, elparm := p.GenElemRef(i, s.genParamRef(p, pi)) valstr, value = s.GenValue(f, elparm, value, false) @@ -1535,7 +1531,7 @@ func (s *genstate) emitParamChecks(f *funcdef, b *bytes.Buffer, pidx int, value // receiver value check if f.isMethod { numel := f.receiver.NumElements() - for i := 0; i < numel; i++ { + for i := range numel { verb(4, "emitting check-code for rcvr el %d value=%d", i, value) elref, elparm := f.receiver.GenElemRef(i, "rcvr") valstr, value = s.GenValue(f, elparm, value, false) @@ -1608,7 +1604,7 @@ func (s *genstate) emitDeferChecks(f *funcdef, b *bytes.Buffer, value int) int { b.WriteString(" // check parm " + which + "\n") numel := p.NumElements() cel := checkableElements(p) - for i := 0; i < numel; i++ { + for i := range numel { elref, elparm := p.GenElemRef(i, s.genParamRef(p, pi)) if elref == "" || elref == "_" || cel == 0 { verb(4, "empty skip p%d el %d", pi, i) @@ -2058,7 +2054,7 @@ func (s *genstate) emitMain(outf *os.File, numit int, fcnmask map[int]int, pkmas for k := 0; k < s.NumTestPackages; k++ { cp := fmt.Sprintf("%s%s%d", s.Tag, CallerName, k) fmt.Fprintf(outf, " go func(ch chan bool) {\n") - for i := 0; i < numit; i++ { + for i := range numit { if shouldEmitFP(i, k, fcnmask, pkmask) { fmt.Fprintf(outf, " %s.%s%d(\"normal\")\n", cp, CallerName, i) if s.tunables.doReflectCall { diff --git a/cmd/stringer/golden_test.go b/cmd/stringer/golden_test.go index 2a81c0855aa..e40b7c53c91 100644 --- a/cmd/stringer/golden_test.go +++ b/cmd/stringer/golden_test.go @@ -453,7 +453,6 @@ func TestGolden(t *testing.T) { dir := t.TempDir() for _, test := range golden { - test := test t.Run(test.name, func(t *testing.T) { input := "package test\n" + test.input file := test.name + ".go" diff --git a/container/intsets/sparse_test.go b/container/intsets/sparse_test.go index cd8ec6e0840..f218e09b6a3 100644 --- a/container/intsets/sparse_test.go +++ b/container/intsets/sparse_test.go @@ -236,7 +236,7 @@ func (set *pset) check(t *testing.T, msg string) { func randomPset(prng *rand.Rand, maxSize int) *pset { set := makePset() size := int(prng.Int()) % maxSize - for i := 0; i < size; i++ { + for range size { // TODO(adonovan): benchmark how performance varies // with this sparsity parameter. n := int(prng.Int()) % 10000 @@ -252,7 +252,7 @@ func TestRandomMutations(t *testing.T) { set := makePset() prng := rand.New(rand.NewSource(0)) - for i := 0; i < 10000; i++ { + for i := range 10000 { n := int(prng.Int())%2000 - 1000 if i%2 == 0 { if debug { @@ -278,9 +278,9 @@ func TestRandomMutations(t *testing.T) { func TestLowerBound(t *testing.T) { // Use random sets of sizes from 0 to about 4000. prng := rand.New(rand.NewSource(0)) - for i := uint(0); i < 12; i++ { + for i := range uint(12) { x := randomPset(prng, 1<= j && e < found { @@ -302,7 +302,7 @@ func TestSetOperations(t *testing.T) { // For each operator, we test variations such as // Z.op(X, Y), Z.op(X, Z) and Z.op(Z, Y) to exercise // the degenerate cases of each method implementation. - for i := uint(0); i < 12; i++ { + for i := range uint(12) { X := randomPset(prng, 1< max { @@ -366,7 +366,7 @@ func (s *gcSizes) ptrdata(T types.Type) int64 { } var o, p int64 - for i := 0; i < nf; i++ { + for i := range nf { ft := t.Field(i).Type() a, sz := s.Alignof(ft), s.Sizeof(ft) fp := s.ptrdata(ft) diff --git a/go/analysis/passes/httpmux/httpmux.go b/go/analysis/passes/httpmux/httpmux.go index 58d3ed5daca..655b78fd1cb 100644 --- a/go/analysis/passes/httpmux/httpmux.go +++ b/go/analysis/passes/httpmux/httpmux.go @@ -9,6 +9,7 @@ import ( "go/constant" "go/types" "regexp" + "slices" "strings" "golang.org/x/mod/semver" @@ -103,12 +104,7 @@ func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool { if f.Type().(*types.Signature).Recv() == nil { return false // not a method } - for _, n := range names { - if f.Name() == n { - return true - } - } - return false // not in names + return slices.Contains(names, f.Name()) } // stringConstantExpr returns expression's string constant value. diff --git a/go/analysis/passes/structtag/structtag.go b/go/analysis/passes/structtag/structtag.go index d926503403d..da4afd1b232 100644 --- a/go/analysis/passes/structtag/structtag.go +++ b/go/analysis/passes/structtag/structtag.go @@ -13,6 +13,7 @@ import ( "go/types" "path/filepath" "reflect" + "slices" "strconv" "strings" @@ -167,11 +168,8 @@ func checkTagDuplicates(pass *analysis.Pass, tag, key string, nearest, field *ty if i := strings.Index(val, ","); i >= 0 { if key == "xml" { // Use a separate namespace for XML attributes. - for _, opt := range strings.Split(val[i:], ",") { - if opt == "attr" { - key += " attribute" // Key is part of the error message. - break - } + if slices.Contains(strings.Split(val[i:], ","), "attr") { + key += " attribute" // Key is part of the error message. } } val = val[:i] diff --git a/go/analysis/passes/testinggoroutine/util.go b/go/analysis/passes/testinggoroutine/util.go index 027c99e6b0f..88e77fb4fc4 100644 --- a/go/analysis/passes/testinggoroutine/util.go +++ b/go/analysis/passes/testinggoroutine/util.go @@ -7,6 +7,7 @@ package testinggoroutine import ( "go/ast" "go/types" + "slices" "golang.org/x/tools/internal/typeparams" ) @@ -48,12 +49,7 @@ func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool { if f.Type().(*types.Signature).Recv() == nil { return false } - for _, n := range names { - if f.Name() == n { - return true - } - } - return false + return slices.Contains(names, f.Name()) } func funcIdent(fun ast.Expr) *ast.Ident { diff --git a/go/ast/astutil/imports.go b/go/ast/astutil/imports.go index a6b5ed0a893..5e5601aa467 100644 --- a/go/ast/astutil/imports.go +++ b/go/ast/astutil/imports.go @@ -9,6 +9,7 @@ import ( "fmt" "go/ast" "go/token" + "slices" "strconv" "strings" ) @@ -186,7 +187,7 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, path string) (added spec.(*ast.ImportSpec).Path.ValuePos = first.Pos() first.Specs = append(first.Specs, spec) } - f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) + f.Decls = slices.Delete(f.Decls, i, i+1) i-- } diff --git a/go/ast/astutil/rewrite_test.go b/go/ast/astutil/rewrite_test.go index 57136a07cab..2e1c77034c8 100644 --- a/go/ast/astutil/rewrite_test.go +++ b/go/ast/astutil/rewrite_test.go @@ -244,7 +244,6 @@ func vardecl(name, typ string) *ast.GenDecl { func TestRewrite(t *testing.T) { t.Run("*", func(t *testing.T) { for _, test := range rewriteTests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() fset := token.NewFileSet() diff --git a/go/buildutil/allpackages.go b/go/buildutil/allpackages.go index dfb8cd6c7b0..32886a7175f 100644 --- a/go/buildutil/allpackages.go +++ b/go/buildutil/allpackages.go @@ -52,7 +52,6 @@ func ForEachPackage(ctxt *build.Context, found func(importPath string, err error var wg sync.WaitGroup for _, root := range ctxt.SrcDirs() { - root := root wg.Add(1) go func() { allPackages(ctxt, root, ch) @@ -107,7 +106,6 @@ func allPackages(ctxt *build.Context, root string, ch chan<- item) { ch <- item{pkg, err} } for _, fi := range files { - fi := fi if fi.IsDir() { wg.Add(1) go func() { diff --git a/go/callgraph/cha/cha_test.go b/go/callgraph/cha/cha_test.go index 7795cb44de0..922541d6c56 100644 --- a/go/callgraph/cha/cha_test.go +++ b/go/callgraph/cha/cha_test.go @@ -40,7 +40,7 @@ var inputs = []string{ func expectation(f *ast.File) (string, token.Pos) { for _, c := range f.Comments { text := strings.TrimSpace(c.Text()) - if t := strings.TrimPrefix(text, "WANT:\n"); t != text { + if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { return t, c.Pos() } } diff --git a/go/callgraph/rta/rta_test.go b/go/callgraph/rta/rta_test.go index 6b16484245b..8cfc73ee4db 100644 --- a/go/callgraph/rta/rta_test.go +++ b/go/callgraph/rta/rta_test.go @@ -105,7 +105,7 @@ func check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) { expectation := func(f *ast.File) (string, int) { for _, c := range f.Comments { text := strings.TrimSpace(c.Text()) - if t := strings.TrimPrefix(text, "WANT:\n"); t != text { + if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { return t, tokFile.Line(c.Pos()) } } @@ -134,7 +134,7 @@ func check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) { // A leading "!" negates the assertion. sense := true - if rest := strings.TrimPrefix(line, "!"); rest != line { + if rest, ok := strings.CutPrefix(line, "!"); ok { sense = false line = strings.TrimSpace(rest) if line == "" { diff --git a/go/callgraph/vta/helpers_test.go b/go/callgraph/vta/helpers_test.go index 59a9277f759..be5e756dcd5 100644 --- a/go/callgraph/vta/helpers_test.go +++ b/go/callgraph/vta/helpers_test.go @@ -28,7 +28,7 @@ import ( func want(f *ast.File) []string { for _, c := range f.Comments { text := strings.TrimSpace(c.Text()) - if t := strings.TrimPrefix(text, "WANT:\n"); t != text { + if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { return strings.Split(t, "\n") } } diff --git a/go/callgraph/vta/internal/trie/op_test.go b/go/callgraph/vta/internal/trie/op_test.go index b4610d55c22..535e7ac2775 100644 --- a/go/callgraph/vta/internal/trie/op_test.go +++ b/go/callgraph/vta/internal/trie/op_test.go @@ -12,6 +12,7 @@ import ( "time" "golang.org/x/tools/go/callgraph/vta/internal/trie" + "maps" ) // This file tests trie.Map by cross checking operations on a collection of @@ -189,12 +190,8 @@ func (c builtinCollection) Intersect(l int, r int) { func (c builtinCollection) Merge(l int, r int) { result := map[uint64]any{} - for k, v := range c[r] { - result[k] = v - } - for k, v := range c[l] { - result[k] = v - } + maps.Copy(result, c[r]) + maps.Copy(result, c[l]) c[l] = result } @@ -217,9 +214,7 @@ func (c builtinCollection) Average(l int, r int) { func (c builtinCollection) Assign(l, r int) { m := map[uint64]any{} - for k, v := range c[r] { - m[k] = v - } + maps.Copy(m, c[r]) c[l] = m } @@ -232,7 +227,7 @@ func newTriesCollection(size int) *trieCollection { b: trie.NewBuilder(), tries: make([]trie.MutMap, size), } - for i := 0; i < size; i++ { + for i := range size { tc.tries[i] = tc.b.MutEmpty() } return tc @@ -240,7 +235,7 @@ func newTriesCollection(size int) *trieCollection { func newMapsCollection(size int) *builtinCollection { maps := make(builtinCollection, size) - for i := 0; i < size; i++ { + for i := range size { maps[i] = map[uint64]any{} } return &maps @@ -290,7 +285,7 @@ func (op operation) Apply(maps mapCollection) any { func distribution(dist map[opCode]int) []opCode { var codes []opCode for op, n := range dist { - for i := 0; i < n; i++ { + for range n { codes = append(codes, op) } } @@ -326,7 +321,7 @@ func randOperator(r *rand.Rand, opts options) operation { func randOperators(r *rand.Rand, numops int, opts options) []operation { ops := make([]operation, numops) - for i := 0; i < numops; i++ { + for i := range numops { ops[i] = randOperator(r, opts) } return ops diff --git a/go/gcexportdata/example_test.go b/go/gcexportdata/example_test.go index 852ba5a597c..d6d69a8aa54 100644 --- a/go/gcexportdata/example_test.go +++ b/go/gcexportdata/example_test.go @@ -15,6 +15,7 @@ import ( "log" "os" "path/filepath" + "slices" "strings" "golang.org/x/tools/go/gcexportdata" @@ -51,13 +52,7 @@ func ExampleRead() { // We can see all the names in Names. members := pkg.Scope().Names() - foundPrintln := false - for _, member := range members { - if member == "Println" { - foundPrintln = true - break - } - } + foundPrintln := slices.Contains(members, "Println") fmt.Print("Package members: ") if foundPrintln { fmt.Println("Println found") diff --git a/go/gcexportdata/gcexportdata.go b/go/gcexportdata/gcexportdata.go index 65fe2628e90..7b90bc92353 100644 --- a/go/gcexportdata/gcexportdata.go +++ b/go/gcexportdata/gcexportdata.go @@ -193,10 +193,7 @@ func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package, return pkg, err default: - l := len(data) - if l > 10 { - l = 10 - } + l := min(len(data), 10) return nil, fmt.Errorf("unexpected export data with prefix %q for path %s", string(data[:l]), path) } } diff --git a/go/packages/external.go b/go/packages/external.go index 91bd62e83b1..f37bc651009 100644 --- a/go/packages/external.go +++ b/go/packages/external.go @@ -90,7 +90,7 @@ func findExternalDriver(cfg *Config) driver { const toolPrefix = "GOPACKAGESDRIVER=" tool := "" for _, env := range cfg.Env { - if val := strings.TrimPrefix(env, toolPrefix); val != env { + if val, ok := strings.CutPrefix(env, toolPrefix); ok { tool = val } } diff --git a/go/packages/overlay_test.go b/go/packages/overlay_test.go index 1108461926f..4a7cc68f4c7 100644 --- a/go/packages/overlay_test.go +++ b/go/packages/overlay_test.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "sort" "testing" @@ -93,7 +94,7 @@ func testOverlayChangesBothPackageNames(t *testing.T, exporter packagestest.Expo if len(initial) != 3 { t.Fatalf("expected 3 packages, got %v", len(initial)) } - for i := 0; i < 3; i++ { + for i := range 3 { if ok := checkPkg(t, initial[i], want[i].id, want[i].name, want[i].count); !ok { t.Errorf("%d: got {%s %s %d}, expected %v", i, initial[i].ID, initial[i].Name, len(initial[i].Syntax), want[i]) @@ -139,7 +140,7 @@ func testOverlayChangesTestPackageName(t *testing.T, exporter packagestest.Expor if len(initial) != 3 { t.Fatalf("expected 3 packages, got %v", len(initial)) } - for i := 0; i < 3; i++ { + for i := range 3 { if ok := checkPkg(t, initial[i], want[i].id, want[i].name, want[i].count); !ok { t.Errorf("got {%s %s %d}, expected %v", initial[i].ID, initial[i].Name, len(initial[i].Syntax), want[i]) @@ -824,11 +825,8 @@ func testInvalidFilesBeforeOverlayContains(t *testing.T, exporter packagestest.E t.Fatalf("expected package ID %q, got %q", tt.wantID, pkg.ID) } var containsFile bool - for _, goFile := range pkg.CompiledGoFiles { - if f == goFile { - containsFile = true - break - } + if slices.Contains(pkg.CompiledGoFiles, f) { + containsFile = true } if !containsFile { t.Fatalf("expected %s in CompiledGoFiles, got %v", f, pkg.CompiledGoFiles) @@ -1054,7 +1052,7 @@ func TestOverlaysInReplace(t *testing.T) { if err := os.Mkdir(dirB, 0775); err != nil { t.Fatal(err) } - if err := os.WriteFile(filepath.Join(dirB, "go.mod"), []byte(fmt.Sprintf("module %s.com", dirB)), 0775); err != nil { + if err := os.WriteFile(filepath.Join(dirB, "go.mod"), fmt.Appendf(nil, "module %s.com", dirB), 0775); err != nil { t.Fatal(err) } if err := os.MkdirAll(filepath.Join(dirB, "inner"), 0775); err != nil { diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 5678b265561..ae3cbb6bb2b 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -20,6 +20,7 @@ import ( "path/filepath" "reflect" "runtime" + "slices" "sort" "strings" "testing" @@ -387,7 +388,7 @@ func TestLoadArgumentListIsNotTooLong(t *testing.T) { defer exported.Cleanup() numOfPatterns := argMax/16 + 1 // the pattern below is approx. 16 chars patterns := make([]string, numOfPatterns) - for i := 0; i < numOfPatterns; i++ { + for i := range numOfPatterns { patterns[i] = fmt.Sprintf("golang.org/mod/p%d", i) } // patterns have more than argMax number of chars combined with whitespaces b/w patterns @@ -1610,7 +1611,7 @@ EOF defer os.Setenv(pathKey, oldPath) // Clone exported.Config config := exported.Config - config.Env = append([]string{}, exported.Config.Env...) + config.Env = slices.Clone(exported.Config.Env) config.Env = append(config.Env, "GOPACKAGESDRIVER="+test.driver) pkgs, err := packages.Load(exported.Config, "golist") if err != nil { @@ -1978,7 +1979,6 @@ func testCgoNoSyntax(t *testing.T, exporter packagestest.Exporter) { packages.NeedName | packages.NeedImports, } for _, mode := range modes { - mode := mode t.Run(fmt.Sprint(mode), func(t *testing.T) { exported.Config.Mode = mode pkgs, err := packages.Load(exported.Config, "golang.org/fake/c") @@ -2787,7 +2787,7 @@ func main() { t.Fatal(err) } - exported.Config.Env = append(append([]string{}, baseEnv...), "GOPACKAGESDRIVER="+emptyDriverPath) + exported.Config.Env = append(slices.Clone(baseEnv), "GOPACKAGESDRIVER="+emptyDriverPath) initial, err := packages.Load(exported.Config, "golang.org/fake/a") if err != nil { t.Fatal(err) @@ -2807,7 +2807,7 @@ func main() { t.Fatal(err) } - exported.Config.Env = append(append([]string{}, baseEnv...), "GOPACKAGESDRIVER="+notHandledDriverPath) + exported.Config.Env = append(slices.Clone(baseEnv), "GOPACKAGESDRIVER="+notHandledDriverPath) initial, err = packages.Load(exported.Config, "golang.org/fake/a") if err != nil { t.Fatal(err) diff --git a/go/packages/packagestest/export.go b/go/packages/packagestest/export.go index 4ac4967b46b..86da99ecdf3 100644 --- a/go/packages/packagestest/export.go +++ b/go/packages/packagestest/export.go @@ -159,7 +159,6 @@ var All = []Exporter{GOPATH, Modules} func TestAll(t *testing.T, f func(*testing.T, Exporter)) { t.Helper() for _, e := range All { - e := e // in case f calls t.Parallel t.Run(e.Name(), func(t *testing.T) { t.Helper() f(t, e) @@ -173,7 +172,6 @@ func TestAll(t *testing.T, f func(*testing.T, Exporter)) { func BenchmarkAll(b *testing.B, f func(*testing.B, Exporter)) { b.Helper() for _, e := range All { - e := e // in case f calls t.Parallel b.Run(e.Name(), func(b *testing.B) { b.Helper() f(b, e) diff --git a/go/ssa/builder_test.go b/go/ssa/builder_test.go index 2589cc82bb6..a48723bd271 100644 --- a/go/ssa/builder_test.go +++ b/go/ssa/builder_test.go @@ -613,7 +613,6 @@ var indirect = R[int].M "(p.S[int]).M[int]", }, } { - entry := entry t.Run(entry.name, func(t *testing.T) { v := p.Var(entry.name) if v == nil { @@ -1011,7 +1010,6 @@ func TestGo117Builtins(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() fset := token.NewFileSet() @@ -1466,7 +1464,6 @@ func TestBuildPackageGo120(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() fset := token.NewFileSet() diff --git a/go/ssa/dom.go b/go/ssa/dom.go index f490986140c..78f651c8ee9 100644 --- a/go/ssa/dom.go +++ b/go/ssa/dom.go @@ -22,6 +22,7 @@ import ( "fmt" "math/big" "os" + "slices" "sort" ) @@ -43,7 +44,7 @@ func (b *BasicBlock) Dominates(c *BasicBlock) bool { // DomPreorder returns a new slice containing the blocks of f // in a preorder traversal of the dominator tree. func (f *Function) DomPreorder() []*BasicBlock { - slice := append([]*BasicBlock(nil), f.Blocks...) + slice := slices.Clone(f.Blocks) sort.Slice(slice, func(i, j int) bool { return slice[i].dom.pre < slice[j].dom.pre }) @@ -54,7 +55,7 @@ func (f *Function) DomPreorder() []*BasicBlock { // in a postorder traversal of the dominator tree. // (This is not the same as a postdominance order.) func (f *Function) DomPostorder() []*BasicBlock { - slice := append([]*BasicBlock(nil), f.Blocks...) + slice := slices.Clone(f.Blocks) sort.Slice(slice, func(i, j int) bool { return slice[i].dom.post < slice[j].dom.post }) @@ -277,8 +278,8 @@ func sanityCheckDomTree(f *Function) { // Check the entire relation. O(n^2). // The Recover block (if any) must be treated specially so we skip it. ok := true - for i := 0; i < n; i++ { - for j := 0; j < n; j++ { + for i := range n { + for j := range n { b, c := f.Blocks[i], f.Blocks[j] if c == f.Recover { continue diff --git a/go/ssa/emit.go b/go/ssa/emit.go index bca79adc4e1..e53ebf5a7fd 100644 --- a/go/ssa/emit.go +++ b/go/ssa/emit.go @@ -496,7 +496,7 @@ func emitTailCall(f *Function, call *Call) { case 1: ret.Results = []Value{tuple} default: - for i := 0; i < nr; i++ { + for i := range nr { v := emitExtract(f, tuple, i) // TODO(adonovan): in principle, this is required: // v = emitConv(f, o.Type, f.Signature.Results[i].Type) diff --git a/go/ssa/instantiate.go b/go/ssa/instantiate.go index 2512f32976c..20a0986e6d3 100644 --- a/go/ssa/instantiate.go +++ b/go/ssa/instantiate.go @@ -7,6 +7,7 @@ package ssa import ( "fmt" "go/types" + "slices" "sync" ) @@ -122,10 +123,5 @@ func (prog *Program) isParameterized(ts ...types.Type) bool { // handle the most common but shallow cases such as T, pkg.T, // *T without consulting the cache under the lock. - for _, t := range ts { - if prog.hasParams.Has(t) { - return true - } - } - return false + return slices.ContainsFunc(ts, prog.hasParams.Has) } diff --git a/go/ssa/interp/external.go b/go/ssa/interp/external.go index 2a3a7e5b79e..2fb683c07fe 100644 --- a/go/ssa/interp/external.go +++ b/go/ssa/interp/external.go @@ -9,6 +9,7 @@ package interp import ( "bytes" + "maps" "math" "os" "runtime" @@ -30,7 +31,7 @@ var externals = make(map[string]externalFn) func init() { // That little dot Û° is an Arabic zero numeral (U+06F0), categories [Nd]. - for k, v := range map[string]externalFn{ + maps.Copy(externals, map[string]externalFn{ "(reflect.Value).Bool": extÛ°reflectÛ°ValueÛ°Bool, "(reflect.Value).CanAddr": extÛ°reflectÛ°ValueÛ°CanAddr, "(reflect.Value).CanInterface": extÛ°reflectÛ°ValueÛ°CanInterface, @@ -111,9 +112,7 @@ func init() { "strings.ToLower": extÛ°stringsÛ°ToLower, "time.Sleep": extÛ°timeÛ°Sleep, "unicode/utf8.DecodeRuneInString": extÛ°unicodeÛ°utf8Û°DecodeRuneInString, - } { - externals[k] = v - } + }) } func extÛ°bytesÛ°Equal(fr *frame, args []value) value { diff --git a/go/ssa/lift.go b/go/ssa/lift.go index 6138ca82e0e..d7c1bf5063e 100644 --- a/go/ssa/lift.go +++ b/go/ssa/lift.go @@ -374,7 +374,7 @@ func (s *blockSet) add(b *BasicBlock) bool { // returns its index, or returns -1 if empty. func (s *blockSet) take() int { l := s.BitLen() - for i := 0; i < l; i++ { + for i := range l { if s.Bit(i) == 1 { s.SetBit(&s.Int, i, 0) return i @@ -403,10 +403,8 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool // Don't lift result values in functions that defer // calls that may recover from panic. if fn := alloc.Parent(); fn.Recover != nil { - for _, nr := range fn.results { - if nr == alloc { - return false - } + if slices.Contains(fn.results, alloc) { + return false } } diff --git a/go/ssa/sanity.go b/go/ssa/sanity.go index 3b862992680..b11680a1e1d 100644 --- a/go/ssa/sanity.go +++ b/go/ssa/sanity.go @@ -14,6 +14,7 @@ import ( "go/types" "io" "os" + "slices" "strings" ) @@ -119,13 +120,7 @@ func (s *sanity) checkInstr(idx int, instr Instruction) { case *Alloc: if !instr.Heap { - found := false - for _, l := range s.fn.Locals { - if l == instr { - found = true - break - } - } + found := slices.Contains(s.fn.Locals, instr) if !found { s.errorf("local alloc %s = %s does not appear in Function.Locals", instr.Name(), instr) } @@ -282,13 +277,7 @@ func (s *sanity) checkBlock(b *BasicBlock, index int) { // Check predecessor and successor relations are dual, // and that all blocks in CFG belong to same function. for _, a := range b.Preds { - found := false - for _, bb := range a.Succs { - if bb == b { - found = true - break - } - } + found := slices.Contains(a.Succs, b) if !found { s.errorf("expected successor edge in predecessor %s; found only: %s", a, a.Succs) } @@ -297,13 +286,7 @@ func (s *sanity) checkBlock(b *BasicBlock, index int) { } } for _, c := range b.Succs { - found := false - for _, bb := range c.Preds { - if bb == b { - found = true - break - } - } + found := slices.Contains(c.Preds, b) if !found { s.errorf("expected predecessor edge in successor %s; found only: %s", c, c.Preds) } diff --git a/go/ssa/subst.go b/go/ssa/subst.go index bbe5796d703..b4ea16854ea 100644 --- a/go/ssa/subst.go +++ b/go/ssa/subst.go @@ -266,7 +266,7 @@ func (subst *subster) interface_(iface *types.Interface) *types.Interface { var methods []*types.Func initMethods := func(n int) { // copy first n explicit methods methods = make([]*types.Func, iface.NumExplicitMethods()) - for i := 0; i < n; i++ { + for i := range n { f := iface.ExplicitMethod(i) norecv := changeRecv(f.Type().(*types.Signature), nil) methods[i] = types.NewFunc(f.Pos(), f.Pkg(), f.Name(), norecv) @@ -290,7 +290,7 @@ func (subst *subster) interface_(iface *types.Interface) *types.Interface { var embeds []types.Type initEmbeds := func(n int) { // copy first n embedded types embeds = make([]types.Type, iface.NumEmbeddeds()) - for i := 0; i < n; i++ { + for i := range n { embeds[i] = iface.EmbeddedType(i) } } diff --git a/go/ssa/util.go b/go/ssa/util.go index 9a73984a6a0..e53b31ff3bb 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -385,7 +385,7 @@ func (m *typeListMap) hash(ts []types.Type) uint32 { // Some smallish prime far away from typeutil.Hash. n := len(ts) h := uint32(13619) + 2*uint32(n) - for i := 0; i < n; i++ { + for i := range n { h += 3 * m.hasher.Hash(ts[i]) } return h diff --git a/godoc/index.go b/godoc/index.go index 05a1a9441ee..853337715c1 100644 --- a/godoc/index.go +++ b/godoc/index.go @@ -65,6 +65,7 @@ import ( "golang.org/x/tools/godoc/util" "golang.org/x/tools/godoc/vfs" + "maps" ) // ---------------------------------------------------------------------------- @@ -862,9 +863,7 @@ func (x *Indexer) indexGoFile(dirname string, filename string, file *token.File, dest = make(map[string]SpotKind) x.exports[pkgPath] = dest } - for k, v := range x.curPkgExports { - dest[k] = v - } + maps.Copy(dest, x.curPkgExports) } } @@ -1069,7 +1068,7 @@ func (c *Corpus) NewIndex() *Index { // convert alist into a map of alternative spellings alts := make(map[string]*AltWords) - for i := 0; i < len(alist); i++ { + for i := range alist { a := alist[i].(*AltWords) alts[a.Canon] = a } diff --git a/godoc/snippet.go b/godoc/snippet.go index 1750478606e..43c1899a093 100644 --- a/godoc/snippet.go +++ b/godoc/snippet.go @@ -14,6 +14,7 @@ import ( "fmt" "go/ast" "go/token" + "slices" ) type Snippet struct { @@ -41,10 +42,8 @@ func findSpec(list []ast.Spec, id *ast.Ident) ast.Spec { return s } case *ast.ValueSpec: - for _, n := range s.Names { - if n == id { - return s - } + if slices.Contains(s.Names, id) { + return s } case *ast.TypeSpec: if s.Name == id { diff --git a/godoc/static/gen_test.go b/godoc/static/gen_test.go index 1f1c62e0e9c..7b7668a558c 100644 --- a/godoc/static/gen_test.go +++ b/godoc/static/gen_test.go @@ -39,7 +39,7 @@ to see the differences.`) // TestAppendQuote ensures that AppendQuote produces a valid literal. func TestAppendQuote(t *testing.T) { var in, out bytes.Buffer - for r := rune(0); r < unicode.MaxRune; r++ { + for r := range unicode.MaxRune { in.WriteRune(r) } appendQuote(&out, in.Bytes()) diff --git a/godoc/versions_test.go b/godoc/versions_test.go index a021616ba11..7b822f69b51 100644 --- a/godoc/versions_test.go +++ b/godoc/versions_test.go @@ -6,6 +6,7 @@ package godoc import ( "go/build" + "slices" "testing" "golang.org/x/tools/internal/testenv" @@ -102,12 +103,7 @@ func TestParseVersionRow(t *testing.T) { // hasTag checks whether a given release tag is contained in the current version // of the go binary. func hasTag(t string) bool { - for _, v := range build.Default.ReleaseTags { - if t == v { - return true - } - } - return false + return slices.Contains(build.Default.ReleaseTags, t) } func TestAPIVersion(t *testing.T) { diff --git a/godoc/vfs/os.go b/godoc/vfs/os.go index 35d050946e6..fe21a58662e 100644 --- a/godoc/vfs/os.go +++ b/godoc/vfs/os.go @@ -12,6 +12,7 @@ import ( pathpkg "path" "path/filepath" "runtime" + "slices" ) // We expose a new variable because otherwise we need to copy the findGOROOT logic again @@ -45,10 +46,8 @@ type osFS struct { func isGoPath(path string) bool { for _, bp := range filepath.SplitList(build.Default.GOPATH) { - for _, gp := range filepath.SplitList(path) { - if bp == gp { - return true - } + if slices.Contains(filepath.SplitList(path), bp) { + return true } } return false diff --git a/godoc/vfs/zipfs/zipfs_test.go b/godoc/vfs/zipfs/zipfs_test.go index cb000361745..3e5a8034a5b 100644 --- a/godoc/vfs/zipfs/zipfs_test.go +++ b/godoc/vfs/zipfs/zipfs_test.go @@ -172,7 +172,7 @@ func TestZipFSOpenSeek(t *testing.T) { defer f.Close() // test Seek() multiple times - for i := 0; i < 3; i++ { + for range 3 { all, err := io.ReadAll(f) if err != nil { t.Error(err) diff --git a/internal/bisect/bisect.go b/internal/bisect/bisect.go index 5a7da4871a8..7b1d112a7cd 100644 --- a/internal/bisect/bisect.go +++ b/internal/bisect/bisect.go @@ -320,7 +320,7 @@ func AppendMarker(dst []byte, id uint64) []byte { const prefix = "[bisect-match 0x" var buf [len(prefix) + 16 + 1]byte copy(buf[:], prefix) - for i := 0; i < 16; i++ { + for i := range 16 { buf[len(prefix)+i] = "0123456789abcdef"[id>>60] id <<= 4 } @@ -504,7 +504,7 @@ func fnvString(h uint64, x string) uint64 { } func fnvUint64(h uint64, x uint64) uint64 { - for i := 0; i < 8; i++ { + for range 8 { h ^= uint64(x & 0xFF) x >>= 8 h *= prime64 @@ -513,7 +513,7 @@ func fnvUint64(h uint64, x uint64) uint64 { } func fnvUint32(h uint64, x uint32) uint64 { - for i := 0; i < 4; i++ { + for range 4 { h ^= uint64(x & 0xFF) x >>= 8 h *= prime64 diff --git a/internal/diff/diff.go b/internal/diff/diff.go index a13547b7a7e..c12bdfd2acd 100644 --- a/internal/diff/diff.go +++ b/internal/diff/diff.go @@ -7,6 +7,7 @@ package diff import ( "fmt" + "slices" "sort" "strings" ) @@ -64,7 +65,7 @@ func ApplyBytes(src []byte, edits []Edit) ([]byte, error) { // It may return a different slice. func validate(src string, edits []Edit) ([]Edit, int, error) { if !sort.IsSorted(editsSort(edits)) { - edits = append([]Edit(nil), edits...) + edits = slices.Clone(edits) SortEdits(edits) } diff --git a/internal/diff/diff_test.go b/internal/diff/diff_test.go index 77a20baf272..9e2a1d23997 100644 --- a/internal/diff/diff_test.go +++ b/internal/diff/diff_test.go @@ -61,7 +61,7 @@ func TestNEdits(t *testing.T) { func TestNRandom(t *testing.T) { rand.Seed(1) - for i := 0; i < 1000; i++ { + for i := range 1000 { a := randstr("abω", 16) b := randstr("abωc", 16) edits := diff.Strings(a, b) @@ -200,7 +200,7 @@ func TestRegressionOld002(t *testing.T) { func randstr(s string, n int) string { src := []rune(s) x := make([]rune, n) - for i := 0; i < n; i++ { + for i := range n { x[i] = src[rand.Intn(len(src))] } return string(x) diff --git a/internal/diff/lcs/common_test.go b/internal/diff/lcs/common_test.go index f19245e404c..68f4485fdb8 100644 --- a/internal/diff/lcs/common_test.go +++ b/internal/diff/lcs/common_test.go @@ -7,6 +7,7 @@ package lcs import ( "log" "math/rand" + "slices" "strings" "testing" ) @@ -72,10 +73,8 @@ func check(t *testing.T, str string, lcs lcs, want []string) { got.WriteString(str[dd.X : dd.X+dd.Len]) } ans := got.String() - for _, w := range want { - if ans == w { - return - } + if slices.Contains(want, ans) { + return } t.Fatalf("str=%q lcs=%v want=%q got=%q", str, lcs, want, ans) } @@ -109,7 +108,7 @@ func lcslen(l lcs) int { func randstr(s string, n int) string { src := []rune(s) x := make([]rune, n) - for i := 0; i < n; i++ { + for i := range n { x[i] = src[rand.Intn(len(src))] } return string(x) diff --git a/internal/diff/lcs/old.go b/internal/diff/lcs/old.go index 7c74b47bb1c..c0d43a6c2c7 100644 --- a/internal/diff/lcs/old.go +++ b/internal/diff/lcs/old.go @@ -377,10 +377,7 @@ func (e *editGraph) twoDone(df, db int) (int, bool) { if (df+db+e.delta)%2 != 0 { return 0, false // diagonals cannot overlap } - kmin := -db + e.delta - if -df > kmin { - kmin = -df - } + kmin := max(-df, -db+e.delta) kmax := db + e.delta if df < kmax { kmax = df diff --git a/internal/diff/lcs/old_test.go b/internal/diff/lcs/old_test.go index ddc3bde0ed2..2eac1af6d2f 100644 --- a/internal/diff/lcs/old_test.go +++ b/internal/diff/lcs/old_test.go @@ -107,7 +107,7 @@ func TestRegressionOld003(t *testing.T) { func TestRandOld(t *testing.T) { rand.Seed(1) - for i := 0; i < 1000; i++ { + for i := range 1000 { // TODO(adonovan): use ASCII and bytesSeqs here? The use of // non-ASCII isn't relevant to the property exercised by the test. a := []rune(randstr("abω", 16)) @@ -186,7 +186,7 @@ func genBench(set string, n int) []struct{ before, after string } { // before and after differing at least once, and about 5% rand.Seed(3) var ans []struct{ before, after string } - for i := 0; i < 24; i++ { + for range 24 { // maybe b should have an approximately known number of diffs a := randstr(set, n) cnt := 0 diff --git a/internal/diff/ndiff.go b/internal/diff/ndiff.go index fbef4d730c5..a2eef26ac77 100644 --- a/internal/diff/ndiff.go +++ b/internal/diff/ndiff.go @@ -72,7 +72,7 @@ func diffRunes(before, after []rune) []Edit { func runes(bytes []byte) []rune { n := utf8.RuneCount(bytes) runes := make([]rune, n) - for i := 0; i < n; i++ { + for i := range n { r, sz := utf8.DecodeRune(bytes) bytes = bytes[sz:] runes[i] = r diff --git a/internal/diffp/diff.go b/internal/diffp/diff.go index aa5ef81ac2e..54ab0888482 100644 --- a/internal/diffp/diff.go +++ b/internal/diffp/diff.go @@ -119,10 +119,7 @@ func Diff(oldName string, old []byte, newName string, new []byte) []byte { // End chunk with common lines for context. if len(ctext) > 0 { - n := end.x - start.x - if n > C { - n = C - } + n := min(end.x-start.x, C) for _, s := range x[start.x : start.x+n] { ctext = append(ctext, " "+s) count.x++ @@ -237,7 +234,7 @@ func tgs(x, y []string) []pair { for i := range T { T[i] = n + 1 } - for i := 0; i < n; i++ { + for i := range n { k := sort.Search(n, func(k int) bool { return T[k] >= J[i] }) diff --git a/internal/event/label/label.go b/internal/event/label/label.go index 7c00ca2a6da..92a39105731 100644 --- a/internal/event/label/label.go +++ b/internal/event/label/label.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "reflect" + "slices" "unsafe" ) @@ -154,10 +155,8 @@ func (f *filter) Valid(index int) bool { func (f *filter) Label(index int) Label { l := f.underlying.Label(index) - for _, f := range f.keys { - if l.Key() == f { - return Label{} - } + if slices.Contains(f.keys, l.Key()) { + return Label{} } return l } diff --git a/internal/gcimporter/gcimporter_test.go b/internal/gcimporter/gcimporter_test.go index 9b38a0e1e28..9dc65fa19f6 100644 --- a/internal/gcimporter/gcimporter_test.go +++ b/internal/gcimporter/gcimporter_test.go @@ -672,7 +672,7 @@ func TestIssue15517(t *testing.T) { // file and package path are different, exposing the problem if present. // The same issue occurs with vendoring.) imports := make(map[string]*types.Package) - for i := 0; i < 3; i++ { + for range 3 { if _, err := gcimporter.Import(token.NewFileSet(), imports, "./././testdata/p", tmpdir, nil); err != nil { t.Fatal(err) } @@ -785,7 +785,7 @@ type K = StillBad[string] // Use the interface instances concurrently. for _, inst := range insts { var wg sync.WaitGroup - for i := 0; i < 2; i++ { + for range 2 { wg.Add(1) go func() { defer wg.Done() diff --git a/internal/gcimporter/iexport.go b/internal/gcimporter/iexport.go index 48e90b29ded..780873e3ae7 100644 --- a/internal/gcimporter/iexport.go +++ b/internal/gcimporter/iexport.go @@ -236,6 +236,7 @@ import ( "io" "math/big" "reflect" + "slices" "sort" "strconv" "strings" @@ -465,7 +466,7 @@ func (p *iexporter) encodeFile(w *intWriter, file *token.File, needed []uint64) w.uint64(size) // Sort the set of needed offsets. Duplicates are harmless. - sort.Slice(needed, func(i, j int) bool { return needed[i] < needed[j] }) + slices.Sort(needed) lines := file.Lines() // byte offset of each line start w.uint64(uint64(len(lines))) @@ -819,7 +820,7 @@ func (p *iexporter) doDecl(obj types.Object) { n := named.NumMethods() w.uint64(uint64(n)) - for i := 0; i < n; i++ { + for i := range n { m := named.Method(i) w.pos(m.Pos()) w.string(m.Name()) @@ -1096,7 +1097,7 @@ func (w *exportWriter) doTyp(t types.Type, pkg *types.Package) { w.pkg(fieldPkg) w.uint64(uint64(n)) - for i := 0; i < n; i++ { + for i := range n { f := t.Field(i) if w.p.shallow { w.objectPath(f) @@ -1145,7 +1146,7 @@ func (w *exportWriter) doTyp(t types.Type, pkg *types.Package) { w.startType(unionType) nt := t.Len() w.uint64(uint64(nt)) - for i := 0; i < nt; i++ { + for i := range nt { term := t.Term(i) w.bool(term.Tilde()) w.typ(term.Type(), pkg) @@ -1274,7 +1275,7 @@ func tparamName(exportName string) string { func (w *exportWriter) paramList(tup *types.Tuple) { n := tup.Len() w.uint64(uint64(n)) - for i := 0; i < n; i++ { + for i := range n { w.param(tup.At(i)) } } diff --git a/internal/gcimporter/iimport.go b/internal/gcimporter/iimport.go index bc6c9741e7d..82e6c9d2dc1 100644 --- a/internal/gcimporter/iimport.go +++ b/internal/gcimporter/iimport.go @@ -16,6 +16,7 @@ import ( "go/types" "io" "math/big" + "slices" "sort" "strings" @@ -314,7 +315,7 @@ func iimportCommon(fset *token.FileSet, getPackages GetPackagesFunc, data []byte pkgs = pkgList[:1] // record all referenced packages as imports - list := append(([]*types.Package)(nil), pkgList[1:]...) + list := slices.Clone(pkgList[1:]) sort.Sort(byPath(list)) pkgs[0].SetImports(list) } diff --git a/internal/gocommand/invoke.go b/internal/gocommand/invoke.go index 7ea9013447b..58721202de7 100644 --- a/internal/gocommand/invoke.go +++ b/internal/gocommand/invoke.go @@ -141,7 +141,7 @@ func (runner *Runner) runPiped(ctx context.Context, inv Invocation, stdout, stde // Wait for all in-progress go commands to return before proceeding, // to avoid load concurrency errors. - for i := 0; i < maxInFlight; i++ { + for range maxInFlight { select { case <-ctx.Done(): return ctx.Err(), ctx.Err() diff --git a/internal/gopathwalk/walk.go b/internal/gopathwalk/walk.go index 984b79c2a07..5252144d046 100644 --- a/internal/gopathwalk/walk.go +++ b/internal/gopathwalk/walk.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "sync" "time" @@ -195,10 +196,8 @@ func (w *walker) getIgnoredDirs(path string) []string { // shouldSkipDir reports whether the file should be skipped or not. func (w *walker) shouldSkipDir(dir string) bool { - for _, ignoredDir := range w.ignoredDirs { - if dir == ignoredDir { - return true - } + if slices.Contains(w.ignoredDirs, dir) { + return true } if w.skip != nil { // Check with the user specified callback. diff --git a/internal/imports/fix.go b/internal/imports/fix.go index c78d10f2d61..89b96381cdc 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -32,6 +32,7 @@ import ( "golang.org/x/tools/internal/gocommand" "golang.org/x/tools/internal/gopathwalk" "golang.org/x/tools/internal/stdlib" + "maps" ) // importToGroup is a list of functions which map from an import path to @@ -968,9 +969,7 @@ func (e *ProcessEnv) CopyConfig() *ProcessEnv { resolver: nil, Env: map[string]string{}, } - for k, v := range e.Env { - copy.Env[k] = v - } + maps.Copy(copy.Env, e.Env) return copy } @@ -1003,9 +1002,7 @@ func (e *ProcessEnv) init() error { if err := json.Unmarshal(stdout.Bytes(), &goEnv); err != nil { return err } - for k, v := range goEnv { - e.Env[k] = v - } + maps.Copy(e.Env, goEnv) e.initialized = true return nil } diff --git a/internal/imports/fix_test.go b/internal/imports/fix_test.go index 478313aec7f..5313956dd63 100644 --- a/internal/imports/fix_test.go +++ b/internal/imports/fix_test.go @@ -2912,7 +2912,7 @@ func _() { wg sync.WaitGroup ) wg.Add(n) - for i := 0; i < n; i++ { + for range n { go func() { defer wg.Done() _, err := t.process("foo.com", "p/first.go", nil, nil) @@ -2983,7 +2983,7 @@ func TestSymbolSearchStarvation(t *testing.T) { } var candidates []pkgDistance - for i := 0; i < candCount; i++ { + for i := range candCount { name := fmt.Sprintf("bar%d", i) candidates = append(candidates, pkgDistance{ pkg: &pkg{ diff --git a/internal/imports/mod.go b/internal/imports/mod.go index 8555e3f83da..df94ec8186e 100644 --- a/internal/imports/mod.go +++ b/internal/imports/mod.go @@ -13,6 +13,7 @@ import ( "path" "path/filepath" "regexp" + "slices" "sort" "strconv" "strings" @@ -150,8 +151,8 @@ func newModuleResolver(e *ProcessEnv, moduleCacheCache *DirInfoCache) (*ModuleRe Path: "", Dir: filepath.Join(filepath.Dir(goWork), "vendor"), } - r.modsByModPath = append(append([]*gocommand.ModuleJSON{}, mainModsVendor...), r.dummyVendorMod) - r.modsByDir = append(append([]*gocommand.ModuleJSON{}, mainModsVendor...), r.dummyVendorMod) + r.modsByModPath = append(slices.Clone(mainModsVendor), r.dummyVendorMod) + r.modsByDir = append(slices.Clone(mainModsVendor), r.dummyVendorMod) } } else { // Vendor mode is off, so run go list -m ... to find everything. diff --git a/internal/imports/mod_cache.go b/internal/imports/mod_cache.go index b1192696b28..b96c9d4bf71 100644 --- a/internal/imports/mod_cache.go +++ b/internal/imports/mod_cache.go @@ -128,7 +128,7 @@ func (d *DirInfoCache) ScanAndListen(ctx context.Context, listener cacheListener // are going to be. Setting an arbitrary limit makes it much easier. const maxInFlight = 10 sema := make(chan struct{}, maxInFlight) - for i := 0; i < maxInFlight; i++ { + for range maxInFlight { sema <- struct{}{} } @@ -156,7 +156,7 @@ func (d *DirInfoCache) ScanAndListen(ctx context.Context, listener cacheListener d.mu.Lock() delete(d.listeners, cookie) d.mu.Unlock() - for i := 0; i < maxInFlight; i++ { + for range maxInFlight { <-sema } } diff --git a/internal/imports/mod_test.go b/internal/imports/mod_test.go index 890dc1b2e25..2862e84d184 100644 --- a/internal/imports/mod_test.go +++ b/internal/imports/mod_test.go @@ -25,6 +25,8 @@ import ( "golang.org/x/tools/internal/proxydir" "golang.org/x/tools/internal/testenv" "golang.org/x/tools/txtar" + "maps" + "slices" ) // Tests that we can find packages in the stdlib. @@ -928,12 +930,7 @@ func scanToSlice(resolver Resolver, exclude []gopathwalk.RootType) ([]*pkg, erro var result []*pkg filter := &scanCallback{ rootFound: func(root gopathwalk.Root) bool { - for _, rt := range exclude { - if root.Type == rt { - return false - } - } - return true + return !slices.Contains(exclude, root.Type) }, dirFound: func(pkg *pkg) bool { return true @@ -1023,9 +1020,7 @@ func setup(t *testing.T, extraEnv map[string]string, main, wd string) *modTest { WorkingDir: filepath.Join(mainDir, wd), GocmdRunner: &gocommand.Runner{}, } - for k, v := range extraEnv { - env.Env[k] = v - } + maps.Copy(env.Env, extraEnv) if *testDebug { env.Logf = log.Printf } diff --git a/internal/imports/sortimports.go b/internal/imports/sortimports.go index da8194fd965..67c17bc4319 100644 --- a/internal/imports/sortimports.go +++ b/internal/imports/sortimports.go @@ -11,6 +11,7 @@ import ( "go/ast" "go/token" "log" + "slices" "sort" "strconv" ) @@ -30,7 +31,7 @@ func sortImports(localPrefix string, tokFile *token.File, f *ast.File) { if len(d.Specs) == 0 { // Empty import block, remove it. - f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) + f.Decls = slices.Delete(f.Decls, i, i+1) } if !d.Lparen.IsValid() { @@ -91,7 +92,7 @@ func mergeImports(f *ast.File) { spec.(*ast.ImportSpec).Path.ValuePos = first.Pos() first.Specs = append(first.Specs, spec) } - f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) + f.Decls = slices.Delete(f.Decls, i, i+1) i-- } } diff --git a/internal/modindex/lookup.go b/internal/modindex/lookup.go index 5499c5c67f3..bd605e0d763 100644 --- a/internal/modindex/lookup.go +++ b/internal/modindex/lookup.go @@ -120,7 +120,7 @@ func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate { px.Results = int16(n) if len(flds) >= 4 { sig := strings.Split(flds[3], " ") - for i := 0; i < len(sig); i++ { + for i := range sig { // $ cannot otherwise occur. removing the spaces // almost works, but for chan struct{}, e.g. sig[i] = strings.Replace(sig[i], "$", " ", -1) @@ -136,7 +136,7 @@ func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate { func toFields(sig []string) []Field { ans := make([]Field, len(sig)/2) - for i := 0; i < len(ans); i++ { + for i := range ans { ans[i] = Field{Arg: sig[2*i], Type: sig[2*i+1]} } return ans diff --git a/internal/packagestest/export.go b/internal/packagestest/export.go index ce992e17a90..4dd2b331736 100644 --- a/internal/packagestest/export.go +++ b/internal/packagestest/export.go @@ -155,7 +155,6 @@ var All = []Exporter{GOPATH, Modules} func TestAll(t *testing.T, f func(*testing.T, Exporter)) { t.Helper() for _, e := range All { - e := e // in case f calls t.Parallel t.Run(e.Name(), func(t *testing.T) { t.Helper() f(t, e) @@ -169,7 +168,6 @@ func TestAll(t *testing.T, f func(*testing.T, Exporter)) { func BenchmarkAll(b *testing.B, f func(*testing.B, Exporter)) { b.Helper() for _, e := range All { - e := e // in case f calls t.Parallel b.Run(e.Name(), func(b *testing.B) { b.Helper() f(b, e) diff --git a/internal/pkgbits/decoder.go b/internal/pkgbits/decoder.go index f6cb37c5c3d..c0aba26c482 100644 --- a/internal/pkgbits/decoder.go +++ b/internal/pkgbits/decoder.go @@ -259,7 +259,7 @@ func (r *Decoder) rawUvarint() uint64 { func readUvarint(r *strings.Reader) (uint64, error) { var x uint64 var s uint - for i := 0; i < binary.MaxVarintLen64; i++ { + for i := range binary.MaxVarintLen64 { b, err := r.ReadByte() if err != nil { if i > 0 && err == io.EOF { diff --git a/internal/proxydir/proxydir.go b/internal/proxydir/proxydir.go index dc6b6ae94e8..bbd1ab4fd26 100644 --- a/internal/proxydir/proxydir.go +++ b/internal/proxydir/proxydir.go @@ -46,7 +46,7 @@ func WriteModuleVersion(rootDir, module, ver string, files map[string][]byte) (r } // info file, just the bare bones. - infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver)) + infoContents := fmt.Appendf(nil, `{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver) if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil { return err } diff --git a/internal/refactor/inline/calleefx_test.go b/internal/refactor/inline/calleefx_test.go index 1fc16aebaac..b643c7a06ac 100644 --- a/internal/refactor/inline/calleefx_test.go +++ b/internal/refactor/inline/calleefx_test.go @@ -107,7 +107,6 @@ func TestCalleeEffects(t *testing.T) { }, } for _, test := range tests { - test := test t.Run(test.descr, func(t *testing.T) { fset := token.NewFileSet() mustParse := func(filename string, content any) *ast.File { diff --git a/internal/refactor/inline/everything_test.go b/internal/refactor/inline/everything_test.go index 12b9ba47f21..a32e0709be1 100644 --- a/internal/refactor/inline/everything_test.go +++ b/internal/refactor/inline/everything_test.go @@ -13,6 +13,7 @@ import ( "log" "os" "path/filepath" + "slices" "strings" "testing" @@ -193,7 +194,7 @@ func TestEverything(t *testing.T) { t.Fatalf("transformed source does not parse: %v", err) } // Splice into original file list. - syntax := append([]*ast.File(nil), callerPkg.Syntax...) + syntax := slices.Clone(callerPkg.Syntax) for i := range callerPkg.Syntax { if syntax[i] == callerFile { syntax[i] = f diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 7817444150e..edd5d836613 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -26,6 +26,7 @@ import ( internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/typeparams" "golang.org/x/tools/internal/typesinternal" + "maps" ) // A Caller describes the function call and its enclosing context. @@ -893,9 +894,7 @@ func (st *state) inlineCall() (*inlineCallResult, error) { elts = append(elts, arg.expr) pure = pure && arg.pure effects = effects || arg.effects - for k, v := range arg.freevars { - freevars[k] = v - } + maps.Copy(freevars, arg.freevars) } args = append(ordinary, &argument{ expr: &ast.CompositeLit{ diff --git a/internal/refactor/inline/inline_test.go b/internal/refactor/inline/inline_test.go index 611541c9677..a3934b5cd68 100644 --- a/internal/refactor/inline/inline_test.go +++ b/internal/refactor/inline/inline_test.go @@ -64,7 +64,6 @@ func TestData(t *testing.T) { t.Fatal(err) } for _, file := range files { - file := file t.Run(filepath.Base(file), func(t *testing.T) { t.Parallel() @@ -1794,7 +1793,6 @@ func TestRedundantConversions(t *testing.T) { func runTests(t *testing.T, tests []testcase) { for _, test := range tests { - test := test t.Run(test.descr, func(t *testing.T) { fset := token.NewFileSet() mustParse := func(filename string, content any) *ast.File { @@ -1885,7 +1883,7 @@ func runTests(t *testing.T, tests []testcase) { res, err := doIt() // Want error? - if rest := strings.TrimPrefix(test.want, "error: "); rest != test.want { + if rest, ok := strings.CutPrefix(test.want, "error: "); ok { if err == nil { t.Fatalf("unexpected success: want error matching %q", rest) } diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 5c541b7b19b..fa53f37f7aa 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -149,7 +149,7 @@ func HasTool(tool string) error { func cgoEnabled(bypassEnvironment bool) (bool, error) { cmd := exec.Command("go", "env", "CGO_ENABLED") if bypassEnvironment { - cmd.Env = append(append([]string(nil), os.Environ()...), "CGO_ENABLED=") + cmd.Env = append(os.Environ(), "CGO_ENABLED=") } out, err := cmd.Output() if err != nil { @@ -251,8 +251,8 @@ func NeedsGoPackagesEnv(t testing.TB, env []string) { t.Helper() for _, v := range env { - if strings.HasPrefix(v, "GOPACKAGESDRIVER=") { - tool := strings.TrimPrefix(v, "GOPACKAGESDRIVER=") + if after, ok := strings.CutPrefix(v, "GOPACKAGESDRIVER="); ok { + tool := after if tool == "off" { NeedsTool(t, "go") } else { diff --git a/internal/typeparams/free.go b/internal/typeparams/free.go index 0ade5c2949e..709d2fc1447 100644 --- a/internal/typeparams/free.go +++ b/internal/typeparams/free.go @@ -70,7 +70,7 @@ func (w *Free) Has(typ types.Type) (res bool) { case *types.Tuple: n := t.Len() - for i := 0; i < n; i++ { + for i := range n { if w.Has(t.At(i).Type()) { return true } diff --git a/playground/socket/socket.go b/playground/socket/socket.go index 378edd4c3a5..c7843e59734 100644 --- a/playground/socket/socket.go +++ b/playground/socket/socket.go @@ -28,6 +28,7 @@ import ( "os/exec" "path/filepath" "runtime" + "slices" "strings" "time" "unicode/utf8" @@ -439,12 +440,7 @@ func (p *process) cmd(dir string, args ...string) *exec.Cmd { } func isNacl() bool { - for _, v := range append(Environ(), os.Environ()...) { - if v == "GOOS=nacl" { - return true - } - } - return false + return slices.Contains(append(Environ(), os.Environ()...), "GOOS=nacl") } // naclCmd returns an *exec.Cmd that executes bin under native client. diff --git a/playground/socket/socket_test.go b/playground/socket/socket_test.go index d410afea875..942f27e2af5 100644 --- a/playground/socket/socket_test.go +++ b/playground/socket/socket_test.go @@ -52,7 +52,7 @@ func TestLimiter(t *testing.T) { ch := make(chan *Message) go func() { var m Message - for i := 0; i < msgLimit+10; i++ { + for range msgLimit + 10 { ch <- &m } ch <- &Message{Kind: "end"} diff --git a/present/link.go b/present/link.go index ef96bf4ef6b..f6a8be1e693 100644 --- a/present/link.go +++ b/present/link.go @@ -86,10 +86,10 @@ func parseInlineLink(s string) (link string, length int) { // If the URL is http://foo.com, drop the http:// // In other words, render [[http://golang.org]] as: // golang.org - if strings.HasPrefix(rawURL, url.Scheme+"://") { - simpleURL = strings.TrimPrefix(rawURL, url.Scheme+"://") - } else if strings.HasPrefix(rawURL, url.Scheme+":") { - simpleURL = strings.TrimPrefix(rawURL, url.Scheme+":") + if after, ok := strings.CutPrefix(rawURL, url.Scheme+"://"); ok { + simpleURL = after + } else if after, ok := strings.CutPrefix(rawURL, url.Scheme+":"); ok { + simpleURL = after } } return renderLink(rawURL, simpleURL), end + 2 diff --git a/present/parse_test.go b/present/parse_test.go index dad57ea77ca..bb0fe72fad0 100644 --- a/present/parse_test.go +++ b/present/parse_test.go @@ -27,7 +27,6 @@ func TestTestdata(t *testing.T) { } files := append(filesP, filesMD...) for _, file := range files { - file := file name := filepath.Base(file) if name == "README" { continue diff --git a/refactor/eg/eg.go b/refactor/eg/eg.go index 15dfbd6ca0f..65a7f690bfd 100644 --- a/refactor/eg/eg.go +++ b/refactor/eg/eg.go @@ -14,6 +14,7 @@ import ( "go/printer" "go/token" "go/types" + "maps" "os" ) @@ -350,18 +351,10 @@ func stmtAndExpr(fn *ast.FuncDecl) ([]ast.Stmt, ast.Expr, error) { // mergeTypeInfo adds type info from src to dst. func mergeTypeInfo(dst, src *types.Info) { - for k, v := range src.Types { - dst.Types[k] = v - } - for k, v := range src.Defs { - dst.Defs[k] = v - } - for k, v := range src.Uses { - dst.Uses[k] = v - } - for k, v := range src.Selections { - dst.Selections[k] = v - } + maps.Copy(dst.Types, src.Types) + maps.Copy(dst.Defs, src.Defs) + maps.Copy(dst.Uses, src.Uses) + maps.Copy(dst.Selections, src.Selections) } // (debugging only) From e74d252b3d8b8c5eb5ba9f0bd475e3575b82f403 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 2 Apr 2025 21:39:18 -0600 Subject: [PATCH 292/309] gopls/internal/analysis/modernize: check nil before calling maybeNaN This CL adds a nil check before calling maybeNaN, as a blank identifier has no type and leads a panic. It fixes the panic in modernize minmax. Fixes golang/go#72928 Change-Id: I57d6da6b48d1c6d95057ca0f064896a935187be7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662195 LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Robert Findley --- gopls/internal/analysis/modernize/minmax.go | 11 ++++++----- .../analysis/modernize/testdata/src/minmax/minmax.go | 7 +++++++ .../modernize/testdata/src/minmax/minmax.go.golden | 7 +++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/gopls/internal/analysis/modernize/minmax.go b/gopls/internal/analysis/modernize/minmax.go index 415e9fc5661..0e43ee11c3d 100644 --- a/gopls/internal/analysis/modernize/minmax.go +++ b/gopls/internal/analysis/modernize/minmax.go @@ -178,11 +178,12 @@ func minmax(pass *analysis.Pass) { if compare, ok := ifStmt.Cond.(*ast.BinaryExpr); ok && ifStmt.Init == nil && isInequality(compare.Op) != 0 && - isAssignBlock(ifStmt.Body) && - !maybeNaN(info.TypeOf(ifStmt.Body.List[0].(*ast.AssignStmt).Lhs[0])) { // lhs - - // Have: if a < b { lhs = rhs } - check(astFile, curIfStmt, compare) + isAssignBlock(ifStmt.Body) { + // a blank var has no type. + if tLHS := info.TypeOf(ifStmt.Body.List[0].(*ast.AssignStmt).Lhs[0]); tLHS != nil && !maybeNaN(tLHS) { + // Have: if a < b { lhs = rhs } + check(astFile, curIfStmt, compare) + } } } } diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go index cd117dabf84..cdc767450d2 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go @@ -149,3 +149,10 @@ func nopeFloat(a, b myfloat) (res myfloat) { } return } + +// Regression test for golang/go#72928. +func underscoreAssign(a, b int) { + if a > b { + _ = a + } +} diff --git a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden index 23bfd6f9ecd..b7be86bf416 100644 --- a/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden +++ b/gopls/internal/analysis/modernize/testdata/src/minmax/minmax.go.golden @@ -136,3 +136,10 @@ func nopeFloat(a, b myfloat) (res myfloat) { } return } + +// Regression test for golang/go#72928. +func underscoreAssign(a, b int) { + if a > b { + _ = a + } +} From 3348ae8f7b1211bbff61a149928d43d709a722ca Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 2 Apr 2025 17:05:01 -0400 Subject: [PATCH 293/309] go/analysis/passes/nilfunc: use typesinternal.Used Replace some logic for finding the types.Object of an expression with typesinternal.Used. This covers a case that was previously missed: instantiation of a qualified function, such as pkg.F[int]. Change-Id: Ib4f8630d859d14c8e1dee792b9a49c5064fc8b61 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662277 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/passes/nilfunc/nilfunc.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/go/analysis/passes/nilfunc/nilfunc.go b/go/analysis/passes/nilfunc/nilfunc.go index 3ac2dcd4907..2b28c5a6b2c 100644 --- a/go/analysis/passes/nilfunc/nilfunc.go +++ b/go/analysis/passes/nilfunc/nilfunc.go @@ -16,7 +16,7 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/analysis/passes/internal/analysisutil" "golang.org/x/tools/go/ast/inspector" - "golang.org/x/tools/internal/typeparams" + "golang.org/x/tools/internal/typesinternal" ) //go:embed doc.go @@ -55,24 +55,8 @@ func run(pass *analysis.Pass) (any, error) { return } - // Only want identifiers or selector expressions. - var obj types.Object - switch v := e2.(type) { - case *ast.Ident: - obj = pass.TypesInfo.Uses[v] - case *ast.SelectorExpr: - obj = pass.TypesInfo.Uses[v.Sel] - case *ast.IndexExpr, *ast.IndexListExpr: - // Check generic functions such as "f[T1,T2]". - x, _, _, _ := typeparams.UnpackIndexExpr(v) - if id, ok := x.(*ast.Ident); ok { - obj = pass.TypesInfo.Uses[id] - } - default: - return - } - // Only want functions. + obj := typesinternal.Used(pass.TypesInfo, e2) if _, ok := obj.(*types.Func); !ok { return } From c788d1715fc963b7127431746444f123cf035756 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 2 Apr 2025 12:15:39 -0400 Subject: [PATCH 294/309] gopls/internal/analysis/modernize: waitgroup: use index.Calls This CL changes the new waitgroup modernizer to use index.Calls to enumerate the calls to WaitGroup.Add directly, instead of searching for all "go" statements. This is an optimization, though only a minor one because go statements are already sufficiently rare that it doesn't matter. The real purpose of the change is to try to establish the form that we wish other modernizers (which may search for more numerous nodes) to follow. Also, remove check for uses of WaitGroup type itself. The modernizer's pattern doesn't depend on the type. Change-Id: Ie87a33b08b71764ced13f204e5e0b6e0ed35d58f Reviewed-on: https://go-review.googlesource.com/c/tools/+/662276 Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam LUCI-TryBot-Result: Go LUCI Commit-Queue: Alan Donovan --- .../internal/analysis/modernize/modernize.go | 8 + .../internal/analysis/modernize/waitgroup.go | 143 ++++++++++-------- 2 files changed, 86 insertions(+), 65 deletions(-) diff --git a/gopls/internal/analysis/modernize/modernize.go b/gopls/internal/analysis/modernize/modernize.go index dbef72fe5cf..b7e943a0c51 100644 --- a/gopls/internal/analysis/modernize/modernize.go +++ b/gopls/internal/analysis/modernize/modernize.go @@ -135,6 +135,7 @@ func isIntLiteral(info *types.Info, e ast.Expr, n int64) bool { // // TODO(adonovan): opt: eliminate this function, instead following the // approach of [fmtappendf], which uses typeindex and [fileUses]. +// See "Tip" at [fileUses] for motivation. func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) iter.Seq[cursor.Cursor] { return func(yield func(cursor.Cursor) bool) { for curFile := range cursor.Root(inspect).Children() { @@ -148,6 +149,13 @@ func filesUsing(inspect *inspector.Inspector, info *types.Info, version string) // fileUses reports whether the specified file uses at least the // specified version of Go (e.g. "go1.24"). +// +// Tip: we recommend using this check "late", just before calling +// pass.Report, rather than "early" (when entering each ast.File, or +// each candidate node of interest, during the traversal), because the +// operation is not free, yet is not a highly selective filter: the +// fraction of files that pass most version checks is high and +// increases over time. func fileUses(info *types.Info, file *ast.File, version string) bool { return !versions.Before(info.FileVersions[file], version) } diff --git a/gopls/internal/analysis/modernize/waitgroup.go b/gopls/internal/analysis/modernize/waitgroup.go index 37a12da5657..080bd4d362a 100644 --- a/gopls/internal/analysis/modernize/waitgroup.go +++ b/gopls/internal/analysis/modernize/waitgroup.go @@ -10,12 +10,9 @@ import ( "slices" "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/inspect" - "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/internal/analysisinternal" typeindexanalyzer "golang.org/x/tools/internal/analysisinternal/typeindex" - "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/typesinternal/typeindex" ) @@ -32,100 +29,116 @@ import ( // => // wg.Go(go func() { ... }) // -// The wg.Done must occur within the first statement of the block in a defer format or last statement of the block, -// and the offered fix only removes the first/last wg.Done call. It doesn't fix the existing wrong usage of sync.WaitGroup. +// The wg.Done must occur within the first statement of the block in a +// defer format or last statement of the block, and the offered fix +// only removes the first/last wg.Done call. It doesn't fix existing +// wrong usage of sync.WaitGroup. +// +// The use of WaitGroup.Go in pattern 1 implicitly introduces a +// 'defer', which may change the behavior in the case of panic from +// the "..." logic. In this instance, the change is safe: before and +// after the transformation, an unhandled panic inevitably results in +// a fatal crash. The fact that the transformed code calls wg.Done() +// before the crash doesn't materially change anything. (If Done had +// other effects, or blocked, or if WaitGroup.Go propagated panics +// from child to parent goroutine, the argument would be different.) func waitgroup(pass *analysis.Pass) { var ( - inspect = pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) index = pass.ResultOf[typeindexanalyzer.Analyzer].(*typeindex.Index) info = pass.TypesInfo - syncWaitGroup = index.Object("sync", "WaitGroup") syncWaitGroupAdd = index.Selection("sync", "WaitGroup", "Add") syncWaitGroupDone = index.Selection("sync", "WaitGroup", "Done") ) - if !index.Used(syncWaitGroup, syncWaitGroupAdd, syncWaitGroupDone) { + if !index.Used(syncWaitGroupDone) { return } - checkWaitGroup := func(file *ast.File, curGostmt cursor.Cursor) { - gostmt := curGostmt.Node().(*ast.GoStmt) - - lit, ok := gostmt.Call.Fun.(*ast.FuncLit) - // go statement must have a no-arg function literal. - if !ok || len(gostmt.Call.Args) != 0 { - return + for curAddCall := range index.Calls(syncWaitGroupAdd) { + // Extract receiver from wg.Add call. + addCall := curAddCall.Node().(*ast.CallExpr) + if !isIntLiteral(info, addCall.Args[0], 1) { + continue // not a call to wg.Add(1) } + // Inv: the Args[0] check ensures addCall is not of + // the form sync.WaitGroup.Add(&wg, 1). + addCallRecv := ast.Unparen(addCall.Fun).(*ast.SelectorExpr).X - // previous node must call wg.Add. - prev, ok := curGostmt.PrevSibling() + // Following statement must be go func() { ... } (). + addStmt, ok := curAddCall.Parent().Node().(*ast.ExprStmt) if !ok { - return + continue // unnecessary parens? } - prevNode := prev.Node() - if !is[*ast.ExprStmt](prevNode) || !is[*ast.CallExpr](prevNode.(*ast.ExprStmt).X) { - return + curNext, ok := curAddCall.Parent().NextSibling() + if !ok { + continue // no successor } - - prevCall := prevNode.(*ast.ExprStmt).X.(*ast.CallExpr) - if typeutil.Callee(info, prevCall) != syncWaitGroupAdd || !isIntLiteral(info, prevCall.Args[0], 1) { - return + goStmt, ok := curNext.Node().(*ast.GoStmt) + if !ok { + continue // not a go stmt + } + lit, ok := goStmt.Call.Fun.(*ast.FuncLit) + if !ok || len(goStmt.Call.Args) != 0 { + continue // go argument is not func(){...}() } - - addCallRecv := ast.Unparen(prevCall.Fun).(*ast.SelectorExpr).X list := lit.Body.List if len(list) == 0 { - return + continue } + // Body must start with "defer wg.Done()" or end with "wg.Done()". var doneStmt ast.Stmt if deferStmt, ok := list[0].(*ast.DeferStmt); ok && typeutil.Callee(info, deferStmt.Call) == syncWaitGroupDone && equalSyntax(ast.Unparen(deferStmt.Call.Fun).(*ast.SelectorExpr).X, addCallRecv) { - // wg.Add(1); go func() { defer wg.Done(); ... }() - // --------- ------ --------------- - - // wg.Go(func() { ... } ) - doneStmt = deferStmt + doneStmt = deferStmt // "defer wg.Done()" + } else if lastStmt, ok := list[len(list)-1].(*ast.ExprStmt); ok { if doneCall, ok := lastStmt.X.(*ast.CallExpr); ok && typeutil.Callee(info, doneCall) == syncWaitGroupDone && equalSyntax(ast.Unparen(doneCall.Fun).(*ast.SelectorExpr).X, addCallRecv) { - // wg.Add(1); go func() { ... ;wg.Done();}() - // --------- ------ ---------- - - // wg.Go(func() { ... } ) - doneStmt = lastStmt + doneStmt = lastStmt // "wg.Done()" } } - if doneStmt != nil { - pass.Report(analysis.Diagnostic{ - Pos: prevNode.Pos(), - End: gostmt.End(), - Category: "waitgroup", - Message: "Goroutine creation can be simplified using WaitGroup.Go", - SuggestedFixes: []analysis.SuggestedFix{{ - Message: "Simplify by using WaitGroup.Go", - TextEdits: slices.Concat( - analysisinternal.DeleteStmt(pass.Fset, file, prevNode.(*ast.ExprStmt), nil), - analysisinternal.DeleteStmt(pass.Fset, file, doneStmt, nil), - []analysis.TextEdit{ - { - Pos: gostmt.Pos(), - End: gostmt.Call.Pos(), - NewText: fmt.Appendf(nil, "%s.Go(", addCallRecv), - }, - { - Pos: gostmt.Call.Lparen, - End: gostmt.Call.Rparen, - }, - }, - ), - }}, - }) + if doneStmt == nil { + continue } - } - for curFile := range filesUsing(inspect, info, "go1.25") { - for curGostmt := range curFile.Preorder((*ast.GoStmt)(nil)) { - checkWaitGroup(curFile.Node().(*ast.File), curGostmt) + file := enclosingFile(curAddCall) + if !fileUses(info, file, "go1.25") { + continue } + + pass.Report(analysis.Diagnostic{ + Pos: addCall.Pos(), + End: goStmt.End(), + Category: "waitgroup", + Message: "Goroutine creation can be simplified using WaitGroup.Go", + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "Simplify by using WaitGroup.Go", + TextEdits: slices.Concat( + // delete "wg.Add(1)" + analysisinternal.DeleteStmt(pass.Fset, file, addStmt, nil), + // delete "wg.Done()" or "defer wg.Done()" + analysisinternal.DeleteStmt(pass.Fset, file, doneStmt, nil), + []analysis.TextEdit{ + // go func() + // ------ + // wg.Go(func() + { + Pos: goStmt.Pos(), + End: goStmt.Call.Pos(), + NewText: fmt.Appendf(nil, "%s.Go(", addCallRecv), + }, + // ... }() + // - + // ... } ) + { + Pos: goStmt.Call.Lparen, + End: goStmt.Call.Rparen, + }, + }, + ), + }}, + }) } } From 7799973f284eec4bb6ccd238fd4207146eed1b36 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Thu, 3 Apr 2025 21:18:46 -0600 Subject: [PATCH 295/309] gopls/internal/analysis/modernize: add docs for missing modernize passes Change-Id: Ief8ff9c3fdb020208feafa21cae60a3d517b4350 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662535 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- gopls/doc/analyzers.md | 7 ++++++- gopls/internal/analysis/modernize/doc.go | 7 ++++++- gopls/internal/doc/api.json | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 82b0e8753f9..0d9fcb2313b 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -514,6 +514,11 @@ consisting of all others. This can be achieved using the -category flag: Categories of modernize diagnostic: + - forvar: remove x := x variable declarations made unnecessary by the new semantics of loops in go1.22. + + - slicescontains: replace 'for i, elem := range s { if elem == needle { ...; break }' + by a call to slices.Contains, added in go1.21. + - minmax: replace an if/else conditional assignment by a call to the built-in min or max functions added in go1.21. @@ -547,7 +552,7 @@ Categories of modernize diagnostic: - rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by "for i := range n", added in go1.22. - - stringseq: replace Split in "for range strings.Split(...)" by go1.24's + - stringsseq: replace Split in "for range strings.Split(...)" by go1.24's more efficient SplitSeq, or Fields with FieldSeq. - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, diff --git a/gopls/internal/analysis/modernize/doc.go b/gopls/internal/analysis/modernize/doc.go index 7bcde40f900..aa052540832 100644 --- a/gopls/internal/analysis/modernize/doc.go +++ b/gopls/internal/analysis/modernize/doc.go @@ -47,6 +47,11 @@ // // Categories of modernize diagnostic: // +// - forvar: remove x := x variable declarations made unnecessary by the new semantics of loops in go1.22. +// +// - slicescontains: replace 'for i, elem := range s { if elem == needle { ...; break }' +// by a call to slices.Contains, added in go1.21. +// // - minmax: replace an if/else conditional assignment by a call to // the built-in min or max functions added in go1.21. // @@ -80,7 +85,7 @@ // - rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by // "for i := range n", added in go1.22. // -// - stringseq: replace Split in "for range strings.Split(...)" by go1.24's +// - stringsseq: replace Split in "for range strings.Split(...)" by go1.24's // more efficient SplitSeq, or Fields with FieldSeq. // // - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index 9dc7aef266d..f624af8632c 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -562,7 +562,7 @@ }, { "Name": "\"modernize\"", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - forvar: remove x := x variable declarations made unnecessary by the new semantics of loops in go1.22.\n\n - slicescontains: replace 'for i, elem := range s { if elem == needle { ...; break }'\n by a call to slices.Contains, added in go1.21.\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringsseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", "Default": "true", "Status": "" }, @@ -1338,7 +1338,7 @@ }, { "Name": "modernize", - "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", + "Doc": "simplify code by using modern constructs\n\nThis analyzer reports opportunities for simplifying and clarifying\nexisting code by using more modern features of Go and its standard\nlibrary.\n\nEach diagnostic provides a fix. Our intent is that these fixes may\nbe safely applied en masse without changing the behavior of your\nprogram. In some cases the suggested fixes are imperfect and may\nlead to (for example) unused imports or unused local variables,\ncausing build breakage. However, these problems are generally\ntrivial to fix. We regard any modernizer whose fix changes program\nbehavior to have a serious bug and will endeavor to fix it.\n\nTo apply all modernization fixes en masse, you can use the\nfollowing command:\n\n\t$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...\n\nIf the tool warns of conflicting fixes, you may need to run it more\nthan once until it has applied all fixes cleanly. This command is\nnot an officially supported interface and may change in the future.\n\nChanges produced by this tool should be reviewed as usual before\nbeing merged. In some cases, a loop may be replaced by a simple\nfunction call, causing comments within the loop to be discarded.\nHuman judgment may be required to avoid losing comments of value.\n\nEach diagnostic reported by modernize has a specific category. (The\ncategories are listed below.) Diagnostics in some categories, such\nas \"efaceany\" (which replaces \"interface{}\" with \"any\" where it is\nsafe to do so) are particularly numerous. It may ease the burden of\ncode review to apply fixes in two passes, the first change\nconsisting only of fixes of category \"efaceany\", the second\nconsisting of all others. This can be achieved using the -category flag:\n\n\t$ modernize -category=efaceany -fix -test ./...\n\t$ modernize -category=-efaceany -fix -test ./...\n\nCategories of modernize diagnostic:\n\n - forvar: remove x := x variable declarations made unnecessary by the new semantics of loops in go1.22.\n\n - slicescontains: replace 'for i, elem := range s { if elem == needle { ...; break }'\n by a call to slices.Contains, added in go1.21.\n\n - minmax: replace an if/else conditional assignment by a call to\n the built-in min or max functions added in go1.21.\n\n - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[i] \u003c s[j] }\n by a call to slices.Sort(s), added in go1.21.\n\n - efaceany: replace interface{} by the 'any' type added in go1.18.\n\n - slicesclone: replace append([]T(nil), s...) by slices.Clone(s) or\n slices.Concat(s), added in go1.21.\n\n - mapsloop: replace a loop around an m[k]=v map update by a call\n to one of the Collect, Copy, Clone, or Insert functions from\n the maps package, added in go1.21.\n\n - fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),\n added in go1.19.\n\n - testingcontext: replace uses of context.WithCancel in tests\n with t.Context, added in go1.24.\n\n - omitzero: replace omitempty by omitzero on structs, added in go1.24.\n\n - bloop: replace \"for i := range b.N\" or \"for range b.N\" in a\n benchmark with \"for b.Loop()\", and remove any preceding calls\n to b.StopTimer, b.StartTimer, and b.ResetTimer.\n\n - slicesdelete: replace append(s[:i], s[i+1]...) by\n slices.Delete(s, i, i+1), added in go1.21.\n\n - rangeint: replace a 3-clause \"for i := 0; i \u003c n; i++\" loop by\n \"for i := range n\", added in go1.22.\n\n - stringsseq: replace Split in \"for range strings.Split(...)\" by go1.24's\n more efficient SplitSeq, or Fields with FieldSeq.\n\n - stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix,\n added to the strings package in go1.20.\n\n - waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.", "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize", "Default": true }, From 83a805742f78e4fdb291567ac94a7b88c57e591f Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Thu, 3 Apr 2025 23:41:08 -0600 Subject: [PATCH 296/309] x/tools: regenerate code after go upgrading This CL runs 'go generate ./...' under x/tools to update outdated code caused by gopls minimal version upgrading to go1.24.2 in CL662036. This CL also adds new environments to disable the cgo and fixs the platform and os to ensure the graph is independent of the calling environment. Change-Id: Iab75df0a5625a273c8c971e19b9ac25e3806933b Reviewed-on: https://go-review.googlesource.com/c/tools/+/662497 Reviewed-by: Carlos Amedee Reviewed-by: Alan Donovan Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- internal/stdlib/deps.go | 528 +++++++++++------------ internal/stdlib/generate.go | 2 + internal/stdlib/manifest.go | 7 - internal/stdlib/testdata/nethttp.deps | 10 +- internal/stdlib/testdata/nethttp.imports | 1 - internal/typeparams/termlist.go | 12 +- internal/typeparams/typeterm.go | 3 + 7 files changed, 282 insertions(+), 281 deletions(-) diff --git a/internal/stdlib/deps.go b/internal/stdlib/deps.go index 7cca431cd65..c50bf406b7f 100644 --- a/internal/stdlib/deps.go +++ b/internal/stdlib/deps.go @@ -12,348 +12,348 @@ type pkginfo struct { } var deps = [...]pkginfo{ - {"archive/tar", "\x03k\x03E5\x01\v\x01#\x01\x01\x02\x05\t\x02\x01\x02\x02\v"}, - {"archive/zip", "\x02\x04a\a\x16\x0205\x01+\x05\x01\x10\x03\x02\r\x04"}, - {"bufio", "\x03k}E\x13"}, - {"bytes", "n+R\x03\fG\x02\x02"}, + {"archive/tar", "\x03j\x03E6\x01\v\x01\"\x01\x01\x02\x05\n\x02\x01\x02\x02\v"}, + {"archive/zip", "\x02\x04`\a\x16\x0206\x01*\x05\x01\x11\x03\x02\r\x04"}, + {"bufio", "\x03j~E\x13"}, + {"bytes", "m+S\x03\fG\x02\x02"}, {"cmp", ""}, {"compress/bzip2", "\x02\x02\xe7\x01B"}, - {"compress/flate", "\x02l\x03z\r\x024\x01\x03"}, - {"compress/gzip", "\x02\x04a\a\x03\x15eT"}, - {"compress/lzw", "\x02l\x03z"}, - {"compress/zlib", "\x02\x04a\a\x03\x13\x01f"}, + {"compress/flate", "\x02k\x03{\r\x024\x01\x03"}, + {"compress/gzip", "\x02\x04`\a\x03\x15fT"}, + {"compress/lzw", "\x02k\x03{"}, + {"compress/zlib", "\x02\x04`\a\x03\x13\x01g"}, {"container/heap", "\xae\x02"}, {"container/list", ""}, {"container/ring", ""}, - {"context", "n\\h\x01\f"}, - {"crypto", "\x84\x01gD"}, + {"context", "m\\i\x01\f"}, + {"crypto", "\x83\x01hD"}, {"crypto/aes", "\x10\n\a\x8e\x02"}, - {"crypto/cipher", "\x03\x1e\x01\x01\x1d\x11\x1d,Q"}, - {"crypto/des", "\x10\x13\x1d.,\x95\x01\x03"}, - {"crypto/dsa", "@\x04*}\x0e"}, - {"crypto/ecdh", "\x03\v\f\x0e\x04\x14\x04\r\x1d}"}, - {"crypto/ecdsa", "\x0e\x05\x03\x04\x01\x0e\x16\x01\x04\f\x01\x1d}\x0e\x04K\x01"}, - {"crypto/ed25519", "\x0e\x1c\x16\n\a\x1d}D"}, - {"crypto/elliptic", "0>}\x0e9"}, - {"crypto/fips140", " \x05\x91\x01"}, - {"crypto/hkdf", "-\x12\x01.\x16"}, - {"crypto/hmac", "\x1a\x14\x11\x01\x113"}, - {"crypto/internal/boring", "\x0e\x02\rg"}, + {"crypto/cipher", "\x03\x1e\x01\x01\x1d\x11\x1c,R"}, + {"crypto/des", "\x10\x13\x1d-,\x96\x01\x03"}, + {"crypto/dsa", "@\x04)~\x0e"}, + {"crypto/ecdh", "\x03\v\f\x0e\x04\x14\x04\r\x1c~"}, + {"crypto/ecdsa", "\x0e\x05\x03\x04\x01\x0e\x16\x01\x04\f\x01\x1c~\x0e\x04K\x01"}, + {"crypto/ed25519", "\x0e\x1c\x16\n\a\x1c~D"}, + {"crypto/elliptic", "0=~\x0e9"}, + {"crypto/fips140", " \x05\x90\x01"}, + {"crypto/hkdf", "-\x12\x01-\x16"}, + {"crypto/hmac", "\x1a\x14\x11\x01\x112"}, + {"crypto/internal/boring", "\x0e\x02\rf"}, {"crypto/internal/boring/bbig", "\x1a\xdf\x01L"}, {"crypto/internal/boring/bcache", "\xb3\x02\x12"}, {"crypto/internal/boring/sig", ""}, - {"crypto/internal/cryptotest", "\x03\r\n)\x0e\x1a\x06\x13\x12#\a\t\x11\x11\x11\x1b\x01\f\f\x05\n"}, + {"crypto/internal/cryptotest", "\x03\r\n)\x0e\x19\x06\x13\x12#\a\t\x11\x12\x11\x1a\r\r\x05\n"}, {"crypto/internal/entropy", "E"}, - {"crypto/internal/fips140", ">0}9\f\x15"}, - {"crypto/internal/fips140/aes", "\x03\x1d\x03\x02\x13\x04\x01\x01\x05+\x8c\x015"}, - {"crypto/internal/fips140/aes/gcm", " \x01\x02\x02\x02\x11\x04\x01\x06+\x8a\x01"}, + {"crypto/internal/fips140", ">/~8\r\x15"}, + {"crypto/internal/fips140/aes", "\x03\x1d\x03\x02\x13\x04\x01\x01\x05*\x8d\x015"}, + {"crypto/internal/fips140/aes/gcm", " \x01\x02\x02\x02\x11\x04\x01\x06*\x8b\x01"}, {"crypto/internal/fips140/alias", "\xc5\x02"}, - {"crypto/internal/fips140/bigmod", "%\x17\x01\x06+\x8c\x01"}, + {"crypto/internal/fips140/bigmod", "%\x17\x01\x06*\x8d\x01"}, {"crypto/internal/fips140/check", " \x0e\x06\b\x02\xad\x01Z"}, - {"crypto/internal/fips140/check/checktest", "%\xff\x01!"}, - {"crypto/internal/fips140/drbg", "\x03\x1c\x01\x01\x04\x13\x04\b\x01)}\x0f8"}, - {"crypto/internal/fips140/ecdh", "\x03\x1d\x05\x02\t\f2}\x0f8"}, - {"crypto/internal/fips140/ecdsa", "\x03\x1d\x04\x01\x02\a\x02\x068}G"}, - {"crypto/internal/fips140/ed25519", "\x03\x1d\x05\x02\x04\v8\xc1\x01\x03"}, - {"crypto/internal/fips140/edwards25519", "%\a\f\x042\x8c\x018"}, - {"crypto/internal/fips140/edwards25519/field", "%\x13\x042\x8c\x01"}, - {"crypto/internal/fips140/hkdf", "\x03\x1d\x05\t\x06:"}, - {"crypto/internal/fips140/hmac", "\x03\x1d\x14\x01\x018"}, - {"crypto/internal/fips140/mlkem", "\x03\x1d\x05\x02\x0e\x03\x042"}, - {"crypto/internal/fips140/nistec", "%\f\a\x042\x8c\x01*\x0e\x13"}, - {"crypto/internal/fips140/nistec/fiat", "%\x136\x8c\x01"}, - {"crypto/internal/fips140/pbkdf2", "\x03\x1d\x05\t\x06:"}, - {"crypto/internal/fips140/rsa", "\x03\x1d\x04\x01\x02\r\x01\x01\x026}G"}, - {"crypto/internal/fips140/sha256", "\x03\x1d\x1c\x01\x06+\x8c\x01"}, - {"crypto/internal/fips140/sha3", "\x03\x1d\x18\x04\x011\x8c\x01K"}, - {"crypto/internal/fips140/sha512", "\x03\x1d\x1c\x01\x06+\x8c\x01"}, + {"crypto/internal/fips140/check/checktest", "%\xfe\x01\""}, + {"crypto/internal/fips140/drbg", "\x03\x1c\x01\x01\x04\x13\x04\b\x01(~\x0f8"}, + {"crypto/internal/fips140/ecdh", "\x03\x1d\x05\x02\t\f1~\x0f8"}, + {"crypto/internal/fips140/ecdsa", "\x03\x1d\x04\x01\x02\a\x02\x067~G"}, + {"crypto/internal/fips140/ed25519", "\x03\x1d\x05\x02\x04\v7\xc2\x01\x03"}, + {"crypto/internal/fips140/edwards25519", "%\a\f\x041\x8d\x018"}, + {"crypto/internal/fips140/edwards25519/field", "%\x13\x041\x8d\x01"}, + {"crypto/internal/fips140/hkdf", "\x03\x1d\x05\t\x069"}, + {"crypto/internal/fips140/hmac", "\x03\x1d\x14\x01\x017"}, + {"crypto/internal/fips140/mlkem", "\x03\x1d\x05\x02\x0e\x03\x041"}, + {"crypto/internal/fips140/nistec", "%\f\a\x041\x8d\x01)\x0f\x13"}, + {"crypto/internal/fips140/nistec/fiat", "%\x135\x8d\x01"}, + {"crypto/internal/fips140/pbkdf2", "\x03\x1d\x05\t\x069"}, + {"crypto/internal/fips140/rsa", "\x03\x1d\x04\x01\x02\r\x01\x01\x025~G"}, + {"crypto/internal/fips140/sha256", "\x03\x1d\x1c\x01\x06*\x8d\x01"}, + {"crypto/internal/fips140/sha3", "\x03\x1d\x18\x04\x010\x8d\x01K"}, + {"crypto/internal/fips140/sha512", "\x03\x1d\x1c\x01\x06*\x8d\x01"}, {"crypto/internal/fips140/ssh", " \x05"}, - {"crypto/internal/fips140/subtle", "#\x19\xbe\x01"}, - {"crypto/internal/fips140/tls12", "\x03\x1d\x05\t\x06\x028"}, - {"crypto/internal/fips140/tls13", "\x03\x1d\x05\b\a\b2"}, + {"crypto/internal/fips140/subtle", "#"}, + {"crypto/internal/fips140/tls12", "\x03\x1d\x05\t\x06\x027"}, + {"crypto/internal/fips140/tls13", "\x03\x1d\x05\b\a\b1"}, {"crypto/internal/fips140deps", ""}, - {"crypto/internal/fips140deps/byteorder", "\x9a\x01"}, - {"crypto/internal/fips140deps/cpu", "\xae\x01\a"}, - {"crypto/internal/fips140deps/godebug", "\xb6\x01"}, - {"crypto/internal/fips140hash", "5\x1a5\xc1\x01"}, - {"crypto/internal/fips140only", "'\r\x01\x01N25"}, + {"crypto/internal/fips140deps/byteorder", "\x99\x01"}, + {"crypto/internal/fips140deps/cpu", "\xad\x01\a"}, + {"crypto/internal/fips140deps/godebug", "\xb5\x01"}, + {"crypto/internal/fips140hash", "5\x1a4\xc2\x01"}, + {"crypto/internal/fips140only", "'\r\x01\x01M26"}, {"crypto/internal/fips140test", ""}, - {"crypto/internal/hpke", "\x0e\x01\x01\x03\x1a\x1d$,`M"}, + {"crypto/internal/hpke", "\x0e\x01\x01\x03\x1a\x1d#,aM"}, {"crypto/internal/impl", "\xb0\x02"}, {"crypto/internal/randutil", "\xeb\x01\x12"}, - {"crypto/internal/sysrand", "\xd7\x01@\x1b\x01\f\x06"}, - {"crypto/internal/sysrand/internal/seccomp", "n"}, - {"crypto/md5", "\x0e2.\x16\x16`"}, + {"crypto/internal/sysrand", "mi\"\x1e\r\x0f\x01\x01\v\x06"}, + {"crypto/internal/sysrand/internal/seccomp", "m"}, + {"crypto/md5", "\x0e2-\x16\x16a"}, {"crypto/mlkem", "/"}, - {"crypto/pbkdf2", "2\r\x01.\x16"}, - {"crypto/rand", "\x1a\x06\a\x19\x04\x01)}\x0eL"}, - {"crypto/rc4", "#\x1d.\xc1\x01"}, - {"crypto/rsa", "\x0e\f\x01\t\x0f\f\x01\x04\x06\a\x1d\x03\x1325\r\x01"}, - {"crypto/sha1", "\x0e\f&.\x16\x16\x14L"}, - {"crypto/sha256", "\x0e\f\x1aP"}, - {"crypto/sha3", "\x0e'O\xc1\x01"}, - {"crypto/sha512", "\x0e\f\x1cN"}, - {"crypto/subtle", "8\x98\x01T"}, - {"crypto/tls", "\x03\b\x02\x01\x01\x01\x01\x02\x01\x01\x01\x03\x01\a\x01\v\x02\n\x01\b\x05\x03\x01\x01\x01\x01\x02\x01\x02\x01\x18\x02\x03\x13\x16\x14\b5\x16\x16\r\t\x01\x01\x01\x02\x01\f\x06\x02\x01"}, + {"crypto/pbkdf2", "2\r\x01-\x16"}, + {"crypto/rand", "\x1a\x06\a\x19\x04\x01(~\x0eL"}, + {"crypto/rc4", "#\x1d-\xc2\x01"}, + {"crypto/rsa", "\x0e\f\x01\t\x0f\f\x01\x04\x06\a\x1c\x03\x1326\r\x01"}, + {"crypto/sha1", "\x0e\f&-\x16\x16\x14M"}, + {"crypto/sha256", "\x0e\f\x1aO"}, + {"crypto/sha3", "\x0e'N\xc2\x01"}, + {"crypto/sha512", "\x0e\f\x1cM"}, + {"crypto/subtle", "8\x96\x01U"}, + {"crypto/tls", "\x03\b\x02\x01\x01\x01\x01\x02\x01\x01\x01\x03\x01\a\x01\v\x02\n\x01\b\x05\x03\x01\x01\x01\x01\x02\x01\x02\x01\x17\x02\x03\x13\x16\x14\b6\x16\x15\r\n\x01\x01\x01\x02\x01\f\x06\x02\x01"}, {"crypto/tls/internal/fips140tls", " \x93\x02"}, - {"crypto/x509", "\x03\v\x01\x01\x01\x01\x01\x01\x01\x011\x03\x02\x01\x01\x02\x05\x01\x0e\x06\x02\x02\x03E5\x03\t\x01\x01\x01\a\x10\x05\t\x05\v\x01\x02\r\x02\x01\x01\x02\x03\x01"}, - {"crypto/x509/internal/macos", "\x03k'\x8f\x01\v\x10\x06"}, - {"crypto/x509/pkix", "d\x06\a\x88\x01F"}, - {"database/sql", "\x03\nK\x16\x03z\f\x06\"\x05\t\x02\x03\x01\f\x02\x02\x02"}, - {"database/sql/driver", "\ra\x03\xae\x01\x10\x10"}, - {"debug/buildinfo", "\x03X\x02\x01\x01\b\a\x03`\x18\x02\x01+\x10\x1e"}, - {"debug/dwarf", "\x03d\a\x03z1\x12\x01\x01"}, - {"debug/elf", "\x03\x06Q\r\a\x03`\x19\x01,\x18\x01\x15"}, - {"debug/gosym", "\x03d\n\xbd\x01\x01\x01\x02"}, - {"debug/macho", "\x03\x06Q\r\n`\x1a,\x18\x01"}, - {"debug/pe", "\x03\x06Q\r\a\x03`\x1a,\x18\x01\x15"}, - {"debug/plan9obj", "g\a\x03`\x1a,"}, - {"embed", "n+:\x18\x01S"}, + {"crypto/x509", "\x03\v\x01\x01\x01\x01\x01\x01\x01\x011\x03\x02\x01\x01\x02\x05\x0e\x06\x02\x02\x03E\x033\x01\x02\t\x01\x01\x01\a\x0f\x05\x01\x06\x02\x05\f\x01\x02\r\x02\x01\x01\x02\x03\x01"}, + {"crypto/x509/pkix", "c\x06\a\x89\x01F"}, + {"database/sql", "\x03\nJ\x16\x03{\f\x06!\x05\n\x02\x03\x01\f\x02\x02\x02"}, + {"database/sql/driver", "\r`\x03\xae\x01\x11\x10"}, + {"debug/buildinfo", "\x03W\x02\x01\x01\b\a\x03`\x19\x02\x01*\x0f "}, + {"debug/dwarf", "\x03c\a\x03{0\x13\x01\x01"}, + {"debug/elf", "\x03\x06P\r\a\x03`\x1a\x01+\x19\x01\x15"}, + {"debug/gosym", "\x03c\n\xbe\x01\x01\x01\x02"}, + {"debug/macho", "\x03\x06P\r\n`\x1b+\x19\x01"}, + {"debug/pe", "\x03\x06P\r\a\x03`\x1b+\x19\x01\x15"}, + {"debug/plan9obj", "f\a\x03`\x1b+"}, + {"embed", "m+:\x19\x01S"}, {"embed/internal/embedtest", ""}, {"encoding", ""}, {"encoding/ascii85", "\xeb\x01D"}, - {"encoding/asn1", "\x03k\x03\x87\x01\x01&\x0e\x02\x01\x0f\x03\x01"}, + {"encoding/asn1", "\x03j\x03\x88\x01\x01%\x0f\x02\x01\x0f\x03\x01"}, {"encoding/base32", "\xeb\x01B\x02"}, - {"encoding/base64", "\x9a\x01QB\x02"}, - {"encoding/binary", "n}\r'\x0e\x05"}, - {"encoding/csv", "\x02\x01k\x03zE\x11\x02"}, - {"encoding/gob", "\x02`\x05\a\x03`\x1a\f\x01\x02\x1d\b\x13\x01\x0e\x02"}, - {"encoding/hex", "n\x03zB\x03"}, - {"encoding/json", "\x03\x01^\x04\b\x03z\r'\x0e\x02\x01\x02\x0f\x01\x01\x02"}, - {"encoding/pem", "\x03c\b}B\x03"}, - {"encoding/xml", "\x02\x01_\f\x03z4\x05\v\x01\x02\x0f\x02"}, - {"errors", "\xca\x01{"}, - {"expvar", "kK9\t\n\x15\r\t\x02\x03\x01\x10"}, - {"flag", "b\f\x03z,\b\x05\t\x02\x01\x0f"}, - {"fmt", "nE8\r\x1f\b\x0e\x02\x03\x11"}, - {"go/ast", "\x03\x01m\x0f\x01j\x03)\b\x0e\x02\x01"}, + {"encoding/base64", "f\x85\x01B\x02"}, + {"encoding/binary", "m~\r&\x0f\x05"}, + {"encoding/csv", "\x02\x01j\x03{E\x11\x02"}, + {"encoding/gob", "\x02_\x05\a\x03`\x1b\f\x01\x02\x1c\b\x14\x01\x0e\x02"}, + {"encoding/hex", "m\x03{B\x03"}, + {"encoding/json", "\x03\x01]\x04\b\x03{\r&\x0f\x02\x01\x02\x0f\x01\x01\x02"}, + {"encoding/pem", "\x03b\b~B\x03"}, + {"encoding/xml", "\x02\x01^\f\x03{3\x05\f\x01\x02\x0f\x02"}, + {"errors", "\xc9\x01|"}, + {"expvar", "jK:\t\n\x14\r\n\x02\x03\x01\x10"}, + {"flag", "a\f\x03{+\b\x05\n\x02\x01\x0f"}, + {"fmt", "mE9\r\x1e\b\x0f\x02\x03\x11"}, + {"go/ast", "\x03\x01l\x0f\x01k\x03(\b\x0f\x02\x01"}, {"go/ast/internal/tests", ""}, - {"go/build", "\x02\x01k\x03\x01\x03\x02\a\x02\x01\x17\x1e\x04\x02\t\x14\x12\x01+\x01\x04\x01\a\t\x02\x01\x11\x02\x02"}, - {"go/build/constraint", "n\xc1\x01\x01\x11\x02"}, - {"go/constant", "q\x10w\x01\x015\x01\x02\x11"}, - {"go/doc", "\x04m\x01\x06\t=-1\x11\x02\x01\x11\x02"}, - {"go/doc/comment", "\x03n\xbc\x01\x01\x01\x01\x11\x02"}, - {"go/format", "\x03n\x01\f\x01\x02jE"}, - {"go/importer", "t\a\x01\x01\x04\x01i9"}, - {"go/internal/gccgoimporter", "\x02\x01X\x13\x03\x05\v\x01g\x02,\x01\x05\x12\x01\v\b"}, - {"go/internal/gcimporter", "\x02o\x10\x01/\x05\x0e',\x16\x03\x02"}, - {"go/internal/srcimporter", "q\x01\x02\n\x03\x01i,\x01\x05\x13\x02\x13"}, - {"go/parser", "\x03k\x03\x01\x03\v\x01j\x01+\x06\x13"}, - {"go/printer", "q\x01\x03\x03\tj\r\x1f\x16\x02\x01\x02\n\x05\x02"}, - {"go/scanner", "\x03n\x10j2\x11\x01\x12\x02"}, - {"go/token", "\x04m\xbc\x01\x02\x03\x01\x0e\x02"}, - {"go/types", "\x03\x01\x06d\x03\x01\x04\b\x03\x02\x15\x1e\x06+\x04\x03\n%\a\t\x01\x01\x01\x02\x01\x0e\x02\x02"}, - {"go/version", "\xbb\x01u"}, + {"go/build", "\x02\x01j\x03\x01\x03\x02\a\x02\x01\x17\x1e\x04\x02\t\x14\x13\x01*\x01\x04\x01\a\n\x02\x01\x11\x02\x02"}, + {"go/build/constraint", "m\xc2\x01\x01\x11\x02"}, + {"go/constant", "p\x10x\x01\x015\x01\x02\x11"}, + {"go/doc", "\x04l\x01\x06\t=.0\x12\x02\x01\x11\x02"}, + {"go/doc/comment", "\x03m\xbd\x01\x01\x01\x01\x11\x02"}, + {"go/format", "\x03m\x01\f\x01\x02kE"}, + {"go/importer", "s\a\x01\x01\x04\x01j8"}, + {"go/internal/gccgoimporter", "\x02\x01W\x13\x03\x05\v\x01h\x02+\x01\x05\x13\x01\v\b"}, + {"go/internal/gcimporter", "\x02n\x10\x01/\x05\x0e(+\x17\x03\x02"}, + {"go/internal/srcimporter", "p\x01\x02\n\x03\x01j+\x01\x05\x14\x02\x13"}, + {"go/parser", "\x03j\x03\x01\x03\v\x01k\x01*\x06\x14"}, + {"go/printer", "p\x01\x03\x03\tk\r\x1e\x17\x02\x01\x02\n\x05\x02"}, + {"go/scanner", "\x03m\x10k1\x12\x01\x12\x02"}, + {"go/token", "\x04l\xbd\x01\x02\x03\x01\x0e\x02"}, + {"go/types", "\x03\x01\x06c\x03\x01\x04\b\x03\x02\x15\x1e\x06,\x04\x03\n$\a\n\x01\x01\x01\x02\x01\x0e\x02\x02"}, + {"go/version", "\xba\x01v"}, {"hash", "\xeb\x01"}, - {"hash/adler32", "n\x16\x16"}, - {"hash/crc32", "n\x16\x16\x14\x84\x01\x01"}, - {"hash/crc64", "n\x16\x16\x98\x01"}, - {"hash/fnv", "n\x16\x16`"}, - {"hash/maphash", "\x95\x01\x05\x1b\x03@M"}, + {"hash/adler32", "m\x16\x16"}, + {"hash/crc32", "m\x16\x16\x14\x85\x01\x01\x12"}, + {"hash/crc64", "m\x16\x16\x99\x01"}, + {"hash/fnv", "m\x16\x16a"}, + {"hash/maphash", "\x94\x01\x05\x1b\x03AM"}, {"html", "\xb0\x02\x02\x11"}, - {"html/template", "\x03h\x06\x19,5\x01\v \x05\x01\x02\x03\r\x01\x02\v\x01\x03\x02"}, - {"image", "\x02l\x1f^\x0f5\x03\x01"}, + {"html/template", "\x03g\x06\x19,6\x01\v\x1f\x05\x01\x02\x03\x0e\x01\x02\v\x01\x03\x02"}, + {"image", "\x02k\x1f_\x0f5\x03\x01"}, {"image/color", ""}, - {"image/color/palette", "\x8d\x01"}, - {"image/draw", "\x8c\x01\x01\x04"}, - {"image/gif", "\x02\x01\x05f\x03\x1b\x01\x01\x01\vQ"}, - {"image/internal/imageutil", "\x8c\x01"}, - {"image/jpeg", "\x02l\x1e\x01\x04Z"}, - {"image/png", "\x02\a^\n\x13\x02\x06\x01^D"}, - {"index/suffixarray", "\x03d\a}\r*\v\x01"}, - {"internal/abi", "\xb5\x01\x90\x01"}, + {"image/color/palette", "\x8c\x01"}, + {"image/draw", "\x8b\x01\x01\x04"}, + {"image/gif", "\x02\x01\x05e\x03\x1b\x01\x01\x01\vR"}, + {"image/internal/imageutil", "\x8b\x01"}, + {"image/jpeg", "\x02k\x1e\x01\x04["}, + {"image/png", "\x02\a]\n\x13\x02\x06\x01_D"}, + {"index/suffixarray", "\x03c\a~\r)\f\x01"}, + {"internal/abi", "\xb4\x01\x91\x01"}, {"internal/asan", "\xc5\x02"}, - {"internal/bisect", "\xa4\x02\x0e\x01"}, - {"internal/buildcfg", "qG_\x06\x02\x05\v\x01"}, - {"internal/bytealg", "\xae\x01\x97\x01"}, + {"internal/bisect", "\xa3\x02\x0f\x01"}, + {"internal/buildcfg", "pG_\x06\x02\x05\f\x01"}, + {"internal/bytealg", "\xad\x01\x98\x01"}, {"internal/byteorder", ""}, {"internal/cfg", ""}, - {"internal/chacha8rand", "\x9a\x01\x1b\x90\x01"}, + {"internal/chacha8rand", "\x99\x01\x1b\x91\x01"}, {"internal/copyright", ""}, {"internal/coverage", ""}, {"internal/coverage/calloc", ""}, - {"internal/coverage/cfile", "k\x06\x17\x16\x01\x02\x01\x01\x01\x01\x01\x01\x01$\x01\x1e,\x06\a\v\x01\x03\f\x06"}, - {"internal/coverage/cformat", "\x04m-\x04I\f6\x01\x02\f"}, - {"internal/coverage/cmerge", "q-Z"}, - {"internal/coverage/decodecounter", "g\n-\v\x02@,\x18\x16"}, - {"internal/coverage/decodemeta", "\x02e\n\x17\x16\v\x02@,"}, - {"internal/coverage/encodecounter", "\x02e\n-\f\x01\x02>\f \x16"}, - {"internal/coverage/encodemeta", "\x02\x01d\n\x13\x04\x16\r\x02>,."}, - {"internal/coverage/pods", "\x04m-y\x06\x05\v\x02\x01"}, + {"internal/coverage/cfile", "j\x06\x17\x16\x01\x02\x01\x01\x01\x01\x01\x01\x01#\x01 +\x06\a\f\x01\x03\f\x06"}, + {"internal/coverage/cformat", "\x04l-\x04J\f6\x01\x02\f"}, + {"internal/coverage/cmerge", "p-["}, + {"internal/coverage/decodecounter", "f\n-\v\x02A+\x19\x16"}, + {"internal/coverage/decodemeta", "\x02d\n\x17\x16\v\x02A+"}, + {"internal/coverage/encodecounter", "\x02d\n-\f\x01\x02?\f\x1f\x17"}, + {"internal/coverage/encodemeta", "\x02\x01c\n\x13\x04\x16\r\x02?+/"}, + {"internal/coverage/pods", "\x04l-y\x06\x05\f\x02\x01"}, {"internal/coverage/rtcov", "\xc5\x02"}, - {"internal/coverage/slicereader", "g\nzZ"}, - {"internal/coverage/slicewriter", "qz"}, - {"internal/coverage/stringtab", "q8\x04>"}, + {"internal/coverage/slicereader", "f\n{Z"}, + {"internal/coverage/slicewriter", "p{"}, + {"internal/coverage/stringtab", "p8\x04?"}, {"internal/coverage/test", ""}, {"internal/coverage/uleb128", ""}, {"internal/cpu", "\xc5\x02"}, - {"internal/dag", "\x04m\xbc\x01\x03"}, - {"internal/diff", "\x03n\xbd\x01\x02"}, - {"internal/exportdata", "\x02\x01k\x03\x03]\x1a,\x01\x05\x12\x01\x02"}, - {"internal/filepathlite", "n+:\x19A"}, - {"internal/fmtsort", "\x04\x9b\x02\x0e"}, - {"internal/fuzz", "\x03\nA\x19\x04\x03\x03\x01\f\x0355\r\x02\x1d\x01\x05\x02\x05\v\x01\x02\x01\x01\v\x04\x02"}, + {"internal/dag", "\x04l\xbd\x01\x03"}, + {"internal/diff", "\x03m\xbe\x01\x02"}, + {"internal/exportdata", "\x02\x01j\x03\x03]\x1b+\x01\x05\x13\x01\x02"}, + {"internal/filepathlite", "m+:\x1aA"}, + {"internal/fmtsort", "\x04\x9a\x02\x0f"}, + {"internal/fuzz", "\x03\nA\x18\x04\x03\x03\x01\f\x0356\r\x02\x1c\x01\x05\x02\x05\f\x01\x02\x01\x01\v\x04\x02"}, {"internal/goarch", ""}, - {"internal/godebug", "\x97\x01 {\x01\x12"}, + {"internal/godebug", "\x96\x01 |\x01\x12"}, {"internal/godebugs", ""}, {"internal/goexperiment", ""}, {"internal/goos", ""}, - {"internal/goroot", "\x97\x02\x01\x05\x13\x02"}, + {"internal/goroot", "\x96\x02\x01\x05\x14\x02"}, {"internal/gover", "\x04"}, {"internal/goversion", ""}, {"internal/itoa", ""}, - {"internal/lazyregexp", "\x97\x02\v\x0e\x02"}, - {"internal/lazytemplate", "\xeb\x01,\x19\x02\v"}, + {"internal/lazyregexp", "\x96\x02\v\x0f\x02"}, + {"internal/lazytemplate", "\xeb\x01+\x1a\x02\v"}, {"internal/msan", "\xc5\x02"}, {"internal/nettrace", ""}, - {"internal/obscuretestdata", "f\x85\x01,"}, - {"internal/oserror", "n"}, - {"internal/pkgbits", "\x03K\x19\a\x03\x05\vj\x0e\x1e\r\v\x01"}, + {"internal/obscuretestdata", "e\x86\x01+"}, + {"internal/oserror", "m"}, + {"internal/pkgbits", "\x03K\x18\a\x03\x05\vk\x0e\x1d\r\f\x01"}, {"internal/platform", ""}, - {"internal/poll", "nO\x1a\x149\x0e\x01\x01\v\x06"}, - {"internal/profile", "\x03\x04g\x03z7\f\x01\x01\x0f"}, + {"internal/poll", "mO\x1a\x158\x0f\x01\x01\v\x06"}, + {"internal/profile", "\x03\x04f\x03{6\r\x01\x01\x0f"}, {"internal/profilerecord", ""}, - {"internal/race", "\x95\x01\xb0\x01"}, - {"internal/reflectlite", "\x95\x01 3+\x1a\x02"}, {"internal/syslist", ""}, - {"internal/testenv", "\x03\na\x02\x01*\x1a\x10'+\x01\x05\a\v\x01\x02\x02\x01\n"}, + {"internal/testenv", "\x03\n`\x02\x01*\x1a\x10(*\x01\x05\a\f\x01\x02\x02\x01\n"}, {"internal/testlog", "\xb2\x02\x01\x12"}, - {"internal/testpty", "n\x03f@\x1d"}, - {"internal/trace", "\x02\x01\x01\x06]\a\x03n\x03\x03\x06\x03\n5\x01\x02\x0f\x06"}, - {"internal/trace/internal/testgen", "\x03d\nl\x03\x02\x03\x011\v\x0e"}, - {"internal/trace/internal/tracev1", "\x03\x01c\a\x03t\x06\r5\x01"}, - {"internal/trace/raw", "\x02e\nq\x03\x06D\x01\x11"}, - {"internal/trace/testtrace", "\x02\x01k\x03l\x03\x06\x057\v\x02\x01"}, - {"internal/trace/tracev2", ""}, - {"internal/trace/traceviewer", "\x02^\v\x06\x1a<\x16\a\a\x04\t\n\x15\x01\x05\a\v\x01\x02\r"}, + {"internal/testpty", "m\x03\xa6\x01"}, + {"internal/trace", "\x02\x01\x01\x06\\\a\x03m\x01\x01\x06\x06\x03\n5\x01\x02\x0f"}, + {"internal/trace/event", ""}, + {"internal/trace/event/go122", "pm"}, + {"internal/trace/internal/oldtrace", "\x03\x01b\a\x03m\b\x06\r5\x01"}, + {"internal/trace/internal/testgen/go122", "\x03c\nl\x01\x01\x03\x04\x010\v\x0f"}, + {"internal/trace/raw", "\x02d\nm\b\x06D\x01\x11"}, + {"internal/trace/testtrace", "\x02\x01j\x03l\x05\x05\x056\f\x02\x01"}, + {"internal/trace/traceviewer", "\x02]\v\x06\x1a<\x16\b\a\x04\t\n\x14\x01\x05\a\f\x01\x02\r"}, {"internal/trace/traceviewer/format", ""}, - {"internal/trace/version", "qq\t"}, - {"internal/txtar", "\x03n\xa6\x01\x19"}, + {"internal/trace/version", "pm\x01\r"}, + {"internal/txtar", "\x03m\xa6\x01\x1a"}, {"internal/types/errors", "\xaf\x02"}, {"internal/unsafeheader", "\xc5\x02"}, - {"internal/xcoff", "Z\r\a\x03`\x1a,\x18\x01"}, - {"internal/zstd", "g\a\x03z\x0f"}, - {"io", "n\xc4\x01"}, - {"io/fs", "n+*(1\x11\x12\x04"}, - {"io/ioutil", "\xeb\x01\x01+\x16\x03"}, - {"iter", "\xc9\x01[!"}, - {"log", "qz\x05'\r\x0e\x01\f"}, + {"internal/xcoff", "Y\r\a\x03`\x1b+\x19\x01"}, + {"internal/zstd", "f\a\x03{\x0f"}, + {"io", "m\xc5\x01"}, + {"io/fs", "m+*)0\x12\x12\x04"}, + {"io/ioutil", "\xeb\x01\x01*\x17\x03"}, + {"iter", "\xc8\x01[\""}, + {"log", "p{\x05&\r\x0f\x01\f"}, {"log/internal", ""}, - {"log/slog", "\x03\nU\t\x03\x03z\x04\x01\x02\x02\x04'\x05\t\x02\x01\x02\x01\f\x02\x02\x02"}, + {"log/slog", "\x03\nT\t\x03\x03{\x04\x01\x02\x02\x04&\x05\n\x02\x01\x02\x01\f\x02\x02\x02"}, {"log/slog/internal", ""}, - {"log/slog/internal/benchmarks", "\ra\x03z\x06\x03;\x10"}, + {"log/slog/internal/benchmarks", "\r`\x03{\x06\x03;\x10"}, {"log/slog/internal/buffer", "\xb2\x02"}, {"log/slog/internal/slogtest", "\xf1\x01"}, - {"log/syslog", "n\x03~\x12\x16\x19\x02\r"}, + {"log/syslog", "m\x03\x7f\x12\x15\x1a\x02\r"}, {"maps", "\xee\x01W"}, - {"math", "\xfa\x01K"}, - {"math/big", "\x03k\x03)Q\r\x02\x021\x02\x01\x02\x13"}, + {"math", "\xad\x01MK"}, + {"math/big", "\x03j\x03)\x14>\r\x02\x023\x01\x02\x13"}, {"math/bits", "\xc5\x02"}, {"math/cmplx", "\xf8\x01\x02"}, - {"math/rand", "\xb6\x01B:\x01\x12"}, - {"math/rand/v2", "n,\x02\\\x02K"}, - {"mime", "\x02\x01c\b\x03z\f \x16\x03\x02\x0f\x02"}, - {"mime/multipart", "\x02\x01G$\x03E5\f\x01\x06\x02\x15\x02\x06\x10\x02\x01\x15"}, - {"mime/quotedprintable", "\x02\x01nz"}, - {"net", "\x04\ta+\x1d\a\x04\x05\x05\a\x01\x04\x14\x01%\x06\r\t\x05\x01\x01\v\x06\a"}, - {"net/http", "\x02\x01\x04\x04\x02=\b\x14\x01\a\x03E5\x01\x03\b\x01\x02\x02\x02\x01\x02\x06\x02\x01\x01\n\x01\x01\x05\x01\x02\x05\t\x01\x01\x01\x02\x01\f\x02\x02\x02\b\x01\x01\x01"}, - {"net/http/cgi", "\x02P\x1c\x03z\x04\b\n\x01\x13\x01\x01\x01\x04\x01\x05\x02\t\x02\x01\x0f\x0e"}, - {"net/http/cookiejar", "\x04j\x03\x90\x01\x01\b\f\x17\x03\x02\r\x04"}, - {"net/http/fcgi", "\x02\x01\nZ\a\x03z\x16\x01\x01\x14\x19\x02\r"}, - {"net/http/httptest", "\x02\x01\nE\x02\x1c\x01z\x04\x12\x01\n\t\x02\x18\x01\x02\r\x0e"}, - {"net/http/httptrace", "\rEo@\x14\n "}, - {"net/http/httputil", "\x02\x01\na\x03z\x04\x0f\x03\x01\x05\x02\x01\v\x01\x1a\x02\r\x0e"}, - {"net/http/internal", "\x02\x01k\x03z"}, + {"math/rand", "\xb5\x01C:\x01\x12"}, + {"math/rand/v2", "m,\x02]\x02K"}, + {"mime", "\x02\x01b\b\x03{\f\x1f\x17\x03\x02\x0f\x02"}, + {"mime/multipart", "\x02\x01G#\x03E6\f\x01\x06\x02\x14\x02\x06\x11\x02\x01\x15"}, + {"mime/quotedprintable", "\x02\x01m{"}, + {"net", "\x04\t`+\x1d\a\x04\x05\f\x01\x04\x15\x01$\x06\r\n\x05\x01\x01\v\x06\a"}, + {"net/http", "\x02\x01\x04\x04\x02=\b\x13\x01\a\x03E6\x01\x03\b\x01\x02\x02\x02\x01\x02\x06\x02\x01\n\x01\x01\x05\x01\x02\x05\n\x01\x01\x01\x02\x01\f\x02\x02\x02\b\x01\x01\x01"}, + {"net/http/cgi", "\x02P\x1b\x03{\x04\b\n\x01\x12\x01\x01\x01\x04\x01\x05\x02\n\x02\x01\x0f\x0e"}, + {"net/http/cookiejar", "\x04i\x03\x91\x01\x01\b\v\x18\x03\x02\r\x04"}, + {"net/http/fcgi", "\x02\x01\nY\a\x03{\x16\x01\x01\x13\x1a\x02\r"}, + {"net/http/httptest", "\x02\x01\nE\x02\x1b\x01{\x04\x12\x01\t\t\x02\x19\x01\x02\r\x0e"}, + {"net/http/httptrace", "\rEnA\x13\n!"}, + {"net/http/httputil", "\x02\x01\n`\x03{\x04\x0f\x03\x01\x05\x02\x01\n\x01\x1b\x02\r\x0e"}, + {"net/http/internal", "\x02\x01j\x03{"}, {"net/http/internal/ascii", "\xb0\x02\x11"}, - {"net/http/internal/httpcommon", "\ra\x03\x96\x01\x0e\x01\x18\x01\x01\x02\x1b\x02"}, {"net/http/internal/testcert", "\xb0\x02"}, - {"net/http/pprof", "\x02\x01\nd\x19,\x11$\x04\x13\x14\x01\r\x06\x02\x01\x02\x01\x0f"}, - {"net/internal/cgotest", "\xd7\x01n"}, - {"net/internal/socktest", "q\xc1\x01\x02"}, - {"net/mail", "\x02l\x03z\x04\x0f\x03\x14\x1b\x02\r\x04"}, - {"net/netip", "\x04j+\x01#;\x025\x15"}, - {"net/rpc", "\x02g\x05\x03\x10\n`\x04\x12\x01\x1d\x0e\x03\x02"}, - {"net/rpc/jsonrpc", "k\x03\x03z\x16\x11 "}, - {"net/smtp", "\x19.\v\x14\b\x03z\x16\x14\x1b"}, - {"net/textproto", "\x02\x01k\x03z\r\t.\x01\x02\x13"}, - {"net/url", "n\x03\x86\x01%\x11\x02\x01\x15"}, - {"os", "n+\x19\v\t\r\x03\x01\x04\x10\x018\t\x05\x01\x01\v\x06"}, - {"os/exec", "\x03\naH \x01\x14\x01+\x06\a\v\x01\x04\v"}, + {"net/http/pprof", "\x02\x01\nc\x19,\x11%\x04\x13\x13\x01\r\x06\x03\x01\x02\x01\x0f"}, + {"net/internal/cgotest", ""}, + {"net/internal/socktest", "p\xc2\x01\x02"}, + {"net/mail", "\x02k\x03{\x04\x0f\x03\x13\x1c\x02\r\x04"}, + {"net/netip", "\x04i+\x01#<\x025\x15"}, + {"net/rpc", "\x02f\x05\x03\x10\na\x04\x12\x01\x1c\x0f\x03\x02"}, + {"net/rpc/jsonrpc", "j\x03\x03{\x16\x10!"}, + {"net/smtp", "\x19.\v\x13\b\x03{\x16\x13\x1c"}, + {"net/textproto", "\x02\x01j\x03{\r\t.\x01\x02\x13"}, + {"net/url", "m\x03\x87\x01$\x12\x02\x01\x15"}, + {"os", "m+\x01\x18\x03\b\t\r\x03\x01\x04\x11\x017\n\x05\x01\x01\v\x06"}, + {"os/exec", "\x03\n`H \x01\x15\x01*\x06\a\f\x01\x04\v"}, {"os/exec/internal/fdtest", "\xb4\x02"}, - {"os/signal", "\r\x8a\x02\x16\x05\x02"}, - {"os/user", "qfM\v\x01\x02\x02\x11"}, - {"path", "n+\xaa\x01"}, - {"path/filepath", "n+\x19:+\r\t\x03\x04\x0f"}, - {"plugin", "n\xc4\x01\x13"}, - {"reflect", "n'\x04\x1c\b\f\x05\x02\x18\x06\n,\v\x03\x0f\x02\x02"}, + {"os/signal", "\r\x89\x02\x17\x05\x02"}, + {"os/user", "\x02\x01j\x03{+\r\f\x01\x02"}, + {"path", "m+\xab\x01"}, + {"path/filepath", "m+\x19;*\r\n\x03\x04\x0f"}, + {"plugin", "m"}, + {"reflect", "m'\x04\x1c\b\f\x04\x02\x1a\x06\n+\f\x03\x0f\x02\x02"}, {"reflect/internal/example1", ""}, {"reflect/internal/example2", ""}, - {"regexp", "\x03\xe8\x018\n\x02\x01\x02\x0f\x02"}, + {"regexp", "\x03\xe8\x017\v\x02\x01\x02\x0f\x02"}, {"regexp/syntax", "\xad\x02\x01\x01\x01\x11\x02"}, - {"runtime", "\x95\x01\x04\x01\x02\f\x06\a\x02\x01\x01\x0f\x04\x01\x01\x01\x01\x03\x0fc"}, - {"runtime/cgo", "\xd0\x01b\x01\x12"}, - {"runtime/coverage", "\xa0\x01K"}, - {"runtime/debug", "qUQ\r\t\x02\x01\x0f\x06"}, + {"runtime", "\x94\x01\x04\x01\x02\f\x06\a\x02\x01\x01\x0f\x03\x01\x01\x01\x01\x01\x03s"}, + {"runtime/coverage", "\x9f\x01L"}, + {"runtime/debug", "pUQ\r\n\x02\x01\x0f\x06"}, + {"runtime/internal/startlinetest", ""}, {"runtime/internal/wasitest", ""}, - {"runtime/metrics", "\xb7\x01A,!"}, - {"runtime/pprof", "\x02\x01\x01\x03\x06Z\a\x03$3#\r\x1f\r\t\x01\x01\x01\x02\x02\b\x03\x06"}, - {"runtime/race", ""}, - {"runtime/trace", "\rdz9\x0e\x01\x12"}, + {"runtime/metrics", "\xb6\x01B+\""}, + {"runtime/pprof", "\x02\x01\x01\x03\x06Y\a\x03$3$\r\x1e\r\n\x01\x01\x01\x02\x02\b\x03\x06"}, + {"runtime/race", "\xab\x02"}, + {"runtime/race/internal/amd64v1", ""}, + {"runtime/trace", "\rc{8\x0f\x01\x12"}, {"slices", "\x04\xea\x01\fK"}, - {"sort", "\xca\x0103"}, - {"strconv", "n+:%\x02I"}, - {"strings", "n'\x04:\x18\x03\f8\x0f\x02\x02"}, + {"sort", "\xc9\x0113"}, + {"strconv", "m+:&\x02I"}, + {"strings", "m'\x04:\x19\x03\f8\x0f\x02\x02"}, {"structs", ""}, - {"sync", "\xc9\x01\vP\x0f\x12"}, + {"sync", "\xc8\x01\vP\x10\x12"}, {"sync/atomic", "\xc5\x02"}, - {"syscall", "n'\x01\x03\x01\x1b\b\x03\x03\x06[\x0e\x01\x12"}, - {"testing", "\x03\na\x02\x01X\x0f\x13\r\x04\x1b\x06\x02\x05\x03\x05\x01\x02\x01\x02\x01\f\x02\x02\x02"}, - {"testing/fstest", "n\x03z\x01\v%\x11\x03\b\a"}, - {"testing/internal/testdeps", "\x02\v\xa7\x01'\x10,\x03\x05\x03\b\x06\x02\r"}, - {"testing/iotest", "\x03k\x03z\x04"}, - {"testing/quick", "p\x01\x87\x01\x04#\x11\x0f"}, - {"testing/slogtest", "\ra\x03\x80\x01.\x05\x11\n"}, - {"text/scanner", "\x03nz,*\x02"}, - {"text/tabwriter", "qzX"}, - {"text/template", "n\x03B8\x01\v\x1f\x01\x05\x01\x02\x05\f\x02\f\x03\x02"}, - {"text/template/parse", "\x03n\xb3\x01\v\x01\x11\x02"}, - {"time", "n+\x1d\x1d'*\x0e\x02\x11"}, - {"time/tzdata", "n\xc6\x01\x11"}, + {"syscall", "m(\x03\x01\x1b\b\x03\x03\x06\aT\x0f\x01\x12"}, + {"testing", "\x03\n`\x02\x01G\x11\x0f\x14\r\x04\x1a\x06\x02\x05\x02\a\x01\x02\x01\x02\x01\f\x02\x02\x02"}, + {"testing/fstest", "m\x03{\x01\v$\x12\x03\b\a"}, + {"testing/internal/testdeps", "\x02\v\xa6\x01'\x11+\x03\x05\x03\b\a\x02\r"}, + {"testing/iotest", "\x03j\x03{\x04"}, + {"testing/quick", "o\x01\x88\x01\x04\"\x12\x0f"}, + {"testing/slogtest", "\r`\x03\x81\x01-\x05\x12\n"}, + {"text/scanner", "\x03m{++\x02"}, + {"text/tabwriter", "p{X"}, + {"text/template", "m\x03B9\x01\v\x1e\x01\x05\x01\x02\x05\r\x02\f\x03\x02"}, + {"text/template/parse", "\x03m\xb3\x01\f\x01\x11\x02"}, + {"time", "m+\x1d\x1d()\x0f\x02\x11"}, + {"time/tzdata", "m\xc7\x01\x11"}, {"unicode", ""}, {"unicode/utf16", ""}, {"unicode/utf8", ""}, - {"unique", "\x95\x01>\x01P\x0e\x13\x12"}, + {"unique", "\x94\x01>\x01P\x0f\x13\x12"}, {"unsafe", ""}, - {"vendor/golang.org/x/crypto/chacha20", "\x10W\a\x8c\x01*&"}, - {"vendor/golang.org/x/crypto/chacha20poly1305", "\x10W\a\xd8\x01\x04\x01"}, - {"vendor/golang.org/x/crypto/cryptobyte", "d\n\x03\x88\x01& \n"}, + {"vendor/golang.org/x/crypto/chacha20", "\x10V\a\x8d\x01)'"}, + {"vendor/golang.org/x/crypto/chacha20poly1305", "\x10V\a\xd9\x01\x04\x01\a"}, + {"vendor/golang.org/x/crypto/cryptobyte", "c\n\x03\x89\x01%!\n"}, {"vendor/golang.org/x/crypto/cryptobyte/asn1", ""}, {"vendor/golang.org/x/crypto/internal/alias", "\xc5\x02"}, - {"vendor/golang.org/x/crypto/internal/poly1305", "Q\x16\x93\x01"}, - {"vendor/golang.org/x/net/dns/dnsmessage", "n"}, - {"vendor/golang.org/x/net/http/httpguts", "\x81\x02\x14\x1b\x13\r"}, - {"vendor/golang.org/x/net/http/httpproxy", "n\x03\x90\x01\x15\x01\x19\x13\r"}, - {"vendor/golang.org/x/net/http2/hpack", "\x03k\x03zG"}, - {"vendor/golang.org/x/net/idna", "q\x87\x018\x13\x10\x02\x01"}, - {"vendor/golang.org/x/net/nettest", "\x03d\a\x03z\x11\x05\x16\x01\f\v\x01\x02\x02\x01\n"}, - {"vendor/golang.org/x/sys/cpu", "\x97\x02\r\v\x01\x15"}, - {"vendor/golang.org/x/text/secure/bidirule", "n\xd5\x01\x11\x01"}, - {"vendor/golang.org/x/text/transform", "\x03k}X"}, - {"vendor/golang.org/x/text/unicode/bidi", "\x03\bf~?\x15"}, - {"vendor/golang.org/x/text/unicode/norm", "g\nzG\x11\x11"}, - {"weak", "\x95\x01\x8f\x01!"}, + {"vendor/golang.org/x/crypto/internal/poly1305", "Q\x15\x94\x01"}, + {"vendor/golang.org/x/net/dns/dnsmessage", "m"}, + {"vendor/golang.org/x/net/http/httpguts", "\x81\x02\x13\x1c\x13\r"}, + {"vendor/golang.org/x/net/http/httpproxy", "m\x03\x91\x01\x0f\x05\x01\x1a\x13\r"}, + {"vendor/golang.org/x/net/http2/hpack", "\x03j\x03{G"}, + {"vendor/golang.org/x/net/idna", "p\x88\x018\x13\x10\x02\x01"}, + {"vendor/golang.org/x/net/nettest", "\x03c\a\x03{\x11\x05\x15\x01\f\f\x01\x02\x02\x01\n"}, + {"vendor/golang.org/x/sys/cpu", "\x96\x02\r\f\x01\x15"}, + {"vendor/golang.org/x/text/secure/bidirule", "m\xd6\x01\x11\x01"}, + {"vendor/golang.org/x/text/transform", "\x03j~X"}, + {"vendor/golang.org/x/text/unicode/bidi", "\x03\be\x7f?\x15"}, + {"vendor/golang.org/x/text/unicode/norm", "f\n{G\x11\x11"}, + {"weak", "\x94\x01\x8f\x01\""}, } diff --git a/internal/stdlib/generate.go b/internal/stdlib/generate.go index 4c67d8bd797..cfef0a2438f 100644 --- a/internal/stdlib/generate.go +++ b/internal/stdlib/generate.go @@ -246,6 +246,7 @@ func deps() { cmd := exec.Command("go", "list", "-deps", "-json", "std") cmd.Stdout = stdout cmd.Stderr = os.Stderr + cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64") if err := cmd.Run(); err != nil { log.Fatal(err) } @@ -336,6 +337,7 @@ var deps = [...]pkginfo{ cmd := exec.Command("go", "list", t.flag, "net/http") cmd.Stdout = stdout cmd.Stderr = os.Stderr + cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64") if err := cmd.Run(); err != nil { log.Fatal(err) } diff --git a/internal/stdlib/manifest.go b/internal/stdlib/manifest.go index 00776a31b60..2b418796abb 100644 --- a/internal/stdlib/manifest.go +++ b/internal/stdlib/manifest.go @@ -7119,7 +7119,6 @@ var PackageSymbols = map[string][]Symbol{ {"FormatFileInfo", Func, 21}, {"Glob", Func, 16}, {"GlobFS", Type, 16}, - {"Lstat", Func, 25}, {"ModeAppend", Const, 16}, {"ModeCharDevice", Const, 16}, {"ModeDevice", Const, 16}, @@ -7144,8 +7143,6 @@ var PackageSymbols = map[string][]Symbol{ {"ReadDirFile", Type, 16}, {"ReadFile", Func, 16}, {"ReadFileFS", Type, 16}, - {"ReadLink", Func, 25}, - {"ReadLinkFS", Type, 25}, {"SkipAll", Var, 20}, {"SkipDir", Var, 16}, {"Stat", Func, 16}, @@ -9149,8 +9146,6 @@ var PackageSymbols = map[string][]Symbol{ {"(*ProcessState).SysUsage", Method, 0}, {"(*ProcessState).SystemTime", Method, 0}, {"(*ProcessState).UserTime", Method, 0}, - {"(*Root).Chmod", Method, 25}, - {"(*Root).Chown", Method, 25}, {"(*Root).Close", Method, 24}, {"(*Root).Create", Method, 24}, {"(*Root).FS", Method, 24}, @@ -16759,11 +16754,9 @@ var PackageSymbols = map[string][]Symbol{ }, "testing/fstest": { {"(MapFS).Glob", Method, 16}, - {"(MapFS).Lstat", Method, 25}, {"(MapFS).Open", Method, 16}, {"(MapFS).ReadDir", Method, 16}, {"(MapFS).ReadFile", Method, 16}, - {"(MapFS).ReadLink", Method, 25}, {"(MapFS).Stat", Method, 16}, {"(MapFS).Sub", Method, 16}, {"MapFS", Type, 16}, diff --git a/internal/stdlib/testdata/nethttp.deps b/internal/stdlib/testdata/nethttp.deps index e1235e84932..71e58a0c693 100644 --- a/internal/stdlib/testdata/nethttp.deps +++ b/internal/stdlib/testdata/nethttp.deps @@ -19,8 +19,8 @@ internal/race internal/runtime/math internal/runtime/sys internal/runtime/maps +internal/runtime/syscall internal/stringslite -internal/trace/tracev2 runtime internal/reflectlite errors @@ -122,6 +122,7 @@ crypto/internal/fips140/tls13 vendor/golang.org/x/crypto/internal/alias vendor/golang.org/x/crypto/chacha20 vendor/golang.org/x/crypto/internal/poly1305 +vendor/golang.org/x/sys/cpu vendor/golang.org/x/crypto/chacha20poly1305 crypto/internal/hpke crypto/md5 @@ -132,7 +133,6 @@ crypto/sha1 crypto/sha256 crypto/tls/internal/fips140tls crypto/dsa -crypto/x509/internal/macos encoding/hex crypto/x509/pkix encoding/base64 @@ -140,13 +140,13 @@ encoding/pem maps vendor/golang.org/x/net/dns/dnsmessage internal/nettrace +internal/singleflight weak unique net/netip -internal/routebsd -internal/singleflight net net/url +path/filepath crypto/x509 crypto/tls vendor/golang.org/x/text/transform @@ -162,10 +162,8 @@ vendor/golang.org/x/net/http/httpproxy vendor/golang.org/x/net/http2/hpack mime mime/quotedprintable -path/filepath mime/multipart net/http/httptrace net/http/internal net/http/internal/ascii -net/http/internal/httpcommon net/http diff --git a/internal/stdlib/testdata/nethttp.imports b/internal/stdlib/testdata/nethttp.imports index 77e78696bdd..de41e46c0fe 100644 --- a/internal/stdlib/testdata/nethttp.imports +++ b/internal/stdlib/testdata/nethttp.imports @@ -27,7 +27,6 @@ net net/http/httptrace net/http/internal net/http/internal/ascii -net/http/internal/httpcommon net/textproto net/url os diff --git a/internal/typeparams/termlist.go b/internal/typeparams/termlist.go index cbd12f80131..9bc29143f6a 100644 --- a/internal/typeparams/termlist.go +++ b/internal/typeparams/termlist.go @@ -1,3 +1,6 @@ +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. +// Source: ../../cmd/compile/internal/types2/termlist.go + // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -7,8 +10,8 @@ package typeparams import ( - "bytes" "go/types" + "strings" ) // A termlist represents the type set represented by the union @@ -22,15 +25,18 @@ type termlist []*term // It is in normal form. var allTermlist = termlist{new(term)} +// termSep is the separator used between individual terms. +const termSep = " | " + // String prints the termlist exactly (without normalization). func (xl termlist) String() string { if len(xl) == 0 { return "∅" } - var buf bytes.Buffer + var buf strings.Builder for i, x := range xl { if i > 0 { - buf.WriteString(" | ") + buf.WriteString(termSep) } buf.WriteString(x.String()) } diff --git a/internal/typeparams/typeterm.go b/internal/typeparams/typeterm.go index 7350bb702a1..fa758cdc989 100644 --- a/internal/typeparams/typeterm.go +++ b/internal/typeparams/typeterm.go @@ -1,3 +1,6 @@ +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. +// Source: ../../cmd/compile/internal/types2/typeterm.go + // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. From 33f80b505bae8678854a2cea554dbbcf57b43f5e Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 3 Apr 2025 15:03:25 -0400 Subject: [PATCH 297/309] typesinternal: remove RequiresFullInfo We're not going to require that functions have a fully populated types.Info. Change-Id: I70e9a56fb71adc2a141bcc3937f4c9de8ca46f29 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662696 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- internal/typesinternal/types.go | 21 -------------- internal/typesinternal/types_test.go | 41 ---------------------------- 2 files changed, 62 deletions(-) delete mode 100644 internal/typesinternal/types_test.go diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index d9ef55ebc77..cc244689ef8 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -7,12 +7,10 @@ package typesinternal import ( - "fmt" "go/ast" "go/token" "go/types" "reflect" - "strings" "unsafe" "golang.org/x/tools/internal/aliases" @@ -144,22 +142,3 @@ func NewTypesInfo() *types.Info { FileVersions: map[*ast.File]string{}, } } - -// RequiresFullInfo panics unless info has non-nil values for all maps. -func RequiresFullInfo(info *types.Info) { - v := reflect.ValueOf(info).Elem() - t := v.Type() - var missing []string - for i := range t.NumField() { - f := t.Field(i) - if f.Type.Kind() == reflect.Map && v.Field(i).IsNil() { - missing = append(missing, f.Name) - } - } - if len(missing) > 0 { - msg := fmt.Sprintf(`A fully populated types.Info value is required. -This one is missing the following fields: -%s`, strings.Join(missing, ", ")) - panic(msg) - } -} diff --git a/internal/typesinternal/types_test.go b/internal/typesinternal/types_test.go deleted file mode 100644 index 2a715549408..00000000000 --- a/internal/typesinternal/types_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2025 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package typesinternal - -import ( - "fmt" - "go/ast" - "go/types" - "regexp" - "testing" -) - -func TestRequiresFullInfo(t *testing.T) { - info := &types.Info{ - Uses: map[*ast.Ident]types.Object{}, - Scopes: map[ast.Node]*types.Scope{}, - } - panics(t, "Types, Instances, Defs, Implicits, Selections, FileVersions", func() { - RequiresFullInfo(info) - }) - - // Shouldn't panic. - RequiresFullInfo(NewTypesInfo()) -} - -// panics asserts that f() panics with with a value whose printed form matches the regexp want. -// Copied from go/analysis/internal/checker/fix_test.go. -func panics(t *testing.T, want string, f func()) { - defer func() { - if x := recover(); x == nil { - t.Errorf("function returned normally, wanted panic") - } else if m, err := regexp.MatchString(want, fmt.Sprint(x)); err != nil { - t.Errorf("panics: invalid regexp %q", want) - } else if !m { - t.Errorf("function panicked with value %q, want match for %q", x, want) - } - }() - f() -} From 17ce4c7b72f9e9fabeda5ed65e0ad9e19da506f7 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 3 Apr 2025 15:10:26 -0400 Subject: [PATCH 298/309] refactor/eg: return error if some info maps are missing NewTransformer requires that some fields of its argument types.Info are populated. This CL checks those requirements, which were formerly implicit. Change-Id: I8ecb7b211e05cd143d04073366927fbd877cf483 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662697 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- refactor/eg/eg.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/refactor/eg/eg.go b/refactor/eg/eg.go index 65a7f690bfd..8de1fd7d1de 100644 --- a/refactor/eg/eg.go +++ b/refactor/eg/eg.go @@ -8,6 +8,7 @@ package eg // import "golang.org/x/tools/refactor/eg" import ( "bytes" + "errors" "fmt" "go/ast" "go/format" @@ -159,6 +160,10 @@ type Transformer struct { // described in the package documentation. // tmplInfo is the type information for tmplFile. func NewTransformer(fset *token.FileSet, tmplPkg *types.Package, tmplFile *ast.File, tmplInfo *types.Info, verbose bool) (*Transformer, error) { + // These maps are required by types.Info.TypeOf. + if tmplInfo.Types == nil || tmplInfo.Defs == nil || tmplInfo.Uses == nil { + return nil, errors.New("eg.NewTransformer: types.Info argument missing one of Types, Defs or Uses") + } // Check the template. beforeSig := funcSig(tmplPkg, "before") if beforeSig == nil { From e29f9ae7c1609b93592398e576786748f91393a9 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 3 Apr 2025 15:41:49 -0400 Subject: [PATCH 299/309] refactor/satisfy: check for presence of types.Info maps Although Finder.Find's doc says that it requires certain maps in its argument types.Info, it didn't actually check. Now it does. Change-Id: I4938f9cbca9377b935d8a027d59438b9697558b7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662698 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- refactor/satisfy/find.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/refactor/satisfy/find.go b/refactor/satisfy/find.go index a897c3c2fd4..766cc575387 100644 --- a/refactor/satisfy/find.go +++ b/refactor/satisfy/find.go @@ -84,6 +84,9 @@ type Finder struct { // info.{Defs,Uses,Selections,Types} must have been populated by the // type-checker. func (f *Finder) Find(info *types.Info, files []*ast.File) { + if info.Defs == nil || info.Uses == nil || info.Selections == nil || info.Types == nil { + panic("Finder.Find: one of info.{Defs,Uses,Selections.Types} is not populated") + } if f.Result == nil { f.Result = make(map[Constraint]bool) } From b437eff8291cf46efe66e499f4c0ac5c8df770d5 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 15 Mar 2025 08:00:39 -0400 Subject: [PATCH 300/309] go/types/typeutil: implement Callee and StaticCallee with Used Also, add a test for typesinternal.Used. It is employed by Callee and StaticCallee, and behaves differently from ClassifyCall in some cases. Change-Id: I21178a2cc8acdc20ebf669bb4741df03851be0b3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658235 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- go/types/typeutil/callee.go | 87 ++++++++++++-------- go/types/typeutil/callee_test.go | 1 + internal/typesinternal/classify_call.go | 44 ++-------- internal/typesinternal/classify_call_test.go | 71 ++++++++-------- 4 files changed, 100 insertions(+), 103 deletions(-) diff --git a/go/types/typeutil/callee.go b/go/types/typeutil/callee.go index 754380351e8..2e3ccaa3dc3 100644 --- a/go/types/typeutil/callee.go +++ b/go/types/typeutil/callee.go @@ -7,45 +7,23 @@ package typeutil import ( "go/ast" "go/types" - - "golang.org/x/tools/internal/typeparams" + _ "unsafe" // for linkname ) // Callee returns the named target of a function call, if any: // a function, method, builtin, or variable. // // Functions and methods may potentially have type parameters. +// +// Note: for calls of instantiated functions and methods, Callee returns +// the corresponding generic function or method on the generic type. func Callee(info *types.Info, call *ast.CallExpr) types.Object { - fun := ast.Unparen(call.Fun) - - // Look through type instantiation if necessary. - isInstance := false - switch fun.(type) { - case *ast.IndexExpr, *ast.IndexListExpr: - // When extracting the callee from an *IndexExpr, we need to check that - // it is a *types.Func and not a *types.Var. - // Example: Don't match a slice m within the expression `m[0]()`. - isInstance = true - fun, _, _, _ = typeparams.UnpackIndexExpr(fun) - } - - var obj types.Object - switch fun := fun.(type) { - case *ast.Ident: - obj = info.Uses[fun] // type, var, builtin, or declared func - case *ast.SelectorExpr: - if sel, ok := info.Selections[fun]; ok { - obj = sel.Obj() // method or field - } else { - obj = info.Uses[fun.Sel] // qualified identifier? - } + obj := used(info, call.Fun) + if obj == nil { + return nil } if _, ok := obj.(*types.TypeName); ok { - return nil // T(x) is a conversion, not a call - } - // A Func is required to match instantiations. - if _, ok := obj.(*types.Func); isInstance && !ok { - return nil // Was not a Func. + return nil } return obj } @@ -56,13 +34,54 @@ func Callee(info *types.Info, call *ast.CallExpr) types.Object { // Note: for calls of instantiated functions and methods, StaticCallee returns // the corresponding generic function or method on the generic type. func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { - if f, ok := Callee(info, call).(*types.Func); ok && !interfaceMethod(f) { - return f + obj := used(info, call.Fun) + fn, _ := obj.(*types.Func) + if fn == nil || interfaceMethod(fn) { + return nil } - return nil + return fn } +// used is the implementation of [internal/typesinternal.Used]. +// It returns the object associated with e. +// See typesinternal.Used for a fuller description. +// This function should live in typesinternal, but cannot because it would +// create an import cycle. +// +//go:linkname used +func used(info *types.Info, e ast.Expr) types.Object { + if info.Types == nil || info.Uses == nil || info.Selections == nil { + panic("one of info.Types, info.Uses or info.Selections is nil; all must be populated") + } + // Look through type instantiation if necessary. + switch d := ast.Unparen(e).(type) { + case *ast.IndexExpr: + if info.Types[d.Index].IsType() { + e = d.X + } + case *ast.IndexListExpr: + e = d.X + } + + var obj types.Object + switch e := ast.Unparen(e).(type) { + case *ast.Ident: + obj = info.Uses[e] // type, var, builtin, or declared func + case *ast.SelectorExpr: + if sel, ok := info.Selections[e]; ok { + obj = sel.Obj() // method or field + } else { + obj = info.Uses[e.Sel] // qualified identifier? + } + } + return obj +} + +// interfaceMethod reports whether its argument is a method of an interface. +// This function should live in typesinternal, but cannot because it would create an import cycle. +// +//go:linkname interfaceMethod func interfaceMethod(f *types.Func) bool { - recv := f.Type().(*types.Signature).Recv() + recv := f.Signature().Recv() return recv != nil && types.IsInterface(recv.Type()) } diff --git a/go/types/typeutil/callee_test.go b/go/types/typeutil/callee_test.go index 1d48bc743a9..3f96533ffff 100644 --- a/go/types/typeutil/callee_test.go +++ b/go/types/typeutil/callee_test.go @@ -122,6 +122,7 @@ func testStaticCallee(t *testing.T, contents []string) { cfg := &types.Config{Importer: closure(packages)} info := &types.Info{ Instances: make(map[*ast.Ident]types.Instance), + Types: make(map[ast.Expr]types.TypeAndValue), Uses: make(map[*ast.Ident]types.Object), Selections: make(map[*ast.SelectorExpr]*types.Selection), FileVersions: make(map[*ast.File]string), diff --git a/internal/typesinternal/classify_call.go b/internal/typesinternal/classify_call.go index 1e79eb2b7ac..9d4da859370 100644 --- a/internal/typesinternal/classify_call.go +++ b/internal/typesinternal/classify_call.go @@ -8,6 +8,7 @@ import ( "fmt" "go/ast" "go/types" + _ "unsafe" ) // CallKind describes the function position of an [*ast.CallExpr]. @@ -74,6 +75,9 @@ func (k CallKind) String() string { // int(x) CallConversion nil // []byte("") CallConversion nil func ClassifyCall(info *types.Info, call *ast.CallExpr) (CallKind, types.Object) { + if info.Types == nil { + panic("ClassifyCall: info.Types is nil") + } if info.Types[call.Fun].IsType() { return CallConversion, nil } @@ -134,40 +138,8 @@ func Used(info *types.Info, e ast.Expr) types.Object { return used(info, e) } -// placeholder: will be moved and documented in the next CL. -func used(info *types.Info, e ast.Expr) types.Object { - e = ast.Unparen(e) - // Look through type instantiation if necessary. - isIndexed := false - switch d := e.(type) { - case *ast.IndexExpr: - if info.Types[d.Index].IsType() { - e = d.X - } - case *ast.IndexListExpr: - e = d.X - } - var obj types.Object - switch e := e.(type) { - case *ast.Ident: - obj = info.Uses[e] // type, var, builtin, or declared func - case *ast.SelectorExpr: - if sel, ok := info.Selections[e]; ok { - obj = sel.Obj() // method or field - } else { - obj = info.Uses[e.Sel] // qualified identifier? - } - } - // If a variable like a slice or map is being indexed, do not - // return an object. - if _, ok := obj.(*types.Var); ok && isIndexed { - return nil - } - return obj -} +//go:linkname used golang.org/x/tools/go/types/typeutil.used +func used(info *types.Info, e ast.Expr) types.Object -// placeholder: will be moved and documented in the next CL. -func interfaceMethod(f *types.Func) bool { - recv := f.Signature().Recv() - return recv != nil && types.IsInterface(recv.Type()) -} +//go:linkname interfaceMethod golang.org/x/tools/go/types/typeutil.interfaceMethod +func interfaceMethod(f *types.Func) bool diff --git a/internal/typesinternal/classify_call_test.go b/internal/typesinternal/classify_call_test.go index 8a6e75a3b0d..6a30ee280df 100644 --- a/internal/typesinternal/classify_call_test.go +++ b/internal/typesinternal/classify_call_test.go @@ -19,9 +19,6 @@ import ( ) func TestClassifyCallAndUsed(t *testing.T) { - // This function directly tests ClassifyCall, but since that - // function's second return value is always the result of Used, - // it effectively tests Used as well. const src = ` package p @@ -78,13 +75,7 @@ func TestClassifyCallAndUsed(t *testing.T) { Error: func(err error) { t.Fatal(err) }, Importer: importer.Default(), } - info := &types.Info{ - Instances: make(map[*ast.Ident]types.Instance), - Uses: make(map[*ast.Ident]types.Object), - Defs: make(map[*ast.Ident]types.Object), - Types: make(map[ast.Expr]types.TypeAndValue), - Selections: make(map[*ast.SelectorExpr]*types.Selection), - } + info := ti.NewTypesInfo() // parse f, err := parser.ParseFile(fset, "classify.go", src, 0) if err != nil { @@ -108,30 +99,36 @@ func TestClassifyCallAndUsed(t *testing.T) { printlnObj := types.Universe.Lookup("println") + typeParam := lookup("tests").Type().(*types.Signature).TypeParams().At(0).Obj() + + // A unique value for marking that Used returns the same object as ClassifyCall. + same := &types.Label{} + // Expected Calls are in the order of CallExprs at the end of src, above. wants := []struct { - kind ti.CallKind - obj types.Object + kind ti.CallKind + classifyObj types.Object // the object returned from ClassifyCall + usedObj types.Object // the object returned from Used, sometimes different }{ - {ti.CallStatic, lookup("g")}, // g - {ti.CallDynamic, nil}, // f - {ti.CallBuiltin, printlnObj}, // println - {ti.CallStatic, member("S", "g")}, // z.g - {ti.CallStatic, member("S", "g")}, // a.b.c.g - {ti.CallStatic, member("S", "g")}, // S.g(z, 1) - {ti.CallDynamic, nil}, // z.f - {ti.CallInterface, member("I", "m")}, // I(nil).m - {ti.CallConversion, nil}, // I(nil) - {ti.CallDynamic, nil}, // m[0] - {ti.CallDynamic, nil}, // n[0] - {ti.CallStatic, lookup("F")}, // F[int] - {ti.CallStatic, lookup("F")}, // F[T] - {ti.CallDynamic, nil}, // f(){} - {ti.CallConversion, nil}, // []byte - {ti.CallConversion, nil}, // A[int] - {ti.CallConversion, nil}, // T - {ti.CallStatic, member("S", "g")}, // (z.g) - {ti.CallStatic, member("S", "g")}, // (z).g + {ti.CallStatic, lookup("g"), same}, // g + {ti.CallDynamic, nil, lookup("f")}, // f + {ti.CallBuiltin, printlnObj, same}, // println + {ti.CallStatic, member("S", "g"), same}, // z.g + {ti.CallStatic, member("S", "g"), same}, // a.b.c.g + {ti.CallStatic, member("S", "g"), same}, // S.g(z, 1) + {ti.CallDynamic, nil, member("z", "f")}, // z.f + {ti.CallInterface, member("I", "m"), same}, // I(nil).m + {ti.CallConversion, nil, lookup("I")}, // I(nil) + {ti.CallDynamic, nil, same}, // m[0] + {ti.CallDynamic, nil, same}, // n[0] + {ti.CallStatic, lookup("F"), same}, // F[int] + {ti.CallStatic, lookup("F"), same}, // F[T] + {ti.CallDynamic, nil, same}, // f(){} + {ti.CallConversion, nil, same}, // []byte + {ti.CallConversion, nil, lookup("A")}, // A[int] + {ti.CallConversion, nil, typeParam}, // T + {ti.CallStatic, member("S", "g"), same}, // (z.g) + {ti.CallStatic, member("S", "g"), same}, // (z).g } i := 0 @@ -152,8 +149,16 @@ func TestClassifyCallAndUsed(t *testing.T) { if gotKind != want.kind { t.Errorf("%s kind: got %s, want %s", prefix, gotKind, want.kind) } - if gotObj != want.obj { - t.Errorf("%s obj: got %v (%[2]T), want %v", prefix, gotObj, want.obj) + if gotObj != want.classifyObj { + t.Errorf("%s obj: got %v (%[2]T), want %v", prefix, gotObj, want.classifyObj) + } + + w := want.usedObj + if w == same { + w = want.classifyObj + } + if g := ti.Used(info, call.Fun); g != w { + t.Errorf("%s used obj: got %v (%[2]T), want %v", prefix, g, w) } i++ } From b948add7e7e4926ce52fb3a01e15c18e4558c252 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 3 Apr 2025 16:51:43 -0400 Subject: [PATCH 301/309] internal/gofix: move from gopls/internal/analysis/gofix Change-Id: I31a899e4705c1c4226f934e015b89e0aa9e576e6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662755 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Jonathan Amsterdam --- gopls/internal/settings/analysis.go | 2 +- .../gofix/cmd/gofix/main.go | 2 +- .../analysis => internal}/gofix/doc.go | 0 .../analysis => internal}/gofix/gofix.go | 38 ++++++++++++++----- .../analysis => internal}/gofix/gofix_test.go | 3 ++ .../gofix/testdata/src/a/a.go | 0 .../gofix/testdata/src/a/a.go.golden | 0 .../gofix/testdata/src/a/internal/d.go | 0 .../gofix/testdata/src/b/b.go | 0 .../gofix/testdata/src/b/b.go.golden | 0 .../gofix/testdata/src/c/c.go | 0 .../gofix/testdata/src/directive/directive.go | 0 .../src/directive/directive.go.golden | 1 - 13 files changed, 33 insertions(+), 13 deletions(-) rename {gopls/internal/analysis => internal}/gofix/cmd/gofix/main.go (89%) rename {gopls/internal/analysis => internal}/gofix/doc.go (100%) rename {gopls/internal/analysis => internal}/gofix/gofix.go (96%) rename {gopls/internal/analysis => internal}/gofix/gofix_test.go (97%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/a/a.go (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/a/a.go.golden (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/a/internal/d.go (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/b/b.go (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/b/b.go.golden (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/c/c.go (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/directive/directive.go (100%) rename {gopls/internal/analysis => internal}/gofix/testdata/src/directive/directive.go.golden (99%) diff --git a/gopls/internal/settings/analysis.go b/gopls/internal/settings/analysis.go index 5ba8bdd06b0..e914407fe6b 100644 --- a/gopls/internal/settings/analysis.go +++ b/gopls/internal/settings/analysis.go @@ -49,7 +49,6 @@ import ( "golang.org/x/tools/gopls/internal/analysis/deprecated" "golang.org/x/tools/gopls/internal/analysis/embeddirective" "golang.org/x/tools/gopls/internal/analysis/fillreturns" - "golang.org/x/tools/gopls/internal/analysis/gofix" "golang.org/x/tools/gopls/internal/analysis/hostport" "golang.org/x/tools/gopls/internal/analysis/infertypeargs" "golang.org/x/tools/gopls/internal/analysis/modernize" @@ -63,6 +62,7 @@ import ( "golang.org/x/tools/gopls/internal/analysis/unusedvariable" "golang.org/x/tools/gopls/internal/analysis/yield" "golang.org/x/tools/gopls/internal/protocol" + "golang.org/x/tools/internal/gofix" ) // Analyzer augments a [analysis.Analyzer] with additional LSP configuration. diff --git a/gopls/internal/analysis/gofix/cmd/gofix/main.go b/internal/gofix/cmd/gofix/main.go similarity index 89% rename from gopls/internal/analysis/gofix/cmd/gofix/main.go rename to internal/gofix/cmd/gofix/main.go index d75978f6e59..9ec77943774 100644 --- a/gopls/internal/analysis/gofix/cmd/gofix/main.go +++ b/internal/gofix/cmd/gofix/main.go @@ -10,7 +10,7 @@ package main import ( "golang.org/x/tools/go/analysis/singlechecker" - "golang.org/x/tools/gopls/internal/analysis/gofix" + "golang.org/x/tools/internal/gofix" ) func main() { singlechecker.Main(gofix.Analyzer) } diff --git a/gopls/internal/analysis/gofix/doc.go b/internal/gofix/doc.go similarity index 100% rename from gopls/internal/analysis/gofix/doc.go rename to internal/gofix/doc.go diff --git a/gopls/internal/analysis/gofix/gofix.go b/internal/gofix/gofix.go similarity index 96% rename from gopls/internal/analysis/gofix/gofix.go rename to internal/gofix/gofix.go index 6f4c8a6e2fd..b2fc5318e09 100644 --- a/gopls/internal/analysis/gofix/gofix.go +++ b/internal/gofix/gofix.go @@ -9,6 +9,7 @@ import ( "go/ast" "go/token" "go/types" + "iter" "slices" "strings" @@ -18,7 +19,6 @@ import ( "golang.org/x/tools/go/analysis/passes/inspect" "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" - "golang.org/x/tools/gopls/internal/util/moreiters" "golang.org/x/tools/internal/analysisinternal" internalastutil "golang.org/x/tools/internal/astutil" "golang.org/x/tools/internal/astutil/cursor" @@ -330,7 +330,7 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId cursor.Cursor) { // Remember the names of the alias's type params. When we check for shadowing // later, we'll ignore these because they won't appear in the replacement text. typeParamNames := map[*types.TypeName]bool{} - for tp := range alias.TypeParams().TypeParams() { + for tp := range listIter(alias.TypeParams()) { typeParamNames[tp.Obj()] = true } rhs := alias.Rhs() @@ -405,7 +405,7 @@ func (a *analyzer) inlineAlias(tn *types.TypeName, curId cursor.Cursor) { // A[int, Foo] as M[int, Foo]. // Don't validate instantiation: it can't panic unless we have a bug, // in which case seeing the stack trace via telemetry would be helpful. - instAlias, _ := types.Instantiate(nil, alias, slices.Collect(targs.Types()), false) + instAlias, _ := types.Instantiate(nil, alias, slices.Collect(listIter(targs)), false) rhs = instAlias.(*types.Alias).Rhs() } // To get the replacement text, render the alias RHS using the package prefixes @@ -437,11 +437,11 @@ func typenames(t types.Type) []*types.TypeName { case *types.Basic: tns = append(tns, types.Universe.Lookup(t.Name()).(*types.TypeName)) case *types.Named: - for t := range t.TypeArgs().Types() { + for t := range listIter(t.TypeArgs()) { visit(t) } case *types.Alias: - for t := range t.TypeArgs().Types() { + for t := range listIter(t.TypeArgs()) { visit(t) } case *types.TypeParam: @@ -458,8 +458,8 @@ func typenames(t types.Type) []*types.TypeName { visit(t.Key()) visit(t.Elem()) case *types.Struct: - for f := range t.Fields() { - visit(f.Type()) + for i := range t.NumFields() { + visit(t.Field(i).Type()) } case *types.Signature: // Ignore the receiver: although it may be present, it has no meaning @@ -479,7 +479,7 @@ func typenames(t types.Type) []*types.TypeName { visit(t.ExplicitMethod(i).Type()) } case *types.Tuple: - for v := range t.Variables() { + for v := range listIter(t) { visit(v.Type()) } case *types.Union: @@ -592,8 +592,10 @@ func (a *analyzer) readFile(node ast.Node) ([]byte, error) { // currentFile returns the unique ast.File for a cursor. func currentFile(c cursor.Cursor) *ast.File { - cf, _ := moreiters.First(c.Enclosing((*ast.File)(nil))) - return cf.Node().(*ast.File) + for cf := range c.Enclosing((*ast.File)(nil)) { + return cf.Node().(*ast.File) + } + panic("no *ast.File enclosing a cursor: impossible") } // hasFixInline reports the presence of a "//go:fix inline" directive @@ -640,3 +642,19 @@ func (*goFixInlineAliasFact) AFact() {} func discard(string, ...any) {} var builtinIota = types.Universe.Lookup("iota") + +type list[T any] interface { + Len() int + At(int) T +} + +// TODO(adonovan): eliminate in favor of go/types@go1.24 iterators. +func listIter[L list[T], T any](lst L) iter.Seq[T] { + return func(yield func(T) bool) { + for i := range lst.Len() { + if !yield(lst.At(i)) { + return + } + } + } +} diff --git a/gopls/internal/analysis/gofix/gofix_test.go b/internal/gofix/gofix_test.go similarity index 97% rename from gopls/internal/analysis/gofix/gofix_test.go rename to internal/gofix/gofix_test.go index 4acc4daf2ff..ae2df3860a8 100644 --- a/gopls/internal/analysis/gofix/gofix_test.go +++ b/internal/gofix/gofix_test.go @@ -19,6 +19,9 @@ import ( ) func TestAnalyzer(t *testing.T) { + if testenv.Go1Point() < 24 { + testenv.NeedsGoExperiment(t, "aliastypeparams") + } analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "a", "b") } diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go b/internal/gofix/testdata/src/a/a.go similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/a/a.go rename to internal/gofix/testdata/src/a/a.go diff --git a/gopls/internal/analysis/gofix/testdata/src/a/a.go.golden b/internal/gofix/testdata/src/a/a.go.golden similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/a/a.go.golden rename to internal/gofix/testdata/src/a/a.go.golden diff --git a/gopls/internal/analysis/gofix/testdata/src/a/internal/d.go b/internal/gofix/testdata/src/a/internal/d.go similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/a/internal/d.go rename to internal/gofix/testdata/src/a/internal/d.go diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go b/internal/gofix/testdata/src/b/b.go similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/b/b.go rename to internal/gofix/testdata/src/b/b.go diff --git a/gopls/internal/analysis/gofix/testdata/src/b/b.go.golden b/internal/gofix/testdata/src/b/b.go.golden similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/b/b.go.golden rename to internal/gofix/testdata/src/b/b.go.golden diff --git a/gopls/internal/analysis/gofix/testdata/src/c/c.go b/internal/gofix/testdata/src/c/c.go similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/c/c.go rename to internal/gofix/testdata/src/c/c.go diff --git a/gopls/internal/analysis/gofix/testdata/src/directive/directive.go b/internal/gofix/testdata/src/directive/directive.go similarity index 100% rename from gopls/internal/analysis/gofix/testdata/src/directive/directive.go rename to internal/gofix/testdata/src/directive/directive.go diff --git a/gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden b/internal/gofix/testdata/src/directive/directive.go.golden similarity index 99% rename from gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden rename to internal/gofix/testdata/src/directive/directive.go.golden index 3e5b3409288..a6625e1731f 100644 --- a/gopls/internal/analysis/gofix/testdata/src/directive/directive.go.golden +++ b/internal/gofix/testdata/src/directive/directive.go.golden @@ -68,4 +68,3 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array // //go:fix inline type EL = map[[2]string][]*T // want EL: `goFixInline alias` - From e850fe1872cee508a221a3efd67dd2901deddc4c Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Fri, 4 Apr 2025 22:42:46 -0600 Subject: [PATCH 302/309] gopls/internal/golang: CodeAction: place gopls doc as the last action This CL adjusts the position of 'gopls.doc.features' at the end of the code action producers to offer it as the last action. It also checks the order of each action in unit test rather than checking existence. Fixes golang/go#72742 Change-Id: I062792a5608fc3d0b4c04334389a4d7066873c62 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662915 Auto-Submit: Alan Donovan Reviewed-by: Alan Donovan Reviewed-by: Carlos Amedee LUCI-TryBot-Result: Go LUCI --- gopls/internal/cmd/integration_test.go | 4 +- gopls/internal/golang/codeaction.go | 10 ++--- gopls/internal/settings/codeactionkind.go | 1 + .../test/integration/misc/codeactions_test.go | 42 +++++++++++++------ 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/gopls/internal/cmd/integration_test.go b/gopls/internal/cmd/integration_test.go index 9d135ceadb2..6e4b450635b 100644 --- a/gopls/internal/cmd/integration_test.go +++ b/gopls/internal/cmd/integration_test.go @@ -1010,9 +1010,9 @@ type C struct{} res := gopls(t, tree, "codeaction", "-title=Browse.*doc", "a/a.go") res.checkExit(true) got := res.stdout - want := `command "Browse gopls feature documentation" [gopls.doc.features]` + + want := `command "Browse documentation for package a" [source.doc]` + "\n" + - `command "Browse documentation for package a" [source.doc]` + + `command "Browse gopls feature documentation" [gopls.doc.features]` + "\n" if got != want { t.Errorf("codeaction: got <<%s>>, want <<%s>>\nstderr:\n%s", got, want, res.stderr) diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index d9f2af47d24..7949493a896 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -14,7 +14,6 @@ import ( "path/filepath" "reflect" "slices" - "sort" "strings" "golang.org/x/tools/go/ast/astutil" @@ -112,10 +111,7 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, } } - sort.Slice(actions, func(i, j int) bool { - return actions[i].Kind < actions[j].Kind - }) - + // Return code actions in the order their providers are listed. return actions, nil } @@ -233,6 +229,8 @@ type codeActionProducer struct { needPkg bool // fn needs type information (req.pkg) } +// Code Actions are returned in the order their producers are listed below. +// Depending on the client, this may influence the order they appear in the UI. var codeActionProducers = [...]codeActionProducer{ {kind: protocol.QuickFix, fn: quickFix, needPkg: true}, {kind: protocol.SourceOrganizeImports, fn: sourceOrganizeImports}, @@ -242,7 +240,6 @@ var codeActionProducers = [...]codeActionProducer{ {kind: settings.GoFreeSymbols, fn: goFreeSymbols}, {kind: settings.GoTest, fn: goTest, needPkg: true}, {kind: settings.GoToggleCompilerOptDetails, fn: toggleCompilerOptDetails}, - {kind: settings.GoplsDocFeatures, fn: goplsDocFeatures}, {kind: settings.RefactorExtractFunction, fn: refactorExtractFunction}, {kind: settings.RefactorExtractMethod, fn: refactorExtractMethod}, {kind: settings.RefactorExtractToNewFile, fn: refactorExtractToNewFile}, @@ -261,6 +258,7 @@ var codeActionProducers = [...]codeActionProducer{ {kind: settings.RefactorRewriteMoveParamRight, fn: refactorRewriteMoveParamRight, needPkg: true}, {kind: settings.RefactorRewriteSplitLines, fn: refactorRewriteSplitLines, needPkg: true}, {kind: settings.RefactorRewriteEliminateDotImport, fn: refactorRewriteEliminateDotImport, needPkg: true}, + {kind: settings.GoplsDocFeatures, fn: goplsDocFeatures}, // offer this one last (#72742) // Note: don't forget to update the allow-list in Server.CodeAction // when adding new query operations like GoTest and GoDoc that diff --git a/gopls/internal/settings/codeactionkind.go b/gopls/internal/settings/codeactionkind.go index 09d9d419567..f6f8a4df2a4 100644 --- a/gopls/internal/settings/codeactionkind.go +++ b/gopls/internal/settings/codeactionkind.go @@ -81,6 +81,7 @@ const ( GoTest protocol.CodeActionKind = "source.test" GoToggleCompilerOptDetails protocol.CodeActionKind = "source.toggleCompilerOptDetails" AddTest protocol.CodeActionKind = "source.addTest" + OrganizeImports protocol.CodeActionKind = "source.organizeImports" // gopls GoplsDocFeatures protocol.CodeActionKind = "gopls.doc.features" diff --git a/gopls/internal/test/integration/misc/codeactions_test.go b/gopls/internal/test/integration/misc/codeactions_test.go index c62a3898e9b..d9c83186d69 100644 --- a/gopls/internal/test/integration/misc/codeactions_test.go +++ b/gopls/internal/test/integration/misc/codeactions_test.go @@ -35,25 +35,28 @@ package a func f() { g() } func g() {} + +-- issue72742/a.go -- +package main + +func main(){ + fmt.Println("helloworld") +} ` Run(t, src, func(t *testing.T, env *Env) { - check := func(filename string, wantKind ...protocol.CodeActionKind) { + check := func(filename string, re string, want []protocol.CodeActionKind) { env.OpenFile(filename) - loc := env.RegexpSearch(filename, `g\(\)`) + loc := env.RegexpSearch(filename, re) actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) if err != nil { t.Fatal(err) } - type kinds = map[protocol.CodeActionKind]bool - got := make(kinds) + type kinds = []protocol.CodeActionKind + got := make(kinds, 0) for _, act := range actions { - got[act.Kind] = true - } - want := make(kinds) - for _, kind := range wantKind { - want[kind] = true + got = append(got, act.Kind) } if diff := cmp.Diff(want, got); diff != "" { @@ -63,20 +66,33 @@ func g() {} } } - check("src/a.go", + check("src/a.go", `g\(\)`, []protocol.CodeActionKind{ settings.AddTest, settings.GoAssembly, settings.GoDoc, settings.GoFreeSymbols, settings.GoToggleCompilerOptDetails, + settings.RefactorInlineCall, settings.GoplsDocFeatures, - settings.RefactorInlineCall) - check("gen/a.go", + }) + + check("gen/a.go", `g\(\)`, []protocol.CodeActionKind{ settings.GoAssembly, settings.GoDoc, settings.GoFreeSymbols, settings.GoToggleCompilerOptDetails, - settings.GoplsDocFeatures) + settings.GoplsDocFeatures, + }) + + check("issue72742/a.go", `fmt`, []protocol.CodeActionKind{ + settings.OrganizeImports, + settings.AddTest, + settings.GoAssembly, + settings.GoDoc, + settings.GoFreeSymbols, + settings.GoToggleCompilerOptDetails, + settings.GoplsDocFeatures, + }) }) } From b97074b9c8ebe7cce7db7be133120bc966f9c33f Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 4 Apr 2025 12:11:52 -0400 Subject: [PATCH 303/309] internal/gofix: fix URLs Change-Id: Id09d42501c242ca702f251e6c06ca5047f7b4ac7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662956 LUCI-TryBot-Result: Go LUCI Auto-Submit: Jonathan Amsterdam Reviewed-by: Alan Donovan --- gopls/doc/analyzers.md | 2 +- gopls/internal/doc/api.json | 2 +- internal/gofix/gofix.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gopls/doc/analyzers.md b/gopls/doc/analyzers.md index 0d9fcb2313b..4b2bff1a63a 100644 --- a/gopls/doc/analyzers.md +++ b/gopls/doc/analyzers.md @@ -298,7 +298,7 @@ The gofix analyzer inlines functions and constants that are marked for inlining. Default: on. -Package documentation: [gofix](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix) +Package documentation: [gofix](https://pkg.go.dev/golang.org/x/tools/internal/gofix) ## `hostport`: check format of addresses passed to net.Dial diff --git a/gopls/internal/doc/api.json b/gopls/internal/doc/api.json index f624af8632c..0852870ba41 100644 --- a/gopls/internal/doc/api.json +++ b/gopls/internal/doc/api.json @@ -1297,7 +1297,7 @@ { "Name": "gofix", "Doc": "apply fixes based on go:fix comment directives\n\nThe gofix analyzer inlines functions and constants that are marked for inlining.", - "URL": "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + "URL": "https://pkg.go.dev/golang.org/x/tools/internal/gofix", "Default": true }, { diff --git a/internal/gofix/gofix.go b/internal/gofix/gofix.go index b2fc5318e09..565272b5e46 100644 --- a/internal/gofix/gofix.go +++ b/internal/gofix/gofix.go @@ -34,7 +34,7 @@ var doc string var Analyzer = &analysis.Analyzer{ Name: "gofix", Doc: analysisinternal.MustExtractDoc(doc, "gofix"), - URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + URL: "https://pkg.go.dev/golang.org/x/tools/internal/gofix", Run: func(pass *analysis.Pass) (any, error) { return run(pass, true) }, FactTypes: []analysis.Fact{ (*goFixInlineFuncFact)(nil), @@ -47,7 +47,7 @@ var Analyzer = &analysis.Analyzer{ var DirectiveAnalyzer = &analysis.Analyzer{ Name: "gofixdirective", Doc: analysisinternal.MustExtractDoc(doc, "gofixdirective"), - URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/gofix", + URL: "https://pkg.go.dev/golang.org/x/tools/internal/gofix", Run: func(pass *analysis.Pass) (any, error) { return run(pass, false) }, FactTypes: []analysis.Fact{ (*goFixInlineFuncFact)(nil), From 3e7f74d009150bf5e66483f3759d8c59f50e873d Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 4 Apr 2025 14:32:05 -0400 Subject: [PATCH 304/309] go/types/typeutil: used doesn't need Info.Selections Whenever info.Selections[e] is set, info.Uses[e.Sel] is set to the same object. So the used function doesn't need info.Selections. Change-Id: I43782ef728179e61084b0d7cdab4f01a8eea9f72 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662957 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan Auto-Submit: Jonathan Amsterdam --- go/types/typeutil/callee.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/go/types/typeutil/callee.go b/go/types/typeutil/callee.go index 2e3ccaa3dc3..eeeb570a73c 100644 --- a/go/types/typeutil/callee.go +++ b/go/types/typeutil/callee.go @@ -50,8 +50,8 @@ func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { // //go:linkname used func used(info *types.Info, e ast.Expr) types.Object { - if info.Types == nil || info.Uses == nil || info.Selections == nil { - panic("one of info.Types, info.Uses or info.Selections is nil; all must be populated") + if info.Types == nil || info.Uses == nil { + panic("one of info.Types or info.Uses is nil; both must be populated") } // Look through type instantiation if necessary. switch d := ast.Unparen(e).(type) { @@ -65,14 +65,13 @@ func used(info *types.Info, e ast.Expr) types.Object { var obj types.Object switch e := ast.Unparen(e).(type) { + // info.Uses always has the object we want, even for selector expressions. + // We don't need info.Selections. + // See go/types/recording.go:recordSelection. case *ast.Ident: obj = info.Uses[e] // type, var, builtin, or declared func case *ast.SelectorExpr: - if sel, ok := info.Selections[e]; ok { - obj = sel.Obj() // method or field - } else { - obj = info.Uses[e.Sel] // qualified identifier? - } + obj = info.Uses[e.Sel] // selector e.f or T.f or qualified identifier pkg.X } return obj } From 11a9b3f89dc9e5a2e6d738d243258495a9c53005 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Sat, 5 Apr 2025 11:31:59 -0400 Subject: [PATCH 305/309] gopls/internal/server: fix event labels after the big rename The old names predate the big directory renaming we did about a year ago. For the record: why can't these event.Start calls be emitted by the generated server handler code? Two reasons: 1) they sometimes have arguments specific to the call; and 2) they cover only the handler proper, but not the time to send the RPC reply message. Had they been automatically generated in the caller, some care would be required to exclude the RPC reply time. Change-Id: I290537579200aab162ac06522a482b4f4e4f7a28 Reviewed-on: https://go-review.googlesource.com/c/tools/+/663135 LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam Auto-Submit: Alan Donovan --- gopls/internal/server/call_hierarchy.go | 6 +++--- gopls/internal/server/code_action.go | 4 ++-- gopls/internal/server/code_lens.go | 2 +- gopls/internal/server/command.go | 4 ++-- gopls/internal/server/completion.go | 2 +- gopls/internal/server/definition.go | 4 ++-- gopls/internal/server/diagnostics.go | 8 ++++---- gopls/internal/server/folding_range.go | 2 +- gopls/internal/server/format.go | 2 +- gopls/internal/server/general.go | 8 ++++---- gopls/internal/server/highlight.go | 2 +- gopls/internal/server/hover.go | 2 +- gopls/internal/server/implementation.go | 2 +- gopls/internal/server/inlay_hint.go | 2 +- gopls/internal/server/link.go | 2 +- gopls/internal/server/references.go | 2 +- gopls/internal/server/rename.go | 4 ++-- gopls/internal/server/selection_range.go | 2 +- gopls/internal/server/semantic.go | 2 +- gopls/internal/server/server.go | 2 +- gopls/internal/server/signature_help.go | 2 +- gopls/internal/server/symbols.go | 2 +- gopls/internal/server/text_synchronization.go | 10 +++++----- gopls/internal/server/workspace.go | 4 ++-- gopls/internal/server/workspace_symbol.go | 2 +- 25 files changed, 42 insertions(+), 42 deletions(-) diff --git a/gopls/internal/server/call_hierarchy.go b/gopls/internal/server/call_hierarchy.go index 671d4f8c81c..758a4628948 100644 --- a/gopls/internal/server/call_hierarchy.go +++ b/gopls/internal/server/call_hierarchy.go @@ -14,7 +14,7 @@ import ( ) func (s *server) PrepareCallHierarchy(ctx context.Context, params *protocol.CallHierarchyPrepareParams) ([]protocol.CallHierarchyItem, error) { - ctx, done := event.Start(ctx, "lsp.Server.prepareCallHierarchy") + ctx, done := event.Start(ctx, "server.PrepareCallHierarchy") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) @@ -29,7 +29,7 @@ func (s *server) PrepareCallHierarchy(ctx context.Context, params *protocol.Call } func (s *server) IncomingCalls(ctx context.Context, params *protocol.CallHierarchyIncomingCallsParams) ([]protocol.CallHierarchyIncomingCall, error) { - ctx, done := event.Start(ctx, "lsp.Server.incomingCalls") + ctx, done := event.Start(ctx, "server.IncomingCalls") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.Item.URI) @@ -44,7 +44,7 @@ func (s *server) IncomingCalls(ctx context.Context, params *protocol.CallHierarc } func (s *server) OutgoingCalls(ctx context.Context, params *protocol.CallHierarchyOutgoingCallsParams) ([]protocol.CallHierarchyOutgoingCall, error) { - ctx, done := event.Start(ctx, "lsp.Server.outgoingCalls") + ctx, done := event.Start(ctx, "server.OutgoingCalls") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.Item.URI) diff --git a/gopls/internal/server/code_action.go b/gopls/internal/server/code_action.go index c36e7c33f94..4617fad5de7 100644 --- a/gopls/internal/server/code_action.go +++ b/gopls/internal/server/code_action.go @@ -22,7 +22,7 @@ import ( ) func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) { - ctx, done := event.Start(ctx, "lsp.Server.codeAction") + ctx, done := event.Start(ctx, "server.CodeAction") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) @@ -225,7 +225,7 @@ func triggerKind(params *protocol.CodeActionParams) protocol.CodeActionTriggerKi // This feature allows capable clients to preview and selectively apply the diff // instead of applying the whole thing unconditionally through workspace/applyEdit. func (s *server) ResolveCodeAction(ctx context.Context, ca *protocol.CodeAction) (*protocol.CodeAction, error) { - ctx, done := event.Start(ctx, "lsp.Server.resolveCodeAction") + ctx, done := event.Start(ctx, "server.ResolveCodeAction") defer done() // Only resolve the code action if there is Data provided. diff --git a/gopls/internal/server/code_lens.go b/gopls/internal/server/code_lens.go index 67b359e866c..2509452f0b5 100644 --- a/gopls/internal/server/code_lens.go +++ b/gopls/internal/server/code_lens.go @@ -22,7 +22,7 @@ import ( // CodeLens reports the set of available CodeLenses // (range-associated commands) in the given file. func (s *server) CodeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) { - ctx, done := event.Start(ctx, "lsp.Server.codeLens", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.CodeLens", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 0142de532c3..ca8177530e5 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -47,7 +47,7 @@ import ( ) func (s *server) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (any, error) { - ctx, done := event.Start(ctx, "lsp.Server.executeCommand") + ctx, done := event.Start(ctx, "server.ExecuteCommand") defer done() // For test synchronization, always create a progress notification. @@ -1652,7 +1652,7 @@ func (c *commandHandler) DiagnoseFiles(ctx context.Context, args command.Diagnos // Though note that implementing pull diagnostics may cause some servers to // request diagnostics in an ad-hoc manner, and break our intentional pacing. - ctx, done := event.Start(ctx, "lsp.server.DiagnoseFiles") + ctx, done := event.Start(ctx, "commandHandler.DiagnoseFiles") defer done() snapshots := make(map[*cache.Snapshot]bool) diff --git a/gopls/internal/server/completion.go b/gopls/internal/server/completion.go index e72d156de05..02604b2f710 100644 --- a/gopls/internal/server/completion.go +++ b/gopls/internal/server/completion.go @@ -27,7 +27,7 @@ func (s *server) Completion(ctx context.Context, params *protocol.CompletionPara recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.completion", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Completion", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/definition.go b/gopls/internal/server/definition.go index 5a9c020cfc5..8b9d42413be 100644 --- a/gopls/internal/server/definition.go +++ b/gopls/internal/server/definition.go @@ -24,7 +24,7 @@ func (s *server) Definition(ctx context.Context, params *protocol.DefinitionPara recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.definition", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Definition", label.URI.Of(params.TextDocument.URI)) defer done() // TODO(rfindley): definition requests should be multiplexed across all views. @@ -46,7 +46,7 @@ func (s *server) Definition(ctx context.Context, params *protocol.DefinitionPara } func (s *server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) { - ctx, done := event.Start(ctx, "lsp.Server.typeDefinition", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.TypeDefinition", label.URI.Of(params.TextDocument.URI)) defer done() // TODO(rfindley): type definition requests should be multiplexed across all views. diff --git a/gopls/internal/server/diagnostics.go b/gopls/internal/server/diagnostics.go index b4e764b1233..92ca54e226a 100644 --- a/gopls/internal/server/diagnostics.go +++ b/gopls/internal/server/diagnostics.go @@ -200,7 +200,7 @@ func (s *server) diagnoseChangedViews(ctx context.Context, modID uint64, lastCha // snapshot (or a subsequent snapshot in the same View) is eventually // diagnosed. func (s *server) diagnoseSnapshot(ctx context.Context, snapshot *cache.Snapshot, changedURIs []protocol.DocumentURI, delay time.Duration) { - ctx, done := event.Start(ctx, "Server.diagnoseSnapshot", snapshot.Labels()...) + ctx, done := event.Start(ctx, "server.diagnoseSnapshot", snapshot.Labels()...) defer done() if delay > 0 { @@ -241,7 +241,7 @@ func (s *server) diagnoseSnapshot(ctx context.Context, snapshot *cache.Snapshot, } func (s *server) diagnoseChangedFiles(ctx context.Context, snapshot *cache.Snapshot, uris []protocol.DocumentURI) (diagMap, error) { - ctx, done := event.Start(ctx, "Server.diagnoseChangedFiles", snapshot.Labels()...) + ctx, done := event.Start(ctx, "server.diagnoseChangedFiles", snapshot.Labels()...) defer done() toDiagnose := make(map[metadata.PackageID]*metadata.Package) @@ -311,7 +311,7 @@ func (s *server) diagnoseChangedFiles(ctx context.Context, snapshot *cache.Snaps } func (s *server) diagnose(ctx context.Context, snapshot *cache.Snapshot) (diagMap, error) { - ctx, done := event.Start(ctx, "Server.diagnose", snapshot.Labels()...) + ctx, done := event.Start(ctx, "server.diagnose", snapshot.Labels()...) defer done() // Wait for a free diagnostics slot. @@ -640,7 +640,7 @@ func (s *server) updateCriticalErrorStatus(ctx context.Context, snapshot *cache. // updateDiagnostics records the result of diagnosing a snapshot, and publishes // any diagnostics that need to be updated on the client. func (s *server) updateDiagnostics(ctx context.Context, snapshot *cache.Snapshot, diagnostics diagMap, final bool) { - ctx, done := event.Start(ctx, "Server.publishDiagnostics") + ctx, done := event.Start(ctx, "server.publishDiagnostics") defer done() s.diagnosticsMu.Lock() diff --git a/gopls/internal/server/folding_range.go b/gopls/internal/server/folding_range.go index b05d5302f10..5dbfd697db4 100644 --- a/gopls/internal/server/folding_range.go +++ b/gopls/internal/server/folding_range.go @@ -15,7 +15,7 @@ import ( ) func (s *server) FoldingRange(ctx context.Context, params *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) { - ctx, done := event.Start(ctx, "lsp.Server.foldingRange", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.FoldingRange", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/format.go b/gopls/internal/server/format.go index 1e6344dcff4..6abbb96d5b6 100644 --- a/gopls/internal/server/format.go +++ b/gopls/internal/server/format.go @@ -17,7 +17,7 @@ import ( ) func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) { - ctx, done := event.Start(ctx, "lsp.Server.formatting", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Formatting", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go index 7368206f578..5e02b832747 100644 --- a/gopls/internal/server/general.go +++ b/gopls/internal/server/general.go @@ -38,7 +38,7 @@ import ( ) func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) { - ctx, done := event.Start(ctx, "lsp.Server.initialize") + ctx, done := event.Start(ctx, "server.Initialize") defer done() var clientName string @@ -208,7 +208,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ } func (s *server) Initialized(ctx context.Context, params *protocol.InitializedParams) error { - ctx, done := event.Start(ctx, "lsp.Server.initialized") + ctx, done := event.Start(ctx, "server.Initialized") defer done() s.stateMu.Lock() @@ -635,7 +635,7 @@ func (s *server) fileOf(ctx context.Context, uri protocol.DocumentURI) (file.Han // Shutdown implements the 'shutdown' LSP handler. It releases resources // associated with the server and waits for all ongoing work to complete. func (s *server) Shutdown(ctx context.Context) error { - ctx, done := event.Start(ctx, "lsp.Server.shutdown") + ctx, done := event.Start(ctx, "server.Shutdown") defer done() s.stateMu.Lock() @@ -662,7 +662,7 @@ func (s *server) Shutdown(ctx context.Context) error { } func (s *server) Exit(ctx context.Context) error { - ctx, done := event.Start(ctx, "lsp.Server.exit") + ctx, done := event.Start(ctx, "server.Exit") defer done() s.stateMu.Lock() diff --git a/gopls/internal/server/highlight.go b/gopls/internal/server/highlight.go index 35ffc2db2f5..04ebbfa25ec 100644 --- a/gopls/internal/server/highlight.go +++ b/gopls/internal/server/highlight.go @@ -16,7 +16,7 @@ import ( ) func (s *server) DocumentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) { - ctx, done := event.Start(ctx, "lsp.Server.documentHighlight", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DocumentHighlight", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/hover.go b/gopls/internal/server/hover.go index 80c35c09565..ed70ce493ba 100644 --- a/gopls/internal/server/hover.go +++ b/gopls/internal/server/hover.go @@ -25,7 +25,7 @@ func (s *server) Hover(ctx context.Context, params *protocol.HoverParams) (_ *pr recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.hover", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Hover", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/implementation.go b/gopls/internal/server/implementation.go index 9e61ebc4d88..9b2c103b2c3 100644 --- a/gopls/internal/server/implementation.go +++ b/gopls/internal/server/implementation.go @@ -21,7 +21,7 @@ func (s *server) Implementation(ctx context.Context, params *protocol.Implementa recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.implementation", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Implementation", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/inlay_hint.go b/gopls/internal/server/inlay_hint.go index fca8bcbc1c8..a11ab4c313a 100644 --- a/gopls/internal/server/inlay_hint.go +++ b/gopls/internal/server/inlay_hint.go @@ -16,7 +16,7 @@ import ( ) func (s *server) InlayHint(ctx context.Context, params *protocol.InlayHintParams) ([]protocol.InlayHint, error) { - ctx, done := event.Start(ctx, "lsp.Server.inlayHint", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.InlayHint", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/link.go b/gopls/internal/server/link.go index 851ec036d4d..cf475ca90c9 100644 --- a/gopls/internal/server/link.go +++ b/gopls/internal/server/link.go @@ -29,7 +29,7 @@ import ( ) func (s *server) DocumentLink(ctx context.Context, params *protocol.DocumentLinkParams) (links []protocol.DocumentLink, err error) { - ctx, done := event.Start(ctx, "lsp.Server.documentLink") + ctx, done := event.Start(ctx, "server.DocumentLink") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/references.go b/gopls/internal/server/references.go index f5019693946..8a01e96498b 100644 --- a/gopls/internal/server/references.go +++ b/gopls/internal/server/references.go @@ -22,7 +22,7 @@ func (s *server) References(ctx context.Context, params *protocol.ReferenceParam recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.references", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.References", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/rename.go b/gopls/internal/server/rename.go index b6fac8ba219..218740bd679 100644 --- a/gopls/internal/server/rename.go +++ b/gopls/internal/server/rename.go @@ -17,7 +17,7 @@ import ( ) func (s *server) Rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) { - ctx, done := event.Start(ctx, "lsp.Server.rename", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.Rename", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) @@ -68,7 +68,7 @@ func (s *server) Rename(ctx context.Context, params *protocol.RenameParams) (*pr // TODO(rfindley): why wouldn't we want to show an error to the user, if the // user initiated a rename request at the cursor? func (s *server) PrepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.PrepareRenamePlaceholder, error) { - ctx, done := event.Start(ctx, "lsp.Server.prepareRename", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.PrepareRename", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/selection_range.go b/gopls/internal/server/selection_range.go index 484e1cf67ab..afc878b1544 100644 --- a/gopls/internal/server/selection_range.go +++ b/gopls/internal/server/selection_range.go @@ -27,7 +27,7 @@ import ( // returned for each cursor to avoid multiple round-trips when the user is // likely to issue this command multiple times in quick succession. func (s *server) SelectionRange(ctx context.Context, params *protocol.SelectionRangeParams) ([]protocol.SelectionRange, error) { - ctx, done := event.Start(ctx, "lsp.Server.selectionRange") + ctx, done := event.Start(ctx, "server.SelectionRange") defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/semantic.go b/gopls/internal/server/semantic.go index f746593a3dd..f0a2e11dd98 100644 --- a/gopls/internal/server/semantic.go +++ b/gopls/internal/server/semantic.go @@ -24,7 +24,7 @@ func (s *server) SemanticTokensRange(ctx context.Context, params *protocol.Seman } func (s *server) semanticTokens(ctx context.Context, td protocol.TextDocumentIdentifier, rng *protocol.Range) (*protocol.SemanticTokens, error) { - ctx, done := event.Start(ctx, "lsp.Server.semanticTokens", label.URI.Of(td.URI)) + ctx, done := event.Start(ctx, "server.semanticTokens", label.URI.Of(td.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, td.URI) diff --git a/gopls/internal/server/server.go b/gopls/internal/server/server.go index 033295ffb32..c22e8f19750 100644 --- a/gopls/internal/server/server.go +++ b/gopls/internal/server/server.go @@ -181,7 +181,7 @@ type server struct { } func (s *server) WorkDoneProgressCancel(ctx context.Context, params *protocol.WorkDoneProgressCancelParams) error { - ctx, done := event.Start(ctx, "lsp.Server.workDoneProgressCancel") + ctx, done := event.Start(ctx, "server.WorkDoneProgressCancel") defer done() return s.progress.Cancel(params.Token) diff --git a/gopls/internal/server/signature_help.go b/gopls/internal/server/signature_help.go index addcfe1e262..eb464c48e27 100644 --- a/gopls/internal/server/signature_help.go +++ b/gopls/internal/server/signature_help.go @@ -15,7 +15,7 @@ import ( ) func (s *server) SignatureHelp(ctx context.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) { - ctx, done := event.Start(ctx, "lsp.Server.signatureHelp", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.SignatureHelp", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/symbols.go b/gopls/internal/server/symbols.go index e35b2c75451..40df7369f51 100644 --- a/gopls/internal/server/symbols.go +++ b/gopls/internal/server/symbols.go @@ -16,7 +16,7 @@ import ( ) func (s *server) DocumentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]any, error) { - ctx, done := event.Start(ctx, "lsp.Server.documentSymbol", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DocumentSymbol", label.URI.Of(params.TextDocument.URI)) defer done() fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) diff --git a/gopls/internal/server/text_synchronization.go b/gopls/internal/server/text_synchronization.go index ad1266d783e..ad8554d9302 100644 --- a/gopls/internal/server/text_synchronization.go +++ b/gopls/internal/server/text_synchronization.go @@ -92,7 +92,7 @@ func (m ModificationSource) String() string { } func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didOpen", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DidOpen", label.URI.Of(params.TextDocument.URI)) defer done() uri := params.TextDocument.URI @@ -121,7 +121,7 @@ func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocume } func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didChange", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DidChange", label.URI.Of(params.TextDocument.URI)) defer done() uri := params.TextDocument.URI @@ -174,7 +174,7 @@ func (s *server) warnAboutModifyingGeneratedFiles(ctx context.Context, uri proto } func (s *server) DidChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didChangeWatchedFiles") + ctx, done := event.Start(ctx, "server.DidChangeWatchedFiles") defer done() var modifications []file.Modification @@ -190,7 +190,7 @@ func (s *server) DidChangeWatchedFiles(ctx context.Context, params *protocol.Did } func (s *server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didSave", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DidSave", label.URI.Of(params.TextDocument.URI)) defer done() c := file.Modification{ @@ -204,7 +204,7 @@ func (s *server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocume } func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didClose", label.URI.Of(params.TextDocument.URI)) + ctx, done := event.Start(ctx, "server.DidClose", label.URI.Of(params.TextDocument.URI)) defer done() return s.didModifyFiles(ctx, []file.Modification{ diff --git a/gopls/internal/server/workspace.go b/gopls/internal/server/workspace.go index 8074ecca444..ced5656c6ac 100644 --- a/gopls/internal/server/workspace.go +++ b/gopls/internal/server/workspace.go @@ -61,7 +61,7 @@ func (s *server) addView(ctx context.Context, name string, dir protocol.Document } func (s *server) DidChangeConfiguration(ctx context.Context, _ *protocol.DidChangeConfigurationParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didChangeConfiguration") + ctx, done := event.Start(ctx, "server.DidChangeConfiguration") defer done() var wg sync.WaitGroup @@ -143,7 +143,7 @@ func (s *server) DidChangeConfiguration(ctx context.Context, _ *protocol.DidChan } func (s *server) DidCreateFiles(ctx context.Context, params *protocol.CreateFilesParams) error { - ctx, done := event.Start(ctx, "lsp.Server.didCreateFiles") + ctx, done := event.Start(ctx, "server.DidCreateFiles") defer done() var allChanges []protocol.DocumentChange diff --git a/gopls/internal/server/workspace_symbol.go b/gopls/internal/server/workspace_symbol.go index 9eafeb015ad..f34e76f7937 100644 --- a/gopls/internal/server/workspace_symbol.go +++ b/gopls/internal/server/workspace_symbol.go @@ -20,7 +20,7 @@ func (s *server) Symbol(ctx context.Context, params *protocol.WorkspaceSymbolPar recordLatency(ctx, rerr) }() - ctx, done := event.Start(ctx, "lsp.Server.symbol") + ctx, done := event.Start(ctx, "server.Symbol") defer done() views := s.session.Views() From e73cd5af773602fdbc6cab94bdc0dc38abf828ab Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 15 Mar 2025 09:03:36 -0400 Subject: [PATCH 306/309] gopls/internal/golang: implement dynamicFuncCallType with typeutil.ClassifyCall Also fix some typos I came across. Change-Id: Ib3e73852c8260bf0a537ffb7c23ec1815c9546e1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/658236 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/implementation.go | 29 +++------- .../testdata/implementation/issue67041.txt | 4 +- internal/typesinternal/classify_call.go | 41 +++++--------- internal/typesinternal/classify_call_test.go | 54 ++++++++----------- 4 files changed, 45 insertions(+), 83 deletions(-) diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index b9a332ac62a..a5ab5d19a13 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -32,6 +32,7 @@ import ( "golang.org/x/tools/internal/astutil/cursor" "golang.org/x/tools/internal/astutil/edge" "golang.org/x/tools/internal/event" + "golang.org/x/tools/internal/typesinternal" ) // This file defines the new implementation of the 'implementation' @@ -937,6 +938,9 @@ func implFuncs(pkg *cache.Package, pgf *parsego.File, pos token.Pos) ([]protocol } info := pkg.TypesInfo() + if info.Types == nil || info.Defs == nil || info.Uses == nil { + panic("one of info.Types, .Defs or .Uses is nil") + } // Find innermost enclosing FuncType or CallExpr. // @@ -1088,29 +1092,10 @@ func beneathFuncDef(cur cursor.Cursor) bool { // // Tested via ../test/marker/testdata/implementation/signature.txt. func dynamicFuncCallType(info *types.Info, call *ast.CallExpr) types.Type { - fun := ast.Unparen(call.Fun) - tv := info.Types[fun] - - // Reject conversion, or call to built-in. - if !tv.IsValue() { - return nil - } - - // Reject call to named func/method. - if id, ok := fun.(*ast.Ident); ok && is[*types.Func](info.Uses[id]) { - return nil + if typesinternal.ClassifyCall(info, call) == typesinternal.CallDynamic { + return info.Types[call.Fun].Type.Underlying() } - - // Reject method selections (T.method() or x.method()) - if sel, ok := fun.(*ast.SelectorExpr); ok { - seln, ok := info.Selections[sel] - if !ok || seln.Kind() != types.FieldVal { - return nil - } - } - - // TODO(adonovan): consider x() where x : TypeParam. - return tv.Type.Underlying() // e.g. x() or x.field() + return nil } // inToken reports whether pos is within the token of diff --git a/gopls/internal/test/marker/testdata/implementation/issue67041.txt b/gopls/internal/test/marker/testdata/implementation/issue67041.txt index 3b058534cd3..78965200b20 100644 --- a/gopls/internal/test/marker/testdata/implementation/issue67041.txt +++ b/gopls/internal/test/marker/testdata/implementation/issue67041.txt @@ -1,5 +1,5 @@ -This test verifies that implementations uses the correct object when querying -local implementations . As described in golang/go#67041), a bug led to it +This test verifies that Implementations uses the correct object when querying +local implementations. As described in golang/go#67041, a bug led to it comparing types from different realms. -- go.mod -- diff --git a/internal/typesinternal/classify_call.go b/internal/typesinternal/classify_call.go index 9d4da859370..35e0f80248f 100644 --- a/internal/typesinternal/classify_call.go +++ b/internal/typesinternal/classify_call.go @@ -42,19 +42,6 @@ func (k CallKind) String() string { // and further classifies function calls as static calls (where the function is known), // dynamic interface calls, and other dynamic calls. // -// For static, interface and builtin calls, ClassifyCall returns the [types.Object] -// for the name of the caller. For calls of instantiated functions and -// methods, it returns the object for the corresponding generic function -// or method on the generic type. -// The relationships between the return values are: -// -// CallKind object -// CallStatic *types.Func -// CallInterface *types.Func -// CallBuiltin *types.Builtin -// CallDynamic nil -// CallConversion nil -// // For the declarations: // // func f() {} @@ -66,33 +53,33 @@ func (k CallKind) String() string { // // ClassifyCall returns the following: // -// f() CallStatic the *types.Func for f -// g[int]() CallStatic the *types.Func for g[T] -// i.M() CallInterface the *types.Func for i.M -// min(1, 2) CallBuiltin the *types.Builtin for min -// v() CallDynamic nil -// s[0]() CallDynamic nil -// int(x) CallConversion nil -// []byte("") CallConversion nil -func ClassifyCall(info *types.Info, call *ast.CallExpr) (CallKind, types.Object) { +// f() CallStatic +// g[int]() CallStatic +// i.M() CallInterface +// min(1, 2) CallBuiltin +// v() CallDynamic +// s[0]() CallDynamic +// int(x) CallConversion +// []byte("") CallConversion +func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind { if info.Types == nil { panic("ClassifyCall: info.Types is nil") } if info.Types[call.Fun].IsType() { - return CallConversion, nil + return CallConversion } obj := Used(info, call.Fun) // Classify the call by the type of the object, if any. switch obj := obj.(type) { case *types.Builtin: - return CallBuiltin, obj + return CallBuiltin case *types.Func: if interfaceMethod(obj) { - return CallInterface, obj + return CallInterface } - return CallStatic, obj + return CallStatic default: - return CallDynamic, nil + return CallDynamic } } diff --git a/internal/typesinternal/classify_call_test.go b/internal/typesinternal/classify_call_test.go index 6a30ee280df..42bdd193725 100644 --- a/internal/typesinternal/classify_call_test.go +++ b/internal/typesinternal/classify_call_test.go @@ -101,34 +101,30 @@ func TestClassifyCallAndUsed(t *testing.T) { typeParam := lookup("tests").Type().(*types.Signature).TypeParams().At(0).Obj() - // A unique value for marking that Used returns the same object as ClassifyCall. - same := &types.Label{} - // Expected Calls are in the order of CallExprs at the end of src, above. wants := []struct { - kind ti.CallKind - classifyObj types.Object // the object returned from ClassifyCall - usedObj types.Object // the object returned from Used, sometimes different + kind ti.CallKind + usedObj types.Object // the object returned from Used }{ - {ti.CallStatic, lookup("g"), same}, // g - {ti.CallDynamic, nil, lookup("f")}, // f - {ti.CallBuiltin, printlnObj, same}, // println - {ti.CallStatic, member("S", "g"), same}, // z.g - {ti.CallStatic, member("S", "g"), same}, // a.b.c.g - {ti.CallStatic, member("S", "g"), same}, // S.g(z, 1) - {ti.CallDynamic, nil, member("z", "f")}, // z.f - {ti.CallInterface, member("I", "m"), same}, // I(nil).m - {ti.CallConversion, nil, lookup("I")}, // I(nil) - {ti.CallDynamic, nil, same}, // m[0] - {ti.CallDynamic, nil, same}, // n[0] - {ti.CallStatic, lookup("F"), same}, // F[int] - {ti.CallStatic, lookup("F"), same}, // F[T] - {ti.CallDynamic, nil, same}, // f(){} - {ti.CallConversion, nil, same}, // []byte - {ti.CallConversion, nil, lookup("A")}, // A[int] - {ti.CallConversion, nil, typeParam}, // T - {ti.CallStatic, member("S", "g"), same}, // (z.g) - {ti.CallStatic, member("S", "g"), same}, // (z).g + {ti.CallStatic, lookup("g")}, // g + {ti.CallDynamic, lookup("f")}, // f + {ti.CallBuiltin, printlnObj}, // println + {ti.CallStatic, member("S", "g")}, // z.g + {ti.CallStatic, member("S", "g")}, // a.b.c.g + {ti.CallStatic, member("S", "g")}, // S.g(z, 1) + {ti.CallDynamic, member("z", "f")}, // z.f + {ti.CallInterface, member("I", "m")}, // I(nil).m + {ti.CallConversion, lookup("I")}, // I(nil) + {ti.CallDynamic, nil}, // m[0] + {ti.CallDynamic, nil}, // n[0] + {ti.CallStatic, lookup("F")}, // F[int] + {ti.CallStatic, lookup("F")}, // F[T] + {ti.CallDynamic, nil}, // f(){} + {ti.CallConversion, nil}, // []byte + {ti.CallConversion, lookup("A")}, // A[int] + {ti.CallConversion, typeParam}, // T + {ti.CallStatic, member("S", "g")}, // (z.g) + {ti.CallStatic, member("S", "g")}, // (z).g } i := 0 @@ -143,20 +139,14 @@ func TestClassifyCallAndUsed(t *testing.T) { } prefix := fmt.Sprintf("%s (#%d)", buf.String(), i) - gotKind, gotObj := ti.ClassifyCall(info, call) + gotKind := ti.ClassifyCall(info, call) want := wants[i] if gotKind != want.kind { t.Errorf("%s kind: got %s, want %s", prefix, gotKind, want.kind) } - if gotObj != want.classifyObj { - t.Errorf("%s obj: got %v (%[2]T), want %v", prefix, gotObj, want.classifyObj) - } w := want.usedObj - if w == same { - w = want.classifyObj - } if g := ti.Used(info, call.Fun); g != w { t.Errorf("%s used obj: got %v (%[2]T), want %v", prefix, g, w) } From 9a1fbbdb530258f2c0db9c411aecf399d1ec256b Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 4 Apr 2025 16:15:19 -0400 Subject: [PATCH 307/309] internal/typesinternal: change Used to UsedIdent Add UsedIdent, which returns the identifier underlying a used object. To get the object associate with e, one can now write info.Uses[UsedIdent(e)] This replaces Used, whose job can now be done by the above expression. As a demonstration, we can simplify the unusedparams analysis. Change-Id: Id6b08b548fa495d42de4a6767bba7717ad1b0d08 Reviewed-on: https://go-review.googlesource.com/c/tools/+/663035 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- go/analysis/passes/nilfunc/nilfunc.go | 2 +- go/types/typeutil/callee.go | 21 +++++------ .../analysis/unusedparams/unusedparams.go | 17 +-------- internal/typesinternal/classify_call.go | 37 ++++++++++--------- internal/typesinternal/classify_call_test.go | 4 +- 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/go/analysis/passes/nilfunc/nilfunc.go b/go/analysis/passes/nilfunc/nilfunc.go index 2b28c5a6b2c..fa1883b0c34 100644 --- a/go/analysis/passes/nilfunc/nilfunc.go +++ b/go/analysis/passes/nilfunc/nilfunc.go @@ -56,7 +56,7 @@ func run(pass *analysis.Pass) (any, error) { } // Only want functions. - obj := typesinternal.Used(pass.TypesInfo, e2) + obj := pass.TypesInfo.Uses[typesinternal.UsedIdent(pass.TypesInfo, e2)] if _, ok := obj.(*types.Func); !ok { return } diff --git a/go/types/typeutil/callee.go b/go/types/typeutil/callee.go index eeeb570a73c..53b71339305 100644 --- a/go/types/typeutil/callee.go +++ b/go/types/typeutil/callee.go @@ -18,7 +18,7 @@ import ( // Note: for calls of instantiated functions and methods, Callee returns // the corresponding generic function or method on the generic type. func Callee(info *types.Info, call *ast.CallExpr) types.Object { - obj := used(info, call.Fun) + obj := info.Uses[usedIdent(info, call.Fun)] if obj == nil { return nil } @@ -34,7 +34,7 @@ func Callee(info *types.Info, call *ast.CallExpr) types.Object { // Note: for calls of instantiated functions and methods, StaticCallee returns // the corresponding generic function or method on the generic type. func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { - obj := used(info, call.Fun) + obj := info.Uses[usedIdent(info, call.Fun)] fn, _ := obj.(*types.Func) if fn == nil || interfaceMethod(fn) { return nil @@ -42,14 +42,14 @@ func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { return fn } -// used is the implementation of [internal/typesinternal.Used]. -// It returns the object associated with e. -// See typesinternal.Used for a fuller description. +// usedIdent is the implementation of [internal/typesinternal.UsedIdent]. +// It returns the identifier associated with e. +// See typesinternal.UsedIdent for a fuller description. // This function should live in typesinternal, but cannot because it would // create an import cycle. // -//go:linkname used -func used(info *types.Info, e ast.Expr) types.Object { +//go:linkname usedIdent +func usedIdent(info *types.Info, e ast.Expr) *ast.Ident { if info.Types == nil || info.Uses == nil { panic("one of info.Types or info.Uses is nil; both must be populated") } @@ -63,17 +63,16 @@ func used(info *types.Info, e ast.Expr) types.Object { e = d.X } - var obj types.Object switch e := ast.Unparen(e).(type) { // info.Uses always has the object we want, even for selector expressions. // We don't need info.Selections. // See go/types/recording.go:recordSelection. case *ast.Ident: - obj = info.Uses[e] // type, var, builtin, or declared func + return e case *ast.SelectorExpr: - obj = info.Uses[e.Sel] // selector e.f or T.f or qualified identifier pkg.X + return e.Sel } - return obj + return nil } // interfaceMethod reports whether its argument is a method of an interface. diff --git a/gopls/internal/analysis/unusedparams/unusedparams.go b/gopls/internal/analysis/unusedparams/unusedparams.go index 559b65d2bc2..12076c5f273 100644 --- a/gopls/internal/analysis/unusedparams/unusedparams.go +++ b/gopls/internal/analysis/unusedparams/unusedparams.go @@ -80,24 +80,9 @@ func run(pass *analysis.Pass) (any, error) { inspect.Preorder(filter, func(n ast.Node) { switch n := n.(type) { case *ast.CallExpr: - // Strip off any generic instantiation. - fun := n.Fun - switch fun_ := fun.(type) { - case *ast.IndexExpr: - fun = fun_.X // f[T]() (funcs[i]() is rejected below) - case *ast.IndexListExpr: - fun = fun_.X // f[K, V]() - } - + id := typesinternal.UsedIdent(pass.TypesInfo, n.Fun) // Find object: // record non-exported function, method, or func-typed var. - var id *ast.Ident - switch fun := fun.(type) { - case *ast.Ident: - id = fun - case *ast.SelectorExpr: - id = fun.Sel - } if id != nil && !id.IsExported() { switch pass.TypesInfo.Uses[id].(type) { case *types.Func, *types.Var: diff --git a/internal/typesinternal/classify_call.go b/internal/typesinternal/classify_call.go index 35e0f80248f..649c82b6bea 100644 --- a/internal/typesinternal/classify_call.go +++ b/internal/typesinternal/classify_call.go @@ -68,7 +68,7 @@ func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind { if info.Types[call.Fun].IsType() { return CallConversion } - obj := Used(info, call.Fun) + obj := info.Uses[UsedIdent(info, call.Fun)] // Classify the call by the type of the object, if any. switch obj := obj.(type) { case *types.Builtin: @@ -83,7 +83,9 @@ func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind { } } -// Used returns the [types.Object] used by e, if any. +// UsedIdent returns the identifier such that info.Uses[UsedIdent(info, e)] +// is the [types.Object] used by e, if any. +// // If e is one of various forms of reference: // // f, c, v, T lexical reference @@ -92,7 +94,8 @@ func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind { // expr.f field or method value selector // T.f method expression selector // -// Used returns the object to which it refers. +// UsedIdent returns the identifier whose is associated value in [types.Info.Uses] +// is the object to which it refers. // // For the declarations: // @@ -105,28 +108,28 @@ func ClassifyCall(info *types.Info, call *ast.CallExpr) CallKind { // i I // ) // -// Used returns the following: +// UsedIdent returns the following: // -// Expr Used -// x the *types.Var for x -// s.f the *types.Var for f -// F[int] the *types.Func for F[T] (not F[int]) -// i.M the *types.Func for i.M -// I.M the *types.Func for I.M -// min the *types.Builtin for min -// int the *types.TypeName for int +// Expr UsedIdent +// x x +// s.f f +// F[int] F +// i.M M +// I.M M +// min min +// int int // 1 nil // a[0] nil // []byte nil // -// Note: if e is an instantiated function or method, Used returns +// Note: if e is an instantiated function or method, UsedIdent returns // the corresponding generic function or method on the generic type. -func Used(info *types.Info, e ast.Expr) types.Object { - return used(info, e) +func UsedIdent(info *types.Info, e ast.Expr) *ast.Ident { + return usedIdent(info, e) } -//go:linkname used golang.org/x/tools/go/types/typeutil.used -func used(info *types.Info, e ast.Expr) types.Object +//go:linkname usedIdent golang.org/x/tools/go/types/typeutil.usedIdent +func usedIdent(info *types.Info, e ast.Expr) *ast.Ident //go:linkname interfaceMethod golang.org/x/tools/go/types/typeutil.interfaceMethod func interfaceMethod(f *types.Func) bool diff --git a/internal/typesinternal/classify_call_test.go b/internal/typesinternal/classify_call_test.go index 42bdd193725..e875727d1a5 100644 --- a/internal/typesinternal/classify_call_test.go +++ b/internal/typesinternal/classify_call_test.go @@ -104,7 +104,7 @@ func TestClassifyCallAndUsed(t *testing.T) { // Expected Calls are in the order of CallExprs at the end of src, above. wants := []struct { kind ti.CallKind - usedObj types.Object // the object returned from Used + usedObj types.Object // the object obtained from the result of UsedIdent }{ {ti.CallStatic, lookup("g")}, // g {ti.CallDynamic, lookup("f")}, // f @@ -147,7 +147,7 @@ func TestClassifyCallAndUsed(t *testing.T) { } w := want.usedObj - if g := ti.Used(info, call.Fun); g != w { + if g := info.Uses[ti.UsedIdent(info, call.Fun)]; g != w { t.Errorf("%s used obj: got %v (%[2]T), want %v", prefix, g, w) } i++ From 5916e3cbd8b65f73c18253607fa0b696fc5b9da6 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 7 Apr 2025 15:51:09 -0400 Subject: [PATCH 308/309] internal/tokeninternal: AddExistingFiles: tweaks for proposal This CL clarifies AddExistingFiles in preparation for its proposal as an upstream change. The two helpers FileSetFor and CloneFileSet are demonstrably mere convenience functions. Updates golang/go#73205 Change-Id: I9b83dc46575417ac074d91f3d5bed942b522da6b Reviewed-on: https://go-review.googlesource.com/c/tools/+/663596 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan Reviewed-by: Jonathan Amsterdam --- internal/tokeninternal/tokeninternal.go | 37 +++++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/tokeninternal/tokeninternal.go b/internal/tokeninternal/tokeninternal.go index 0a73e2ebda3..549bb183976 100644 --- a/internal/tokeninternal/tokeninternal.go +++ b/internal/tokeninternal/tokeninternal.go @@ -9,6 +9,7 @@ package tokeninternal import ( "fmt" "go/token" + "slices" "sort" "sync" "sync/atomic" @@ -18,7 +19,29 @@ import ( // AddExistingFiles adds the specified files to the FileSet if they // are not already present. It panics if any pair of files in the // resulting FileSet would overlap. +// +// TODO(adonovan): add this a method to FileSet; see +// https://github.com/golang/go/issues/73205 func AddExistingFiles(fset *token.FileSet, files []*token.File) { + + // This function cannot be implemented as: + // + // for _, file := range files { + // if prev := fset.File(token.Pos(file.Base())); prev != nil { + // if prev != file { + // panic("FileSet contains a different file at the same base") + // } + // continue + // } + // file2 := fset.AddFile(file.Name(), file.Base(), file.Size()) + // file2.SetLines(file.Lines()) + // } + // + // because all calls to AddFile must be in increasing order. + // AddExistingFiles lets us augment an existing FileSet + // sequentially, so long as all sets of files have disjoint + // ranges. + // Punch through the FileSet encapsulation. type tokenFileSet struct { // This type remained essentially consistent from go1.16 to go1.21. @@ -83,10 +106,7 @@ func AddExistingFiles(fset *token.FileSet, files []*token.File) { // of their Base. func FileSetFor(files ...*token.File) *token.FileSet { fset := token.NewFileSet() - for _, f := range files { - f2 := fset.AddFile(f.Name(), f.Base(), f.Size()) - f2.SetLines(f.Lines()) - } + AddExistingFiles(fset, files) return fset } @@ -94,12 +114,5 @@ func FileSetFor(files ...*token.File) *token.FileSet { // create copies of the token.Files in fset: they are added to the resulting // FileSet unmodified. func CloneFileSet(fset *token.FileSet) *token.FileSet { - var files []*token.File - fset.Iterate(func(f *token.File) bool { - files = append(files, f) - return true - }) - newFileSet := token.NewFileSet() - AddExistingFiles(newFileSet, files) - return newFileSet + return FileSetFor(slices.Collect(fset.Iterate)...) } From 456962ef0d65798b244374a272fb19b7d9723b62 Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Mon, 7 Apr 2025 14:12:03 -0700 Subject: [PATCH 309/309] go.mod: update golang.org/x dependencies Update golang.org/x dependencies to their latest tagged versions. Change-Id: Ia69bcc0a41699a8a7b57bc09bee11baa68495e3b Reviewed-on: https://go-review.googlesource.com/c/tools/+/663616 Auto-Submit: Gopher Robot LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: David Chase --- go.mod | 6 +++--- go.sum | 12 ++++++------ gopls/go.mod | 6 +++--- gopls/go.sum | 18 +++++++++--------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 3a120629b94..7e4e371b770 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( github.com/google/go-cmp v0.6.0 github.com/yuin/goldmark v1.4.13 golang.org/x/mod v0.24.0 - golang.org/x/net v0.37.0 - golang.org/x/sync v0.12.0 + golang.org/x/net v0.39.0 + golang.org/x/sync v0.13.0 golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 ) -require golang.org/x/sys v0.31.0 // indirect +require golang.org/x/sys v0.32.0 // indirect diff --git a/go.sum b/go.sum index 3d0337c8351..ff5857bd93a 100644 --- a/go.sum +++ b/go.sum @@ -4,11 +4,11 @@ github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= -golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= -golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= -golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= -golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY= +golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= +golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= +golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= +golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMePxjXAJctxKmyjKUsjA11Uzuk= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= diff --git a/gopls/go.mod b/gopls/go.mod index 5cabb7974de..c09e2daf7bd 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -6,10 +6,10 @@ require ( github.com/google/go-cmp v0.6.0 github.com/jba/templatecheck v0.7.1 golang.org/x/mod v0.24.0 - golang.org/x/sync v0.12.0 - golang.org/x/sys v0.31.0 + golang.org/x/sync v0.13.0 + golang.org/x/sys v0.32.0 golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc - golang.org/x/text v0.23.0 + golang.org/x/text v0.24.0 golang.org/x/tools v0.30.0 golang.org/x/vuln v1.1.4 gopkg.in/yaml.v3 v3.0.1 diff --git a/gopls/go.sum b/gopls/go.sum index 20633541388..f5a9bbde4ca 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -16,7 +16,7 @@ github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGK github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa h1:Br3+0EZZohShrmVVc85znGpxw7Ca8hsUJlrdT/JQGw8= golang.org/x/exp/typeparams v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:LKZHyeOpPuZcMgxeHjJp4p5yvxrCX1xDvH10zYHhjjQ= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= @@ -25,27 +25,27 @@ golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= -golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= +golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= +golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc h1:HS+G1Mhh2dxM8ObutfYKdjfD7zpkyeP/UxeRnJpIZtQ= golang.org/x/telemetry v0.0.0-20250220152412-165e2f84edbc/go.mod h1:bDzXkYUaHzz51CtDy5kh/jR4lgPxsdbqC37kp/dzhCc= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g= +golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= -golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= +golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= golang.org/x/vuln v1.1.4 h1:Ju8QsuyhX3Hk8ma3CesTbO8vfJD9EvUBgHvkxHBzj0I= golang.org/x/vuln v1.1.4/go.mod h1:F+45wmU18ym/ca5PLTPLsSzr2KppzswxPP603ldA67s= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=