golang/go

x/tools/gopls: OOB index crash in completion.expectedReturnStmtType

Closed this issue · 5 comments

#!stacks
"runtime.goPanicIndex" && "go/types.(*Tuple).At" && "completion.expectedReturnStmtType"

Issue created by stacks.

// expectedReturnStmtType returns the expected type of a return statement.
// 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) {
			return enclosingSig.Results().At(resultIdx).Type() // <--- At panics
		}
	}
	return nil
}

This stack 69yMYw was reported by telemetry:

golang.org/x/tools/gopls@v0.17.0-pre.3 go1.23.2 linux/amd64 other,vscode (5)

Closely related to #70634.

This is indeed a logic bug, as described in #70634 (comment).

Change https://go.dev/cl/632936 mentions this issue: gopls/internal/golang/completion: fix crash with extra results

Change https://go.dev/cl/633706 mentions this issue: gopls/internal/golang/completion: fix crash with extra results