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: Implement interface methods cannot handle type if it was defined inside type() #56825

Open
digitallyserviced opened this issue Nov 17, 2022 · 0 comments
Labels
gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@digitallyserviced
Copy link

digitallyserviced commented Nov 17, 2022

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?

  • Stub Methods generated to implement B on A with the methods inserted AFTER the type() closure. I actually assumed this would create methods after the current closure i happen to be in that is part global package scope. Then because the methods are inserted right next to where my cursor/file i am viewing is, I can cut/copy them to where I need.
  • A clear error result that checks for the insert location is not within a type()
  • Actually printing the file this occurs in rather than package:LINE:COL
  • Extracting the type definition to the package scope and inserting methods under that, maybe to a new file event if option/setting... Dunno

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.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 17, 2022
@gopherbot gopherbot added this to the Unreleased milestone Nov 17, 2022
@jamalc jamalc added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants