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

Closed
sauterp opened this issue Apr 25, 2023 · 3 comments
Assignees
Labels
BadErrorMessage Issues related compiler error messages that should be better. NeedsFix The path to resolution is known, but the work has not been done.
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
@griesemer griesemer added the BadErrorMessage Issues related compiler error messages that should be better. label Dec 7, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/549298 mentions this issue: go/types, types2: better error message when missing method is unexported

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Dec 13, 2023
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
Change lookupMethod such that "foldCase" means "ignore case
and package" and analyze a lookup result further to determine
if a method name was not exported, and report a better error
message in that case.

Fixes golang#59831.

Change-Id: Ice6222e1fc00dba13caeda6c48971e8473d12da5
Reviewed-on: https://go-review.googlesource.com/c/go/+/549298
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BadErrorMessage Issues related compiler error messages that should be better. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants