Skip to content

Commit ef3d603

Browse files
committed
gopls/internal/golang/completion: fix crash with extra results
Fix an incorrect condition when extracting expected result types: we must check the length of signature, not number of returns, before accessing the expected result type. Fixes golang/go#70636 Change-Id: I59d84283073c99117c40de584db6f576f2484206 Reviewed-on: https://go-review.googlesource.com/c/tools/+/632936 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8ffeaba commit ef3d603

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

gopls/internal/golang/completion/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,7 @@ func expectedAssignStmtTypes(pkg *cache.Package, node *ast.AssignStmt, pos token
26242624
// Returns nil if enclosingSig is nil.
26252625
func expectedReturnStmtType(enclosingSig *types.Signature, node *ast.ReturnStmt, pos token.Pos) types.Type {
26262626
if enclosingSig != nil {
2627-
if resultIdx := exprAtPos(pos, node.Results); resultIdx < len(node.Results) {
2627+
if resultIdx := exprAtPos(pos, node.Results); resultIdx < enclosingSig.Results().Len() {
26282628
return enclosingSig.Results().At(resultIdx).Type()
26292629
}
26302630
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This test reproduces the crash of golang/go#70636, an out of bounds error when
2+
analyzing a return statement with more results than the signature expects.
3+
4+
-- flags --
5+
-ignore_extra_diags
6+
7+
-- go.mod --
8+
module example.com
9+
10+
go 1.21
11+
12+
-- p.go --
13+
package p
14+
15+
var xx int
16+
var xy string
17+
18+
19+
func _() {
20+
return Foo(x) //@ rank(re"x()", "xx", "xy")
21+
}
22+
23+
func Foo[T any](t T) T {}

0 commit comments

Comments
 (0)