[gopls-release-branch.0.17] gopls/internal/golang/completion: fix crash in instance conversion The new inference logic assumed that a CallExpr surrounding an instance was a function, but it could be a conversion, builtin, or invalid type. Add the missing check. Fixes golang/go#70889 Change-Id: I0b05a90cca671196cf5cfd8782b6863e17485cc1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/637635 Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Robert Findley <rfindley@google.com> Commit-Queue: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> (cherry picked from commit 74dea82592ad27cc0a4954831ee4bca305ed5ebd) Reviewed-on: https://go-review.googlesource.com/c/tools/+/637675
diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index 7b4abe7..b5bf7fd 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go
@@ -2690,7 +2690,7 @@ return substs } -// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of it's call site. +// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of its call site. // If successful, the inf parameter is returned with only it's objType field updated. // // callNodeIdx is the index within the completion path of the type parameter's parent call expression. @@ -2704,9 +2704,12 @@ if !ok { return nil } + sig, ok := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature) + if !ok { + return nil + } - // Infer the type parameters in a function call based on it's context - sig := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature) + // Infer the type parameters in a function call based on context expectedResults := inferExpectedResultTypes(c, callNodeIdx) if typeParamIdx < 0 || typeParamIdx >= sig.TypeParams().Len() { return nil
diff --git a/gopls/internal/test/marker/testdata/completion/type_params.txt b/gopls/internal/test/marker/testdata/completion/type_params.txt index 8e2f5d7..12d3634 100644 --- a/gopls/internal/test/marker/testdata/completion/type_params.txt +++ b/gopls/internal/test/marker/testdata/completion/type_params.txt
@@ -15,11 +15,13 @@ func one[a int | string]() {} func two[a int | string, b float64 | int]() {} +type three[a any] int func _() { one[]() //@rank("]", string, float64) two[]() //@rank("]", int, float64) two[int, f]() //@rank("]", float64, float32) + int(three[]) //@rank("]") // must not crash (golang/go#70889) } func slices[a []int | []float64]() {} //@item(tpInts, "[]int", "[]int", "type"),item(tpFloats, "[]float64", "[]float64", "type")