From 9a041363f32ff667dcdd74248070db07106dfd78 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 3 Dec 2024 22:29:23 +0000 Subject: [PATCH] gopls/internal/golang/stubmethods: refine crash into bug report This CL refine an as-yet unreproducible crash into a bug report. It seems clear that the code intended to check that sig != nil, yet instead checked that sig.Results != nil. With that said, I don't think it's possible to have a return statement without an enclosing signature. For now, turn this into a bug report. For golang/go#70666 Change-Id: I79031d437d643f6d70360e3abde86166176647b3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633375 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/stubmethods/stubmethods.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gopls/internal/golang/stubmethods/stubmethods.go b/gopls/internal/golang/stubmethods/stubmethods.go index 450a8e4ddca..f380f5b984d 100644 --- a/gopls/internal/golang/stubmethods/stubmethods.go +++ b/gopls/internal/golang/stubmethods/stubmethods.go @@ -13,9 +13,11 @@ import ( "go/ast" "go/token" "go/types" - "golang.org/x/tools/internal/typesinternal" "strings" + "golang.org/x/tools/internal/typesinternal" + + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/gopls/internal/util/typesutil" ) @@ -282,8 +284,9 @@ func fromReturnStmt(fset *token.FileSet, info *types.Info, pos token.Pos, path [ } sig := typesutil.EnclosingSignature(path, info) - if sig.Results() == nil { - return nil, fmt.Errorf("could not find the enclosing function of the return statement") + 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") } rets := sig.Results() // The return operands and function results must match.