Skip to content

cmd/compile: provide a more explanatory error message on trying to take the address of an unaddressable value e.g. constants, integer and string literals #28393

Open
@deanveloper

Description

@deanveloper

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

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN="/home/24G/.bin"
GOCACHE="/home/24G/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/24G/go"
GOPROXY=""
GORACE=""
GOROOT="/snap/go/2890"
GOTMPDIR=""
GOTOOLDIR="/snap/go/2890/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-build434234612=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/uU1XNXaFYG-

What did you expect to see?

This isn't a bug report, that's exactly what I expected haha. But the purpose of this issue is to get a more helpful error message.

What did you see instead?

cannot take the address of 5

This is quite repulsive toward beginners. People who encounter this error may not understand exactly how constant values work, and what the differences between constants and normal values (after all, they look a lot like variables). Pointers are extremely hard for beginners to deal with, and changing this error message will make the language a bit more approachable.

This is especially unintuitive because &StructValue{...} is okay, but &5 is not.

I propose instead that the message is changed to cannot take the address of the constant value <const>. This should at least explain a reason why the value is not addressable.

If there are cases where this is error message change would be unhelpful, that would be important to consider.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions