cmd/compile: misleading compiler error on missing package #53535
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
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
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
See the following snippet: https://go.dev/play/p/t7gvzqCICZd
This program defines a type W that is intended to implement io.WriteCloser. It embeds
*bufio.Writer
(note: the snippet uses_bufio
instead because the go playground would reinsert back the missing bufio import, which defeats the example) and implements a Close method.The type W is used in the main function, and the definition of W is at the end of the file. Between the two, a handful of invalid Go code is inserted to emulate the apparition of other compiler errors during a normal development workflow.
The result of compiling the invalid program are these error messages:
The first message that the user sees complains that W does not implement WriteCloser. This is very misleading, as I thought at first that I had messed up the definition of Close (or used the wrong receiver) or the embedding of *bufio.Writer, especially since the error that shows the cause (
undefined: bufio
) appears so far from the error that results from it (cannot use W{} as type io.WriteCloser
).What did you expect to see?
I would have expected the first error message to tell me that bufio was undefined in the definition of W, or have it not be shown altogether until bufio was imported, or have it appear after the error about bufio being undefined.
It seems very weird that the Go compiler would have any idea whether W implements
io.WriteCloser
when it doesn't know what the type of its own embedded field is.The text was updated successfully, but these errors were encountered: