Skip to content

x/tools/gopls: 'implementations' doesn't work with genericsΒ #59224

Closed

Description

@adonovan

Even some of the most basic scenarios of 'implementations' are not working at all with generics, for example when the concrete and interface types are both generalized over [T any] and are defined within the same file:

xtools$ cat d/d.go 
package d

type Collection[T any] interface {
	Push(T) error
	Pop() (*T, bool)
}

type C[T any] struct{}

func (C[T]) Push(t T) error { return nil }

func (C[T]) Pop() (*T, bool) {
	var t T
	return &t, true
}

var _ Collection[int] = C[int]{}

xtools$ go run ./gopls implementation ./d/d.go:#120  # within "Push" concrete method identifier
xtools$

This is not a recent regression:

xtools$ ./gopls-v0.11.0 implementation ./d/d.go:#120  
xtools$

A similar lack of results is obtained by querying the Push abstract method, or the Collection interface, or the C concrete type.

(This test case was a further reduction of the one described in https://youtrack.jetbrains.com/issue/GO-12702/Go-to-Declaration-Implementation-not-available-for-generics-interfaces, referenced by golang/vscode-go#2711.)

Activity

  1. self-assigned this
    on Mar 24, 2023
  2. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    goplsIssues related to the Go language server, gopls.
    on Mar 24, 2023
  3. added this to the Unreleased milestone on Mar 24, 2023
  4. added
    gopls/genericsIssues related to gopls' support for generics
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    and removed
    goplsIssues related to the Go language server, gopls.
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    on Mar 24, 2023
  5. linjunhao1997 commented on Apr 1, 2023

    @linjunhao1997

    I think this is urgently needed

  6. fangjh13 commented on May 29, 2023

    @fangjh13

    I would like to know how the progress is going, it's really needed.

  7. adonovan commented on May 30, 2023

    @adonovan
    MemberAuthor

    I would like to know how the progress is going, it's really needed.

    No progress, sorry. Haven't even designed the algorithm yet. But let's make it a priority for 0.13.

  8. 14 remaining items

  9. gopherbot commented on Dec 9, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/634597 mentions this issue: internal/typeparams: delete GenericAssignableTo

  10. gopherbot commented on Dec 9, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/634596 mentions this issue: gopls/internal/golang: Implementations: support generics

  11. added a commit that references this issue on Dec 11, 2024
    c1ff179
  12. DingYuan0118 commented on Jan 24, 2025

    @DingYuan0118

    Hello, half year passed. Any progress on this one?

  13. adonovan commented on Jan 24, 2025

    @adonovan
    MemberAuthor

    Yes! Run go install golang.org/x/tools/gopls@master and let us know how this feature works for you.

  14. DingYuan0118 commented on Jan 24, 2025

    @DingYuan0118

    Thanks for the quick response.

    And I have got a question. I found that the Goland IDE already have a similar func like

    Image

    Is there any difference between it and gopls?

  15. DingYuan0118 commented on Jan 24, 2025

    @DingYuan0118

    reports

    /Users/.../go/go1.23.4/bin/go install golang.org/x/tools/gopls@master
    go: golang.org/x/tools/gopls@master (in golang.org/x/tools/gopls@v0.0.0-20250124010602-45227b6df520):
            The go.mod file for the module providing named packages contains one or
            more replace directives. It must not contain directives that would cause
            it to be interpreted differently than if it were the main module.
    
  16. adonovan commented on Jan 24, 2025

    @adonovan
    MemberAuthor

    Is there any difference between it and gopls?

    I don't use goland, so I can't answer that question. I am sure the feature is similar in the broad strokes (connecting interfaces with implementations), but it may differ in the details. For example, gopls only reports package-level types from other packages, so a type such as type T struct { io.Reader } appearing with in a function will not be found be a query for implementations of io.Reader. Also, gopls' matching algorithm for generic types and instantiations is not perfectly accurate.

    /Users/.../go/go1.23.4/bin/go install golang.org/x/tools/gopls@master ... [errors]

    Ah, sorry about that, our go.mod file uses a replace directive so you can't go install direct from master, only tagged releases.

    You'll need to git clone https://go.googlesource.com/tools and then cd gopls and go install.

  17. DingYuan0118 commented on Jan 24, 2025

    @DingYuan0118

    It works good for this demo.

    But our project can not upgrade go version so quickly and still use go1.19. So it is a little painful that I can not use this feature...

  18. adonovan commented on Jan 24, 2025

    @adonovan
    MemberAuthor

    But our project can not upgrade go version so quickly and still use go1.19. So it is a little painful that I can not use this feature...

    go1.19? That's very old: gopls v0.15 dropped support for go1.19 last April. You should find a way to upgrade your project.

  19. locked and limited conversation to collaborators on Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.gopls/genericsIssues related to gopls' support for generics

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions