x/tools/gopls: reverse type inference for completions in generic function call params
jacobzim-stl opened this issue · 2 comments
gopls version
golang.org/x/tools/gopls v0.0.0-20240919171440-cd349f34d533+dirty
golang.org/x/tools/gopls@v0.0.0-20240919171440-cd349f34d533+dirty
github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/mod@v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/sync@v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/telemetry@v0.0.0-20240829154258-f29ab539cc98 h1:Wm3cG5X6sZ0RSVRc/H1/sciC4AT6HAKgLCSH2lbpR/c=
golang.org/x/text@v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/tools@v0.21.1-0.20240508182429-e35e4ccd0d2d => ../
golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/tools@v0.4.7 h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/gofumpt@v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
mvdan.cc/xurls/v2@v2.5.0 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
Related Issues and Documentation
- x/tools/gopls: completion gives wrong lambda suggestion for pointer receivers of a generic type #61189 (closed)
- x/tools/gopls: completion and quick fix for filling in type parameters in generic types #64501
- x/tools/gopls: Autocompletion implementation should contain comments for the interface. #65543 (closed)
- x/tools/gopls: Automatic completion of functions using interface annotations #59594
- x/tools/gopls: unnamed generics parameter formatted to be unparened and named in go2go #40681 (closed)
- x/tools/gopls: improve the 'implementation' query on interfaces #68641
- x/tools/gopls: type alias rename changes aliased type #61625 (closed)
- x/tools/gopls: Implement interface methods cannot handle type if it was defined inside type() #56825 (closed)
- x/tools/gopls: feature: code action to generate a stub function from a "no function f" type error #69692
- x/tools/gopls: completion fails for incomplete function literal parameter types #36479 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Change https://go.dev/cl/618675 mentions this issue: completions/inference: infer polymorphic types in completions