Skip to content

Commit 416ad03

Browse files
findleyrgopherbot
authored andcommitted
[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 <[email protected]> Auto-Submit: Robert Findley <[email protected]> Commit-Queue: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> (cherry picked from commit 74dea82) Reviewed-on: https://go-review.googlesource.com/c/tools/+/637675
1 parent 688577c commit 416ad03

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
@@ -2690,7 +2690,7 @@ func reverseInferTypeArgs(sig *types.Signature, typeArgs []types.Type, expectedR
26902690
return substs
26912691
}
26922692

2693-
// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of it's call site.
2693+
// inferExpectedTypeArg gives a type param candidateInference based on the surroundings of its call site.
26942694
// If successful, the inf parameter is returned with only it's objType field updated.
26952695
//
26962696
// callNodeIdx is the index within the completion path of the type parameter's parent call expression.
@@ -2704,9 +2704,12 @@ func (c *completer) inferExpectedTypeArg(callNodeIdx int, typeParamIdx int) type
27042704
if !ok {
27052705
return nil
27062706
}
2707+
sig, ok := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature)
2708+
if !ok {
2709+
return nil
2710+
}
27072711

2708-
// Infer the type parameters in a function call based on it's context
2709-
sig := c.pkg.TypesInfo().Types[callNode.Fun].Type.(*types.Signature)
2712+
// Infer the type parameters in a function call based on context
27102713
expectedResults := inferExpectedResultTypes(c, callNodeIdx)
27112714
if typeParamIdx < 0 || typeParamIdx >= sig.TypeParams().Len() {
27122715
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)