Skip to content

cmd/compile: "unknown field" error for unexported field in struct literal is misleadingΒ #31053

@cben

Description

@cben

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

$ go version
go version go1.12beta2 linux/amd64

Does this issue reproduce with the latest release?

yes, same on Playground

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/bpaskinc/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/bpaskinc/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/golang"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/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-build091900586=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main
import "net/http"
var _ = http.Server{doneChan: nil}  // this field does exist but isn't public

For a full matrix of (outside/inside package X exists/unknown X Public/private X struct literal/object.field) see: https://play.golang.org/p/cWl3JV8pSNL

What did you expect to see?

An error message indicated the problem is I'm trying to set an unexported field (from outside the package). Something like:
cannot set unexported field 'doneChan' in struct literal of type http.Server
If the field really doesn't exist, the current message is OK.

This is only about struct literals. When simply reading (or assigning) object.field, the messages already make the distinction between can't access vs really doesn't exist:
s.doneChan undefined (cannot refer to unexported field or method doneChan)
s.noSuchPrivate undefined (type http.Server has no field or method noSuchPrivate)

What did you see instead?

error message: unknown field 'doneChan' in struct literal of type http.Server
which IMHO is misleading because the field does exist, I just can't see it from this place.


This is a followup to #25727, the necessary change is probably similar. cc @odeke-em who's improved these messages before (thanks!).
I can submit a PR if nobody gets to it first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions