From c2e057bbd2de0afa44c8449ee828c700db19d2e9 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 9 Sep 2024 13:50:43 -0400 Subject: [PATCH] gopls: use Func.Signature everywhere Updates golang/go#65917 Change-Id: I20bec8b0a1778f0d2d81f729d12ba966799c7805 Reviewed-on: https://go-review.googlesource.com/c/tools/+/612037 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/methodsets/methodsets.go | 2 +- gopls/internal/golang/code_lens.go | 2 +- gopls/internal/golang/codeaction.go | 2 +- gopls/internal/golang/completion/completion.go | 2 +- gopls/internal/golang/completion/format.go | 2 +- gopls/internal/golang/hover.go | 6 +++--- gopls/internal/golang/implementation.go | 4 ++-- gopls/internal/golang/pkgdoc.go | 10 +++++----- gopls/internal/golang/references.go | 2 +- gopls/internal/golang/rename.go | 6 +++--- gopls/internal/golang/rename_check.go | 2 +- gopls/internal/golang/stub.go | 4 ++-- 12 files changed, 22 insertions(+), 22 deletions(-) diff --git a/gopls/internal/cache/methodsets/methodsets.go b/gopls/internal/cache/methodsets/methodsets.go index ed7ead7a747..f6a2ba96b33 100644 --- a/gopls/internal/cache/methodsets/methodsets.go +++ b/gopls/internal/cache/methodsets/methodsets.go @@ -449,7 +449,7 @@ func fingerprint(method *types.Func) (string, bool) { } buf.WriteString(method.Id()) // e.g. "pkg.Type" - sig := method.Type().(*types.Signature) + sig := method.Signature() fprint(sig.Params()) fprint(sig.Results()) return buf.String(), tricky diff --git a/gopls/internal/golang/code_lens.go b/gopls/internal/golang/code_lens.go index a4aab3e16b0..26380b865af 100644 --- a/gopls/internal/golang/code_lens.go +++ b/gopls/internal/golang/code_lens.go @@ -129,7 +129,7 @@ func matchTestFunc(fn *ast.FuncDecl, info *types.Info, nameRe *regexp.Regexp, pa if !ok { return false } - sig := obj.Type().(*types.Signature) + sig := obj.Signature() // Test functions should have only one parameter. if sig.Params().Len() != 1 { return false diff --git a/gopls/internal/golang/codeaction.go b/gopls/internal/golang/codeaction.go index 8f402267393..1921d1326b8 100644 --- a/gopls/internal/golang/codeaction.go +++ b/gopls/internal/golang/codeaction.go @@ -597,7 +597,7 @@ func getGoAssemblyAction(view *cache.View, pkg *cache.Package, pgf *parsego.File if len(path) >= 2 { // [... FuncDecl File] if decl, ok := path[len(path)-2].(*ast.FuncDecl); ok { if fn, ok := pkg.TypesInfo().Defs[decl.Name].(*types.Func); ok { - sig := fn.Type().(*types.Signature) + sig := fn.Signature() // Compute the linker symbol of the enclosing function. var sym strings.Builder diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index 88528df6a79..3fdc2a8c62a 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -1036,7 +1036,7 @@ func (c *completer) populateCommentCompletions(comment *ast.CommentGroup) { // collect receiver struct fields if node.Recv != nil { - sig := c.pkg.TypesInfo().Defs[node.Name].(*types.Func).Type().(*types.Signature) + sig := c.pkg.TypesInfo().Defs[node.Name].(*types.Func).Signature() _, named := typesinternal.ReceiverNamed(sig.Recv()) // may be nil if ill-typed if named != nil { if recvStruct, ok := named.Underlying().(*types.Struct); ok { diff --git a/gopls/internal/golang/completion/format.go b/gopls/internal/golang/completion/format.go index dbc57c18082..2e35fd5de38 100644 --- a/gopls/internal/golang/completion/format.go +++ b/gopls/internal/golang/completion/format.go @@ -95,7 +95,7 @@ func (c *completer) item(ctx context.Context, cand candidate) (CompletionItem, e break } case *types.Func: - if obj.Type().(*types.Signature).Recv() == nil { + if obj.Signature().Recv() == nil { kind = protocol.FunctionCompletion } else { kind = protocol.MethodCompletion diff --git a/gopls/internal/golang/hover.go b/gopls/internal/golang/hover.go index 129adde7fcc..019d09ac027 100644 --- a/gopls/internal/golang/hover.go +++ b/gopls/internal/golang/hover.go @@ -535,7 +535,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro var recv types.Object switch obj := obj.(type) { case *types.Func: - sig := obj.Type().(*types.Signature) + sig := obj.Signature() if sig.Recv() != nil { tname := typeToObject(sig.Recv().Type()) if tname != nil { // beware typed nil @@ -962,7 +962,7 @@ func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file // specifically, we show the receiver name, // and replace the period in (T).f by a space (#62190). - sig := obj.Type().(*types.Signature) + sig := obj.Signature() var buf bytes.Buffer buf.WriteString("func ") @@ -1236,7 +1236,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol { // Handle Method. if fn, _ := obj.(*types.Func); fn != nil { - isPtr, named := typesinternal.ReceiverNamed(fn.Type().(*types.Signature).Recv()) + isPtr, named := typesinternal.ReceiverNamed(fn.Signature().Recv()) if isPackageLevel(named.Obj()) { for _, s := range symbols { if s.Kind != stdlib.Method { diff --git a/gopls/internal/golang/implementation.go b/gopls/internal/golang/implementation.go index 72679ad7176..b3accff452f 100644 --- a/gopls/internal/golang/implementation.go +++ b/gopls/internal/golang/implementation.go @@ -126,7 +126,7 @@ func implementations(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand return obj.Type(), "" case *types.Func: // For methods, use the receiver type, which may be anonymous. - if recv := obj.Type().(*types.Signature).Recv(); recv != nil { + if recv := obj.Signature().Recv(); recv != nil { return recv.Type(), obj.Id() } } @@ -317,7 +317,7 @@ func implementsObj(ctx context.Context, snapshot *cache.Snapshot, uri protocol.D case *types.TypeName: // ok case *types.Func: - if obj.Type().(*types.Signature).Recv() == nil { + if obj.Signature().Recv() == nil { return nil, nil, fmt.Errorf("%s is a function, not a method", id.Name) } case nil: diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 38d043b7d74..ed8f1b388f0 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -120,7 +120,7 @@ func DocFragment(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (p if !sym.Exported() { // Unexported method of exported type? if fn, ok := sym.(*types.Func); ok { - if recv := fn.Type().(*types.Signature).Recv(); recv != nil { + if recv := fn.Signature().Recv(); recv != nil { _, named := typesinternal.ReceiverNamed(recv) if named != nil && named.Obj().Exported() { sym = named.Obj() @@ -147,7 +147,7 @@ func DocFragment(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (p // Inv: sym is field or method, or local. switch sym := sym.(type) { case *types.Func: // => method - sig := sym.Type().(*types.Signature) + sig := sym.Signature() isPtr, named := typesinternal.ReceiverNamed(sig.Recv()) if named != nil { if !named.Obj().Exported() { @@ -469,7 +469,7 @@ window.addEventListener('load', function() { label := obj.Name() // for a type if fn, ok := obj.(*types.Func); ok { var buf strings.Builder - sig := fn.Type().(*types.Signature) + sig := fn.Signature() if sig.Recv() != nil { fmt.Fprintf(&buf, "(%s) ", sig.Recv().Name()) fragment = recvType + "." + fn.Name() @@ -551,7 +551,7 @@ window.addEventListener('load', function() { // method of package-level named type? if fn, ok := obj.(*types.Func); ok { - sig := fn.Type().(*types.Signature) + sig := fn.Signature() if sig.Recv() != nil { _, named := typesinternal.ReceiverNamed(sig.Recv()) if named != nil { @@ -648,7 +648,7 @@ window.addEventListener('load', function() { fnString := func(fn *types.Func) string { pkgRelative := typesinternal.NameRelativeTo(pkg.Types()) - sig := fn.Type().(*types.Signature) + sig := fn.Signature() // Emit "func (recv T) F". var buf bytes.Buffer diff --git a/gopls/internal/golang/references.go b/gopls/internal/golang/references.go index b78bd9041d5..6679b45df6b 100644 --- a/gopls/internal/golang/references.go +++ b/gopls/internal/golang/references.go @@ -615,7 +615,7 @@ func localReferences(pkg *cache.Package, targets map[types.Object]bool, correspo // comparisons for obj, if it is a method, or nil otherwise. func effectiveReceiver(obj types.Object) types.Type { if fn, ok := obj.(*types.Func); ok { - if recv := fn.Type().(*types.Signature).Recv(); recv != nil { + if recv := fn.Signature().Recv(); recv != nil { return methodsets.EnsurePointer(recv.Type()) } } diff --git a/gopls/internal/golang/rename.go b/gopls/internal/golang/rename.go index c5cf0ac0932..12d9d283915 100644 --- a/gopls/internal/golang/rename.go +++ b/gopls/internal/golang/rename.go @@ -426,7 +426,7 @@ func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, f file.Handle // contain a reference (xrefs) to the target field. case *types.Func: - if obj.Type().(*types.Signature).Recv() != nil { + if obj.Signature().Recv() != nil { transitive = true // method } @@ -978,7 +978,7 @@ func renameObjects(newName string, pkg *cache.Package, targets ...types.Object) // TODO(adonovan): pull this into the caller. for _, obj := range targets { if obj, ok := obj.(*types.Func); ok { - recv := obj.Type().(*types.Signature).Recv() + recv := obj.Signature().Recv() if recv != nil && types.IsInterface(recv.Type().Underlying()) { r.changeMethods = true break @@ -1168,7 +1168,7 @@ func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit, if !isFunc { continue } - recv := obj.Type().(*types.Signature).Recv() + recv := obj.Signature().Recv() if recv == nil { continue } diff --git a/gopls/internal/golang/rename_check.go b/gopls/internal/golang/rename_check.go index 497f1a09ca2..574ea8dbea7 100644 --- a/gopls/internal/golang/rename_check.go +++ b/gopls/internal/golang/rename_check.go @@ -883,7 +883,7 @@ func (r *renamer) satisfy() map[satisfy.Constraint]bool { // recv returns the method's receiver. func recv(meth *types.Func) *types.Var { - return meth.Type().(*types.Signature).Recv() + return meth.Signature().Recv() } // someUse returns an arbitrary use of obj within info. diff --git a/gopls/internal/golang/stub.go b/gopls/internal/golang/stub.go index 47bcf3a7dcf..db405631c9e 100644 --- a/gopls/internal/golang/stub.go +++ b/gopls/internal/golang/stub.go @@ -208,7 +208,7 @@ func stubMethodsFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache. // Otherwise, use lowercase for the first letter of the object. rn := strings.ToLower(si.Concrete.Obj().Name()[0:1]) for i := 0; i < si.Concrete.NumMethods(); i++ { - if recv := si.Concrete.Method(i).Type().(*types.Signature).Recv(); recv.Name() != "" { + if recv := si.Concrete.Method(i).Signature().Recv(); recv.Name() != "" { rn = recv.Name() break } @@ -229,7 +229,7 @@ func stubMethodsFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache. for index := range missing { mrn := rn + " " - sig := missing[index].fn.Type().(*types.Signature) + sig := missing[index].fn.Signature() if checkRecvName(sig.Params()) || checkRecvName(sig.Results()) { mrn = "" }