From 7d6c71f28a65221daa3a0866385eb51216793823 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 17 Nov 2021 11:20:46 -0500 Subject: [PATCH] internal/lsp/source/completion: avoid invalid AST in enclosingSignature Guard against the panic in golang/go#49397, though it should not be possible with well-formed AST. Updates golang/go#49397 Change-Id: I3428504a4cfd361c1f51316d8c9aeee4aa9d7f42 Reviewed-on: https://go-review.googlesource.com/c/tools/+/364675 Trust: Robert Findley Run-TryBot: Robert Findley Reviewed-by: Hyang-Ah Hana Kim gopls-CI: kokoro TryBot-Result: Go Bot --- internal/lsp/source/completion/completion.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index dbc380c0f3..94389c74cb 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -1694,6 +1694,12 @@ func enclosingFunction(path []ast.Node, info *types.Info) *funcInfo { } case *ast.FuncLit: if typ, ok := info.Types[t]; ok { + if sig, _ := typ.Type.(*types.Signature); sig == nil { + // golang/go#49397: it should not be possible, but we somehow arrived + // here with a non-signature type, most likely due to AST mangling + // such that node.Type is not a FuncType. + return nil + } return &funcInfo{ sig: typ.Type.(*types.Signature), body: t.Body,