Skip to content
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

x/tools/gopls: reverse type inference for completions in generic function call params #69754

Closed
jacobzim-stl opened this issue Oct 3, 2024 · 3 comments
Assignees
Labels
FeatureRequest gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jacobzim-stl
Copy link

gopls version

golang.org/x/tools/gopls v0.0.0-20240919171440-cd349f34d533+dirty
golang.org/x/tools/[email protected]+dirty
github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/[email protected] h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/[email protected] h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/[email protected] h1:Wm3cG5X6sZ0RSVRc/H1/sciC4AT6HAKgLCSH2lbpR/c=
golang.org/x/[email protected] h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/[email protected] => ../
golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/[email protected] h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/[email protected] h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: devel go1.24-097b7162ad Fri Sep 20 20:19:15 2024 +0000

go env

GO111MODULE=''
GOARCH='arm64'
GOBIN='/Users/jzimmerman-stainless/go/bin'
GOCACHE='/Users/jzimmerman-stainless/Library/Caches/go-build'
GOENV='/Users/jzimmerman-stainless/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/jzimmerman-stainless/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/jzimmerman-stainless/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/jzimmerman-stainless/Desktop/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/jzimmerman-stainless/Desktop/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-097b7162ad Fri Sep 20 20:19:15 2024 +0000'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/jzimmerman-stainless/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/dd/02btngf95192vx03n6g2t8gh0000gn/T/go-build3842107580=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

This is a feature request.

When writing a generic function that returns a parameterized type in an assignment, gopls makes no "reverse type inferences" for completions in the function parameters. With reverse type inference, completions could exist for type parameters, and automatic conversion completions for interface types.

I have a video that demonstrates a patched implementation with this working

completions.mov

I measured that when using libraries that make heavy use of generics, there is a roughly 5x productivity boost after this was patched.

What did you see happen?

type Wrap[T any] struct {
      inner: *T	
}

func NewWrap[T any](x T) {
     return Wrap[T]{ inner: &x }
}

type InterfaceA interface {
        implA()
}

type TypeA struct {}

func (t TypeA) implA() {}

func main() {
	var ta TypeA
	var ia InterfaceA
	
	var w Wrap[InterfaceA]
    w = NewWrap(/* cursor here */)     // case 1
    w = NewWrap[/* cursor here */]()  // case 2
}

No special completions, just lexical

What did you expect to see?

Case 1.
I would expect completions of with the following options
ia
ta
TypeA{}

Additionally, ta and TypeA{} should have a conversion to InterfaceA(ta) or InterfaceA(TypeA{}) when they are selected.

Case 2.
This should complete to the type name InterfaceA

Editor and settings

No response

Logs

No response

@jacobzim-stl jacobzim-stl added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Oct 3, 2024
@gopherbot gopherbot added this to the Unreleased milestone Oct 3, 2024
@jacobzim-stl jacobzim-stl changed the title x/tools/gopls: issue title x/tools/gopls: reverse type inference for completions in generic function call params Oct 3, 2024
@hyangah hyangah modified the milestones: Unreleased, gopls/unplanned Oct 3, 2024
@hyangah hyangah added the gopls/completion Issues related to auto-completion in gopls. label Oct 3, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/618675 mentions this issue: completions/inference: infer polymorphic types in completions

@findleyr
Copy link
Member

Looks like this will make v0.17.0, since we plan to cut another prerelease next week 🚀 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants