From ef3d60311b7adee9f40ebd57d6805c521b471e37 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 2 Dec 2024 19:41:52 +0000 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI --- .../internal/golang/completion/completion.go | 2 +- .../marker/testdata/completion/issue70636.txt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 gopls/internal/test/marker/testdata/completion/issue70636.txt 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 {}