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: confusing errors when forgetting an import for an anonymous field #27938

Closed
mvdan opened this issue Sep 29, 2018 · 9 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Sep 29, 2018

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

go version devel +a0e7f12771 Sat Sep 29 10:07:58 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes - checked 1.11.

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mvdan/go/cache"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/mvdan/go/land:/home/mvdan/go"
GOPROXY=""
GORACE=""
GOROOT="/home/mvdan/tip"
GOTMPDIR=""
GOTOOLDIR="/home/mvdan/tip/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build472648887=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Forget to add an import for an anonymous struct field.

What did you expect to see?

What I see when the field isn't anonymous:

$ cat f.go
package p

type t struct {
        F sync.Mutex
}
$ go build f.go
# command-line-arguments
./f.go:4:4: undefined: sync

What did you see instead?

A confusing error about sync, and a redundant error about Mutex:

$ cat f.go
package p

type t struct {
        sync.Mutex
}
$ go build f.go
# command-line-arguments
./f.go:4:2: sync is not a package
./f.go:4:2: undefined: Mutex

/cc @griesemer

@mvdan mvdan added the NeedsFix The path to resolution is known, but the work has not been done. label Sep 29, 2018
@griesemer griesemer self-assigned this Sep 30, 2018
@griesemer griesemer added this to the Go1.12 milestone Sep 30, 2018
@griesemer
Copy link
Contributor

griesemer commented Sep 30, 2018

Indeed. Could use a better error message. Not urgent (ok to postpone if we don't get to it for 1.12).

@hellozee
Copy link
Contributor

hellozee commented Oct 19, 2018

I would like to give it a try, if anyone could give me some pointers?

@griesemer
Copy link
Contributor

FWIW, go/types reports: "undeclared name: sync" which seems clearer.

@hellozee Not sure if this is a great "starter issue" in the compiler as it seems pretty central to how name lookup works - but I'm just guessing; haven't looked into this yet. But basically 1) you'd have to find which part of the code reports "sync is not a package" and why it's not complaining about sync not being defined instead. And 2) mark the respective node with Broke or SetDiag so that we don't get a 2nd error.

@hellozee
Copy link
Contributor

@griesemer sure

@hellozee
Copy link
Contributor

@griesemer the files in question would be src/cmd/compile/internal/gc/noder.go , packname method of the noder structure. And src/cmd/compile/internal/gc/typecheck.go, typecheck1 function.
Am I going in the right direction?

@griesemer
Copy link
Contributor

@hellozee Yes, but it seems like this issue requires a bit more thought since the problem appears during "noding". It may be possible to avoid this with some restructuring, but that wouldn't be a "starter issue".

@hellozee
Copy link
Contributor

Ohh better to leave it to experts then, thanks for heads up though @griesemer

@gopherbot
Copy link

gopherbot commented Oct 23, 2018

Change https://golang.org/cl/144259 mentions this issue: cmd/compile: better errors for structs with conflicting fields and methods

Edit: referred to wrong issue in CL by mistake.

@gopherbot
Copy link

Change https://golang.org/cl/144261 mentions this issue: cmd/compile: better error for embedded field referring to missing import

@golang golang locked and limited conversation to collaborators Oct 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants