golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.7k stars 17.62k forks source link

x/tools/gopls: Implement interface methods cannot handle type if it was defined inside type() #56825

Closed digitallyserviced closed 1 year ago

digitallyserviced commented 1 year ago

gopls version

:!gopls -v version
[No write since last change]
Build info
----------
golang.org/x/tools/gopls v0.10.1
    golang.org/x/tools/gopls@v0.10.1 h1:JoHe17pdZ8Vsa24/GUO8iTVTKPh0EOBiWpPop7XJybI=
    github.com/BurntSushi/toml@v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
    github.com/google/go-cmp@v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
    golang.org/x/exp/typeparams@v0.0.0-20220722155223-a9213eeb770e h1:7Xs2YCOpMlNqSQSmrrnhlzBXIE/bpMecZplbLePTJvE=
    golang.org/x/mod@v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
    golang.org/x/sync@v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
    golang.org/x/sys@v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
    golang.org/x/text@v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
    golang.org/x/tools@v0.2.1-0.20221101170700-b5bc717366b2 h1:KBm+UwBaO/tdQ35tfGvxH1FUCiXRg4MoTzkznsdeab8=
    golang.org/x/vuln@v0.0.0-20221010193109-563322be2ea9 h1:KaYZQUtEEaV8aVADIHAuYBTjo77aUcCvC7KTGKM3J1I=
    honnef.co/go/tools@v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
    mvdan.cc/gofumpt@v0.3.1 h1:avhhrOmv0IuvQVK7fvwV91oFSGAk5/6Po8GXTzICeu8=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.19.2

go env

:!go env
[No write since last change]
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/chris/.cache/go-build"
GOENV="/home/chris/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/chris/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/chris/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/chris/Documents/coolors/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2226506532=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Wrote some code that I then wanted to generate stubs for a struct to impl an interface.


package main

type(
    A struct {
        a int
    }
    B interface {
        A() int
    }
    C interface {
        B() int
    }
)

func main(){
    // define type you want to impl stubs
    a := A{1}
    // try to assign to an interface type of `B`
    // diags will indicate `A` doesnt implement `B` 
    var testImpl B = a
    // at this point a code action will be available on `a` 
    // to implement interface methods for `B` 
}

What did you expect to see?

What did you see instead?

When accepting to run the code action for implementing method stubs for the interface it fails with an error similar to.... Which is very confusing and makes no real mention or reference to the problem. Especially since we're not in a func or have one at the referenced line.

gopls: 0: could not reparse file: main:#LINENO:1: expected 'IDENT', found 'func' (and #ERRS more errors)

with the #LINENO referencing the line after the type definition for the struct (A) we are trying to generate stubs for. There is also no file name listed. If you have multiple files in the same package and the struct is in another file this error is useless.

The #ERRS I am unsure of what additional issues it is complaining about, but irrelevant to the issue/solution.

Simply moving the type definition for the struct we want to generate stub methods for outside of the type() closure resolves the issue. It would seem the stubs are generated under the struct's definition expecting them to be in the global/outside any closure.


package main

type(   
    B interface {
        A() int
    }
    C interface {
        B() int
    }
)

type A struct {
    a int
} 

func main(){
    // define type you want to impl stubs
    a := A{1}
    // try to assign to an interface type of `B`
    // diags will indicate `A` doesnt implement `B` 
    var testImpl B = a
    // at this point a code action will be available on `a` 
    // to implement interface methods for `B` 
}

Editor and settings

Neovim v0.9.0-dev All analyses and hints enabled through gopls.

Logs

THX U! I have struggled with this error for months only having recently spent a few hours to investigate why some types wouldn't work with this.

adonovan commented 1 year ago

Thanks for the bug report.

Note to us: the logic responsible for the bug is here, where it assumes the insertion point is the End of the syntax node that is the parent (nodes[1]) of the TypeName object (si.Concrete.Obj). But of course this is only the end of a TypeSpec within a parenthesized group.

gopherbot commented 1 year ago

Change https://go.dev/cl/456316 mentions this issue: gopls/internal/lsp/source: emit interface stub methods at top level

digitallyserviced commented 1 year ago

oh wow, haven't been keeping up with my notifications. I wanted to make sure I updated this and made it known that I appreciate it's attention and all con7r's of gopls and go general... you guys rock!

@adonovan... thanks for your effort to resolve, especially since you had to deal with fixing tests... That is 11x more effort than I would have put in for tests if the bug had been fixed lol