-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
spec: Empty struct address comparison returns false even if addresses are equal #23440
Comments
Who is more spec-compliant in this case: gccgo or 6g? |
This is implementation defined behavior. From the spec:
Closing as it is working as intended. |
It says they may not be equal, but their values in this particular case seems to be equal. But anyway, thank you for clarification. |
@randall77, is there any reason the address comparison does not always return either true or false for empty structs? |
The spec says "may or may not be equal." Either option is permitted. This is an intentional language choice to give implementations flexibility in how they handle pointers to zero-sized objects. If every pointer to a zero-sized object were required to be different, then each allocation of a zero-sized object would have to allocate at least one byte. If every pointer to a zero-sized object were required to be the same, it would be different to handle taking the address of a zero-sized field within a larger struct. Further discussion of this should take place on a forum, not the issue tracker. See https://golang.org/wiki/Questions . Thanks. |
What version of Go are you using (
go version
)?Reproducible in Go 1.8.1, Go devel, and on playground.
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?(some paths are manually edited to avoid personal data leakage.)
What did you do?
Run this code: https://play.golang.org/p/9C0puRUstrP.
What did you expect to see?
Both functions print same address and
address comparison yields true.
What did you see instead?
Both functions print same address, but
only
f2
printstrue
for address comparison.Works as expected in GCCGO:
Attaching 6g output here for comparison:
The actual addresses does not matter. Only true/false results do.
The spec only mentions that their addresses may be identical,
which seems to be the case, but for some reason comparison disagrees.
The
false
case can be reduced:The assembly output on x86 for it is:
...which basically means
return false
.If I understand this right, unless we seen that addresses are equal by printing it or assigning it numerical representation, comparison can always return false.
Using numerical value returns true:
The text was updated successfully, but these errors were encountered: