Skip to content

Commit

Permalink
gopls/internal/golang/completion: fix crash in instance conversion
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr authored and gopherbot committed Dec 18, 2024
1 parent 8843590 commit 74dea82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions gopls/internal/golang/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2708,7 +2708,7 @@ func reverseInferTypeArgs(sig *types.Signature, typeArgs []types.Type, expectedR
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.
Expand All @@ -2722,9 +2722,12 @@ func (c *completer) inferExpectedTypeArg(callNodeIdx int, typeParamIdx int) type
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ package typeparams

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")
Expand Down

0 comments on commit 74dea82

Please sign in to comment.