Skip to content

Commit 74dea82

Browse files
findleyrgopherbot
authored andcommitted
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 <[email protected]> Auto-Submit: Robert Findley <[email protected]> Commit-Queue: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8843590 commit 74dea82

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

gopls/internal/golang/completion/completion.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ func reverseInferTypeArgs(sig *types.Signature, typeArgs []types.Type, expectedR
27082708
return substs
27092709
}
27102710

2711-
// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of it's call site.
2711+
// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of its call site.
27122712
// If successful, the inf parameter is returned with only it's objType field updated.
27132713
//
27142714
// callNodeIdx is the index within the completion path of the type parameter's parent call expression.
@@ -2722,9 +2722,12 @@ func (c *completer) inferExpectedTypeArg(callNodeIdx int, typeParamIdx int) type
27222722
if !ok {
27232723
return nil
27242724
}
2725+
sig, ok := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature)
2726+
if !ok {
2727+
return nil
2728+
}
27252729

2726-
// Infer the type parameters in a function call based on it's context
2727-
sig := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature)
2730+
// Infer the type parameters in a function call based on context
27282731
expectedResults := inferExpectedResultTypes(c, callNodeIdx)
27292732
if typeParamIdx < 0 || typeParamIdx >= sig.TypeParams().Len() {
27302733
return nil

gopls/internal/test/marker/testdata/completion/type_params.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ package typeparams
1515

1616
func one[a int | string]() {}
1717
func two[a int | string, b float64 | int]() {}
18+
type three[a any] int
1819

1920
func _() {
2021
one[]() //@rank("]", string, float64)
2122
two[]() //@rank("]", int, float64)
2223
two[int, f]() //@rank("]", float64, float32)
24+
int(three[]) //@rank("]") // must not crash (golang/go#70889)
2325
}
2426

2527
func slices[a []int | []float64]() {} //@item(tpInts, "[]int", "[]int", "type"),item(tpFloats, "[]float64", "[]float64", "type")

0 commit comments

Comments
 (0)