diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index e52dc9b73ec..7b4abe774a4 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go @@ -2624,7 +2624,7 @@ func expectedAssignStmtTypes(pkg *cache.Package, node *ast.AssignStmt, pos token // Returns nil if enclosingSig is nil. func expectedReturnStmtType(enclosingSig *types.Signature, node *ast.ReturnStmt, pos token.Pos) types.Type { if enclosingSig != nil { - if resultIdx := exprAtPos(pos, node.Results); resultIdx < len(node.Results) { + if resultIdx := exprAtPos(pos, node.Results); resultIdx < enclosingSig.Results().Len() { return enclosingSig.Results().At(resultIdx).Type() } } diff --git a/gopls/internal/test/marker/testdata/completion/issue70636.txt b/gopls/internal/test/marker/testdata/completion/issue70636.txt new file mode 100644 index 00000000000..a684ee905aa --- /dev/null +++ b/gopls/internal/test/marker/testdata/completion/issue70636.txt @@ -0,0 +1,23 @@ +This test reproduces the crash of golang/go#70636, an out of bounds error when +analyzing a return statement with more results than the signature expects. + +-- flags -- +-ignore_extra_diags + +-- go.mod -- +module example.com + +go 1.21 + +-- p.go -- +package p + +var xx int +var xy string + + +func _() { + return Foo(x) //@ rank(re"x()", "xx", "xy") +} + +func Foo[T any](t T) T {}