Skip to content

gopls/internal/golang/completion: don't make unnecessary conversions for generic functions #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion gopls/internal/golang/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,9 @@ Nodes:
return inf
}

if sig.TypeParams().Len() > 0 {
// Inference is necessary only when function results are generic.
var free typeparams.Free
if free.Has(sig.Results()) {
targs := c.getTypeArgs(node)
res := inferExpectedResultTypes(c, i)
substs := reverseInferTypeArgs(sig, targs, res)
Expand Down
3 changes: 3 additions & 0 deletions gopls/internal/golang/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,9 @@ func unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool {

// typeParams yields all the free type parameters within t that are relevant for
// unification.
//
// Note: this function is tailored for the specific needs of the unification algorithm.
// Don't try to use it for other purposes, see [typeparams.Free] instead.
func typeParams(t types.Type) iter.Seq[*types.TypeParam] {

return func(yield func(*types.TypeParam) bool) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- flags --
-ignore_extra_diags

-- declarations.go --
package x

import (
"cmp"
"io"
"os"
)

var File *os.File

func A[T cmp.Ordered](T) int { return 0 }

func B[T comparable](T) int { return 0 }

func C[T int | string](T) int { return 0 }

func D[T io.Reader](T) int { return 0 }

-- a.go --
package x

func _(i int) {
i = A(File.Nam) //@acceptcompletion(re"Nam()", "Name", A)
}

-- @A/a.go --
package x

func _(i int) {
i = A(File.Name()) //@acceptcompletion(re"Nam()", "Name", A)
}

-- b.go --
package x

func _(i int) {
i = B(File.Nam) //@acceptcompletion(re"Nam()", "Name", B)
}

-- @B/b.go --
package x

func _(i int) {
i = B(File.Name()) //@acceptcompletion(re"Nam()", "Name", B)
}

-- c.go --
package x

func _(i int) {
i = C(File.Nam) //@acceptcompletion(re"Nam()", "Name", C)
}

-- @C/c.go --
package x

func _(i int) {
i = C(File.Name()) //@acceptcompletion(re"Nam()", "Name", C)
}

-- d.go --
package x

func _(i int) {
i = D(Fil) //@acceptcompletion(re"Fil()", "File", D)
}

-- @D/d.go --
package x

func _(i int) {
i = D(File) //@acceptcompletion(re"Fil()", "File", D)
}