gopls/internal/util/typesutil: refine EnclosingSignature bug.Report
This CL causes EnclosingSignature to report a bug when it fails
because of missing type information, but not due to missing
syntax (no enclosing function). With luck it will help us track
down the associated issue.
For golang/go#70666
Change-Id: Icec1651e2d1c43a5265a576b796a660eb8f836f2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/728080
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Madeline Kalil <mkalil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/gopls/internal/golang/stubmethods/stubmethods.go b/gopls/internal/golang/stubmethods/stubmethods.go
index 136e7ee..6f2bf3c 100644
--- a/gopls/internal/golang/stubmethods/stubmethods.go
+++ b/gopls/internal/golang/stubmethods/stubmethods.go
@@ -20,7 +20,6 @@
"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"
)
@@ -270,8 +269,12 @@
sig := typesutil.EnclosingSignature(curResult, info)
if sig == nil {
- // golang/go#70666: this bug may be reached in practice.
- return nil, bug.Errorf("could not find the enclosing function of the return statement")
+ // Either curResult is not within a function (incontheivable?),
+ // or the function's type information is missing (in which case
+ // EnclosingSignature will have called bug.Report).
+ // Don't report a second bug here.
+ // See https://go.dev/issue/70666.
+ return nil, fmt.Errorf("internal error: return statement lacks type information or enclosing function (issue 70666)")
}
rets := sig.Results()
// The return operands and function results must match.
diff --git a/gopls/internal/util/typesutil/typesutil.go b/gopls/internal/util/typesutil/typesutil.go
index 5558c67..9d95581 100644
--- a/gopls/internal/util/typesutil/typesutil.go
+++ b/gopls/internal/util/typesutil/typesutil.go
@@ -13,6 +13,7 @@
"golang.org/x/tools/go/ast/edge"
"golang.org/x/tools/go/ast/inspector"
+ "golang.org/x/tools/gopls/internal/util/bug"
)
// FormatTypeParams turns TypeParamList into its Go representation, such as:
@@ -188,21 +189,25 @@
}
// EnclosingSignature returns the signature of the innermost
-// function enclosing the syntax node denoted by cur
-// or nil if the node is not within a function.
+// function enclosing the syntax node denoted by cur.
+// It returns nil if the node is not within a function,
+// or the function's type information is missing.
func EnclosingSignature(cur inspector.Cursor, info *types.Info) *types.Signature {
+loop:
for c := range cur.Enclosing((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) {
switch n := c.Node().(type) {
case *ast.FuncDecl:
if f, ok := info.Defs[n.Name]; ok {
return f.Type().(*types.Signature)
}
- return nil
+ bug.Reportf("FuncDecl defines no types.Func (#70666)")
+ break loop
case *ast.FuncLit:
if f, ok := info.Types[n]; ok {
return f.Type.(*types.Signature)
}
- return nil
+ bug.Reportf("FuncLit has no type (#70666)")
+ break loop
}
}
return nil