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

go/types: improve the error message does not implement interface by adding a hint if a method is unexported in a different package #59831

Open
sauterp opened this issue Apr 25, 2023 · 2 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@sauterp
Copy link
Contributor

sauterp commented Apr 25, 2023

What version of Go are you using (go version)?

go1.20 linux/amd64

Does this issue reproduce with the latest release?

yes, I reproduced it with 1.20.3

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/sauterp/.cache/go-build"
GOENV="/home/sauterp/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/sauterp/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/sauterp/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build424782179=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was refactoring some code that included an unexported type implementing the unexported methods of an unexported interface like this:
https://go.dev/play/p/LVz8GjGiyuw

I moved the interface to a separate package and exported it there, but I forgot to export its method:
https://go.dev/play/p/fCCisc3I6Y0

The error message

./prog.go:12:22: cannot use &pkg.MyImplementation{} (value of type *pkg.MyImplementation) as myInterface value in variable declaration: *pkg.MyImplementation does not implement myInterface (missing method privateMethod)

This error message could be improved. IMO the statement *pkg.MyImplementation does not implement myInterface is not correct and confusing, since the code compiles if both implementation and interface are in the same package. The hint (missing method privateMethod) adds to this confusion since my implementation does have privateMethod.

I think it's correct that the code fails, since an unexported method from another package should not be available in the importing package. But we could change the error message to something like *pkg.MyImplementation cannot implement unexported method privateMethod from myInterface since they are in different packages.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 25, 2023
@dmitshur dmitshur added this to the Backlog milestone Apr 25, 2023
@dmitshur
Copy link
Contributor

CC @griesemer, @findleyr.

@griesemer griesemer self-assigned this Apr 26, 2023
@raoulvdberge
Copy link

I stumbled upon this too today, it confused me a bit.

@griesemer griesemer modified the milestones: Backlog, Go1.22 Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants