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: empty struct addresses are not equal #23370

Closed
mnhkahn opened this issue Jan 8, 2018 · 7 comments

Comments

Projects
None yet
8 participants
@mnhkahn
Copy link

commented Jan 8, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.2 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="/vagrant/gopath/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/vagrant/gopath"
GORACE=""
GOROOT="/vagrant/go"
GOTOOLDIR="/vagrant/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build779422972=/tmp/go-build"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

var a, b struct{}
fmt.Println(&a == &b) // true

code link: https://play.golang.org/p/zUnW3LHCCbi.

What did you expect to see?

Print true.

What did you see instead?

Print false. Go with version 1.3.3 prints true but 1.9 print false. Is this reasonable or a bug?

@geekhckr

This comment has been minimized.

Copy link

commented Jan 8, 2018

You are comparing addresses of empty structs. If you compare values, then it returns true.
Try
fmt.Println(a == b) // true

@mvdan mvdan closed this Jan 8, 2018

@davecheney

This comment has been minimized.

Copy link
Contributor

commented Jan 8, 2018

@mvdan the OPs example prints true in previous versions of Go. They want to know if this change was intentional, or a bug.

@mvdan mvdan reopened this Jan 8, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Jan 8, 2018

I did not get that impression from reading the post, my apologies.

@ALTree

This comment has been minimized.

Copy link
Member

commented Jan 8, 2018

The title is misleading. You're not comparing empty structs, you are comparing the addresses of two struct{} variables.

Also your expectations are incorrect. The two addresses does not need to be equal. The spec says:

Pointers to distinct zero-size variables may or may not be equal.

So both true and false are correct answers.

@ALTree ALTree changed the title Empty struct are not equal. cmd/compile: empty struct addresses are not equal Jan 8, 2018

@ALTree

This comment has been minimized.

Copy link
Member

commented Jan 8, 2018

I'd argue that it doesn't really matter whether the change (which btw was introduced in go1.6, the first version that prints false) was introduced intentionally or as a side effect of new optimizations in the compiler, since both answers are allowed by the spec, but I'm labelling this as needsInvestigation in case there's actually something unexpected going on.

@mindlesslcc

This comment has been minimized.

Copy link

commented Jan 8, 2018

Two distinct zero-size variables may have the same address in memory.
https://golang.org/ref/spec#Size_and_alignment_guarantees

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 8, 2018

Yes, this is working as intended per the spec, as others have pointed out.

@bradfitz bradfitz closed this Jan 8, 2018

@golang golang locked and limited conversation to collaborators Jan 8, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.