From cbc0cc175f843e92918825cb2d508091f057e36d Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Tue, 11 Feb 2020 21:48:52 -0800 Subject: [PATCH] internal/lsp/source: fix completion crash in append() We were crashing in cases like: var _ []byte = append([]byte{}, ""...<>) We were type asserting the type of append's second param to *types.Slice, but in this case it is a string (*types.Basic). Fix by checking the type assert was successful. Note that we still don't attempt to give string completions when appending to a byte slice. We can add that special case later once everyone is clamoring for it. Change-Id: I1d2fbd7f538e580d33c2dab4ef127a88e16d7ced Reviewed-on: https://go-review.googlesource.com/c/tools/+/219144 Run-TryBot: Muir Manders TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/source/completion.go | 4 +++- internal/lsp/testdata/lsp/primarymod/builtins/builtin_args.go | 2 ++ internal/lsp/testdata/lsp/summary.txt.golden | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 712b595ebe..3bf3fed756 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -1424,7 +1424,9 @@ Nodes: // param w/ further expressions, we expect a single // variadic item. if beyondLastParam || isLastParam && len(node.Args) > numParams { - inf.objType = sig.Params().At(numParams - 1).Type().(*types.Slice).Elem() + if slice, ok := sig.Params().At(numParams - 1).Type().(*types.Slice); ok { + inf.objType = slice.Elem() + } break Nodes } diff --git a/internal/lsp/testdata/lsp/primarymod/builtins/builtin_args.go b/internal/lsp/testdata/lsp/primarymod/builtins/builtin_args.go index 0ce5aa4802..d115ada455 100644 --- a/internal/lsp/testdata/lsp/primarymod/builtins/builtin_args.go +++ b/internal/lsp/testdata/lsp/primarymod/builtins/builtin_args.go @@ -22,6 +22,8 @@ func _() { append() //@rank(")", builtinSlice, builtinChan) + var _ []byte = append([]byte(nil), ""...) //@rank(") //") + copy() //@rank(")", builtinSlice, builtinChan) copy(aSlice, aS) //@rank(")", builtinSlice, builtinString) copy(aS, aSlice) //@rank(",", builtinSlice, builtinString) diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden index 739316e565..c5efd04365 100644 --- a/internal/lsp/testdata/lsp/summary.txt.golden +++ b/internal/lsp/testdata/lsp/summary.txt.golden @@ -4,7 +4,7 @@ CompletionSnippetCount = 67 UnimportedCompletionsCount = 11 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 -RankedCompletionsCount = 85 +RankedCompletionsCount = 86 CaseSensitiveCompletionsCount = 4 DiagnosticsCount = 38 FoldingRangesCount = 2