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

cmd/compile: misleading compiler error on missing package #53535

Open
Snaipe opened this issue Jun 24, 2022 · 1 comment
Open

cmd/compile: misleading compiler error on missing package #53535

Snaipe opened this issue Jun 24, 2022 · 1 comment
Labels
compiler/runtime NeedsInvestigation
Milestone

Comments

@Snaipe
Copy link

Snaipe commented Jun 24, 2022

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

$ go version
go version go1.18.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/snaipe/.local/var/cache/go-build"
GOENV="/home/snaipe/.local/etc/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/snaipe/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/snaipe/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.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build554584298=/tmp/go-build -gno-record-gcc-switches"

What 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:

./prog.go:8:25: cannot use W{} (value of type W) as type io.WriteCloser in variable declaration:
	W does not implement io.WriteCloser (missing Write method)
./prog.go:12:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:13:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:14:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:15:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:16:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:17:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:18:6: invalid redeclared in this block
	./prog.go:11:6: other declaration of invalid
./prog.go:21:3: undefined: bufio

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.

@heschi heschi added the NeedsInvestigation label Jul 6, 2022
@heschi heschi added this to the Backlog milestone Jul 6, 2022
@heschi
Copy link
Contributor

heschi commented Jul 6, 2022

cc @randall77 @griesemer

@gopherbot gopherbot added the compiler/runtime label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime NeedsInvestigation
Projects
Status: Triage Backlog
Development

No branches or pull requests

3 participants