lsp/completion: fix variadic param candidate ordering edge case

In cases like:

    var foo func(...interface{})
    var one int
    var two func() (int, int)

    foo(<>)

At <> we were preferring "two()" over "one" because we were really
excited that the multi return value function was usable. "one" was not
preferred because the expected value is interface{} (we default to
saying candidates _don't_ match interface{} to give non-type based
aspects of candidate inference a chance to shine).

Fix by applying the corresponding interface{} logic to the assignees
checking: ignore the case of completing into func(...interface{})
since all multi return value functions would match.

Fixes golang/go#46378.

Change-Id: I355daa75e067e8b14508ca50b8d3b6b727df5fec
Reviewed-on: https://go-review.googlesource.com/c/tools/+/323509
Run-TryBot: Muir Manders <muir@mnd.rs>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Peter Weinberger <pjw@google.com>
This commit is contained in:
Muir Manders 2021-05-28 17:39:50 -07:00 committed by Rebecca Stambler
parent 6123e5fbf2
commit df07577eb1
3 changed files with 16 additions and 1 deletions

View File

@ -2702,6 +2702,12 @@ func (ci *candidateInference) assigneesMatch(cand *candidate, sig *types.Signatu
return false
}
// Don't prefer completing into func(...interface{}) calls since all
// functions wouuld match.
if ci.variadicAssignees && len(ci.assignees) == 1 && isEmptyInterface(deslice(ci.assignees[0])) {
return false
}
var numberOfResultsCouldMatch bool
if ci.variadicAssignees {
numberOfResultsCouldMatch = sig.Results().Len() >= len(ci.assignees)-1

View File

@ -37,3 +37,12 @@ func _() {
var variadic func(int, ...int)
variadic() //@rank(")", multiF1, multiF0),rank(")", multiF2, multiF0),rank(")", multiF3, multiF0)
}
func _() {
var baz func(...interface{})
var otterNap func() (int, int) //@item(multiTwo, "otterNap", "func() (int, int)", "var")
var one int //@item(multiOne, "one", "int", "var")
baz(on) //@rank(")", multiOne, multiTwo)
}

View File

@ -6,7 +6,7 @@ CompletionSnippetCount = 95
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8
RankedCompletionsCount = 165
RankedCompletionsCount = 166
CaseSensitiveCompletionsCount = 4
DiagnosticsCount = 37
FoldingRangesCount = 2