go/types: improve the error message does not implement interface
by adding a hint if a method is unexported in a different package
#59831
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
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
OutputWhat 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 haveprivateMethod
.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.
The text was updated successfully, but these errors were encountered: